2016-11-26 13:14:30 +01:00
|
|
|
local protection_detectors = {}
|
2013-09-03 01:16:14 +02:00
|
|
|
|
2016-11-26 13:32:11 +01:00
|
|
|
-- Other protection mods should be able to display their protection in the hud
|
2016-11-26 13:23:46 +01:00
|
|
|
areas.register_hud_handler = function(handler)
|
2016-11-26 14:27:15 +01:00
|
|
|
protection_detectors[#protection_detectors + 1] = handler
|
2016-11-26 13:14:30 +01:00
|
|
|
end
|
|
|
|
|
2016-11-26 13:32:11 +01:00
|
|
|
-- Generalized call to registered handlers to add their proeciton labels to the areas list
|
2016-11-26 14:27:15 +01:00
|
|
|
local detect_extra_protection = function(pos,area_list)
|
2016-11-26 13:14:30 +01:00
|
|
|
if #protection_detectors <= 0 then
|
2016-11-26 14:27:15 +01:00
|
|
|
return area_list
|
2016-11-26 13:14:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
for idx=1,#protection_detectors do
|
|
|
|
local func = protection_detectors[idx]
|
2016-11-26 14:27:15 +01:00
|
|
|
area_list = func(pos,area_list)
|
2016-11-26 08:20:23 +01:00
|
|
|
end
|
2016-11-26 14:27:15 +01:00
|
|
|
return area_list
|
2016-11-26 08:20:23 +01:00
|
|
|
end
|
|
|
|
|
2015-07-09 11:42:13 +02:00
|
|
|
--- Returns a list of areas that include the provided position.
|
2014-01-16 02:10:24 +01:00
|
|
|
function areas:getAreasAtPos(pos)
|
2015-07-09 11:42:13 +02:00
|
|
|
local res = {}
|
2016-11-26 13:14:30 +01:00
|
|
|
res = detect_extra_protection(pos,res)
|
2016-11-26 08:20:23 +01:00
|
|
|
|
2015-07-09 11:42:13 +02:00
|
|
|
if self.store then
|
|
|
|
local a = self.store:get_areas_for_pos(pos, false, true)
|
|
|
|
for store_id, store_area in pairs(a) do
|
|
|
|
local id = tonumber(store_area.data)
|
|
|
|
res[id] = self.areas[id]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
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
|
2016-11-26 08:20:23 +01:00
|
|
|
(px >= ap1.x and px <= ap2.x) and
|
|
|
|
(py >= ap1.y and py <= ap2.y) and
|
|
|
|
(pz >= ap1.z and pz <= ap2.z)
|
|
|
|
then
|
2015-07-09 11:42:13 +02:00
|
|
|
res[id] = area
|
|
|
|
end
|
2014-01-16 02:10:24 +01:00
|
|
|
end
|
|
|
|
end
|
2015-07-09 11:42:13 +02:00
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Returns areas that intersect with the passed area.
|
|
|
|
function areas:getAreasIntersectingArea(pos1, pos2)
|
|
|
|
local res = {}
|
|
|
|
if self.store then
|
|
|
|
local a = self.store:get_areas_in_area(pos1, pos2,
|
|
|
|
true, false, true)
|
|
|
|
for store_id, store_area in pairs(a) do
|
|
|
|
local id = tonumber(store_area.data)
|
|
|
|
res[id] = self.areas[id]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self:sortPos(pos1, pos2)
|
|
|
|
local p1x, p1y, p1z = pos1.x, pos1.y, pos1.z
|
|
|
|
local p2x, p2y, p2z = pos2.x, pos2.y, pos2.z
|
|
|
|
for id, area in pairs(self.areas) do
|
|
|
|
local ap1, ap2 = area.pos1, area.pos2
|
|
|
|
if
|
|
|
|
(ap1.x <= p2x and ap2.x >= p1x) and
|
|
|
|
(ap1.y <= p2y and ap2.y >= p1y) and
|
|
|
|
(ap1.z <= p2z and ap2.z >= p1z) then
|
|
|
|
-- Found an intersecting area.
|
|
|
|
res[id] = area
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return res
|
2014-01-16 02:10:24 +01:00
|
|
|
end
|
|
|
|
|
2013-09-03 01:16:14 +02:00
|
|
|
-- Checks if the area is unprotected or owned by you
|
|
|
|
function areas:canInteract(pos, name)
|
2014-07-13 01:06:05 +02:00
|
|
|
if minetest.check_player_privs(name, self.adminPrivs) then
|
2013-09-03 01:16:14 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
local owned = false
|
2014-01-16 02:10:24 +01:00
|
|
|
for _, area in pairs(self:getAreasAtPos(pos)) do
|
2014-03-23 17:25:44 +01:00
|
|
|
if area.owner == name or area.open then
|
2014-01-16 02:10:24 +01:00
|
|
|
return true
|
|
|
|
else
|
2014-03-23 17:25:44 +01:00
|
|
|
owned = true
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return not owned
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Returns a table (list) of all players that own an area
|
|
|
|
function areas:getNodeOwners(pos)
|
|
|
|
local owners = {}
|
2014-01-16 02:10:24 +01:00
|
|
|
for _, area in pairs(self:getAreasAtPos(pos)) do
|
|
|
|
table.insert(owners, area.owner)
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
|
|
|
return owners
|
|
|
|
end
|
|
|
|
|
2014-07-12 22:37:54 +02:00
|
|
|
--- Checks if the area intersects with an area that the player can't interact in.
|
|
|
|
-- Note that this fails and returns false when the specified area is fully
|
2014-11-14 19:21:09 +01:00
|
|
|
-- owned by the player, but with multiple protection zones, none of which
|
2014-07-12 22:37:54 +02:00
|
|
|
-- cover the entire checked area.
|
2015-07-09 11:42:13 +02:00
|
|
|
-- @param name (optional) Player name. If not specified checks for any intersecting areas.
|
|
|
|
-- @param allow_open Whether open areas should be counted as if they didn't exist.
|
2014-07-12 22:37:54 +02:00
|
|
|
-- @return Boolean indicating whether the player can interact in that area.
|
2015-07-09 11:42:13 +02:00
|
|
|
-- @return Un-owned intersecting area ID, if found.
|
2014-11-14 19:21:09 +01:00
|
|
|
function areas:canInteractInArea(pos1, pos2, name, allow_open)
|
2014-07-13 01:06:05 +02:00
|
|
|
if name and minetest.check_player_privs(name, self.adminPrivs) then
|
|
|
|
return true
|
|
|
|
end
|
2015-07-09 11:42:13 +02:00
|
|
|
self:sortPos(pos1, pos2)
|
|
|
|
|
|
|
|
-- Intersecting non-owned area ID, if found.
|
|
|
|
local blocking_area = nil
|
|
|
|
|
|
|
|
local areas = self:getAreasIntersectingArea(pos1, pos2)
|
|
|
|
for id, area in pairs(areas) do
|
|
|
|
-- First check for a fully enclosing owned area.
|
|
|
|
-- A little optimization: isAreaOwner isn't necessary
|
|
|
|
-- here since we're iterating over all relevant areas.
|
|
|
|
if area.owner == name and
|
|
|
|
self:isSubarea(pos1, pos2, id) then
|
|
|
|
return true
|
2014-07-12 22:37:54 +02:00
|
|
|
end
|
2015-07-09 11:42:13 +02:00
|
|
|
|
|
|
|
-- Then check for intersecting non-owned (blocking) areas.
|
|
|
|
-- We don't bother with this check if we've already found a
|
|
|
|
-- blocking area, as the check is somewhat expensive.
|
|
|
|
-- The area blocks if the area is closed or open areas aren't
|
|
|
|
-- acceptable to the caller, and the area isn't owned.
|
|
|
|
-- Note: We can't return directly here, because there might be
|
|
|
|
-- an exclosing owned area that we haven't gotten to yet.
|
|
|
|
if not blocking_area and
|
|
|
|
(not allow_open or not area.open) and
|
|
|
|
(not name or not self:isAreaOwner(id, name)) then
|
|
|
|
blocking_area = id
|
2014-07-12 22:37:54 +02:00
|
|
|
end
|
|
|
|
end
|
2015-07-09 11:42:13 +02:00
|
|
|
|
|
|
|
if blocking_area then
|
|
|
|
return false, blocking_area
|
|
|
|
end
|
|
|
|
|
|
|
|
-- There are no intersecting areas or they are only partially
|
|
|
|
-- intersecting areas and they are all owned by the player.
|
2014-07-12 22:37:54 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|