internal.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. function areas:player_exists(name)
  2. return minetest.get_auth_handler().get_auth(name) ~= nil
  3. end
  4. -- Save the areas table to a file
  5. function areas:save()
  6. local datastr = minetest.serialize(self.areas)
  7. if not datastr then
  8. minetest.log("error", "[areas] Failed to serialize area data!")
  9. return
  10. end
  11. local file, err = io.open(self.config.filename, "w")
  12. if err then
  13. return err
  14. end
  15. file:write(datastr)
  16. file:close()
  17. end
  18. -- Load the areas table from the save file
  19. function areas:load()
  20. local file, err = io.open(self.config.filename, "r")
  21. if err then
  22. self.areas = self.areas or {}
  23. return err
  24. end
  25. self.areas = minetest.deserialize(file:read("*a"))
  26. if type(self.areas) ~= "table" then
  27. self.areas = {}
  28. end
  29. file:close()
  30. self:populateStore()
  31. end
  32. --- Checks an AreaStore ID.
  33. -- Deletes the AreaStore (falling back to the iterative method)
  34. -- and prints an error message if the ID is invalid.
  35. -- @return Whether the ID was valid.
  36. function areas:checkAreaStoreId(sid)
  37. if not sid then
  38. minetest.log("error", "AreaStore failed to find an ID for an "
  39. .."area! Falling back to iterative area checking.")
  40. self.store = nil
  41. self.store_ids = nil
  42. end
  43. return sid and true or false
  44. end
  45. -- Populates the AreaStore after loading, if needed.
  46. function areas:populateStore()
  47. if not rawget(_G, "AreaStore") then
  48. return
  49. end
  50. local store = AreaStore()
  51. local store_ids = {}
  52. for id, area in pairs(areas.areas) do
  53. local sid = store:insert_area(area.pos1,
  54. area.pos2, tostring(id))
  55. if not self:checkAreaStoreId(sid) then
  56. return
  57. end
  58. store_ids[id] = sid
  59. end
  60. self.store = store
  61. self.store_ids = store_ids
  62. end
  63. -- Finds the first usable index in a table
  64. -- Eg: {[1]=false,[4]=true} -> 2
  65. local function findFirstUnusedIndex(t)
  66. local i = 0
  67. repeat i = i + 1
  68. until t[i] == nil
  69. return i
  70. end
  71. --- Add a area.
  72. -- @return The new area's ID.
  73. function areas:add(owner, name, pos1, pos2, parent)
  74. local id = findFirstUnusedIndex(self.areas)
  75. self.areas[id] = {
  76. name = name,
  77. pos1 = pos1,
  78. pos2 = pos2,
  79. owner = owner,
  80. parent = parent
  81. }
  82. -- Add to AreaStore
  83. if self.store then
  84. local sid = self.store:insert_area(pos1, pos2, tostring(id))
  85. if self:checkAreaStoreId(sid) then
  86. self.store_ids[id] = sid
  87. end
  88. end
  89. return id
  90. end
  91. --- Remove a area, and optionally it's children recursively.
  92. -- If a area is deleted non-recursively the children will
  93. -- have the removed area's parent as their new parent.
  94. function areas:remove(id, recurse)
  95. if recurse then
  96. -- Recursively find child entries and remove them
  97. local cids = self:getChildren(id)
  98. for _, cid in pairs(cids) do
  99. self:remove(cid, true)
  100. end
  101. else
  102. -- Update parents
  103. local parent = self.areas[id].parent
  104. local children = self:getChildren(id)
  105. for _, cid in pairs(children) do
  106. -- The subarea parent will be niled out if the
  107. -- removed area does not have a parent
  108. self.areas[cid].parent = parent
  109. end
  110. end
  111. -- Remove main entry
  112. self.areas[id] = nil
  113. -- Remove from AreaStore
  114. if self.store then
  115. self.store:remove_area(self.store_ids[id])
  116. self.store_ids[id] = nil
  117. end
  118. end
  119. --- Move an area.
  120. function areas:move(id, area, pos1, pos2)
  121. area.pos1 = pos1
  122. area.pos2 = pos2
  123. if self.store then
  124. self.store:remove_area(areas.store_ids[id])
  125. local sid = self.store:insert_area(pos1, pos2, tostring(id))
  126. if self:checkAreaStoreId(sid) then
  127. self.store_ids[id] = sid
  128. end
  129. end
  130. end
  131. -- Checks if a area between two points is entirely contained by another area.
  132. -- Positions must be sorted.
  133. function areas:isSubarea(pos1, pos2, id)
  134. local area = self.areas[id]
  135. if not area then
  136. return false
  137. end
  138. local ap1, ap2 = area.pos1, area.pos2
  139. local ap1x, ap1y, ap1z = ap1.x, ap1.y, ap1.z
  140. local ap2x, ap2y, ap2z = ap2.x, ap2.y, ap2.z
  141. local p1x, p1y, p1z = pos1.x, pos1.y, pos1.z
  142. local p2x, p2y, p2z = pos2.x, pos2.y, pos2.z
  143. if
  144. (p1x >= ap1x and p1x <= ap2x) and
  145. (p2x >= ap1x and p2x <= ap2x) and
  146. (p1y >= ap1y and p1y <= ap2y) and
  147. (p2y >= ap1y and p2y <= ap2y) and
  148. (p1z >= ap1z and p1z <= ap2z) and
  149. (p2z >= ap1z and p2z <= ap2z) then
  150. return true
  151. end
  152. end
  153. -- Returns a table (list) of children of an area given it's identifier
  154. function areas:getChildren(id)
  155. local children = {}
  156. for cid, area in pairs(self.areas) do
  157. if area.parent and area.parent == id then
  158. table.insert(children, cid)
  159. end
  160. end
  161. return children
  162. end
  163. -- Checks if the user has sufficient privileges.
  164. -- If the player is not a administrator it also checks
  165. -- if the area intersects other areas that they do not own.
  166. -- Also checks the size of the area and if the user already
  167. -- has more than max_areas.
  168. function areas:canPlayerAddArea(pos1, pos2, name)
  169. local privs = minetest.get_player_privs(name)
  170. if privs.areas then
  171. return true
  172. end
  173. -- Check self protection privilege, if it is enabled,
  174. -- and if the area is too big.
  175. if not self.config.self_protection or
  176. not privs[areas.config.self_protection_privilege] then
  177. return false, "Self protection is disabled or you do not have"
  178. .." the necessary privilege."
  179. end
  180. local max_size = privs.areas_high_limit and
  181. self.config.self_protection_max_size_high or
  182. self.config.self_protection_max_size
  183. if
  184. (pos2.x - pos1.x) > max_size.x or
  185. (pos2.y - pos1.y) > max_size.y or
  186. (pos2.z - pos1.z) > max_size.z then
  187. return false, "Area is too big."
  188. end
  189. -- Check number of areas the user has and make sure it not above the max
  190. local count = 0
  191. for _, area in pairs(self.areas) do
  192. if area.owner == name then
  193. count = count + 1
  194. end
  195. end
  196. local max_areas = privs.areas_high_limit and
  197. self.config.self_protection_max_areas_high or
  198. self.config.self_protection_max_areas
  199. if count >= max_areas then
  200. return false, "You have reached the maximum amount of"
  201. .." areas that you are allowed to protect."
  202. end
  203. -- Check intersecting areas
  204. local can, id = self:canInteractInArea(pos1, pos2, name)
  205. if not can then
  206. local area = self.areas[id]
  207. return false, ("The area intersects with %s [%u] (%s).")
  208. :format(area.name, id, area.owner)
  209. end
  210. return true
  211. end
  212. -- Given a id returns a string in the format:
  213. -- "name [id]: owner (x1, y1, z1) (x2, y2, z2) -> children"
  214. function areas:toString(id)
  215. local area = self.areas[id]
  216. local message = ("%s [%d]: %s %s %s"):format(
  217. area.name, id, area.owner,
  218. minetest.pos_to_string(area.pos1),
  219. minetest.pos_to_string(area.pos2))
  220. local children = areas:getChildren(id)
  221. if #children > 0 then
  222. message = message.." -> "..table.concat(children, ", ")
  223. end
  224. return message
  225. end
  226. -- Re-order areas in table by their identifiers
  227. function areas:sort()
  228. local sa = {}
  229. for k, area in pairs(self.areas) do
  230. if not area.parent then
  231. table.insert(sa, area)
  232. local newid = #sa
  233. for _, subarea in pairs(self.areas) do
  234. if subarea.parent == k then
  235. subarea.parent = newid
  236. table.insert(sa, subarea)
  237. end
  238. end
  239. end
  240. end
  241. self.areas = sa
  242. end
  243. -- Checks if a player owns an area or a parent of it
  244. function areas:isAreaOwner(id, name)
  245. local cur = self.areas[id]
  246. if cur and minetest.check_player_privs(name, self.adminPrivs) then
  247. return true
  248. end
  249. while cur do
  250. if cur.owner == name then
  251. return true
  252. elseif cur.parent then
  253. cur = self.areas[cur.parent]
  254. else
  255. return false
  256. end
  257. end
  258. return false
  259. end