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},
23 set_node = function(self, node, meta)
25 self.meta = meta or {}
26 self.object:set_properties({
28 textures = {node.name},
32 get_staticdata = function(self)
37 return core.serialize(ds)
40 on_activate = function(self, staticdata)
41 self.object:set_armor_groups({immortal = 1})
43 local ds = core.deserialize(staticdata)
44 if ds and ds.node then
45 self:set_node(ds.node, ds.meta)
48 elseif staticdata ~= "" then
49 self:set_node({name = staticdata})
53 on_step = function(self, dtime)
55 local acceleration = self.object:getacceleration()
56 if not vector.equals(acceleration, {x = 0, y = -10, z = 0}) then
57 self.object:setacceleration({x = 0, y = -10, z = 0})
59 -- Turn to actual node when colliding with ground, or continue to move
60 local pos = self.object:getpos()
61 -- Position of bottom center point
62 local bcp = {x = pos.x, y = pos.y - 0.7, z = pos.z}
63 -- 'bcn' is nil for unloaded nodes
64 local bcn = core.get_node_or_nil(bcp)
65 -- Delete on contact with ignore at world edges
66 if bcn and bcn.name == "ignore" then
70 local bcd = bcn and core.registered_nodes[bcn.name]
72 (not bcd or bcd.walkable or
73 (core.get_item_group(self.node.name, "float") ~= 0 and
74 bcd.liquidtype ~= "none")) then
75 if bcd and bcd.leveled and
76 bcn.name == self.node.name then
77 local addlevel = self.node.level
78 if not addlevel or addlevel <= 0 then
79 addlevel = bcd.leveled
81 if core.add_node_level(bcp, addlevel) == 0 then
85 elseif bcd and bcd.buildable_to and
86 (core.get_item_group(self.node.name, "float") == 0 or
87 bcd.liquidtype == "none") then
91 local np = {x = bcp.x, y = bcp.y + 1, z = bcp.z}
93 local n2 = core.get_node(np)
94 local nd = core.registered_nodes[n2.name]
95 -- If it's not air or liquid, remove node and replace it with
97 if n2.name ~= "air" and (not nd or nd.liquidtype == "none") then
99 if nd and nd.buildable_to == false then
101 local drops = core.get_node_drops(n2, "")
102 for _, dropped_item in pairs(drops) do
103 core.add_item(np, dropped_item)
107 for _, callback in pairs(core.registered_on_dignodes) do
111 -- Create node and remove entity
112 if core.registered_nodes[self.node.name] then
113 core.add_node(np, self.node)
115 local meta = core.get_meta(np)
116 meta:from_table(self.meta)
120 core.check_for_falling(np)
123 local vel = self.object:getvelocity()
124 if vector.equals(vel, {x = 0, y = 0, z = 0}) then
125 local npos = self.object:getpos()
126 self.object:setpos(vector.round(npos))
131 local function spawn_falling_node(p, node, meta)
132 local obj = core.add_entity(p, "__builtin:falling_node")
134 obj:get_luaentity():set_node(node, meta)
138 function core.spawn_falling_node(pos)
139 local node = core.get_node(pos)
140 if node.name == "air" or node.name == "ignore" then
143 local obj = core.add_entity(pos, "__builtin:falling_node")
145 obj:get_luaentity():set_node(node)
146 core.remove_node(pos)
152 local function drop_attached_node(p)
153 local n = core.get_node(p)
155 for _, item in pairs(core.get_node_drops(n, "")) do
157 x = p.x + math.random()/2 - 0.25,
158 y = p.y + math.random()/2 - 0.25,
159 z = p.z + math.random()/2 - 0.25,
161 core.add_item(pos, item)
165 function builtin_shared.check_attached_node(p, n)
166 local def = core.registered_nodes[n.name]
167 local d = {x = 0, y = 0, z = 0}
168 if def.paramtype2 == "wallmounted" or
169 def.paramtype2 == "colorwallmounted" then
170 -- The fallback vector here is in case 'wallmounted to dir' is nil due
171 -- to voxelmanip placing a wallmounted node without resetting a
172 -- pre-existing param2 value that is out-of-range for wallmounted.
173 -- The fallback vector corresponds to param2 = 0.
174 d = core.wallmounted_to_dir(n.param2) or {x = 0, y = 1, z = 0}
178 local p2 = vector.add(p, d)
179 local nn = core.get_node(p2).name
180 local def2 = core.registered_nodes[nn]
181 if def2 and not def2.walkable then
188 -- Some common functions
191 function core.check_single_for_falling(p)
192 local n = core.get_node(p)
193 if core.get_item_group(n.name, "falling_node") ~= 0 then
194 local p_bottom = {x = p.x, y = p.y - 1, z = p.z}
195 -- Only spawn falling node if node below is loaded
196 local n_bottom = core.get_node_or_nil(p_bottom)
197 local d_bottom = n_bottom and core.registered_nodes[n_bottom.name]
200 (core.get_item_group(n.name, "float") == 0 or
201 d_bottom.liquidtype == "none") and
203 (n.name ~= n_bottom.name or (d_bottom.leveled and
204 core.get_node_level(p_bottom) <
205 core.get_node_max_level(p_bottom))) and
207 (not d_bottom.walkable or d_bottom.buildable_to) then
208 n.level = core.get_node_level(p)
209 local meta = core.get_meta(p)
212 metatable = meta:to_table()
215 spawn_falling_node(p, n, metatable)
220 if core.get_item_group(n.name, "attached_node") ~= 0 then
221 if not builtin_shared.check_attached_node(p, n) then
222 drop_attached_node(p)
230 -- This table is specifically ordered.
231 -- We don't walk diagonals, only our direct neighbors, and self.
232 -- Down first as likely case, but always before self. The same with sides.
233 -- Up must come last, so that things above self will also fall all at once.
234 local check_for_falling_neighbors = {
235 {x = -1, y = -1, z = 0},
236 {x = 1, y = -1, z = 0},
237 {x = 0, y = -1, z = -1},
238 {x = 0, y = -1, z = 1},
239 {x = 0, y = -1, z = 0},
240 {x = -1, y = 0, z = 0},
241 {x = 1, y = 0, z = 0},
242 {x = 0, y = 0, z = 1},
243 {x = 0, y = 0, z = -1},
244 {x = 0, y = 0, z = 0},
245 {x = 0, y = 1, z = 0},
248 function core.check_for_falling(p)
249 -- Round p to prevent falling entities to get stuck.
252 -- We make a stack, and manually maintain size for performance.
253 -- Stored in the stack, we will maintain tables with pos, and
254 -- last neighbor visited. This way, when we get back to each
255 -- node, we know which directions we have already walked, and
256 -- which direction is the next to walk.
259 -- The neighbor order we will visit from our table.
263 -- Push current pos onto the stack.
265 s[n] = {p = p, v = v}
266 -- Select next node from neighbor list.
267 p = vector.add(p, check_for_falling_neighbors[v])
268 -- Now we check out the node. If it is in need of an update,
269 -- it will let us know in the return value (true = updated).
270 if not core.check_single_for_falling(p) then
271 -- If we don't need to "recurse" (walk) to it then pop
272 -- our previous pos off the stack and continue from there,
273 -- with the v value we were at when we last were at that
281 -- If there's nothing left on the stack, and no
282 -- more sides to walk to, we're done and can exit
283 if n == 0 and v == 11 then
287 -- The next round walk the next neighbor in list.
290 -- If we did need to walk the neighbor, then
291 -- start walking it from the walk order start (1),
292 -- and not the order we just pushed up the stack.
302 local function on_placenode(p, node)
303 core.check_for_falling(p)
305 core.register_on_placenode(on_placenode)
307 local function on_dignode(p, node)
308 core.check_for_falling(p)
310 core.register_on_dignode(on_dignode)
312 local function on_punchnode(p, node)
313 core.check_for_falling(p)
315 core.register_on_punchnode(on_punchnode)
318 -- Globally exported functions
321 -- TODO remove this function after the 0.4.15 release
322 function nodeupdate(p)
323 core.log("deprecated", "nodeupdate: deprecated, please use core.check_for_falling instead")
324 core.check_for_falling(p)
327 -- TODO remove this function after the 0.4.15 release
328 function nodeupdate_single(p)
329 core.log("deprecated", "nodeupdate_single: deprecated, please use core.check_single_for_falling instead")
330 core.check_single_for_falling(p)