]> git.lizzy.rs Git - Crafter.git/blob - mods/train/init.lua
fe8775069cf9174810e927ec4dfc81f150c70e96
[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         local coupler_velocity = self.coupler1:get_velocity()
228
229         if self.axis_lock == "x" then
230                 local velocity_real = self.object:get_velocity()
231                 local distance = vector.distance(pos,pos2)
232                 local new_vel = vector.new(0,0,0)
233                 if distance > coupler_goal then
234                         local velocity = (distance-coupler_goal)*5
235                         local dir = vector.direction(vector.new(pos.x,0,0),vector.new(pos2.x,0,0))
236                         self.dir = dir
237                         new_vel = vector.multiply(dir,velocity)
238                 else
239                         if vector.equals(coupler_velocity,vector.new(0,0,0)) then
240                                 new_vel = vector.multiply(velocity_real,-1)
241                         else
242                                 new_vel = vector.multiply(velocity_real,-0.05)
243                         end
244                 end
245                 self.object:add_velocity(new_vel)
246         elseif self.axis_lock == "z" then
247                 local velocity_real = self.object:get_velocity()
248                 local distance = vector.distance(pos,pos2)
249                 local new_vel = vector.new(0,0,0)
250                 if distance > coupler_goal then
251                         local velocity = (distance-coupler_goal)*5
252                         local dir = vector.direction(vector.new(0,0,pos.z),vector.new(0,0,pos2.z))
253                         self.dir = dir
254                         new_vel = vector.multiply(dir,velocity)
255                 else
256                         if vector.equals(coupler_velocity,vector.new(0,0,0)) then
257                                 new_vel = vector.multiply(velocity_real,-1)
258                         else
259                                 new_vel = vector.multiply(velocity_real,-0.05)
260                         end
261                 end
262                 self.object:add_velocity(new_vel)
263         end
264
265         return
266 end
267
268
269 local function rail_brain(self,pos)
270
271         if not self.dir then self.dir = vector.new(0,0,0) end
272
273         local pos2 = self.object:get_pos()
274
275         local dir = self.dir
276
277         speed_limiter(self,6)
278
279         if not pool[minetest.hash_node_position(vector.add(pos,dir))] then
280
281                 if straight_snap(pos,self,dir) then
282                         return
283                 end
284
285                 local possible_dirs = create_axis(pos)
286
287                 if table.getn(possible_dirs) == 0 then
288                         --stop slow down become physical
289                 else
290                         for _,dir2 in pairs(possible_dirs) do
291                                 if turn_snap(pos,self,dir,dir2) then
292                                         return
293                                 end
294                                 if climb_snap(pos,self,dir,dir2) then
295                                         return
296                                 end
297                         end
298                 end
299         else
300                 if self.is_car then
301                         coupling_logic(self)
302                 end
303         end
304
305 end
306
307
308 --[[
309  █████╗ ██████╗ ██╗    ██████╗ ███████╗ ██████╗ ██╗███╗   ██╗
310 ██╔══██╗██╔══██╗██║    ██╔══██╗██╔════╝██╔════╝ ██║████╗  ██║
311 ███████║██████╔╝██║    ██████╔╝█████╗  ██║  ███╗██║██╔██╗ ██║
312 ██╔══██║██╔═══╝ ██║    ██╔══██╗██╔══╝  ██║   ██║██║██║╚██╗██║
313 ██║  ██║██║     ██║    ██████╔╝███████╗╚██████╔╝██║██║ ╚████║
314 ╚═╝  ╚═╝╚═╝     ╚═╝    ╚═════╝ ╚══════╝ ╚═════╝ ╚═╝╚═╝  ╚═══╝
315 ]]--
316
317
318 function register_train(name,data)
319 local train = {}
320
321 train.power            = data.power
322 train.coupler_distance = data.coupler_distance
323 train.is_car           = data.is_car
324 train.is_engine        = data.is_engine
325 train.max_speed        = data.max_speed
326 train.driver           = nil
327
328 train.initial_properties = {
329         physical = false, -- otherwise going uphill breaks
330         collisionbox = {-0.4, -0.5, -0.4, 0.4, 0.45, 0.4},
331         visual = "mesh",
332         mesh = data.mesh,
333         visual_size = {x=1, y=1},
334         textures = {data.texture},
335 }
336
337
338 train.on_step = function(self,dtime)
339         if dtime > 0.1 then
340                 self.object:set_pos(self.old_pos)
341         end
342         local pos = vector.round(self.object:get_pos())
343         if not self.axis_lock then
344                 local possible_dirs = create_axis(pos)
345                 for _,dir in pairs(possible_dirs) do
346                         if dir.x ~=0 then
347                                 self.axis_lock = "x"
348                                 self.dir = dir
349                                 direction_snap(self)
350                                 break
351                         elseif dir.z ~= 0 then
352                                 self.axis_lock = "z"
353                                 self.dir = dir
354                                 direction_snap(self)
355                                 break
356                         end
357                 end
358         else
359                 rail_brain(self,pos)
360                 --collision_detect(self)
361         end
362         self.old_pos = self.object:get_pos()
363 end
364
365
366
367
368 train.on_punch = function(self, puncher)
369         if not puncher:get_wielded_item():get_name() == "train:wrench" then
370                 return
371         end
372
373         if self.is_engine and puncher:get_player_control().sneak then
374                 if vector.equals(self.object:get_velocity(),vector.new(0,0,0)) then
375                         if self.dir.y == 0 then
376                                 self.dir = vector.multiply(self.dir,-1)
377                                 direction_snap(self)
378                                 minetest.sound_play("wrench",{
379                                         object = self.object,
380                                         gain = 1.0,
381                                         max_hear_distance = 64,
382                                 })
383                         end
384                 end
385                 return
386         end
387
388         if self.is_engine then
389                 self.object:set_velocity(vector.multiply(self.dir,self.max_speed))
390                 return
391         end
392
393         if self.coupler1 then
394                 self.coupler1:get_luaentity().coupler2 = nil
395                 self.coupler1 = nil
396         end
397
398         if self.coupler2 then
399                 self.coupler2:get_luaentity().coupler1 = nil
400                 self.coupler2 = nil
401         end
402
403 end
404
405
406 train.on_rightclick = function(self,clicker)
407         --[[
408         if clicker:get_wielded_item():get_name() == "utility:furnace" then
409                 local obj = minetest.add_entity(pos, "train:furnace")
410                 obj:set_attach(self.object,"",vector.new(0,0,0),vector.new(0,0,0))
411                 minetest.sound_play("wrench",{
412                         object = self.object,
413                         gain = 1.0,
414                         max_hear_distance = 64,
415                 })
416                 coupling_particles(pos,true)
417                 self.furnace = true
418                 return
419         end
420         ]]--
421
422         if clicker:get_wielded_item():get_name() ~= "train:wrench" then
423                 if self.is_engine then
424                         if not self.driver then
425                                 print("jump on in")
426                                 clicker:set_attach(self.object, "", data.body_pos, data.body_rotation)
427                                 clicker:set_eye_offset(data.eye_offset,{x=0,y=0,z=0})
428                                 player_is_attached(clicker,true)
429                                 set_player_animation(clicker,"stand",0)
430                                 local rotation = self.object:get_rotation()
431                                 clicker:set_look_vertical(0)
432                                 clicker:set_look_horizontal(rotation.y)
433                                 self.object:set_velocity(vector.multiply(self.dir,self.max_speed))
434                                 self.driver = clicker
435                         elseif clicker == self.driver then
436                                 print("jumpin off!")
437                                 clicker:set_detach()
438                                 clicker:set_eye_offset({x=0,y=0,z=0},{x=0,y=0,z=0})
439                                 player_is_attached(clicker,false)
440                                 set_player_animation(clicker,"stand",0)
441                                 self.object:set_velocity(vector.new(0,0,0))
442                                 self.driver = nil
443                         end
444                         return
445                 end
446                 return
447         end
448
449         local pos = self.object:get_pos()
450
451         local name = clicker:get_player_name()
452         if not pool[name] then
453                 if not self.coupler2 then
454                         pool[name] = self.object
455                         minetest.sound_play("wrench",{
456                                 object = self.object,
457                                 gain = 1.0,
458                                 max_hear_distance = 64,
459                         })
460                         coupling_particles(pos,true)
461                 else
462                         minetest.sound_play("wrench",{
463                                 object = self.object,
464                                 gain = 1.0,
465                                 max_hear_distance = 64,
466                                 pitch = 0.7,
467                         })
468                         coupling_particles(pos,false)
469                 end
470         else
471                 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
472                         self.coupler1 = pool[name]
473                         pool[name]:get_luaentity().coupler2 = self.object
474                         minetest.sound_play("wrench",{
475                                 object = self.object,
476                                 gain = 1.0,
477                                 max_hear_distance = 64,
478                         })
479                         coupling_particles(pos,true)
480                 else
481                         minetest.sound_play("wrench",{
482                                 object = self.object,
483                                 gain = 1.0,
484                                 max_hear_distance = 64,
485                                 pitch = 0.7,
486                         })
487                         coupling_particles(pos,false)
488                 end
489                 pool[name] = nil
490         end
491 end
492
493 --get old data
494 train.on_activate = function(self,staticdata, dtime_s)
495         self.object:set_armor_groups({immortal=1})
496         if string.sub(staticdata, 1, string.len("return")) ~= "return" then
497                 return
498         end
499         local data = minetest.deserialize(staticdata)
500         if type(data) ~= "table" then
501                 return
502         end
503         self.old_pos = self.object:get_pos()
504         self.velocity = vector.new(0,0,0)
505 end
506
507 train.get_staticdata = function(self)
508         return minetest.serialize({
509         })
510 end
511
512 minetest.register_entity(name, train)
513
514 end
515
516 --[[
517 ███████╗███╗   ██╗██████╗ 
518 ██╔════╝████╗  ██║██╔══██╗
519 █████╗  ██╔██╗ ██║██║  ██║
520 ██╔══╝  ██║╚██╗██║██║  ██║
521 ███████╗██║ ╚████║██████╔╝
522 ╚══════╝╚═╝  ╚═══╝╚═════╝ 
523 ]]--
524
525
526
527
528 register_train("train:steam_train",{
529         mesh = "steam_train.b3d",
530         texture = "steam_train.png",
531         is_engine = true,
532         power = 6,
533         max_speed = 6,
534         coupler_distance = 3,
535         body_pos = vector.new(0,0,-15),
536         body_rotation = vector.new(0,0,0),
537         eye_offset = vector.new(6,-1,-10)
538 })
539
540 register_train("train:minecart",{
541         mesh = "minecart.x",
542         texture = "minecart.png",
543         --is_engine = true,
544         is_car = true,
545         --power = 6,
546         max_speed = 6,
547         coupler_distance = 1.3,
548         --body_pos = vector.new(0,0,-15),
549         --body_rotation = vector.new(0,0,0),
550         --eye_offset = vector.new(6,-1,-10)
551 })
552
553
554
555 minetest.register_craftitem("train:train", {
556         description = "train",
557         inventory_image = "minecartitem.png",
558         wield_image = "minecartitem.png",
559         on_place = function(itemstack, placer, pointed_thing)
560                 if not pointed_thing.type == "node" then
561                         return
562                 end
563                 
564                 local sneak = placer:get_player_control().sneak
565                 local noddef = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
566                 if not sneak and noddef.on_rightclick then
567                         minetest.item_place(itemstack, placer, pointed_thing)
568                         return
569                 end
570                 
571                 if minetest.get_item_group(minetest.get_node(pointed_thing.under).name, "rail")>0 then
572                         minetest.add_entity(pointed_thing.under, "train:steam_train")
573                 else
574                         return
575                 end
576
577                 itemstack:take_item()
578
579                 return itemstack
580         end,
581 })
582
583 minetest.register_craft({
584         output = "train:minecart",
585         recipe = {
586                 {"main:iron", "main:iron", "main:iron"},
587                 {"main:iron", "main:iron", "main:iron"},
588         },
589 })
590
591
592 minetest.register_craftitem("train:minecart", {
593         description = "train",
594         inventory_image = "minecartitem.png",
595         wield_image = "minecartitem.png",
596         on_place = function(itemstack, placer, pointed_thing)
597                 if not pointed_thing.type == "node" then
598                         return
599                 end
600                 
601                 local sneak = placer:get_player_control().sneak
602                 local noddef = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
603                 if not sneak and noddef.on_rightclick then
604                         minetest.item_place(itemstack, placer, pointed_thing)
605                         return
606                 end
607                 
608                 if minetest.get_item_group(minetest.get_node(pointed_thing.under).name, "rail")>0 then
609                         minetest.add_entity(pointed_thing.under, "train:minecart")
610                 else
611                         return
612                 end
613
614                 itemstack:take_item()
615
616                 return itemstack
617         end,
618 })
619
620 minetest.register_craft({
621         output = "train:train",
622         recipe = {
623                 {"main:iron", "", "main:iron"},
624                 {"main:iron", "main:iron", "main:iron"},
625         },
626 })
627
628
629
630 minetest.register_node("train:rail",{
631         description = "Rail",
632         wield_image = "rail.png",
633         tiles = {
634                 "rail.png", "railcurve.png",
635                 "railt.png", "railcross.png"
636         },
637         drawtype = "raillike",
638         paramtype = "light",
639         sunlight_propagates = true,
640         is_ground_content = false,
641         walkable = false,
642         node_placement_prediction = "",
643         selection_box = {
644                 type = "fixed",
645                 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
646         },
647         sounds = main.stoneSound(),
648         after_place_node = function(pos)
649                 data_injection(pos,true)
650         end,
651         after_destruct = function(pos)
652                 data_injection(pos)
653         end,
654         groups={stone=1,wood=1,rail=1,attached_node=1},
655 })
656
657
658 minetest.register_lbm({
659         name = "train:rail",
660         nodenames = {"train:rail"},
661         run_at_every_load = true,
662         action = function(pos)
663                 data_injection(pos,true)
664         end,
665 })
666
667 minetest.register_craft({
668         output = "train:rail 32",
669         recipe = {
670                 {"main:iron","","main:iron"},
671                 {"main:iron","main:stick","main:iron"},
672                 {"main:iron","","main:iron"}
673         }
674 })
675
676
677 minetest.register_food("train:wrench",{
678         description = "Train Wrench",
679         texture = "wrench.png",
680 })
681
682 minetest.register_craft({
683         output = "train:wrench",
684         recipe = {
685                 {"main:iron", "", "main:iron"},
686                 {"main:iron", "main:lapis", "main:iron"},
687                 {"", "main:lapis", ""}
688         }
689 })
690
691
692
693 minetest.register_entity("train:furnace", {
694         initial_properties = {
695                 visual = "wielditem",
696                 visual_size = {x = 0.6, y = 0.6},
697                 textures = {},
698                 physical = true,
699                 is_visible = false,
700                 collide_with_objects = false,
701                 pointable=false,
702                 collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
703         },
704         set_node = function(self)
705                 self.object:set_properties({
706                         is_visible = true,
707                         textures = {"utility:furnace"},
708                 })
709         end,
710
711
712         on_activate = function(self, staticdata)
713                 self.object:set_armor_groups({immortal = 1})
714
715                 self:set_node()
716         end,
717 })