]> git.lizzy.rs Git - Crafter.git/blob - mods/train/init.lua
Add in ability to turn train around
[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                         if self.dir.y == 0 then
364                                 self.dir = vector.multiply(self.dir,-1)
365                                 direction_snap(self)
366                                 minetest.sound_play("wrench",{
367                                         object = self.object,
368                                         gain = 1.0,
369                                         max_hear_distance = 64,
370                                 })
371                         end
372                 end
373                 return
374         end
375
376         if self.coupler1 then
377                 self.coupler1:get_luaentity().coupler2 = nil
378                 self.coupler1 = nil
379         end
380
381         if self.coupler2 then
382                 self.coupler2:get_luaentity().coupler1 = nil
383                 self.coupler2 = nil
384         end
385
386 end
387
388
389 train.on_rightclick = function(self,clicker)
390         --[[
391         if clicker:get_wielded_item():get_name() == "utility:furnace" then
392                 local obj = minetest.add_entity(pos, "train:furnace")
393                 obj:set_attach(self.object,"",vector.new(0,0,0),vector.new(0,0,0))
394                 minetest.sound_play("wrench",{
395                         object = self.object,
396                         gain = 1.0,
397                         max_hear_distance = 64,
398                 })
399                 coupling_particles(pos,true)
400                 self.furnace = true
401                 return
402         end
403         ]]--
404
405         if clicker:get_wielded_item():get_name() ~= "train:wrench" then
406                 if self.is_engine then
407                         if not self.driver then
408                                 print("jump on in")
409                                 clicker:set_attach(self.object, "", data.body_pos, data.body_rotation)
410                                 clicker:set_eye_offset(data.eye_offset,{x=0,y=0,z=0})
411                                 player_is_attached(clicker,true)
412                                 set_player_animation(clicker,"stand",0)
413                                 local rotation = self.object:get_rotation()
414                                 clicker:set_look_vertical(0)
415                                 clicker:set_look_horizontal(rotation.y)
416                                 self.object:set_velocity(vector.multiply(self.dir,self.max_speed))
417                                 self.driver = clicker
418                         elseif clicker == self.driver then
419                                 print("jumpin off!")
420                                 clicker:set_detach()
421                                 clicker:set_eye_offset({x=0,y=0,z=0},{x=0,y=0,z=0})
422                                 player_is_attached(clicker,false)
423                                 set_player_animation(clicker,"stand",0)
424                                 self.object:set_velocity(vector.new(0,0,0))
425                                 self.driver = nil
426                         end
427                         return
428                 end
429                 return
430         end
431
432         local pos = self.object:get_pos()
433
434         local name = clicker:get_player_name()
435         if not pool[name] then
436                 if not self.coupler2 then
437                         pool[name] = self.object
438                         minetest.sound_play("wrench",{
439                                 object = self.object,
440                                 gain = 1.0,
441                                 max_hear_distance = 64,
442                         })
443                         coupling_particles(pos,true)
444                 else
445                         minetest.sound_play("wrench",{
446                                 object = self.object,
447                                 gain = 1.0,
448                                 max_hear_distance = 64,
449                                 pitch = 0.7,
450                         })
451                         coupling_particles(pos,false)
452                 end
453         else
454                 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
455                         self.coupler1 = pool[name]
456                         pool[name]:get_luaentity().coupler2 = self.object
457                         minetest.sound_play("wrench",{
458                                 object = self.object,
459                                 gain = 1.0,
460                                 max_hear_distance = 64,
461                         })
462                         coupling_particles(pos,true)
463                 else
464                         minetest.sound_play("wrench",{
465                                 object = self.object,
466                                 gain = 1.0,
467                                 max_hear_distance = 64,
468                                 pitch = 0.7,
469                         })
470                         coupling_particles(pos,false)
471                 end
472                 pool[name] = nil
473         end
474 end
475
476 --get old data
477 train.on_activate = function(self,staticdata, dtime_s)
478         self.object:set_armor_groups({immortal=1})
479         if string.sub(staticdata, 1, string.len("return")) ~= "return" then
480                 return
481         end
482         local data = minetest.deserialize(staticdata)
483         if type(data) ~= "table" then
484                 return
485         end
486         self.old_pos = self.object:get_pos()
487         self.velocity = vector.new(0,0,0)
488 end
489
490 train.get_staticdata = function(self)
491         return minetest.serialize({
492         })
493 end
494
495 minetest.register_entity(name, train)
496
497 end
498
499 --[[
500 ███████╗███╗   ██╗██████╗ 
501 ██╔════╝████╗  ██║██╔══██╗
502 █████╗  ██╔██╗ ██║██║  ██║
503 ██╔══╝  ██║╚██╗██║██║  ██║
504 ███████╗██║ ╚████║██████╔╝
505 ╚══════╝╚═╝  ╚═══╝╚═════╝ 
506 ]]--
507
508
509
510
511 register_train("train:steam_train",{
512         mesh = "steam_train.b3d",
513         texture = "steam_train.png",
514         is_engine = true,
515         power = 6,
516         max_speed = 6,
517         coupler_distance = 2,
518         body_pos = vector.new(0,0,-15),
519         body_rotation = vector.new(0,0,0),
520         eye_offset = vector.new(6,-1,-10)
521 })
522
523
524
525 minetest.register_craftitem("train:train", {
526         description = "train",
527         inventory_image = "minecartitem.png",
528         wield_image = "minecartitem.png",
529         on_place = function(itemstack, placer, pointed_thing)
530                 if not pointed_thing.type == "node" then
531                         return
532                 end
533                 
534                 local sneak = placer:get_player_control().sneak
535                 local noddef = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
536                 if not sneak and noddef.on_rightclick then
537                         minetest.item_place(itemstack, placer, pointed_thing)
538                         return
539                 end
540                 
541                 if minetest.get_item_group(minetest.get_node(pointed_thing.under).name, "rail")>0 then
542                         minetest.add_entity(pointed_thing.under, "train:steam_train")
543                 else
544                         return
545                 end
546
547                 itemstack:take_item()
548
549                 return itemstack
550         end,
551 })
552
553 minetest.register_craft({
554         output = "train:train",
555         recipe = {
556                 {"main:iron", "", "main:iron"},
557                 {"main:iron", "main:iron", "main:iron"},
558         },
559 })
560
561
562
563
564
565 minetest.register_node("train:rail",{
566         description = "Rail",
567         wield_image = "rail.png",
568         tiles = {
569                 "rail.png", "railcurve.png",
570                 "railt.png", "railcross.png"
571         },
572         drawtype = "raillike",
573         paramtype = "light",
574         sunlight_propagates = true,
575         is_ground_content = false,
576         walkable = false,
577         node_placement_prediction = "",
578         selection_box = {
579                 type = "fixed",
580                 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
581         },
582         sounds = main.stoneSound(),
583         after_place_node = function(pos)
584                 data_injection(pos,true)
585         end,
586         after_destruct = function(pos)
587                 data_injection(pos)
588         end,
589         groups={stone=1,wood=1,rail=1,attached_node=1},
590 })
591
592
593 minetest.register_lbm({
594         name = "train:rail",
595         nodenames = {"train:rail"},
596         run_at_every_load = true,
597         action = function(pos)
598                 data_injection(pos,true)
599         end,
600 })
601
602 minetest.register_craft({
603         output = "train:rail 32",
604         recipe = {
605                 {"main:iron","","main:iron"},
606                 {"main:iron","main:stick","main:iron"},
607                 {"main:iron","","main:iron"}
608         }
609 })
610
611
612 minetest.register_food("train:wrench",{
613         description = "Train Wrench",
614         texture = "wrench.png",
615 })
616
617 minetest.register_craft({
618         output = "train:wrench",
619         recipe = {
620                 {"main:iron", "", "main:iron"},
621                 {"main:iron", "main:lapis", "main:iron"},
622                 {"", "main:lapis", ""}
623         }
624 })
625
626
627
628 minetest.register_entity("train:furnace", {
629         initial_properties = {
630                 visual = "wielditem",
631                 visual_size = {x = 0.6, y = 0.6},
632                 textures = {},
633                 physical = true,
634                 is_visible = false,
635                 collide_with_objects = false,
636                 pointable=false,
637                 collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
638         },
639         set_node = function(self)
640                 self.object:set_properties({
641                         is_visible = true,
642                         textures = {"utility:furnace"},
643                 })
644         end,
645
646
647         on_activate = function(self, staticdata)
648                 self.object:set_armor_groups({immortal = 1})
649
650                 self:set_node()
651         end,
652 })