aboutsummaryrefslogtreecommitdiff
path: root/hud.lua
diff options
context:
space:
mode:
authorfluxionary <25628292+fluxionary@users.noreply.github.com>2022-02-13 06:54:41 -0800
committerGitHub <noreply@github.com>2022-02-13 15:54:41 +0100
commit2651262fa3134415f349f63840c89486fabd9063 (patch)
tree3fa7f2ac228e662b4e9bc8071acc2f0650c44e65 /hud.lua
parent1a41379e1d7ae69347f0e6ee6a997234f7590793 (diff)
downloadmesecons_debug-2651262fa3134415f349f63840c89486fabd9063.tar.gz
mesecons_debug-2651262fa3134415f349f63840c89486fabd9063.zip
rework mesecons debug to be more flexible (#7)
* add proper settings (untested) * more constants -> settings * normalize whitespace between code files * refactor globalsteps in order to simplify logic * minor refactoring * rename file * use mod_storage for persistent data; optimize context initialization * refactoring (moving files around) * rewrite penalty * add settings; document; allow changing while game is running * add command to update settings * update init after splitting commands into files * fix bugs; add debugging tools; too much for one commit... * fix whitelist conversion * add adjustable blinky plant to timer overrides * add some more mesecons nodes with repeating timers * resolve luacheck warnings * tweak hud * Update documentation; parameterize more things; refactor some logic for readability
Diffstat (limited to 'hud.lua')
-rw-r--r--hud.lua151
1 files changed, 85 insertions, 66 deletions
diff --git a/hud.lua b/hud.lua
index c769d37..d8a4e25 100644
--- a/hud.lua
+++ b/hud.lua
@@ -1,84 +1,103 @@
-local HUD_POSITION = {x = 0.1, y = 0.8}
-local HUD_ALIGNMENT = {x = 1, y = 0}
+local hud_refresh_interval = mesecons_debug.settings.hud_refresh_interval
+mesecons_debug.settings._subscribe_for_modification("hud_refresh_interval",
+ function(value) hud_refresh_interval = value end)
-local hud = {}
-
-
-minetest.register_on_joinplayer(function(player)
- local hud_data = {}
- hud[player:get_player_name()] = hud_data
-
- hud_data.txt = player:hud_add({
- hud_elem_type = "text",
- position = HUD_POSITION,
- offset = {x = 0, y = 0},
- text = "",
- alignment = HUD_ALIGNMENT,
- scale = {x = 100, y = 100},
- number = 0xFF0000
- })
-
-end)
+local HUD_POSITION = { x = 0.1, y = 0.8 }
+local HUD_ALIGNMENT = { x = 1, y = 0 }
+local hudid_by_playername = {}
minetest.register_on_leaveplayer(function(player)
- hud[player:get_player_name()] = nil
+ hudid_by_playername[player:get_player_name()] = nil
end)
-
-
local function get_info(player)
- local pos = player:get_pos()
- local blockpos = mesecons_debug.get_blockpos(pos)
- local ctx = mesecons_debug.get_context(pos)
+ local pos = player:get_pos()
+ local blockpos = mesecons_debug.get_blockpos(pos)
+ local ctx = mesecons_debug.get_context(pos)
- local percent = math.floor(ctx.avg_micros / mesecons_debug.max_usage_micros * 100)
+ local total = mesecons_debug.avg_total_micros_per_second
+ if total == 0 then total = 1 end
+ local percent = ctx.avg_micros_per_second * 100 / total
- local txt = "Mesecons @ (" .. blockpos.x .. "/" .. blockpos.y .. "/" .. blockpos.z .. ") "
+ local txt = ("mesecons @ %s\n"):format(
+ minetest.pos_to_string(blockpos)
+ )
- if ctx.whitelisted then
- txt = txt .. "whitelisted, no limits"
- return txt, 0x00FF00
- end
-
- txt = txt ..
- " usage: " .. ctx.avg_micros .. " us/s .. (" .. percent .. "%) " ..
- "penalty: " .. math.floor(ctx.penalty*10)/10 .. " s"
+ if ctx.whitelisted then
+ txt = txt .. "whitelisted, no limits"
+ return txt, 0x00FFFF
+ end
- if ctx.penalty <= 0.1 then
- return txt, 0x00FF00
- elseif ctx.penalty < 0.5 then
- return txt, 0xFFFF00
- else
- return txt, 0xFF0000
- end
+ txt = txt .. ("usage: %.0f us/s .. (%.1f%%) penalty: %.2fs"):format(
+ ctx.avg_micros_per_second,
+ percent,
+ ctx.penalty
+ )
+ txt = txt .. ("\nlag: %.2f (%s); mesecons load = %s"):format(
+ mesecons_debug.avg_lag,
+ mesecons_debug.lag_level,
+ mesecons_debug.load_level
+ )
+ if minetest.get_server_max_lag then
+ txt = txt .. ("; max_lag: %.2f"):format(
+ minetest.get_server_max_lag()
+ )
+ end
+ txt = txt .. ("; #players = %i"):format(
+ #minetest.get_connected_players()
+ )
+ txt = txt .. ("\npenalties enabled = %s; mesecons enabled = %s"):format(
+ mesecons_debug.enabled,
+ mesecons_debug.mesecons_enabled
+ )
+
+ if ctx.penalty <= 1 then
+ return txt, 0x00FF00
+ elseif ctx.penalty <= 10 then
+ return txt, 0xFFFF00
+ else
+ return txt, 0xFF0000
+ end
end
local timer = 0
minetest.register_globalstep(function(dtime)
- timer = timer + dtime
- if timer < 1 then
- return
- end
- timer = 0
-
- for _, player in ipairs(minetest.get_connected_players()) do
- local playername = player:get_player_name()
- local hud_data = hud[playername]
- local hud_enable = mesecons_debug.hud[playername]
- if hud_enable then
- local txt, color = get_info(player)
- player:hud_change(hud_data.txt, "text", txt)
- player:hud_change(hud_data.txt, "color", color)
-
- elseif hud_enable == false then
- mesecons_debug.hud[playername] = nil
- player:hud_change(hud_data.txt, "text", "")
-
+ timer = timer + dtime
+ if timer < hud_refresh_interval then
+ return
+ end
+ timer = 0
+
+ for _, player in ipairs(minetest.get_connected_players()) do
+ local playername = player:get_player_name()
+ local hudid = hudid_by_playername[playername]
+ local hud_enabled = mesecons_debug.hud_enabled_by_playername[playername]
+
+ if hud_enabled then
+ local text, color = get_info(player)
+ if hudid then
+ player:hud_change(hudid, "text", text)
+ player:hud_change(hudid, "number", color)
+
+ else
+ hudid_by_playername[playername] = player:hud_add({
+ hud_elem_type = "text",
+ position = HUD_POSITION,
+ offset = { x = 0, y = 0 },
+ text = text,
+ number = color,
+ alignment = HUD_ALIGNMENT,
+ scale = { x = 100, y = 100 },
+ })
+ end
+
+ else
+ if hudid then
+ player:hud_remove(hudid)
+ hudid_by_playername[playername] = nil
+ end
+ end
end
-
- end
-
-
end)