]> git.lizzy.rs Git - minetest.git/blob - src/server/serveractiveobjectmap.cpp
Fix various clang-tidy reported performance-type-promotion-in-math-fn
[minetest.git] / src / server / serveractiveobjectmap.cpp
1 /*
2 Minetest
3 Copyright (C) 2018 numZero, Lobachevsky Vitaly <numzer0@yandex.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "serveractiveobjectmap.h"
21 #include <cmath>
22 #include "constants.h"
23 #include "log.h"
24 #include "serverobject.h"
25
26 static constexpr float granularity = 16.0 * BS;
27
28 static aabb3s16 calcBox(const aabb3f &cb)
29 {
30         return aabb3s16(
31                         std::floor(cb.MinEdge.X / granularity),
32                         std::floor(cb.MinEdge.Y / granularity),
33                         std::floor(cb.MinEdge.Z / granularity),
34                         std::ceil(cb.MaxEdge.X / granularity),
35                         std::ceil(cb.MaxEdge.Y / granularity),
36                         std::ceil(cb.MaxEdge.Z / granularity));
37 }
38
39 void ServerActiveObjectMap::addObject(ServerActiveObject *object)
40 {
41         aabb3f cb;
42         Wrapper w;
43         u16 id = object->getId();
44         if (!isFreeId(id))
45                 throw std::logic_error("ServerActiveObjectMap::addObject: "
46                         "object ID in use: " + std::to_string(id));
47         w.object = object;
48         w.has_box = w.object->getCollisionBox(&cb);
49         if (w.has_box) {
50                 w.box = calcBox(cb);
51                 addObjectRefs(id, w.box);
52         }
53         objects.emplace(id, w);
54 }
55
56 ServerActiveObject *ServerActiveObjectMap::removeObject(u16 id)
57 {
58         auto pw = objects.find(id);
59         if (pw == objects.end())
60                 return nullptr; // silently ignore non-existent object
61         Wrapper w = pw->second;
62         if (w.has_box)
63                 removeObjectRefs(id, w.box);
64         objects.erase(pw);
65         return w.object;
66 }
67
68 void ServerActiveObjectMap::removeObject(ServerActiveObject *object)
69 {
70         removeObject(object->getId());
71 }
72
73 void ServerActiveObjectMap::updateObject(u16 id)
74 {
75         auto pw = objects.find(id);
76         if (pw == objects.end()) {
77                 warningstream << "Trying to update non-existent object: " << id
78                                 << std::endl;
79                 return;
80         }
81         Wrapper &w = pw->second;
82         aabb3f cb;
83         aabb3s16 box;
84         bool has_box = w.object->getCollisionBox(&cb);
85         if (has_box)
86                 box = calcBox(cb);
87         if (w.has_box && has_box && w.box == box)
88                 return;
89         if (w.has_box)
90                 removeObjectRefs(id, w.box);
91         w.box = box;
92         w.has_box = has_box;
93         if (w.has_box)
94                 addObjectRefs(id, w.box);
95 }
96
97 void ServerActiveObjectMap::updateObject(ServerActiveObject *object)
98 {
99         updateObject(object->getId());
100 }
101
102 ServerActiveObject *ServerActiveObjectMap::getObject(u16 id) const
103 {
104         auto pw = objects.find(id);
105         if (pw == objects.end())
106                 return nullptr;
107         return pw->second.object;
108 }
109
110 std::vector<u16> ServerActiveObjectMap::getObjectsInsideRadius(v3f pos, float radius)
111 {
112         std::vector<u16> result;
113         auto nearby = getObjectsNearBox(calcBox({pos - radius, pos + radius}));
114         for (auto &id : nearby) {
115                 ServerActiveObject *obj = getObject(id);
116                 v3f objectpos = obj->getBasePosition();
117                 if (objectpos.getDistanceFrom(pos) > radius)
118                         continue;
119                 result.push_back(id);
120         }
121         return result;
122 }
123
124 std::vector<u16> ServerActiveObjectMap::getObjectsTouchingBox(const aabb3f &box)
125 {
126         std::vector<u16> result;
127         auto nearby = getObjectsNearBox(calcBox(box));
128         for (auto &id : nearby) {
129                 ServerActiveObject *obj = getObject(id);
130                 aabb3f cb;
131                 if (!obj->getCollisionBox(&cb))
132                         continue;
133                 if (!box.intersectsWithBox(cb))
134                         continue;
135                 result.push_back(id);
136         }
137         return result;
138 }
139
140 std::unordered_set<u16> ServerActiveObjectMap::getObjectsNearBox(const aabb3s16 &box)
141 {
142         std::unordered_set<u16> result;
143         v3s16 p;
144         for (p.Z = box.MinEdge.Z; p.Z <= box.MaxEdge.Z; p.Z++)
145         for (p.Y = box.MinEdge.Y; p.Y <= box.MaxEdge.Y; p.Y++)
146         for (p.X = box.MinEdge.X; p.X <= box.MaxEdge.X; p.X++) {
147                 auto bounds = refmap.equal_range(p);
148                 for (auto iter = bounds.first; iter != bounds.second; ++iter)
149                         result.insert(iter->second);
150         }
151         return result;
152 }
153
154 void ServerActiveObjectMap::addObjectRefs(u16 id, const aabb3s16 &box)
155 {
156         v3s16 p;
157         for (p.Z = box.MinEdge.Z; p.Z <= box.MaxEdge.Z; p.Z++)
158         for (p.Y = box.MinEdge.Y; p.Y <= box.MaxEdge.Y; p.Y++)
159         for (p.X = box.MinEdge.X; p.X <= box.MaxEdge.X; p.X++)
160                 refmap.emplace(p, id);
161 }
162
163 void ServerActiveObjectMap::removeObjectRefs(u16 id, const aabb3s16 &box)
164 {
165         v3s16 p;
166         for (p.Z = box.MinEdge.Z; p.Z <= box.MaxEdge.Z; p.Z++)
167         for (p.Y = box.MinEdge.Y; p.Y <= box.MaxEdge.Y; p.Y++)
168         for (p.X = box.MinEdge.X; p.X <= box.MaxEdge.X; p.X++) {
169                 auto bounds = refmap.equal_range(p);
170                 for (auto iter = bounds.first; iter != bounds.second;)
171                         if (iter->second == id)
172                                 refmap.erase(iter++);
173                         else
174                                 ++iter;
175         }
176 }
177
178 bool ServerActiveObjectMap::isFreeId(u16 id)
179 {
180         if (id == 0)
181                 return false;
182         return objects.find(id) == objects.end();
183 }
184
185 u16 ServerActiveObjectMap::getFreeId()
186 {
187         // try to reuse id's as late as possible
188         static u16 last_used_id = 0;
189         u16 startid = last_used_id;
190         for (;;) {
191                 last_used_id++;
192                 if (isFreeId(last_used_id))
193                         return last_used_id;
194                 if (last_used_id == startid) // wrapped around
195                         return 0;
196         }
197 }