areas/hud.lua

71 lines
2.0 KiB
Lua
Raw Normal View History

-- This is inspired by the landrush mod by Bremaweb
2020-03-08 22:15:00 +01:00
local S = minetest.get_translator("areas")
areas.hud = {}
areas.hud.refresh = 0
minetest.register_globalstep(function(dtime)
areas.hud.refresh = areas.hud.refresh + dtime
if areas.hud.refresh > areas.config["tick"] then
areas.hud.refresh = 0
else
return
end
for _, player in pairs(minetest.get_connected_players()) do
local name = player:get_player_name()
local pos = vector.round(player:get_pos())
pos = vector.apply(pos, function(p)
return math.max(math.min(p, 2147483), -2147483)
end)
2014-10-14 02:32:02 +02:00
local areaStrings = {}
2016-11-26 08:20:23 +01:00
for id, area in pairs(areas:getAreasAtPos(pos)) do
local faction_info = area.faction_open and areas.factions_available and
factions.get_player_faction(area.owner)
area.faction_open = faction_info
table.insert(areaStrings, ("%s [%u] (%s%s%s)")
2014-03-30 19:53:56 +02:00
:format(area.name, id, area.owner,
area.open and S(":open") or area.openfarming and ":openfarming" or "",
faction_info and ":"..faction_info or ""))
end
2016-11-26 08:20:23 +01:00
for i, area in pairs(areas:getExternalHudEntries(pos)) do
local str = ""
if area.name then str = area.name .. " " end
if area.id then str = str.."["..area.id.."] " end
if area.owner then str = str.."("..area.owner..")" end
table.insert(areaStrings, str)
end
2020-03-08 22:15:00 +01:00
local areaString = S("Areas:")
if #areaStrings > 0 then
areaString = areaString.."\n"..
table.concat(areaStrings, "\n")
end
2014-10-14 02:32:02 +02:00
local hud = areas.hud[name]
if not hud then
hud = {}
areas.hud[name] = hud
hud.areasId = player:hud_add({
hud_elem_type = "text",
name = "Areas",
number = 0xFFFFFF,
position = {x=0, y=1},
offset = {x=8, y=-8},
text = areaString,
scale = {x=200, y=60},
alignment = {x=1, y=-1},
})
2014-10-14 02:32:02 +02:00
hud.oldAreas = areaString
return
2014-10-14 02:32:02 +02:00
elseif hud.oldAreas ~= areaString then
player:hud_change(hud.areasId, "text", areaString)
2014-10-14 02:32:02 +02:00
hud.oldAreas = areaString
end
end
end)
minetest.register_on_leaveplayer(function(player)
areas.hud[player:get_player_name()] = nil
end)