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)
21 local stack = ItemStack(node.name)
22 local itemtable = stack:to_table()
25 itemname = stack:to_table().name
27 local item_texture = nil
29 if core.registered_items[itemname] then
30 item_texture = core.registered_items[itemname].inventory_image
31 item_type = core.registered_items[itemname].type
35 textures = {node.name},
37 self.object:set_properties(prop)
40 get_staticdata = function(self)
44 on_activate = function(self, staticdata)
45 self.object:set_armor_groups({immortal=1})
46 self:set_node({name=staticdata})
49 on_step = function(self, dtime)
51 self.object:setacceleration({x=0, y=-10, z=0})
52 -- Turn to actual sand when collides to ground or just move
53 local pos = self.object:getpos()
54 local bcp = {x=pos.x, y=pos.y-0.7, z=pos.z} -- Position of bottom center point
55 local bcn = core.get_node(bcp)
56 local bcd = core.registered_nodes[bcn.name]
57 -- Note: walkable is in the node definition, not in item groups
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 addlevel == nil 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 -- If it's not air or liquid, remove node and replace it with
83 if n2.name ~= "air" and (not core.registered_nodes[n2.name] or
84 core.registered_nodes[n2.name].liquidtype == "none") then
86 if core.registered_nodes[n2.name].buildable_to == false then
88 local drops = core.get_node_drops(n2.name, "")
90 for _, dropped_item in ipairs(drops) do
91 core.add_item(np, dropped_item)
96 for _, callback in ipairs(core.registered_on_dignodes) do
100 -- Create node and remove entity
101 core.add_node(np, self.node)
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 function spawn_falling_node(p, node)
115 local obj = core.add_entity(p, "__builtin:falling_node")
116 obj:get_luaentity():set_node(node)
119 function drop_attached_node(p)
120 local nn = core.get_node(p).name
122 for _,item in ipairs(core.get_node_drops(nn, "")) do
124 x = p.x + math.random()/2 - 0.25,
125 y = p.y + math.random()/2 - 0.25,
126 z = p.z + math.random()/2 - 0.25,
128 core.add_item(pos, item)
132 function check_attached_node(p, n)
133 local def = core.registered_nodes[n.name]
134 local d = {x=0, y=0, z=0}
135 if def.paramtype2 == "wallmounted" then
136 if n.param2 == 0 then
138 elseif n.param2 == 1 then
140 elseif n.param2 == 2 then
142 elseif n.param2 == 3 then
144 elseif n.param2 == 4 then
146 elseif n.param2 == 5 then
152 local p2 = {x=p.x+d.x, y=p.y+d.y, z=p.z+d.z}
153 local nn = core.get_node(p2).name
154 local def2 = core.registered_nodes[nn]
155 if def2 and not def2.walkable then
162 -- Some common functions
165 function nodeupdate_single(p, delay)
166 local n = core.get_node(p)
167 if core.get_item_group(n.name, "falling_node") ~= 0 then
168 local p_bottom = {x=p.x, y=p.y-1, z=p.z}
169 local n_bottom = core.get_node(p_bottom)
170 -- Note: walkable is in the node definition, not in item groups
171 if core.registered_nodes[n_bottom.name] and
172 (core.get_item_group(n.name, "float") == 0 or
173 core.registered_nodes[n_bottom.name].liquidtype == "none") and
174 (n.name ~= n_bottom.name or (core.registered_nodes[n_bottom.name].leveled and
175 core.get_node_level(p_bottom) < core.get_node_max_level(p_bottom))) and
176 (not core.registered_nodes[n_bottom.name].walkable or
177 core.registered_nodes[n_bottom.name].buildable_to) then
179 core.after(0.1, nodeupdate_single, {x=p.x, y=p.y, z=p.z}, false)
181 n.level = core.get_node_level(p)
183 spawn_falling_node(p, n)
189 if core.get_item_group(n.name, "attached_node") ~= 0 then
190 if not check_attached_node(p, n) then
191 drop_attached_node(p)
197 function nodeupdate(p, delay)
198 -- Round p to prevent falling entities to get stuck
199 p.x = math.floor(p.x+0.5)
200 p.y = math.floor(p.y+0.5)
201 p.z = math.floor(p.z+0.5)
206 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))
216 function on_placenode(p, node)
219 core.register_on_placenode(on_placenode)
221 function on_dignode(p, node)
224 core.register_on_dignode(on_dignode)