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.
This commit is contained in:
trans_soup 2023-10-16 10:46:32 +02:00
parent 20b29c833f
commit 263e98d526
2 changed files with 64 additions and 0 deletions

View file

@ -17,5 +17,6 @@ load_file("util_node")
load_file("wrappers")
load_file("random_tick")
load_file("loose_nodes")
load_file("cleanup")

View file

@ -0,0 +1,63 @@
-- 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