From 21d96a9f007776c7929896332fa3ebbf8ce811d2 Mon Sep 17 00:00:00 2001 From: LeMagnesium Date: Thu, 8 Jan 2015 22:21:44 +0100 Subject: [PATCH] Update areas part 1 --- mods/areas/api.lua | 19 ++++++++++------ mods/areas/chatcommands.lua | 45 ++++++++++++++++++++++++++++--------- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/mods/areas/api.lua b/mods/areas/api.lua index 0afd72ed..0311c492 100755 --- a/mods/areas/api.lua +++ b/mods/areas/api.lua @@ -1,8 +1,9 @@ --- +--- + -- 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 + 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 @@ -42,17 +43,18 @@ end --- 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 --- owned by the player, but with miltiple protection zones, none of which +-- owned by the player, but with multiple protection zones, none of which -- cover the entire checked area. -- @param name (optional) player name. If not specified checks for any intersecting areas. +-- @param allow_open Whether open areas should be counted as is they didn't exist. -- @return Boolean indicating whether the player can interact in that area. -- @return Un-owned intersecting area id, if found. -function areas:canInteractInArea(pos1, pos2, name) +function areas:canInteractInArea(pos1, pos2, name, allow_open) if name and minetest.check_player_privs(name, self.adminPrivs) then return true end areas:sortPos(pos1, pos2) - -- First check for a fully enclosing owned area + -- First check for a fully enclosing owned area. if name then for id, area in pairs(self.areas) do -- A little optimization: isAreaOwner isn't necessary @@ -69,8 +71,11 @@ function areas:canInteractInArea(pos1, pos2, name) if (p1.x <= pos2.x and p2.x >= pos1.x) and (p1.y <= pos2.y and p2.y >= pos1.y) and (p1.z <= pos2.z and p2.z >= pos1.z) then - -- Found an intersecting area - if not name or not areas:isAreaOwner(id, name) then + -- Found an intersecting area. + -- Return if the area is closed or open areas aren't + -- allowed, and the area isn't owned. + if (not allow_open or not area.open) and + (not name or not areas:isAreaOwner(id, name)) then return false, id end end diff --git a/mods/areas/chatcommands.lua b/mods/areas/chatcommands.lua index b4673314..2bcbbf01 100755 --- a/mods/areas/chatcommands.lua +++ b/mods/areas/chatcommands.lua @@ -2,7 +2,7 @@ minetest.register_chatcommand("protect", { params = "", description = "Protect your own area", - privs = {[areas.self_protection_privilege]=true}, + privs = {[areas.config.self_protection_privilege]=true} func = function(name, param) if param == "" then return false, "Invalid usage, see /help protect." @@ -143,6 +143,7 @@ minetest.register_chatcommand("rename_area", { minetest.register_chatcommand("find_areas", { params = "", description = "Find areas using a Lua regular expression", + privs = areas.adminPrivs, func = function(name, param) if param == "" then return false, "A regular expression is required." @@ -156,14 +157,14 @@ minetest.register_chatcommand("find_areas", { return false, "Invalid regular expression." end - local matches = {} - for id, area in pairs(areas.areas) do - if areas:isAreaOwner(id, name) and - areas:toString(id):find(param) then - table.insert(matches, areas:toString(id)) + local matches = {} + for id, area in pairs(areas.areas) do + local str = areas:toString(id) + if str:find(param) then + table.insert(matches, str) end end - if #matches > 1 then + if #matches > 0 then return true, table.concat(matches, "\n") else return true, "No matches found." @@ -238,12 +239,11 @@ minetest.register_chatcommand("change_owner", { description = "Change the owner of an area using it's ID", func = function(name, param) local id, newOwner = param:match("^(%d+)%s(%S+)$") - if not id then return false, "Invalid usage, see" .." /help change_owner." end - + if not areas:player_exists(newOwner) then return false, "The player \""..newOwner .."\" does not exist." @@ -269,7 +269,6 @@ minetest.register_chatcommand("area_open", { description = "Toggle an area open (anyone can interact) or closed", func = function(name, param) local id = tonumber(param) - if not id then return false, "Invalid usage, see /help area_open." end @@ -286,3 +285,29 @@ minetest.register_chatcommand("area_open", { end }) +minetest.register_chatcommand("move_area", { + params = "", + description = "Move (or resize) an area to the current positions.", + privs = areas.adminPrivs, + func = function(name, param) + local id = tonumber(param) + if not id then + return false, "Invalid usage, see /help move_area." + end + + local area = areas.areas[id] + if not area then + return false, "Area does not exist." + end + + local pos1, pos2 = areas:getPos(name) + if not pos1 then + return false, "You need to select an area first." + end + + area.pos1 = pos1 + area.pos2 = pos2 + areas:save() + return true, "Area successfully moved." + end, +})