From ae937581e7167e2d6e9444b6ac2cd00bf2e2beca Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Tue, 10 Sep 2019 07:24:51 +0200 Subject: Revert "remove hacky overrides" This reverts commit f9f5339e71b4768cc035b58da7b785d294fe1beb. --- actionqueue.lua | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ globalstep.lua | 43 +++++++++++++++++++++++++++++++++ init.lua | 3 ++- 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 actionqueue.lua create mode 100644 globalstep.lua diff --git a/actionqueue.lua b/actionqueue.lua new file mode 100644 index 0000000..3f1862f --- /dev/null +++ b/actionqueue.lua @@ -0,0 +1,74 @@ +-- smarter mesecons actionqueue +-- TODO: create PR if ot works properly + + +-- execute the stored functions on a globalstep +-- if however, the pos of a function is not loaded (get_node_or_nil == nil), do NOT execute the function +-- this makes sure that resuming mesecons circuits when restarting minetest works fine +-- However, even that does not work in some cases, that's why we delay the time the globalsteps +-- start to be execute by 5 seconds +local get_highest_priority = function (actions) + local highestp = -1 + local highesti + for i, ac in ipairs(actions) do + if ac.priority > highestp then + highestp = ac.priority + highesti = i + end + end + + return highesti +end + +local m_time = 0 +local resumetime = mesecon.setting("resumetime", 4) +minetest.register_globalstep(function (dtime) + m_time = m_time + dtime + -- don't even try if server has not been running for XY seconds; resumetime = time to wait + -- after starting the server before processing the ActionQueue, don't set this too low + if (m_time < resumetime) then return end + + if not mesecons_debug.enabled then + return + end + + local actions = mesecon.tablecopy(mesecon.queue.actions) + local actions_now={} + + mesecon.queue.actions = {} + + -- sort actions into two categories: + -- those toexecute now (actions_now) and those to execute later (mesecon.queue.actions) + for _, ac in ipairs(actions) do + if ac.time > 0 then + ac.time = ac.time - dtime -- executed later + table.insert(mesecon.queue.actions, ac) + else + table.insert(actions_now, ac) + end + end + + if #actions_now > 30000 then + -- too much actions, purge them + return + end + + local t0 = minetest.get_us_time() + + while(#actions_now > 0) do -- execute highest priorities first, until all are executed + local hp = get_highest_priority(actions_now) + local action = actions_now[hp] + + local t1 = minetest.get_us_time() + local diff = t1 - t0 + if diff > 75000 then + -- execute remaining actions in next globalstep + table.insert(mesecon.queue.actions, 1, action) + else + mesecon.queue:execute(action) + table.remove(actions_now, hp) + end + + end +end) + diff --git a/globalstep.lua b/globalstep.lua new file mode 100644 index 0000000..cd0d95d --- /dev/null +++ b/globalstep.lua @@ -0,0 +1,43 @@ + +-- globalstep on/off +local i = 0 +for _, globalstep in ipairs(minetest.registered_globalsteps) do + local info = minetest.callback_origins[globalstep] + if not info then + break + end + + local modname = info.mod + + if modname == "mesecons" then + i = i + 1 + -- 1 = execute globalstep + -- 2 = cooldown globalstep + if i == 1 then + local fn = function(dtime) + globalstep(dtime) + end + + minetest.callback_origins[fn] = info + minetest.registered_globalsteps[i] = fn + end + end +end + +-- execute() +local old_execute = mesecon.queue.execute +mesecon.queue.execute = function(...) + if mesecons_debug.enabled then + old_execute(...) + end +end + +-- add_action() +local old_add_action = mesecon.queue.add_action +mesecon.queue.add_action = function(...) + if mesecons_debug.enabled then + old_add_action(...) + end +end + + diff --git a/init.lua b/init.lua index 6653914..8a3eb3a 100644 --- a/init.lua +++ b/init.lua @@ -10,6 +10,7 @@ dofile(MP.."/api_action_on.lua") dofile(MP.."/api_nodetimer.lua") dofile(MP.."/register.lua") dofile(MP.."/flush.lua") - +dofile(MP.."/globalstep.lua") +dofile(MP.."/actionqueue.lua") print("[OK] mesecons_debug loaded") -- cgit v1.2.3