2013-11-19 03:01:24 +01:00
|
|
|
|
2013-09-03 01:16:14 +02:00
|
|
|
function areas:player_exists(name)
|
|
|
|
return minetest.auth_table[name] ~= nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Save the areas table to a file
|
|
|
|
function areas:save()
|
2014-02-04 21:11:02 +01:00
|
|
|
local datastr = minetest.serialize(self.areas)
|
|
|
|
if not datastr then
|
|
|
|
minetest.log("error", "[areas] Failed to serialize area data!")
|
|
|
|
return
|
|
|
|
end
|
2014-11-18 03:16:59 +01:00
|
|
|
local file, err = io.open(self.config.filename, "w")
|
2013-09-03 01:16:14 +02:00
|
|
|
if err then
|
|
|
|
return err
|
|
|
|
end
|
2014-02-04 21:11:02 +01:00
|
|
|
file:write(datastr)
|
2013-09-03 01:16:14 +02:00
|
|
|
file:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Load the areas table from the save file
|
|
|
|
function areas:load()
|
2014-11-18 03:16:59 +01:00
|
|
|
local file, err = io.open(self.config.filename, "r")
|
2013-09-03 01:16:14 +02:00
|
|
|
if err then
|
|
|
|
self.areas = self.areas or {}
|
|
|
|
return err
|
|
|
|
end
|
|
|
|
self.areas = minetest.deserialize(file:read("*a"))
|
2013-11-19 03:01:24 +01:00
|
|
|
if type(self.areas) ~= "table" then
|
|
|
|
self.areas = {}
|
|
|
|
end
|
2013-09-03 01:16:14 +02:00
|
|
|
file:close()
|
2015-07-09 11:42:13 +02:00
|
|
|
self:populateStore()
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Checks an AreaStore ID.
|
|
|
|
-- Deletes the AreaStore (falling back to the iterative method)
|
|
|
|
-- and prints an error message if the ID is invalid.
|
|
|
|
-- @return Whether the ID was valid.
|
|
|
|
function areas:checkAreaStoreId(sid)
|
|
|
|
if not sid then
|
|
|
|
minetest.log("error", "AreaStore failed to find an ID for an "
|
|
|
|
.."area! Falling back to iterative area checking.")
|
|
|
|
self.store = nil
|
|
|
|
self.store_ids = nil
|
|
|
|
end
|
|
|
|
return sid and true or false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Populates the AreaStore after loading, if needed.
|
|
|
|
function areas:populateStore()
|
|
|
|
if not rawget(_G, "AreaStore") then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local store = AreaStore()
|
|
|
|
local store_ids = {}
|
|
|
|
for id, area in pairs(areas.areas) do
|
|
|
|
local sid = store:insert_area(area.pos1,
|
|
|
|
area.pos2, tostring(id))
|
|
|
|
if not self:checkAreaStoreId(sid) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
store_ids[id] = sid
|
|
|
|
end
|
|
|
|
self.store = store
|
|
|
|
self.store_ids = store_ids
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
|
|
|
|
2013-11-19 03:01:24 +01:00
|
|
|
-- Finds the first usable index in a table
|
|
|
|
-- Eg: {[1]=false,[4]=true} -> 2
|
|
|
|
local function findFirstUnusedIndex(t)
|
2014-01-16 01:32:51 +01:00
|
|
|
local i = 0
|
|
|
|
repeat i = i + 1
|
|
|
|
until t[i] == nil
|
|
|
|
return i
|
2013-11-19 03:01:24 +01:00
|
|
|
end
|
|
|
|
|
2015-07-09 11:42:13 +02:00
|
|
|
--- Add a area.
|
|
|
|
-- @return The new area's ID.
|
2013-09-03 01:16:14 +02:00
|
|
|
function areas:add(owner, name, pos1, pos2, parent)
|
2013-11-19 03:01:24 +01:00
|
|
|
local id = findFirstUnusedIndex(self.areas)
|
2015-07-09 11:42:13 +02:00
|
|
|
self.areas[id] = {
|
|
|
|
name = name,
|
|
|
|
pos1 = pos1,
|
|
|
|
pos2 = pos2,
|
|
|
|
owner = owner,
|
|
|
|
parent = parent
|
|
|
|
}
|
|
|
|
-- Add to AreaStore
|
|
|
|
if self.store then
|
|
|
|
local sid = self.store:insert_area(pos1, pos2, tostring(id))
|
|
|
|
if self:checkAreaStoreId(sid) then
|
|
|
|
self.store_ids[id] = sid
|
|
|
|
end
|
|
|
|
end
|
2013-11-19 03:01:24 +01:00
|
|
|
return id
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
|
|
|
|
2015-07-09 11:42:13 +02:00
|
|
|
--- Remove a area, and optionally it's children recursively.
|
2013-09-03 01:16:14 +02:00
|
|
|
-- If a area is deleted non-recursively the children will
|
|
|
|
-- have the removed area's parent as their new parent.
|
2014-11-14 19:21:09 +01:00
|
|
|
function areas:remove(id, recurse)
|
2013-09-03 01:16:14 +02:00
|
|
|
if recurse then
|
|
|
|
-- Recursively find child entries and remove them
|
|
|
|
local cids = self:getChildren(id)
|
|
|
|
for _, cid in pairs(cids) do
|
2014-11-14 19:21:09 +01:00
|
|
|
self:remove(cid, true)
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
-- Update parents
|
2013-11-19 03:01:24 +01:00
|
|
|
local parent = self.areas[id].parent
|
2013-09-03 01:16:14 +02:00
|
|
|
local children = self:getChildren(id)
|
2013-11-19 03:01:24 +01:00
|
|
|
for _, cid in pairs(children) do
|
2013-09-03 07:33:08 +02:00
|
|
|
-- The subarea parent will be niled out if the
|
|
|
|
-- removed area does not have a parent
|
2013-11-19 03:01:24 +01:00
|
|
|
self.areas[cid].parent = parent
|
2013-09-03 01:16:14 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Remove main entry
|
2013-11-19 03:01:24 +01:00
|
|
|
self.areas[id] = nil
|
2015-07-09 11:42:13 +02:00
|
|
|
|
|
|
|
-- Remove from AreaStore
|
|
|
|
if self.store then
|
|
|
|
self.store:remove_area(self.store_ids[id])
|
|
|
|
self.store_ids[id] = nil
|
|
|
|
end
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Checks if a area between two points is entirely contained by another area
|
|
|
|
function areas:isSubarea(pos1, pos2, id)
|
2013-11-19 03:01:24 +01:00
|
|
|
local area = self.areas[id]
|
2013-09-03 07:33:08 +02:00
|
|
|
if not area then
|
|
|
|
return false
|
|
|
|
end
|
2014-07-12 22:37:54 +02:00
|
|
|
local p1, p2 = area.pos1, area.pos2
|
2013-09-03 07:33:08 +02:00
|
|
|
if (pos1.x >= p1.x and pos1.x <= p2.x) and
|
|
|
|
(pos2.x >= p1.x and pos2.x <= p2.x) and
|
|
|
|
(pos1.y >= p1.y and pos1.y <= p2.y) and
|
|
|
|
(pos2.y >= p1.y and pos2.y <= p2.y) and
|
|
|
|
(pos1.z >= p1.z and pos1.z <= p2.z) and
|
|
|
|
(pos2.z >= p1.z and pos2.z <= p2.z) then
|
|
|
|
return true
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Returns a table (list) of children of an area given it's identifier
|
|
|
|
function areas:getChildren(id)
|
|
|
|
local children = {}
|
2013-11-19 03:01:24 +01:00
|
|
|
for cid, area in pairs(self.areas) do
|
2013-09-03 01:16:14 +02:00
|
|
|
if area.parent and area.parent == id then
|
2013-11-19 03:01:24 +01:00
|
|
|
table.insert(children, cid)
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return children
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Checks if the user has sufficient privileges.
|
|
|
|
-- If the player is not a administrator it also checks
|
|
|
|
-- if the area intersects other areas that they do not own.
|
2013-09-03 07:33:08 +02:00
|
|
|
-- Also checks the size of the area and if the user already
|
|
|
|
-- has more than max_areas.
|
2013-09-03 01:16:14 +02:00
|
|
|
function areas:canPlayerAddArea(pos1, pos2, name)
|
2014-11-14 19:21:09 +01:00
|
|
|
local privs = minetest.get_player_privs(name)
|
|
|
|
if privs.areas then
|
2013-09-03 01:16:14 +02:00
|
|
|
return true
|
2013-09-03 07:33:08 +02:00
|
|
|
end
|
2013-09-03 01:16:14 +02:00
|
|
|
|
2013-09-03 07:33:08 +02:00
|
|
|
-- Check self protection privilege, if it is enabled,
|
|
|
|
-- and if the area is too big.
|
2014-11-18 03:16:59 +01:00
|
|
|
if not self.config.self_protection or
|
|
|
|
not privs[areas.config.self_protection_privilege] then
|
2013-09-03 07:33:08 +02:00
|
|
|
return false, "Self protection is disabled or you do not have"
|
|
|
|
.." the necessary privilege."
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
|
|
|
|
2014-11-14 19:21:09 +01:00
|
|
|
local max_size = privs.areas_high_limit and
|
2014-11-18 03:16:59 +01:00
|
|
|
self.config.self_protection_max_size_high or
|
|
|
|
self.config.self_protection_max_size
|
2014-11-14 19:21:09 +01:00
|
|
|
if
|
|
|
|
(pos2.x - pos1.x) > max_size.x or
|
|
|
|
(pos2.y - pos1.y) > max_size.y or
|
|
|
|
(pos2.z - pos1.z) > max_size.z then
|
2013-09-03 01:16:14 +02:00
|
|
|
return false, "Area is too big."
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check number of areas the user has and make sure it not above the max
|
2014-01-16 02:13:47 +01:00
|
|
|
local count = 0
|
|
|
|
for _, area in pairs(self.areas) do
|
|
|
|
if area.owner == name then
|
|
|
|
count = count + 1
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
|
|
|
end
|
2014-11-14 19:21:09 +01:00
|
|
|
local max_areas = privs.areas_high_limit and
|
2014-11-18 03:16:59 +01:00
|
|
|
self.config.self_protection_max_areas_high or
|
|
|
|
self.config.self_protection_max_areas
|
2014-11-14 19:21:09 +01:00
|
|
|
if count >= max_areas then
|
2014-03-23 04:24:29 +01:00
|
|
|
return false, "You have reached the maximum amount of"
|
|
|
|
.." areas that you are allowed to protect."
|
2014-01-16 02:13:47 +01:00
|
|
|
end
|
2013-09-03 01:16:14 +02:00
|
|
|
|
|
|
|
-- Check intersecting areas
|
2014-07-12 22:37:54 +02:00
|
|
|
local can, id = self:canInteractInArea(pos1, pos2, name)
|
|
|
|
if not can then
|
|
|
|
local area = self.areas[id]
|
2014-11-14 19:21:09 +01:00
|
|
|
return false, ("The area intersects with %s [%u] (%s).")
|
2014-07-12 22:37:54 +02:00
|
|
|
:format(area.name, id, area.owner)
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
|
|
|
|
2014-03-23 04:24:29 +01:00
|
|
|
return true
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
|
|
|
|
2013-11-19 03:01:24 +01:00
|
|
|
-- Given a id returns a string in the format:
|
2013-09-03 07:33:08 +02:00
|
|
|
-- "name [id]: owner (x1, y1, z1) (x2, y2, z2) -> children"
|
2013-11-19 03:01:24 +01:00
|
|
|
function areas:toString(id)
|
|
|
|
local area = self.areas[id]
|
|
|
|
local message = ("%s [%d]: %s %s %s"):format(
|
|
|
|
area.name, id, area.owner,
|
|
|
|
minetest.pos_to_string(area.pos1),
|
|
|
|
minetest.pos_to_string(area.pos2))
|
2013-09-03 01:16:14 +02:00
|
|
|
|
|
|
|
local children = areas:getChildren(id)
|
|
|
|
if #children > 0 then
|
2013-11-19 03:01:24 +01:00
|
|
|
message = message.." -> "..table.concat(children, ", ")
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
2013-11-19 03:01:24 +01:00
|
|
|
return message
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Re-order areas in table by their identifiers
|
|
|
|
function areas:sort()
|
2013-11-19 03:01:24 +01:00
|
|
|
local sa = {}
|
2013-09-03 01:16:14 +02:00
|
|
|
for k, area in pairs(self.areas) do
|
2013-11-19 03:01:24 +01:00
|
|
|
if not area.parent then
|
|
|
|
table.insert(sa, area)
|
|
|
|
local newid = #sa
|
2013-09-03 01:16:14 +02:00
|
|
|
for _, subarea in pairs(self.areas) do
|
2013-11-19 03:01:24 +01:00
|
|
|
if subarea.parent == k then
|
|
|
|
subarea.parent = newid
|
|
|
|
table.insert(sa, subarea)
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-11-19 03:01:24 +01:00
|
|
|
self.areas = sa
|
2013-09-03 01:16:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Checks if a player owns an area or a parent of it
|
|
|
|
function areas:isAreaOwner(id, name)
|
2013-11-19 03:01:24 +01:00
|
|
|
local cur = self.areas[id]
|
2014-07-13 01:10:26 +02:00
|
|
|
if cur and minetest.check_player_privs(name, self.adminPrivs) then
|
2013-09-03 01:16:14 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
while cur do
|
|
|
|
if cur.owner == name then
|
|
|
|
return true
|
|
|
|
elseif cur.parent then
|
2013-11-19 03:01:24 +01:00
|
|
|
cur = self.areas[cur.parent]
|
2013-09-03 01:16:14 +02:00
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|