summaryrefslogtreecommitdiffstats
path: root/src/Generating/POCPieceGenerator.cpp
blob: 9ed4b565ece128a6cf31bee9c8fa66bce6be4ada (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

// POCPieceGenerator.cpp

// Implements the cPOCPieceGenerator class representing a Proof-Of_Concept structure generator using the cPieceGenerator technique
// The generator generates a maze of rooms at {0, 50, 0}

#include "Globals.h"
#include "POCPieceGenerator.h"
#include "ChunkDesc.h"





/** POC pieces are simple boxes that have connectors in the middle of their walls.
Each wall has one connector, there are 3 connector types that get assigned semi-randomly.
The piece also knows how to imprint itself in a cChunkDesc, each piece has a different color glass
and each connector is uses a different color wool frame. */
class cPOCPiece :
	public cPiece
{
public:
	cPOCPiece(int a_SizeXZ, int a_Height) :
		m_SizeXZ(a_SizeXZ),
		m_Height(a_Height)
	{
		m_Connectors.push_back(cConnector(m_SizeXZ / 2, a_Height / 2, 0,            0,            BLOCK_FACE_ZM));
		m_Connectors.push_back(cConnector(m_SizeXZ / 2, a_Height / 2, m_SizeXZ - 1, 1,            BLOCK_FACE_ZP));
		m_Connectors.push_back(cConnector(0,            a_Height / 2, m_SizeXZ / 2, 2,            BLOCK_FACE_XM));
		m_Connectors.push_back(cConnector(m_SizeXZ - 1, a_Height - 1, m_SizeXZ / 2, m_SizeXZ % 3, BLOCK_FACE_XP));
	}
	
	
	/** Imprints the piece in the specified chunk. Assumes they intersect. */
	void ImprintInChunk(cChunkDesc & a_ChunkDesc, const Vector3i & a_Pos, int a_NumCCWRotations)
	{
		int BlockX = a_ChunkDesc.GetChunkX() * cChunkDef::Width;
		int BlockZ = a_ChunkDesc.GetChunkZ() * cChunkDef::Width;
		Vector3i Min = a_Pos;
		Min.Move(-BlockX, 0, -BlockZ);
		Vector3i Max = Min;
		Max.Move(m_SizeXZ - 1, m_Height - 1, m_SizeXZ - 1);
		ASSERT(Min.x < cChunkDef::Width);
		ASSERT(Min.z < cChunkDef::Width);
		ASSERT(Max.x >= 0);
		ASSERT(Max.z >= 0);
		if (Min.x >= 0)
		{
			// Draw the XM wall:
			a_ChunkDesc.FillRelCuboid(Min.x, Min.x, Min.y, Max.y, Min.z, Max.z, E_BLOCK_STAINED_GLASS, m_SizeXZ % 16);
		}
		if (Min.z >= 0)
		{
			// Draw the ZM wall:
			a_ChunkDesc.FillRelCuboid(Min.x, Max.x, Min.y, Max.y, Min.z, Min.z, E_BLOCK_STAINED_GLASS, m_SizeXZ % 16);
		}
		if (Max.x < cChunkDef::Width)
		{
			// Draw the XP wall:
			a_ChunkDesc.FillRelCuboid(Max.x, Max.x, Min.y, Max.y, Min.z, Max.z, E_BLOCK_STAINED_GLASS, m_SizeXZ % 16);
		}
		if (Max.z < cChunkDef::Width)
		{
			// Draw the ZP wall:
			a_ChunkDesc.FillRelCuboid(Min.x, Max.x, Min.y, Max.y, Max.z, Max.z, E_BLOCK_STAINED_GLASS, m_SizeXZ % 16);
		}
		
		// Draw all the connectors:
		for (cConnectors::const_iterator itr = m_Connectors.begin(), end = m_Connectors.end(); itr != end; ++itr)
		{
			cConnector Conn = cPiece::RotateMoveConnector(*itr, a_NumCCWRotations, a_Pos.x, a_Pos.y, a_Pos.z);
			Conn.m_Pos.Move(-BlockX, 0, -BlockZ);
			if (
				(Conn.m_Pos.x >= 0) && (Conn.m_Pos.x < cChunkDef::Width) &&
				(Conn.m_Pos.z >= 0) && (Conn.m_Pos.z < cChunkDef::Width)
			)
			{
				a_ChunkDesc.SetBlockTypeMeta(Conn.m_Pos.x, Conn.m_Pos.y, Conn.m_Pos.z, E_BLOCK_WOOL, itr->m_Type % 16);
			}
			
			/*
			// TODO: Frame the connectors
			switch (itr->m_Direction)
			{
				case BLOCK_FACE_XM:
				case BLOCK_FACE_XP:
				{
					// TODO
					break;
				}
				
				case BLOCK_FACE_ZM:
				case BLOCK_FACE_ZP:
				{
					// TODO
					break;
				}
			}
			*/
		}  // for itr - m_Connectors[]
	}
	
protected:
	int m_SizeXZ;
	int m_Height;
	cConnectors m_Connectors;
	
	// cPiece overrides:
	virtual cConnectors GetConnectors(void) const override
	{
		return m_Connectors;
	}
	
	virtual Vector3i GetSize(void) const override
	{
		return Vector3i(m_SizeXZ, m_Height, m_SizeXZ);
	}
	
	virtual cCuboid GetHitBox(void) const override
	{
		return cCuboid(0, 0, 0, m_SizeXZ - 1, m_Height - 1, m_SizeXZ - 1);
	}
	
	virtual bool CanRotateCCW(int a_NumRotations) const override
	{
		return true;
	}
};





/*
static void DebugPieces(const cPlacedPieces & a_Pieces)
{
	size_t idx = 0;
	for (cPlacedPieces::const_iterator itr = a_Pieces.begin(), end = a_Pieces.end(); itr != end; ++itr, ++idx)
	{
		const cCuboid & HitBox = (*itr)->GetHitBox();
		printf("  %u: %d rotations, {%d - %d, %d - %d}\n",
			idx, (*itr)->GetNumCCWRotations(),
			HitBox.p1.x, HitBox.p2.x, HitBox.p1.z, HitBox.p2.z
		);
	}  // for itr - a_Pieces[]
}
//*/





///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cPOCPieceGenerator:

cPOCPieceGenerator::cPOCPieceGenerator(int a_Seed) :
	m_Seed(a_Seed)
{
	// Prepare a vector of available pieces:
	m_AvailPieces.push_back(new cPOCPiece(5, 3));
	m_AvailPieces.push_back(new cPOCPiece(7, 5));
	m_AvailPieces.push_back(new cPOCPiece(9, 5));
	m_AvailPieces.push_back(new cPOCPiece(5, 7));
	
	// Generate the structure:
	cBFSPieceGenerator Gen(*this, a_Seed);
	Gen.PlacePieces(0, 50, 0, 6, m_Pieces);
	
	// DebugPieces(m_Pieces);
	
	// Get the smallest cuboid encompassing the entire generated structure:
	cCuboid Bounds(0, 50, 0, 0, 50, 0);
	for (cPlacedPieces::const_iterator itr = m_Pieces.begin(), end = m_Pieces.end(); itr != end; ++itr)
	{
		Vector3i MinCoords = (*itr)->GetCoords();
		Bounds.Engulf(MinCoords);
		Bounds.Engulf(MinCoords + (*itr)->GetPiece().GetSize());
	}  // for itr - m_Pieces[]
	m_Bounds = Bounds;
}





cPOCPieceGenerator::~cPOCPieceGenerator()
{
	cPieceGenerator::FreePieces(m_Pieces);
}





void cPOCPieceGenerator::GenFinish(cChunkDesc & a_ChunkDesc)
{
	int BlockX = a_ChunkDesc.GetChunkX() * cChunkDef::Width;
	int BlockZ = a_ChunkDesc.GetChunkZ() * cChunkDef::Width;
	if (
		(BlockX + 16 < m_Bounds.p1.x) || (BlockX > m_Bounds.p2.x) ||  // X coords out of bounds of the generated structure
		(BlockZ + 16 < m_Bounds.p1.z) || (BlockZ > m_Bounds.p2.z)     // Z coords out of bounds of the generated structure
	)
	{
		return;
	}
	
	// Imprint each piece in the chunk:
	for (cPlacedPieces::const_iterator itr = m_Pieces.begin(), end = m_Pieces.end(); itr != end; ++itr)
	{
		const Vector3i & Pos = (*itr)->GetCoords();
		Vector3i Size = (*itr)->GetPiece().GetSize();
		if (((*itr)->GetNumCCWRotations() % 2) == 1)
		{
			std::swap(Size.x, Size.z);
		}
		if (
			(Pos.x >= BlockX + 16) || (Pos.x + Size.x - 1 < BlockX) ||
			(Pos.z >= BlockZ + 16) || (Pos.z + Size.z - 1 < BlockZ)
		)
		{
			// This piece doesn't intersect the chunk
			continue;
		}
			
		((cPOCPiece &)(*itr)->GetPiece()).ImprintInChunk(a_ChunkDesc, Pos, (*itr)->GetNumCCWRotations());
	}  // for itr - m_Pieces[]
	a_ChunkDesc.UpdateHeightmap();
}





cPieces cPOCPieceGenerator::GetPiecesWithConnector(int a_ConnectorType)
{
	// Each piece has each connector
	return m_AvailPieces;
}





cPieces cPOCPieceGenerator::GetStartingPieces(void)
{
	// Any piece can be a starting piece
	return m_AvailPieces;
}





void cPOCPieceGenerator::PiecePlaced(const cPiece & a_Piece)
{
	UNUSED(a_Piece);
}





void cPOCPieceGenerator::Reset(void)
{
	// Nothing needed
}