2014-07-24 12:32:05 -04:00
|
|
|
|
|
|
|
// SetChunkData.cpp
|
|
|
|
|
|
|
|
// Implements the cSetChunkData class used for sending loaded / generated chunk
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "SetChunkData.h"
|
2014-08-29 12:19:27 -04:00
|
|
|
#include "BlockEntities/BlockEntity.h"
|
2014-07-24 12:32:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cSetChunkData::cSetChunkData(int a_ChunkX, int a_ChunkZ, bool a_ShouldMarkDirty) :
|
|
|
|
m_ChunkX(a_ChunkX),
|
|
|
|
m_ChunkZ(a_ChunkZ),
|
2014-08-21 15:58:08 -04:00
|
|
|
m_IsLightValid(false),
|
|
|
|
m_IsHeightMapValid(false),
|
|
|
|
m_AreBiomesValid(false),
|
2014-07-24 12:32:05 -04:00
|
|
|
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,
|
2015-04-27 15:18:21 -04:00
|
|
|
cEntityList && a_Entities,
|
|
|
|
cBlockEntityList && a_BlockEntities,
|
2014-07-24 12:32:05 -04:00
|
|
|
bool a_ShouldMarkDirty
|
|
|
|
) :
|
|
|
|
m_ChunkX(a_ChunkX),
|
|
|
|
m_ChunkZ(a_ChunkZ),
|
|
|
|
m_ShouldMarkDirty(a_ShouldMarkDirty)
|
|
|
|
{
|
|
|
|
// Check the params' validity:
|
2014-10-20 16:55:07 -04:00
|
|
|
ASSERT(a_BlockTypes != nullptr);
|
|
|
|
ASSERT(a_BlockMetas != nullptr);
|
2014-07-24 12:32:05 -04:00
|
|
|
|
|
|
|
// 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:
|
2014-10-20 16:55:07 -04:00
|
|
|
if ((a_BlockLight != nullptr) && (a_SkyLight != nullptr))
|
2014-07-24 12:32:05 -04:00
|
|
|
{
|
|
|
|
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:
|
2014-10-20 16:55:07 -04:00
|
|
|
if (a_HeightMap != nullptr)
|
2014-07-24 12:32:05 -04:00
|
|
|
{
|
|
|
|
memcpy(m_HeightMap, a_HeightMap, sizeof(m_HeightMap));
|
|
|
|
m_IsHeightMapValid = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_IsHeightMapValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy biomes, if available:
|
2014-10-20 16:55:07 -04:00
|
|
|
if (a_Biomes != nullptr)
|
2014-07-24 12:32:05 -04:00
|
|
|
{
|
|
|
|
memcpy(m_Biomes, a_Biomes, sizeof(m_Biomes));
|
|
|
|
m_AreBiomesValid = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_AreBiomesValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move entities and blockentities:
|
2015-04-27 15:18:21 -04:00
|
|
|
m_Entities = std::move(a_Entities);
|
|
|
|
m_BlockEntities = std::move(a_BlockEntities);
|
2014-07-24 12:32:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2015-04-27 20:51:21 -04:00
|
|
|
m_HeightMap[x + z * cChunkDef::Width] = static_cast<HEIGHTTYPE>(y);
|
2014-07-24 12:32:05 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // for y
|
|
|
|
} // for z
|
|
|
|
} // for x
|
|
|
|
m_IsHeightMapValid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-29 12:19:27 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
);
|
2015-05-05 17:08:40 -04:00
|
|
|
cBlockEntityList::iterator itr2 = itr;
|
|
|
|
++itr2;
|
2014-08-29 12:19:27 -04:00
|
|
|
delete *itr;
|
2015-05-05 17:08:40 -04:00
|
|
|
m_BlockEntities.erase(itr);
|
|
|
|
itr = itr2;
|
2014-08-29 12:19:27 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Good blocktype, keep the block entity:
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
} // for itr - m_BlockEntities[]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|