1 -- Minetest: builtin/item.lua
7 core.register_entity(":__builtin:falling_node", {
10 collide_with_objects = false,
11 collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
14 visual_size = {x=0.667, y=0.667},
19 set_node = function(self, node)
23 textures = {node.name},
25 self.object:set_properties(prop)
28 get_staticdata = function(self)
32 on_activate = function(self, staticdata)
33 self.object:set_armor_groups({immortal=1})
35 self:set_node({name=staticdata})
39 on_step = function(self, dtime)
41 self.object:setacceleration({x=0, y=-10, z=0})
42 -- Turn to actual sand when collides to ground or just move
43 local pos = self.object:getpos()
44 local bcp = {x=pos.x, y=pos.y-0.7, z=pos.z} -- Position of bottom center point
45 local bcn = core.get_node(bcp)
46 local bcd = core.registered_nodes[bcn.name]
47 -- Note: walkable is in the node definition, not in item groups
50 (core.get_item_group(self.node.name, "float") ~= 0 and
51 bcd.liquidtype ~= "none")) then
52 if bcd and bcd.leveled and
53 bcn.name == self.node.name then
54 local addlevel = self.node.level
55 if addlevel == nil or addlevel <= 0 then
56 addlevel = bcd.leveled
58 if core.add_node_level(bcp, addlevel) == 0 then
62 elseif bcd and bcd.buildable_to and
63 (core.get_item_group(self.node.name, "float") == 0 or
64 bcd.liquidtype == "none") then
68 local np = {x=bcp.x, y=bcp.y+1, z=bcp.z}
70 local n2 = core.get_node(np)
71 -- If it's not air or liquid, remove node and replace it with
73 if n2.name ~= "air" and (not core.registered_nodes[n2.name] or
74 core.registered_nodes[n2.name].liquidtype == "none") then
76 if core.registered_nodes[n2.name].buildable_to == false then
78 local drops = core.get_node_drops(n2.name, "")
80 for _, dropped_item in ipairs(drops) do
81 core.add_item(np, dropped_item)
86 for _, callback in ipairs(core.registered_on_dignodes) do
90 -- Create node and remove entity
91 core.add_node(np, self.node)
96 local vel = self.object:getvelocity()
97 if vector.equals(vel, {x=0,y=0,z=0}) then
98 local npos = self.object:getpos()
99 self.object:setpos(vector.round(npos))
104 function spawn_falling_node(p, node)
105 local obj = core.add_entity(p, "__builtin:falling_node")
106 obj:get_luaentity():set_node(node)
109 function drop_attached_node(p)
110 local nn = core.get_node(p).name
112 for _,item in ipairs(core.get_node_drops(nn, "")) do
114 x = p.x + math.random()/2 - 0.25,
115 y = p.y + math.random()/2 - 0.25,
116 z = p.z + math.random()/2 - 0.25,
118 core.add_item(pos, item)
122 function check_attached_node(p, n)
123 local def = core.registered_nodes[n.name]
124 local d = {x=0, y=0, z=0}
125 if def.paramtype2 == "wallmounted" then
126 if n.param2 == 0 then
128 elseif n.param2 == 1 then
130 elseif n.param2 == 2 then
132 elseif n.param2 == 3 then
134 elseif n.param2 == 4 then
136 elseif n.param2 == 5 then
142 local p2 = {x=p.x+d.x, y=p.y+d.y, z=p.z+d.z}
143 local nn = core.get_node(p2).name
144 local def2 = core.registered_nodes[nn]
145 if def2 and not def2.walkable then
152 -- Some common functions
155 function nodeupdate_single(p, delay)
156 local n = core.get_node(p)
157 if core.get_item_group(n.name, "falling_node") ~= 0 then
158 local p_bottom = {x=p.x, y=p.y-1, z=p.z}
159 local n_bottom = core.get_node(p_bottom)
160 -- Note: walkable is in the node definition, not in item groups
161 if core.registered_nodes[n_bottom.name] and
162 (core.get_item_group(n.name, "float") == 0 or
163 core.registered_nodes[n_bottom.name].liquidtype == "none") and
164 (n.name ~= n_bottom.name or (core.registered_nodes[n_bottom.name].leveled and
165 core.get_node_level(p_bottom) < core.get_node_max_level(p_bottom))) and
166 (not core.registered_nodes[n_bottom.name].walkable or
167 core.registered_nodes[n_bottom.name].buildable_to) then
169 core.after(0.1, nodeupdate_single, {x=p.x, y=p.y, z=p.z}, false)
171 n.level = core.get_node_level(p)
173 spawn_falling_node(p, n)
179 if core.get_item_group(n.name, "attached_node") ~= 0 then
180 if not check_attached_node(p, n) then
181 drop_attached_node(p)
187 function nodeupdate(p, delay)
188 -- Round p to prevent falling entities to get stuck
189 p.x = math.floor(p.x+0.5)
190 p.y = math.floor(p.y+0.5)
191 p.z = math.floor(p.z+0.5)
196 nodeupdate_single({x=p.x+x, y=p.y+y, z=p.z+z}, delay or not (x==0 and y==0 and z==0))
206 function on_placenode(p, node)
209 core.register_on_placenode(on_placenode)
211 function on_dignode(p, node)
214 core.register_on_dignode(on_dignode)