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()
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))
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))
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))
72 local function rail_brain(self,pos)
73 if not self.dir then return end
75 --if self.dir then print(dump(self.dir)) end
77 local pos2 = self.object:get_pos()
81 local triggered = false
83 if dir.x < 0 and pos2.x < pos.x then
85 elseif dir.x > 0 and pos2.x > pos.x then
87 elseif dir.z < 0 and pos2.z < pos.z then
89 elseif dir.z > 0 and pos2.z > pos.z then
93 if triggered and not pool[minetest.hash_node_position(vector.add(pos,dir))] then
94 local possible_dirs = create_axis(pos)
95 if table.getn(possible_dirs) == 0 then
96 --stop slow down become physical, something
98 for _,dir2 in pairs(possible_dirs) do
99 if dir.x ~= 0 and dir2.z ~= 0 then
100 local transmitted = self.object:get_velocity().x
101 self.object:set_velocity(vector.new(0,0,transmitted))
102 self.dir = vector.direction(vector.new(0,0,transmitted),vector.new(0,0,0))
103 elseif dir.z ~= 0 and dir2.x ~= 0 then
104 local transmitted = self.object:get_velocity().z
105 self.object:set_velocity(vector.new(transmitted,0,0))
106 self.dir = vector.direction(vector.new(transmitted,0,0),vector.new(0,0,0))
118 minecart.on_step = function(self,dtime)
119 local pos = vector.round(self.object:get_pos())
120 if not self.axis_lock then
121 local possible_dirs = create_axis(pos)
122 for _,dir in pairs(possible_dirs) do
126 elseif dir.z ~= 0 then
132 collision_detect(self)
137 minecart.on_rightclick = function(self,clicker)
141 minecart.on_activate = function(self,staticdata, dtime_s)
142 self.object:set_armor_groups({immortal=1})
143 if string.sub(staticdata, 1, string.len("return")) ~= "return" then
146 local data = minetest.deserialize(staticdata)
147 if type(data) ~= "table" then
153 minecart.get_staticdata = function(self)
154 return minetest.serialize({
160 minecart.initial_properties = {
161 physical = false, -- otherwise going uphill breaks
162 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},
165 visual_size = {x=1, y=1},
166 textures = {"minecart.png"},
170 minecart.on_punch = function(self,puncher, time_from_last_punch, tool_capabilities, dir, damage)
171 --local obj = minetest.add_item(self.object:getpos(), "minecart:minecart")
172 --self.object:remove()
177 minetest.register_entity("minecart:minecart", minecart)
190 minetest.register_craftitem("minecart:minecart", {
191 description = "Minecart",
192 inventory_image = "minecartitem.png",
193 wield_image = "minecartitem.png",
194 on_place = function(itemstack, placer, pointed_thing)
195 if not pointed_thing.type == "node" then
199 local sneak = placer:get_player_control().sneak
200 local noddef = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
201 if not sneak and noddef.on_rightclick then
202 minetest.item_place(itemstack, placer, pointed_thing)
206 if minetest.get_item_group(minetest.get_node(pointed_thing.under).name, "rail")>0 then
207 minetest.add_entity(pointed_thing.under, "minecart:minecart")
212 itemstack:take_item()
218 minetest.register_craft({
219 output = "minecart:minecart",
221 {"main:iron", "", "main:iron"},
222 {"main:iron", "main:iron", "main:iron"},
230 minetest.register_node("minecart:rail",{
231 description = "Rail",
232 wield_image = "rail.png",
234 "rail.png", "railcurve.png",
235 "railt.png", "railcross.png"
237 drawtype = "raillike",
239 sunlight_propagates = true,
240 is_ground_content = false,
242 node_placement_prediction = "",
245 fixed = {-1/2, -1/2, -1/2, 1/2, -1/2+1/16, 1/2},
247 sounds = main.stoneSound(),
248 after_place_node = function(pos)
249 data_injection(pos,true)
251 after_destruct = function(pos)
254 groups={stone=1,wood=1,rail=1,attached_node=1},
258 minetest.register_lbm({
259 name = "minecart:rail",
260 nodenames = {"minecart:rail"},
261 run_at_every_load = true,
262 action = function(pos)
263 data_injection(pos,true)
264 print("buildin dat cash")
268 minetest.register_craft({
269 output = "minecart:rail 32",
271 {"main:iron","","main:iron"},
272 {"main:iron","main:stick","main:iron"},
273 {"main:iron","","main:iron"}