mirror of
https://github.com/minetest-mods/areas.git
synced 2024-12-24 17:50:40 +01:00
Add IDs to HUD and areas:getAreasAtPos(pos)
This commit is contained in:
parent
f7f4c2ddb5
commit
ed7f57bf2f
38
api.lua
38
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
|
-- 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 area.owner == name then
|
||||||
if pos.x >= p1.x and pos.x <= p2.x and
|
return true
|
||||||
pos.y >= p1.y and pos.y <= p2.y and
|
else
|
||||||
pos.z >= p1.z and pos.z <= p2.z then
|
owned = true
|
||||||
if area.owner == name then
|
|
||||||
return true
|
|
||||||
else
|
|
||||||
owned = true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return not owned
|
return not owned
|
||||||
@ -23,12 +33,8 @@ 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
|
table.insert(owners, area.owner)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
return owners
|
return owners
|
||||||
end
|
end
|
||||||
|
30
hud.lua
30
hud.lua
@ -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)
|
||||||
|
29
legacy.lua
29
legacy.lua
@ -77,13 +77,8 @@ 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
|
return area.owner
|
||||||
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
|
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
@ -93,14 +88,9 @@ 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 name == area.owner then
|
||||||
if pos.x >= p1.x and pos.x <= p2.x and
|
return true
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
@ -108,13 +98,8 @@ 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
|
return true
|
||||||
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
|
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user