aboutsummaryrefslogtreecommitdiff
path: root/globalstep.lua
blob: e28d79eb4b0a338b62f86668a2b6f7144b9e422c (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

-- enable/disable mesecons entirely

local enabled = true
local queue_dump_counter = 0

local step_index = 0

-- globalstep on/off
for i, globalstep in ipairs(minetest.registered_globalsteps) do
  local info = minetest.callback_origins[globalstep]
  if not info then
    break
  end

  local modname = info.mod

  if modname == "mesecons" then
    step_index = step_index + 1
    if step_index > 1 then
	 -- only override first globalstep in mesecons
	break
    end

    local cooldown = 0
    local last_run_time = 0

    local fn = function(dtime)
      if cooldown > 0 then
        cooldown = cooldown - 1
        return
      end

      if enabled then

        local max_globalstep_time = tonumber(minetest.settings:get("mesecons_debug_max_globalstep_time")) or 75000
	local min_delay_time = tonumber(minetest.settings:get("mesecons_debug_min_delay_time")) or 200000
        local cooldown_steps = tonumber(minetest.settings:get("mesecons_debug_cooldown_steps")) or 5
        local autoflush = minetest.settings:get_bool("mesecons_debug_autoflush", false)

	local now = minetest.get_us_time()
	if (now - last_run_time) < min_delay_time then
		-- adhere to min delay
		return
	end

	last_run_time = now

	if queue_dump_counter > 0 then
		-- dump action queue
		mesecons_debug.dump_queue()
		queue_dump_counter = queue_dump_counter - 1
	end

	-- execute with time measurement
        local t0 = minetest.get_us_time()
        globalstep(dtime)
        local t1 = minetest.get_us_time()
        local diff = t1 - t0

        if diff > max_globalstep_time then
          cooldown = cooldown_steps
          minetest.log("warning", "[mesecons_debug] cooldown triggered")
          if autoflush then
            mesecon.queue.actions = {}
          end
        end
      end
    end

    minetest.callback_origins[fn] = info
    minetest.registered_globalsteps[i] = fn
  end
end

-- execute()
local old_execute = mesecon.queue.execute
mesecon.queue.execute = function(...)
  if enabled then
	 old_execute(...)
  end
end

-- add_action()
local old_add_action = mesecon.queue.add_action
mesecon.queue.add_action = function(...)
  if enabled then
    old_add_action(...)
  end
end


-- mesecons commands
minetest.register_chatcommand("dump_queue", {
    description = "dumps the current actionqueue to a file for later processing",
    privs = { mesecons_debug = true },
    func = function()
	queue_dump_counter = 10
	return true, "processing.."
    end
})


minetest.register_chatcommand("mesecons_enable", {
  description = "enables the mesecons globlastep",
  privs = {mesecons_debug=true},
  func = function()
    enabled = true
    return true, "mesecons enabled"
  end
})

minetest.register_chatcommand("mesecons_disable", {
  description = "disables the mesecons globlastep",
  privs = {mesecons_debug=true},
  func = function()
    enabled = false
    -- flush actions, while we are on it
    mesecon.queue.actions = {}
    return true, "mesecons disabled"
  end
})