Decrease minetest.after globalstep lag

* abort if theres no active timer
* only reduce the timer.time of all timers when its necessary
* move updating timers_to_add into a seperate function
This commit is contained in:
HybridDog 2015-06-03 15:58:38 +02:00 committed by est31
parent 74d8b341bc
commit 8383a61eac

@ -4,40 +4,72 @@
-- Misc. API functions -- Misc. API functions
-- --
core.timers_to_add = {} local timers = {}
core.timers = {} local mintime
core.register_globalstep(function(dtime) local function update_timers(delay)
for _, timer in ipairs(core.timers_to_add) do mintime = false
table.insert(core.timers, timer) local sub = 0
end for index = 1, #timers do
core.timers_to_add = {} index = index - sub
local index = 1 local timer = timers[index]
while index <= #core.timers do timer.time = timer.time - delay
local timer = core.timers[index]
timer.time = timer.time - dtime
if timer.time <= 0 then if timer.time <= 0 then
timer.func(unpack(timer.args or {})) timer.func(unpack(timer.args or {}))
table.remove(core.timers,index) table.remove(timers, index)
sub = sub + 1
elseif mintime then
mintime = math.min(mintime, timer.time)
else else
index = index + 1 mintime = timer.time
end end
end end
end
local timers_to_add
local function add_timers()
for _, timer in ipairs(timers_to_add) do
table.insert(timers, timer)
end
timers_to_add = false
end
local delay = 0
core.register_globalstep(function(dtime)
if not mintime then
-- abort if no timers are running
return
end
if timers_to_add then
add_timers()
end
delay = delay + dtime
if delay < mintime then
return
end
update_timers(delay)
delay = 0
end) end)
function core.after(time, func, ...) function core.after(time, func, ...)
assert(tonumber(time) and type(func) == "function", assert(tonumber(time) and type(func) == "function",
"Invalid core.after invocation") "Invalid core.after invocation")
table.insert(core.timers_to_add, {time=time, func=func, args={...}}) if not mintime then
mintime = time
timers_to_add = {{time=time+delay, func=func, args={...}}}
return
end
mintime = math.min(mintime, time)
timers_to_add = timers_to_add or {}
timers_to_add[#timers_to_add+1] = {time=time+delay, func=func, args={...}}
end end
function core.check_player_privs(name, privs) function core.check_player_privs(name, privs)
local player_privs = core.get_player_privs(name) local player_privs = core.get_player_privs(name)
local missing_privileges = {} local missing_privileges = {}
for priv, val in pairs(privs) do for priv, val in pairs(privs) do
if val then if val
if not player_privs[priv] then and not player_privs[priv] then
table.insert(missing_privileges, priv) table.insert(missing_privileges, priv)
end
end end
end end
if #missing_privileges > 0 then if #missing_privileges > 0 then