summaryrefslogtreecommitdiffstats
path: root/src/Generating/PieceModifier.cpp
blob: 54d6a2c21363d5b1446e9a5d1df6abb13a41f629 (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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402

// PieceModifier.cpp

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

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





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





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





////////////////////////////////////////////////////////////////////////////////
/** A modifier which is pseudo-randomly replacing blocks to other types and metas. */
class cPieceModifierRandomizeBlocks:
	public cPiece::cPieceModifier
{
public:
	cPieceModifierRandomizeBlocks(void) :
		m_Seed()
	{
	}

	virtual bool InitializeFromString(const AString & a_Params, bool a_LogWarnings) override
	{
		m_AllWeights = 0;
		AString Params = a_Params;


		/** BlocksToReplace parsing */
		auto idxPipe = Params.find('|');
		if (idxPipe != AString::npos)
		{
			AString blocksToReplaceStr = Params.substr(0, idxPipe);
			auto blocksToReplace = StringSplitAndTrim(blocksToReplaceStr, ",");
			for (size_t i = 0; i < blocksToReplace.size(); i++)
			{
				BLOCKTYPE blockType = static_cast<BLOCKTYPE>(BlockStringToType(blocksToReplace[i]));
				if ((blockType == E_BLOCK_AIR) && !NoCaseCompare(blocksToReplace[i], "Air"))
				{
					CONDWARNING(a_LogWarnings, "Cannot parse block type from string \"%s\"!", blocksToReplace[i].c_str());
					return false;
				}
				m_BlocksToReplace[blockType] = 1;
			}

			Params = Params.substr(idxPipe + 1, Params.length() - 1);
		}
		if (m_BlocksToReplace.size() == 0)
		{
			CONDWARNING(a_LogWarnings, "You must specify at least one block to replace%s!", "");
			return false;
		}


		/** Meta params parsing */
		auto idxSqBracketStart = Params.find('[');
		auto idxSqBracketStartLast = Params.find_last_of('[');

		bool isMultiMeta = false;
		if ((idxSqBracketStart != idxSqBracketStartLast) && (idxSqBracketStartLast != Params.length() - 1))
		{
			// Meta per block type
			isMultiMeta = true;
		}
		else
		{
			auto idxSqBracketEnd = Params.find(']');
			if ((idxSqBracketEnd == Params.length() - 1) && (idxSqBracketStart != AString::npos))
			{
				AString metaParamsStr = Params.substr(idxSqBracketStart + 1, Params.length() - idxSqBracketStart - 2);
				std::array<int, 4> metaParamsInt;
				if (!ParseMeta(metaParamsStr, metaParamsInt, a_LogWarnings))
				{
					return false;
				}

				m_MinMeta = metaParamsInt[0];
				m_MaxMeta = metaParamsInt[1];
				m_MinNoiseMeta = metaParamsInt[2];
				m_MaxNoiseMeta = metaParamsInt[3];

				Params = Params.substr(0, idxSqBracketStart);
			}
		}


		// BlocksToRandomize parsing
		auto BlocksToRandomize = StringSplitAndTrim(Params, ";");
		for (size_t i = 0; i < BlocksToRandomize.size(); i++)
		{
			AString block = BlocksToRandomize[i];

			cRandomizedBlock Block{};

			if (isMultiMeta)
			{
				auto sqBrStart = block.find('[');
				if (sqBrStart != AString::npos)
				{
					auto sqBrEnd = block.find(']');
					if (sqBrEnd != block.size() - 1)
					{
						CONDWARNING(a_LogWarnings, "If present, block meta params must be at the end of block to randomize definition \"%s\"!", block.c_str());
						return false;

					}
					AString metaParamsStr = block.substr(sqBrStart + 1, block.size() - sqBrStart - 2);

					std::array<int, 4> metaParamsInt;
					if (!ParseMeta(metaParamsStr, metaParamsInt, a_LogWarnings))
					{
						return false;
					}

					Block.m_MinMeta = metaParamsInt[0];
					Block.m_MaxMeta = metaParamsInt[1];
					Block.m_MinNoiseMeta = metaParamsInt[2];
					Block.m_MaxNoiseMeta = metaParamsInt[3];

					block = block.substr(0, sqBrStart);

				}
				// No meta randomization for this block
			}

			auto BlockParams = StringSplitAndTrim(block, ",");
			if (BlockParams.size() < 2)
			{
				CONDWARNING(a_LogWarnings, "Block weight is required param \"%s\"!", BlockParams[0].c_str());
				return false;
			}

			BLOCKTYPE BlockType = static_cast<BLOCKTYPE>(BlockStringToType(BlockParams[0]));
			int BlockWeight = 0;
			if ((BlockType != E_BLOCK_AIR) && !NoCaseCompare(BlockParams[0], "Air"))
			{
				// Failed to parse block type
				CONDWARNING(
					a_LogWarnings, "Cannot parse block type from string \"%s\"!",
					BlockParams[0].c_str());
				return false;
			}

			if (!StringToInteger(BlockParams[1], BlockWeight))
			{
				// Failed to parse the crop weight:
				CONDWARNING(
					a_LogWarnings,
					"Cannot parse block weight from string \"%s\"!",
					BlockParams[1].c_str());
				return false;
			}


			Block.m_Type = BlockType;
			Block.m_Weight = BlockWeight;
			m_AllWeights += BlockWeight;

			m_BlocksToRandomize.push_back(Block);
		}
		if (m_BlocksToRandomize.size() == 0)
		{
			CONDWARNING(a_LogWarnings, "You must specify at least one block to randomize%s!", "");
			return false;
		}

		return true;
	}





	bool ParseMeta(const AString & a_Meta, std::array<int, 4> & a_ParsedMeta, bool a_LogWarnings)
	{
		auto MetaParams = StringSplitAndTrim(a_Meta, ",");

		for (size_t i = 0; i < MetaParams.size(); i++)
		{
			int Value;
			if (!StringToInteger(MetaParams[i], Value))
			{
				// Failed to parse meta parameter from string:
				CONDWARNING(a_LogWarnings, "Cannot parse meta param from string \"%s\", meta: %s!", MetaParams[i].c_str(), a_Meta.c_str());
				return false;
			}

			if (i > 3)
			{
				CONDWARNING(a_LogWarnings, "Unsupported meta param \"%d\"!", Value);
				return false;
			}

			a_ParsedMeta[i] = Value;
		}

		if (MetaParams.size() == 1)
		{
			// Noise is not used for meta
			a_ParsedMeta[1] = a_ParsedMeta[0];
		}
		else if (MetaParams.size() == 2)
		{
			// Noise range is same as meta range
			a_ParsedMeta[2] = a_ParsedMeta[0];
			a_ParsedMeta[3] = a_ParsedMeta[1];
		}
		else if (MetaParams.size() == 3)
		{
			a_ParsedMeta[3] = a_ParsedMeta[1];
		}

		return true;
	}





	virtual void Modify(cBlockArea & a_Image, const Vector3i a_PiecePos, const int a_PieceRot) override
	{
		cNoise Noise(m_Seed);
		cNoise PieceNoise(Noise.IntNoise3DInt(a_PiecePos));

		size_t NumBlocks = a_Image.GetBlockCount();
		BLOCKTYPE * BlockTypes = a_Image.GetBlockTypes();
		BLOCKTYPE * BlockMetas = a_Image.GetBlockMetas();

		for (size_t i = 0; i < NumBlocks; i++)
		{
			if (m_BlocksToReplace.count(BlockTypes[i]))
			{
				float BlockRnd = PieceNoise.IntNoise2DInRange(a_PieceRot, static_cast<int>(i), 0, m_AllWeights);

				int weightDelta = 0;
				for (auto & blockToRnd : m_BlocksToRandomize)
				{
					weightDelta += blockToRnd.m_Weight;
					if (BlockRnd <= weightDelta)
					{
						BlockTypes[i] = blockToRnd.m_Type;

						// Per block meta params
						if (blockToRnd.m_MinMeta < blockToRnd.m_MaxMeta)
						{
							int BlockMetaRnd = std::clamp(static_cast<int>(PieceNoise.IntNoise2DInRange(a_PieceRot*2, static_cast<int>(i), blockToRnd.m_MinNoiseMeta, blockToRnd.m_MaxNoiseMeta)), blockToRnd.m_MinMeta, blockToRnd.m_MaxMeta);
							BlockMetas[i] = static_cast<NIBBLETYPE>(BlockMetaRnd);
						}
						else if ((blockToRnd.m_MaxMeta > -1) && (blockToRnd.m_MaxMeta == blockToRnd.m_MinMeta))
						{
							// Change meta if at least minimum meta was specified
							BlockMetas[i] = static_cast<NIBBLETYPE>(blockToRnd.m_MaxMeta);
						}
						break;
					}
				}

				// All blocks meta params
				if (m_MaxMeta > m_MinMeta)
				{
					int BlockMetaRnd = std::clamp(static_cast<int>(PieceNoise.IntNoise2DInRange(a_PieceRot*2, static_cast<int>(i), m_MinNoiseMeta, m_MaxNoiseMeta)), m_MinMeta, m_MaxMeta);
					BlockMetas[i] = static_cast<NIBBLETYPE>(BlockMetaRnd);
				}
				else if ((m_MaxMeta > -1) && (m_MaxMeta == m_MinMeta))
				{
					// Change meta if at least minimum meta was specified
					BlockMetas[i] = static_cast<NIBBLETYPE>(m_MaxMeta);
				}
			}
		}  // for i - BlockTypes[]
	}

	virtual void AssignSeed(int a_Seed) override
	{
		m_Seed = a_Seed + SEED_OFFSET;
	}
protected:
	int m_Seed;
	int m_AllWeights = 0;


	/** Block types of a blocks which are being replaced by this strategy */
	std::map<BLOCKTYPE, int> m_BlocksToReplace;

	/** Randomized blocks with their weights and meta params */
	cRandomizedBlocks m_BlocksToRandomize;

	/** Minimum meta to randomize */
	int m_MinMeta = 0;

	/** Maximum meta to randomize */
	int m_MaxMeta = -1;

	/** Maximum meta in noise range */
	int m_MaxNoiseMeta = 0;

	/** Minimum meta in noise range */
	int m_MinNoiseMeta = 0;
};





////////////////////////////////////////////////////////////////////////////////
// CreatePieceModifierFromString:

bool CreatePieceModifierFromString(const AString & a_Definition, std::shared_ptr<cPiece::cPieceModifiers> & a_Modifiers, bool a_LogWarnings)
{

	auto idxCurlyStart = a_Definition.find('{');
	auto idxCurlyFirstEnd = a_Definition.find('}');
	if ((idxCurlyStart == AString::npos) && (idxCurlyFirstEnd == AString::npos))
	{
		CONDWARNING(a_LogWarnings, "Piece metadata \"Modifiers\" needs at least one valid modifier definition \"%s\"!", a_Definition.c_str());
		return false;
	}

	auto modifiersStr = StringSplitAndTrim(a_Definition, "{");

	for (size_t i = 0; i < modifiersStr.size(); i++)
	{
		AString modifierStr = TrimString(modifiersStr[i]);

		if (modifierStr.size() == 0)
		{
			continue;
		}
		auto idxCurlyEnd = modifierStr.find('}');
		if (idxCurlyEnd == AString::npos)
		{
			CONDWARNING(a_LogWarnings, "Modifier definition must end with curly bracket \"%s\"!!", modifierStr.c_str());
			return false;
		}

		modifierStr = modifierStr.substr(0, idxCurlyEnd);

		// Break apart the modifier class, the first parameter before the first pipe char:
		auto idxPipe = modifierStr.find('|');
		if (idxPipe == AString::npos)
		{
			idxPipe = modifierStr.length();
		}
		AString ModifierClass = modifierStr.substr(0, idxPipe);

		// Create a modifier class based on the class string:
		cPiece::cPieceModifierPtr Modifier;
		if (NoCaseCompare(ModifierClass, "RandomizeBlocks") == 0)
		{
			Modifier = std::make_shared<cPieceModifierRandomizeBlocks>();
		}

		if (Modifier == nullptr)
		{
			CONDWARNING(a_LogWarnings, "Unknown modifier class \"%s\" %s!", ModifierClass.c_str(), modifierStr.c_str());
			return false;
		}

		// Initialize the modifier's parameters:
		AString Params;
		if (idxPipe < modifierStr.length())
		{
			Params = modifierStr.substr(idxPipe + 1);
		}

		if (!Modifier->InitializeFromString(Params, a_LogWarnings))
		{
			CONDWARNING(a_LogWarnings, "InitializeFromString error \"%s\" -- %!", Params.c_str(), modifierStr.c_str());
			return false;
		}

		a_Modifiers->push_back(Modifier);
	}

	return true;
}