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:
parent
2de047d37a
commit
249a698b52
1 changed files with 19 additions and 13 deletions
|
@ -4,6 +4,21 @@ local function is_same_kind (pos, kindname)
|
||||||
return kindname and node_kindname and kindname == node_kindname
|
return kindname and node_kindname and kindname == node_kindname
|
||||||
end
|
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)
|
function blockgame.check_for_stacking (pos)
|
||||||
local node = minetest.get_node(pos)
|
local node = minetest.get_node(pos)
|
||||||
if not blockgame.item_matches(node.name, {"group:leveled"}) then return end
|
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 kindname = def.kindname
|
||||||
local level_max = def.level_max
|
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
|
if is_same_kind(below, kindname) then
|
||||||
local below_node = minetest.get_node(below)
|
local new_level = stack_attempt(below, def.level)
|
||||||
local below_def = minetest.registered_nodes[below_node.name]
|
|
||||||
|
|
||||||
if below_def.level ~= level_max then
|
if new_level ~= def.level then
|
||||||
local max_diff = level_max - below_def.level
|
minetest.swap_node(pos, {name = kindname .. "_" .. new_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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue