1 -- Minetest: builtin/item.lua
3 local builtin_shared = ...
9 core.register_entity(":__builtin:falling_node", {
10 initial_properties = {
12 visual_size = {x = 0.667, y = 0.667},
16 collide_with_objects = false,
17 collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
22 set_node = function(self, node)
24 self.object:set_properties({
26 textures = {node.name},
30 get_staticdata = function(self)
31 return core.serialize(self.node)
34 on_activate = function(self, staticdata)
35 self.object:set_armor_groups({immortal = 1})
37 local node = core.deserialize(staticdata)
40 elseif staticdata ~= "" then
41 self:set_node({name = staticdata})
45 on_step = function(self, dtime)
47 local acceleration = self.object:getacceleration()
48 if not vector.equals(acceleration, {x = 0, y = -10, z = 0}) then
49 self.object:setacceleration({x = 0, y = -10, z = 0})
51 -- Turn to actual node when colliding with ground, or continue to move
52 local pos = self.object:getpos()
53 -- Position of bottom center point
54 local bcp = {x = pos.x, y = pos.y - 0.7, z = pos.z}
55 -- Avoid bugs caused by an unloaded node below
56 local bcn = core.get_node_or_nil(bcp)
57 local bcd = bcn and core.registered_nodes[bcn.name]
59 (not bcd or bcd.walkable or
60 (core.get_item_group(self.node.name, "float") ~= 0 and
61 bcd.liquidtype ~= "none")) then
62 if bcd and bcd.leveled and
63 bcn.name == self.node.name then
64 local addlevel = self.node.level
65 if not addlevel or addlevel <= 0 then
66 addlevel = bcd.leveled
68 if core.add_node_level(bcp, addlevel) == 0 then
72 elseif bcd and bcd.buildable_to and
73 (core.get_item_group(self.node.name, "float") == 0 or
74 bcd.liquidtype == "none") then
78 local np = {x = bcp.x, y = bcp.y + 1, z = bcp.z}
80 local n2 = core.get_node(np)
81 local nd = core.registered_nodes[n2.name]
82 -- If it's not air or liquid, remove node and replace it with
84 if n2.name ~= "air" and (not nd or nd.liquidtype == "none") then
86 if nd.buildable_to == false then
88 local drops = core.get_node_drops(n2.name, "")
89 for _, dropped_item in pairs(drops) do
90 core.add_item(np, dropped_item)
94 for _, callback in pairs(core.registered_on_dignodes) do
98 -- Create node and remove entity
99 if core.registered_nodes[self.node.name] then
100 core.add_node(np, self.node)
103 core.check_for_falling(np)
106 local vel = self.object:getvelocity()
107 if vector.equals(vel, {x = 0, y = 0, z = 0}) then
108 local npos = self.object:getpos()
109 self.object:setpos(vector.round(npos))
114 local function spawn_falling_node(p, node)
115 local obj = core.add_entity(p, "__builtin:falling_node")
117 obj:get_luaentity():set_node(node)
121 local function drop_attached_node(p)
122 local nn = core.get_node(p).name
124 for _, item in pairs(core.get_node_drops(nn, "")) do
126 x = p.x + math.random()/2 - 0.25,
127 y = p.y + math.random()/2 - 0.25,
128 z = p.z + math.random()/2 - 0.25,
130 core.add_item(pos, item)
134 function builtin_shared.check_attached_node(p, n)
135 local def = core.registered_nodes[n.name]
136 local d = {x = 0, y = 0, z = 0}
137 if def.paramtype2 == "wallmounted" then
138 -- The fallback vector here is in case 'wallmounted to dir' is nil due
139 -- to voxelmanip placing a wallmounted node without resetting a
140 -- pre-existing param2 value that is out-of-range for wallmounted.
141 -- The fallback vector corresponds to param2 = 0.
142 d = core.wallmounted_to_dir(n.param2) or {x = 0, y = 1, z = 0}
146 local p2 = vector.add(p, d)
147 local nn = core.get_node(p2).name
148 local def2 = core.registered_nodes[nn]
149 if def2 and not def2.walkable then
156 -- Some common functions
159 function core.check_single_for_falling(p)
160 local n = core.get_node(p)
161 if core.get_item_group(n.name, "falling_node") ~= 0 then
162 local p_bottom = {x = p.x, y = p.y - 1, z = p.z}
163 -- Only spawn falling node if node below is loaded
164 local n_bottom = core.get_node_or_nil(p_bottom)
165 local d_bottom = n_bottom and core.registered_nodes[n_bottom.name]
168 (core.get_item_group(n.name, "float") == 0 or
169 d_bottom.liquidtype == "none") and
171 (n.name ~= n_bottom.name or (d_bottom.leveled and
172 core.get_node_level(p_bottom) <
173 core.get_node_max_level(p_bottom))) and
175 (not d_bottom.walkable or d_bottom.buildable_to) then
176 n.level = core.get_node_level(p)
178 spawn_falling_node(p, n)
183 if core.get_item_group(n.name, "attached_node") ~= 0 then
184 if not builtin_shared.check_attached_node(p, n) then
185 drop_attached_node(p)
193 -- This table is specifically ordered.
194 -- We don't walk diagonals, only our direct neighbors, and self.
195 -- Down first as likely case, but always before self. The same with sides.
196 -- Up must come last, so that things above self will also fall all at once.
197 local check_for_falling_neighbors = {
198 {x = -1, y = -1, z = 0},
199 {x = 1, y = -1, z = 0},
200 {x = 0, y = -1, z = -1},
201 {x = 0, y = -1, z = 1},
202 {x = 0, y = -1, z = 0},
203 {x = -1, y = 0, z = 0},
204 {x = 1, y = 0, z = 0},
205 {x = 0, y = 0, z = 1},
206 {x = 0, y = 0, z = -1},
207 {x = 0, y = 0, z = 0},
208 {x = 0, y = 1, z = 0},
211 function core.check_for_falling(p)
212 -- Round p to prevent falling entities to get stuck.
215 -- We make a stack, and manually maintain size for performance.
216 -- Stored in the stack, we will maintain tables with pos, and
217 -- last neighbor visited. This way, when we get back to each
218 -- node, we know which directions we have already walked, and
219 -- which direction is the next to walk.
222 -- The neighbor order we will visit from our table.
226 -- Push current pos onto the stack.
228 s[n] = {p = p, v = v}
229 -- Select next node from neighbor list.
230 p = vector.add(p, check_for_falling_neighbors[v])
231 -- Now we check out the node. If it is in need of an update,
232 -- it will let us know in the return value (true = updated).
233 if not core.check_single_for_falling(p) then
234 -- If we don't need to "recurse" (walk) to it then pop
235 -- our previous pos off the stack and continue from there,
236 -- with the v value we were at when we last were at that
244 -- If there's nothing left on the stack, and no
245 -- more sides to walk to, we're done and can exit
246 if n == 0 and v == 11 then
250 -- The next round walk the next neighbor in list.
253 -- If we did need to walk the neighbor, then
254 -- start walking it from the walk order start (1),
255 -- and not the order we just pushed up the stack.
265 local function on_placenode(p, node)
266 core.check_for_falling(p)
268 core.register_on_placenode(on_placenode)
270 local function on_dignode(p, node)
271 core.check_for_falling(p)
273 core.register_on_dignode(on_dignode)
275 local function on_punchnode(p, node)
276 core.check_for_falling(p)
278 core.register_on_punchnode(on_punchnode)
281 -- Globally exported functions
284 -- TODO remove this function after the 0.4.15 release
285 function nodeupdate(p)
286 core.log("deprecated", "nodeupdate: deprecated, please use core.check_for_falling instead")
287 core.check_for_falling(p)
290 -- TODO remove this function after the 0.4.15 release
291 function nodeupdate_single(p)
292 core.log("deprecated", "nodeupdate_single: deprecated, please use core.check_single_for_falling instead")
293 core.check_single_for_falling(p)