]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/areastore.h
Add function to get server info.
[dragonfireclient.git] / src / util / areastore.h
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 #ifndef AREA_STORE_H_
21 #define AREA_STORE_H_
22
23 #include "irr_v3d.h"
24 #include "noise.h" // for PcgRandom
25 #include <map>
26 #include <list>
27 #include <vector>
28 #include <istream>
29 #include "util/container.h"
30 #include "util/numeric.h"
31 #ifndef ANDROID
32         #include "cmake_config.h"
33 #endif
34 #if USE_SPATIAL
35         #include <spatialindex/SpatialIndex.h>
36         #include "util/serialize.h"
37 #endif
38
39
40 struct Area {
41         Area() : id(U32_MAX) {}
42         Area(const v3s16 &mine, const v3s16 &maxe) :
43                 id(U32_MAX), minedge(mine), maxedge(maxe)
44         {
45                 sortBoxVerticies(minedge, maxedge);
46         }
47
48         u32 id;
49         v3s16 minedge, maxedge;
50         std::string data;
51 };
52
53
54 class AreaStore {
55 public:
56         AreaStore() :
57                 m_cache_enabled(true),
58                 m_cacheblock_radius(64),
59                 m_res_cache(1000, &cacheMiss, this),
60                 m_next_id(0)
61         {}
62
63         virtual ~AreaStore() {}
64
65         static AreaStore *getOptimalImplementation();
66
67         virtual void reserve(size_t count) {};
68         size_t size() const { return areas_map.size(); }
69
70         /// Add an area to the store.
71         /// Updates the area's ID if it hasn't already been set.
72         /// @return Whether the area insertion was successful.
73         virtual bool insertArea(Area *a) = 0;
74
75         /// Removes an area from the store by ID.
76         /// @return Whether the area was in the store and removed.
77         virtual bool removeArea(u32 id) = 0;
78
79         /// Finds areas that the passed position is contained in.
80         /// Stores output in passed vector.
81         void getAreasForPos(std::vector<Area *> *result, v3s16 pos);
82
83         /// Finds areas that are completely contained inside the area defined
84         /// by the passed edges.  If @p accept_overlap is true this finds any
85         /// areas that intersect with the passed area at any point.
86         virtual void getAreasInArea(std::vector<Area *> *result,
87                 v3s16 minedge, v3s16 maxedge, bool accept_overlap) = 0;
88
89         /// Sets cache parameters.
90         void setCacheParams(bool enabled, u8 block_radius, size_t limit);
91
92         /// Returns a pointer to the area coresponding to the passed ID,
93         /// or NULL if it doesn't exist.
94         const Area *getArea(u32 id) const;
95
96         /// Serializes the store's areas to a binary ostream.
97         void serialize(std::ostream &is) const;
98
99         /// Deserializes the Areas from a binary istream.
100         /// This does not currently clear the AreaStore before adding the
101         /// areas, making it possible to deserialize multiple serialized
102         /// AreaStores.
103         void deserialize(std::istream &is);
104
105 protected:
106         /// Invalidates the getAreasForPos cache.
107         /// Call after adding or removing an area.
108         void invalidateCache();
109
110         /// Implementation of getAreasForPos.
111         /// getAreasForPos calls this if the cache is disabled.
112         virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0;
113
114         /// Returns the next area ID and increments it.
115         u32 getNextId() { return m_next_id++; }
116
117         // Note: This can't be an unordered_map, since all
118         // references would be invalidated on rehash.
119         typedef std::map<u32, Area> AreaMap;
120         AreaMap areas_map;
121
122 private:
123         /// Called by the cache when a value isn't found in the cache.
124         static void cacheMiss(void *data, const v3s16 &mpos, std::vector<Area *> *dest);
125
126         bool m_cache_enabled;
127         /// Range, in nodes, of the getAreasForPos cache.
128         /// If you modify this, call invalidateCache()
129         u8 m_cacheblock_radius;
130         LRUCache<v3s16, std::vector<Area *> > m_res_cache;
131
132         u32 m_next_id;
133 };
134
135
136 class VectorAreaStore : public AreaStore {
137 public:
138         virtual void reserve(size_t count) { m_areas.reserve(count); }
139         virtual bool insertArea(Area *a);
140         virtual bool removeArea(u32 id);
141         virtual void getAreasInArea(std::vector<Area *> *result,
142                 v3s16 minedge, v3s16 maxedge, bool accept_overlap);
143
144 protected:
145         virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
146
147 private:
148         std::vector<Area *> m_areas;
149 };
150
151
152 #if USE_SPATIAL
153
154 class SpatialAreaStore : public AreaStore {
155 public:
156         SpatialAreaStore();
157         virtual ~SpatialAreaStore();
158
159         virtual bool insertArea(Area *a);
160         virtual bool removeArea(u32 id);
161         virtual void getAreasInArea(std::vector<Area *> *result,
162                 v3s16 minedge, v3s16 maxedge, bool accept_overlap);
163
164 protected:
165         virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
166
167 private:
168         SpatialIndex::ISpatialIndex *m_tree;
169         SpatialIndex::IStorageManager *m_storagemanager;
170
171         class VectorResultVisitor : public SpatialIndex::IVisitor {
172         public:
173                 VectorResultVisitor(std::vector<Area *> *result, SpatialAreaStore *store) :
174                         m_store(store),
175                         m_result(result)
176                 {}
177                 ~VectorResultVisitor() {}
178
179                 virtual void visitNode(const SpatialIndex::INode &in) {}
180
181                 virtual void visitData(const SpatialIndex::IData &in)
182                 {
183                         u32 id = in.getIdentifier();
184
185                         std::map<u32, Area>::iterator itr = m_store->areas_map.find(id);
186                         assert(itr != m_store->areas_map.end());
187                         m_result->push_back(&itr->second);
188                 }
189
190                 virtual void visitData(std::vector<const SpatialIndex::IData *> &v)
191                 {
192                         for (size_t i = 0; i < v.size(); i++)
193                                 visitData(*(v[i]));
194                 }
195
196         private:
197                 SpatialAreaStore *m_store;
198                 std::vector<Area *> *m_result;
199         };
200 };
201
202 #endif // USE_SPATIAL
203
204 #endif // AREA_STORE_H_