summaryrefslogtreecommitdiffstats
path: root/src/Generating/VerticalStrategy.cpp
blob: 0651958f9eb8ca70c35d21408da146c9875ecb7c (plain) (blame)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348

// VerticalStrategy.cpp

// Implements the various classes descending from cPiece::cVerticalStrategy

#include "Globals.h"
#include "VerticalStrategy.h"
#include "../Noise/Noise.h"





// Constant that is added to random seed
static const int SEED_OFFSET = 135;





// Emit a warning if the first param is true
#define CONDWARNING(ShouldLog, Fmt, ...) \
	do { \
		if (ShouldLog) \
		{ \
			LOGWARNING(Fmt, __VA_ARGS__); \
		} \
	} while (false)





////////////////////////////////////////////////////////////////////////////////
// Globals:

/** Parses a string containing a range in which both values are optional ("<MinHeight>|<MaxHeight>") into Min, Range.
Returns true if successful, false on failure.
If a_LogWarnings is true, outputs failure reasons to console.
The range is returned in a_Min and a_Range, they are left unchanged if the range value is not present in the string. */
namespace VerticalStrategy
{
	static bool ParseRange(const AString & a_Params, int & a_Min, int & a_Range, bool a_LogWarnings)
	{
		auto params = StringSplitAndTrim(a_Params, "|");
		if (params.size() == 0)
		{
			// No params, generate directly on top:
			return true;
		}
		if (!StringToInteger(params[0], a_Min))
		{
			// Failed to parse the min rel height:
			CONDWARNING(a_LogWarnings, "Cannot parse minimum height from string \"%s\"!", params[0].c_str());
			return false;
		}
		if (params.size() == 1)
		{
			// Only one param was given, there's no range
			return true;
		}
		int maxHeight = a_Min;
		if (!StringToInteger(params[1], maxHeight))
		{
			CONDWARNING(a_LogWarnings, "Cannot parse maximum height from string \"%s\"!", params[1].c_str());
			return false;
		}
		if (maxHeight < a_Min)
		{
			std::swap(maxHeight, a_Min);
		}
		a_Range = maxHeight - a_Min + 1;
		return true;
	}
}





////////////////////////////////////////////////////////////////////////////////
/** A vertical strategy that places the piece at a predefined height. */
class cVerticalStrategyFixed:
	public cPiece::cVerticalStrategy
{
public:
	cVerticalStrategyFixed(void):
		m_Height(-1000)  // Default to "unassigned" height
	{
	}


	virtual int GetVerticalPlacement(int a_BlockX, int a_BlockZ) override
	{
		return m_Height;
	}


	virtual bool InitializeFromString(const AString & a_Params, bool a_LogWarnings) override
	{
		// Params: "<Height>", compulsory
		if (!StringToInteger(a_Params, m_Height))
		{
			CONDWARNING(a_LogWarnings, "Cannot parse the fixed height from string \"%s\"!", a_Params.c_str());
			return false;
		}
		return true;
	}

protected:
	/** Height at which the pieces are placed.
	Note that this height may be outside the world, so that only a part of the piece is generated. */
	int m_Height;
};





////////////////////////////////////////////////////////////////////////////////
/** A vertical strategy that places the piece in a random height between two heights. */
class cVerticalStrategyRange:
	public cPiece::cVerticalStrategy
{
public:
	cVerticalStrategyRange(void):
		m_Seed(0),
		m_Min(-1),  // Default to "unassigned" height
		m_Range(1)
	{
	}


	virtual int GetVerticalPlacement(int a_BlockX, int a_BlockZ) override
	{
		cNoise Noise(m_Seed);
		return m_Min + (Noise.IntNoise2DInt(a_BlockX, a_BlockZ) / 7) % m_Range;
	}


	virtual bool InitializeFromString(const AString & a_Params, bool a_LogWarnings) override
	{
		// Params: "<MinHeight>|<MaxHeight>", all compulsory
		auto params = StringSplitAndTrim(a_Params, "|");
		if (params.size() != 2)
		{
			CONDWARNING(a_LogWarnings, "Cannot parse the range parameters from string \"%s\"!", a_Params.c_str());
			return false;
		}
		int Max = 0;
		if (!StringToInteger(params[0], m_Min) || !StringToInteger(params[1], Max))
		{
			CONDWARNING(a_LogWarnings, "Cannot parse the minimum or maximum height from string \"%s\"!", a_Params.c_str());
			return false;
		}
		if (m_Min > Max)
		{
			std::swap(m_Min, Max);
		}
		m_Range = Max - m_Min + 1;
		return true;
	}


	virtual void AssignGens(int a_Seed, cBiomeGenPtr & a_BiomeGen, cTerrainHeightGenPtr & a_TerrainHeightGen, int a_SeaLevel) override
	{
		m_Seed = a_Seed + SEED_OFFSET;
	}

protected:
	/** Seed for the random generator. Received in AssignGens(). */
	int m_Seed;

	/** Range for the random generator. Received in InitializeFromString(). */
	int m_Min, m_Range;
};





