Add sounds for falling and attached nodes (#7719)
[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         meta = {},
22
23         set_node = function(self, node, meta)
24                 self.node = node
25                 self.meta = meta or {}
26                 self.object:set_properties({
27                         is_visible = true,
28                         textures = {node.name},
29                 })
30         end,
31
32         get_staticdata = function(self)
33                 local ds = {
34                         node = self.node,
35                         meta = self.meta,
36                 }
37                 return core.serialize(ds)
38         end,
39
40         on_activate = function(self, staticdata)
41                 self.object:set_armor_groups({immortal = 1})
42
43                 local ds = core.deserialize(staticdata)
44                 if ds and ds.node then
45                         self:set_node(ds.node, ds.meta)
46                 elseif ds then
47                         self:set_node(ds)
48                 elseif staticdata ~= "" then
49                         self:set_node({name = staticdata})
50                 end
51         end,
52
53         on_step = function(self, dtime)
54                 -- Set gravity
55                 local acceleration = self.object:get_acceleration()
56                 if not vector.equals(acceleration, {x = 0, y = -10, z = 0}) then
57                         self.object:set_acceleration({x = 0, y = -10, z = 0})
58                 end
59                 -- Turn to actual node when colliding with ground, or continue to move
60                 local pos = self.object:get_pos()
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
67                         self.object:remove()
68                         return
69                 end
70                 local bcd = bcn and core.registered_nodes[bcn.name]
71                 if bcn and
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
80                                 end
81                                 if core.add_node_level(bcp, addlevel) == 0 then
82                                         self.object:remove()
83                                         return
84                                 end
85                         elseif bcd and bcd.buildable_to and
86                                         (core.get_item_group(self.node.name, "float") == 0 or
87                                         bcd.liquidtype == "none") then
88                                 core.remove_node(bcp)
89                                 return
90                         end
91                         local np = {x = bcp.x, y = bcp.y + 1, z = bcp.z}
92                         -- Check what's here
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
96                         -- it's drops
97                         if n2.name ~= "air" and (not nd or nd.liquidtype == "none") then
98                                 core.remove_node(np)
99                                 if nd and nd.buildable_to == false then
100                                         -- Add dropped items
101                                         local drops = core.get_node_drops(n2, "")
102                                         for _, dropped_item in pairs(drops) do
103                                                 core.add_item(np, dropped_item)
104                                         end
105                                 end
106                                 -- Run script hook
107                                 for _, callback in pairs(core.registered_on_dignodes) do
108                                         callback(np, n2)
109                                 end
110                         end
111                         -- Create node and remove entity
112                         local def = core.registered_nodes[self.node.name]
113                         if def then
114                                 core.add_node(np, self.node)
115                                 if self.meta then
116                                         local meta = core.get_meta(np)
117                                         meta:from_table(self.meta)
118                                 end
119                                 if def.sounds and def.sounds.place then
120                                         core.sound_play(def.sounds.place, {pos = np})
121                                 end
122                         end
123                         self.object:remove()
124                         core.check_for_falling(np)
125                         return
126                 end
127                 local vel = self.object:get_velocity()
128                 if vector.equals(vel, {x = 0, y = 0, z = 0}) then
129                         local npos = self.object:get_pos()
130                         self.object:set_pos(vector.round(npos))
131                 end
132         end
133 })
134
135 local function convert_to_falling_node(pos, node)
136         local obj = core.add_entity(pos, "__builtin:falling_node")
137         if not obj then
138                 return false
139         end
140         node.level = core.get_node_level(pos)
141         local meta = core.get_meta(pos)
142         local metatable = meta and meta:to_table() or {}
143
144         local def = core.registered_nodes[node.name]
145         if def and def.sounds and def.sounds.fall then
146                 core.sound_play(def.sounds.fall, {pos = pos})
147         end
148
149         obj:get_luaentity():set_node(node, metatable)
150         core.remove_node(pos)
151         return true
152 end
153
154 function core.spawn_falling_node(pos)
155         local node = core.get_node(pos)
156         if node.name == "air" or node.name == "ignore" then
157                 return false
158         end
159         return convert_to_falling_node(pos, node)
160 end
161
162 local function drop_attached_node(p)
163         local n = core.get_node(p)
164         local drops = core.get_node_drops(n, "")
165         local def = core.registered_items[n.name]
166         if def and def.preserve_metadata then
167                 local oldmeta = core.get_meta(p):to_table().fields
168                 -- Copy pos and node because the callback can modify them.
169                 local pos_copy = {x=p.x, y=p.y, z=p.z}
170                 local node_copy = {name=n.name, param1=n.param1, param2=n.param2}
171                 local drop_stacks = {}
172                 for k, v in pairs(drops) do
173                         drop_stacks[k] = ItemStack(v)
174                 end
175                 drops = drop_stacks
176                 def.preserve_metadata(pos_copy, node_copy, oldmeta, drops)
177         end
178         if def and def.sounds and def.sounds.fall then
179                 core.sound_play(def.sounds.fall, {pos = p})
180         end
181         core.remove_node(p)
182         for _, item in pairs(drops) do
183                 local pos = {
184                         x = p.x + math.random()/2 - 0.25,
185                         y = p.y + math.random()/2 - 0.25,
186                         z = p.z + math.random()/2 - 0.25,
187                 }
188                 core.add_item(pos, item)
189         end
190 end
191
192 function builtin_shared.check_attached_node(p, n)
193         local def = core.registered_nodes[n.name]
194         local d = {x = 0, y = 0, z = 0}
195         if def.paramtype2 == "wallmounted" or
196                         def.paramtype2 == "colorwallmounted" then
197                 -- The fallback vector here is in case 'wallmounted to dir' is nil due
198                 -- to voxelmanip placing a wallmounted node without resetting a
199                 -- pre-existing param2 value that is out-of-range for wallmounted.
200                 -- The fallback vector corresponds to param2 = 0.
201                 d = core.wallmounted_to_dir(n.param2) or {x = 0, y = 1, z = 0}
202         else
203                 d.y = -1
204         end
205         local p2 = vector.add(p, d)
206         local nn = core.get_node(p2).name
207         local def2 = core.registered_nodes[nn]
208         if def2 and not def2.walkable then
209                 return false
210         end
211         return true
212 end
213
214 --
215 -- Some common functions
216 --
217
218 function core.check_single_for_falling(p)
219         local n = core.get_node(p)
220         if core.get_item_group(n.name, "falling_node") ~= 0 then
221                 local p_bottom = {x = p.x, y = p.y - 1, z = p.z}
222                 -- Only spawn falling node if node below is loaded
223                 local n_bottom = core.get_node_or_nil(p_bottom)
224                 local d_bottom = n_bottom and core.registered_nodes[n_bottom.name]
225                 if d_bottom and
226
227                                 (core.get_item_group(n.name, "float") == 0 or
228                                 d_bottom.liquidtype == "none") and
229
230                                 (n.name ~= n_bottom.name or (d_bottom.leveled and
231                                 core.get_node_level(p_bottom) <
232                                 core.get_node_max_level(p_bottom))) and
233
234                                 (not d_bottom.walkable or d_bottom.buildable_to) then
235                         convert_to_falling_node(p, n)
236                         return true
237                 end
238         end
239
240         if core.get_item_group(n.name, "attached_node") ~= 0 then
241                 if not builtin_shared.check_attached_node(p, n) then
242                         drop_attached_node(p)
243                         return true
244                 end
245         end
246
247         return false
248 end
249
250 -- This table is specifically ordered.
251 -- We don't walk diagonals, only our direct neighbors, and self.
252 -- Down first as likely case, but always before self. The same with sides.
253 -- Up must come last, so that things above self will also fall all at once.
254 local check_for_falling_neighbors = {
255         {x = -1, y = -1, z = 0},
256         {x = 1, y = -1, z = 0},
257         {x = 0, y = -1, z = -1},
258         {x = 0, y = -1, z = 1},
259         {x = 0, y = -1, z = 0},
260         {x = -1, y = 0, z = 0},
261         {x = 1, y = 0, z = 0},
262         {x = 0, y = 0, z = 1},
263         {x = 0, y = 0, z = -1},
264         {x = 0, y = 0, z = 0},
265         {x = 0, y = 1, z = 0},
266 }
267
268 function core.check_for_falling(p)
269         -- Round p to prevent falling entities to get stuck.
270         p = vector.round(p)
271
272         -- We make a stack, and manually maintain size for performance.
273         -- Stored in the stack, we will maintain tables with pos, and
274         -- last neighbor visited. This way, when we get back to each
275         -- node, we know which directions we have already walked, and
276         -- which direction is the next to walk.
277         local s = {}
278         local n = 0
279         -- The neighbor order we will visit from our table.
280         local v = 1
281
282         while true do
283                 -- Push current pos onto the stack.
284                 n = n + 1
285                 s[n] = {p = p, v = v}
286                 -- Select next node from neighbor list.
287                 p = vector.add(p, check_for_falling_neighbors[v])
288                 -- Now we check out the node. If it is in need of an update,
289                 -- it will let us know in the return value (true = updated).
290                 if not core.check_single_for_falling(p) then
291                         -- If we don't need to "recurse" (walk) to it then pop
292                         -- our previous pos off the stack and continue from there,
293                         -- with the v value we were at when we last were at that
294                         -- node
295                         repeat
296                                 local pop = s[n]
297                                 p = pop.p
298                                 v = pop.v
299                                 s[n] = nil
300                                 n = n - 1
301                                 -- If there's nothing left on the stack, and no
302                                 -- more sides to walk to, we're done and can exit
303                                 if n == 0 and v == 11 then
304                                         return
305                                 end
306                         until v < 11
307                         -- The next round walk the next neighbor in list.
308                         v = v + 1
309                 else
310                         -- If we did need to walk the neighbor, then
311                         -- start walking it from the walk order start (1),
312                         -- and not the order we just pushed up the stack.
313                         v = 1
314                 end
315         end
316 end
317
318 --
319 -- Global callbacks
320 --
321
322 local function on_placenode(p, node)
323         core.check_for_falling(p)
324 end
325 core.register_on_placenode(on_placenode)
326
327 local function on_dignode(p, node)
328         core.check_for_falling(p)
329 end
330 core.register_on_dignode(on_dignode)
331
332 local function on_punchnode(p, node)
333         core.check_for_falling(p)
334 end
335 core.register_on_punchnode(on_punchnode)