api.lua 4.7 KB

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