blockgame/mods/bg_api/loose_nodes.lua
trans_soup 263e98d526 add basic api support for loose nodes.
expose an api function to register "loose" versions of existing nodes,
and implement some behaviour for such nodes; they will fall down if
there is air below them, and will eventually become their non-loose
counterparts if there is not air below them.
2023-10-16 10:46:32 +02:00

63 lines
1.6 KiB
Lua

-- TODO: collapse nodes when the node under them is removed.
local function attempt_collapse_at (pos)
local below = pos + blockgame.vector.dirs.down
if minetest.get_node(below).name ~= "air" then return end
minetest.spawn_falling_node(pos)
end
local function place_loose_node (itemstack, placer, pointed)
local result = minetest.item_place(itemstack, placer, pointed)
attempt_collapse_at(pointed.above)
return result
end
-- collapse nodes that somehow managed to stay in the air.
blockgame.register_abm({
label = "collapse collapsible nodes",
nodenames = {"group:loose"},
neighbors = {"air"},
interval = 15,
chance = 1,
action = attempt_collapse_at,
})
local function attempt_settle_at (pos)
local below = pos + blockgame.vector.dirs.down
if minetest.get_node(below).name == "air" then return end
local def = minetest.registered_nodes[minetest.get_node(pos).name]
minetest.set_node(pos, {name = def.stable_version})
end
-- settle nodes that are still (in the future, it will take some time for loose nodes that are still to settle).
blockgame.register_abm({
label = "settle collapsible nodes",
nodenames = {"group:loose"},
interval = 30,
chance = 10,
action = attempt_settle_at,
})
function blockgame.register_loose_version (name)
local def = minetest.registered_nodes[name]
if not def then return false end
local groups = {}
for key, value in pairs(def.groups) do
groups[key] = value
end
groups.loose = 1
blockgame.register_node(name .. "_loose", {
description = "Loose " .. def.description,
tiles = def.tiles, -- temporary
groups = groups,
on_place = place_loose_node,
stable_version = name,
})
return true
end