From 1a625bc4441987e07538f35deba964110427febf Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Tue, 21 Jan 2020 18:20:47 +0100 Subject: [PATCH] clamp radiation damage abm to 100 calls per second max --- technic/radiation.lua | 5 ++++- technic/util/throttle.lua | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 technic/util/throttle.lua diff --git a/technic/radiation.lua b/technic/radiation.lua index 61b7215..f2897cc 100644 --- a/technic/radiation.lua +++ b/technic/radiation.lua @@ -28,6 +28,9 @@ or complex internal structure should show no radiation resistance. Fractional resistance values are permitted. --]] +local MP = minetest.get_modpath("technic") +local throttle = dofile(MP .. "/util/throttle.lua") + local S = technic.getter local rad_resistance_node = { @@ -362,7 +365,7 @@ if minetest.settings:get_bool("enable_damage") then nodenames = {"group:radioactive"}, interval = 1, chance = 2, - action = dmg_abm, + action = throttle(100, dmg_abm), }) if longterm_damage then diff --git a/technic/util/throttle.lua b/technic/util/throttle.lua new file mode 100644 index 0000000..99d4ec2 --- /dev/null +++ b/technic/util/throttle.lua @@ -0,0 +1,26 @@ + +local function throttle(callspersecond, fn) + local time = 0 + local count = 0 + + return function(...) + local now = minetest.get_us_time() + if (now - time) > 1000000 then + -- reset time + time = now + count = 0 + else + -- check max calls + count = count + 1 + if count > callspersecond then + return + end + end + + return pcall(fn, ...) + end + +end + + +return throttle