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 -- Checks if the area is unprotected or owned by you
function areas:canInteract(pos, name) function areas:canInteract(pos, name)
if minetest.check_player_privs(name, {areas=true}) then if minetest.check_player_privs(name, {areas=true}) then
return true return true
end end
local owned = false local owned = false
for _, area in pairs(self.areas) do for _, area in pairs(self:getAreasAtPos(pos)) 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 if area.owner == name then
return true return true
else else
owned = true owned = true
end end
end end
end
return not owned return not owned
end end
-- Returns a table (list) of all players that own an area -- Returns a table (list) of all players that own an area
function areas:getNodeOwners(pos) function areas:getNodeOwners(pos)
local owners = {} local owners = {}
for _, area in pairs(self.areas) do for _, area in pairs(self:getAreasAtPos(pos)) 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) table.insert(owners, area.owner)
end end
end
return owners return owners
end end

30
hud.lua
View File

@ -6,28 +6,36 @@ minetest.register_globalstep(function(dtime)
for _, player in pairs(minetest.get_connected_players()) do for _, player in pairs(minetest.get_connected_players()) do
local name = player:get_player_name() local name = player:get_player_name()
local pos = vector.round(player:getpos()) local pos = vector.round(player:getpos())
local owners = areas:getNodeOwners(pos) local a = areas:getAreasAtPos(pos)
local ownerString = table.concat(owners, ", ") 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 if not areas.hud[name] then
areas.hud[name] = {} areas.hud[name] = {}
areas.hud[name].ownersId = player:hud_add({ areas.hud[name].areasId = player:hud_add({
hud_elem_type = "text", hud_elem_type = "text",
name = "AreaOwners", name = "Areas",
number = 0xFFFFFF, number = 0xFFFFFF,
position = {x=0, y=1}, position = {x=0, y=1},
offset = {x=5, y=-60}, offset = {x=5, y=-60},
direction = 0, direction = 0,
text = "Area owners: "..ownerString, text = "Areas: "..areaString,
scale = {x=200, y=60}, scale = {x=200, y=60},
alignment = {x=1, y=1}, alignment = {x=1, y=1},
}) })
areas.hud[name].oldOwners = ownerString areas.hud[name].oldAreas = areaString
return return
end elseif areas.hud[name].oldAreas ~= areaString then
if areas.hud[name].oldOwners ~= ownerString then player:hud_change(areas.hud[name].areasId, "text",
player:hud_change(areas.hud[name].ownersId, "text", "Areas: "..areaString)
"Area owners: "..ownerString) areas.hud[name].oldAreas = areaString
areas.hud[name].oldOwners = ownerString
end end
end end
end) end)

View File

@ -77,14 +77,9 @@ end
-- Returns the name of the first player that owns an area -- Returns the name of the first player that owns an area
function areas.getNodeOwnerName(pos) function areas.getNodeOwnerName(pos)
for _, area in pairs(areas.areas) do for id, area in pairs(areas:getAreasAtPos(pos)) 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 return area.owner
end end
end
return false return false
end end
@ -93,29 +88,19 @@ function areas.isNodeOwner(pos, name)
if minetest.check_player_privs(name, {areas=true}) then if minetest.check_player_privs(name, {areas=true}) then
return true return true
end end
for _, area in pairs(areas.areas) do for id, area in pairs(areas:getAreasAtPos(pos)) 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 if name == area.owner then
return true return true
end end
end end
end
return false return false
end end
-- Checks if an area has an owner -- Checks if an area has an owner
function areas.hasOwner(pos) function areas.hasOwner(pos)
for _, area in pairs(areas.areas) do for id, area in pairs(areas:getAreasAtPos(pos)) 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 return true
end end
end
return false return false
end end