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