summaryrefslogtreecommitdiffstats
path: root/source/Caves.cpp
blob: 1f7453389b4ef6af4c8c32bb7b92bbdbd236c837 (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

// Caves.cpp

// Implements the various cave structure generators:
//   - cStructGenWormNestCaves
//   - cStructGenMarbleCaves
//   - cStructGenNetherCaves

#include "Globals.h"
#include "Caves.h"





struct cDefPoint
{
	int m_BlockX;
	int m_BlockZ;
	int m_Radius;
	
	cDefPoint(int a_BlockX, int a_BlockZ, int a_Radius) :
		m_BlockX(a_BlockX),
		m_BlockZ(a_BlockZ),
		m_Radius(a_Radius)
	{
	}
} ;

typedef std::vector<cDefPoint> cDefPoints;





/// A single non-branching tunnel
class cCaveTunnel
{
	cDefPoints m_Points;
	
	/// Generates the shaping defpoints for the ravine, based on the ravine block coords and noise
	void GenerateBaseDefPoints(int a_BlockX, int a_BlockZ, int a_Size, cNoise & a_Noise);
	
	/// Refines (adds and smooths) defpoints from a_Src into a_Dst
	void RefineDefPoints(const cDefPoints & a_Src, cDefPoints & a_Dst);
	
	/// Does one round of smoothing, two passes of RefineDefPoints()
	void Smooth(void);
	
	/// Linearly interpolates the points so that the maximum distance between two neighbors is max 1 block
	void FinishLinear(void);
	
public:
	// Coords for which the ravine was generated (not necessarily the center)
	int m_BlockX;
	int m_BlockZ;
	
	cCaveTunnel(int a_BlockStartX, int a_BlockStartZ, int a_Size, cNoise & a_Noise);
	
	/// Carves the tunnel into the chunk specified
	void ProcessChunk(
		int a_ChunkX, int a_ChunkZ, 
		cChunkDef::BlockTypes & a_BlockTypes, 
		cChunkDef::HeightMap & a_HeightMap
	);
} ;

typedef std::vector<cCaveTunnel *> cCaveTunnels;





/// A collection of connected tunnels, possibly branching.
class cStructGenWormNestCaves::cCaveSystem
{
public:
	cCaveSystem(int a_BlockX, int a_BlockZ, int a_Size, cNoise & a_Noise);
	~cCaveSystem();
	
	/// Carves the cave system into the chunk specified
	void ProcessChunk(
		int a_ChunkX, int a_ChunkZ, 
		cChunkDef::BlockTypes & a_BlockTypes, 
		cChunkDef::HeightMap & a_HeightMap
	);
	
protected:
	int m_BlockX;
	int m_BlockZ;
	cCaveTunnels m_Tunnels;
	
	void Clear(void);
} ;





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cCaveTunnel:

void cCaveTunnel::ProcessChunk(
	int a_ChunkX, int a_ChunkZ, 
	cChunkDef::BlockTypes & a_BlockTypes, 
	cChunkDef::HeightMap & a_HeightMap
)
{
	// TODO
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cStructGenWormNestCaves::cCaveSystem:

cStructGenWormNestCaves::cCaveSystem::cCaveSystem(int a_BlockX, int a_BlockZ, int a_Size, cNoise & a_Noise) :
	m_BlockX(a_BlockX),
	m_BlockZ(a_BlockZ)
{
	// TODO: Generate a cave system
}





cStructGenWormNestCaves::cCaveSystem::~cCaveSystem()
{
	Clear();
}






void cStructGenWormNestCaves::cCaveSystem::Clear(void)
{
	// TODO
}





void cStructGenWormNestCaves::cCaveSystem::ProcessChunk(
	int a_ChunkX, int a_ChunkZ, 
	cChunkDef::BlockTypes & a_BlockTypes, 
	cChunkDef::HeightMap & a_HeightMap
)
{
	for (cCaveTunnels::const_iterator itr = m_Tunnels.begin(), end = m_Tunnels.end(); itr != end; ++itr)
	{
		(*itr)->ProcessChunk(a_ChunkX, a_ChunkZ, a_BlockTypes, a_HeightMap);
	}  // for itr - m_Tunnels[]
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cStructGenWormNestCaves:

cStructGenWormNestCaves::~cStructGenWormNestCaves()
{
	ClearCache();
}





void cStructGenWormNestCaves::ClearCache(void)
{
	for (cCaves::const_iterator itr = m_Cache.begin(), end = m_Cache.end(); itr != end; ++itr)
	{
		delete *itr;
	}  // for itr - m_Cache[]
	m_Cache.clear();
}





void cStructGenWormNestCaves::GenStructures(
	int a_ChunkX, int a_ChunkZ,
	cChunkDef::BlockTypes & a_BlockTypes,   // Block types to read and change
	cChunkDef::BlockNibbles & a_BlockMeta,  // Block meta to read and change
	cChunkDef::HeightMap & a_HeightMap,     // Height map to read and change by the current data
	cEntityList & a_Entities,               // Entities may be added or deleted
	cBlockEntityList & a_BlockEntities      // Block entities may be added or deleted
)
{
	cCaves Caves;
	GetCavesForChunk(a_ChunkX, a_ChunkZ, Caves);
	for (cCaves::const_iterator itr = Caves.begin(); itr != Caves.end(); ++itr)
	{
		(*itr)->ProcessChunk(a_ChunkX, a_ChunkZ, a_BlockTypes, a_HeightMap);
	}  // for itr - Caves[]
}





void cStructGenWormNestCaves::GetCavesForChunk(int a_ChunkX, int a_ChunkZ, cStructGenWormNestCaves::cCaves & a_Caves)
{
	// TODO: Implement proper caching
	//  - don't destroy caves that are reusable
	//  - don't create caves that are already in the cache
	
	ClearCache();
	
	int BaseX = a_ChunkX * cChunkDef::Width / m_Size;
	int BaseZ = a_ChunkZ * cChunkDef::Width / m_Size;
	if (BaseX < 0)
	{
		--BaseX;
	}
	if (BaseZ < 0)
	{
		--BaseZ;
	}
	BaseX -= 4;
	BaseZ -= 4;
	for (int x = 0; x < 8; x++)
	{
		int RealX = (BaseX + x) * m_Size;
		for (int z = 0; z < 8; z++)
		{
			int RealZ = (BaseZ + z) * m_Size;
			m_Cache.push_back(new cCaveSystem(RealX, RealZ, m_Size, m_Noise));
		}
	}
	a_Caves = m_Cache;
}






///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cStructGenMarbleCaves:

static float GetMarbleNoise( float x, float y, float z, cNoise & a_Noise )
{
	static const float PI_2 = 1.57079633f;
	float oct1 = (a_Noise.CubicNoise3D(x * 0.1f, y * 0.1f, z * 0.1f )) * 4;

	oct1 = oct1 * oct1 * oct1;
	if (oct1 < 0.f)  oct1 = PI_2;
	if (oct1 > PI_2) oct1 = PI_2;

	return oct1;
}





void cStructGenMarbleCaves::GenStructures(
	int a_ChunkX, int a_ChunkZ, 
	cChunkDef::BlockTypes & a_BlockTypes,   // Block types to read and change
	cChunkDef::BlockNibbles & a_BlockMeta,  // Block meta to read and change
	cChunkDef::HeightMap & a_HeightMap,     // Height map to read and change by the current data
	cEntityList & a_Entities,               // Entities may be added or deleted
	cBlockEntityList & a_BlockEntities      // Block entities may be added or deleted
)
{
	cNoise Noise(m_Seed);
	for (int z = 0; z < cChunkDef::Width; z++) 
	{
		const float zz = (float)(a_ChunkZ * cChunkDef::Width + z);
		for (int x = 0; x < cChunkDef::Width; x++)
		{
			const float xx = (float)(a_ChunkX * cChunkDef::Width + x);
			
			int Top = cChunkDef::GetHeight(a_HeightMap, x, z);
			for (int y = 1; y < Top; ++y )
			{
				if (cChunkDef::GetBlock(a_BlockTypes, x, y, z) != E_BLOCK_STONE)
				{
					continue;
				}

				const float yy = (float)y;
				const float WaveNoise = 1;
				if (cosf(GetMarbleNoise(xx, yy * 0.5f, zz, Noise)) * fabs(cosf(yy * 0.2f + WaveNoise * 2) * 0.75f + WaveNoise) > 0.0005f)
				{
					if (y > 4)
					{
						cChunkDef::SetBlock(a_BlockTypes, x, y, z, E_BLOCK_AIR);
					}
					else
					{
						cChunkDef::SetBlock(a_BlockTypes, x, y, z, E_BLOCK_STATIONARY_LAVA);
					}
				}
			}  // for y
		}  // for x
	}  // for z
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cStructGenDualRidgeCaves:

void cStructGenDualRidgeCaves::GenStructures(
	int a_ChunkX, int a_ChunkZ, 
	cChunkDef::BlockTypes & a_BlockTypes,   // Block types to read and change
	cChunkDef::BlockNibbles & a_BlockMeta,  // Block meta to read and change
	cChunkDef::HeightMap & a_HeightMap,     // Height map to read and change by the current data
	cEntityList & a_Entities,               // Entities may be added or deleted
	cBlockEntityList & a_BlockEntities      // Block entities may be added or deleted
)
{
	cNoise Noise1(m_Seed);
	cNoise Noise2(2 * m_Seed + 19999);
	for (int z = 0; z < cChunkDef::Width; z++) 
	{
		const float zz = (float)(a_ChunkZ * cChunkDef::Width + z) / 10;
		for (int x = 0; x < cChunkDef::Width; x++)
		{
			const float xx = (float)(a_ChunkX * cChunkDef::Width + x) / 10;
			
			int Top = cChunkDef::GetHeight(a_HeightMap, x, z);
			for (int y = 1; y <= Top; ++y)
			{
				/*
				if (cChunkDef::GetBlock(a_BlockTypes, x, y, z) != E_BLOCK_STONE)
				{
					continue;
				}
				*/

				const float yy = (float)y / 10;
				const float WaveNoise = 1;
				float n1 = Noise1.CubicNoise3D(xx, yy, zz);
				float n2 = Noise2.CubicNoise3D(xx, yy, zz);
				float n3 = Noise1.CubicNoise3D(xx * 4, yy * 4, zz * 4) / 4;
				float n4 = Noise2.CubicNoise3D(xx * 4, yy * 4, zz * 4) / 4;
				if ((abs(n1 + n3) * abs(n2 + n4)) > m_Threshold)
				{
					if (y > 10)
					{
						cChunkDef::SetBlock(a_BlockTypes, x, y, z, E_BLOCK_AIR);
					}
					else
					{
						cChunkDef::SetBlock(a_BlockTypes, x, y, z, E_BLOCK_STATIONARY_LAVA);
					}
				}
			}  // for y
		}  // for x
	}  // for z
}