hud.lua 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. local HUD_POSITION = {x = 0.1, y = 0.8}
  2. local HUD_ALIGNMENT = {x = 1, y = 0}
  3. local hud = {}
  4. minetest.register_on_joinplayer(function(player)
  5. local hud_data = {}
  6. hud[player:get_player_name()] = hud_data
  7. hud_data.txt = player:hud_add({
  8. hud_elem_type = "text",
  9. position = HUD_POSITION,
  10. offset = {x = 0, y = 0},
  11. text = "",
  12. alignment = HUD_ALIGNMENT,
  13. scale = {x = 100, y = 100},
  14. number = 0xFF0000
  15. })
  16. end)
  17. minetest.register_on_leaveplayer(function(player)
  18. hud[player:get_player_name()] = nil
  19. end)
  20. local function get_info(player)
  21. local pos = player:get_pos()
  22. local blockpos = mesecons_debug.get_blockpos(pos)
  23. local ctx = mesecons_debug.get_context(pos)
  24. local percent = math.floor(ctx.avg_micros / mesecons_debug.max_usage_micros * 100)
  25. local txt = "Mesecons @ (" .. blockpos.x .. "/" .. blockpos.y .. "/" .. blockpos.z .. ") "
  26. if ctx.whitelisted then
  27. txt = txt .. "whitelisted, no limits"
  28. return txt, 0x00FF00
  29. end
  30. txt = txt ..
  31. " usage: " .. ctx.avg_micros .. " us/s .. (" .. percent .. "%) " ..
  32. "penalty: " .. math.floor(ctx.penalty*10)/10 .. " s"
  33. if ctx.penalty <= 0.1 then
  34. return txt, 0x00FF00
  35. elseif ctx.penalty < 0.5 then
  36. return txt, 0xFFFF00
  37. else
  38. return txt, 0xFF0000
  39. end
  40. end
  41. local timer = 0
  42. minetest.register_globalstep(function(dtime)
  43. timer = timer + dtime
  44. if timer < 1 then
  45. return
  46. end
  47. timer = 0
  48. for _, player in ipairs(minetest.get_connected_players()) do
  49. local playername = player:get_player_name()
  50. local hud_data = hud[playername]
  51. local hud_enable = mesecons_debug.hud[playername]
  52. if hud_enable then
  53. local txt, color = get_info(player)
  54. player:hud_change(hud_data.txt, "text", txt)
  55. player:hud_change(hud_data.txt, "color", color)
  56. elseif hud_enable == false then
  57. mesecons_debug.hud[playername] = nil
  58. player:hud_change(hud_data.txt, "text", "")
  59. end
  60. end
  61. end)