summaryrefslogtreecommitdiffstats
path: root/Tools/AnvilStats/HeightMap.cpp
blob: d00ed248f9b8f05c0e4925483c96e70b4ee142f4 (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

// HeightMap.cpp

// Implements the cHeightMap class representing a cCallback descendant that draws a B&W map of heights for the world

#include "Globals.h"
#include "HeightMap.h"




static const unsigned char g_BMPHeader[] =
{
	0x42, 0x4D, 0x36, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00,
	0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x01, 0x00, 0x20, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x13, 0x0B, 0x00, 0x00, 0x13, 0x0B, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00
} ;





cHeightMap::cHeightMap(void) :
	m_CurrentRegionX(0),
	m_CurrentRegionZ(0),
	m_IsCurrentRegionValid(false)
{
}





void cHeightMap::Finish(void)
{
	if (m_IsCurrentRegionValid)
	{
		StartNewRegion(0, 0);
	}
}





bool cHeightMap::OnNewChunk(int a_ChunkX, int a_ChunkZ)
{
	int RegionX = (a_ChunkX < 0) ? (a_ChunkX - 31) / 32 : a_ChunkX / 32;
	int RegionZ = (a_ChunkZ < 0) ? (a_ChunkZ - 31) / 32 : a_ChunkZ / 32;
	if ((RegionX != m_CurrentRegionX) || (RegionZ != m_CurrentRegionZ))
	{
		if (m_IsCurrentRegionValid)
		{
			StartNewRegion(RegionX, RegionZ);
		}
		m_CurrentRegionX = RegionX;
		m_CurrentRegionZ = RegionZ;
	}
	m_IsCurrentRegionValid = true;
	m_CurrentChunkX = a_ChunkX;
	m_CurrentChunkZ = a_ChunkZ;
	m_CurrentChunkOffX = m_CurrentChunkX - m_CurrentRegionX * 32;
	m_CurrentChunkOffZ = m_CurrentChunkZ - m_CurrentRegionZ * 32;
	memset(m_BlockTypes, 0, sizeof(m_BlockTypes));
	return false;
}





bool cHeightMap::OnHeightMap(const int * a_HeightMapBE)
{
	ASSERT(m_CurrentChunkOffX >= 0);
	ASSERT(m_CurrentChunkOffX < 32);
	ASSERT(m_CurrentChunkOffZ >= 0);
	ASSERT(m_CurrentChunkOffZ < 32);
	int * BaseHeight = m_Height + m_CurrentChunkOffZ * 16 * 512 + m_CurrentChunkOffX * 16;
	for (int z = 0; z < 16; z++)
	{
		int * Row = BaseHeight + z * 512;
		for (int x = 0; x < 16; x++)
		{
			Row[x] = ntohl(a_HeightMapBE[z * 16 + x]);
		}
	}  // for z
	return false;  // Still want blockdata to remove trees from the heightmap
}





bool cHeightMap::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
)
{
	// Copy the section data into the appropriate place in the internal buffer
	memcpy(m_BlockTypes + a_Y * 16 * 16 * 16, a_BlockTypes, 16 * 16 * 16);
	return false;
}





bool cHeightMap::OnSectionsFinished(void)
{
	// Remove trees from the heightmap:
	for (int z = 0; z < 16; z++)
	{
		for (int x = 0; x < 16; x++)
		{
			for (int y = m_Height[512 * (16 * m_CurrentChunkOffZ + z) + 16 * m_CurrentChunkOffX + x]; y >= 0; y--)
			{
				if (IsGround(m_BlockTypes[256 * y + 16 * z + x]))
				{
					m_Height[512 * (16 * m_CurrentChunkOffZ + z) + 16 * m_CurrentChunkOffX + x] = y;
					break;  // for y
				}
			}  // for y
		}  // for x
	}  // for z
	return true;
}





void cHeightMap::StartNewRegion(int a_RegionX, int a_RegionZ)
{
	AString FileName;
	Printf(FileName, "Height.%d.%d.bmp", m_CurrentRegionX, m_CurrentRegionZ);
	cFile f;
	if (!f.Open(FileName, cFile::fmWrite))
	{
		LOG("Cannot open file \"%s\" for writing the height map. Data for this region lost.", FileName.c_str());
	}
	else
	{
		f.Write(g_BMPHeader, sizeof(g_BMPHeader));
		for (int z = 0; z < 512; z++)
		{
			int RowData[512];
			int * HeightRow = m_Height + z * 512;
			for (int x = 0; x < 512; x++)
			{
				RowData[x] = std::max(std::min(HeightRow[x], 255), 0) * 0x010101;
			}
			f.Write(RowData, sizeof(RowData));
		}  // for z
	}

	memset(m_Height, 0, sizeof(m_Height));
	m_CurrentRegionX = a_RegionX;
	m_CurrentRegionZ = a_RegionZ;
}





