summaryrefslogtreecommitdiffstats
path: root/src/SetChunkData.cpp
blob: 707dfb9e8d1b3eefbf16aa9183416e5b7bbef860 (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

// SetChunkData.cpp

// Implements the cSetChunkData class used for sending loaded / generated chunk

#include "Globals.h"
#include "SetChunkData.h"
#include "BlockEntities/BlockEntity.h"





cSetChunkData::cSetChunkData(int a_ChunkX, int a_ChunkZ, bool a_ShouldMarkDirty) :
	m_ChunkX(a_ChunkX),
	m_ChunkZ(a_ChunkZ),
	m_IsLightValid(false),
	m_IsHeightMapValid(false),
	m_AreBiomesValid(false),
	m_ShouldMarkDirty(a_ShouldMarkDirty)
{
}





cSetChunkData::cSetChunkData(
	int a_ChunkX, int a_ChunkZ,
	const BLOCKTYPE * a_BlockTypes,
	const NIBBLETYPE * a_BlockMetas,
	const NIBBLETYPE * a_BlockLight,
	const NIBBLETYPE * a_SkyLight,
	const cChunkDef::HeightMap * a_HeightMap,
	const cChunkDef::BiomeMap * a_Biomes,
	cEntityList & a_Entities,
	cBlockEntityList & a_BlockEntities,
	bool a_ShouldMarkDirty
) :
	m_ChunkX(a_ChunkX),
	m_ChunkZ(a_ChunkZ),
	m_ShouldMarkDirty(a_ShouldMarkDirty)
{
	// Check the params' validity:
	ASSERT(a_BlockTypes != NULL);
	ASSERT(a_BlockMetas != NULL);

	// Copy block types and metas:
	memcpy(m_BlockTypes, a_BlockTypes, sizeof(cChunkDef::BlockTypes));
	memcpy(m_BlockMetas, a_BlockMetas, sizeof(cChunkDef::BlockNibbles));
	
	// Copy lights, if both given:
	if ((a_BlockLight != NULL) && (a_SkyLight != NULL))
	{
		memcpy(m_BlockLight, a_BlockLight, sizeof(m_BlockLight));
		memcpy(m_SkyLight,   a_SkyLight,   sizeof(m_SkyLight));
		m_IsLightValid = true;
	}
	else
	{
		m_IsLightValid = false;
	}
	
	// Copy the heightmap, if available:
	if (a_HeightMap != NULL)
	{
		memcpy(m_HeightMap, a_HeightMap, sizeof(m_HeightMap));
		m_IsHeightMapValid = true;
	}
	else
	{
		m_IsHeightMapValid = false;
	}
	
	// Copy biomes, if available:
	if (a_Biomes != NULL)
	{
		memcpy(m_Biomes, a_Biomes, sizeof(m_Biomes));
		m_AreBiomesValid = true;
	}
	else
	{
		m_AreBiomesValid = false;
	}
	
	// Move entities and blockentities:
	std::swap(m_Entities,      a_Entities);
	std::swap(m_BlockEntities, a_BlockEntities);
}





void cSetChunkData::CalculateHeightMap(void)
{
	for (int x = 0; x < cChunkDef::Width; x++)
	{
		for (int z = 0; z < cChunkDef::Width; z++)
		{
			for (int y = cChunkDef::Height - 1; y > -1; y--)
			{
				int index = cChunkDef::MakeIndexNoCheck(x, y, z);
				if (m_BlockTypes[index] != E_BLOCK_AIR)
				{
					m_HeightMap[x + z * cChunkDef::Width] = (HEIGHTTYPE)y;
					break;
				}
			}  // for y
		}  // for z
	}  // for x
	m_IsHeightMapValid = true;
}






void cSetChunkData::RemoveInvalidBlockEntities(void)
{
	for (cBlockEntityList::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end();)
	{
		BLOCKTYPE EntityBlockType = (*itr)->GetBlockType();
		BLOCKTYPE WorldBlockType = cChunkDef::GetBlock(m_BlockTypes, (*itr)->GetRelX(), (*itr)->GetPosY(), (*itr)->GetRelZ());
		if (EntityBlockType != WorldBlockType)
		{
			// Bad blocktype, remove the block entity:
			LOGD("Block entity blocktype mismatch at {%d, %d, %d}: entity for blocktype %s(%d) in block %s(%d). Deleting the block entity.",
				(*itr)->GetPosX(), (*itr)->GetPosY(), (*itr)->GetPosZ(),
				ItemTypeToString(EntityBlockType).c_str(), EntityBlockType,
				ItemTypeToString(WorldBlockType).c_str(),  WorldBlockType
			);
			cBlockEntityList::iterator itr2 = itr;
			itr2++;
			delete *itr;
			m_BlockEntities.erase(itr);
			itr = itr2;
		}
		else
		{
			// Good blocktype, keep the block entity:
			++itr;
		}
	}  // for itr - m_BlockEntities[]
}