Skip to content

Commit

Permalink
reduce technic_run interval if lag is too high / hot-reload offdelay …
Browse files Browse the repository at this point in the history
…setting / cache-flush command
  • Loading branch information
BuckarooBanzay committed Feb 3, 2020
1 parent 60963ca commit 8ab47b2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions technic/machines/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
15 changes: 15 additions & 0 deletions technic/machines/max_lag.lua
Original file line number Diff line number Diff line change
@@ -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
27 changes: 25 additions & 2 deletions technic/machines/switching_station_globalstep.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
})

0 comments on commit 8ab47b2

Please sign in to comment.