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