forked from voodooattack/ADWIF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapgenerator.hpp
261 lines (216 loc) · 7.84 KB
/
mapgenerator.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* Copyright (c) 2013, Abdullah A. Hassan <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef MAPGENERATOR_H
#define MAPGENERATOR_H
#include "config.hpp"
#include "animation.hpp"
#include <vector>
#include <string>
#include <map>
#include <cmath>
#include <unordered_map>
#include <memory>
#include <random>
#include <utility>
#include <boost/polygon/polygon.hpp>
#include <boost/multi_array.hpp>
#include <boost/polygon/gtl.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/index/rtree.hpp>
#include <boost/atomic.hpp>
#include <boost/thread/shared_mutex.hpp>
#include <boost/logic/tribool.hpp>
#ifdef NOISE_DIR_IS_LIBNOISE
#include <libnoise/noise.h>
#else
#include <noise/noise.h>
#endif
#include <json/value.h>
#include <FreeImagePlus.h>
namespace ADWIF
{
class Engine;
class Game;
class GenerateTerrainTask;
typedef boost::polygon::polygon_with_holes_data<double> Polygon;
typedef boost::polygon::polygon_traits<Polygon>::point_type Point2D;
typedef boost::geometry::model::point<int, 3, boost::geometry::cs::cartesian> Point3D;
typedef boost::geometry::model::box<Point3D> Box3D;
typedef std::pair<Box3D, std::shared_ptr<GenerateTerrainTask>> SIVal;
typedef boost::geometry::index::rtree<SIVal, boost::geometry::index::quadratic<16> > SpatialIndex;
struct BiomeCell
{
std::string name;
int x, y;
double height;
bool aquatic;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & name;
ar & x;
ar & y;
ar & height;
ar & aquatic;
}
bool operator== (const BiomeCell & other) const
{
return x == other.x && y == other.y && height == other.height && name == other.name && aquatic == other.aquatic;
}
};
struct Region
{
std::string biome;
std::string name;
std::string desc;
Point2D centroid;
Polygon poly;
double area;
bool infinite;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & biome;
ar & name;
ar & desc;
ar & centroid;
ar & poly;
ar & area;
ar & infinite;
}
};
class MapGenerator: public std::enable_shared_from_this<MapGenerator>
{
public:
MapGenerator(const std::shared_ptr<class Game> & game);
~MapGenerator();
std::shared_ptr<class Game> game() { return myGame.lock(); }
void game(std::shared_ptr<class Game> & game) { myGame = game; }
const fipImage & heightmap() const { return myHeightMap; }
void heightmapImage(const fipImage & image) { myHeightMap = image; myHeightMap.convertTo32Bits(); }
const fipImage & mapImage() const { return myMapImg; }
void mapImage(const fipImage & image) { myMapImg = image; myMapImg.convertTo32Bits(); }
int chunkSizeX() const { return myChunkSizeX; }
void chunkSizeX( int size) { myChunkSizeX = size; }
int chunkSizeY() const { return myChunkSizeY; }
void chunkSizeY( int size) { myChunkSizeY = size; }
int chunkSizeZ() const { return myChunkSizeZ; }
void chunkSizeZ( int size) { myChunkSizeZ = size; }
std::mt19937 & random() { return myRandomEngine; }
int height() const { return myHeight; }
int width() const { return myWidth; }
int depth() const { return myDepth; }
void depth( int depth) { myDepth = depth; }
const boost::multi_array<BiomeCell, 2> & biomeMap() const { return myBiomeMap; }
boost::multi_array<BiomeCell, 2> & biomeMap() { return myBiomeMap; }
const std::vector<Region> & regions() const { return myRegions; }
std::vector<Region> & regions() { return myRegions; }
int preprocessingProgress() const { return myMapPreprocessingProgress.load(); }
void init();
void generateAround( int x, int y, int z = 0, int radius = 1, int radiusZ = 1);
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & mySeed;
ar & myChunkSizeX;
ar & myChunkSizeY;
ar & myChunkSizeZ;
ar & myColourIndex;
ar & myRandomEngine;
ar & myGenerationMap;
ar & myBiomeMap;
ar & myHeights;
ar & myInitialisedFlag;
ar & myRegions;
ar & myHeight;
ar & myWidth;
ar & myDepth;
}
void notifyLoad();
void notifySave();
void abort();
inline int getHeight(double x, double y, double z = 0)
{
return ceil(getHeightReal(x, y, z));
}
inline double getHeightReal(double x, double y, double z = 0)
{
return myHeightSource->GetValue(x, y, z) * (double)myChunkSizeZ * ((double)myDepth / 2.0);
}
private:
bool generateBiomeMap();
public:
boost::logic::tribool isGenerated(int x, int y, int z)
{
boost::upgrade_lock<boost::shared_mutex> guard(myGenerationLock);
return myGenerationMap[x][y][z+myDepth/2];
}
void setGenerated(int x, int y, int z, const boost::logic::tribool & g)
{
boost::upgrade_lock<boost::shared_mutex> guard(myGenerationLock);
boost::upgrade_to_unique_lock<boost::shared_mutex> lock(guard);
myGenerationMap[x][y][z+myDepth/2] = g;
}
void notifyComplete(const std::shared_ptr<GenerateTerrainTask> & task);
private:
inline static uint32_t getPixelColour (int x, int y, const fipImage & img)
{
RGBQUAD pptc;
img.getPixelColor(x,y,&pptc);
return pptc.rgbBlue | pptc.rgbGreen << 8 | pptc.rgbRed << 16;
}
private:
std::weak_ptr<class Game> myGame;
fipImage myMapImg;
fipImage myHeightMap;
int myChunkSizeX, myChunkSizeY, myChunkSizeZ;
std::unordered_map<uint32_t, std::string> myColourIndex;
std::mt19937 myRandomEngine;
boost::multi_array<boost::tribool, 3> myGenerationMap;
boost::shared_mutex myGenerationLock;
boost::multi_array<BiomeCell, 2> myBiomeMap;
boost::multi_array<double, 2> myHeights;
std::vector<Region> myRegions;
int myHeight, myWidth, myDepth;
unsigned int mySeed;
std::vector<std::shared_ptr<noise::module::Module>> myNoiseModules;
std::map<std::string, std::shared_ptr<noise::module::Module>> myNoiseModuleDefs;
std::shared_ptr<noise::module::Module> myHeightSource;
SpatialIndex myIndex;
boost::atomic_int myMapPreprocessingProgress;
bool myInitialisedFlag;
};
}
namespace std
{
template <> struct hash<ADWIF::BiomeCell>
{
size_t operator()(const ADWIF::BiomeCell & bc) const
{
size_t h = 0;
boost::hash_combine(h, bc.name);
boost::hash_combine(h, bc.x);
boost::hash_combine(h, bc.y);
boost::hash_combine(h, bc.height);
return h;
}
};
}
#endif // MAPGENERATOR_H