summaryrefslogtreecommitdiffstats
path: root/Tools/BlockZapper/Regions.cpp
blob: 515699532e93cd5532eec37f40dad65b40406943 (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

// Regions.cpp

// Implements the cRegions class representing the list of regions to zap

#include "Globals.h"

#include "Regions.h"





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cRegion:

cRegion::cRegion(void) :
	m_MinX(0),
	m_MaxX(0),
	m_MinY(0),
	m_MaxY(0),
	m_MinZ(0),
	m_MaxZ(0),
	m_ShouldZapBlocks(false),
	m_ShouldZapEntities(false)
{
}





cRegion::cRegion(int a_MinX, int a_MaxX, int a_MinY, int a_MaxY, int a_MinZ, int a_MaxZ, bool a_ShouldZapBlocks, bool a_ShouldZapEntities) :
	m_MinX(a_MinX),
	m_MaxX(a_MaxX),
	m_MinY(std::max(0, std::min(255, a_MinY))),
	m_MaxY(std::max(0, std::min(255, a_MaxY))),
	m_MinZ(a_MinZ),
	m_MaxZ(a_MaxZ),
	m_ShouldZapBlocks(a_ShouldZapBlocks),
	m_ShouldZapEntities(a_ShouldZapEntities)
{
}





bool cRegion::TouchesChunk(int a_ChunkX, int a_ChunkZ) const
{
	int ChunkBeginX = a_ChunkX * 16;
	int ChunkEndX = a_ChunkX * 16 + 15;
	int ChunkBeginZ = a_ChunkZ * 16;
	int ChunkEndZ = a_ChunkZ * 16 + 15;
	if (
		(m_MinX > ChunkEndX) || (m_MaxX < ChunkBeginX) ||
		(m_MinZ > ChunkEndZ) || (m_MaxZ < ChunkBeginZ)
	)
	{
		return false;
	}
	return true;
}





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cRegions:

void cRegions::Read(std::istream & a_Stream)
{
	while (!a_Stream.eof())
	{
		AString Line;
		std::getline(a_Stream, Line);
		
		// Process the line
		AStringVector Split = StringSplit(Line, " \t");
		AStringVector NonEmpty;
		for (AStringVector::const_iterator itr = Split.begin(), end = Split.end(); itr != end; ++itr)
		{
			if (!itr->empty())
			{
				NonEmpty.push_back(*itr);
			}
		}  // for itr - Split[]
		switch (NonEmpty.size())
		{
			case 6:
			case 7:
			{
				AddRegion(NonEmpty);
				break;
			}
			default:
			{
				fprintf(stderr, "Cannot parse line \"%s\", ignoring", Line.c_str());
				break;
			}
		}
	}
}





void cRegions::AddRegion(const AStringVector & a_Split)
{
	ASSERT((a_Split.size() == 6) || (a_Split.size() == 7));
	
	int Coords[6];
	for (int i = 0; i < 6; i++)
	{
		Coords[i] = atoi(a_Split[i].c_str());
		if ((Coords[i] == 0) && (a_Split[i] != "0"))
		{
			fprintf(stderr, "Bad coord: \"%s\". Ignoring line.", a_Split[i].c_str());
			return;
		}
	}  // for i - a_Split[]
	
	bool ShouldZapBlocks = true;
	bool ShouldZapEntities = false;
	
	if (a_Split.size() == 7)
	{
		AString Upper = a_Split[6];
		StrToUpper(Upper);
		if (Upper == "E")
		{
			ShouldZapEntities = true;
			ShouldZapBlocks = false;
		}
		else if (Upper == "BE")
		{
			ShouldZapEntities = true;
		}
		else if (Upper == "B")
		{
			// Nothing needed
		}
		else
		{
			fprintf(stderr, "Bad zap specifier: \"%s\". Ignoring line.", a_Split[6].c_str());
			return;
		}
	}
	
	// Swap coords, if needed:
	for (int i = 0; i < 3; i++)
	{
		if (Coords[2 * i] > Coords[2 * i + 1])
		{
			std::swap(Coords[2 * i], Coords[2 * i + 1]);
		}
	}
	
	// Store the region
	m_Regions.push_back(cRegion(Coords[0], Coords[1], Coords[2], Coords[3], Coords[4], Coords[5], ShouldZapBlocks, ShouldZapEntities));
}