From 8ab47b2c7814b002facc05bc1f12405b76598f8a Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Mon, 3 Feb 2020 10:29:20 +0100 Subject: [PATCH] reduce technic_run interval if lag is too high / hot-reload offdelay setting / cache-flush command --- README.md | 4 +++ technic/machines/init.lua | 1 + technic/machines/max_lag.lua | 15 +++++++++++ .../machines/switching_station_globalstep.lua | 27 +++++++++++++++++-- 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 technic/machines/max_lag.lua diff --git a/README.md b/README.md index f144a4e..42e3692 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,10 @@ Recommended mods that build on the `technic mod`: * **technic.switch_max_range** max cable length (default: 256) * **technic.switch.off_delay_seconds** switching station off delay (default: 300 seconds) +# Chat commands + +* **/technic_flush_switch_cache** clears the switching station cache (stops all unloaded switches) + # Open issues * Documentation (markdown) diff --git a/technic/machines/init.lua b/technic/machines/init.lua index 735dc1f..fe087d1 100644 --- a/technic/machines/init.lua +++ b/technic/machines/init.lua @@ -7,6 +7,7 @@ dofile(path.."/LV/init.lua") dofile(path.."/MV/init.lua") dofile(path.."/HV/init.lua") +dofile(path.."/max_lag.lua") dofile(path.."/switching_station.lua") dofile(path.."/switching_station_globalstep.lua") diff --git a/technic/machines/max_lag.lua b/technic/machines/max_lag.lua new file mode 100644 index 0000000..af412db --- /dev/null +++ b/technic/machines/max_lag.lua @@ -0,0 +1,15 @@ +local function explode(sep, input) + local t={} + local i=0 + for k in string.gmatch(input,"([^"..sep.."]+)") do + t[i]=k + i=i+1 + end + return t +end + +function technic.get_max_lag() + local arrayoutput = explode(", ",minetest.get_server_status()) + local arrayoutput2 = explode("=",arrayoutput[4]) + return tonumber(arrayoutput2[1]) +end diff --git a/technic/machines/switching_station_globalstep.lua b/technic/machines/switching_station_globalstep.lua index e7ff2aa..8b354a1 100644 --- a/technic/machines/switching_station_globalstep.lua +++ b/technic/machines/switching_station_globalstep.lua @@ -44,19 +44,33 @@ minetest.register_abm({ end }) -local off_delay_seconds = tonumber(minetest.settings:get("technic.switch.off_delay_seconds") or "300") + +-- the interval between technic_run calls +local technic_run_interval = 1.0 -- iterate over all collected switching stations and execute the technic_run function local timer = 0 minetest.register_globalstep(function(dtime) timer = timer + dtime - if timer < 1.0 then + if timer < technic_run_interval then return end timer = 0 + local max_lag = technic.get_max_lag() + if max_lag > 1.5 then + -- slow down technic execution if the lag is higher than usual + technic_run_interval = 1.5 + else + -- normal run_interval + technic_run_interval = 1.0 + end + local now = minetest.get_us_time() + + local off_delay_seconds = tonumber(minetest.settings:get("technic.switch.off_delay_seconds") or "300") local off_delay_micros = off_delay_seconds*1000*1000 + local active_switches = 0 for hash, switch in pairs(switches) do @@ -121,3 +135,12 @@ minetest.register_globalstep(function(dtime) end) + + +minetest.register_chatcommand("technic_flush_switch_cache", { + description = "removes all loaded switching stations from the cache", + privs = { server = true }, + func = function() + switches = {} + end +})