1
0
cuberite-2a/src/ChunkData.cpp

731 lines
16 KiB
C++
Raw Normal View History

2014-05-29 12:25:08 -04:00
// ChunkData.cpp
// Implements the cChunkData class that represents the block's type, meta, blocklight and skylight storage for a chunk
#include "Globals.h"
2014-05-21 14:58:48 -04:00
#include "ChunkData.h"
#include "BlockType.h"
2014-05-29 12:25:08 -04:00
namespace
2014-05-29 12:25:08 -04:00
{
/** Returns true if all a_Array's elements between [0] and [a_NumElements - 1] are equal to a_Value. */
template <typename T>
bool IsAllValue(const T * a_Array, size_t a_NumElements, T a_Value)
2014-05-29 12:25:08 -04:00
{
for (size_t i = 0; i < a_NumElements; i++)
2014-05-29 12:25:08 -04:00
{
if (a_Array[i] != a_Value)
{
return false;
}
2014-05-29 12:25:08 -04:00
}
return true;
2014-05-29 12:25:08 -04:00
}
struct sSectionIndices
{
int Section = 0; // Index into m_Sections
int Index = 0; // Index into a single sChunkSection
};
sSectionIndices IndicesFromRelPos(Vector3i a_RelPos)
{
ASSERT(cChunkDef::IsValidRelPos(a_RelPos));
sSectionIndices Ret;
Ret.Section = a_RelPos.y / cChunkData::SectionHeight;
Ret.Index = cChunkDef::MakeIndexNoCheck(a_RelPos.x, a_RelPos.y % cChunkData::SectionHeight, a_RelPos.z);
return Ret;
}
} // namespace (anonymous)
2014-05-29 12:25:08 -04:00
cChunkData::cChunkData(cAllocationPool<cChunkData::sChunkSection> & a_Pool):
m_Sections(),
m_Pool(a_Pool)
2014-05-24 08:37:25 -04:00
{
}
2014-05-28 05:35:55 -04:00
cChunkData::cChunkData(cChunkData && a_Other):
m_Pool(a_Other.m_Pool)
2014-05-24 08:37:25 -04:00
{
2014-05-29 12:25:08 -04:00
for (size_t i = 0; i < NumSections; i++)
2014-05-24 08:37:25 -04:00
{
m_Sections[i] = a_Other.m_Sections[i];
a_Other.m_Sections[i] = nullptr;
2014-05-24 08:37:25 -04:00
}
}
2014-05-28 05:35:55 -04:00
cChunkData::~cChunkData()
{
Clear();
}
2014-05-24 08:37:25 -04:00
2014-05-28 05:35:55 -04:00
void cChunkData::Assign(const cChunkData & a_Other)
{
// If assigning to self, no-op
if (&a_Other == this)
2014-05-24 08:37:25 -04:00
{
return;
2014-05-24 08:37:25 -04:00
}
2016-02-05 16:45:45 -05:00
Clear();
for (size_t i = 0; i < NumSections; ++i)
2014-05-24 08:37:25 -04:00
{
if (a_Other.m_Sections[i] != nullptr)
2014-05-24 08:37:25 -04:00
{
m_Sections[i] = Allocate();
*m_Sections[i] = *a_Other.m_Sections[i];
2014-05-24 08:37:25 -04:00
}
}
}
2016-02-05 16:45:45 -05:00
2014-05-24 08:37:25 -04:00
void cChunkData::Assign(cChunkData && a_Other)
{
if (&a_Other == this)
2014-05-24 08:37:25 -04:00
{
return;
2014-05-24 08:37:25 -04:00
}
if (m_Pool != a_Other.m_Pool)
{
// Cannot transfer the memory, do a copy instead
const cChunkData & CopyOther = a_Other;
Assign(CopyOther);
return;
}
Clear();
for (size_t i = 0; i < NumSections; i++)
{
m_Sections[i] = a_Other.m_Sections[i];
a_Other.m_Sections[i] = nullptr;
}
}
2014-05-24 08:37:25 -04:00
2014-05-28 05:35:55 -04:00
BLOCKTYPE cChunkData::GetBlock(Vector3i a_RelPos) const
2014-05-24 08:37:25 -04:00
{
if (!cChunkDef::IsValidRelPos(a_RelPos))
{
return E_BLOCK_AIR; // Coordinates are outside outside the world, so this must be an air block
}
auto Idxs = IndicesFromRelPos(a_RelPos);
if (m_Sections[Idxs.Section] != nullptr)
2014-05-24 08:37:25 -04:00
{
return m_Sections[Idxs.Section]->m_BlockTypes[Idxs.Index];
2014-05-24 08:37:25 -04:00
}
else
{
return 0;
}
}
2014-05-28 05:35:55 -04:00
void cChunkData::SetBlock(Vector3i a_RelPos, BLOCKTYPE a_Block)
2014-05-24 08:37:25 -04:00
{
if (!cChunkDef::IsValidRelPos(a_RelPos))
2014-05-24 08:37:25 -04:00
{
ASSERT(!"cChunkData::SetMeta(): index out of range!");
return;
}
auto Idxs = IndicesFromRelPos(a_RelPos);
if (m_Sections[Idxs.Section] == nullptr)
2014-05-24 08:37:25 -04:00
{
if (a_Block == 0x00)
{
return;
}
m_Sections[Idxs.Section] = Allocate();
if (m_Sections[Idxs.Section] == nullptr)
2014-05-24 08:37:25 -04:00
{
ASSERT(!"Failed to allocate a new section in Chunkbuffer");
return;
}
ZeroSection(m_Sections[Idxs.Section]);
2014-05-24 08:37:25 -04:00
}
m_Sections[Idxs.Section]->m_BlockTypes[Idxs.Index] = a_Block;
2014-05-24 08:37:25 -04:00
}
2014-05-28 05:35:55 -04:00
NIBBLETYPE cChunkData::GetMeta(Vector3i a_RelPos) const
2014-05-24 08:37:25 -04:00
{
if (cChunkDef::IsValidRelPos(a_RelPos))
2014-05-24 08:37:25 -04:00
{
auto Idxs = IndicesFromRelPos(a_RelPos);
if (m_Sections[Idxs.Section] != nullptr)
2014-05-24 08:37:25 -04:00
{
return (m_Sections[Idxs.Section]->m_BlockMetas[Idxs.Index / 2] >> ((Idxs.Index & 1) * 4)) & 0x0f;
2014-05-24 08:37:25 -04:00
}
else
{
return 0;
}
}
// Coordinates are outside outside the world, so it must be an air block with a blank meta
2014-05-24 08:37:25 -04:00
return 0;
}
2014-05-28 05:35:55 -04:00
bool cChunkData::SetMeta(Vector3i a_RelPos, NIBBLETYPE a_Nibble)
2014-05-24 08:37:25 -04:00
{
if (!cChunkDef::IsValidRelPos(a_RelPos))
2014-05-24 08:37:25 -04:00
{
ASSERT(!"cChunkData::SetMeta(): index out of range!");
return false;
}
auto Idxs = IndicesFromRelPos(a_RelPos);
if (m_Sections[Idxs.Section] == nullptr)
2014-05-24 08:37:25 -04:00
{
if ((a_Nibble & 0xf) == 0x00)
{
return false;
}
m_Sections[Idxs.Section] = Allocate();
if (m_Sections[Idxs.Section] == nullptr)
2014-05-24 08:37:25 -04:00
{
ASSERT(!"Failed to allocate a new section in Chunkbuffer");
return false;
}
ZeroSection(m_Sections[Idxs.Section]);
2014-05-24 08:37:25 -04:00
}
NIBBLETYPE oldval = m_Sections[Idxs.Section]->m_BlockMetas[Idxs.Index / 2] >> ((Idxs.Index & 1) * 4) & 0xf;
m_Sections[Idxs.Section]->m_BlockMetas[Idxs.Index / 2] = static_cast<NIBBLETYPE>(
(m_Sections[Idxs.Section]->m_BlockMetas[Idxs.Index / 2] & (0xf0 >> ((Idxs.Index & 1) * 4))) | // The untouched nibble
((a_Nibble & 0x0f) << ((Idxs.Index & 1) * 4)) // The nibble being set
2014-05-24 08:37:25 -04:00
);
return oldval != a_Nibble;
2014-05-24 08:37:25 -04:00
}
2014-05-28 05:35:55 -04:00
NIBBLETYPE cChunkData::GetBlockLight(Vector3i a_RelPos) const
2014-05-24 08:37:25 -04:00
{
if (cChunkDef::IsValidRelPos(a_RelPos))
2014-05-24 08:37:25 -04:00
{
auto Idxs = IndicesFromRelPos(a_RelPos);
if (m_Sections[Idxs.Section] != nullptr)
2014-05-24 08:37:25 -04:00
{
return (m_Sections[Idxs.Section]->m_BlockLight[Idxs.Index / 2] >> ((Idxs.Index & 1) * 4)) & 0x0f;
2014-05-24 08:37:25 -04:00
}
else
{
return 0;
}
}
ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!");
return 0;
}
2014-05-28 05:35:55 -04:00
NIBBLETYPE cChunkData::GetSkyLight(Vector3i a_RelPos) const
2014-05-24 08:37:25 -04:00
{
if (cChunkDef::IsValidRelPos(a_RelPos))
2014-05-24 08:37:25 -04:00
{
auto Idxs = IndicesFromRelPos(a_RelPos);
if (m_Sections[Idxs.Section] != nullptr)
2014-05-24 08:37:25 -04:00
{
return (m_Sections[Idxs.Section]->m_BlockSkyLight[Idxs.Index / 2] >> ((Idxs.Index & 1) * 4)) & 0x0f;
2014-05-24 08:37:25 -04:00
}
else
{
return 0xF;
}
}
ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!");
return 0;
}
2014-05-28 05:35:55 -04:00
const cChunkData::sChunkSection * cChunkData::GetSection(size_t a_SectionNum) const
{
if (a_SectionNum < NumSections)
{
return m_Sections[a_SectionNum];
}
ASSERT(!"cChunkData::GetSection: section index out of range");
return nullptr;
}
UInt16 cChunkData::GetSectionBitmask() const
{
static_assert(NumSections <= 16U, "cChunkData::GetSectionBitmask needs a bigger data type");
UInt16 Res = 0U;
for (size_t i = 0U; i < NumSections; ++i)
{
Res |= ((m_Sections[i] != nullptr) << i);
}
return Res;
}
void cChunkData::Clear()
{
for (size_t i = 0; i < NumSections; ++i)
{
2014-10-20 16:55:07 -04:00
if (m_Sections[i] != nullptr)
{
Free(m_Sections[i]);
m_Sections[i] = nullptr;
}
}
}
2014-05-29 12:25:08 -04:00
void cChunkData::CopyBlockTypes(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Length) const
{
2014-05-30 09:43:33 -04:00
size_t ToSkip = a_Idx;
2016-02-05 16:45:45 -05:00
2014-05-29 12:25:08 -04:00
for (size_t i = 0; i < NumSections; i++)
{
size_t StartPos = 0;
if (ToSkip > 0)
2014-05-24 08:33:40 -04:00
{
2014-05-30 09:43:33 -04:00
StartPos = std::min(ToSkip, +SectionBlockCount);
ToSkip -= StartPos;
2014-05-24 08:33:40 -04:00
}
2014-05-30 09:43:33 -04:00
if (StartPos < SectionBlockCount)
{
2014-05-30 09:43:33 -04:00
size_t ToCopy = std::min(+SectionBlockCount - StartPos, a_Length);
2014-05-29 03:09:11 -04:00
a_Length -= ToCopy;
2014-10-20 16:55:07 -04:00
if (m_Sections[i] != nullptr)
2014-04-27 10:38:16 -04:00
{
2014-05-30 09:43:33 -04:00
BLOCKTYPE * blockbuffer = m_Sections[i]->m_BlockTypes;
memcpy(&a_Dest[(i * SectionBlockCount) + StartPos - a_Idx], blockbuffer + StartPos, sizeof(BLOCKTYPE) * ToCopy);
2014-04-27 10:38:16 -04:00
}
else
{
memset(&a_Dest[(i * SectionBlockCount) + StartPos - a_Idx], 0, sizeof(BLOCKTYPE) * ToCopy);
2014-04-27 10:38:16 -04:00
}
}
}
}
2014-05-29 12:25:08 -04:00
void cChunkData::CopyMetas(NIBBLETYPE * a_Dest) const
{
2014-05-29 12:25:08 -04:00
for (size_t i = 0; i < NumSections; i++)
{
2014-10-20 16:55:07 -04:00
if (m_Sections[i] != nullptr)
2014-04-27 10:38:16 -04:00
{
memcpy(&a_Dest[i * SectionBlockCount / 2], &m_Sections[i]->m_BlockMetas, sizeof(m_Sections[i]->m_BlockMetas));
2014-04-27 10:38:16 -04:00
}
else
{
memset(&a_Dest[i * SectionBlockCount / 2], 0, sizeof(m_Sections[i]->m_BlockMetas));
2014-04-27 10:38:16 -04:00
}
}
}
2014-05-28 16:40:19 -04:00
void cChunkData::CopyBlockLight(NIBBLETYPE * a_Dest) const
{
2014-05-29 12:25:08 -04:00
for (size_t i = 0; i < NumSections; i++)
{
2014-10-20 16:55:07 -04:00
if (m_Sections[i] != nullptr)
2014-04-27 10:38:16 -04:00
{
memcpy(&a_Dest[i * SectionBlockCount / 2], &m_Sections[i]->m_BlockLight, sizeof(m_Sections[i]->m_BlockLight));
2014-04-27 10:38:16 -04:00
}
else
{
memset(&a_Dest[i * SectionBlockCount / 2], 0, sizeof(m_Sections[i]->m_BlockLight));
2014-04-27 10:38:16 -04:00
}
}
}
2014-05-28 16:40:19 -04:00
void cChunkData::CopySkyLight(NIBBLETYPE * a_Dest) const
{
2014-05-29 12:25:08 -04:00
for (size_t i = 0; i < NumSections; i++)
{
2014-10-20 16:55:07 -04:00
if (m_Sections[i] != nullptr)
2014-04-27 10:38:16 -04:00
{
memcpy(&a_Dest[i * SectionBlockCount / 2], &m_Sections[i]->m_BlockSkyLight, sizeof(m_Sections[i]->m_BlockSkyLight));
2014-04-27 10:38:16 -04:00
}
else
{
memset(&a_Dest[i * SectionBlockCount / 2], 0xff, sizeof(m_Sections[i]->m_BlockSkyLight));
2014-04-27 10:38:16 -04:00
}
2017-08-20 18:23:23 -04:00
}
}
void cChunkData::FillBlockTypes(BLOCKTYPE a_Value)
{
// If needed, allocate any missing sections
if (a_Value != 0x00)
{
for (auto & Section : m_Sections)
{
if (Section == nullptr)
{
Section = Allocate();
std::fill(std::begin(Section->m_BlockMetas), std::end(Section->m_BlockMetas), 0x00);
std::fill(std::begin(Section->m_BlockLight), std::end(Section->m_BlockLight), 0x00);
std::fill(std::begin(Section->m_BlockSkyLight), std::end(Section->m_BlockSkyLight), 0xff);
}
}
}
for (auto Section : m_Sections)
{
if (Section != nullptr)
{
std::fill(std::begin(Section->m_BlockTypes), std::end(Section->m_BlockTypes), a_Value);
}
}
}
void cChunkData::FillMetas(NIBBLETYPE a_Value)
{
// If needed, allocate any missing sections
if (a_Value != 0x00)
{
for (auto & Section : m_Sections)
{
if (Section == nullptr)
{
Section = Allocate();
std::fill(std::begin(Section->m_BlockTypes), std::end(Section->m_BlockTypes), 0x00);
std::fill(std::begin(Section->m_BlockLight), std::end(Section->m_BlockLight), 0x00);
std::fill(std::begin(Section->m_BlockSkyLight), std::end(Section->m_BlockSkyLight), 0xff);
}
}
}
NIBBLETYPE NewMeta = static_cast<NIBBLETYPE>((a_Value << 4) | a_Value);
for (auto Section : m_Sections)
{
if (Section != nullptr)
{
std::fill(std::begin(Section->m_BlockMetas), std::end(Section->m_BlockMetas), NewMeta);
}
}
}
void cChunkData::FillBlockLight(NIBBLETYPE a_Value)
{
// If needed, allocate any missing sections
if (a_Value != 0x00)
{
for (auto & Section : m_Sections)
{
if (Section == nullptr)
{
Section = Allocate();
std::fill(std::begin(Section->m_BlockTypes), std::end(Section->m_BlockTypes), 0x00);
std::fill(std::begin(Section->m_BlockMetas), std::end(Section->m_BlockMetas), 0x00);
std::fill(std::begin(Section->m_BlockSkyLight), std::end(Section->m_BlockSkyLight), 0xff);
}
}
}
NIBBLETYPE NewLight = static_cast<NIBBLETYPE>((a_Value << 4) | a_Value);
for (auto Section : m_Sections)
{
if (Section != nullptr)
{
std::fill(std::begin(Section->m_BlockLight), std::end(Section->m_BlockLight), NewLight);
}
}
}
void cChunkData::FillSkyLight(NIBBLETYPE a_Value)
{
// If needed, allocate any missing sections
if (a_Value != 0x0f)
{
for (auto & Section : m_Sections)
{
if (Section == nullptr)
{
Section = Allocate();
std::fill(std::begin(Section->m_BlockTypes), std::end(Section->m_BlockTypes), 0x00);
std::fill(std::begin(Section->m_BlockMetas), std::end(Section->m_BlockMetas), 0x00);
std::fill(std::begin(Section->m_BlockLight), std::end(Section->m_BlockLight), 0x00);
}
}
}
NIBBLETYPE NewSkyLight = static_cast<NIBBLETYPE>((a_Value << 4) | a_Value);
for (auto Section : m_Sections)
{
if (Section != nullptr)
{
std::fill(std::begin(Section->m_BlockSkyLight), std::end(Section->m_BlockSkyLight), NewSkyLight);
}
}
}
2014-05-29 12:25:08 -04:00
void cChunkData::SetBlockTypes(const BLOCKTYPE * a_Src)
{
2014-10-20 16:55:07 -04:00
ASSERT(a_Src != nullptr);
2016-02-05 16:45:45 -05:00
2014-05-29 12:25:08 -04:00
for (size_t i = 0; i < NumSections; i++)
{
2014-05-29 12:25:08 -04:00
// If the section is already allocated, copy the data into it:
2014-10-20 16:55:07 -04:00
if (m_Sections[i] != nullptr)
{
2014-05-30 03:17:17 -04:00
memcpy(m_Sections[i]->m_BlockTypes, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockTypes));
2014-05-29 12:25:08 -04:00
continue;
}
2014-05-29 12:25:08 -04:00
// The section doesn't exist, find out if it is needed:
if (IsAllValue(a_Src + i * SectionBlockCount, SectionBlockCount, static_cast<BLOCKTYPE>(0)))
2014-04-27 10:38:16 -04:00
{
2014-05-29 12:25:08 -04:00
// No need for the section, the data is all-air
continue;
2014-04-27 10:38:16 -04:00
}
2016-02-05 16:45:45 -05:00
2014-05-29 12:25:08 -04:00
// Allocate the section and copy the data into it:
m_Sections[i] = Allocate();
2014-05-30 03:17:17 -04:00
memcpy(m_Sections[i]->m_BlockTypes, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockTypes));
memset(m_Sections[i]->m_BlockMetas, 0x00, sizeof(m_Sections[i]->m_BlockMetas));
2014-05-29 12:25:08 -04:00
memset(m_Sections[i]->m_BlockLight, 0x00, sizeof(m_Sections[i]->m_BlockLight));
memset(m_Sections[i]->m_BlockSkyLight, 0xff, sizeof(m_Sections[i]->m_BlockSkyLight));
} // for i - m_Sections[]
}
2014-05-29 12:25:08 -04:00
void cChunkData::SetMetas(const NIBBLETYPE * a_Src)
{
2014-10-20 16:55:07 -04:00
ASSERT(a_Src != nullptr);
2016-02-05 16:45:45 -05:00
2014-05-29 12:25:08 -04:00
for (size_t i = 0; i < NumSections; i++)
{
2014-05-29 12:25:08 -04:00
// If the section is already allocated, copy the data into it:
2014-10-20 16:55:07 -04:00
if (m_Sections[i] != nullptr)
{
2014-05-30 05:35:29 -04:00
memcpy(m_Sections[i]->m_BlockMetas, &a_Src[i * SectionBlockCount / 2], sizeof(m_Sections[i]->m_BlockMetas));
2014-05-29 12:25:08 -04:00
continue;
}
2016-02-05 16:45:45 -05:00
2014-05-29 12:25:08 -04:00
// The section doesn't exist, find out if it is needed:
if (IsAllValue(a_Src + i * SectionBlockCount / 2, SectionBlockCount / 2, static_cast<NIBBLETYPE>(0)))
2014-04-27 10:38:16 -04:00
{
2014-05-29 12:25:08 -04:00
// No need for the section, the data is all zeroes
continue;
2014-04-27 10:38:16 -04:00
}
2016-02-05 16:45:45 -05:00
2014-05-29 12:25:08 -04:00
// Allocate the section and copy the data into it:
m_Sections[i] = Allocate();
2014-05-30 05:35:29 -04:00
memcpy(m_Sections[i]->m_BlockMetas, &a_Src[i * SectionBlockCount / 2], sizeof(m_Sections[i]->m_BlockMetas));
2014-05-29 12:25:08 -04:00
memset(m_Sections[i]->m_BlockTypes, 0x00, sizeof(m_Sections[i]->m_BlockTypes));
memset(m_Sections[i]->m_BlockLight, 0x00, sizeof(m_Sections[i]->m_BlockLight));
memset(m_Sections[i]->m_BlockSkyLight, 0xff, sizeof(m_Sections[i]->m_BlockSkyLight));
} // for i - m_Sections[]
}
2014-05-29 12:25:08 -04:00
2014-05-28 16:40:19 -04:00
void cChunkData::SetBlockLight(const NIBBLETYPE * a_Src)
{
2014-10-20 16:55:07 -04:00
if (a_Src == nullptr)
2014-05-28 16:40:19 -04:00
{
return;
}
2016-02-05 16:45:45 -05:00
2014-05-29 12:25:08 -04:00
for (size_t i = 0; i < NumSections; i++)
{
2014-05-29 12:25:08 -04:00
// If the section is already allocated, copy the data into it:
2014-10-20 16:55:07 -04:00
if (m_Sections[i] != nullptr)
{
2014-05-30 05:35:29 -04:00
memcpy(m_Sections[i]->m_BlockLight, &a_Src[i * SectionBlockCount / 2], sizeof(m_Sections[i]->m_BlockLight));
2014-05-29 12:25:08 -04:00
continue;
}
2016-02-05 16:45:45 -05:00
2014-05-29 12:25:08 -04:00
// The section doesn't exist, find out if it is needed:
if (IsAllValue(a_Src + i * SectionBlockCount / 2, SectionBlockCount / 2, static_cast<NIBBLETYPE>(0)))
2014-04-27 10:38:16 -04:00
{
2014-05-29 12:25:08 -04:00
// No need for the section, the data is all zeroes
continue;
2014-04-27 10:38:16 -04:00
}
2016-02-05 16:45:45 -05:00
2014-05-29 12:25:08 -04:00
// Allocate the section and copy the data into it:
m_Sections[i] = Allocate();
2014-05-30 05:35:29 -04:00
memcpy(m_Sections[i]->m_BlockLight, &a_Src[i * SectionBlockCount / 2], sizeof(m_Sections[i]->m_BlockLight));
2014-05-29 12:25:08 -04:00
memset(m_Sections[i]->m_BlockTypes, 0x00, sizeof(m_Sections[i]->m_BlockTypes));
memset(m_Sections[i]->m_BlockMetas, 0x00, sizeof(m_Sections[i]->m_BlockMetas));
2014-05-29 12:25:08 -04:00
memset(m_Sections[i]->m_BlockSkyLight, 0xff, sizeof(m_Sections[i]->m_BlockSkyLight));
} // for i - m_Sections[]
}
2014-05-28 16:40:19 -04:00
void cChunkData::SetSkyLight(const NIBBLETYPE * a_Src)
{
2014-10-20 16:55:07 -04:00
if (a_Src == nullptr)
2014-05-28 16:40:19 -04:00
{
return;
}
2016-02-05 16:45:45 -05:00
2014-05-29 12:25:08 -04:00
for (size_t i = 0; i < NumSections; i++)
{
2014-05-29 12:25:08 -04:00
// If the section is already allocated, copy the data into it:
2014-10-20 16:55:07 -04:00
if (m_Sections[i] != nullptr)
{
2014-05-30 05:35:29 -04:00
memcpy(m_Sections[i]->m_BlockSkyLight, &a_Src[i * SectionBlockCount / 2], sizeof(m_Sections[i]->m_BlockSkyLight));
2014-05-29 12:25:08 -04:00
continue;
}
2014-05-29 12:25:08 -04:00
// The section doesn't exist, find out if it is needed:
if (IsAllValue(a_Src + i * SectionBlockCount / 2, SectionBlockCount / 2, static_cast<NIBBLETYPE>(0xff)))
2014-04-27 10:38:16 -04:00
{
2014-05-29 12:25:08 -04:00
// No need for the section, the data is all zeroes
continue;
2014-04-27 10:38:16 -04:00
}
2016-02-05 16:45:45 -05:00
2014-05-29 12:25:08 -04:00
// Allocate the section and copy the data into it:
m_Sections[i] = Allocate();
2014-05-30 05:35:29 -04:00
memcpy(m_Sections[i]->m_BlockSkyLight, &a_Src[i * SectionBlockCount / 2], sizeof(m_Sections[i]->m_BlockSkyLight));
2014-05-29 12:25:08 -04:00
memset(m_Sections[i]->m_BlockTypes, 0x00, sizeof(m_Sections[i]->m_BlockTypes));
memset(m_Sections[i]->m_BlockMetas, 0x00, sizeof(m_Sections[i]->m_BlockMetas));
2014-05-29 12:25:08 -04:00
memset(m_Sections[i]->m_BlockLight, 0x00, sizeof(m_Sections[i]->m_BlockLight));
} // for i - m_Sections[]
}
UInt32 cChunkData::NumPresentSections() const
{
UInt32 Ret = 0U;
for (size_t i = 0; i < NumSections; i++)
{
if (m_Sections[i] != nullptr)
{
++Ret;
}
}
return Ret;
}
2014-05-29 03:09:11 -04:00
cChunkData::sChunkSection * cChunkData::Allocate(void)
{
return m_Pool.Allocate();
}
2014-05-28 05:35:55 -04:00
2014-05-29 03:09:11 -04:00
void cChunkData::Free(cChunkData::sChunkSection * a_Section)
{
m_Pool.Free(a_Section);
}
2014-05-28 05:35:55 -04:00
2014-05-28 16:40:19 -04:00
void cChunkData::ZeroSection(cChunkData::sChunkSection * a_Section) const
{
2014-05-29 12:25:08 -04:00
memset(a_Section->m_BlockTypes, 0x00, sizeof(a_Section->m_BlockTypes));
memset(a_Section->m_BlockMetas, 0x00, sizeof(a_Section->m_BlockMetas));
2014-05-29 12:25:08 -04:00
memset(a_Section->m_BlockLight, 0x00, sizeof(a_Section->m_BlockLight));
memset(a_Section->m_BlockSkyLight, 0xff, sizeof(a_Section->m_BlockSkyLight));
}