|
4 | 4 | -- Misc. API functions |
5 | 5 | -- |
6 | 6 |
|
7 | | -core.timers_to_add = {} |
8 | | -core.timers = {} |
9 | | -core.register_globalstep(function(dtime) |
10 | | - for _, timer in ipairs(core.timers_to_add) do |
11 | | - table.insert(core.timers, timer) |
12 | | - end |
13 | | - core.timers_to_add = {} |
14 | | - local index = 1 |
15 | | - while index <= #core.timers do |
16 | | - local timer = core.timers[index] |
17 | | - timer.time = timer.time - dtime |
| 7 | +local timers = {} |
| 8 | +local mintime |
| 9 | +local function update_timers(delay) |
| 10 | + mintime = false |
| 11 | + local sub = 0 |
| 12 | + for index = 1, #timers do |
| 13 | + index = index - sub |
| 14 | + local timer = timers[index] |
| 15 | + timer.time = timer.time - delay |
18 | 16 | if timer.time <= 0 then |
19 | 17 | timer.func(unpack(timer.args or {})) |
20 | | - table.remove(core.timers,index) |
| 18 | + table.remove(timers, index) |
| 19 | + sub = sub + 1 |
| 20 | + elseif mintime then |
| 21 | + mintime = math.min(mintime, timer.time) |
21 | 22 | else |
22 | | - index = index + 1 |
| 23 | + mintime = timer.time |
23 | 24 | end |
24 | 25 | end |
| 26 | +end |
| 27 | + |
| 28 | +local timers_to_add |
| 29 | +local function add_timers() |
| 30 | + for _, timer in ipairs(timers_to_add) do |
| 31 | + table.insert(timers, timer) |
| 32 | + end |
| 33 | + timers_to_add = false |
| 34 | +end |
| 35 | + |
| 36 | +local delay = 0 |
| 37 | +core.register_globalstep(function(dtime) |
| 38 | + if not mintime then |
| 39 | + -- abort if no timers are running |
| 40 | + return |
| 41 | + end |
| 42 | + if timers_to_add then |
| 43 | + add_timers() |
| 44 | + end |
| 45 | + delay = delay + dtime |
| 46 | + if delay < mintime then |
| 47 | + return |
| 48 | + end |
| 49 | + update_timers(delay) |
| 50 | + delay = 0 |
25 | 51 | end) |
26 | 52 |
|
27 | 53 | function core.after(time, func, ...) |
28 | 54 | assert(tonumber(time) and type(func) == "function", |
29 | 55 | "Invalid core.after invocation") |
30 | | - table.insert(core.timers_to_add, {time=time, func=func, args={...}}) |
| 56 | + if not mintime then |
| 57 | + mintime = time |
| 58 | + timers_to_add = {{time=time+delay, func=func, args={...}}} |
| 59 | + return |
| 60 | + end |
| 61 | + mintime = math.min(mintime, time) |
| 62 | + timers_to_add = timers_to_add or {} |
| 63 | + timers_to_add[#timers_to_add+1] = {time=time+delay, func=func, args={...}} |
31 | 64 | end |
32 | 65 |
|
33 | 66 | function core.check_player_privs(name, privs) |
34 | 67 | local player_privs = core.get_player_privs(name) |
35 | 68 | local missing_privileges = {} |
36 | 69 | for priv, val in pairs(privs) do |
37 | | - if val then |
38 | | - if not player_privs[priv] then |
39 | | - table.insert(missing_privileges, priv) |
40 | | - end |
| 70 | + if val |
| 71 | + and not player_privs[priv] then |
| 72 | + table.insert(missing_privileges, priv) |
41 | 73 | end |
42 | 74 | end |
43 | 75 | if #missing_privileges > 0 then |
|
0 commit comments