]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/areastore.cpp
Add function to get server info.
[dragonfireclient.git] / src / util / areastore.cpp
1 /*
2 Minetest
3 Copyright (C) 2015 est31 <mtest31@outlook.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 "util/areastore.h"
21 #include "util/serialize.h"
22 #include "util/container.h"
23
24 #if USE_SPATIAL
25         #include <spatialindex/SpatialIndex.h>
26         #include <spatialindex/RTree.h>
27         #include <spatialindex/Point.h>
28 #endif
29
30 #define AST_SMALLER_EQ_AS(p, q) (((p).X <= (q).X) && ((p).Y <= (q).Y) && ((p).Z <= (q).Z))
31
32 #define AST_OVERLAPS_IN_DIMENSION(amine, amaxe, b, d) \
33         (!(((amine).d > (b)->maxedge.d) || ((amaxe).d < (b)->minedge.d)))
34
35 #define AST_CONTAINS_PT(a, p) (AST_SMALLER_EQ_AS((a)->minedge, (p)) && \
36         AST_SMALLER_EQ_AS((p), (a)->maxedge))
37
38 #define AST_CONTAINS_AREA(amine, amaxe, b)         \
39         (AST_SMALLER_EQ_AS((amine), (b)->minedge) \
40         && AST_SMALLER_EQ_AS((b)->maxedge, (amaxe)))
41
42 #define AST_AREAS_OVERLAP(amine, amaxe, b)                \
43         (AST_OVERLAPS_IN_DIMENSION((amine), (amaxe), (b), X) && \
44         AST_OVERLAPS_IN_DIMENSION((amine), (amaxe), (b), Y) &&  \
45         AST_OVERLAPS_IN_DIMENSION((amine), (amaxe), (b), Z))
46
47
48 AreaStore *AreaStore::getOptimalImplementation()
49 {
50 #if USE_SPATIAL
51         return new SpatialAreaStore();
52 #else
53         return new VectorAreaStore();
54 #endif
55 }
56
57 const Area *AreaStore::getArea(u32 id) const
58 {
59         AreaMap::const_iterator it = areas_map.find(id);
60         if (it == areas_map.end())
61                 return NULL;
62         return &it->second;
63 }
64
65 void AreaStore::serialize(std::ostream &os) const
66 {
67         writeU8(os, 0); // Serialisation version
68
69         // TODO: Compression?
70         writeU16(os, areas_map.size());
71         for (AreaMap::const_iterator it = areas_map.begin();
72                         it != areas_map.end(); ++it) {
73                 const Area &a = it->second;
74                 writeV3S16(os, a.minedge);
75                 writeV3S16(os, a.maxedge);
76                 writeU16(os, a.data.size());
77                 os.write(a.data.data(), a.data.size());
78         }
79 }
80
81 void AreaStore::deserialize(std::istream &is)
82 {
83         u8 ver = readU8(is);
84         if (ver != 0)
85                 throw SerializationError("Unknown AreaStore "
86                                 "serialization version!");
87
88         u16 num_areas = readU16(is);
89         for (u32 i = 0; i < num_areas; ++i) {
90                 Area a;
91                 a.minedge = readV3S16(is);
92                 a.maxedge = readV3S16(is);
93                 u16 data_len = readU16(is);
94                 char *data = new char[data_len];
95                 is.read(data, data_len);
96                 a.data = std::string(data, data_len);
97                 insertArea(&a);
98                 delete [] data;
99         }
100 }
101
102 void AreaStore::invalidateCache()
103 {
104         if (m_cache_enabled) {
105                 m_res_cache.invalidate();
106         }
107 }
108
109 void AreaStore::setCacheParams(bool enabled, u8 block_radius, size_t limit)
110 {
111         m_cache_enabled = enabled;
112         m_cacheblock_radius = MYMAX(block_radius, 16);
113         m_res_cache.setLimit(MYMAX(limit, 20));
114         invalidateCache();
115 }
116
117 void AreaStore::cacheMiss(void *data, const v3s16 &mpos, std::vector<Area *> *dest)
118 {
119         AreaStore *as = (AreaStore *)data;
120         u8 r = as->m_cacheblock_radius;
121
122         // get the points at the edges of the mapblock
123         v3s16 minedge(mpos.X * r, mpos.Y * r, mpos.Z * r);
124         v3s16 maxedge(
125                 minedge.X + r - 1,
126                 minedge.Y + r - 1,
127                 minedge.Z + r - 1);
128
129         as->getAreasInArea(dest, minedge, maxedge, true);
130
131         /* infostream << "Cache miss with " << dest->size() << " areas, between ("
132                         << minedge.X << ", " << minedge.Y << ", " << minedge.Z
133                         << ") and ("
134                         << maxedge.X << ", " << maxedge.Y << ", " << maxedge.Z
135                         << ")" << std::endl; // */
136 }
137
138 void AreaStore::getAreasForPos(std::vector<Area *> *result, v3s16 pos)
139 {
140         if (m_cache_enabled) {
141                 v3s16 mblock = getContainerPos(pos, m_cacheblock_radius);
142                 const std::vector<Area *> *pre_list = m_res_cache.lookupCache(mblock);
143
144                 size_t s_p_l = pre_list->size();
145                 for (size_t i = 0; i < s_p_l; i++) {
146                         Area *b = (*pre_list)[i];
147                         if (AST_CONTAINS_PT(b, pos)) {
148                                 result->push_back(b);
149                         }
150                 }
151         } else {
152                 return getAreasForPosImpl(result, pos);
153         }
154 }
155
156
157 ////
158 // VectorAreaStore
159 ////
160
161
162 bool VectorAreaStore::insertArea(Area *a)
163 {
164         if (a->id == U32_MAX)
165                 a->id = getNextId();
166         std::pair<AreaMap::iterator, bool> res =
167                         areas_map.insert(std::make_pair(a->id, *a));
168         if (!res.second)
169                 // ID is not unique
170                 return false;
171         m_areas.push_back(&res.first->second);
172         invalidateCache();
173         return true;
174 }
175
176 bool VectorAreaStore::removeArea(u32 id)
177 {
178         AreaMap::iterator it = areas_map.find(id);
179         if (it == areas_map.end())
180                 return false;
181         Area *a = &it->second;
182         for (std::vector<Area *>::iterator v_it = m_areas.begin();
183                         v_it != m_areas.end(); ++v_it) {
184                 if (*v_it == a) {
185                         m_areas.erase(v_it);
186                         break;
187                 }
188         }
189         areas_map.erase(it);
190         invalidateCache();
191         return true;
192 }
193
194 void VectorAreaStore::getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos)
195 {
196         for (size_t i = 0; i < m_areas.size(); ++i) {
197                 Area *b = m_areas[i];
198                 if (AST_CONTAINS_PT(b, pos)) {
199                         result->push_back(b);
200                 }
201         }
202 }
203
204 void VectorAreaStore::getAreasInArea(std::vector<Area *> *result,
205                 v3s16 minedge, v3s16 maxedge, bool accept_overlap)
206 {
207         for (size_t i = 0; i < m_areas.size(); ++i) {
208                 Area *b = m_areas[i];
209                 if (accept_overlap ? AST_AREAS_OVERLAP(minedge, maxedge, b) :
210                                 AST_CONTAINS_AREA(minedge, maxedge, b)) {
211                         result->push_back(b);
212                 }
213         }
214 }
215
216 #if USE_SPATIAL
217
218 static inline SpatialIndex::Region get_spatial_region(const v3s16 minedge,
219                 const v3s16 maxedge)
220 {
221         const double p_low[] = {(double)minedge.X,
222                 (double)minedge.Y, (double)minedge.Z};
223         const double p_high[] = {(double)maxedge.X, (double)maxedge.Y,
224                 (double)maxedge.Z};
225         return SpatialIndex::Region(p_low, p_high, 3);
226 }
227
228 static inline SpatialIndex::Point get_spatial_point(const v3s16 pos)
229 {
230         const double p[] = {(double)pos.X, (double)pos.Y, (double)pos.Z};
231         return SpatialIndex::Point(p, 3);
232 }
233
234
235 bool SpatialAreaStore::insertArea(Area *a)
236 {
237         if (a->id == U32_MAX)
238                 a->id = getNextId();
239         if (!areas_map.insert(std::make_pair(a->id, *a)).second)
240                 // ID is not unique
241                 return false;
242         m_tree->insertData(0, NULL, get_spatial_region(a->minedge, a->maxedge), a->id);
243         invalidateCache();
244         return true;
245 }
246
247 bool SpatialAreaStore::removeArea(u32 id)
248 {
249         std::map<u32, Area>::iterator itr = areas_map.find(id);
250         if (itr != areas_map.end()) {
251                 Area *a = &itr->second;
252                 bool result = m_tree->deleteData(get_spatial_region(a->minedge,
253                         a->maxedge), id);
254                 areas_map.erase(itr);
255                 invalidateCache();
256                 return result;
257         } else {
258                 return false;
259         }
260 }
261
262 void SpatialAreaStore::getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos)
263 {
264         VectorResultVisitor visitor(result, this);
265         m_tree->pointLocationQuery(get_spatial_point(pos), visitor);
266 }
267
268 void SpatialAreaStore::getAreasInArea(std::vector<Area *> *result,
269                 v3s16 minedge, v3s16 maxedge, bool accept_overlap)
270 {
271         VectorResultVisitor visitor(result, this);
272         if (accept_overlap) {
273                 m_tree->intersectsWithQuery(get_spatial_region(minedge, maxedge),
274                         visitor);
275         } else {
276                 m_tree->containsWhatQuery(get_spatial_region(minedge, maxedge), visitor);
277         }
278 }
279
280 SpatialAreaStore::~SpatialAreaStore()
281 {
282         delete m_tree;
283 }
284
285 SpatialAreaStore::SpatialAreaStore()
286 {
287         m_storagemanager =
288                 SpatialIndex::StorageManager::createNewMemoryStorageManager();
289         SpatialIndex::id_type id;
290         m_tree = SpatialIndex::RTree::createNewRTree(
291                 *m_storagemanager,
292                 .7, // Fill factor
293                 100, // Index capacity
294                 100, // Leaf capacity
295                 3, // dimension :)
296                 SpatialIndex::RTree::RV_RSTAR,
297                 id);
298 }
299
300 #endif