]> git.lizzy.rs Git - minetest.git/blob - builtin/game/falling.lua
Add buttons to ContentDB in game bar and configure world (#9944)
[minetest.git] / builtin / game / falling.lua
1 -- Minetest: builtin/item.lua
2
3 local builtin_shared = ...
4 local SCALE = 0.667
5
6 local facedir_to_euler = {
7         {y = 0, x = 0, z = 0},
8         {y = -math.pi/2, x = 0, z = 0},
9         {y = math.pi, x = 0, z = 0},
10         {y = math.pi/2, x = 0, z = 0},
11         {y = math.pi/2, x = -math.pi/2, z = math.pi/2},
12         {y = math.pi/2, x = math.pi, z = math.pi/2},
13         {y = math.pi/2, x = math.pi/2, z = math.pi/2},
14         {y = math.pi/2, x = 0, z = math.pi/2},
15         {y = -math.pi/2, x = math.pi/2, z = math.pi/2},
16         {y = -math.pi/2, x = 0, z = math.pi/2},
17         {y = -math.pi/2, x = -math.pi/2, z = math.pi/2},
18         {y = -math.pi/2, x = math.pi, z = math.pi/2},
19         {y = 0, x = 0, z = math.pi/2},
20         {y = 0, x = -math.pi/2, z = math.pi/2},
21         {y = 0, x = math.pi, z = math.pi/2},
22         {y = 0, x = math.pi/2, z = math.pi/2},
23         {y = math.pi, x = math.pi, z = math.pi/2},
24         {y = math.pi, x = math.pi/2, z = math.pi/2},
25         {y = math.pi, x = 0, z = math.pi/2},
26         {y = math.pi, x = -math.pi/2, z = math.pi/2},
27         {y = math.pi, x = math.pi, z = 0},
28         {y = -math.pi/2, x = math.pi, z = 0},
29         {y = 0, x = math.pi, z = 0},
30         {y = math.pi/2, x = math.pi, z = 0}
31 }
32
33 local gravity = tonumber(core.settings:get("movement_gravity")) or 9.81
34
35 --
36 -- Falling stuff
37 --
38
39 core.register_entity(":__builtin:falling_node", {
40         initial_properties = {
41                 visual = "item",
42                 visual_size = {x = SCALE, y = SCALE, z = SCALE},
43                 textures = {},
44                 physical = true,
45                 is_visible = false,
46                 collide_with_objects = true,
47                 collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
48         },
49
50         node = {},
51         meta = {},
52         floats = false,
53
54         set_node = function(self, node, meta)
55                 self.node = node
56                 meta = meta or {}
57                 if type(meta.to_table) == "function" then
58                         meta = meta:to_table()
59                 end
60                 for _, list in pairs(meta.inventory or {}) do
61                         for i, stack in pairs(list) do
62                                 if type(stack) == "userdata" then
63                                         list[i] = stack:to_string()
64                                 end
65                         end
66                 end
67                 local def = core.registered_nodes[node.name]
68                 if not def then
69                         -- Don't allow unknown nodes to fall
70                         core.log("info",
71                                 "Unknown falling node removed at "..
72                                 core.pos_to_string(self.object:get_pos()))
73                         self.object:remove()
74                         return
75                 end
76                 self.meta = meta
77
78                 -- Cache whether we're supposed to float on water
79                 self.floats = core.get_item_group(node.name, "float") ~= 0
80
81                 -- Set entity visuals
82                 if def.drawtype == "torchlike" or def.drawtype == "signlike" then
83                         local textures
84                         if def.tiles and def.tiles[1] then
85                                 local tile = def.tiles[1]
86                                 if type(tile) == "table" then
87                                         tile = tile.name
88                                 end
89                                 if def.drawtype == "torchlike" then
90                                         textures = { "("..tile..")^[transformFX", tile }
91                                 else
92                                         textures = { tile, "("..tile..")^[transformFX" }
93                                 end
94                         end
95                         local vsize
96                         if def.visual_scale then
97                                 local s = def.visual_scale
98                                 vsize = {x = s, y = s, z = s}
99                         end
100                         self.object:set_properties({
101                                 is_visible = true,
102                                 visual = "upright_sprite",
103                                 visual_size = vsize,
104                                 textures = textures,
105                                 glow = def.light_source,
106                         })
107                 elseif def.drawtype ~= "airlike" then
108                         local itemstring = node.name
109                         if core.is_colored_paramtype(def.paramtype2) then
110                                 itemstring = core.itemstring_with_palette(itemstring, node.param2)
111                         end
112                         -- FIXME: solution needed for paramtype2 == "leveled"
113                         local vsize
114                         if def.visual_scale then
115                                 local s = def.visual_scale * SCALE
116                                 vsize = {x = s, y = s, z = s}
117                         end
118                         self.object:set_properties({
119                                 is_visible = true,
120                                 wield_item = itemstring,
121                                 visual_size = vsize,
122                                 glow = def.light_source,
123                         })
124                 end
125
126                 -- Set collision box (certain nodeboxes only for now)
127                 local nb_types = {fixed=true, leveled=true, connected=true}
128                 if def.drawtype == "nodebox" and def.node_box and
129                         nb_types[def.node_box.type] then
130                         local box = table.copy(def.node_box.fixed)
131                         if type(box[1]) == "table" then
132                                 box = #box == 1 and box[1] or nil -- We can only use a single box
133                         end
134                         if box then
135                                 if def.paramtype2 == "leveled" and (self.node.level or 0) > 0 then
136                                         box[5] = -0.5 + self.node.level / 64
137                                 end
138                                 self.object:set_properties({
139                                         collisionbox = box
140                                 })
141                         end
142                 end
143
144                 -- Rotate entity
145                 if def.drawtype == "torchlike" then
146                         self.object:set_yaw(math.pi*0.25)
147                 elseif (node.param2 ~= 0 and (def.wield_image == ""
148                                 or def.wield_image == nil))
149                                 or def.drawtype == "signlike"
150                                 or def.drawtype == "mesh"
151                                 or def.drawtype == "normal"
152                                 or def.drawtype == "nodebox" then
153                         if (def.paramtype2 == "facedir" or def.paramtype2 == "colorfacedir") then
154                                 local fdir = node.param2 % 32
155                                 -- Get rotation from a precalculated lookup table
156                                 local euler = facedir_to_euler[fdir + 1]
157                                 if euler then
158                                         self.object:set_rotation(euler)
159                                 end
160                         elseif (def.paramtype2 == "wallmounted" or def.paramtype2 == "colorwallmounted") then
161                                 local rot = node.param2 % 8
162                                 local pitch, yaw, roll = 0, 0, 0
163                                 if rot == 1 then
164                                         pitch, yaw = math.pi, math.pi
165                                 elseif rot == 2 then
166                                         pitch, yaw = math.pi/2, math.pi/2
167                                 elseif rot == 3 then
168                                         pitch, yaw = math.pi/2, -math.pi/2
169                                 elseif rot == 4 then
170                                         pitch, yaw = math.pi/2, math.pi
171                                 elseif rot == 5 then
172                                         pitch, yaw = math.pi/2, 0
173                                 end
174                                 if def.drawtype == "signlike" then
175                                         pitch = pitch - math.pi/2
176                                         if rot == 0 then
177                                                 yaw = yaw + math.pi/2
178                                         elseif rot == 1 then
179                                                 yaw = yaw - math.pi/2
180                                         end
181                                 elseif def.drawtype == "mesh" or def.drawtype == "normal" then
182                                         if rot >= 0 and rot <= 1 then
183                                                 roll = roll + math.pi
184                                         else
185                                                 yaw = yaw + math.pi
186                                         end
187                                 end
188                                 self.object:set_rotation({x=pitch, y=yaw, z=roll})
189                         end
190                 end
191         end,
192
193         get_staticdata = function(self)
194                 local ds = {
195                         node = self.node,
196                         meta = self.meta,
197                 }
198                 return core.serialize(ds)
199         end,
200
201         on_activate = function(self, staticdata)
202                 self.object:set_armor_groups({immortal = 1})
203                 self.object:set_acceleration({x = 0, y = -gravity, z = 0})
204
205                 local ds = core.deserialize(staticdata)
206                 if ds and ds.node then
207                         self:set_node(ds.node, ds.meta)
208                 elseif ds then
209                         self:set_node(ds)
210                 elseif staticdata ~= "" then
211                         self:set_node({name = staticdata})
212                 end
213         end,
214
215         try_place = function(self, bcp, bcn)
216                 local bcd = core.registered_nodes[bcn.name]
217                 -- Add levels if dropped on same leveled node
218                 if bcd and bcd.paramtype2 == "leveled" and
219                                 bcn.name == self.node.name then
220                         local addlevel = self.node.level
221                         if (addlevel or 0) <= 0 then
222                                 addlevel = bcd.leveled
223                         end
224                         if core.add_node_level(bcp, addlevel) < addlevel then
225                                 return true
226                         elseif bcd.buildable_to then
227                                 -- Node level has already reached max, don't place anything
228                                 return true
229                         end
230                 end
231
232                 -- Decide if we're replacing the node or placing on top
233                 local np = vector.new(bcp)
234                 if bcd and bcd.buildable_to and
235                                 (not self.floats or bcd.liquidtype == "none") then
236                         core.remove_node(bcp)
237                 else
238                         np.y = np.y + 1
239                 end
240
241                 -- Check what's here
242                 local n2 = core.get_node(np)
243                 local nd = core.registered_nodes[n2.name]
244                 -- If it's not air or liquid, remove node and replace it with
245                 -- it's drops
246                 if n2.name ~= "air" and (not nd or nd.liquidtype == "none") then
247                         if nd and nd.buildable_to == false then
248                                 nd.on_dig(np, n2, nil)
249                                 -- If it's still there, it might be protected
250                                 if core.get_node(np).name == n2.name then
251                                         return false
252                                 end
253                         else
254                                 core.remove_node(np)
255                         end
256                 end
257
258                 -- Create node
259                 local def = core.registered_nodes[self.node.name]
260                 if def then
261                         core.add_node(np, self.node)
262                         if self.meta then
263                                 core.get_meta(np):from_table(self.meta)
264                         end
265                         if def.sounds and def.sounds.place then
266                                 core.sound_play(def.sounds.place, {pos = np}, true)
267                         end
268                 end
269                 core.check_for_falling(np)
270                 return true
271         end,
272
273         on_step = function(self, dtime, moveresult)
274                 -- Fallback code since collision detection can't tell us
275                 -- about liquids (which do not collide)
276                 if self.floats then
277                         local pos = self.object:get_pos()
278
279                         local bcp = vector.round({x = pos.x, y = pos.y - 0.7, z = pos.z})
280                         local bcn = core.get_node(bcp)
281
282                         local bcd = core.registered_nodes[bcn.name]
283                         if bcd and bcd.liquidtype ~= "none" then
284                                 if self:try_place(bcp, bcn) then
285                                         self.object:remove()
286                                         return
287                                 end
288                         end
289                 end
290
291                 assert(moveresult)
292                 if not moveresult.collides then
293                         return -- Nothing to do :)
294                 end
295
296                 local bcp, bcn
297                 local player_collision
298                 if moveresult.touching_ground then
299                         for _, info in ipairs(moveresult.collisions) do
300                                 if info.type == "object" then
301                                         if info.axis == "y" and info.object:is_player() then
302                                                 player_collision = info
303                                         end
304                                 elseif info.axis == "y" then
305                                         bcp = info.node_pos
306                                         bcn = core.get_node(bcp)
307                                         break
308                                 end
309                         end
310                 end
311
312                 if not bcp then
313                         -- We're colliding with something, but not the ground. Irrelevant to us.
314                         if player_collision then
315                                 -- Continue falling through players by moving a little into
316                                 -- their collision box
317                                 -- TODO: this hack could be avoided in the future if objects
318                                 --       could choose who to collide with
319                                 local vel = self.object:get_velocity()
320                                 self.object:set_velocity({
321                                         x = vel.x,
322                                         y = player_collision.old_velocity.y,
323                                         z = vel.z
324                                 })
325                                 self.object:set_pos(vector.add(self.object:get_pos(),
326                                         {x = 0, y = -0.5, z = 0}))
327                         end
328                         return
329                 elseif bcn.name == "ignore" then
330                         -- Delete on contact with ignore at world edges
331                         self.object:remove()
332                         return
333                 end
334
335                 local failure = false
336
337                 local pos = self.object:get_pos()
338                 local distance = vector.apply(vector.subtract(pos, bcp), math.abs)
339                 if distance.x >= 1 or distance.z >= 1 then
340                         -- We're colliding with some part of a node that's sticking out
341                         -- Since we don't want to visually teleport, drop as item
342                         failure = true
343                 elseif distance.y >= 2 then
344                         -- Doors consist of a hidden top node and a bottom node that is
345                         -- the actual door. Despite the top node being solid, the moveresult
346                         -- almost always indicates collision with the bottom node.
347                         -- Compensate for this by checking the top node
348                         bcp.y = bcp.y + 1
349                         bcn = core.get_node(bcp)
350                         local def = core.registered_nodes[bcn.name]
351                         if not (def and def.walkable) then
352                                 failure = true -- This is unexpected, fail
353                         end
354                 end
355
356                 -- Try to actually place ourselves
357                 if not failure then
358                         failure = not self:try_place(bcp, bcn)
359                 end
360
361                 if failure then
362                         local drops = core.get_node_drops(self.node, "")
363                         for _, item in pairs(drops) do
364                                 core.add_item(pos, item)
365                         end
366                 end
367                 self.object:remove()
368         end
369 })
370
371 local function convert_to_falling_node(pos, node)
372         local obj = core.add_entity(pos, "__builtin:falling_node")
373         if not obj then
374                 return false
375         end
376         -- remember node level, the entities' set_node() uses this
377         node.level = core.get_node_level(pos)
378         local meta = core.get_meta(pos)
379         local metatable = meta and meta:to_table() or {}
380
381         local def = core.registered_nodes[node.name]
382         if def and def.sounds and def.sounds.fall then
383                 core.sound_play(def.sounds.fall, {pos = pos}, true)
384         end
385
386         obj:get_luaentity():set_node(node, metatable)
387         core.remove_node(pos)
388         return true
389 end
390
391 function core.spawn_falling_node(pos)
392         local node = core.get_node(pos)
393         if node.name == "air" or node.name == "ignore" then
394                 return false
395         end
396         return convert_to_falling_node(pos, node)
397 end
398
399 local function drop_attached_node(p)
400         local n = core.get_node(p)
401         local drops = core.get_node_drops(n, "")
402         local def = core.registered_items[n.name]
403         if def and def.preserve_metadata then
404                 local oldmeta = core.get_meta(p):to_table().fields
405                 -- Copy pos and node because the callback can modify them.
406                 local pos_copy = {x=p.x, y=p.y, z=p.z}
407                 local node_copy = {name=n.name, param1=n.param1, param2=n.param2}
408                 local drop_stacks = {}
409                 for k, v in pairs(drops) do
410                         drop_stacks[k] = ItemStack(v)
411                 end
412                 drops = drop_stacks
413                 def.preserve_metadata(pos_copy, node_copy, oldmeta, drops)
414         end
415         if def and def.sounds and def.sounds.fall then
416                 core.sound_play(def.sounds.fall, {pos = p}, true)
417         end
418         core.remove_node(p)
419         for _, item in pairs(drops) do
420                 local pos = {
421                         x = p.x + math.random()/2 - 0.25,
422                         y = p.y + math.random()/2 - 0.25,
423                         z = p.z + math.random()/2 - 0.25,
424                 }
425                 core.add_item(pos, item)
426         end
427 end
428
429 function builtin_shared.check_attached_node(p, n)
430         local def = core.registered_nodes[n.name]
431         local d = {x = 0, y = 0, z = 0}
432         if def.paramtype2 == "wallmounted" or
433                         def.paramtype2 == "colorwallmounted" then
434                 -- The fallback vector here is in case 'wallmounted to dir' is nil due
435                 -- to voxelmanip placing a wallmounted node without resetting a
436                 -- pre-existing param2 value that is out-of-range for wallmounted.
437                 -- The fallback vector corresponds to param2 = 0.
438                 d = core.wallmounted_to_dir(n.param2) or {x = 0, y = 1, z = 0}
439         else
440                 d.y = -1
441         end
442         local p2 = vector.add(p, d)
443         local nn = core.get_node(p2).name
444         local def2 = core.registered_nodes[nn]
445         if def2 and not def2.walkable then
446                 return false
447         end
448         return true
449 end
450
451 --
452 -- Some common functions
453 --
454
455 function core.check_single_for_falling(p)
456         local n = core.get_node(p)
457         if core.get_item_group(n.name, "falling_node") ~= 0 then
458                 local p_bottom = {x = p.x, y = p.y - 1, z = p.z}
459                 -- Only spawn falling node if node below is loaded
460                 local n_bottom = core.get_node_or_nil(p_bottom)
461                 local d_bottom = n_bottom and core.registered_nodes[n_bottom.name]
462                 if d_bottom then
463                         local same = n.name == n_bottom.name
464                         -- Let leveled nodes fall if it can merge with the bottom node
465                         if same and d_bottom.paramtype2 == "leveled" and
466                                         core.get_node_level(p_bottom) <
467                                         core.get_node_max_level(p_bottom) then
468                                 convert_to_falling_node(p, n)
469                                 return true
470                         end
471                         -- Otherwise only if the bottom node is considered "fall through"
472                         if not same and
473                                         (not d_bottom.walkable or d_bottom.buildable_to) and
474                                         (core.get_item_group(n.name, "float") == 0 or
475                                         d_bottom.liquidtype == "none") then
476                                 convert_to_falling_node(p, n)
477                                 return true
478                         end
479                 end
480         end
481
482         if core.get_item_group(n.name, "attached_node") ~= 0 then
483                 if not builtin_shared.check_attached_node(p, n) then
484                         drop_attached_node(p)
485                         return true
486                 end
487         end
488
489         return false
490 end
491
492 -- This table is specifically ordered.
493 -- We don't walk diagonals, only our direct neighbors, and self.
494 -- Down first as likely case, but always before self. The same with sides.
495 -- Up must come last, so that things above self will also fall all at once.
496 local check_for_falling_neighbors = {
497         {x = -1, y = -1, z = 0},
498         {x = 1, y = -1, z = 0},
499         {x = 0, y = -1, z = -1},
500         {x = 0, y = -1, z = 1},
501         {x = 0, y = -1, z = 0},
502         {x = -1, y = 0, z = 0},
503         {x = 1, y = 0, z = 0},
504         {x = 0, y = 0, z = 1},
505         {x = 0, y = 0, z = -1},
506         {x = 0, y = 0, z = 0},
507         {x = 0, y = 1, z = 0},
508 }
509
510 function core.check_for_falling(p)
511         -- Round p to prevent falling entities to get stuck.
512         p = vector.round(p)
513
514         -- We make a stack, and manually maintain size for performance.
515         -- Stored in the stack, we will maintain tables with pos, and
516         -- last neighbor visited. This way, when we get back to each
517         -- node, we know which directions we have already walked, and
518         -- which direction is the next to walk.
519         local s = {}
520         local n = 0
521         -- The neighbor order we will visit from our table.
522         local v = 1
523
524         while true do
525                 -- Push current pos onto the stack.
526                 n = n + 1
527                 s[n] = {p = p, v = v}
528                 -- Select next node from neighbor list.
529                 p = vector.add(p, check_for_falling_neighbors[v])
530                 -- Now we check out the node. If it is in need of an update,
531                 -- it will let us know in the return value (true = updated).
532                 if not core.check_single_for_falling(p) then
533                         -- If we don't need to "recurse" (walk) to it then pop
534                         -- our previous pos off the stack and continue from there,
535                         -- with the v value we were at when we last were at that
536                         -- node
537                         repeat
538                                 local pop = s[n]
539                                 p = pop.p
540                                 v = pop.v
541                                 s[n] = nil
542                                 n = n - 1
543                                 -- If there's nothing left on the stack, and no
544                                 -- more sides to walk to, we're done and can exit
545                                 if n == 0 and v == 11 then
546                                         return
547                                 end
548                         until v < 11
549                         -- The next round walk the next neighbor in list.
550                         v = v + 1
551                 else
552                         -- If we did need to walk the neighbor, then
553                         -- start walking it from the walk order start (1),
554                         -- and not the order we just pushed up the stack.
555                         v = 1
556                 end
557         end
558 end
559
560 --
561 -- Global callbacks
562 --
563
564 local function on_placenode(p, node)
565         core.check_for_falling(p)
566 end
567 core.register_on_placenode(on_placenode)
568
569 local function on_dignode(p, node)
570         core.check_for_falling(p)
571 end
572 core.register_on_dignode(on_dignode)
573
574 local function on_punchnode(p, node)
575         core.check_for_falling(p)
576 end
577 core.register_on_punchnode(on_punchnode)