hud.lua 2.4 KB

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