Add minetest.spawn_falling_node(pos) (#5339)
[oweals/minetest.git] / builtin / game / falling.lua
1 -- Minetest: builtin/item.lua
2
3 local builtin_shared = ...
4
5 --
6 -- Falling stuff
7 --
8
9 core.register_entity(":__builtin:falling_node", {
10         initial_properties = {
11                 visual = "wielditem",
12                 visual_size = {x = 0.667, y = 0.667},
13                 textures = {},
14                 physical = true,
15                 is_visible = false,
16                 collide_with_objects = false,
17                 collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
18         },
19
20         node = {},
21
22         set_node = function(self, node)
23                 self.node = node
24                 self.object:set_properties({
25                         is_visible = true,
26                         textures = {node.name},
27                 })
28         end,
29
30         get_staticdata = function(self)
31                 return core.serialize(self.node)
32         end,
33
34         on_activate = function(self, staticdata)
35                 self.object:set_armor_groups({immortal = 1})
36                 
37                 local node = core.deserialize(staticdata)
38                 if node then
39                         self:set_node(node)
40                 elseif staticdata ~= "" then
41                         self:set_node({name = staticdata})
42                 end
43         end,
44
45         on_step = function(self, dtime)
46                 -- Set gravity
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})
50                 end
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]
58                 if bcn and
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
67                                 end
68                                 if core.add_node_level(bcp, addlevel) == 0 then
69                                         self.object:remove()
70                                         return
71                                 end
72                         elseif bcd and bcd.buildable_to and
73                                         (core.get_item_group(self.node.name, "float") == 0 or
74                                         bcd.liquidtype == "none") then
75                                 core.remove_node(bcp)
76                                 return
77                         end
78                         local np = {x = bcp.x, y = bcp.y + 1, z = bcp.z}
79                         -- Check what's here
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
83                         -- it's drops
84                         if n2.name ~= "air" and (not nd or nd.liquidtype == "none") then
85                                 core.remove_node(np)
86                                 if nd and nd.buildable_to == false then
87                                         -- Add dropped items
88                                         local drops = core.get_node_drops(n2.name, "")
89                                         for _, dropped_item in pairs(drops) do
90                                                 core.add_item(np, dropped_item)
91                                         end
92                                 end
93                                 -- Run script hook
94                                 for _, callback in pairs(core.registered_on_dignodes) do
95                                         callback(np, n2)
96                                 end
97                         end
98                         -- Create node and remove entity
99                         if core.registered_nodes[self.node.name] then
100                                 core.add_node(np, self.node)
101                         end
102                         self.object:remove()
103                         core.check_for_falling(np)
104                         return
105                 end
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))
110                 end
111         end
112 })
113
114 local function spawn_falling_node(p, node)
115         local obj = core.add_entity(p, "__builtin:falling_node")
116         if obj then
117                 obj:get_luaentity():set_node(node)
118         end
119 end
120
121 function core.spawn_falling_node(pos)
122         local node = core.get_node(pos)
123         if node.name == "air" or node.name == "ignore" then
124                 return false
125         end
126         local obj = core.add_entity(pos, "__builtin:falling_node")
127         if obj then
128                 obj:get_luaentity():set_node(node)
129                 core.remove_node(pos)
130                 return true
131         end
132         return false
133 end
134
135 local function drop_attached_node(p)
136         local nn = core.get_node(p).name
137         core.remove_node(p)
138         for _, item in pairs(core.get_node_drops(nn, "")) do
139                 local pos = {
140                         x = p.x + math.random()/2 - 0.25,
141                         y = p.y + math.random()/2 - 0.25,
142                         z = p.z + math.random()/2 - 0.25,
143                 }
144                 core.add_item(pos, item)
145         end
146 end
147
148 function builtin_shared.check_attached_node(p, n)
149         local def = core.registered_nodes[n.name]
150         local d = {x = 0, y = 0, z = 0}
151         if def.paramtype2 == "wallmounted" or
152                         def.paramtype2 == "colorwallmounted" then
153                 -- The fallback vector here is in case 'wallmounted to dir' is nil due
154                 -- to voxelmanip placing a wallmounted node without resetting a
155                 -- pre-existing param2 value that is out-of-range for wallmounted.
156                 -- The fallback vector corresponds to param2 = 0.
157                 d = core.wallmounted_to_dir(n.param2) or {x = 0, y = 1, z = 0}
158         else
159                 d.y = -1
160         end
161         local p2 = vector.add(p, d)
162         local nn = core.get_node(p2).name
163         local def2 = core.registered_nodes[nn]
164         if def2 and not def2.walkable then
165                 return false
166         end
167         return true
168 end
169
170 --
171 -- Some common functions
172 --
173
174 function core.check_single_for_falling(p)
175         local n = core.get_node(p)
176         if core.get_item_group(n.name, "falling_node") ~= 0 then
177                 local p_bottom = {x = p.x, y = p.y - 1, z = p.z}
178                 -- Only spawn falling node if node below is loaded
179                 local n_bottom = core.get_node_or_nil(p_bottom)
180                 local d_bottom = n_bottom and core.registered_nodes[n_bottom.name]
181                 if d_bottom and
182
183                                 (core.get_item_group(n.name, "float") == 0 or
184                                 d_bottom.liquidtype == "none") and
185
186                                 (n.name ~= n_bottom.name or (d_bottom.leveled and
187                                 core.get_node_level(p_bottom) <
188                                 core.get_node_max_level(p_bottom))) and
189
190                                 (not d_bottom.walkable or d_bottom.buildable_to) then
191                         n.level = core.get_node_level(p)
192                         core.remove_node(p)
193                         spawn_falling_node(p, n)
194                         return true
195                 end
196         end
197
198         if core.get_item_group(n.name, "attached_node") ~= 0 then
199                 if not builtin_shared.check_attached_node(p, n) then
200                         drop_attached_node(p)
201                         return true
202                 end
203         end
204
205         return false
206 end
207
208 -- This table is specifically ordered.
209 -- We don't walk diagonals, only our direct neighbors, and self.
210 -- Down first as likely case, but always before self. The same with sides.
211 -- Up must come last, so that things above self will also fall all at once.
212 local check_for_falling_neighbors = {
213         {x = -1, y = -1, z = 0},
214         {x = 1, y = -1, z = 0},
215         {x = 0, y = -1, z = -1},
216         {x = 0, y = -1, z = 1},
217         {x = 0, y = -1, z = 0},
218         {x = -1, y = 0, z = 0},
219         {x = 1, y = 0, z = 0},
220         {x = 0, y = 0, z = 1},
221         {x = 0, y = 0, z = -1},
222         {x = 0, y = 0, z = 0},
223         {x = 0, y = 1, z = 0},
224 }
225
226 function core.check_for_falling(p)
227         -- Round p to prevent falling entities to get stuck.
228         p = vector.round(p)
229
230         -- We make a stack, and manually maintain size for performance.
231         -- Stored in the stack, we will maintain tables with pos, and
232         -- last neighbor visited. This way, when we get back to each
233         -- node, we know which directions we have already walked, and
234         -- which direction is the next to walk.
235         local s = {}
236         local n = 0
237         -- The neighbor order we will visit from our table.
238         local v = 1
239
240         while true do
241                 -- Push current pos onto the stack.
242                 n = n + 1
243                 s[n] = {p = p, v = v}
244                 -- Select next node from neighbor list.
245                 p = vector.add(p, check_for_falling_neighbors[v])
246                 -- Now we check out the node. If it is in need of an update,
247                 -- it will let us know in the return value (true = updated).
248                 if not core.check_single_for_falling(p) then
249                         -- If we don't need to "recurse" (walk) to it then pop
250                         -- our previous pos off the stack and continue from there,
251                         -- with the v value we were at when we last were at that
252                         -- node
253                         repeat
254                                 local pop = s[n]
255                                 p = pop.p
256                                 v = pop.v
257                                 s[n] = nil
258                                 n = n - 1
259                                 -- If there's nothing left on the stack, and no
260                                 -- more sides to walk to, we're done and can exit
261                                 if n == 0 and v == 11 then
262                                         return
263                                 end
264                         until v < 11
265                         -- The next round walk the next neighbor in list.
266                         v = v + 1
267                 else
268                         -- If we did need to walk the neighbor, then
269                         -- start walking it from the walk order start (1),
270                         -- and not the order we just pushed up the stack.
271                         v = 1
272                 end
273         end
274 end
275
276 --
277 -- Global callbacks
278 --
279
280 local function on_placenode(p, node)
281         core.check_for_falling(p)
282 end
283 core.register_on_placenode(on_placenode)
284
285 local function on_dignode(p, node)
286         core.check_for_falling(p)
287 end
288 core.register_on_dignode(on_dignode)
289
290 local function on_punchnode(p, node)
291         core.check_for_falling(p)
292 end
293 core.register_on_punchnode(on_punchnode)
294
295 --
296 -- Globally exported functions
297 --
298
299 -- TODO remove this function after the 0.4.15 release
300 function nodeupdate(p)
301         core.log("deprecated", "nodeupdate: deprecated, please use core.check_for_falling instead")
302         core.check_for_falling(p)
303 end
304
305 -- TODO remove this function after the 0.4.15 release
306 function nodeupdate_single(p)
307         core.log("deprecated", "nodeupdate_single: deprecated, please use core.check_single_for_falling instead")
308         core.check_single_for_falling(p)
309 end