Fix crash when using nametag
[MineClone/MineClone2.git] / mods / ENTITIES / mcl_minecarts / functions.lua
blob42cdecd12738313cd61aa41158b7c987bf6561b0
1 function mcl_minecarts:get_sign(z)
2 if z == 0 then
3 return 0
4 else
5 return z / math.abs(z)
6 end
7 end
9 function mcl_minecarts:velocity_to_dir(v)
10 if math.abs(v.x) > math.abs(v.z) then
11 return {x=mcl_minecarts:get_sign(v.x), y=mcl_minecarts:get_sign(v.y), z=0}
12 else
13 return {x=0, y=mcl_minecarts:get_sign(v.y), z=mcl_minecarts:get_sign(v.z)}
14 end
15 end
17 function mcl_minecarts:is_rail(pos, railtype)
18 local node = minetest.get_node(pos).name
19 if node == "ignore" then
20 local vm = minetest.get_voxel_manip()
21 local emin, emax = vm:read_from_map(pos, pos)
22 local area = VoxelArea:new{
23 MinEdge = emin,
24 MaxEdge = emax,
26 local data = vm:get_data()
27 local vi = area:indexp(pos)
28 node = minetest.get_name_from_content_id(data[vi])
29 end
30 if minetest.get_item_group(node, "rail") == 0 then
31 return false
32 end
33 if not railtype then
34 return true
35 end
36 return minetest.get_item_group(node, "connect_to_raillike") == railtype
37 end
39 function mcl_minecarts:check_front_up_down(pos, dir_, check_down, railtype)
40 local dir = vector.new(dir_)
41 local cur = nil
43 -- Front
44 dir.y = 0
45 cur = vector.add(pos, dir)
46 if mcl_minecarts:is_rail(cur, railtype) then
47 return dir
48 end
49 -- Up
50 if check_down then
51 dir.y = 1
52 cur = vector.add(pos, dir)
53 if mcl_minecarts:is_rail(cur, railtype) then
54 return dir
55 end
56 end
57 -- Down
58 dir.y = -1
59 cur = vector.add(pos, dir)
60 if mcl_minecarts:is_rail(cur, railtype) then
61 return dir
62 end
63 return nil
64 end
66 function mcl_minecarts:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
67 local pos = vector.round(pos_)
68 local cur = nil
69 local left_check, right_check = true, true
71 -- Check left and right
72 local left = {x=0, y=0, z=0}
73 local right = {x=0, y=0, z=0}
74 if dir.z ~= 0 and dir.x == 0 then
75 left.x = -dir.z
76 right.x = dir.z
77 elseif dir.x ~= 0 and dir.z == 0 then
78 left.z = dir.x
79 right.z = -dir.x
80 end
82 if ctrl then
83 if old_switch == 1 then
84 left_check = false
85 elseif old_switch == 2 then
86 right_check = false
87 end
88 if ctrl.left and left_check then
89 cur = mcl_minecarts:check_front_up_down(pos, left, false, railtype)
90 if cur then
91 return cur, 1
92 end
93 left_check = false
94 end
95 if ctrl.right and right_check then
96 cur = mcl_minecarts:check_front_up_down(pos, right, false, railtype)
97 if cur then
98 return cur, 2
99 end
100 right_check = true
104 -- Normal
105 cur = mcl_minecarts:check_front_up_down(pos, dir, true, railtype)
106 if cur then
107 return cur
110 -- Left, if not already checked
111 if left_check then
112 cur = mcl_minecarts:check_front_up_down(pos, left, false, railtype)
113 if cur then
114 return cur
118 -- Right, if not already checked
119 if right_check then
120 cur = mcl_minecarts:check_front_up_down(pos, right, false, railtype)
121 if cur then
122 return cur
126 -- Backwards
127 if not old_switch then
128 cur = mcl_minecarts:check_front_up_down(pos, {
129 x = -dir.x,
130 y = dir.y,
131 z = -dir.z
132 }, true, railtype)
133 if cur then
134 return cur
138 return {x=0, y=0, z=0}