activeobjectmgr.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <cmath>
  17. #include <log.h>
  18. #include "profiler.h"
  19. #include "activeobjectmgr.h"
  20. namespace client
  21. {
  22. ActiveObjectMgr::~ActiveObjectMgr()
  23. {
  24. if (!m_active_objects.empty()) {
  25. warningstream << "client::ActiveObjectMgr::~ActiveObjectMgr(): not cleared."
  26. << std::endl;
  27. clear();
  28. }
  29. }
  30. void ActiveObjectMgr::step(
  31. float dtime, const std::function<void(ClientActiveObject *)> &f)
  32. {
  33. size_t count = 0;
  34. for (auto &ao_it : m_active_objects.iter()) {
  35. if (!ao_it.second)
  36. continue;
  37. count++;
  38. f(ao_it.second.get());
  39. }
  40. g_profiler->avg("ActiveObjectMgr: CAO count [#]", count);
  41. }
  42. bool ActiveObjectMgr::registerObject(std::unique_ptr<ClientActiveObject> obj)
  43. {
  44. assert(obj); // Pre-condition
  45. if (obj->getId() == 0) {
  46. u16 new_id = getFreeId();
  47. if (new_id == 0) {
  48. infostream << "Client::ActiveObjectMgr::registerObject(): "
  49. << "no free id available" << std::endl;
  50. return false;
  51. }
  52. obj->setId(new_id);
  53. }
  54. if (!isFreeId(obj->getId())) {
  55. infostream << "Client::ActiveObjectMgr::registerObject(): "
  56. << "id is not free (" << obj->getId() << ")" << std::endl;
  57. return false;
  58. }
  59. infostream << "Client::ActiveObjectMgr::registerObject(): "
  60. << "added (id=" << obj->getId() << ")" << std::endl;
  61. m_active_objects.put(obj->getId(), std::move(obj));
  62. return true;
  63. }
  64. void ActiveObjectMgr::removeObject(u16 id)
  65. {
  66. verbosestream << "Client::ActiveObjectMgr::removeObject(): "
  67. << "id=" << id << std::endl;
  68. std::unique_ptr<ClientActiveObject> obj = m_active_objects.take(id);
  69. if (!obj) {
  70. infostream << "Client::ActiveObjectMgr::removeObject(): "
  71. << "id=" << id << " not found" << std::endl;
  72. return;
  73. }
  74. obj->removeFromScene(true);
  75. }
  76. void ActiveObjectMgr::getActiveObjects(const v3f &origin, f32 max_d,
  77. std::vector<DistanceSortedActiveObject> &dest)
  78. {
  79. f32 max_d2 = max_d * max_d;
  80. for (auto &ao_it : m_active_objects.iter()) {
  81. ClientActiveObject *obj = ao_it.second.get();
  82. if (!obj)
  83. continue;
  84. f32 d2 = (obj->getPosition() - origin).getLengthSQ();
  85. if (d2 > max_d2)
  86. continue;
  87. dest.emplace_back(obj, d2);
  88. }
  89. }
  90. std::vector<DistanceSortedActiveObject> ActiveObjectMgr::getActiveSelectableObjects(const core::line3d<f32> &shootline)
  91. {
  92. std::vector<DistanceSortedActiveObject> dest;
  93. f32 max_d = shootline.getLength();
  94. v3f dir = shootline.getVector().normalize();
  95. for (auto &ao_it : m_active_objects.iter()) {
  96. ClientActiveObject *obj = ao_it.second.get();
  97. if (!obj)
  98. continue;
  99. aabb3f selection_box;
  100. if (!obj->getSelectionBox(&selection_box))
  101. continue;
  102. v3f obj_center = obj->getPosition() + selection_box.getCenter();
  103. f32 obj_radius_sq = selection_box.getExtent().getLengthSQ() / 4;
  104. v3f c = obj_center - shootline.start;
  105. f32 a = dir.dotProduct(c); // project c onto dir
  106. f32 b_sq = c.getLengthSQ() - a * a; // distance from shootline to obj_center, squared
  107. if (b_sq > obj_radius_sq)
  108. continue;
  109. // backward- and far-plane
  110. f32 obj_radius = std::sqrt(obj_radius_sq);
  111. if (a < -obj_radius || a > max_d + obj_radius)
  112. continue;
  113. dest.emplace_back(obj, a);
  114. }
  115. return dest;
  116. }
  117. } // namespace client