aboutsummaryrefslogtreecommitdiff
path: root/hud.lua
blob: 8aa6561c39d5bfa48abc6203865357e486cf346c (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

local HUD_POSITION = {x = 0.1, y = 0.8}
local HUD_ALIGNMENT = {x = 1, y = 0}

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)


minetest.register_on_leaveplayer(function(player)
  hud[player:get_player_name()] = nil
end)


local function get_blockpos(pos)
	return {x = math.floor(pos.x / 16),
	        y = math.floor(pos.y / 16),
	        z = math.floor(pos.z / 16)}
end

local function get_info(player)
  local pos = player:get_pos()
  local blockpos = get_blockpos(pos)
  local ctx = mesecons_debug.get_context(pos)

  local percent = math.floor(ctx.avg_micros / mesecons_debug.max_usage_micros * 100)

  local txt = "Mesecons @ (" .. blockpos.x .. "/" .. blockpos.y .. "/" .. blockpos.z .. ") " ..
    " usage: " .. ctx.avg_micros .. " us/s .. (" .. percent .. "%) " ..
    "penalty: " .. math.floor(ctx.penalty*10)/10 .. " s"

  if ctx.penalty <= 0.1 then
    return txt, 0x00FF00
  elseif ctx.penalty < 0.5 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", "")

    end

  end


end)