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