Fixes last nodestage cannot be reached in absentia

This commit is contained in:
AliasAlreadyTaken 2024-07-02 18:15:05 +02:00
parent 1c159c47c9
commit 53f9867113
3 changed files with 32 additions and 18 deletions

View File

@ -1,8 +1,8 @@
function yl_api_nodestages.create_stage(modname, itemname, stages, overwrite)
function yl_api_nodestages.create_stage(stage, overwrite)
-- Creates a single stage
end
function yl_api_nodestages.add_stage(modname, itemname, stages, overwrite)
function yl_api_nodestages.add_stage(stage, overwrite)
-- Adds a stage to an existing block
end
@ -100,6 +100,6 @@ end
-- Delete
--
function yl_api_nodestages.remove_stages(modname, itemname)
return yl_api_nodestages.register_stages(modname, itemname, {}, true)
function yl_api_nodestages.remove_stage(stage)
return yl_api_nodestages.register_stage(modname, itemname, {}, true)
end

View File

@ -1,8 +1,11 @@
-- The functions and variables in this file are only for use in the mod itself.
-- Those that do real work should be local and wrapped in public functions
local function log(text)
local logmessage = yl_api_nodestages.t("log_prefix", yl_api_nodestages.modname, text)
if yl_api_nodestages.settings.debug then minetest.log("action", logmessage) end
local logmessage = yl_api_nodestages.t("log_prefix",
yl_api_nodestages.modname, text)
if yl_api_nodestages.settings.debug then
minetest.log("action", logmessage)
end
return logmessage
end
@ -42,7 +45,7 @@ end
function yl_api_nodestages.on_construct(pos) return on_construct(pos) end
local function get_next_stage(next_stages)
local function get_target_nodename(next_stages)
local sum = 0
for _, stage_chance in pairs(next_stages) do sum = sum + stage_chance[2] end
local dice = math.random(sum)
@ -72,14 +75,22 @@ local function on_timer(pos, elapsed)
repeat
duration = stage.duration
timer = timer + duration
target_nodename = get_next_stage(stage.next_stages)
-- Last stage most often does not have a _stage
stage = minetest.registered_nodes[target_nodename] and
minetest.registered_nodes[target_nodename]._stage or nil
until ((timer >= elapsed) or (stage == nil) or (stage.duration == 0))
target_nodename = get_target_nodename(stage.next_stages)
if ((target_nodename == nil) or
(minetest.registered_nodes[target_nodename] == nil)) then return end
if ((target_nodename == nil) or
(minetest.registered_nodes[target_nodename] == nil)) then
-- Fallback to last known target_nodename
minetest.log("warning",
yl_api_nodestages.t("target_nodestage_does_not_exist",
dump(target_nodename),
dump(node.name)))
target_nodename = stage.stage_name
else
-- Last stage most often does not have a _stage
stage = minetest.registered_nodes[target_nodename] and
minetest.registered_nodes[target_nodename]._stage or nil
end
until ((timer >= elapsed) or (stage == nil) or (stage.duration == 0))
minetest.set_node(pos, {name = target_nodename})
@ -132,4 +143,5 @@ function calc_duration(duration)
return {min = d_min, max = d_max}
end
function yl_api_nodestages.calc_duration(duration) return calc_duration(duration) end
function yl_api_nodestages.calc_duration(duration) return
calc_duration(duration) end

View File

@ -3,18 +3,18 @@ local S = minetest.get_translator(yl_api_nodestages.modname)
local texts = {}
--function yl_api_nodestages.t(key, ...) return S(texts[key], ...) or "" end
function yl_api_nodestages.t(key, ...) return string.format(texts[key], ...) or "" end
function yl_api_nodestages.t(key, ...) return S(texts[key], ...) or "" end
-- Fixed texts
texts["log_prefix"] = "[MOD] %s : %s"
texts["log_prefix"] = "[MOD] @1 : @2"
texts["get_savepath"] = "get_savepath : @1"
texts["get_filepath"] = "get_filepath : @1"
-- Translateable texts
texts["information_additional"] = S("YL Food API")
texts["information_additional"] = "YL Food API"
texts["api_sent_x_to_y"] = "Sent @1 to @2"
@ -33,3 +33,5 @@ texts["error_item_exists"] = "@1 already exists (Stage: @2), but overwrite was n
texts["error_not_a_position"] = "@1 not a position"
texts["error_function_not_available"] = "@1 not available"
texts["target_nodestage_does_not_exist"] = "Target nodestage @1 does not exist. Source: @2"