refactor: stacking of leveled nodes.

extract level transfer between leveled nodes into own function, separate
from code that decides when and how to use that.
This commit is contained in:
trans_soup 2023-10-22 22:09:22 +02:00
parent 2de047d37a
commit 249a698b52

View file

@ -4,6 +4,21 @@ local function is_same_kind (pos, kindname)
return kindname and node_kindname and kindname == node_kindname
end
local function stack_attempt (pos, source_level)
local node = minetest.get_node(pos)
local def = minetest.registered_nodes[node.name]
if def.level == level_max then return source_level end
local level_max = def.level_max
local kindname = def.kindname
local max_diff = level_max - def.level
local diff = math.min(max_diff, source_level)
minetest.swap_node(pos, {name = kindname .. "_" .. (def.level + diff)})
return source_level - diff
end
function blockgame.check_for_stacking (pos)
local node = minetest.get_node(pos)
if not blockgame.item_matches(node.name, {"group:leveled"}) then return end
@ -11,21 +26,12 @@ function blockgame.check_for_stacking (pos)
local kindname = def.kindname
local level_max = def.level_max
local below = pos + blockgame.vector.dirs.down
local below = pos:offset(0, -1, 0)
if is_same_kind(below, kindname) then
local below_node = minetest.get_node(below)
local below_def = minetest.registered_nodes[below_node.name]
local new_level = stack_attempt(below, def.level)
if below_def.level ~= level_max then
local max_diff = level_max - below_def.level
local diff = math.min(max_diff, def.level)
minetest.swap_node(below, {name = kindname .. "_" .. (below_def.level + diff)})
if diff == def.level then
minetest.remove_node(pos)
else
minetest.swap_node(pos, {name = kindname .. "_" .. (def.level - diff)})
end
if new_level ~= def.level then
minetest.swap_node(pos, {name = kindname .. "_" .. new_level})
end
end