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 = vector.new(SCALE, SCALE, 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 = vector.new(s, s, 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"
114 -- Calculate size of falling node
116 s.x = (def.visual_scale or 1) * SCALE
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
125 self.object:set_properties({
127 wield_item = itemstring,
129 glow = def.light_source,
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
142 if def.paramtype2 == "leveled" and (self.node.level or 0) > 0 then
143 box[5] = -0.5 + self.node.level / 64
145 self.object:set_properties({
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]
165 self.object:set_rotation(euler)
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
174 local pitch, yaw, roll = 0, 0, 0
175 if def.drawtype == "nodebox" or def.drawtype == "mesh" then
177 pitch, yaw = math.pi/2, 0
179 pitch, yaw = -math.pi/2, math.pi
181 pitch, yaw = 0, math.pi/2
183 pitch, yaw = 0, -math.pi/2
185 pitch, yaw = 0, math.pi
189 pitch, yaw = math.pi, math.pi
191 pitch, yaw = math.pi/2, math.pi/2
193 pitch, yaw = math.pi/2, -math.pi/2
195 pitch, yaw = math.pi/2, math.pi
197 pitch, yaw = math.pi/2, 0
200 if def.drawtype == "signlike" then
201 pitch = pitch - math.pi/2
203 yaw = yaw + math.pi/2
205 yaw = yaw - math.pi/2
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
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)
227 get_staticdata = function(self)
232 return core.serialize(ds)
235 on_activate = function(self, staticdata)
236 self.object:set_armor_groups({immortal = 1})
237 self.object:set_acceleration(vector.new(0, -gravity, 0))
239 local ds = core.deserialize(staticdata)
240 if ds and ds.node then
241 self:set_node(ds.node, ds.meta)
244 elseif staticdata ~= "" then
245 self:set_node({name = staticdata})
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
258 if core.add_node_level(bcp, addlevel) < addlevel then
260 elseif bcd.buildable_to then
261 -- Node level has already reached max, don't place anything
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)
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
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
293 local def = core.registered_nodes[self.node.name]
295 core.add_node(np, self.node)
297 core.get_meta(np):from_table(self.meta)
299 if def.sounds and def.sounds.place then
300 core.sound_play(def.sounds.place, {pos = np}, true)
303 core.check_for_falling(np)
307 on_step = function(self, dtime, moveresult)
308 -- Fallback code since collision detection can't tell us
309 -- about liquids (which do not collide)
311 local pos = self.object:get_pos()
313 local bcp = pos:offset(0, -0.7, 0):round()
314 local bcn = core.get_node(bcp)
316 local bcd = core.registered_nodes[bcn.name]
317 if bcd and bcd.liquidtype ~= "none" then
318 if self:try_place(bcp, bcn) then
326 if not moveresult.collides then
327 return -- Nothing to do :)
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
338 elseif info.axis == "y" then
340 bcn = core.get_node(bcp)
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(
356 player_collision.old_velocity.y,
359 self.object:set_pos(self.object:get_pos():offset(0, -0.5, 0))
362 elseif bcn.name == "ignore" then
363 -- Delete on contact with ignore at world edges
368 local failure = false
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
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
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
389 -- Try to actually place ourselves
391 failure = not self:try_place(bcp, bcn)
395 local drops = core.get_node_drops(self.node, "")
396 for _, item in pairs(drops) do
397 core.add_item(pos, item)
404 local function convert_to_falling_node(pos, node)
405 local obj = core.add_entity(pos, "__builtin:falling_node")
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 {}
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)
419 obj:get_luaentity():set_node(node, metatable)
420 core.remove_node(pos)
424 function core.spawn_falling_node(pos)
425 local node = core.get_node(pos)
426 if node.name == "air" or node.name == "ignore" then
429 return convert_to_falling_node(pos, node)
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)
446 def.preserve_metadata(pos_copy, node_copy, oldmeta, drops)
448 if def and def.sounds and def.sounds.fall then
449 core.sound_play(def.sounds.fall, {pos = p}, true)
452 for _, item in pairs(drops) do
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,
458 core.add_item(pos, item)
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)
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
485 -- Some common functions
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]
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)
504 -- Otherwise only if the bottom node is considered "fall through"
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)
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)
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),
543 function core.check_for_falling(p)
544 -- Round p to prevent falling entities to get stuck.
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.
554 -- The neighbor order we will visit from our table.
558 -- Push current pos onto the stack.
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
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
582 -- The next round walk the next neighbor in list.
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.
597 local function on_placenode(p, node)
598 core.check_for_falling(p)
600 core.register_on_placenode(on_placenode)
602 local function on_dignode(p, node)
603 core.check_for_falling(p)
605 core.register_on_dignode(on_dignode)
607 local function on_punchnode(p, node)
608 core.check_for_falling(p)
610 core.register_on_punchnode(on_punchnode)