hud.lua 4.0 KB

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