From ed7f57bf2f21af64039f07ac547ff676167b0ede Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Wed, 15 Jan 2014 20:10:24 -0500 Subject: [PATCH] Add IDs to HUD and areas:getAreasAtPos(pos) --- api.lua | 38 ++++++++++++++++++++++---------------- hud.lua | 30 +++++++++++++++++++----------- legacy.lua | 29 +++++++---------------------- 3 files changed, 48 insertions(+), 49 deletions(-) diff --git a/api.lua b/api.lua index 88c8b1e..95a3987 100644 --- a/api.lua +++ b/api.lua @@ -1,20 +1,30 @@ +-- Returns a list of areas that include the provided position +function areas:getAreasAtPos(pos) + local a = {} + local px, py, pz = pos.x, pos.y, pos.z + for id, area in pairs(self.areas) do + local ap1, ap2 = area.pos1, area.pos2 + if px >= ap1.x and px <= ap2.x and + py >= ap1.y and py <= ap2.y and + pz >= ap1.z and pz <= ap2.z then + a[id] = area + end + end + return a +end + -- Checks if the area is unprotected or owned by you function areas:canInteract(pos, name) if minetest.check_player_privs(name, {areas=true}) then return true end local owned = false - for _, area in pairs(self.areas) do - p1, p2 = area.pos1, area.pos2 - if pos.x >= p1.x and pos.x <= p2.x and - pos.y >= p1.y and pos.y <= p2.y and - pos.z >= p1.z and pos.z <= p2.z then - if area.owner == name then - return true - else - owned = true - end + for _, area in pairs(self:getAreasAtPos(pos)) do + if area.owner == name then + return true + else + owned = true end end return not owned @@ -23,12 +33,8 @@ end -- Returns a table (list) of all players that own an area function areas:getNodeOwners(pos) local owners = {} - for _, area in pairs(self.areas) do - if pos.x >= area.pos1.x and pos.x <= area.pos2.x and - pos.y >= area.pos1.y and pos.y <= area.pos2.y and - pos.z >= area.pos1.z and pos.z <= area.pos2.z then - table.insert(owners, area.owner) - end + for _, area in pairs(self:getAreasAtPos(pos)) do + table.insert(owners, area.owner) end return owners end diff --git a/hud.lua b/hud.lua index 2ed2e0e..1280afa 100644 --- a/hud.lua +++ b/hud.lua @@ -6,28 +6,36 @@ minetest.register_globalstep(function(dtime) for _, player in pairs(minetest.get_connected_players()) do local name = player:get_player_name() local pos = vector.round(player:getpos()) - local owners = areas:getNodeOwners(pos) - local ownerString = table.concat(owners, ", ") + local a = areas:getAreasAtPos(pos) + local areaString = "" + local first = true + for id, area in pairs(areas:getAreasAtPos(pos)) do + if not first then + areaString = areaString..", " + else + first = false + end + areaString = areaString..id.." ("..area.owner..")" + end if not areas.hud[name] then areas.hud[name] = {} - areas.hud[name].ownersId = player:hud_add({ + areas.hud[name].areasId = player:hud_add({ hud_elem_type = "text", - name = "AreaOwners", + name = "Areas", number = 0xFFFFFF, position = {x=0, y=1}, offset = {x=5, y=-60}, direction = 0, - text = "Area owners: "..ownerString, + text = "Areas: "..areaString, scale = {x=200, y=60}, alignment = {x=1, y=1}, }) - areas.hud[name].oldOwners = ownerString + areas.hud[name].oldAreas = areaString return - end - if areas.hud[name].oldOwners ~= ownerString then - player:hud_change(areas.hud[name].ownersId, "text", - "Area owners: "..ownerString) - areas.hud[name].oldOwners = ownerString + elseif areas.hud[name].oldAreas ~= areaString then + player:hud_change(areas.hud[name].areasId, "text", + "Areas: "..areaString) + areas.hud[name].oldAreas = areaString end end end) diff --git a/legacy.lua b/legacy.lua index f11eca2..7eabf90 100644 --- a/legacy.lua +++ b/legacy.lua @@ -77,13 +77,8 @@ end -- Returns the name of the first player that owns an area function areas.getNodeOwnerName(pos) - for _, area in pairs(areas.areas) do - p1, p2 = area.pos1, area.pos2 - if pos.x >= p1.x and pos.x <= p2.x and - pos.y >= p1.y and pos.y <= p2.y and - pos.z >= p1.z and pos.z <= p2.z then - return area.owner - end + for id, area in pairs(areas:getAreasAtPos(pos)) do + return area.owner end return false end @@ -93,14 +88,9 @@ function areas.isNodeOwner(pos, name) if minetest.check_player_privs(name, {areas=true}) then return true end - for _, area in pairs(areas.areas) do - p1, p2 = area.pos1, area.pos2 - if pos.x >= p1.x and pos.x <= p2.x and - pos.y >= p1.y and pos.y <= p2.y and - pos.z >= p1.z and pos.z <= p2.z then - if name == area.owner then - return true - end + for id, area in pairs(areas:getAreasAtPos(pos)) do + if name == area.owner then + return true end end return false @@ -108,13 +98,8 @@ end -- Checks if an area has an owner function areas.hasOwner(pos) - for _, area in pairs(areas.areas) do - p1, p2 = area.pos1, area.pos2 - if pos.x >= p1.x and pos.x <= p2.x and - pos.y >= p1.y and pos.y <= p2.y and - pos.z >= p1.z and pos.z <= p2.z then - return true - end + for id, area in pairs(areas:getAreasAtPos(pos)) do + return true end return false end