clamp radiation damage abm to 100 calls per second max

This commit is contained in:
Thomas Rudin 2020-01-21 18:20:47 +01:00
parent a76e90d9de
commit 1a625bc444
2 changed files with 30 additions and 1 deletions

View File

@ -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

26
technic/util/throttle.lua Normal file
View File

@ -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