hud.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. protector = protector or {}
  2. protector.hud = protector.hud or {}
  3. -- Localize.
  4. local hud = protector.hud
  5. hud.players = {}
  6. function protector.update_nearby_players(pos)
  7. local players = minetest.get_connected_players()
  8. for _, player in ipairs(players) do
  9. local p1 = player:get_pos()
  10. if vector.distance(pos, p1) <= 6 then
  11. local pname = player:get_player_name()
  12. hud.players[pname].moved = true -- Will trigger a HUD update.
  13. end
  14. end
  15. end
  16. local gs_timer = 0.0
  17. local gs_timestep = 0.5
  18. minetest.register_globalstep(function(dtime)
  19. gs_timer = gs_timer + dtime
  20. if gs_timer < gs_timestep then return end
  21. gs_timer = 0.0
  22. local allplayers = minetest.get_connected_players()
  23. for _, player in ipairs(allplayers) do
  24. local control = player:get_player_control()
  25. local pname = player:get_player_name()
  26. -- Is the player currently moving?
  27. local moving = (control.sneak or control.jump or control.left or control.right or control.up or control.down)
  28. local timer = hud.players[pname].timer
  29. -- Detect if the player moved through some other means.
  30. local pos = vector.round(player:get_pos())
  31. if not vector.equals(hud.players[pname].pos, pos) then
  32. moving = true
  33. end
  34. hud.players[pname].pos = pos
  35. local owner_str = hud.players[pname].owner
  36. -- Advance clock if player is not moving.
  37. if moving then
  38. timer = 0
  39. owner_str = "Pending"
  40. hud.players[pname].moved = true
  41. else
  42. if hud.players[pname].moved then
  43. timer = timer + gs_timestep
  44. owner_str = "Checking"
  45. end
  46. end
  47. if timer > 5 then
  48. -- Player position has already been obtained.
  49. local owner = protector.get_node_owner(pos)
  50. if owner and owner ~= "" then
  51. owner_str = "<" .. rename.gpn(owner) .. ">"
  52. else
  53. owner_str = "Nobody"
  54. end
  55. timer = 0
  56. hud.players[pname].moved = false
  57. end
  58. local hud_text = "Realm: " .. rc.pos_to_name(pos) .. "\nPos: " .. rc.pos_to_string(pos):gsub(",", ", ") .. "\nOwner: " .. owner_str
  59. if hud_text ~= hud.players[pname].text then
  60. if not hud.players[pname].id then
  61. hud.players[pname].id = player:hud_add({
  62. hud_elem_type = "text",
  63. name = "Protector Area",
  64. number = 0xFFFFFF, --0xFFFF22,
  65. position = {x=0, y=1},
  66. offset = {x=16, y=-130},
  67. text = hud_text,
  68. alignment = {x=1, y=1},
  69. })
  70. else
  71. player:hud_change(hud.players[pname].id, "text", hud_text)
  72. end
  73. hud.players[pname].text = hud_text
  74. hud.players[pname].owner = owner_str
  75. end
  76. local yaw = (player:get_look_horizontal() * 180.0) / math.pi
  77. local div = 360 / 8
  78. local dir = "N/A"
  79. yaw = yaw + (360 / 16)
  80. if yaw > 360 then
  81. yaw = yaw - 360
  82. end
  83. if yaw < div*1 then
  84. dir = "N [+Z]"
  85. elseif yaw < div*2 then
  86. dir = "NW [-X +Z]"
  87. elseif yaw < div*3 then
  88. dir = "W [-X]"
  89. elseif yaw < div*4 then
  90. dir = "SW [-X -Z]"
  91. elseif yaw < div*5 then
  92. dir = "S [-Z]"
  93. elseif yaw < div*6 then
  94. dir = "SE [+X -Z]"
  95. elseif yaw < div*7 then
  96. dir = "E [+X]"
  97. elseif yaw < div*8 then
  98. dir = "NE [+X +Z]"
  99. elseif yaw < div*9 then
  100. dir = "N [+Z]"
  101. end
  102. local dir_text = "Facing: " .. dir
  103. if dir_text ~= hud.players[pname].dir then
  104. if not hud.players[pname].id2 then
  105. hud.players[pname].id2 = player:hud_add({
  106. hud_elem_type = "text",
  107. number = 0xFFFFFF,
  108. position = {x=1, y=1},
  109. offset = {x=-16, y=-130 + 18*2},
  110. text = dir_text,
  111. alignment = {x=-1, y=1},
  112. })
  113. else
  114. player:hud_change(hud.players[pname].id2, "text", dir_text)
  115. end
  116. hud.players[pname].dir = dir_text
  117. end
  118. -- Store timer.
  119. hud.players[pname].timer = timer
  120. end
  121. end)
  122. minetest.register_on_joinplayer(function(player)
  123. local pname = player:get_player_name()
  124. hud.players[pname] = {
  125. timer = 0,
  126. text = "", -- Keep track of last displayed text.
  127. owner = "",
  128. dir = "",
  129. moved = true,
  130. pos = {x=0, y=0, z=0},
  131. }
  132. end)
  133. minetest.register_on_leaveplayer(function(player, timedout)
  134. local pname = player:get_player_name()
  135. hud.players[pname] = nil
  136. end)