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 -- Avoid bugs caused by an unloaded node below
64 local bcn = core.get_node_or_nil(bcp)
65 local bcd = bcn and core.registered_nodes[bcn.name]
67 (not bcd or bcd.walkable or
68 (core.get_item_group(self.node.name, "float") ~= 0 and
69 bcd.liquidtype ~= "none")) then
70 if bcd and bcd.leveled and
71 bcn.name == self.node.name then
72 local addlevel = self.node.level
73 if not addlevel or addlevel <= 0 then
74 addlevel = bcd.leveled
76 if core.add_node_level(bcp, addlevel) == 0 then
80 elseif bcd and bcd.buildable_to and
81 (core.get_item_group(self.node.name, "float") == 0 or
82 bcd.liquidtype == "none") then
86 local np = {x = bcp.x, y = bcp.y + 1, z = bcp.z}
88 local n2 = core.get_node(np)
89 local nd = core.registered_nodes[n2.name]
90 -- If it's not air or liquid, remove node and replace it with
92 if n2.name ~= "air" and (not nd or nd.liquidtype == "none") then
94 if nd and nd.buildable_to == false then
96 local drops = core.get_node_drops(n2, "")
97 for _, dropped_item in pairs(drops) do
98 core.add_item(np, dropped_item)
102 for _, callback in pairs(core.registered_on_dignodes) do
106 -- Create node and remove entity
107 local def = core.registered_nodes[self.node.name]
109 core.add_node(np, self.node)
111 local meta = core.get_meta(np)
112 meta:from_table(self.meta)
114 if def.sounds and def.sounds.place and def.sounds.place.name then
115 core.sound_play(def.sounds.place, {pos = np})
119 core.check_for_falling(np)
122 local vel = self.object:getvelocity()
123 if vector.equals(vel, {x = 0, y = 0, z = 0}) then
124 local npos = self.object:getpos()
125 self.object:setpos(vector.round(npos))
130 local function spawn_falling_node(p, node, meta)
131 local obj = core.add_entity(p, "__builtin:falling_node")
133 obj:get_luaentity():set_node(node, meta)
137 function core.spawn_falling_node(pos)
138 local node = core.get_node(pos)
139 if node.name == "air" or node.name == "ignore" then
142 local obj = core.add_entity(pos, "__builtin:falling_node")
144 obj:get_luaentity():set_node(node)
145 core.remove_node(pos)
151 local function drop_attached_node(p)
152 local n = core.get_node(p)
153 local drops = core.get_node_drops(n, "")
154 local def = core.registered_items[n.name]
155 if def and def.preserve_metadata then
156 local oldmeta = core.get_meta(p):to_table().fields
157 -- Copy pos and node because the callback can modify them.
158 local pos_copy = {x=p.x, y=p.y, z=p.z}
159 local node_copy = {name=n.name, param1=n.param1, param2=n.param2}
160 local drop_stacks = {}
161 for k, v in pairs(drops) do
162 drop_stacks[k] = ItemStack(v)
165 def.preserve_metadata(pos_copy, node_copy, oldmeta, drops)
168 for _, item in pairs(drops) do
170 x = p.x + math.random()/2 - 0.25,
171 y = p.y + math.random()/2 - 0.25,
172 z = p.z + math.random()/2 - 0.25,
174 core.add_item(pos, item)
178 function builtin_shared.check_attached_node(p, n)
179 local def = core.registered_nodes[n.name]
180 local d = {x = 0, y = 0, z = 0}
181 if def.paramtype2 == "wallmounted" or
182 def.paramtype2 == "colorwallmounted" then
183 -- The fallback vector here is in case 'wallmounted to dir' is nil due
184 -- to voxelmanip placing a wallmounted node without resetting a
185 -- pre-existing param2 value that is out-of-range for wallmounted.
186 -- The fallback vector corresponds to param2 = 0.
187 d = core.wallmounted_to_dir(n.param2) or {x = 0, y = 1, z = 0}
191 local p2 = vector.add(p, d)
192 local nn = core.get_node(p2).name
193 local def2 = core.registered_nodes[nn]
194 if def2 and not def2.walkable then
201 -- Some common functions
204 function core.check_single_for_falling(p)
205 local n = core.get_node(p)
206 if core.get_item_group(n.name, "falling_node") ~= 0 then
207 local p_bottom = {x = p.x, y = p.y - 1, z = p.z}
208 -- Only spawn falling node if node below is loaded
209 local n_bottom = core.get_node_or_nil(p_bottom)
210 local d_bottom = n_bottom and core.registered_nodes[n_bottom.name]
213 (core.get_item_group(n.name, "float") == 0 or
214 d_bottom.liquidtype == "none") and
216 (n.name ~= n_bottom.name or (d_bottom.leveled and
217 core.get_node_level(p_bottom) <
218 core.get_node_max_level(p_bottom))) and
220 (not d_bottom.walkable or d_bottom.buildable_to) then
221 n.level = core.get_node_level(p)
222 local meta = core.get_meta(p)
225 metatable = meta:to_table()
228 spawn_falling_node(p, n, metatable)
233 if core.get_item_group(n.name, "attached_node") ~= 0 then
234 if not builtin_shared.check_attached_node(p, n) then
235 drop_attached_node(p)
243 -- This table is specifically ordered.
244 -- We don't walk diagonals, only our direct neighbors, and self.
245 -- Down first as likely case, but always before self. The same with sides.
246 -- Up must come last, so that things above self will also fall all at once.
247 local check_for_falling_neighbors = {
248 {x = -1, y = -1, z = 0},
249 {x = 1, y = -1, z = 0},
250 {x = 0, y = -1, z = -1},
251 {x = 0, y = -1, z = 1},
252 {x = 0, y = -1, z = 0},
253 {x = -1, y = 0, z = 0},
254 {x = 1, y = 0, z = 0},
255 {x = 0, y = 0, z = 1},
256 {x = 0, y = 0, z = -1},
257 {x = 0, y = 0, z = 0},
258 {x = 0, y = 1, z = 0},
261 function core.check_for_falling(p)
262 -- Round p to prevent falling entities to get stuck.
265 -- We make a stack, and manually maintain size for performance.
266 -- Stored in the stack, we will maintain tables with pos, and
267 -- last neighbor visited. This way, when we get back to each
268 -- node, we know which directions we have already walked, and
269 -- which direction is the next to walk.
272 -- The neighbor order we will visit from our table.
276 -- Push current pos onto the stack.
278 s[n] = {p = p, v = v}
279 -- Select next node from neighbor list.
280 p = vector.add(p, check_for_falling_neighbors[v])
281 -- Now we check out the node. If it is in need of an update,
282 -- it will let us know in the return value (true = updated).
283 if not core.check_single_for_falling(p) then
284 -- If we don't need to "recurse" (walk) to it then pop
285 -- our previous pos off the stack and continue from there,
286 -- with the v value we were at when we last were at that
294 -- If there's nothing left on the stack, and no
295 -- more sides to walk to, we're done and can exit
296 if n == 0 and v == 11 then
300 -- The next round walk the next neighbor in list.
303 -- If we did need to walk the neighbor, then
304 -- start walking it from the walk order start (1),
305 -- and not the order we just pushed up the stack.
315 local function on_placenode(p, node)
316 core.check_for_falling(p)
318 core.register_on_placenode(on_placenode)
320 local function on_dignode(p, node)
321 core.check_for_falling(p)
323 core.register_on_dignode(on_dignode)
325 local function on_punchnode(p, node)
326 core.check_for_falling(p)
328 core.register_on_punchnode(on_punchnode)