3 Copyright (C) 2015 est31 <mtest31@outlook.com>
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.
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.
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.
24 #include "noise.h" // for PcgRandom
29 #include "util/container.h"
30 #include "util/numeric.h"
32 #include "cmake_config.h"
35 #include <spatialindex/SpatialIndex.h>
36 #include "util/serialize.h"
39 #define AST_EXTREMIFY(min, max, pa, pb) \
40 (min).X = MYMIN((pa).X, (pb).X); \
41 (min).Y = MYMIN((pa).Y, (pb).Y); \
42 (min).Z = MYMIN((pa).Z, (pb).Z); \
43 (max).X = MYMAX((pa).X, (pb).X); \
44 (max).Y = MYMAX((pa).Y, (pb).Y); \
45 (max).Z = MYMAX((pa).Z, (pb).Z);
47 #define AREA_ID_INVALID 0
50 Area(const v3s16 &minedge, const v3s16 &maxedge)
52 this->minedge = minedge;
53 this->maxedge = maxedge;
63 AST_EXTREMIFY(nminedge, nmaxedge, minedge, maxedge)
75 std::vector<std::string> get_areastore_typenames();
79 // TODO change to unordered_map when we can
80 std::map<u32, Area> areas_map;
81 void invalidateCache();
82 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0;
83 bool cache_enabled; // don't write to this from subclasses, only read.
85 virtual void insertArea(const Area &a) = 0;
86 virtual void reserve(size_t count) {};
87 virtual bool removeArea(u32 id) = 0;
88 void getAreasForPos(std::vector<Area *> *result, v3s16 pos);
89 virtual void getAreasInArea(std::vector<Area *> *result,
90 v3s16 minedge, v3s16 maxedge, bool accept_overlap) = 0;
93 // calls a passed function for every stored area, until the
94 // callback returns true. If that happens, it returns true,
95 // if the search is exhausted, it returns false
96 virtual bool forEach(bool (*callback)(void *args, Area *a), void *args) const = 0;
104 m_cacheblock_radius(64),
105 m_res_cache(1000, &cacheMiss, this),
110 void setCacheParams(bool enabled, u8 block_radius, size_t limit);
112 u32 getFreeId(v3s16 minedge, v3s16 maxedge);
113 const Area *getArea(u32 id) const;
116 bool deserialize(std::istream &is);
117 void serialize(std::ostream &is) const;
120 static void cacheMiss(void *data, const v3s16 &mpos, std::vector<Area *> *dest);
121 u8 m_cacheblock_radius; // if you modify this, call invalidateCache()
122 LRUCache<v3s16, std::vector<Area *> > m_res_cache;
128 class VectorAreaStore : public AreaStore {
130 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
132 virtual void insertArea(const Area &a);
133 virtual void reserve(size_t count);
134 virtual bool removeArea(u32 id);
135 virtual void getAreasInArea(std::vector<Area *> *result,
136 v3s16 minedge, v3s16 maxedge, bool accept_overlap);
137 // virtual bool forEach(bool (*callback)(void *args, Area *a), void *args) const;
139 std::vector<Area *> m_areas;
144 class SpatialAreaStore : public AreaStore {
146 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
149 virtual void insertArea(const Area &a);
150 virtual bool removeArea(u32 id);
151 virtual void getAreasInArea(std::vector<Area *> *result,
152 v3s16 minedge, v3s16 maxedge, bool accept_overlap);
153 // virtual bool forEach(bool (*callback)(void *args, Area *a), void *args) const;
155 virtual ~SpatialAreaStore();
157 SpatialIndex::ISpatialIndex *m_tree;
158 SpatialIndex::IStorageManager *m_storagemanager;
160 class VectorResultVisitor : public SpatialIndex::IVisitor {
162 SpatialAreaStore *m_store;
163 std::vector<Area *> *m_result;
165 VectorResultVisitor(std::vector<Area *> *result, SpatialAreaStore *store)
171 virtual void visitNode(const SpatialIndex::INode &in)
175 virtual void visitData(const SpatialIndex::IData &in)
177 u32 id = in.getIdentifier();
179 std::map<u32, Area>::iterator itr = m_store->areas_map.find(id);
180 assert(itr != m_store->areas_map.end());
181 m_result->push_back(&itr->second);
184 virtual void visitData(std::vector<const SpatialIndex::IData *> &v)
186 for (size_t i = 0; i < v.size(); i++)
190 ~VectorResultVisitor() {}
196 #endif /* AREASTORE_H_ */