////////////////////////////////////////////////////////////////////////////////
/** A vertical strategy that places the piece in a specified range relative to the top of the terrain. */
class cVerticalStrategyTerrainTop:
	public cPiece::cVerticalStrategy
{
public:

	virtual int GetVerticalPlacement(int a_BlockX, int a_BlockZ) override
	{
		ASSERT(m_HeightGen != nullptr);

		int ChunkX, ChunkZ;
		cChunkDef::BlockToChunk(a_BlockX, a_BlockZ, ChunkX, ChunkZ);
		cChunkDef::HeightMap HeightMap;
		m_HeightGen->GenHeightMap({ChunkX, ChunkZ}, HeightMap);
		cNoise noise(m_Seed);
		int rel = m_MinRelHeight + (noise.IntNoise2DInt(a_BlockX, a_BlockZ) / 7) % m_RelHeightRange + 1;
		return cChunkDef::GetHeight(HeightMap, a_BlockX - ChunkX * cChunkDef::Width, a_BlockZ - ChunkZ * cChunkDef::Width) + rel;
	}


	virtual bool InitializeFromString(const AString & a_Params, bool a_LogWarnings) override
	{
		// Params: "<MinRelativeHeight>|<MaxRelativeHeight>", all optional
		m_MinRelHeight = 0;
		m_RelHeightRange = 1;
		return VerticalStrategy::ParseRange(a_Params, m_MinRelHeight, m_RelHeightRange, a_LogWarnings);
	}


	virtual void AssignGens(int a_Seed, cBiomeGenPtr & a_BiomeGen, cTerrainHeightGenPtr & a_HeightGen, int a_SeaLevel) override
	{
		m_Seed = a_Seed + SEED_OFFSET;
		m_HeightGen = a_HeightGen;
	}

protected:
	/** Seed for the random generator. */
	int m_Seed;

	/** Height generator from which the top of the terrain is read. */
	cTerrainHeightGenPtr m_HeightGen;

	/** Minimum relative height at which the prefab is placed. */
	int m_MinRelHeight;

	/** Range of the relative heights at which the prefab can be placed above the minimum. */
	int m_RelHeightRange;
};





////////////////////////////////////////////////////////////////////////////////
/** A vertical strategy that places the piece within a range on top of the terrain or ocean, whichever's higher. */
class cVerticalStrategyTerrainOrOceanTop:
	public cPiece::cVerticalStrategy
{
public:

	virtual int GetVerticalPlacement(int a_BlockX, int a_BlockZ) override
	{
		ASSERT(m_HeightGen != nullptr);

		int ChunkX, ChunkZ;
		cChunkDef::BlockToChunk(a_BlockX, a_BlockZ, ChunkX, ChunkZ);
		cChunkDef::HeightMap HeightMap;
		m_HeightGen->GenHeightMap({ChunkX, ChunkZ}, HeightMap);
		int terrainHeight = static_cast<int>(cChunkDef::GetHeight(HeightMap, a_BlockX - ChunkX * cChunkDef::Width, a_BlockZ - ChunkZ * cChunkDef::Width));
		terrainHeight = std::max(1 + terrainHeight, m_SeaLevel);
		cNoise noise(m_Seed);
		int rel = m_MinRelHeight + (noise.IntNoise2DInt(a_BlockX, a_BlockZ) / 7) % m_RelHeightRange + 1;
		return terrainHeight + rel;
	}


	virtual bool InitializeFromString(const AString & a_Params, bool a_LogWarnings) override
	{
		// Params: "<MinRelativeHeight>|<MaxRelativeHeight>", all optional
		m_MinRelHeight = 0;
		m_RelHeightRange = 1;
		return VerticalStrategy::ParseRange(a_Params, m_MinRelHeight, m_RelHeightRange, a_LogWarnings);
	}


	virtual void AssignGens(int a_Seed, cBiomeGenPtr & a_BiomeGen, cTerrainHeightGenPtr & a_HeightGen, int a_SeaLevel) override
	{
		m_Seed = a_Seed + SEED_OFFSET;
		m_HeightGen = a_HeightGen;
		m_SeaLevel = a_SeaLevel;
	}

protected:
	/** Seed for the random generator. */
	int m_Seed;

	/** Height generator from which the top of the terrain is read. */
	cTerrainHeightGenPtr m_HeightGen;

	/** The sea level used by the world. */
	int m_SeaLevel;

	/** Minimum relative height at which the prefab is placed. */
	int m_MinRelHeight;

	/** Range of the relative heights at which the prefab can be placed above the minimum. */
	int m_RelHeightRange;
};





////////////////////////////////////////////////////////////////////////////////
// CreateVerticalStrategyFromString:

cPiece::cVerticalStrategyPtr CreateVerticalStrategyFromString(const AString & a_StrategyDesc, bool a_LogWarnings)
{
	// Break apart the strategy class, the first parameter before the first pipe char:
	auto idxPipe = a_StrategyDesc.find('|');
	if (idxPipe == AString::npos)
	{
		idxPipe = a_StrategyDesc.length();
	}
	AString StrategyClass = a_StrategyDesc.substr(0, idxPipe);

	// Create a strategy class based on the class string:
	cPiece::cVerticalStrategyPtr Strategy;
	if (NoCaseCompare(StrategyClass, "Fixed") == 0)
	{
		Strategy = std::make_shared<cVerticalStrategyFixed>();
	}
	else if (NoCaseCompare(StrategyClass, "Range") == 0)
	{
		Strategy = std::make_shared<cVerticalStrategyRange>();
	}
	else if (NoCaseCompare(StrategyClass, "TerrainTop") == 0)
	{
		Strategy = std::make_shared<cVerticalStrategyTerrainTop>();
	}
	else if (NoCaseCompare(StrategyClass, "TerrainOrOceanTop") == 0)
	{
		Strategy = std::make_shared<cVerticalStrategyTerrainOrOceanTop>();
	}
	else
	{
		return nullptr;
	}

	// Initialize the strategy's parameters:
	AString Params;
	if (idxPipe < a_StrategyDesc.length())
	{
		Params = a_StrategyDesc.substr(idxPipe + 1);
	}
	if (!Strategy->InitializeFromString(Params, a_LogWarnings))
	{
		return nullptr;
	}

	return Strategy;
}