summaryrefslogtreecommitdiffstats
path: root/src/Generating/PrefabStructure.cpp
blob: 9814610b470051d210e4707e299f54aae116cbaa (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

// PrefabStructure.cpp

// Implements the cPrefabStructure class representing a cGridStructGen::cStructure descendant based on placed cPrefab instances

#include "Globals.h"
#include "PrefabStructure.h"
#include "Prefab.h"





cPrefabStructure::cPrefabStructure(
	int a_GridX, int a_GridZ,
	int a_OriginX, int a_OriginZ,
	cPlacedPieces && a_Pieces,
	cTerrainHeightGen & a_HeightGen
):
	Super(a_GridX, a_GridZ, a_OriginX, a_OriginZ),
	m_Pieces(std::move(a_Pieces)),
	m_HeightGen(a_HeightGen)
{
}





void cPrefabStructure::DrawIntoChunk(cChunkDesc & a_Chunk)
{
	// Iterate over all items
	// Each intersecting prefab is placed on ground, if requested, then drawn
	for (cPlacedPieces::iterator itr = m_Pieces.begin(), end = m_Pieces.end(); itr != end; ++itr)
	{
		const cPrefab & Prefab = static_cast<const cPrefab &>((*itr)->GetPiece());
		if (Prefab.ShouldMoveToGround() && !(*itr)->HasBeenMovedToGround())
		{
			PlacePieceOnGround(**itr);
		}
		Prefab.Draw(a_Chunk, itr->get());
	}  // for itr - m_PlacedPieces[]
}





void cPrefabStructure::PlacePieceOnGround(cPlacedPiece & a_Piece)
{
	cPiece::cConnector FirstConnector = a_Piece.GetRotatedConnector(0);
	int ChunkX, ChunkZ;
	int BlockX = FirstConnector.m_Pos.x;
	int BlockZ = FirstConnector.m_Pos.z;
	int BlockY;
	cChunkDef::AbsoluteToRelative(BlockX, BlockY, BlockZ, ChunkX, ChunkZ);
	cChunkDef::HeightMap HeightMap;
	m_HeightGen.GenHeightMap({ChunkX, ChunkZ}, HeightMap);
	int TerrainHeight = cChunkDef::GetHeight(HeightMap, BlockX, BlockZ);
	a_Piece.MoveToGroundBy(TerrainHeight - FirstConnector.m_Pos.y + 1);
}