summaryrefslogtreecommitdiffstats
path: root/Tools/AnvilStats/SpringStats.cpp
blob: 51b7f9d5de5d95fb1d2bcf051618d565b608357f (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

// SpringStats.cpp

// Implements the cSpringStats class representing a cCallback descendant that collects statistics on lava and water springs

#include "Globals.h"
#include "SpringStats.h"





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cSpringStats::cStats

cSpringStats::cStats::cStats(void) :
	m_TotalChunks(0)
{
	memset(m_LavaSprings,  0, sizeof(m_LavaSprings));
	memset(m_WaterSprings, 0, sizeof(m_WaterSprings));
}





void cSpringStats::cStats::Add(const cSpringStats::cStats & a_Other)
{
	m_TotalChunks += a_Other.m_TotalChunks;
	for (int Biome = 0; Biome < 256; Biome++)
	{
		for (int Height = 0; Height < 256; Height++)
		{
			m_LavaSprings[Biome][Height]  += a_Other.m_LavaSprings[Biome][Height];
			m_WaterSprings[Biome][Height] += a_Other.m_WaterSprings[Biome][Height];
		}
	}
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cSpringStats:

cSpringStats::cSpringStats(void) :
	m_AreBiomesValid(false)
{
}





bool cSpringStats::OnNewChunk(int a_ChunkX, int a_ChunkZ)
{
	memset(m_BlockTypes, 0, sizeof(m_BlockTypes));
	m_AreBiomesValid = false;
	return false;
}





bool cSpringStats::OnBiomes(const unsigned char * a_BiomeData)
{
	memcpy(m_Biomes, a_BiomeData, sizeof(m_Biomes));
	m_AreBiomesValid = true;
	return false;
}





bool cSpringStats::OnSection(
	unsigned char a_Y,
	const BLOCKTYPE * a_BlockTypes,
	const NIBBLETYPE * a_BlockAdditional,
	const NIBBLETYPE * a_BlockMeta,
	const NIBBLETYPE * a_BlockLight,
	const NIBBLETYPE * a_BlockSkyLight
)
{
	memcpy(m_BlockTypes + ((int)a_Y) * 16 * 16 * 16,     a_BlockTypes, 16 * 16 * 16);
	memcpy(m_BlockMetas + ((int)a_Y) * 16 * 16 * 16 / 2, a_BlockMeta,  16 * 16 * 16 / 2);
	return false;
}





bool cSpringStats::OnSectionsFinished(void)
{
	if (!m_AreBiomesValid)
	{
		return true;
	}
	
	// Calc the spring stats:
	for (int y = 1; y < 255; y++)
	{
		int BaseY = y * 16 * 16;
		for (int z = 1; z < 15; z++)
		{
			int Base = BaseY + z * 16;
			for (int x = 1; x < 15; x++)
			{
				if (cChunkDef::GetNibble(m_BlockMetas, x, y, z) != 0)
				{
					// Not a source block
					continue;
				}
				switch (m_BlockTypes[Base + x])
				{
					case E_BLOCK_WATER:
					case E_BLOCK_STATIONARY_WATER:
					{
						TestSpring(x, y, z, m_Stats.m_WaterSprings);
						break;
					}
					case E_BLOCK_LAVA:
					case E_BLOCK_STATIONARY_LAVA:
					{
						TestSpring(x, y, z, m_Stats.m_LavaSprings);
						break;
					}
				}  // switch (BlockType)
			}  // for x
		}  // for z
	}  // for y
	m_Stats.m_TotalChunks += 1;
	return true;
}





void cSpringStats::TestSpring(int a_RelX, int a_RelY, int a_RelZ, cSpringStats::cStats::SpringStats & a_Stats)
{
	static const struct
	{
		int x, y, z;
	} Coords[] =
	{
		{-1,  0,  0},
		{ 1,  0,  0},
		{ 0, -1,  0},
		{ 0,  1,  0},
		{ 0,  0, -1},
		{ 0,  0,  1},
	} ;
	bool HasFluidNextToIt = false;
	for (int i = 0; i < ARRAYCOUNT(Coords); i++)
	{
		switch (cChunkDef::GetBlock(m_BlockTypes, a_RelX + Coords[i].x, a_RelY + Coords[i].y, a_RelZ + Coords[i].z))
		{
			case E_BLOCK_WATER:
			case E_BLOCK_STATIONARY_WATER:
			case E_BLOCK_LAVA:
			case E_BLOCK_STATIONARY_LAVA:
			{
				if (cChunkDef::GetNibble(m_BlockMetas, a_RelX + Coords[i].x, a_RelY + Coords[i].y, a_RelZ + Coords[i].z) == 0)
				{
					// There is another source block next to this, so this is not a spring
					return;
				}
				HasFluidNextToIt = true;
			}
		}  // switch (BlockType)
	}  // for i - Coords[]
	
	if (!HasFluidNextToIt)
	{
		// Surrounded by solids on all sides, this is probably not a spring,
		// but rather a bedrocked lake or something similar. Dont want.
		return;
	}
	
	// No source blocks next to the specified block, so it is a spring. Add it to stats:
	a_Stats[a_RelY][((unsigned char *)m_Biomes)[a_RelX + 16 * a_RelZ]] += 1;
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cSpringStatsFactory:

cSpringStatsFactory::~cSpringStatsFactory()
{
	LOG("cSpringStats:");
	LOG("  Joining results...");
	JoinResults();
	LOG("  Total %llu chunks went through", m_CombinedStats.m_TotalChunks);

	// Save statistics:
	LOG("  Saving statistics into files:");
	LOG("    Springs.xls");
	SaveTotals("Springs.xls");
	LOG("    BiomeWaterSprings.xls");
	SaveStatistics(m_CombinedStats.m_WaterSprings, "BiomeWaterSprings.xls");
	LOG("    BiomeLavaSprings.xls");
	SaveStatistics(m_CombinedStats.m_LavaSprings, "BiomeLavaSprings.xls");
}





void cSpringStatsFactory::JoinResults(void)
{
	for (cCallbacks::iterator itr = m_Callbacks.begin(), end = m_Callbacks.end(); itr != end; ++itr)
	{
		m_CombinedStats.Add(((cSpringStats *)(*itr))->GetStats());
	}  // for itr - m_Callbacks[]
}





void cSpringStatsFactory::SaveTotals(const AString & a_FileName)
{
	cFile f(a_FileName, cFile::fmWrite);
	if (!f.IsOpen())
	{
		LOG("Cannot open file \"%s\" for writing!", a_FileName.c_str());
		return;
	}
	f.Printf("Height\tWater\tLava\n");
	for (int Height = 0; Height < 256; Height++)
	{
		UInt64 TotalW = 0;
		UInt64 TotalL = 0;
		for (int Biome = 0; Biome < 256; Biome++)
		{
			TotalW += m_CombinedStats.m_WaterSprings[Height][Biome];
			TotalL += m_CombinedStats.m_LavaSprings[Height][Biome];
		}
		f.Printf("%d\t%llu\t%llu\n", Height, TotalW, TotalL);
	}
	f.Printf("\n# Chunks\t%llu", m_CombinedStats.m_TotalChunks);
}





void cSpringStatsFactory::SaveStatistics(const cSpringStats::cStats::SpringStats & a_Stats, const AString & a_FileName)
{
	cFile f(a_FileName, cFile::fmWrite);
	if (!f.IsOpen())
	{
		LOG("Cannot open file \"%s\" for writing!", a_FileName.c_str());
		return;
	}
	for (int Height = 0; Height < 256; Height++)
	{
		AString Line;
		Line.reserve(2000);
		Printf(Line, "%d\t", Height);
		for (int Biome = 0; Biome < 256; Biome++)
		{
			AppendPrintf(Line, "%llu\t", a_Stats[Height][Biome]);
		}
		Line.append("\n");
		f.Write(Line.c_str(), Line.size());
	}
}