activeobjectmgr.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include <log.h>
  17. #include "mapblock.h"
  18. #include "profiler.h"
  19. #include "activeobjectmgr.h"
  20. namespace server
  21. {
  22. ActiveObjectMgr::~ActiveObjectMgr()
  23. {
  24. if (!m_active_objects.empty()) {
  25. warningstream << "server::ActiveObjectMgr::~ActiveObjectMgr(): not cleared."
  26. << std::endl;
  27. clear();
  28. }
  29. }
  30. void ActiveObjectMgr::clearIf(const std::function<bool(ServerActiveObject *, u16)> &cb)
  31. {
  32. for (auto &it : m_active_objects.iter()) {
  33. if (!it.second)
  34. continue;
  35. if (cb(it.second.get(), it.first)) {
  36. // Remove reference from m_active_objects
  37. m_active_objects.remove(it.first);
  38. }
  39. }
  40. }
  41. void ActiveObjectMgr::step(
  42. float dtime, const std::function<void(ServerActiveObject *)> &f)
  43. {
  44. size_t count = 0;
  45. for (auto &ao_it : m_active_objects.iter()) {
  46. if (!ao_it.second)
  47. continue;
  48. count++;
  49. f(ao_it.second.get());
  50. }
  51. g_profiler->avg("ActiveObjectMgr: SAO count [#]", count);
  52. }
  53. bool ActiveObjectMgr::registerObject(std::unique_ptr<ServerActiveObject> obj)
  54. {
  55. assert(obj); // Pre-condition
  56. if (obj->getId() == 0) {
  57. u16 new_id = getFreeId();
  58. if (new_id == 0) {
  59. errorstream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
  60. << "no free id available" << std::endl;
  61. return false;
  62. }
  63. obj->setId(new_id);
  64. } else {
  65. verbosestream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
  66. << "supplied with id " << obj->getId() << std::endl;
  67. }
  68. if (!isFreeId(obj->getId())) {
  69. errorstream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
  70. << "id is not free (" << obj->getId() << ")" << std::endl;
  71. return false;
  72. }
  73. if (objectpos_over_limit(obj->getBasePosition())) {
  74. v3f p = obj->getBasePosition();
  75. warningstream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
  76. << "object position (" << p.X << "," << p.Y << "," << p.Z
  77. << ") outside maximum range" << std::endl;
  78. return false;
  79. }
  80. auto obj_id = obj->getId();
  81. m_active_objects.put(obj_id, std::move(obj));
  82. auto new_size = m_active_objects.size();
  83. verbosestream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
  84. << "Added id=" << obj_id << "; there are now ";
  85. if (new_size == decltype(m_active_objects)::unknown)
  86. verbosestream << "???";
  87. else
  88. verbosestream << new_size;
  89. verbosestream << " active objects." << std::endl;
  90. return true;
  91. }
  92. void ActiveObjectMgr::removeObject(u16 id)
  93. {
  94. verbosestream << "Server::ActiveObjectMgr::removeObject(): "
  95. << "id=" << id << std::endl;
  96. // this will take the object out of the map and then destruct it
  97. bool ok = m_active_objects.remove(id);
  98. if (!ok) {
  99. infostream << "Server::ActiveObjectMgr::removeObject(): "
  100. << "id=" << id << " not found" << std::endl;
  101. }
  102. }
  103. void ActiveObjectMgr::getObjectsInsideRadius(const v3f &pos, float radius,
  104. std::vector<ServerActiveObject *> &result,
  105. std::function<bool(ServerActiveObject *obj)> include_obj_cb)
  106. {
  107. float r2 = radius * radius;
  108. for (auto &activeObject : m_active_objects.iter()) {
  109. ServerActiveObject *obj = activeObject.second.get();
  110. if (!obj)
  111. continue;
  112. const v3f &objectpos = obj->getBasePosition();
  113. if (objectpos.getDistanceFromSQ(pos) > r2)
  114. continue;
  115. if (!include_obj_cb || include_obj_cb(obj))
  116. result.push_back(obj);
  117. }
  118. }
  119. void ActiveObjectMgr::getObjectsInArea(const aabb3f &box,
  120. std::vector<ServerActiveObject *> &result,
  121. std::function<bool(ServerActiveObject *obj)> include_obj_cb)
  122. {
  123. for (auto &activeObject : m_active_objects.iter()) {
  124. ServerActiveObject *obj = activeObject.second.get();
  125. if (!obj)
  126. continue;
  127. const v3f &objectpos = obj->getBasePosition();
  128. if (!box.isPointInside(objectpos))
  129. continue;
  130. if (!include_obj_cb || include_obj_cb(obj))
  131. result.push_back(obj);
  132. }
  133. }
  134. void ActiveObjectMgr::getAddedActiveObjectsAroundPos(const v3f &player_pos, f32 radius,
  135. f32 player_radius, std::set<u16> &current_objects,
  136. std::queue<u16> &added_objects)
  137. {
  138. /*
  139. Go through the object list,
  140. - discard removed/deactivated objects,
  141. - discard objects that are too far away,
  142. - discard objects that are found in current_objects.
  143. - add remaining objects to added_objects
  144. */
  145. for (auto &ao_it : m_active_objects.iter()) {
  146. u16 id = ao_it.first;
  147. // Get object
  148. ServerActiveObject *object = ao_it.second.get();
  149. if (!object)
  150. continue;
  151. if (object->isGone())
  152. continue;
  153. f32 distance_f = object->getBasePosition().getDistanceFrom(player_pos);
  154. if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
  155. // Discard if too far
  156. if (distance_f > player_radius && player_radius != 0)
  157. continue;
  158. } else if (distance_f > radius)
  159. continue;
  160. // Discard if already on current_objects
  161. auto n = current_objects.find(id);
  162. if (n != current_objects.end())
  163. continue;
  164. // Add to added_objects
  165. added_objects.push(id);
  166. }
  167. }
  168. } // namespace server