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"
42 Area(const v3s16 &mine, const v3s16 &maxe)
46 sortBoxVerticies(minedge, maxedge);
58 void invalidateCache();
59 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0;
60 u32 getNextId() { return m_next_id++; }
62 // TODO change to unordered_map when we can
63 std::map<u32, Area> areas_map;
65 // Updates the area's ID
66 virtual bool insertArea(Area *a) = 0;
67 virtual void reserve(size_t count) {};
68 virtual bool removeArea(u32 id) = 0;
69 void getAreasForPos(std::vector<Area *> *result, v3s16 pos);
70 virtual void getAreasInArea(std::vector<Area *> *result,
71 v3s16 minedge, v3s16 maxedge, bool accept_overlap) = 0;
74 // calls a passed function for every stored area, until the
75 // callback returns true. If that happens, it returns true,
76 // if the search is exhausted, it returns false
77 virtual bool forEach(bool (*callback)(void *args, Area *a), void *args) const = 0;
84 m_cacheblock_radius(64),
85 m_res_cache(1000, &cacheMiss, this),
91 void setCacheParams(bool enabled, u8 block_radius, size_t limit);
93 const Area *getArea(u32 id) const;
96 static AreaStore *getOptimalImplementation();
98 bool deserialize(std::istream &is);
99 void serialize(std::ostream &is) const;
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;
106 bool m_cache_enabled;
110 class VectorAreaStore : public AreaStore {
112 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
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;
121 std::vector<Area *> m_areas;
126 class SpatialAreaStore : public AreaStore {
128 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
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;
137 virtual ~SpatialAreaStore();
139 SpatialIndex::ISpatialIndex *m_tree;
140 SpatialIndex::IStorageManager *m_storagemanager;
142 class VectorResultVisitor : public SpatialIndex::IVisitor {
144 SpatialAreaStore *m_store;
145 std::vector<Area *> *m_result;
147 VectorResultVisitor(std::vector<Area *> *result, SpatialAreaStore *store)
153 virtual void visitNode(const SpatialIndex::INode &in)
157 virtual void visitData(const SpatialIndex::IData &in)
159 u32 id = in.getIdentifier();
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);
166 virtual void visitData(std::vector<const SpatialIndex::IData *> &v)
168 for (size_t i = 0; i < v.size(); i++)
172 ~VectorResultVisitor() {}
178 #endif // AREA_STORE_H_