2014-04-26 13:50:23 -04:00
|
|
|
|
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
|
|
|
|
|
2014-04-26 13:50:23 -04:00
|
|
|
#include "Globals.h"
|
2014-05-21 14:58:48 -04:00
|
|
|
#include "ChunkData.h"
|
2014-04-26 13:50:23 -04:00
|
|
|
|
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> inline bool IsAllValue(const T * a_Array, size_t a_NumElements, T a_Value)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < a_NumElements; i++)
|
|
|
|
{
|
|
|
|
if (a_Array[i] != a_Value)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-16 10:12:50 -04:00
|
|
|
cChunkData::cChunkData(cAllocationPool<cChunkData::sChunkSection> & a_Pool) :
|
2014-05-24 08:37:25 -04:00
|
|
|
#if __cplusplus < 201103L
|
|
|
|
// auto_ptr style interface for memory management
|
2014-06-16 10:12:50 -04:00
|
|
|
m_IsOwner(true),
|
2014-05-24 08:37:25 -04:00
|
|
|
#endif
|
2014-06-16 10:12:50 -04:00
|
|
|
m_Pool(a_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-28 05:35:55 -04:00
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
m_Sections[i] = nullptr;
|
2014-05-28 05:35:55 -04:00
|
|
|
}
|
2014-05-24 08:37:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-28 05:35:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-24 08:37:25 -04:00
|
|
|
cChunkData::~cChunkData()
|
|
|
|
{
|
|
|
|
#if __cplusplus < 201103L
|
|
|
|
// auto_ptr style interface for memory management
|
2014-05-28 05:35:55 -04:00
|
|
|
if (!m_IsOwner)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2014-05-29 12:25:08 -04:00
|
|
|
for (size_t i = 0; i < NumSections; i++)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
2014-05-28 05:35:55 -04:00
|
|
|
Free(m_Sections[i]);
|
2014-10-20 16:55:07 -04:00
|
|
|
m_Sections[i] = nullptr;
|
2014-05-24 08:37:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-28 05:35:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-24 08:37:25 -04:00
|
|
|
#if __cplusplus < 201103L
|
|
|
|
// auto_ptr style interface for memory management
|
2014-05-28 05:35:55 -04:00
|
|
|
cChunkData::cChunkData(const cChunkData & a_Other) :
|
2014-06-16 10:12:50 -04:00
|
|
|
m_IsOwner(true),
|
|
|
|
m_Pool(a_Other.m_Pool)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
2014-05-28 05:35:55 -04:00
|
|
|
// Move contents and ownership from a_Other to this, pointer-wise:
|
2014-05-29 12:25:08 -04:00
|
|
|
for (size_t i = 0; i < NumSections; i++)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
2014-05-28 05:35:55 -04:00
|
|
|
m_Sections[i] = a_Other.m_Sections[i];
|
2014-05-24 08:37:25 -04:00
|
|
|
}
|
2014-05-28 05:35:55 -04:00
|
|
|
a_Other.m_IsOwner = false;
|
2014-05-24 08:37:25 -04:00
|
|
|
}
|
|
|
|
|
2014-05-28 05:35:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cChunkData & cChunkData::operator =(const cChunkData & a_Other)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
2014-05-28 05:35:55 -04:00
|
|
|
// If assigning to self, no-op
|
|
|
|
if (&a_Other == this)
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-05-28 05:35:55 -04:00
|
|
|
// Free any currently held contents:
|
|
|
|
if (m_IsOwner)
|
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
|
|
|
{
|
2014-05-28 05:35:55 -04:00
|
|
|
Free(m_Sections[i]);
|
2014-10-20 16:55:07 -04:00
|
|
|
m_Sections[i] = nullptr;
|
2014-05-24 08:37:25 -04:00
|
|
|
}
|
|
|
|
}
|
2014-06-16 10:12:50 -04:00
|
|
|
|
2014-05-28 05:35:55 -04:00
|
|
|
// Move contents and ownership from a_Other to this, pointer-wise:
|
|
|
|
m_IsOwner = true;
|
2014-05-29 12:25:08 -04:00
|
|
|
for (size_t i = 0; i < NumSections; i++)
|
2014-05-28 05:35:55 -04:00
|
|
|
{
|
|
|
|
m_Sections[i] = a_Other.m_Sections[i];
|
|
|
|
}
|
|
|
|
a_Other.m_IsOwner = false;
|
2014-06-16 10:12:50 -04:00
|
|
|
ASSERT(&m_Pool == &a_Other.m_Pool);
|
2014-05-24 08:37:25 -04:00
|
|
|
return *this;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-05-24 08:37:25 -04:00
|
|
|
#else
|
2014-05-28 05:35:55 -04:00
|
|
|
|
2014-05-24 08:37:25 -04:00
|
|
|
// unique_ptr style interface for memory management
|
2014-06-16 10:12:50 -04:00
|
|
|
cChunkData::cChunkData(cChunkData && other) :
|
|
|
|
m_Pool(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] = other.m_Sections[i];
|
2014-10-20 16:55:07 -04:00
|
|
|
other.m_Sections[i] = nullptr;
|
2014-05-24 08:37:25 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-24 08:37:25 -04:00
|
|
|
|
2014-05-28 05:35:55 -04:00
|
|
|
cChunkData & cChunkData::operator =(cChunkData && other)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
|
|
|
if (&other != this)
|
|
|
|
{
|
2014-06-16 10:12:50 -04:00
|
|
|
ASSERT(&m_Pool == &other.m_Pool);
|
2014-05-29 12:25:08 -04:00
|
|
|
for (size_t i = 0; i < NumSections; i++)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
2014-05-28 05:35:55 -04:00
|
|
|
Free(m_Sections[i]);
|
2014-05-24 08:37:25 -04:00
|
|
|
m_Sections[i] = other.m_Sections[i];
|
2014-10-20 16:55:07 -04:00
|
|
|
other.m_Sections[i] = nullptr;
|
2014-05-24 08:37:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-05-28 05:35:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-24 08:37:25 -04:00
|
|
|
BLOCKTYPE cChunkData::GetBlock(int a_X, int a_Y, int a_Z) const
|
|
|
|
{
|
2016-03-24 12:18:14 -04:00
|
|
|
if (
|
|
|
|
(a_X < 0) || (a_X >= cChunkDef::Width) ||
|
|
|
|
(a_Y < 0) || (a_Y >= cChunkDef::Height) ||
|
|
|
|
(a_Z < 0) || (a_Z >= cChunkDef::Width)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return E_BLOCK_AIR; // Coordinates are outside outside the world, so this must be an air block
|
|
|
|
}
|
2014-05-29 12:25:08 -04:00
|
|
|
int Section = a_Y / SectionHeight;
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_Sections[Section] != nullptr)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
int Index = cChunkDef::MakeIndexNoCheck(a_X, static_cast<int>(static_cast<UInt32>(a_Y) - (static_cast<UInt32>(Section) * SectionHeight)), a_Z);
|
2014-05-24 08:37:25 -04:00
|
|
|
return m_Sections[Section]->m_BlockTypes[Index];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-28 05:35:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-24 08:37:25 -04:00
|
|
|
void cChunkData::SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_Block)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
(a_RelX >= cChunkDef::Width) || (a_RelX < 0) ||
|
|
|
|
(a_RelY >= cChunkDef::Height) || (a_RelY < 0) ||
|
|
|
|
(a_RelZ >= cChunkDef::Width) || (a_RelZ < 0)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
ASSERT(!"cChunkData::SetMeta(): index out of range!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-29 11:04:03 -04:00
|
|
|
int Section = static_cast<int>(static_cast<UInt32>(a_RelY) / SectionHeight);
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_Sections[Section] == nullptr)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
|
|
|
if (a_Block == 0x00)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_Sections[Section] = Allocate();
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_Sections[Section] == nullptr)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
|
|
|
ASSERT(!"Failed to allocate a new section in Chunkbuffer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ZeroSection(m_Sections[Section]);
|
|
|
|
}
|
2015-07-29 11:04:03 -04:00
|
|
|
int Index = cChunkDef::MakeIndexNoCheck(a_RelX, static_cast<int>(static_cast<UInt32>(a_RelY) - (static_cast<UInt32>(Section) * SectionHeight)), a_RelZ);
|
2014-05-24 08:37:25 -04:00
|
|
|
m_Sections[Section]->m_BlockTypes[Index] = a_Block;
|
|
|
|
}
|
|
|
|
|
2014-05-28 05:35:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-24 08:37:25 -04:00
|
|
|
NIBBLETYPE cChunkData::GetMeta(int a_RelX, int a_RelY, int a_RelZ) const
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
(a_RelX < cChunkDef::Width) && (a_RelX > -1) &&
|
|
|
|
(a_RelY < cChunkDef::Height) && (a_RelY > -1) &&
|
|
|
|
(a_RelZ < cChunkDef::Width) && (a_RelZ > -1))
|
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
int Section = static_cast<int>(static_cast<UInt32>(a_RelY) / SectionHeight);
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_Sections[Section] != nullptr)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
int Index = cChunkDef::MakeIndexNoCheck(a_RelX, static_cast<int>(static_cast<UInt32>(a_RelY) - (static_cast<UInt32>(Section) * SectionHeight)), a_RelZ);
|
2014-05-29 14:18:37 -04:00
|
|
|
return (m_Sections[Section]->m_BlockMetas[Index / 2] >> ((Index & 1) * 4)) & 0x0f;
|
2014-05-24 08:37:25 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-03-24 12:18:14 -04:00
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-24 08:37:25 -04:00
|
|
|
bool cChunkData::SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
(a_RelX >= cChunkDef::Width) || (a_RelX < 0) ||
|
|
|
|
(a_RelY >= cChunkDef::Height) || (a_RelY < 0) ||
|
|
|
|
(a_RelZ >= cChunkDef::Width) || (a_RelZ < 0)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
ASSERT(!"cChunkData::SetMeta(): index out of range!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-29 11:04:03 -04:00
|
|
|
int Section = static_cast<int>(static_cast<UInt32>(a_RelY) / SectionHeight);
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_Sections[Section] == nullptr)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
|
|
|
if ((a_Nibble & 0xf) == 0x00)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_Sections[Section] = Allocate();
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_Sections[Section] == nullptr)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
|
|
|
ASSERT(!"Failed to allocate a new section in Chunkbuffer");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ZeroSection(m_Sections[Section]);
|
|
|
|
}
|
2015-07-29 11:04:03 -04:00
|
|
|
int Index = cChunkDef::MakeIndexNoCheck(a_RelX, static_cast<int>(static_cast<UInt32>(a_RelY) - (static_cast<UInt32>(Section) * SectionHeight)), a_RelZ);
|
2014-05-29 14:18:37 -04:00
|
|
|
NIBBLETYPE oldval = m_Sections[Section]->m_BlockMetas[Index / 2] >> ((Index & 1) * 4) & 0xf;
|
|
|
|
m_Sections[Section]->m_BlockMetas[Index / 2] = static_cast<NIBBLETYPE>(
|
|
|
|
(m_Sections[Section]->m_BlockMetas[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble
|
2014-05-24 08:37:25 -04:00
|
|
|
((a_Nibble & 0x0f) << ((Index & 1) * 4)) // The nibble being set
|
|
|
|
);
|
2014-06-06 18:23:28 -04:00
|
|
|
return oldval != a_Nibble;
|
2014-05-24 08:37:25 -04:00
|
|
|
}
|
|
|
|
|
2014-05-28 05:35:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-24 08:37:25 -04:00
|
|
|
NIBBLETYPE cChunkData::GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const
|
|
|
|
{
|
2014-05-28 05:35:55 -04:00
|
|
|
if (
|
|
|
|
(a_RelX < cChunkDef::Width) && (a_RelX > -1) &&
|
|
|
|
(a_RelY < cChunkDef::Height) && (a_RelY > -1) &&
|
|
|
|
(a_RelZ < cChunkDef::Width) && (a_RelZ > -1)
|
|
|
|
)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
int Section = static_cast<int>(static_cast<UInt32>(a_RelY) / SectionHeight);
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_Sections[Section] != nullptr)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
int Index = cChunkDef::MakeIndexNoCheck(a_RelX, static_cast<int>(static_cast<UInt32>(a_RelY) - (static_cast<UInt32>(Section) * SectionHeight)), a_RelZ);
|
2014-05-24 08:37:25 -04:00
|
|
|
return (m_Sections[Section]->m_BlockLight[Index / 2] >> ((Index & 1) * 4)) & 0x0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-28 05:35:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-24 08:37:25 -04:00
|
|
|
NIBBLETYPE cChunkData::GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const
|
|
|
|
{
|
|
|
|
if ((a_RelX < cChunkDef::Width) && (a_RelX > -1) && (a_RelY < cChunkDef::Height) && (a_RelY > -1) && (a_RelZ < cChunkDef::Width) && (a_RelZ > -1))
|
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
int Section = static_cast<int>(static_cast<UInt32>(a_RelY) / SectionHeight);
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_Sections[Section] != nullptr)
|
2014-05-24 08:37:25 -04:00
|
|
|
{
|
2015-07-29 11:04:03 -04:00
|
|
|
int Index = cChunkDef::MakeIndexNoCheck(a_RelX, static_cast<int>(static_cast<UInt32>(a_RelY) - (static_cast<UInt32>(Section) * SectionHeight)), a_RelZ);
|
2014-06-04 04:21:44 -04:00
|
|
|
return (m_Sections[Section]->m_BlockSkyLight[Index / 2] >> ((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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cChunkData cChunkData::Copy(void) const
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-06-16 10:12:50 -04:00
|
|
|
cChunkData copy(m_Pool);
|
2014-05-29 12:25:08 -04:00
|
|
|
for (size_t i = 0; i < NumSections; i++)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_Sections[i] != nullptr)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-06-16 10:12:50 -04:00
|
|
|
copy.m_Sections[i] = copy.Allocate();
|
2014-04-26 13:50:23 -04:00
|
|
|
*copy.m_Sections[i] = *m_Sections[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-29 12:25:08 -04:00
|
|
|
void cChunkData::CopyBlockTypes(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Length) const
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
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++)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-29 14:18:37 -04:00
|
|
|
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-04-26 13:50:23 -04:00
|
|
|
{
|
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
|
|
|
|
{
|
2015-03-10 16:32:01 -04:00
|
|
|
memset(&a_Dest[(i * SectionBlockCount) + StartPos - a_Idx], 0, sizeof(BLOCKTYPE) * ToCopy);
|
2014-04-27 10:38:16 -04:00
|
|
|
}
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-29 12:25:08 -04:00
|
|
|
void cChunkData::CopyMetas(NIBBLETYPE * a_Dest) const
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-29 12:25:08 -04:00
|
|
|
for (size_t i = 0; i < NumSections; i++)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_Sections[i] != nullptr)
|
2014-04-27 10:38:16 -04:00
|
|
|
{
|
2014-05-29 14:18:37 -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
|
|
|
|
{
|
2014-05-29 14:18:37 -04:00
|
|
|
memset(&a_Dest[i * SectionBlockCount / 2], 0, sizeof(m_Sections[i]->m_BlockMetas));
|
2014-04-27 10:38:16 -04:00
|
|
|
}
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-28 16:40:19 -04:00
|
|
|
void cChunkData::CopyBlockLight(NIBBLETYPE * a_Dest) const
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-29 12:25:08 -04:00
|
|
|
for (size_t i = 0; i < NumSections; i++)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_Sections[i] != nullptr)
|
2014-04-27 10:38:16 -04:00
|
|
|
{
|
2014-05-29 14:18:37 -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
|
|
|
|
{
|
2014-05-29 14:18:37 -04:00
|
|
|
memset(&a_Dest[i * SectionBlockCount / 2], 0, sizeof(m_Sections[i]->m_BlockLight));
|
2014-04-27 10:38:16 -04:00
|
|
|
}
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-28 16:40:19 -04:00
|
|
|
void cChunkData::CopySkyLight(NIBBLETYPE * a_Dest) const
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-29 12:25:08 -04:00
|
|
|
for (size_t i = 0; i < NumSections; i++)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-10-20 16:55:07 -04:00
|
|
|
if (m_Sections[i] != nullptr)
|
2014-04-27 10:38:16 -04:00
|
|
|
{
|
2014-05-29 14:18:37 -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
|
|
|
|
{
|
2014-05-29 14:18:37 -04:00
|
|
|
memset(&a_Dest[i * SectionBlockCount / 2], 0xff, sizeof(m_Sections[i]->m_BlockSkyLight));
|
2014-04-27 10:38:16 -04:00
|
|
|
}
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-29 12:25:08 -04:00
|
|
|
void cChunkData::SetBlockTypes(const BLOCKTYPE * a_Src)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
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-04-26 13:50:23 -04:00
|
|
|
{
|
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-04-26 13:50:23 -04:00
|
|
|
{
|
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-04-26 13:50:23 -04:00
|
|
|
}
|
2014-05-29 12:25:08 -04:00
|
|
|
|
|
|
|
// The section doesn't exist, find out if it is needed:
|
2015-07-29 11:04:03 -04:00
|
|
|
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));
|
2014-05-29 14:18:37 -04:00
|
|
|
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-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-29 12:25:08 -04:00
|
|
|
void cChunkData::SetMetas(const NIBBLETYPE * a_Src)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
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-04-26 13:50:23 -04:00
|
|
|
{
|
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-04-26 13:50:23 -04:00
|
|
|
{
|
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;
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
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:
|
2015-07-29 11:04:03 -04:00
|
|
|
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-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-29 12:25:08 -04:00
|
|
|
|
2014-05-28 16:40:19 -04:00
|
|
|
void cChunkData::SetBlockLight(const NIBBLETYPE * a_Src)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
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-04-26 13:50:23 -04:00
|
|
|
{
|
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-04-26 13:50:23 -04:00
|
|
|
{
|
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;
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
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:
|
2015-07-29 11:04:03 -04:00
|
|
|
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));
|
2014-05-29 14:18:37 -04:00
|
|
|
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-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-28 16:40:19 -04:00
|
|
|
void cChunkData::SetSkyLight(const NIBBLETYPE * a_Src)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
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-04-26 13:50:23 -04:00
|
|
|
{
|
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-04-26 13:50:23 -04:00
|
|
|
{
|
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-04-26 13:50:23 -04:00
|
|
|
}
|
2014-05-29 12:25:08 -04:00
|
|
|
|
|
|
|
// The section doesn't exist, find out if it is needed:
|
2015-07-29 11:04:03 -04:00
|
|
|
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));
|
2014-05-29 14:18:37 -04:00
|
|
|
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[]
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-29 03:09:11 -04:00
|
|
|
cChunkData::sChunkSection * cChunkData::Allocate(void)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-06-16 10:12:50 -04:00
|
|
|
return m_Pool.Allocate();
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
2014-04-27 11:11:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-28 05:35:55 -04:00
|
|
|
|
|
|
|
|
2014-05-29 03:09:11 -04:00
|
|
|
void cChunkData::Free(cChunkData::sChunkSection * a_Section)
|
2014-04-27 11:11:56 -04:00
|
|
|
{
|
2014-06-16 10:12:50 -04:00
|
|
|
m_Pool.Free(a_Section);
|
2014-04-27 11:11:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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-11 10:52:02 -04:00
|
|
|
{
|
2014-05-29 12:25:08 -04:00
|
|
|
memset(a_Section->m_BlockTypes, 0x00, sizeof(a_Section->m_BlockTypes));
|
2014-05-29 14:18:37 -04:00
|
|
|
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));
|
2014-05-11 10:52:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|