activeobjectmgr.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "profiler.h"
  18. #include "activeobjectmgr.h"
  19. namespace client
  20. {
  21. void ActiveObjectMgr::clear()
  22. {
  23. // delete active objects
  24. for (auto &active_object : m_active_objects) {
  25. delete active_object.second;
  26. // Object must be marked as gone when children try to detach
  27. active_object.second = nullptr;
  28. }
  29. m_active_objects.clear();
  30. }
  31. void ActiveObjectMgr::step(
  32. float dtime, const std::function<void(ClientActiveObject *)> &f)
  33. {
  34. g_profiler->avg("ActiveObjectMgr: CAO count [#]", m_active_objects.size());
  35. for (auto &ao_it : m_active_objects) {
  36. f(ao_it.second);
  37. }
  38. }
  39. // clang-format off
  40. bool ActiveObjectMgr::registerObject(ClientActiveObject *obj)
  41. {
  42. assert(obj); // Pre-condition
  43. if (obj->getId() == 0) {
  44. u16 new_id = getFreeId();
  45. if (new_id == 0) {
  46. infostream << "Client::ActiveObjectMgr::registerObject(): "
  47. << "no free id available" << std::endl;
  48. delete obj;
  49. return false;
  50. }
  51. obj->setId(new_id);
  52. }
  53. if (!isFreeId(obj->getId())) {
  54. infostream << "Client::ActiveObjectMgr::registerObject(): "
  55. << "id is not free (" << obj->getId() << ")" << std::endl;
  56. delete obj;
  57. return false;
  58. }
  59. infostream << "Client::ActiveObjectMgr::registerObject(): "
  60. << "added (id=" << obj->getId() << ")" << std::endl;
  61. m_active_objects[obj->getId()] = obj;
  62. return true;
  63. }
  64. void ActiveObjectMgr::removeObject(u16 id)
  65. {
  66. verbosestream << "Client::ActiveObjectMgr::removeObject(): "
  67. << "id=" << id << std::endl;
  68. ClientActiveObject *obj = getActiveObject(id);
  69. if (!obj) {
  70. infostream << "Client::ActiveObjectMgr::removeObject(): "
  71. << "id=" << id << " not found" << std::endl;
  72. return;
  73. }
  74. m_active_objects.erase(id);
  75. obj->removeFromScene(true);
  76. delete obj;
  77. }
  78. // clang-format on
  79. void ActiveObjectMgr::getActiveObjects(const v3f &origin, f32 max_d,
  80. std::vector<DistanceSortedActiveObject> &dest)
  81. {
  82. f32 max_d2 = max_d * max_d;
  83. for (auto &ao_it : m_active_objects) {
  84. ClientActiveObject *obj = ao_it.second;
  85. f32 d2 = (obj->getPosition() - origin).getLengthSQ();
  86. if (d2 > max_d2)
  87. continue;
  88. dest.emplace_back(obj, d2);
  89. }
  90. }
  91. } // namespace client