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.object:setacceleration({x=0, y=-10, z=0})
47 self:set_node({name=staticdata})
50 on_step = function(self, dtime)
52 self.object:setacceleration({x=0, y=-10, z=0})
53 -- Turn to actual sand when collides to ground or just move
54 local pos = self.object:getpos()
55 local bcp = {x=pos.x, y=pos.y-0.7, z=pos.z} -- Position of bottom center point
56 local bcn = core.get_node(bcp)
57 local bcd = core.registered_nodes[bcn.name]
58 -- Note: walkable is in the node definition, not in item groups
61 (core.get_item_group(self.node.name, "float") ~= 0 and
62 bcd.liquidtype ~= "none")) then
63 if bcd and bcd.leveled and
64 bcn.name == self.node.name then
65 local addlevel = self.node.level
66 if addlevel == nil or addlevel <= 0 then
67 addlevel = bcd.leveled
69 if core.add_node_level(bcp, addlevel) == 0 then
73 elseif bcd and bcd.buildable_to and
74 (core.get_item_group(self.node.name, "float") == 0 or
75 bcd.liquidtype == "none") then
79 local np = {x=bcp.x, y=bcp.y+1, z=bcp.z}
81 local n2 = core.get_node(np)
82 -- If it's not air or liquid, remove node and replace it with
84 if n2.name ~= "air" and (not core.registered_nodes[n2.name] or
85 core.registered_nodes[n2.name].liquidtype == "none") then
86 local drops = core.get_node_drops(n2.name, "")
90 for _, dropped_item in ipairs(drops) do
91 core.add_item(np, dropped_item)
95 for _, callback in ipairs(core.registered_on_dignodes) do
99 -- Create node and remove entity
100 core.add_node(np, self.node)
109 function spawn_falling_node(p, node)
110 obj = core.add_entity(p, "__builtin:falling_node")
111 obj:get_luaentity():set_node(node)
114 function drop_attached_node(p)
115 local nn = core.get_node(p).name
117 for _,item in ipairs(core.get_node_drops(nn, "")) do
119 x = p.x + math.random()/2 - 0.25,
120 y = p.y + math.random()/2 - 0.25,
121 z = p.z + math.random()/2 - 0.25,
123 core.add_item(pos, item)
127 function check_attached_node(p, n)
128 local def = core.registered_nodes[n.name]
129 local d = {x=0, y=0, z=0}
130 if def.paramtype2 == "wallmounted" then
131 if n.param2 == 0 then
133 elseif n.param2 == 1 then
135 elseif n.param2 == 2 then
137 elseif n.param2 == 3 then
139 elseif n.param2 == 4 then
141 elseif n.param2 == 5 then
147 local p2 = {x=p.x+d.x, y=p.y+d.y, z=p.z+d.z}
148 local nn = core.get_node(p2).name
149 local def2 = core.registered_nodes[nn]
150 if def2 and not def2.walkable then
157 -- Some common functions
160 function nodeupdate_single(p, delay)
162 if core.get_item_group(n.name, "falling_node") ~= 0 then
163 p_bottom = {x=p.x, y=p.y-1, z=p.z}
164 n_bottom = core.get_node(p_bottom)
165 -- Note: walkable is in the node definition, not in item groups
166 if core.registered_nodes[n_bottom.name] and
167 (core.get_item_group(n.name, "float") == 0 or
168 core.registered_nodes[n_bottom.name].liquidtype == "none") and
169 (n.name ~= n_bottom.name or (core.registered_nodes[n_bottom.name].leveled and
170 core.get_node_level(p_bottom) < core.get_node_max_level(p_bottom))) and
171 (not core.registered_nodes[n_bottom.name].walkable or
172 core.registered_nodes[n_bottom.name].buildable_to) then
174 core.after(0.1, nodeupdate_single, {x=p.x, y=p.y, z=p.z}, false)
176 n.level = core.env:get_node_level(p)
178 spawn_falling_node(p, n)
184 if core.get_item_group(n.name, "attached_node") ~= 0 then
185 if not check_attached_node(p, n) then
186 drop_attached_node(p)
192 function nodeupdate(p, delay)
193 -- Round p to prevent falling entities to get stuck
194 p.x = math.floor(p.x+0.5)
195 p.y = math.floor(p.y+0.5)
196 p.z = math.floor(p.z+0.5)
201 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))
211 function on_placenode(p, node)
214 core.register_on_placenode(on_placenode)
216 function on_dignode(p, node)
219 core.register_on_dignode(on_dignode)