hud.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. protector = protector or {}
  2. protector.hud = protector.hud or {}
  3. -- Localize.
  4. local hud = protector.hud
  5. hud.players = hud.players or {}
  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. function hud.globalstep(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 > 1 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) ..
  62. "\nPos: " .. rc.pos_to_string(pos):gsub(",", ", ") ..
  63. "\nClaim: " .. owner_str
  64. if hud_text ~= hud.players[pname].text then
  65. if not hud.players[pname].id then
  66. hud.players[pname].id = player:hud_add({
  67. hud_elem_type = "text",
  68. name = "Protector Area",
  69. number = 0xFFFFFF, --0xFFFF22,
  70. position = {x=0, y=1},
  71. offset = {x=16, y=-130},
  72. text = hud_text,
  73. alignment = {x=1, y=1},
  74. })
  75. else
  76. player:hud_change(hud.players[pname].id, "text", hud_text)
  77. end
  78. hud.players[pname].text = hud_text
  79. hud.players[pname].owner = owner_str
  80. end
  81. local yaw = (player:get_look_horizontal() * 180.0) / math.pi
  82. local div = 360 / 8
  83. local dir = "N/A"
  84. yaw = yaw + (360 / 16)
  85. if yaw > 360 then
  86. yaw = yaw - 360
  87. end
  88. if yaw < div*1 then
  89. dir = "N [+Z]"
  90. elseif yaw < div*2 then
  91. dir = "NW [-X +Z]"
  92. elseif yaw < div*3 then
  93. dir = "W [-X]"
  94. elseif yaw < div*4 then
  95. dir = "SW [-X -Z]"
  96. elseif yaw < div*5 then
  97. dir = "S [-Z]"
  98. elseif yaw < div*6 then
  99. dir = "SE [+X -Z]"
  100. elseif yaw < div*7 then
  101. dir = "E [+X]"
  102. elseif yaw < div*8 then
  103. dir = "NE [+X +Z]"
  104. elseif yaw < div*9 then
  105. dir = "N [+Z]"
  106. end
  107. local dir_text = "Facing: " .. dir
  108. if dir_text ~= hud.players[pname].dir then
  109. if not hud.players[pname].id2 then
  110. hud.players[pname].id2 = player:hud_add({
  111. hud_elem_type = "text",
  112. number = 0xFFFFFF,
  113. position = {x=1, y=1},
  114. offset = {x=-16, y=-130 + 18*2},
  115. text = dir_text,
  116. alignment = {x=-1, y=1},
  117. })
  118. else
  119. player:hud_change(hud.players[pname].id2, "text", dir_text)
  120. end
  121. hud.players[pname].dir = dir_text
  122. end
  123. -- Store timer.
  124. hud.players[pname].timer = timer
  125. end
  126. end
  127. function hud.joinplayer(player)
  128. local pname = player:get_player_name()
  129. hud.players[pname] = {
  130. timer = 0,
  131. text = "", -- Keep track of last displayed text.
  132. owner = "",
  133. dir = "",
  134. moved = true,
  135. pos = {x=0, y=0, z=0},
  136. }
  137. end
  138. function hud.leaveplayer(player, timedout)
  139. local pname = player:get_player_name()
  140. hud.players[pname] = nil
  141. end
  142. if not hud.registered then
  143. minetest.register_globalstep(function(...) return hud.globalstep(...) end)
  144. minetest.register_on_joinplayer(function(...) return hud.joinplayer(...) end)
  145. minetest.register_on_leaveplayer(function(...) return hud.leaveplayer(...) end)
  146. local c = "protector:hud"
  147. local f = protector.modpath .. "/hud.lua"
  148. reload.register_file(c, f, false)
  149. hud.registered = true
  150. end