Simplify AreaStore ID management
[oweals/minetest.git] / src / 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 AREASTORE_H_
21 #define AREASTORE_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() {}
42         Area(const v3s16 &mine, const v3s16 &maxe)
43         {
44                 minedge = mine;
45                 maxedge = maxe;
46                 sortBoxVerticies(minedge, maxedge);
47         }
48
49         u32 id;
50         v3s16 minedge;
51         v3s16 maxedge;
52         std::string data;
53 };
54
55 std::vector<std::string> get_areastore_typenames();
56
57 class AreaStore {
58 protected:
59         void invalidateCache();
60         virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0;
61         u32 getNextId() { return m_next_id++; }
62
63         // TODO change to unordered_map when we can
64         std::map<u32, Area> areas_map;
65         bool cache_enabled; // don't write to this from subclasses, only read.
66 public:
67         // Updates the area's ID
68         virtual bool insertArea(Area *a) = 0;
69         virtual void reserve(size_t count) {};
70         virtual bool removeArea(u32 id) = 0;
71         void getAreasForPos(std::vector<Area *> *result, v3s16 pos);
72         virtual void getAreasInArea(std::vector<Area *> *result,
73                 v3s16 minedge, v3s16 maxedge, bool accept_overlap) = 0;
74
75 #if 0
76         // calls a passed function for every stored area, until the
77         // callback returns true. If that happens, it returns true,
78         // if the search is exhausted, it returns false
79         virtual bool forEach(bool (*callback)(void *args, Area *a), void *args) const = 0;
80 #endif
81
82         virtual ~AreaStore()
83         {}
84
85         AreaStore() :
86                 cache_enabled(true),
87                 m_cacheblock_radius(64),
88                 m_res_cache(1000, &cacheMiss, this),
89                 m_next_id(0)
90         {
91         }
92
93         void setCacheParams(bool enabled, u8 block_radius, size_t limit);
94
95         const Area *getArea(u32 id) const;
96         u16 size() const;
97 #if 0
98         bool deserialize(std::istream &is);
99         void serialize(std::ostream &is) const;
100 #endif
101 private:
102         static void cacheMiss(void *data, const v3s16 &mpos, std::vector<Area *> *dest);
103         u8 m_cacheblock_radius; // if you modify this, call invalidateCache()
104         LRUCache<v3s16, std::vector<Area *> > m_res_cache;
105         u32 m_next_id;
106
107 };
108
109
110 class VectorAreaStore : public AreaStore {
111 protected:
112         virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
113 public:
114         virtual bool insertArea(Area *a);
115         virtual void reserve(size_t count);
116         virtual bool removeArea(u32 id);
117         virtual void getAreasInArea(std::vector<Area *> *result,
118                 v3s16 minedge, v3s16 maxedge, bool accept_overlap);
119         // virtual bool forEach(bool (*callback)(void *args, Area *a), void *args) const;
120 private:
121         std::vector<Area *> m_areas;
122 };
123
124 #if USE_SPATIAL
125
126 class SpatialAreaStore : public AreaStore {
127 protected:
128         virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
129 public:
130         SpatialAreaStore();
131         virtual bool insertArea(Area *a);
132         virtual bool removeArea(u32 id);
133         virtual void getAreasInArea(std::vector<Area *> *result,
134                 v3s16 minedge, v3s16 maxedge, bool accept_overlap);
135         // virtual bool forEach(bool (*callback)(void *args, Area *a), void *args) const;
136
137         virtual ~SpatialAreaStore();
138 private:
139         SpatialIndex::ISpatialIndex *m_tree;
140         SpatialIndex::IStorageManager *m_storagemanager;
141
142         class VectorResultVisitor : public SpatialIndex::IVisitor {
143         private:
144                 SpatialAreaStore *m_store;
145                 std::vector<Area *> *m_result;
146         public:
147                 VectorResultVisitor(std::vector<Area *> *result, SpatialAreaStore *store)
148                 {
149                         m_store = store;
150                         m_result = result;
151                 }
152
153                 virtual void visitNode(const SpatialIndex::INode &in)
154                 {
155                 }
156
157                 virtual void visitData(const SpatialIndex::IData &in)
158                 {
159                         u32 id = in.getIdentifier();
160
161                         std::map<u32, Area>::iterator itr = m_store->areas_map.find(id);
162                         assert(itr != m_store->areas_map.end());
163                         m_result->push_back(&itr->second);
164                 }
165
166                 virtual void visitData(std::vector<const SpatialIndex::IData *> &v)
167                 {
168                         for (size_t i = 0; i < v.size(); i++)
169                                 visitData(*(v[i]));
170                 }
171
172                 ~VectorResultVisitor() {}
173         };
174 };
175
176 #endif
177
178 #endif /* AREASTORE_H_ */