From fd5d69f197e97b4c089ea6b9bcdc975a744c2f83 Mon Sep 17 00:00:00 2001 From: MoNTE48 Date: Wed, 16 Oct 2019 21:25:21 +0200 Subject: [PATCH] Replace globalstep on playerstep, hide Areas text on free areas --- hud.lua | 103 ++++++++++++++++++++++++++------------------------------ 1 file changed, 48 insertions(+), 55 deletions(-) diff --git a/hud.lua b/hud.lua index 729b5a3..bc78a20 100644 --- a/hud.lua +++ b/hud.lua @@ -1,67 +1,60 @@ -- This is inspired by the landrush mod by Bremaweb areas.hud = {} -areas.hud.refresh = 0 -minetest.register_globalstep(function(dtime) +minetest.register_playerstep(function(dtime, playernames) + for _, name in pairs(playernames) do + local player = minetest.get_player_by_name(name) + if player and player:is_player() then + local pos = vector.round(player:get_pos()) + pos = vector.apply(pos, function(p) + return math.max(math.min(p, 2147483), -2147483) + end) + local areaStrings = {} - areas.hud.refresh = areas.hud.refresh + dtime - if areas.hud.refresh > areas.config["tick"] then - areas.hud.refresh = 0 - else - return - end + for id, area in pairs(areas:getAreasAtPos(pos)) do + table.insert(areaStrings, ("%s [%u] (%s%s)") + :format(area.name, id, area.owner, + area.open and ":open" or "")) + 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) - local areaStrings = {} + 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 - for id, area in pairs(areas:getAreasAtPos(pos)) do - table.insert(areaStrings, ("%s [%u] (%s%s)") - :format(area.name, id, area.owner, - area.open and ":open" or "")) - end - - 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 - - local areaString = "Areas:" - if #areaStrings > 0 then - areaString = areaString.."\n".. - table.concat(areaStrings, "\n") - end - 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}, - }) - hud.oldAreas = areaString - return - elseif hud.oldAreas ~= areaString then - player:hud_change(hud.areasId, "text", areaString) - hud.oldAreas = areaString + local areaString = "" + if #areaStrings > 0 then + areaString = "Areas:" + areaString = areaString.."\n".. + table.concat(areaStrings, "\n") + end + 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 = "", + scale = {x=200, y=60}, + alignment = {x=1, y=-1}, + }) + hud.oldAreas = areaString + return + elseif hud.oldAreas ~= areaString then + player:hud_change(hud.areasId, "text", areaString) + hud.oldAreas = areaString + end end end -end) +end, false) minetest.register_on_leaveplayer(function(player) areas.hud[player:get_player_name()] = nil end) -