]> git.lizzy.rs Git - Crafter.git/blob - mods/minecart/init.lua
c42862ab3d178996ae44169925a02358617c35f2
[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)))/10
55                                 local dir = vector.direction(vector.new(pos2.x,0,0),vector.new(pos.x,0,0))
56                                 --self.object:add_velocity(vector.multiply(dir,velocity))
57                                 self.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)))/10
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.velocity = vector.multiply(dir,velocity)
64                                 self.dir = dir
65                         end
66                         return
67                 end
68         end
69 end
70
71 local function direction_snap(self)
72         local dir = self.dir
73         local yaw = minetest.dir_to_yaw(dir)
74         self.object:set_rotation(vector.new(0,yaw,0))
75 end
76
77 local function turn_snap(pos,self,dir,dir2)
78         if dir.x ~= 0 and dir2.z ~= 0 then
79                 --local inertia = math.abs(self.object:get_velocity().x)
80                 local inertia = math.abs(self.velocity.x)
81                 --self.object:set_velocity(vector.multiply(dir2,inertia))
82
83                 self.velocity = vector.multiply(dir2,inertia)
84                 self.dir = dir2
85                 self.axis_lock = "z"
86                 self.object:set_pos(pos)
87                 direction_snap(self)
88                 return(true)
89         elseif dir.z ~= 0 and dir2.x ~= 0 then
90                 --local inertia = math.abs(self.object:get_velocity().z)
91                 --print(dump(self.velocity))
92                 local inertia = math.abs(self.velocity.z)
93                 --self.object:set_velocity(vector.multiply(dir2,inertia))
94                 
95                 self.velocity = vector.multiply(dir2,inertia)
96
97                 self.dir = dir2
98                 self.axis_lock = "x"
99                 self.object:set_pos(pos)
100                 direction_snap(self)
101                 return(true)
102         end
103 end
104
105 local function rail_brain(self,pos)
106         if not self.dir then return end
107
108         --if self.dir then print(dump(self.dir)) end
109
110         local pos2 = self.object:get_pos()
111
112         local dir = self.dir
113
114         local triggered = false
115
116         if dir.x < 0 and pos2.x < pos.x then
117                 triggered = true
118         elseif dir.x > 0 and pos2.x > pos.x then
119                 triggered = true
120         elseif dir.z < 0 and pos2.z < pos.z then
121                 triggered = true
122         elseif dir.z > 0 and pos2.z > pos.z then
123                 triggered = true
124         end
125
126         --print(dump(dir))
127         if triggered and not pool[minetest.hash_node_position(vector.add(pos,dir))] then
128                 local possible_dirs = create_axis(pos)
129                 if table.getn(possible_dirs) == 0 then
130                         --print("train fails")
131                         --stop slow down become physical, something
132                 else
133                         for _,dir2 in pairs(possible_dirs) do
134                                 if turn_snap(pos,self,dir,dir2) then
135                                         return
136                                 end
137                         end
138                 end
139         end
140 end
141
142
143
144
145 local minecart = {}
146
147 minecart.on_step = function(self,dtime)
148         local float_pos = self.object:get_pos()
149         local pos = vector.round(float_pos)
150
151         if self.velocity then
152                 local new_vel = dtime/0.01
153                 print(new_vel)
154                 self.object:move_to(vector.add(float_pos,vector.multiply(self.velocity,new_vel)))
155         end
156
157         --stop minecarts from derailing when going super fast
158         if self.old_pos and vector.distance(float_pos,self.old_pos) > 0.5 then
159                 self.object:move_to(self.old_pos)
160                 float_pos = self.object:get_pos()
161                 pos = vector.round(self.old_pos)
162         end
163
164         if not self.axis_lock then
165                 local possible_dirs = create_axis(pos)
166                 for _,dir in pairs(possible_dirs) do
167                         if dir.x ~=0 then
168                                 self.axis_lock = "x"
169                                 break
170                         elseif dir.z ~= 0 then
171                                 self.axis_lock = "z"
172                                 break
173                         end
174                 end
175         else
176                 collision_detect(self)
177                 rail_brain(self,pos)
178         end
179         self.old_pos = float_pos
180 end
181
182 minecart.on_rightclick = function(self,clicker)
183 end
184
185 --get old data
186 minecart.on_activate = function(self,staticdata, dtime_s)
187         self.object:set_armor_groups({immortal=1})
188         if string.sub(staticdata, 1, string.len("return")) ~= "return" then
189                 return
190         end
191         local data = minetest.deserialize(staticdata)
192         if type(data) ~= "table" then
193                 return
194         end
195         self.old_pos = self.object:get_pos()
196         self.velocity = vector.new(0,0,0)
197 end
198
199 minecart.get_staticdata = function(self)
200         return minetest.serialize({
201         })
202 end
203
204
205
206 minecart.initial_properties = {
207         physical = false, -- otherwise going uphill breaks
208         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},
209         visual = "mesh",
210         mesh = "minecart.x",
211         visual_size = {x=1, y=1},
212         textures = {"minecart.png"},
213 }
214
215
216 minecart.on_punch = function(self,puncher, time_from_last_punch, tool_capabilities, dir, damage)
217         --local obj = minetest.add_item(self.object:getpos(), "minecart:minecart")
218         --self.object:remove()
219 end
220
221         
222
223 minetest.register_entity("minecart:minecart", minecart)
224
225
226
227
228
229
230
231
232
233
234
235
236 minetest.register_craftitem("minecart:minecart", {
237         description = "Minecart",
238         inventory_image = "minecartitem.png",
239         wield_image = "minecartitem.png",
240         on_place = function(itemstack, placer, pointed_thing)
241                 if not pointed_thing.type == "node" then
242                         return
243                 end
244                 
245                 local sneak = placer:get_player_control().sneak
246                 local noddef = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
247                 if not sneak and noddef.on_rightclick then
248                         minetest.item_place(itemstack, placer, pointed_thing)
249                         return
250                 end
251                 
252                 if minetest.get_item_group(minetest.get_node(pointed_thing.under).name, "rail")>0 then
253                         minetest.add_entity(pointed_thing.under, "minecart:minecart")
254                 else
255                         return
256                 end
257
258                 itemstack:take_item()
259
260                 return itemstack
261         end,
262 })
263
264 minetest.register_craft({
265         output = "minecart:minecart",
266         recipe = {
267                 {"main:iron", "", "main:iron"},
268                 {"main:iron", "main:iron", "main:iron"},
269         },
270 })
271
272
273
274
275
276 minetest.register_node("minecart:rail",{
277         description = "Rail",
278         wield_image = "rail.png",
279         tiles = {
280                 "rail.png", "railcurve.png",
281                 "railt.png", "railcross.png"
282         },
283         drawtype = "raillike",
284         paramtype = "light",
285         sunlight_propagates = true,
286         is_ground_content = false,
287         walkable = false,
288         node_placement_prediction = "",
289         selection_box = {
290                 type = "fixed",
291                 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
292         },
293         sounds = main.stoneSound(),
294         after_place_node = function(pos)
295                 data_injection(pos,true)
296         end,
297         after_destruct = function(pos)
298                 data_injection(pos)
299         end,
300         groups={stone=1,wood=1,rail=1,attached_node=1},
301 })
302
303
304 minetest.register_lbm({
305         name = "minecart:rail",
306         nodenames = {"minecart:rail"},
307         run_at_every_load = true,
308         action = function(pos)
309                 data_injection(pos,true)
310                 print("buildin dat cash")
311         end,
312 })
313
314 minetest.register_craft({
315         output = "minecart:rail 32",
316         recipe = {
317                 {"main:iron","","main:iron"},
318                 {"main:iron","main:stick","main:iron"},
319                 {"main:iron","","main:iron"}
320         }
321 })