]> git.lizzy.rs Git - Crafter.git/blob - mods/minecart/init.lua
Snap minecart back to designated rail on turn
[Crafter.git] / mods / minecart / init.lua
1 local pool = {}
2
3 local dirs = {
4         {x= 1,y= 0,z= 0},
5         {x=-1,y= 0,z= 0},
6
7         {x= 1,y= 1,z= 0}, 
8         {x=-1,y= 1,z= 0},
9
10         {x= 1,y=-1,z= 0},
11         {x=-1,y=-1,z= 0},
12
13         {x= 0,y= 0,z= 1},
14         {x= 0,y= 0,z=-1},
15
16         {x= 0,y= 1,z= 1},
17         {x= 0,y= 1,z=-1},
18
19         {x= 0,y=-1,z= 1},
20         {x= 0,y=-1,z=-1},
21 }
22
23 local axis_order = {
24
25 }
26 local function data_injection(pos,data)
27         if data then
28                 pool[minetest.hash_node_position(pos)] = true
29         else
30                 pool[minetest.hash_node_position(pos)] = nil
31         end
32 end
33
34
35 local function create_axis(pos)
36         local possible_dirs = {}
37         for _,dir in pairs(dirs) do
38                 local pos2 = vector.add(pos,dir)
39                 if pool[minetest.hash_node_position(pos2)] then
40                         table.insert(possible_dirs,dir)
41                 end
42         end
43         return(possible_dirs)
44 end
45
46 local function collision_detect(self)
47         if not self.axis_lock then return end
48         local pos = self.object:get_pos()
49
50         for _,object in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
51                 if object:is_player() then
52                         local pos2 = object:get_pos()
53                         if self.axis_lock == "x" then
54                                 local velocity = 1-vector.distance(vector.new(pos.x,0,0),vector.new(pos2.x,0,0))
55                                 print(velocity)
56                                 local dir = vector.direction(vector.new(pos2.x,0,0),vector.new(pos.x,0,0))
57                                 self.object:add_velocity(vector.multiply(dir,velocity))
58                                 self.dir = dir
59                         elseif self.axis_lock == "z" then
60                                 local velocity = 1-vector.distance(vector.new(0,0,pos.z),vector.new(0,0,pos2.z))
61                                 local dir = vector.direction(vector.new(0,0,pos2.z),vector.new(0,0,pos.z))
62                                 self.object:add_velocity(vector.multiply(dir,velocity))
63                                 self.dir = dir
64                         end
65                         return
66                 end
67         end
68 end
69
70
71
72 local function rail_brain(self,pos)
73         if not self.dir then return end
74
75         --if self.dir then print(dump(self.dir)) end
76
77         local pos2 = self.object:get_pos()
78
79         local dir = self.dir
80
81         local triggered = false
82
83         if dir.x < 0 and pos2.x < pos.x then
84                 triggered = true
85         elseif dir.x > 0 and pos2.x > pos.x then
86                 triggered = true
87         elseif dir.z < 0 and pos2.z < pos.z then
88                 triggered = true
89         elseif dir.z > 0 and pos2.z > pos.z then
90                 triggered = true
91         end
92
93         --print(dump(dir))
94         if triggered and not pool[minetest.hash_node_position(vector.add(pos,dir))] then
95                 local possible_dirs = create_axis(pos)
96                 if table.getn(possible_dirs) == 0 then
97                         --stop slow down become physical, something
98                 else
99                         for _,dir2 in pairs(possible_dirs) do
100                                 if dir.x ~= 0 and dir2.z ~= 0 then
101                                         local intertia = math.abs(self.object:get_velocity().x)
102                                         self.object:set_velocity(vector.multiply(dir2,intertia))
103                                         self.dir = dir2
104                                         self.axis_lock = "z"
105                                         self.object:set_pos(pos)
106                                         break
107                                 elseif dir.z ~= 0 and dir2.x ~= 0 then
108                                         local intertia = math.abs(self.object:get_velocity().z)
109                                         self.object:set_velocity(vector.multiply(dir2,intertia))
110                                         self.dir = dir2
111                                         self.axis_lock = "x"
112                                         self.object:set_pos(pos)
113                                         break
114                                 end
115                         end
116                 end
117         end
118 end
119
120
121
122
123 local minecart = {}
124
125 minecart.on_step = function(self,dtime)
126         local pos = vector.round(self.object:get_pos())
127         if not self.axis_lock then
128                 local possible_dirs = create_axis(pos)
129                 for _,dir in pairs(possible_dirs) do
130                         if dir.x ~=0 then
131                                 self.axis_lock = "x"
132                                 break
133                         elseif dir.z ~= 0 then
134                                 self.axis_lock = "z"
135                                 break
136                         end
137                 end
138         else
139                 collision_detect(self)
140                 rail_brain(self,pos)
141         end
142 end
143
144 minecart.on_rightclick = function(self,clicker)
145 end
146
147 --get old data
148 minecart.on_activate = function(self,staticdata, dtime_s)
149         self.object:set_armor_groups({immortal=1})
150         if string.sub(staticdata, 1, string.len("return")) ~= "return" then
151                 return
152         end
153         local data = minetest.deserialize(staticdata)
154         if type(data) ~= "table" then
155                 return
156         end
157
158 end
159
160 minecart.get_staticdata = function(self)
161         return minetest.serialize({
162         })
163 end
164
165
166
167 minecart.initial_properties = {
168         physical = false, -- otherwise going uphill breaks
169         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},
170         visual = "mesh",
171         mesh = "minecart.x",
172         visual_size = {x=1, y=1},
173         textures = {"minecart.png"},
174 }
175
176
177 minecart.on_punch = function(self,puncher, time_from_last_punch, tool_capabilities, dir, damage)
178         --local obj = minetest.add_item(self.object:getpos(), "minecart:minecart")
179         --self.object:remove()
180 end
181
182         
183
184 minetest.register_entity("minecart:minecart", minecart)
185
186
187
188
189
190
191
192
193
194
195
196
197 minetest.register_craftitem("minecart:minecart", {
198         description = "Minecart",
199         inventory_image = "minecartitem.png",
200         wield_image = "minecartitem.png",
201         on_place = function(itemstack, placer, pointed_thing)
202                 if not pointed_thing.type == "node" then
203                         return
204                 end
205                 
206                 local sneak = placer:get_player_control().sneak
207                 local noddef = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
208                 if not sneak and noddef.on_rightclick then
209                         minetest.item_place(itemstack, placer, pointed_thing)
210                         return
211                 end
212                 
213                 if minetest.get_item_group(minetest.get_node(pointed_thing.under).name, "rail")>0 then
214                         minetest.add_entity(pointed_thing.under, "minecart:minecart")
215                 else
216                         return
217                 end
218
219                 itemstack:take_item()
220
221                 return itemstack
222         end,
223 })
224
225 minetest.register_craft({
226         output = "minecart:minecart",
227         recipe = {
228                 {"main:iron", "", "main:iron"},
229                 {"main:iron", "main:iron", "main:iron"},
230         },
231 })
232
233
234
235
236
237 minetest.register_node("minecart:rail",{
238         description = "Rail",
239         wield_image = "rail.png",
240         tiles = {
241                 "rail.png", "railcurve.png",
242                 "railt.png", "railcross.png"
243         },
244         drawtype = "raillike",
245         paramtype = "light",
246         sunlight_propagates = true,
247         is_ground_content = false,
248         walkable = false,
249         node_placement_prediction = "",
250         selection_box = {
251                 type = "fixed",
252                 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
253         },
254         sounds = main.stoneSound(),
255         after_place_node = function(pos)
256                 data_injection(pos,true)
257         end,
258         after_destruct = function(pos)
259                 data_injection(pos)
260         end,
261         groups={stone=1,wood=1,rail=1,attached_node=1},
262 })
263
264
265 minetest.register_lbm({
266         name = "minecart:rail",
267         nodenames = {"minecart:rail"},
268         run_at_every_load = true,
269         action = function(pos)
270                 data_injection(pos,true)
271                 print("buildin dat cash")
272         end,
273 })
274
275 minetest.register_craft({
276         output = "minecart:rail 32",
277         recipe = {
278                 {"main:iron","","main:iron"},
279                 {"main:iron","main:stick","main:iron"},
280                 {"main:iron","","main:iron"}
281         }
282 })