Adds preconditions and performance profiling

This commit is contained in:
AliasAlreadyTaken 2024-07-02 22:52:40 +02:00
parent 53f9867113
commit 30ce6c3e9f
2 changed files with 58 additions and 12 deletions

View File

@ -1,3 +1,7 @@
local bucket = 0
local numz = 0
-- 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)
@ -45,13 +49,26 @@ end
function yl_api_nodestages.on_construct(pos) return on_construct(pos) end
local function get_target_nodename(next_stages)
local function get_target_nodename(node, pos, next_stages)
local sum = 0
for _, stage_chance in pairs(next_stages) do sum = sum + stage_chance[2] end
local dice = math.random(sum)
for _, stage_chance in pairs(next_stages) do
dice = dice - stage_chance[2]
if dice <= 0 then return stage_chance[1] end
if dice <= 0 then
if (minetest.registered_nodes[stage_chance[1]] == nil) then
minetest.log("warning",
yl_api_nodestages.t(
"target_nodestage_does_not_exist",
dump(stage_chance[1]), dump(node.name),
dump(pos)))
return nil
elseif ((type(stage_chance[3]) == "function") and (stage_chance[3](pos) == false) ) then
return node.name
else
return stage_chance[1]
end
end
end
-- should never return this
@ -60,6 +77,11 @@ end
local function on_timer(pos, elapsed)
core.log("action","MEOW3")
local t1 = core.get_us_time()
numz = numz +1
local node = minetest.get_node(pos)
local stage = minetest.registered_nodes[node.name]._stage
@ -68,23 +90,28 @@ local function on_timer(pos, elapsed)
local duration
if (stage == nil) or (stage.next_stages == nil) then
minetest.get_node_timer(pos):stop()
yl_api_nodestages.remove_timer(pos)
bucket = bucket + (core.get_us_time() - t1)
return
end
repeat
duration = stage.duration
timer = timer + duration
target_nodename = get_target_nodename(stage.next_stages)
target_nodename = get_target_nodename(node, pos, stage.next_stages)
if ((target_nodename == nil) or
(minetest.registered_nodes[target_nodename] == nil)) then
if (target_nodename == yl_api_nodestages.error) then
-- Thing kaputt, stop it.
minetest.log("error", yl_api_nodestages.t("undefined_state_abort"))
yl_api_nodestages.remove_timer(pos)
bucket = bucket + (core.get_us_time() - t1)
return
end
if (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
break
else
-- Last stage most often does not have a _stage
stage = minetest.registered_nodes[target_nodename] and
@ -92,6 +119,13 @@ local function on_timer(pos, elapsed)
end
until ((timer >= elapsed) or (stage == nil) or (stage.duration == 0))
--[[
if (node.name == target_nodename) then
bucket = bucket + (core.get_us_time() - t1)
return true
end
]]--
minetest.set_node(pos, {name = target_nodename})
local remaining = timer - elapsed
@ -99,7 +133,7 @@ local function on_timer(pos, elapsed)
local nodetimer = minetest.get_node_timer(pos)
nodetimer:set(stage.duration, remaining)
end
bucket = bucket + (core.get_us_time() - t1)
end
function yl_api_nodestages.on_timer(pos, elapsed) return on_timer(pos, elapsed) end
@ -145,3 +179,13 @@ end
function yl_api_nodestages.calc_duration(duration) return
calc_duration(duration) end
local function gs(dtime)
if bucket > 0 then
core.chat_send_all("dtime =" .. dump(dtime) .. ", bucket= " .. dump(bucket) .. ", numz=".. dump(numz))
end
bucket = 0
numz = 0
end
core.register_globalstep(gs)

View File

@ -34,4 +34,6 @@ 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"
texts["target_nodestage_does_not_exist"] = "Target nodestage @1 does not exist. Source: @2 Pos: @3"
texts["undefined_state_abort"] = "Thing kaputt, aborting"