]> git.lizzy.rs Git - Crafter.git/blob - mods/train/init.lua
Turn "minecart" mod into "train" mod
[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         local yaw = minetest.dir_to_yaw(dir)
119         self.object:set_rotation(vector.new(pitch,yaw,0))
120 end
121
122 local function turn_snap(pos,self,dir,dir2)
123         if self.axis_lock == "x" then
124                 if dir.x ~= 0 and dir2.z ~= 0 then
125                         local velocity = self.object:get_velocity()
126                         local inertia = math.abs(velocity.x)
127                         self.object:set_velocity(vector.multiply(dir2,inertia))
128                         self.dir = dir2
129                         self.axis_lock = "z"
130                         self.object:set_pos(pos)
131                         direction_snap(self)
132                         return(true)
133                 end
134         end
135         if self.axis_lock == "z" then
136                 if dir.z ~= 0 and dir2.x ~= 0 then
137                         local velocity = self.object:get_velocity()
138                         local inertia = math.abs(velocity.z)
139                         self.object:set_velocity(vector.multiply(dir2,inertia))
140                         self.dir = dir2
141                         self.axis_lock = "x"
142                         self.object:set_pos(pos)
143                         direction_snap(self)
144                         return(true)
145                 end
146         end
147         return(false)
148 end
149
150 local function climb_snap(pos,self,dir,dir2)
151         if self.axis_lock == "x" then
152                 if dir.x == dir2.x and dir2.y ~= 0 then
153                         local velocity = self.object:get_velocity()
154                         local inertia = math.abs(velocity.x)
155                         self.object:set_velocity(vector.multiply(dir2,inertia))
156                         self.dir = dir2
157                         self.axis_lock = "x"
158                         self.object:set_pos(pos)
159                         direction_snap(self)
160                         return(true)
161                 end
162         end
163         if self.axis_lock == "z" then
164                 if dir.z == dir2.z and dir2.y ~= 0 then
165                         local velocity = self.object:get_velocity()
166                         local inertia = math.abs(velocity.z)
167                         self.object:set_velocity(vector.multiply(dir2,inertia))
168                         self.dir = dir2
169                         self.axis_lock = "z"
170                         self.object:set_pos(pos)
171                         direction_snap(self)
172                         return(true)
173                 end
174         end
175         return(false)
176 end
177
178 local function straight_snap(pos,self,dir)
179         if self.axis_lock == "x" then
180                 if dir.x ~= 0 and pool[minetest.hash_node_position(vector.add(pos,vector.new(dir.x,0,0)))] then
181                         local velocity = self.object:get_velocity()
182                         self.object:set_velocity(vector.new(velocity.x,0,0))
183                         self.dir = vector.new(dir.x,0,0)
184                         self.axis_lock = "x"
185                         self.object:set_pos(pos)
186                         direction_snap(self)
187                         return(true)
188                 end
189         end
190         if self.axis_lock == "z" then
191                 if dir.z ~= 0 and pool[minetest.hash_node_position(vector.add(pos,vector.new(0,0,dir.z)))] then
192                         local velocity = self.object:get_velocity()
193                         self.object:set_velocity(vector.new(0,0,velocity.z))
194                         self.dir = vector.new(0,0,dir.z)
195                         self.axis_lock = "z"
196                         self.object:set_pos(pos)
197                         direction_snap(self)
198                         return(true)
199                 end
200         end
201         return(false)
202 end
203
204
205 local function coupling_logic(self)
206         
207         if not self.axis_lock then return end
208
209         if not self.coupler1 then return end
210
211         if self.dir.y ~= 0 then return end
212
213         local pos = self.object:get_pos()
214         
215         local pos2 = self.coupler1:get_pos()
216
217         if self.axis_lock == "x" then
218                 local velocity_real = self.object:get_velocity()
219                 local distance = vector.distance(pos,pos2)
220                 local new_vel = vector.new(0,0,0)
221                 if distance > 1.5 then
222                         local velocity = (distance-1)
223                         local dir = vector.direction(vector.new(pos.x,0,0),vector.new(pos2.x,0,0))
224                         self.dir = dir
225                         new_vel = vector.multiply(dir,velocity)
226                 else
227                         new_vel = vector.multiply(velocity_real,-1)
228                 end
229                 self.object:add_velocity(new_vel)
230         elseif self.axis_lock == "z" then
231                 local velocity_real = self.object:get_velocity()
232                 local distance = vector.distance(pos,pos2)
233                 local new_vel = vector.new(0,0,0)
234                 if distance > 1.5 then
235                         local velocity = (distance-1)
236                         local dir = vector.direction(vector.new(0,0,pos.z),vector.new(0,0,pos2.z))
237                         self.dir = dir
238                         new_vel = vector.multiply(dir,velocity)
239                 else
240                         new_vel = vector.multiply(velocity_real,-1)
241                 end
242                 self.object:add_velocity(new_vel)
243         end
244
245         return
246 end
247
248
249 local function rail_brain(self,pos)
250
251         if not self.dir then self.dir = vector.new(0,0,0) end
252
253         local pos2 = self.object:get_pos()
254
255         local dir = self.dir
256
257         speed_limiter(self,6)
258
259         if not pool[minetest.hash_node_position(vector.add(pos,dir))] then
260
261                 if straight_snap(pos,self,dir) then
262                         return
263                 end
264
265                 local possible_dirs = create_axis(pos)
266
267                 if table.getn(possible_dirs) == 0 then
268                         --stop slow down become physical
269                 else
270                         for _,dir2 in pairs(possible_dirs) do
271                                 if turn_snap(pos,self,dir,dir2) then
272                                         return
273                                 end
274                                 if climb_snap(pos,self,dir,dir2) then
275                                         return
276                                 end
277                         end
278                 end
279         else
280                 coupling_logic(self)
281         end
282
283 end
284
285 function register_train(name,data)
286
287         local train = {}
288
289         train.on_step = function(self,dtime)
290                 if dtime > 0.1 then
291                         self.object:set_pos(self.old_pos)
292                 end
293                 local pos = vector.round(self.object:get_pos())
294                 if not self.axis_lock then
295                         local possible_dirs = create_axis(pos)
296                         for _,dir in pairs(possible_dirs) do
297                                 if dir.x ~=0 then
298                                         self.axis_lock = "x"
299                                         self.dir = dir
300                                         direction_snap(self)
301                                         break
302                                 elseif dir.z ~= 0 then
303                                         self.axis_lock = "z"
304                                         self.dir = dir
305                                         direction_snap(self)
306                                         break
307                                 end
308                         end
309                 else
310                         rail_brain(self,pos)
311                         --collision_detect(self)
312                 end
313                 self.old_pos = self.object:get_pos()
314         end
315
316
317         train.on_punch = function(self, puncher)
318                 if not puncher:get_wielded_item():get_name() == "train:wrench" then
319                         return
320                 end
321                 if self.furnace then
322                         self.object:set_velocity(vector.multiply(self.dir,6))
323                         minetest.add_particlespawner({
324                                 amount = 30,
325                                 time = 0,
326                                 minpos = vector.new(0,0.5,0),
327                                 maxpos = vector.new(0,0.5,0),
328                                 minvel = vector.new(0,0,0),
329                                 maxvel = vector.new(0,0,0),
330                                 minacc = {x=0, y=3, z=0},
331                                 maxacc = {x=0, y=5, z=0},
332                                 minexptime = 1.1,
333                                 maxexptime = 1.5,
334                                 minsize = 1,
335                                 maxsize = 2,
336                                 collisiondetection = false,
337                                 collision_removal = false,
338                                 vertical = false,
339                                 texture = "smoke.png",
340                                 attached = self.object
341                         })
342                 end
343
344         end
345
346
347         train.on_rightclick = function(self,clicker)
348                 local pos = self.object:get_pos()
349                 if clicker:get_wielded_item():get_name() == "utility:furnace" then
350                         local obj = minetest.add_entity(pos, "train:furnace")
351                         obj:set_attach(self.object,"",vector.new(0,0,0),vector.new(0,0,0))
352                         minetest.sound_play("wrench",{
353                                 object = self.object,
354                                 gain = 1.0,
355                                 max_hear_distance = 64,
356                         })
357                         coupling_particles(pos,true)
358                         self.furnace = true
359                         return
360                 end
361
362                 if not clicker:get_wielded_item():get_name() == "train:wrench" then
363                         return
364                 end
365
366                 local name = clicker:get_player_name()
367                 if not pool[name] then
368                         if not self.coupler2 then
369                                 pool[name] = self.object
370                                 minetest.sound_play("wrench",{
371                                         object = self.object,
372                                         gain = 1.0,
373                                         max_hear_distance = 64,
374                                 })
375                                 coupling_particles(pos,true)
376                         else
377                                 minetest.sound_play("wrench",{
378                                         object = self.object,
379                                         gain = 1.0,
380                                         max_hear_distance = 64,
381                                         pitch = 0.7,
382                                 })
383                                 coupling_particles(pos,false)
384                         end
385                 else
386                         if pool[name] ~= self.object and not (pool[name]:get_luaentity().coupler1 and pool[name]:get_luaentity().coupler1 == self.object or self.coupler2) then
387                                 self.coupler1 = pool[name]
388                                 pool[name]:get_luaentity().coupler2 = self.object
389                                 minetest.sound_play("wrench",{
390                                         object = self.object,
391                                         gain = 1.0,
392                                         max_hear_distance = 64,
393                                 })
394                                 coupling_particles(pos,true)
395                         else
396                                 minetest.sound_play("wrench",{
397                                         object = self.object,
398                                         gain = 1.0,
399                                         max_hear_distance = 64,
400                                         pitch = 0.7,
401                                 })
402                                 coupling_particles(pos,false)
403                         end
404                         pool[name] = nil
405                 end
406         end
407
408         --get old data
409         train.on_activate = function(self,staticdata, dtime_s)
410                 self.object:set_armor_groups({immortal=1})
411                 if string.sub(staticdata, 1, string.len("return")) ~= "return" then
412                         return
413                 end
414                 local data = minetest.deserialize(staticdata)
415                 if type(data) ~= "table" then
416                         return
417                 end
418                 self.old_pos = self.object:get_pos()
419                 self.velocity = vector.new(0,0,0)
420         end
421
422         train.get_staticdata = function(self)
423                 return minetest.serialize({
424                 })
425         end
426
427
428
429         train.initial_properties = {
430                 physical = false, -- otherwise going uphill breaks
431                 collisionbox = {-0.4, -0.5, -0.4, 0.4, 0.45, 0.4},--{-0.5, -0.4, -0.5, 0.5, 0.25, 0.5},
432                 visual = "mesh",
433                 mesh = "steam_train.b3d",
434                 visual_size = {x=1, y=1},
435                 textures = {"steam_train.png"},
436         }
437
438                 
439
440         minetest.register_entity(name, train)
441
442 end
443
444
445
446
447
448
449
450
451
452
453 minetest.register_craftitem("train:minecart", {
454         description = "train",
455         inventory_image = "minecartitem.png",
456         wield_image = "minecartitem.png",
457         on_place = function(itemstack, placer, pointed_thing)
458                 if not pointed_thing.type == "node" then
459                         return
460                 end
461                 
462                 local sneak = placer:get_player_control().sneak
463                 local noddef = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
464                 if not sneak and noddef.on_rightclick then
465                         minetest.item_place(itemstack, placer, pointed_thing)
466                         return
467                 end
468                 
469                 if minetest.get_item_group(minetest.get_node(pointed_thing.under).name, "rail")>0 then
470                         minetest.add_entity(pointed_thing.under, "train:train")
471                 else
472                         return
473                 end
474
475                 itemstack:take_item()
476
477                 return itemstack
478         end,
479 })
480
481 minetest.register_craft({
482         output = "train:train",
483         recipe = {
484                 {"main:iron", "", "main:iron"},
485                 {"main:iron", "main:iron", "main:iron"},
486         },
487 })
488
489
490
491
492
493 minetest.register_node("train:rail",{
494         description = "Rail",
495         wield_image = "rail.png",
496         tiles = {
497                 "rail.png", "railcurve.png",
498                 "railt.png", "railcross.png"
499         },
500         drawtype = "raillike",
501         paramtype = "light",
502         sunlight_propagates = true,
503         is_ground_content = false,
504         walkable = false,
505         node_placement_prediction = "",
506         selection_box = {
507                 type = "fixed",
508                 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
509         },
510         sounds = main.stoneSound(),
511         after_place_node = function(pos)
512                 data_injection(pos,true)
513         end,
514         after_destruct = function(pos)
515                 data_injection(pos)
516         end,
517         groups={stone=1,wood=1,rail=1,attached_node=1},
518 })
519
520
521 minetest.register_lbm({
522         name = "train:rail",
523         nodenames = {"train:rail"},
524         run_at_every_load = true,
525         action = function(pos)
526                 data_injection(pos,true)
527         end,
528 })
529
530 minetest.register_craft({
531         output = "train:rail 32",
532         recipe = {
533                 {"main:iron","","main:iron"},
534                 {"main:iron","main:stick","main:iron"},
535                 {"main:iron","","main:iron"}
536         }
537 })
538
539
540 minetest.register_food("train:wrench",{
541         description = "Train Wrench",
542         texture = "wrench.png",
543 })
544
545 minetest.register_craft({
546         output = "train:wrench",
547         recipe = {
548                 {"main:iron", "", "main:iron"},
549                 {"main:iron", "main:lapis", "main:iron"},
550                 {"", "main:lapis", ""}
551         }
552 })
553
554
555
556 minetest.register_entity("train:furnace", {
557         initial_properties = {
558                 visual = "wielditem",
559                 visual_size = {x = 0.6, y = 0.6},
560                 textures = {},
561                 physical = true,
562                 is_visible = false,
563                 collide_with_objects = false,
564                 pointable=false,
565                 collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
566         },
567         set_node = function(self)
568                 self.object:set_properties({
569                         is_visible = true,
570                         textures = {"utility:furnace"},
571                 })
572         end,
573
574
575         on_activate = function(self, staticdata)
576                 self.object:set_armor_groups({immortal = 1})
577
578                 self:set_node()
579         end,
580 })