hud.lua 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. -- This is inspired by the landrush mod by Bremaweb
  2. areas.hud = {}
  3. areas.hud.refresh = 0
  4. minetest.register_globalstep(function(dtime)
  5. areas.hud.refresh = areas.hud.refresh + dtime
  6. if areas.hud.refresh > areas.config["tick"] then
  7. areas.hud.refresh = 0
  8. else
  9. return
  10. end
  11. for _, player in pairs(minetest.get_connected_players()) do
  12. local name = player:get_player_name()
  13. local pos = vector.round(player:get_pos())
  14. pos = vector.apply(pos, function(p)
  15. return math.max(math.min(p, 2147483), -2147483)
  16. end)
  17. local areaStrings = {}
  18. for id, area in pairs(areas:getAreasAtPos(pos)) do
  19. local faction_info = area.faction_open and areas.factions_available and
  20. factions.get_player_faction(area.owner)
  21. area.faction_open = faction_info
  22. table.insert(areaStrings, ("%s [%u] (%s%s%s)")
  23. :format(area.name, id, area.owner,
  24. area.open and ":open" or "",
  25. faction_info and ":"..faction_info or ""))
  26. end
  27. for i, area in pairs(areas:getExternalHudEntries(pos)) do
  28. local str = ""
  29. if area.name then str = area.name .. " " end
  30. if area.id then str = str.."["..area.id.."] " end
  31. if area.owner then str = str.."("..area.owner..")" end
  32. table.insert(areaStrings, str)
  33. end
  34. local areaString = "Areas:"
  35. if #areaStrings > 0 then
  36. areaString = areaString.."\n"..
  37. table.concat(areaStrings, "\n")
  38. end
  39. local hud = areas.hud[name]
  40. if not hud then
  41. hud = {}
  42. areas.hud[name] = hud
  43. hud.areasId = player:hud_add({
  44. hud_elem_type = "text",
  45. name = "Areas",
  46. number = 0xFFFFFF,
  47. position = {x=0, y=1},
  48. offset = {x=8, y=-8},
  49. text = areaString,
  50. scale = {x=200, y=60},
  51. alignment = {x=1, y=-1},
  52. })
  53. hud.oldAreas = areaString
  54. return
  55. elseif hud.oldAreas ~= areaString then
  56. player:hud_change(hud.areasId, "text", areaString)
  57. hud.oldAreas = areaString
  58. end
  59. end
  60. end)
  61. minetest.register_on_leaveplayer(function(player)
  62. areas.hud[player:get_player_name()] = nil
  63. end)