bool cHeightMap::IsGround(BLOCKTYPE a_BlockType)
{
	// Name all blocks that are NOT ground, return false for them:
	switch (a_BlockType)
	{
		case E_BLOCK_AIR:
		case E_BLOCK_BED:
		case E_BLOCK_BEETROOTS:
		case E_BLOCK_BREWING_STAND:
		case E_BLOCK_BROWN_MUSHROOM:
		case E_BLOCK_CACTUS:
		case E_BLOCK_CAKE:
		case E_BLOCK_CARROTS:
		case E_BLOCK_CAULDRON:
		case E_BLOCK_CHEST:
		case E_BLOCK_CHORUS_FLOWER:
		case E_BLOCK_CHORUS_PLANT:
		case E_BLOCK_COBBLESTONE_WALL:
		case E_BLOCK_COBWEB:
		case E_BLOCK_COCOA_POD:
		case E_BLOCK_CROPS:
		case E_BLOCK_DEAD_BUSH:
		case E_BLOCK_DETECTOR_RAIL:
		case E_BLOCK_DIRT:
		case E_BLOCK_DRAGON_EGG:
		case E_BLOCK_END_GATEWAY:
		case E_BLOCK_END_PORTAL:
		case E_BLOCK_END_ROD:
		case E_BLOCK_ENDER_CHEST:
		case E_BLOCK_FENCE:
		case E_BLOCK_FENCE_GATE:
		case E_BLOCK_FIRE:
		case E_BLOCK_FLOWER_POT:
		case E_BLOCK_HEAD:
		case E_BLOCK_IRON_BARS:
		case E_BLOCK_LADDER:
		case E_BLOCK_LAVA:
		case E_BLOCK_LEAVES:
		case E_BLOCK_LEVER:
		case E_BLOCK_LILY_PAD:
		case E_BLOCK_LOG:  // NOTE: This block is actually solid, but we don't want it because it's the thing that trees are made of, and we're getting rid of trees
		case E_BLOCK_MELON:
		case E_BLOCK_MELON_STEM:
		case E_BLOCK_NETHER_BRICK_FENCE:
		case E_BLOCK_NETHER_PORTAL:
		case E_BLOCK_POWERED_RAIL:
		case E_BLOCK_PUMPKIN:
		case E_BLOCK_PUMPKIN_STEM:
		case E_BLOCK_RAIL:
		case E_BLOCK_RED_ROSE:
		case E_BLOCK_RED_MUSHROOM:
		case E_BLOCK_REDSTONE_REPEATER_OFF:
		case E_BLOCK_REDSTONE_REPEATER_ON:
		case E_BLOCK_REDSTONE_TORCH_OFF:
		case E_BLOCK_REDSTONE_TORCH_ON:
		case E_BLOCK_REDSTONE_WIRE:
		case E_BLOCK_REEDS:
		case E_BLOCK_SAPLING:
		case E_BLOCK_SIGN_POST:
		case E_BLOCK_SNOW:
		case E_BLOCK_STATIONARY_LAVA:
		case E_BLOCK_STATIONARY_WATER:
		case E_BLOCK_STONE_BUTTON:
		case E_BLOCK_STONE_PRESSURE_PLATE:
		case E_BLOCK_STRUCTURE_VOID:
		case E_BLOCK_TALL_GRASS:
		case E_BLOCK_TORCH:
		case E_BLOCK_TRIPWIRE:
		case E_BLOCK_TRIPWIRE_HOOK:
		case E_BLOCK_VINES:
		case E_BLOCK_WALLSIGN:
		case E_BLOCK_WATER:
		case E_BLOCK_WOODEN_BUTTON:
		case E_BLOCK_WOODEN_PRESSURE_PLATE:
		case E_BLOCK_YELLOW_FLOWER:
		{
			return false;
		}
	}
	return true;
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cHeightMapFactory:

cHeightMapFactory::~cHeightMapFactory()
{
	// Force all threads to save their last regions:
	for (cCallbacks::iterator itr = m_Callbacks.begin(), end = m_Callbacks.end(); itr != end; ++itr)
	{
		((cHeightMap *)(*itr))->Finish();
	}
	// TODO: Join all the files into one giant image file
}