]> git.lizzy.rs Git - Crafter.git/blob - mods/train/init.lua
Add in ability to drive train
[Crafter.git] / mods / train / init.lua
1 local pool = {}
2
3 local player_pool = {}
4
5
6 local dirs = {
7         {x= 1,y= 0,z= 0},
8         {x=-1,y= 0,z= 0},
9
10         {x= 1,y= 1,z= 0}, 
11         {x=-1,y= 1,z= 0},
12
13         {x= 1,y=-1,z= 0},
14         {x=-1,y=-1,z= 0},
15
16         {x= 0,y= 0,z= 1},
17         {x= 0,y= 0,z=-1},
18
19         {x= 0,y= 1,z= 1},
20         {x= 0,y= 1,z=-1},
21
22         {x= 0,y=-1,z= 1},
23         {x= 0,y=-1,z=-1},
24 }
25
26 local function coupling_particles(pos,truth)
27         local color = "red"
28         if truth then
29                 color = "green"
30         end
31
32         minetest.add_particlespawner({
33                 amount = 15,
34                 time = 0.001,
35                 minpos = pos,
36                 maxpos = pos,
37                 minvel = vector.new(-10,-10,-10),
38                 maxvel = vector.new(10,10,10),
39                 minacc = {x=0, y=0, z=0},
40                 maxacc = {x=0, y=0, z=0},
41                 minexptime = 1.1,
42                 maxexptime = 1.5,
43                 minsize = 1,
44                 maxsize = 2,
45                 collisiondetection = false,
46                 collision_removal = false,
47                 vertical = false,
48                 texture = "couple_particle.png^[colorize:"..color..":200",
49                 glow = 14,
50         })
51 end
52
53 local function data_injection(pos,data)
54         if data then
55                 pool[minetest.hash_node_position(pos)] = true
56         else
57                 pool[minetest.hash_node_position(pos)] = nil
58         end
59 end
60
61 local function speed_limiter(self,speed)
62         local test = self.object:get_velocity()--vector.multiply(self.velocity,new_vel)
63
64         if test.x > speed then
65                 test.x = speed
66         elseif test.x < -speed then
67                 test.x = -speed
68         end
69         if test.z > speed then
70                 test.z = speed
71         elseif test.z < -speed then
72                 test.z = -speed         
73         end
74         self.object:set_velocity(test)
75 end
76
77 local function create_axis(pos)
78         local possible_dirs = {}
79         for _,dir in pairs(dirs) do
80                 local pos2 = vector.add(pos,dir)
81                 if pool[minetest.hash_node_position(pos2)] then
82                         table.insert(possible_dirs,dir)
83                 end
84         end
85         return(possible_dirs)
86 end
87
88 local function collision_detect(self)
89         if not self.axis_lock then return end
90         local pos = self.object:get_pos()
91         for _,object in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
92                 if object:is_player() then
93                         local pos2 = object:get_pos()
94                         if self.axis_lock == "x" then
95
96                                 local velocity = (1-vector.distance(vector.new(pos.x,0,0),vector.new(pos2.x,0,0)))*5
97                                 local dir = vector.direction(vector.new(pos2.x,0,0),vector.new(pos.x,0,0))
98                                 local new_vel = vector.multiply(dir,velocity)
99                                 self.object:add_velocity(new_vel)
100                                 self.dir = dir
101                         elseif self.axis_lock == "z" then
102                                 local velocity = (1-vector.distance(vector.new(0,0,pos.z),vector.new(0,0,pos2.z)))*5
103                                 local dir = vector.direction(vector.new(0,0,pos2.z),vector.new(0,0,pos.z))
104                                 local new_vel = vector.multiply(dir,velocity)
105                                 self.object:add_velocity(new_vel)
106                                 self.dir = dir
107                         end
108                         return
109                 end
110         end
111 end
112
113 local function direction_snap(self)
114         local dir = self.dir
115         local pitch = 0
116         if dir.y == 1 then pitch = math.pi/4 end
117         if dir.y == -1 then pitch = -math.pi/4 end
118
119         local yaw = minetest.dir_to_yaw(dir)
120
121         if self.driver then
122                 self.driver:set_look_vertical(pitch)
123                 self.driver:set_look_horizontal(yaw)
124         end
125         self.object:set_rotation(vector.new(pitch,yaw,0))
126
127         
128 end
129
130 local function turn_snap(pos,self,dir,dir2)
131         if self.axis_lock == "x" then
132                 if dir.x ~= 0 and dir2.z ~= 0 then
133                         local velocity = self.object:get_velocity()
134                         local inertia = math.abs(velocity.x)
135                         self.object:set_velocity(vector.multiply(dir2,inertia))
136                         self.dir = dir2
137                         self.axis_lock = "z"
138                         self.object:set_pos(pos)
139                         direction_snap(self)
140                         return(true)
141                 end
142         end
143         if self.axis_lock == "z" then
144                 if dir.z ~= 0 and dir2.x ~= 0 then
145                         local velocity = self.object:get_velocity()
146                         local inertia = math.abs(velocity.z)
147                         self.object:set_velocity(vector.multiply(dir2,inertia))
148                         self.dir = dir2
149                         self.axis_lock = "x"
150                         self.object:set_pos(pos)
151                         direction_snap(self)
152                         return(true)
153                 end
154         end
155         return(false)
156 end
157
158 local function climb_snap(pos,self,dir,dir2)
159         if self.axis_lock == "x" then
160                 if dir.x == dir2.x and dir2.y ~= 0 then
161                         local velocity = self.object:get_velocity()
162                         local inertia = math.abs(velocity.x)
163                         self.object:set_velocity(vector.multiply(dir2,inertia))
164                         self.dir = dir2
165                         self.axis_lock = "x"
166                         self.object:set_pos(pos)
167                         direction_snap(self)
168                         return(true)
169                 end
170         end
171         if self.axis_lock == "z" then
172                 if dir.z == dir2.z and dir2.y ~= 0 then
173                         local velocity = self.object:get_velocity()
174                         local inertia = math.abs(velocity.z)
175                         self.object:set_velocity(vector.multiply(dir2,inertia))
176                         self.dir = dir2
177                         self.axis_lock = "z"
178                         self.object:set_pos(pos)
179                         direction_snap(self)
180                         return(true)
181                 end
182         end
183         return(false)
184 end
185
186 local function straight_snap(pos,self,dir)
187         if self.axis_lock == "x" then
188                 if dir.x ~= 0 and pool[minetest.hash_node_position(vector.add(pos,vector.new(dir.x,0,0)))] then
189                         local velocity = self.object:get_velocity()
190                         self.object:set_velocity(vector.new(velocity.x,0,0))
191                         self.dir = vector.new(dir.x,0,0)
192                         self.axis_lock = "x"
193                         self.object:set_pos(pos)
194                         direction_snap(self)
195                         return(true)
196                 end
197         end
198         if self.axis_lock == "z" then
199                 if dir.z ~= 0 and pool[minetest.hash_node_position(vector.add(pos,vector.new(0,0,dir.z)))] then
200                         local velocity = self.object:get_velocity()
201                         self.object:set_velocity(vector.new(0,0,velocity.z))
202                         self.dir = vector.new(0,0,dir.z)
203                         self.axis_lock = "z"
204                         self.object:set_pos(pos)
205                         direction_snap(self)
206                         return(true)
207                 end
208         end
209         return(false)
210 end
211
212
213 local function coupling_logic(self)
214         
215         if not self.axis_lock then return end
216
217         if not self.coupler1 then return end
218
219         if self.dir.y ~= 0 then return end
220
221         local pos = self.object:get_pos()
222         
223         local pos2 = self.coupler1:get_pos()
224
225         local coupler_goal = self.coupler1:get_luaentity().coupler_distance
226
227         if self.axis_lock == "x" then
228                 local velocity_real = self.object:get_velocity()
229                 local distance = vector.distance(pos,pos2)
230                 local new_vel = vector.new(0,0,0)
231                 if distance > coupler_goal then
232                         local velocity = (distance-coupler_goal)
233                         local dir = vector.direction(vector.new(pos.x,0,0),vector.new(pos2.x,0,0))
234                         self.dir = dir
235                         new_vel = vector.multiply(dir,velocity)
236                 else
237                         new_vel = vector.multiply(velocity_real,-1)
238                 end
239                 self.object:add_velocity(new_vel)
240         elseif self.axis_lock == "z" then
241                 local velocity_real = self.object:get_velocity()
242                 local distance = vector.distance(pos,pos2)
243                 local new_vel = vector.new(0,0,0)
244                 if distance > coupler_goal then
245                         local velocity = (distance-coupler_goal)
246                         local dir = vector.direction(vector.new(0,0,pos.z),vector.new(0,0,pos2.z))
247                         self.dir = dir
248                         new_vel = vector.multiply(dir,velocity)
249                 else
250                         new_vel = vector.multiply(velocity_real,-1)
251                 end
252                 self.object:add_velocity(new_vel)
253         end
254
255         return
256 end
257
258
259 local function rail_brain(self,pos)
260
261         if not self.dir then self.dir = vector.new(0,0,0) end
262
263         local pos2 = self.object:get_pos()
264
265         local dir = self.dir
266
267         speed_limiter(self,6)
268
269         if not pool[minetest.hash_node_position(vector.add(pos,dir))] then
270
271                 if straight_snap(pos,self,dir) then
272                         return
273                 end
274
275                 local possible_dirs = create_axis(pos)
276
277                 if table.getn(possible_dirs) == 0 then
278                         --stop slow down become physical
279                 else
280                         for _,dir2 in pairs(possible_dirs) do
281                                 if turn_snap(pos,self,dir,dir2) then
282                                         return
283                                 end
284                                 if climb_snap(pos,self,dir,dir2) then
285                                         return
286                                 end
287                         end
288                 end
289         else
290                 coupling_logic(self)
291         end
292
293 end
294
295
296 --[[
297  █████╗ ██████╗ ██╗    ██████╗ ███████╗ ██████╗ ██╗███╗   ██╗
298 ██╔══██╗██╔══██╗██║    ██╔══██╗██╔════╝██╔════╝ ██║████╗  ██║
299 ███████║██████╔╝██║    ██████╔╝█████╗  ██║  ███╗██║██╔██╗ ██║
300 ██╔══██║██╔═══╝ ██║    ██╔══██╗██╔══╝  ██║   ██║██║██║╚██╗██║
301 ██║  ██║██║     ██║    ██████╔╝███████╗╚██████╔╝██║██║ ╚████║
302 ╚═╝  ╚═╝╚═╝     ╚═╝    ╚═════╝ ╚══════╝ ╚═════╝ ╚═╝╚═╝  ╚═══╝
303 ]]--
304
305
306 function register_train(name,data)
307 local train = {}
308
309 train.power            = data.power
310 train.coupler_distance = data.coupler_distance
311 train.is_car           = data.is_car
312 train.is_engine        = data.is_engine
313 train.max_speed        = data.max_speed
314 train.driver           = nil
315
316 train.initial_properties = {
317         physical = false, -- otherwise going uphill breaks
318         collisionbox = {-0.4, -0.5, -0.4, 0.4, 0.45, 0.4},
319         visual = "mesh",
320         mesh = data.mesh,
321         visual_size = {x=1, y=1},
322         textures = {data.texture},
323 }
324
325
326 train.on_step = function(self,dtime)
327         if dtime > 0.1 then
328                 self.object:set_pos(self.old_pos)
329         end
330         local pos = vector.round(self.object:get_pos())
331         if not self.axis_lock then
332                 local possible_dirs = create_axis(pos)
333                 for _,dir in pairs(possible_dirs) do
334                         if dir.x ~=0 then
335                                 self.axis_lock = "x"
336                                 self.dir = dir
337                                 direction_snap(self)
338                                 break
339                         elseif dir.z ~= 0 then
340                                 self.axis_lock = "z"
341                                 self.dir = dir
342                                 direction_snap(self)
343                                 break
344                         end
345                 end
346         else
347                 rail_brain(self,pos)
348                 --collision_detect(self)
349         end
350         self.old_pos = self.object:get_pos()
351 end
352
353
354
355
356 train.on_punch = function(self, puncher)
357         if not puncher:get_wielded_item():get_name() == "train:wrench" then
358                 return
359         end
360
361         if self.is_engine and puncher:get_player_control().sneak then
362                 if vector.equals(self.object:get_velocity(),vector.new(0,0,0)) then
363                         print("reverse direction")
364                 end
365                 return
366         end
367
368         if self.coupler1 then
369                 self.coupler1:get_luaentity().coupler2 = nil
370                 self.coupler1 = nil
371         end
372
373         if self.coupler2 then
374                 self.coupler2:get_luaentity().coupler1 = nil
375                 self.coupler2 = nil
376         end
377
378 end
379
380
381 train.on_rightclick = function(self,clicker)
382         --[[
383         if clicker:get_wielded_item():get_name() == "utility:furnace" then
384                 local obj = minetest.add_entity(pos, "train:furnace")
385                 obj:set_attach(self.object,"",vector.new(0,0,0),vector.new(0,0,0))
386                 minetest.sound_play("wrench",{
387                         object = self.object,
388                         gain = 1.0,
389                         max_hear_distance = 64,
390                 })
391                 coupling_particles(pos,true)
392                 self.furnace = true
393                 return
394         end
395         ]]--
396
397         if clicker:get_wielded_item():get_name() ~= "train:wrench" then
398                 if self.is_engine then
399                         print("jump on in")
400                         clicker:set_attach(self.object, "", data.body_pos, data.body_rotation)
401                         clicker:set_eye_offset(data.eye_offset,{x=0,y=0,z=0})
402                         player_is_attached(clicker,true)
403                         set_player_animation(clicker,"stand",0)
404                         local rotation = self.object:get_rotation()
405                         clicker:set_look_vertical(0)
406                         clicker:set_look_horizontal(rotation.y)
407                         self.object:set_velocity(vector.multiply(self.dir,self.max_speed))
408                         self.driver = clicker
409                         return
410                 end
411                 return
412         end
413
414         local pos = self.object:get_pos()
415
416         local name = clicker:get_player_name()
417         if not pool[name] then
418                 if not self.coupler2 then
419                         pool[name] = self.object
420                         minetest.sound_play("wrench",{
421                                 object = self.object,
422                                 gain = 1.0,
423                                 max_hear_distance = 64,
424                         })
425                         coupling_particles(pos,true)
426                 else
427                         minetest.sound_play("wrench",{
428                                 object = self.object,
429                                 gain = 1.0,
430                                 max_hear_distance = 64,
431                                 pitch = 0.7,
432                         })
433                         coupling_particles(pos,false)
434                 end
435         else
436                 if not self.is_engine and pool[name] ~= self.object and not (pool[name]:get_luaentity().coupler1 and pool[name]:get_luaentity().coupler1 == self.object or self.coupler2) then
437                         self.coupler1 = pool[name]
438                         pool[name]:get_luaentity().coupler2 = self.object
439                         minetest.sound_play("wrench",{
440                                 object = self.object,
441                                 gain = 1.0,
442                                 max_hear_distance = 64,
443                         })
444                         coupling_particles(pos,true)
445                 else
446                         minetest.sound_play("wrench",{
447                                 object = self.object,
448                                 gain = 1.0,
449                                 max_hear_distance = 64,
450                                 pitch = 0.7,
451                         })
452                         coupling_particles(pos,false)
453                 end
454                 pool[name] = nil
455         end
456 end
457
458 --get old data
459 train.on_activate = function(self,staticdata, dtime_s)
460         self.object:set_armor_groups({immortal=1})
461         if string.sub(staticdata, 1, string.len("return")) ~= "return" then
462                 return
463         end
464         local data = minetest.deserialize(staticdata)
465         if type(data) ~= "table" then
466                 return
467         end
468         self.old_pos = self.object:get_pos()
469         self.velocity = vector.new(0,0,0)
470 end
471
472 train.get_staticdata = function(self)
473         return minetest.serialize({
474         })
475 end
476
477 minetest.register_entity(name, train)
478
479 end
480
481 --[[
482 ███████╗███╗   ██╗██████╗ 
483 ██╔════╝████╗  ██║██╔══██╗
484 █████╗  ██╔██╗ ██║██║  ██║
485 ██╔══╝  ██║╚██╗██║██║  ██║
486 ███████╗██║ ╚████║██████╔╝
487 ╚══════╝╚═╝  ╚═══╝╚═════╝ 
488 ]]--
489
490
491
492
493 register_train("train:steam_train",{
494         mesh = "steam_train.b3d",
495         texture = "steam_train.png",
496         is_engine = true,
497         power = 6,
498         max_speed = 6,
499         coupler_distance = 2,
500         body_pos = vector.new(0,0,-15),
501         body_rotation = vector.new(0,0,0),
502         eye_offset = vector.new(6,-1,-10)
503 })
504
505
506
507 minetest.register_craftitem("train:train", {
508         description = "train",
509         inventory_image = "minecartitem.png",
510         wield_image = "minecartitem.png",
511         on_place = function(itemstack, placer, pointed_thing)
512                 if not pointed_thing.type == "node" then
513                         return
514                 end
515                 
516                 local sneak = placer:get_player_control().sneak
517                 local noddef = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
518                 if not sneak and noddef.on_rightclick then
519                         minetest.item_place(itemstack, placer, pointed_thing)
520                         return
521                 end
522                 
523                 if minetest.get_item_group(minetest.get_node(pointed_thing.under).name, "rail")>0 then
524                         minetest.add_entity(pointed_thing.under, "train:steam_train")
525                 else
526                         return
527                 end
528
529                 itemstack:take_item()
530
531                 return itemstack
532         end,
533 })
534
535 minetest.register_craft({
536         output = "train:train",
537         recipe = {
538                 {"main:iron", "", "main:iron"},
539                 {"main:iron", "main:iron", "main:iron"},
540         },
541 })
542
543
544
545
546
547 minetest.register_node("train:rail",{
548         description = "Rail",
549         wield_image = "rail.png",
550         tiles = {
551                 "rail.png", "railcurve.png",
552                 "railt.png", "railcross.png"
553         },
554         drawtype = "raillike",
555         paramtype = "light",
556         sunlight_propagates = true,
557         is_ground_content = false,
558         walkable = false,
559         node_placement_prediction = "",
560         selection_box = {
561                 type = "fixed",
562                 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
563         },
564         sounds = main.stoneSound(),
565         after_place_node = function(pos)
566                 data_injection(pos,true)
567         end,
568         after_destruct = function(pos)
569                 data_injection(pos)
570         end,
571         groups={stone=1,wood=1,rail=1,attached_node=1},
572 })
573
574
575 minetest.register_lbm({
576         name = "train:rail",
577         nodenames = {"train:rail"},
578         run_at_every_load = true,
579         action = function(pos)
580                 data_injection(pos,true)
581         end,
582 })
583
584 minetest.register_craft({
585         output = "train:rail 32",
586         recipe = {
587                 {"main:iron","","main:iron"},
588                 {"main:iron","main:stick","main:iron"},
589                 {"main:iron","","main:iron"}
590         }
591 })
592
593
594 minetest.register_food("train:wrench",{
595         description = "Train Wrench",
596         texture = "wrench.png",
597 })
598
599 minetest.register_craft({
600         output = "train:wrench",
601         recipe = {
602                 {"main:iron", "", "main:iron"},
603                 {"main:iron", "main:lapis", "main:iron"},
604                 {"", "main:lapis", ""}
605         }
606 })
607
608
609
610 minetest.register_entity("train:furnace", {
611         initial_properties = {
612                 visual = "wielditem",
613                 visual_size = {x = 0.6, y = 0.6},
614                 textures = {},
615                 physical = true,
616                 is_visible = false,
617                 collide_with_objects = false,
618                 pointable=false,
619                 collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
620         },
621         set_node = function(self)
622                 self.object:set_properties({
623                         is_visible = true,
624                         textures = {"utility:furnace"},
625                 })
626         end,
627
628
629         on_activate = function(self, staticdata)
630                 self.object:set_armor_groups({immortal = 1})
631
632                 self:set_node()
633         end,
634 })