Add IDs to HUD and areas:getAreasAtPos(pos)

This commit is contained in:
ShadowNinja 2014-01-15 20:10:24 -05:00
parent f7f4c2ddb5
commit ed7f57bf2f
3 changed files with 48 additions and 49 deletions

28
api.lua
View File

@ -1,35 +1,41 @@
-- 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
for _, area in pairs(self:getAreasAtPos(pos)) do
if area.owner == name then
return true
else
owned = true
end
end
end
return not owned
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
for _, area in pairs(self:getAreasAtPos(pos)) do
table.insert(owners, area.owner)
end
end
return owners
end

30
hud.lua
View File

@ -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)

View File

@ -77,14 +77,9 @@ 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
for id, area in pairs(areas:getAreasAtPos(pos)) do
return area.owner
end
end
return false
end
@ -93,29 +88,19 @@ 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
for id, area in pairs(areas:getAreasAtPos(pos)) do
if name == area.owner then
return true
end
end
end
return false
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
for id, area in pairs(areas:getAreasAtPos(pos)) do
return true
end
end
return false
end