api.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. local hudHandlers = {}
  2. --- Adds a function as a HUD handler, it will be able to add items to the Areas HUD element.
  3. function areas:registerHudHandler(handler)
  4. table.insert(hudHandlers, handler)
  5. end
  6. function areas:getExternalHudEntries(pos)
  7. local areas = {}
  8. for _, func in pairs(hudHandlers) do
  9. func(pos, areas)
  10. end
  11. return areas
  12. end
  13. --- Returns a list of areas that include the provided position.
  14. function areas:getAreasAtPos(pos)
  15. local res = {}
  16. if self.store then
  17. local a = self.store:get_areas_for_pos(pos, false, true)
  18. for store_id, store_area in pairs(a) do
  19. local id = tonumber(store_area.data)
  20. res[id] = self.areas[id]
  21. end
  22. else
  23. local px, py, pz = pos.x, pos.y, pos.z
  24. for id, area in pairs(self.areas) do
  25. local ap1, ap2 = area.pos1, area.pos2
  26. if
  27. (px >= ap1.x and px <= ap2.x) and
  28. (py >= ap1.y and py <= ap2.y) and
  29. (pz >= ap1.z and pz <= ap2.z) then
  30. res[id] = area
  31. end
  32. end
  33. end
  34. return res
  35. end
  36. --- Returns areas that intersect with the passed area.
  37. function areas:getAreasIntersectingArea(pos1, pos2)
  38. local res = {}
  39. if self.store then
  40. local a = self.store:get_areas_in_area(pos1, pos2,
  41. true, false, true)
  42. for store_id, store_area in pairs(a) do
  43. local id = tonumber(store_area.data)
  44. res[id] = self.areas[id]
  45. end
  46. else
  47. self:sortPos(pos1, pos2)
  48. local p1x, p1y, p1z = pos1.x, pos1.y, pos1.z
  49. local p2x, p2y, p2z = pos2.x, pos2.y, pos2.z
  50. for id, area in pairs(self.areas) do
  51. local ap1, ap2 = area.pos1, area.pos2
  52. if
  53. (ap1.x <= p2x and ap2.x >= p1x) and
  54. (ap1.y <= p2y and ap2.y >= p1y) and
  55. (ap1.z <= p2z and ap2.z >= p1z) then
  56. -- Found an intersecting area.
  57. res[id] = area
  58. end
  59. end
  60. end
  61. return res
  62. end
  63. -- Checks if the area is unprotected or owned by you
  64. function areas:canInteract(pos, name)
  65. if minetest.check_player_privs(name, self.adminPrivs) then
  66. return true
  67. end
  68. local owned = false
  69. for _, area in pairs(self:getAreasAtPos(pos)) do
  70. if area.owner == name or area.open then
  71. return true
  72. else
  73. owned = true
  74. end
  75. end
  76. return not owned
  77. end
  78. -- Returns a table (list) of all players that own an area
  79. function areas:getNodeOwners(pos)
  80. local owners = {}
  81. for _, area in pairs(self:getAreasAtPos(pos)) do
  82. table.insert(owners, area.owner)
  83. end
  84. return owners
  85. end
  86. --- Checks if the area intersects with an area that the player can't interact in.
  87. -- Note that this fails and returns false when the specified area is fully
  88. -- owned by the player, but with multiple protection zones, none of which
  89. -- cover the entire checked area.
  90. -- @param name (optional) Player name. If not specified checks for any intersecting areas.
  91. -- @param allow_open Whether open areas should be counted as if they didn't exist.
  92. -- @return Boolean indicating whether the player can interact in that area.
  93. -- @return Un-owned intersecting area ID, if found.
  94. function areas:canInteractInArea(pos1, pos2, name, allow_open)
  95. if name and minetest.check_player_privs(name, self.adminPrivs) then
  96. return true
  97. end
  98. self:sortPos(pos1, pos2)
  99. -- Intersecting non-owned area ID, if found.
  100. local blocking_area = nil
  101. local areas = self:getAreasIntersectingArea(pos1, pos2)
  102. for id, area in pairs(areas) do
  103. -- First check for a fully enclosing owned area.
  104. -- A little optimization: isAreaOwner isn't necessary
  105. -- here since we're iterating over all relevant areas.
  106. if area.owner == name and
  107. self:isSubarea(pos1, pos2, id) then
  108. return true
  109. end
  110. -- Then check for intersecting non-owned (blocking) areas.
  111. -- We don't bother with this check if we've already found a
  112. -- blocking area, as the check is somewhat expensive.
  113. -- The area blocks if the area is closed or open areas aren't
  114. -- acceptable to the caller, and the area isn't owned.
  115. -- Note: We can't return directly here, because there might be
  116. -- an exclosing owned area that we haven't gotten to yet.
  117. if not blocking_area and
  118. (not allow_open or not area.open) and
  119. (not name or not self:isAreaOwner(id, name)) then
  120. blocking_area = id
  121. end
  122. end
  123. if blocking_area then
  124. return false, blocking_area
  125. end
  126. -- There are no intersecting areas or they are only partially
  127. -- intersecting areas and they are all owned by the player.
  128. return true
  129. end