From e7ebdbd090ed7832bc1f37650e724771fb783eec Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Sun, 5 Jan 2020 16:45:10 +0100 Subject: [PATCH] polyfuse overload --- technic/machines/switching_station.lua | 26 --------- .../machines/switching_station_globalstep.lua | 58 ++++++++++++++++--- 2 files changed, 51 insertions(+), 33 deletions(-) diff --git a/technic/machines/switching_station.lua b/technic/machines/switching_station.lua index 8130492..1ada2fe 100644 --- a/technic/machines/switching_station.lua +++ b/technic/machines/switching_station.lua @@ -234,16 +234,6 @@ minetest.register_chatcommand("powerctrl", { end }) -local check_timer = function(pos, meta, diff) - if diff > 250000 then - minetest.log("warning", "[technic] disabling switching station @ " .. minetest.pos_to_string(pos)) - meta:set_int("lag", math.floor(diff / 1000)) - meta:set_int("overload", 300) - meta:set_int("active", 0) - meta:set_string("infotext", "Overload detected!") - end -end - -- Run all the nodes local function run_nodes(list, run_stage) for _, pos in ipairs(list) do @@ -276,19 +266,6 @@ technic.switching_station_run = function(pos) local RE_nodes local machine_name = S("Switching Station") - local overload = meta:get_int("overload") - if overload > 0 then - local lag_millis = meta:get_int("lag") or 0 - meta:set_int("overload", overload - 1) - meta:set_string("infotext", "Overload detected, resetting in " .. overload .. " seconds (generated lag: " .. lag_millis .. " ms)") - if overload == 1 then - -- re-enable in next step - meta:set_int("active", 1) - end - return - - end - -- Which kind of network are we on: pos1 = {x=pos.x, y=pos.y-1, z=pos.z} @@ -425,7 +402,6 @@ technic.switching_station_run = function(pos) local t1 = minetest.get_us_time() local diff = t1 - t0 if diff > 50000 then - check_timer(pos, meta, diff) minetest.log("warning", "[technic] [+supply] switching station abm took " .. diff .. " us at " .. minetest.pos_to_string(pos)) end @@ -455,7 +431,6 @@ technic.switching_station_run = function(pos) local t1 = minetest.get_us_time() local diff = t1 - t0 if diff > 50000 then - check_timer(pos, meta, diff) minetest.log("warning", "[technic] [-supply] switching station abm took " .. diff .. " us at " .. minetest.pos_to_string(pos)) end @@ -480,7 +455,6 @@ technic.switching_station_run = function(pos) local t1 = minetest.get_us_time() local diff = t1 - t0 if diff > 50000 then - check_timer(pos, meta, diff) minetest.log("warning", "[technic] switching station abm took " .. diff .. " us at " .. minetest.pos_to_string(pos)) end diff --git a/technic/machines/switching_station_globalstep.lua b/technic/machines/switching_station_globalstep.lua index 12f389c..fb48d71 100644 --- a/technic/machines/switching_station_globalstep.lua +++ b/technic/machines/switching_station_globalstep.lua @@ -1,7 +1,22 @@ local has_monitoring_mod = minetest.get_modpath("monitoring") -local switches = {} -- pos_hash -> time_us +local switches = {} -- pos_hash -> { time = time_us } + +local function get_switch_data(pos) + local hash = minetest.hash_node_position(pos) + local switch = switches[hash] + + if not switch then + switch = { + time = 0, + skip = 0 + } + switches[hash] = switch + end + + return switch +end local active_switching_stations_metric, switching_stations_usage_metric @@ -24,9 +39,8 @@ minetest.register_abm({ interval = 1, chance = 1, action = function(pos) - local hash = minetest.hash_node_position(pos) - local time_us = minetest.get_us_time() - switches[hash] = time_us + local switch = get_switch_data(pos) + switch.time = minetest.get_us_time() end }) @@ -44,9 +58,9 @@ minetest.register_globalstep(function(dtime) local off_delay_micros = off_delay_seconds*1000*1000 local active_switches = 0 - for hash, time_us in pairs(switches) do + for hash, switch in pairs(switches) do local pos = minetest.get_position_from_hash(hash) - local diff = now - time_us + local diff = now - switch.time minetest.get_voxel_manip(pos, pos) local node = minetest.get_node(pos) @@ -58,7 +72,37 @@ minetest.register_globalstep(function(dtime) elseif diff < off_delay_micros then -- station active active_switches = active_switches + 1 - technic.switching_station_run(pos) + + if switch.skip < 1 then + + local start = minetest.get_us_time() + technic.switching_station_run(pos) + local switch_diff = minetest.get_us_time() - start + + + local meta = minetest.get_meta(pos) + + -- overload detection + if switch_diff > 250000 then + switch.skip = 30 + elseif switch_diff > 100000 then + switch.skip = 20 + elseif switch_diff > 50000 then + switch.skip = 10 + elseif switch_diff > 25000 then + switch.skip = 2 + end + + if switch.skip > 0 then + local efficiency = math.floor(1/switch.skip*100) + meta:set_string("infotext", "Polyfuse triggered, current efficiency: " .. + efficiency .. "% generated lag : " .. math.floor(switch_diff/1000) .. " ms") + end + + else + switch.skip = math.max(switch.skip - 1, 0) + end + else -- station timed out