serveractiveobjectmap.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. Minetest
  3. Copyright (C) 2018 numZero, Lobachevsky Vitaly <numzer0@yandex.com>
  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 "serveractiveobjectmap.h"
  17. #include <cmath>
  18. #include "constants.h"
  19. #include "log.h"
  20. #include "serverobject.h"
  21. static constexpr float granularity = 16.0 * BS;
  22. static v3s16 getChunkPos(const v3f &pos)
  23. {
  24. return v3s16(
  25. std::floor(pos.X / granularity),
  26. std::floor(pos.Y / granularity),
  27. std::floor(pos.Z / granularity));
  28. }
  29. static aabb3s16 calcBox(const aabb3f &cb)
  30. {
  31. return { getChunkPos(cb.MinEdge), getChunkPos(cb.MaxEdge) };
  32. }
  33. void ServerActiveObjectMap::addObject(ServerActiveObject *object)
  34. {
  35. aabb3f cb;
  36. Wrapper w;
  37. u16 id = object->getId();
  38. if (!isFreeId(id))
  39. throw std::logic_error("ServerActiveObjectMap::addObject: "
  40. "object ID in use: " + std::to_string(id));
  41. w.object = object;
  42. w.has_box = w.object->getCollisionBox(&cb);
  43. w.pos = getChunkPos(w.object->getBasePosition());
  44. if (w.has_box) {
  45. w.box = calcBox(cb);
  46. addObjectRefs(id, w.box);
  47. }
  48. addObjectRef(id, w.pos);
  49. objects.emplace(id, w);
  50. }
  51. ServerActiveObject *ServerActiveObjectMap::removeObject(u16 id)
  52. {
  53. auto pw = objects.find(id);
  54. if (pw == objects.end())
  55. return nullptr; // silently ignore non-existent object
  56. Wrapper w = pw->second;
  57. if (w.has_box)
  58. removeObjectRefs(id, w.box);
  59. removeObjectRef(id, w.pos);
  60. objects.erase(pw);
  61. return w.object;
  62. }
  63. void ServerActiveObjectMap::removeObject(ServerActiveObject *object)
  64. {
  65. removeObject(object->getId());
  66. }
  67. void ServerActiveObjectMap::updateObject(u16 id)
  68. {
  69. auto pw = objects.find(id);
  70. if (pw == objects.end()) {
  71. warningstream << "Trying to update non-existent object: " << id
  72. << std::endl;
  73. return;
  74. }
  75. Wrapper &w = pw->second;
  76. v3s16 pos = getChunkPos(w.object->getBasePosition());
  77. aabb3f cb;
  78. aabb3s16 box;
  79. bool has_box = w.object->getCollisionBox(&cb);
  80. if (has_box)
  81. box = calcBox(cb);
  82. if (w.has_box && has_box && w.box == box && pos == w.pos)
  83. return;
  84. if (w.has_box)
  85. removeObjectRefs(id, w.box);
  86. removeObjectRef(id, w.pos);
  87. w.box = box;
  88. w.has_box = has_box;
  89. if (w.has_box)
  90. addObjectRefs(id, w.box);
  91. addObjectRef(id, w.pos);
  92. }
  93. void ServerActiveObjectMap::updateObject(ServerActiveObject *object)
  94. {
  95. updateObject(object->getId());
  96. }
  97. ServerActiveObject *ServerActiveObjectMap::getObject(u16 id) const
  98. {
  99. auto pw = objects.find(id);
  100. if (pw == objects.end())
  101. return nullptr;
  102. return pw->second.object;
  103. }
  104. std::vector<u16> ServerActiveObjectMap::getObjectsInsideRadius(v3f pos, float radius)
  105. {
  106. std::vector<u16> result;
  107. auto nearby = getObjectsNearBox(calcBox({pos - radius, pos + radius}));
  108. for (auto &id : nearby) {
  109. ServerActiveObject *obj = getObject(id);
  110. v3f objectpos = obj->getBasePosition();
  111. if (objectpos.getDistanceFrom(pos) > radius)
  112. continue;
  113. result.push_back(id);
  114. }
  115. return result;
  116. }
  117. std::vector<u16> ServerActiveObjectMap::getObjectsTouchingBox(const aabb3f &box)
  118. {
  119. std::vector<u16> result;
  120. auto nearby = getObjectsNearBox(calcBox(box));
  121. for (auto &id : nearby) {
  122. ServerActiveObject *obj = getObject(id);
  123. aabb3f cb;
  124. if (!obj->getCollisionBox(&cb))
  125. continue;
  126. if (!box.intersectsWithBox(cb))
  127. continue;
  128. result.push_back(id);
  129. }
  130. return result;
  131. }
  132. std::unordered_set<u16> ServerActiveObjectMap::getObjectsNearBox(const aabb3s16 &box)
  133. {
  134. std::unordered_set<u16> result;
  135. v3s16 p;
  136. for (p.Z = box.MinEdge.Z; p.Z <= box.MaxEdge.Z; p.Z++)
  137. for (p.Y = box.MinEdge.Y; p.Y <= box.MaxEdge.Y; p.Y++)
  138. for (p.X = box.MinEdge.X; p.X <= box.MaxEdge.X; p.X++) {
  139. auto bounds = refmap.equal_range(p);
  140. for (auto iter = bounds.first; iter != bounds.second; ++iter)
  141. result.insert(iter->second);
  142. }
  143. return result;
  144. }
  145. void ServerActiveObjectMap::addObjectRef(u16 id, v3s16 pos)
  146. {
  147. refmap.emplace(pos, id);
  148. }
  149. void ServerActiveObjectMap::removeObjectRef(u16 id, v3s16 pos)
  150. {
  151. auto bounds = refmap.equal_range(pos);
  152. for (auto iter = bounds.first; iter != bounds.second;) {
  153. if (iter->second == id)
  154. iter = refmap.erase(iter);
  155. else
  156. ++iter;
  157. }
  158. }
  159. void ServerActiveObjectMap::addObjectRefs(u16 id, const aabb3s16 &box)
  160. {
  161. v3s16 p;
  162. for (p.Z = box.MinEdge.Z; p.Z <= box.MaxEdge.Z; p.Z++)
  163. for (p.Y = box.MinEdge.Y; p.Y <= box.MaxEdge.Y; p.Y++)
  164. for (p.X = box.MinEdge.X; p.X <= box.MaxEdge.X; p.X++)
  165. addObjectRef(id, p);
  166. }
  167. void ServerActiveObjectMap::removeObjectRefs(u16 id, const aabb3s16 &box)
  168. {
  169. v3s16 p;
  170. for (p.Z = box.MinEdge.Z; p.Z <= box.MaxEdge.Z; p.Z++)
  171. for (p.Y = box.MinEdge.Y; p.Y <= box.MaxEdge.Y; p.Y++)
  172. for (p.X = box.MinEdge.X; p.X <= box.MaxEdge.X; p.X++)
  173. removeObjectRef(id, p);
  174. }
  175. bool ServerActiveObjectMap::isFreeId(u16 id)
  176. {
  177. if (id == 0)
  178. return false;
  179. return objects.find(id) == objects.end();
  180. }
  181. u16 ServerActiveObjectMap::getFreeId()
  182. {
  183. // try to reuse id's as late as possible
  184. static u16 last_used_id = 0;
  185. u16 startid = last_used_id;
  186. for (;;) {
  187. last_used_id++;
  188. if (isFreeId(last_used_id))
  189. return last_used_id;
  190. if (last_used_id == startid) // wrapped around
  191. return 0;
  192. }
  193. }