1 -- Minetest: builtin/item.lua
3 local builtin_shared = ...
6 local facedir_to_euler = {
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}
33 local gravity = tonumber(core.settings:get("movement_gravity")) or 9.81
39 core.register_entity(":__builtin:falling_node", {
40 initial_properties = {
42 visual_size = {x = SCALE, y = SCALE, z = SCALE},
46 collide_with_objects = true,
47 collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
54 set_node = function(self, node, meta)
55 node.param2 = node.param2 or 0
58 if type(meta.to_table) == "function" then
59 meta = meta:to_table()
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()
68 local def = core.registered_nodes[node.name]
70 -- Don't allow unknown nodes to fall
72 "Unknown falling node removed at "..
73 core.pos_to_string(self.object:get_pos()))
79 -- Cache whether we're supposed to float on water
80 self.floats = core.get_item_group(node.name, "float") ~= 0
83 if def.drawtype == "torchlike" or def.drawtype == "signlike" then
85 if def.tiles and def.tiles[1] then
86 local tile = def.tiles[1]
87 if type(tile) == "table" then
90 if def.drawtype == "torchlike" then
91 textures = { "("..tile..")^[transformFX", tile }
93 textures = { tile, "("..tile..")^[transformFX" }
97 if def.visual_scale then
98 local s = def.visual_scale
99 vsize = {x = s, y = s, z = s}
101 self.object:set_properties({
103 visual = "upright_sprite",
106 glow = def.light_source,
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)
113 -- FIXME: solution needed for paramtype2 == "leveled"
115 if def.visual_scale then
116 local s = def.visual_scale * SCALE
117 vsize = {x = s, y = s, z = s}
119 self.object:set_properties({
121 wield_item = itemstring,
123 glow = def.light_source,
127 -- Set collision box (certain nodeboxes only for now)
128 local nb_types = {fixed=true, leveled=true, connected=true}
129 if def.drawtype == "nodebox" and def.node_box and
130 nb_types[def.node_box.type] and def.node_box.fixed then
131 local box = table.copy(def.node_box.fixed)
132 if type(box[1]) == "table" then
133 box = #box == 1 and box[1] or nil -- We can only use a single box
136 if def.paramtype2 == "leveled" and (self.node.level or 0) > 0 then
137 box[5] = -0.5 + self.node.level / 64
139 self.object:set_properties({
146 if def.drawtype == "torchlike" then
147 self.object:set_yaw(math.pi*0.25)
148 elseif ((node.param2 ~= 0 or def.drawtype == "nodebox" or def.drawtype == "mesh")
149 and (def.wield_image == "" or def.wield_image == nil))
150 or def.drawtype == "signlike"
151 or def.drawtype == "mesh"
152 or def.drawtype == "normal"
153 or def.drawtype == "nodebox" then
154 if (def.paramtype2 == "facedir" or def.paramtype2 == "colorfacedir") then
155 local fdir = node.param2 % 32
156 -- Get rotation from a precalculated lookup table
157 local euler = facedir_to_euler[fdir + 1]
159 self.object:set_rotation(euler)
161 elseif (def.paramtype2 == "wallmounted" or def.paramtype2 == "colorwallmounted" or def.drawtype == "signlike") then
162 local rot = node.param2 % 8
163 if (def.drawtype == "signlike" and def.paramtype2 ~= "wallmounted" and def.paramtype2 ~= "colorwallmounted") then
164 -- Change rotation to "floor" by default for non-wallmounted paramtype2
167 local pitch, yaw, roll = 0, 0, 0
168 if def.drawtype == "nodebox" or def.drawtype == "mesh" then
170 pitch, yaw = math.pi/2, 0
172 pitch, yaw = -math.pi/2, math.pi
174 pitch, yaw = 0, math.pi/2
176 pitch, yaw = 0, -math.pi/2
178 pitch, yaw = 0, math.pi
182 pitch, yaw = math.pi, math.pi
184 pitch, yaw = math.pi/2, math.pi/2
186 pitch, yaw = math.pi/2, -math.pi/2
188 pitch, yaw = math.pi/2, math.pi
190 pitch, yaw = math.pi/2, 0
193 if def.drawtype == "signlike" then
194 pitch = pitch - math.pi/2
196 yaw = yaw + math.pi/2
198 yaw = yaw - math.pi/2
200 elseif def.drawtype == "mesh" or def.drawtype == "normal" or def.drawtype == "nodebox" then
201 if rot >= 0 and rot <= 1 then
202 roll = roll + math.pi
207 self.object:set_rotation({x=pitch, y=yaw, z=roll})
212 get_staticdata = function(self)
217 return core.serialize(ds)
220 on_activate = function(self, staticdata)
221 self.object:set_armor_groups({immortal = 1})
222 self.object:set_acceleration({x = 0, y = -gravity, z = 0})
224 local ds = core.deserialize(staticdata)
225 if ds and ds.node then
226 self:set_node(ds.node, ds.meta)
229 elseif staticdata ~= "" then
230 self:set_node({name = staticdata})
234 try_place = function(self, bcp, bcn)
235 local bcd = core.registered_nodes[bcn.name]
236 -- Add levels if dropped on same leveled node
237 if bcd and bcd.paramtype2 == "leveled" and
238 bcn.name == self.node.name then
239 local addlevel = self.node.level
240 if (addlevel or 0) <= 0 then
241 addlevel = bcd.leveled
243 if core.add_node_level(bcp, addlevel) < addlevel then
245 elseif bcd.buildable_to then
246 -- Node level has already reached max, don't place anything
251 -- Decide if we're replacing the node or placing on top
252 local np = vector.new(bcp)
253 if bcd and bcd.buildable_to and
254 (not self.floats or bcd.liquidtype == "none") then
255 core.remove_node(bcp)
261 local n2 = core.get_node(np)
262 local nd = core.registered_nodes[n2.name]
263 -- If it's not air or liquid, remove node and replace it with
265 if n2.name ~= "air" and (not nd or nd.liquidtype == "none") then
266 if nd and nd.buildable_to == false then
267 nd.on_dig(np, n2, nil)
268 -- If it's still there, it might be protected
269 if core.get_node(np).name == n2.name then
278 local def = core.registered_nodes[self.node.name]
280 core.add_node(np, self.node)
282 core.get_meta(np):from_table(self.meta)
284 if def.sounds and def.sounds.place then
285 core.sound_play(def.sounds.place, {pos = np}, true)
288 core.check_for_falling(np)
292 on_step = function(self, dtime, moveresult)
293 -- Fallback code since collision detection can't tell us
294 -- about liquids (which do not collide)
296 local pos = self.object:get_pos()
298 local bcp = vector.round({x = pos.x, y = pos.y - 0.7, z = pos.z})
299 local bcn = core.get_node(bcp)
301 local bcd = core.registered_nodes[bcn.name]
302 if bcd and bcd.liquidtype ~= "none" then
303 if self:try_place(bcp, bcn) then
311 if not moveresult.collides then
312 return -- Nothing to do :)
316 local player_collision
317 if moveresult.touching_ground then
318 for _, info in ipairs(moveresult.collisions) do
319 if info.type == "object" then
320 if info.axis == "y" and info.object:is_player() then
321 player_collision = info
323 elseif info.axis == "y" then
325 bcn = core.get_node(bcp)
332 -- We're colliding with something, but not the ground. Irrelevant to us.
333 if player_collision then
334 -- Continue falling through players by moving a little into
335 -- their collision box
336 -- TODO: this hack could be avoided in the future if objects
337 -- could choose who to collide with
338 local vel = self.object:get_velocity()
339 self.object:set_velocity({
341 y = player_collision.old_velocity.y,
344 self.object:set_pos(vector.add(self.object:get_pos(),
345 {x = 0, y = -0.5, z = 0}))
348 elseif bcn.name == "ignore" then
349 -- Delete on contact with ignore at world edges
354 local failure = false
356 local pos = self.object:get_pos()
357 local distance = vector.apply(vector.subtract(pos, bcp), math.abs)
358 if distance.x >= 1 or distance.z >= 1 then
359 -- We're colliding with some part of a node that's sticking out
360 -- Since we don't want to visually teleport, drop as item
362 elseif distance.y >= 2 then
363 -- Doors consist of a hidden top node and a bottom node that is
364 -- the actual door. Despite the top node being solid, the moveresult
365 -- almost always indicates collision with the bottom node.
366 -- Compensate for this by checking the top node
368 bcn = core.get_node(bcp)
369 local def = core.registered_nodes[bcn.name]
370 if not (def and def.walkable) then
371 failure = true -- This is unexpected, fail
375 -- Try to actually place ourselves
377 failure = not self:try_place(bcp, bcn)
381 local drops = core.get_node_drops(self.node, "")
382 for _, item in pairs(drops) do
383 core.add_item(pos, item)
390 local function convert_to_falling_node(pos, node)
391 local obj = core.add_entity(pos, "__builtin:falling_node")
395 -- remember node level, the entities' set_node() uses this
396 node.level = core.get_node_level(pos)
397 local meta = core.get_meta(pos)
398 local metatable = meta and meta:to_table() or {}
400 local def = core.registered_nodes[node.name]
401 if def and def.sounds and def.sounds.fall then
402 core.sound_play(def.sounds.fall, {pos = pos}, true)
405 obj:get_luaentity():set_node(node, metatable)
406 core.remove_node(pos)
410 function core.spawn_falling_node(pos)
411 local node = core.get_node(pos)
412 if node.name == "air" or node.name == "ignore" then
415 return convert_to_falling_node(pos, node)
418 local function drop_attached_node(p)
419 local n = core.get_node(p)
420 local drops = core.get_node_drops(n, "")
421 local def = core.registered_items[n.name]
422 if def and def.preserve_metadata then
423 local oldmeta = core.get_meta(p):to_table().fields
424 -- Copy pos and node because the callback can modify them.
425 local pos_copy = {x=p.x, y=p.y, z=p.z}
426 local node_copy = {name=n.name, param1=n.param1, param2=n.param2}
427 local drop_stacks = {}
428 for k, v in pairs(drops) do
429 drop_stacks[k] = ItemStack(v)
432 def.preserve_metadata(pos_copy, node_copy, oldmeta, drops)
434 if def and def.sounds and def.sounds.fall then
435 core.sound_play(def.sounds.fall, {pos = p}, true)
438 for _, item in pairs(drops) do
440 x = p.x + math.random()/2 - 0.25,
441 y = p.y + math.random()/2 - 0.25,
442 z = p.z + math.random()/2 - 0.25,
444 core.add_item(pos, item)
448 function builtin_shared.check_attached_node(p, n)
449 local def = core.registered_nodes[n.name]
450 local d = {x = 0, y = 0, z = 0}
451 if def.paramtype2 == "wallmounted" or
452 def.paramtype2 == "colorwallmounted" then
453 -- The fallback vector here is in case 'wallmounted to dir' is nil due
454 -- to voxelmanip placing a wallmounted node without resetting a
455 -- pre-existing param2 value that is out-of-range for wallmounted.
456 -- The fallback vector corresponds to param2 = 0.
457 d = core.wallmounted_to_dir(n.param2) or {x = 0, y = 1, z = 0}
461 local p2 = vector.add(p, d)
462 local nn = core.get_node(p2).name
463 local def2 = core.registered_nodes[nn]
464 if def2 and not def2.walkable then
471 -- Some common functions
474 function core.check_single_for_falling(p)
475 local n = core.get_node(p)
476 if core.get_item_group(n.name, "falling_node") ~= 0 then
477 local p_bottom = {x = p.x, y = p.y - 1, z = p.z}
478 -- Only spawn falling node if node below is loaded
479 local n_bottom = core.get_node_or_nil(p_bottom)
480 local d_bottom = n_bottom and core.registered_nodes[n_bottom.name]
482 local same = n.name == n_bottom.name
483 -- Let leveled nodes fall if it can merge with the bottom node
484 if same and d_bottom.paramtype2 == "leveled" and
485 core.get_node_level(p_bottom) <
486 core.get_node_max_level(p_bottom) then
487 convert_to_falling_node(p, n)
490 -- Otherwise only if the bottom node is considered "fall through"
492 (not d_bottom.walkable or d_bottom.buildable_to) and
493 (core.get_item_group(n.name, "float") == 0 or
494 d_bottom.liquidtype == "none") then
495 convert_to_falling_node(p, n)
501 if core.get_item_group(n.name, "attached_node") ~= 0 then
502 if not builtin_shared.check_attached_node(p, n) then
503 drop_attached_node(p)
511 -- This table is specifically ordered.
512 -- We don't walk diagonals, only our direct neighbors, and self.
513 -- Down first as likely case, but always before self. The same with sides.
514 -- Up must come last, so that things above self will also fall all at once.
515 local check_for_falling_neighbors = {
516 {x = -1, y = -1, z = 0},
517 {x = 1, y = -1, z = 0},
518 {x = 0, y = -1, z = -1},
519 {x = 0, y = -1, z = 1},
520 {x = 0, y = -1, z = 0},
521 {x = -1, y = 0, z = 0},
522 {x = 1, y = 0, z = 0},
523 {x = 0, y = 0, z = 1},
524 {x = 0, y = 0, z = -1},
525 {x = 0, y = 0, z = 0},
526 {x = 0, y = 1, z = 0},
529 function core.check_for_falling(p)
530 -- Round p to prevent falling entities to get stuck.
533 -- We make a stack, and manually maintain size for performance.
534 -- Stored in the stack, we will maintain tables with pos, and
535 -- last neighbor visited. This way, when we get back to each
536 -- node, we know which directions we have already walked, and
537 -- which direction is the next to walk.
540 -- The neighbor order we will visit from our table.
544 -- Push current pos onto the stack.
546 s[n] = {p = p, v = v}
547 -- Select next node from neighbor list.
548 p = vector.add(p, check_for_falling_neighbors[v])
549 -- Now we check out the node. If it is in need of an update,
550 -- it will let us know in the return value (true = updated).
551 if not core.check_single_for_falling(p) then
552 -- If we don't need to "recurse" (walk) to it then pop
553 -- our previous pos off the stack and continue from there,
554 -- with the v value we were at when we last were at that
562 -- If there's nothing left on the stack, and no
563 -- more sides to walk to, we're done and can exit
564 if n == 0 and v == 11 then
568 -- The next round walk the next neighbor in list.
571 -- If we did need to walk the neighbor, then
572 -- start walking it from the walk order start (1),
573 -- and not the order we just pushed up the stack.
583 local function on_placenode(p, node)
584 core.check_for_falling(p)
586 core.register_on_placenode(on_placenode)
588 local function on_dignode(p, node)
589 core.check_for_falling(p)
591 core.register_on_dignode(on_dignode)
593 local function on_punchnode(p, node)
594 core.check_for_falling(p)
596 core.register_on_punchnode(on_punchnode)