aboutsummaryrefslogtreecommitdiff
path: root/overrides/mesecons_queue.lua
blob: d14b30c9543d438dfc79122c28ab1b37769cfe41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
local penalty_mapblock_disabled = mesecons_debug.settings.penalty_mapblock_disabled
mesecons_debug.settings._subscribe_for_modification("penalty_mapblock_disabled", function(value)
    penalty_mapblock_disabled = value
end)

local check_pos = mesecons_debug.check_pos

local old_execute = mesecon.queue.execute
mesecon.queue.execute = function(self, action)
    if not mesecons_debug.enabled then
        return old_execute(self, action)
    elseif not mesecons_debug.mesecons_enabled then
        return
    end

    if not check_pos(action.pos) then
        mesecons_debug.log("error", "mesecons action has an invalid position and cannot be processed %s", dump(action))
        return
    end

    local ctx = mesecons_debug.get_context(action.pos)
    if ctx.whitelisted then
        return old_execute(self, action)
    end

    local t0 = minetest.get_us_time()
    local rv = old_execute(self, action)
    local micros = minetest.get_us_time() - t0

    mesecons_debug.total_micros = mesecons_debug.total_micros + micros
    ctx.micros = ctx.micros + micros
    ctx.mtime = t0 -- modification time

    return rv
end

-- add_action()
local old_add_action = mesecon.queue.add_action
mesecon.queue.add_action = function(self, pos, func, params, time, overwritecheck, priority)
    if not mesecons_debug.enabled then
        return old_add_action(self, pos, func, params, time, overwritecheck, priority)
    elseif not mesecons_debug.mesecons_enabled then
        return
    end

    local ctx = mesecons_debug.get_context(pos)

    if not ctx.whitelisted and ctx.penalty > penalty_mapblock_disabled then
        -- penalty exceeded disable-threshold, don't even add the action
        return
    end

    return old_add_action(self, pos, func, params, (time or 0) + ctx.penalty, overwritecheck, priority)
end