26 local function data_injection(pos,data)
28 pool[minetest.hash_node_position(pos)] = true
30 pool[minetest.hash_node_position(pos)] = nil
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)
46 local function collision_detect(self)
47 if not self.axis_lock then return end
48 local pos = self.object:get_pos()
49 for _,object in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
50 if object:is_player() then
51 local pos2 = object:get_pos()
52 if self.axis_lock == "x" then
53 local velocity = (1-vector.distance(vector.new(pos.x,0,0),vector.new(pos2.x,0,0)))/50
54 local dir = vector.direction(vector.new(pos2.x,0,0),vector.new(pos.x,0,0))
55 local new_vel = vector.multiply(dir,velocity)
56 self.velocity = new_vel
58 elseif self.axis_lock == "z" then
59 local velocity = (1-vector.distance(vector.new(0,0,pos.z),vector.new(0,0,pos2.z)))/50
60 local dir = vector.direction(vector.new(0,0,pos2.z),vector.new(0,0,pos.z))
61 self.velocity = vector.multiply(dir,velocity)
69 local function direction_snap(self)
71 local yaw = minetest.dir_to_yaw(dir)
72 self.object:set_rotation(vector.new(0,yaw,0))
75 local function turn_snap(pos,self,dir,dir2)
76 if dir.x ~= 0 and dir2.z ~= 0 then
77 local inertia = math.abs(self.velocity.x)
78 self.velocity = vector.multiply(dir2,inertia)
81 self.object:set_pos(pos)
84 elseif dir.z ~= 0 and dir2.x ~= 0 then
85 local inertia = math.abs(self.velocity.z)
86 self.velocity = vector.multiply(dir2,inertia)
89 self.object:set_pos(pos)
95 local function climb_snap(pos,self,dir,dir2)
96 if dir.x == dir2.x and dir2.y ~= 0 then
99 local inertia = math.abs(self.velocity.x)
100 self.velocity = vector.multiply(dir2,inertia)
103 self.object:set_pos(pos)
106 elseif dir.z == dir2.z and dir2.y ~= 0 then
107 local inertia = math.abs(self.velocity.z)
108 self.velocity = vector.multiply(dir2,inertia)
111 self.object:set_pos(pos)
117 local function straight_snap(pos,self,dir)
118 if dir.x ~= 0 and pool[minetest.hash_node_position(vector.add(pos,vector.new(dir.x,0,0)))] then
119 self.velocity = vector.new(self.velocity.x,0,0)
120 self.dir = vector.new(dir.x,0,0)
122 self.object:set_pos(pos)
125 elseif dir.z ~= 0 and pool[minetest.hash_node_position(vector.add(pos,vector.new(0,0,dir.z)))] then
126 self.velocity = vector.new(0,0,self.velocity.z)
127 self.dir = vector.new(0,0,dir.z)
129 self.object:set_pos(pos)
135 local function rail_brain(self,pos)
136 if not self.dir then return end
138 --if self.dir then print(dump(self.dir)) end
140 local pos2 = self.object:get_pos()
144 local triggered = false
146 if dir.x < 0 and pos2.x < pos.x then
148 elseif dir.x > 0 and pos2.x > pos.x then
150 elseif dir.z < 0 and pos2.z < pos.z then
152 elseif dir.z > 0 and pos2.z > pos.z then
157 if triggered and not pool[minetest.hash_node_position(vector.add(pos,dir))] then
159 if straight_snap(pos,self,dir) then
163 local possible_dirs = create_axis(pos)
165 if table.getn(possible_dirs) == 0 then
166 --print("train fails")
167 --stop slow down become physical, something
169 for _,dir2 in pairs(possible_dirs) do
170 if turn_snap(pos,self,dir,dir2) then
173 if climb_snap(pos,self,dir,dir2) then
186 minecart.on_step = function(self,dtime)
187 local float_pos = self.object:get_pos()
188 local pos = vector.round(float_pos)
190 if self.velocity then
191 local new_vel = dtime*1000
192 local test = vector.multiply(self.velocity,new_vel)
196 elseif test.x < -0.5 then
201 elseif test.z < -0.5 then
204 self.object:move_to(vector.add(float_pos,test))
207 if not self.axis_lock then
208 local possible_dirs = create_axis(pos)
209 for _,dir in pairs(possible_dirs) do
212 self.dir = vector.new(1,0,0)
215 elseif dir.z ~= 0 then
217 self.dir = vector.new(0,0,1)
223 collision_detect(self)
226 self.old_pos = float_pos
229 minecart.on_rightclick = function(self,clicker)
233 minecart.on_activate = function(self,staticdata, dtime_s)
234 self.object:set_armor_groups({immortal=1})
235 if string.sub(staticdata, 1, string.len("return")) ~= "return" then
238 local data = minetest.deserialize(staticdata)
239 if type(data) ~= "table" then
242 self.old_pos = self.object:get_pos()
243 self.velocity = vector.new(0,0,0)
246 minecart.get_staticdata = function(self)
247 return minetest.serialize({
253 minecart.initial_properties = {
254 physical = false, -- otherwise going uphill breaks
255 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},
258 visual_size = {x=1, y=1},
259 textures = {"minecart.png"},
263 minecart.on_punch = function(self,puncher, time_from_last_punch, tool_capabilities, dir, damage)
264 --local obj = minetest.add_item(self.object:getpos(), "minecart:minecart")
265 --self.object:remove()
270 minetest.register_entity("minecart:minecart", minecart)
283 minetest.register_craftitem("minecart:minecart", {
284 description = "Minecart",
285 inventory_image = "minecartitem.png",
286 wield_image = "minecartitem.png",
287 on_place = function(itemstack, placer, pointed_thing)
288 if not pointed_thing.type == "node" then
292 local sneak = placer:get_player_control().sneak
293 local noddef = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
294 if not sneak and noddef.on_rightclick then
295 minetest.item_place(itemstack, placer, pointed_thing)
299 if minetest.get_item_group(minetest.get_node(pointed_thing.under).name, "rail")>0 then
300 minetest.add_entity(pointed_thing.under, "minecart:minecart")
305 itemstack:take_item()
311 minetest.register_craft({
312 output = "minecart:minecart",
314 {"main:iron", "", "main:iron"},
315 {"main:iron", "main:iron", "main:iron"},
323 minetest.register_node("minecart:rail",{
324 description = "Rail",
325 wield_image = "rail.png",
327 "rail.png", "railcurve.png",
328 "railt.png", "railcross.png"
330 drawtype = "raillike",
332 sunlight_propagates = true,
333 is_ground_content = false,
335 node_placement_prediction = "",
338 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
340 sounds = main.stoneSound(),
341 after_place_node = function(pos)
342 data_injection(pos,true)
344 after_destruct = function(pos)
347 groups={stone=1,wood=1,rail=1,attached_node=1},
351 minetest.register_lbm({
352 name = "minecart:rail",
353 nodenames = {"minecart:rail"},
354 run_at_every_load = true,
355 action = function(pos)
356 data_injection(pos,true)
357 print("buildin dat cash")
361 minetest.register_craft({
362 output = "minecart:rail 32",
364 {"main:iron","","main:iron"},
365 {"main:iron","main:stick","main:iron"},
366 {"main:iron","","main:iron"}