2014-04-26 13:50:23 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2014-05-03 09:02:51 -04:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
|
|
|
|
#include "ChunkDef.h"
|
|
|
|
|
|
|
|
|
2014-04-26 13:50:23 -04:00
|
|
|
#define CHUNK_SECTION_HEIGHT 16
|
|
|
|
#define CHUNK_SECTION_NUM (256 / CHUNK_SECTION_HEIGHT)
|
|
|
|
|
|
|
|
#if __cplusplus < 201103L
|
|
|
|
// auto_ptr style interface for memory management
|
|
|
|
#else
|
|
|
|
// unique_ptr style interface for memory management
|
|
|
|
#endif
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
class cChunkData
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
cChunkData()
|
2014-04-26 13:50:23 -04:00
|
|
|
#if __cplusplus < 201103L
|
|
|
|
// auto_ptr style interface for memory management
|
|
|
|
: IsOwner(true)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
memset(m_Sections, 0, sizeof(m_Sections));
|
|
|
|
}
|
2014-05-21 14:58:48 -04:00
|
|
|
~cChunkData()
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
#if __cplusplus < 201103L
|
|
|
|
// auto_ptr style interface for memory management
|
|
|
|
if(!IsOwner) return;
|
|
|
|
#endif
|
|
|
|
for (int i = 0; i < CHUNK_SECTION_NUM; i++)
|
|
|
|
{
|
2014-05-21 15:08:34 -04:00
|
|
|
if(m_Sections[i] == NULL) Free(m_Sections[i]);;
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if __cplusplus < 201103L
|
|
|
|
// auto_ptr style interface for memory management
|
2014-05-21 14:58:48 -04:00
|
|
|
cChunkData(const cChunkData& other) :
|
2014-04-27 10:10:30 -04:00
|
|
|
IsOwner(true)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
for (int i = 0; i < CHUNK_SECTION_NUM; i++)
|
|
|
|
{
|
|
|
|
m_Sections[i] = other.m_Sections[i];
|
|
|
|
}
|
|
|
|
other.IsOwner = false;
|
|
|
|
}
|
2014-04-27 10:10:30 -04:00
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
cChunkData& operator=(const cChunkData& other)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-17 10:32:28 -04:00
|
|
|
if(&other != this)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-17 09:04:44 -04:00
|
|
|
if(IsOwner)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < CHUNK_SECTION_NUM; i++)
|
|
|
|
{
|
|
|
|
if(m_Sections[i]) Free(m_Sections[i]);;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
IsOwner = true;
|
2014-04-26 13:50:23 -04:00
|
|
|
for (int i = 0; i < CHUNK_SECTION_NUM; i++)
|
|
|
|
{
|
2014-05-17 09:04:44 -04:00
|
|
|
m_Sections[i] = other.m_Sections[i];
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
2014-05-17 09:04:44 -04:00
|
|
|
other.IsOwner = false;
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
2014-05-17 09:29:15 -04:00
|
|
|
return *this;
|
2014-05-17 09:04:44 -04:00
|
|
|
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
// unique_ptr style interface for memory management
|
2014-05-21 14:58:48 -04:00
|
|
|
cChunkData(cChunkData&& other)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
for (int i = 0; i < CHUNK_SECTION_NUM; i++)
|
|
|
|
{
|
|
|
|
m_Sections[i] = other.m_Sections[i];
|
2014-05-17 10:11:58 -04:00
|
|
|
other.m_Sections[i] = 0;
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
cChunkData& operator=(cChunkData&& other)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-17 09:29:15 -04:00
|
|
|
if(&other != this)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-17 09:29:15 -04:00
|
|
|
for (int i = 0; i < CHUNK_SECTION_NUM; i++)
|
|
|
|
{
|
2014-05-21 15:08:34 -04:00
|
|
|
Free(m_Sections[i]);;
|
2014-05-17 09:29:15 -04:00
|
|
|
m_Sections[i] = other.m_Sections[i];
|
2014-05-17 10:11:58 -04:00
|
|
|
other.m_Sections[i] = 0;
|
2014-05-17 09:29:15 -04:00
|
|
|
}
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
2014-05-17 09:29:15 -04:00
|
|
|
return *this;
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
BLOCKTYPE GetBlock(int a_X, int a_Y, int a_Z) const
|
|
|
|
{
|
|
|
|
ASSERT((a_X >= 0) && (a_X < cChunkDef::Width));
|
|
|
|
ASSERT((a_Y >= 0) && (a_Y < cChunkDef::Height));
|
|
|
|
ASSERT((a_Z >= 0) && (a_Z < cChunkDef::Width));
|
|
|
|
int Section = a_Y / CHUNK_SECTION_HEIGHT;
|
2014-05-21 15:08:34 -04:00
|
|
|
if(m_Sections[Section] == NULL)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
int Index = cChunkDef::MakeIndexNoCheck(a_X, a_Y - (Section * CHUNK_SECTION_HEIGHT), a_Z);
|
|
|
|
return m_Sections[Section]->m_BlockTypes[Index];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void 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)
|
|
|
|
)
|
|
|
|
{
|
2014-05-21 14:58:48 -04:00
|
|
|
ASSERT(!"cChunkData::SetMeta(): index out of range!");
|
2014-04-26 13:50:23 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Section = a_RelY / CHUNK_SECTION_HEIGHT;
|
2014-05-21 15:08:34 -04:00
|
|
|
if(m_Sections[Section] != NULL)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-11 10:52:02 -04:00
|
|
|
if(a_Block == 0x00)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-04-26 13:50:23 -04:00
|
|
|
m_Sections[Section] = Allocate();
|
2014-05-21 15:08:34 -04:00
|
|
|
if(m_Sections[Section] != NULL)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-10 12:46:49 -04:00
|
|
|
ASSERT(!"Failed to allocate a new section in Chunkbuffer");
|
2014-04-26 13:50:23 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-05-11 10:52:02 -04:00
|
|
|
ZeroSection(m_Sections[Section]);
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ);
|
|
|
|
m_Sections[Section]->m_BlockTypes[Index] = a_Block;
|
|
|
|
}
|
|
|
|
|
|
|
|
NIBBLETYPE 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))
|
|
|
|
{
|
|
|
|
int Section = a_RelY / CHUNK_SECTION_HEIGHT;
|
2014-05-21 15:08:34 -04:00
|
|
|
if(m_Sections[Section] == NULL)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ);
|
|
|
|
return (m_Sections[Section]->m_BlockMeta[Index / 2] >> ((Index & 1) * 4)) & 0x0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2014-05-21 14:58:48 -04:00
|
|
|
ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!");
|
2014-04-26 13:50:23 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void 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)
|
|
|
|
)
|
|
|
|
{
|
2014-05-21 14:58:48 -04:00
|
|
|
ASSERT(!"cChunkData::SetMeta(): index out of range!");
|
2014-04-26 13:50:23 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Section = a_RelY / CHUNK_SECTION_HEIGHT;
|
2014-05-21 15:08:34 -04:00
|
|
|
if(m_Sections[Section] != NULL)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-11 10:52:02 -04:00
|
|
|
if((a_Nibble & 0xf) == 0x00)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-04-26 13:50:23 -04:00
|
|
|
m_Sections[Section] = Allocate();
|
2014-05-21 15:08:34 -04:00
|
|
|
if(m_Sections[Section] != NULL)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-10 12:46:49 -04:00
|
|
|
ASSERT(!"Failed to allocate a new section in Chunkbuffer");
|
2014-04-26 13:50:23 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-05-11 10:52:02 -04:00
|
|
|
ZeroSection(m_Sections[Section]);
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ);
|
|
|
|
m_Sections[Section]->m_BlockMeta[Index / 2] = static_cast<NIBBLETYPE>(
|
|
|
|
(m_Sections[Section]->m_BlockMeta[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble
|
|
|
|
((a_Nibble & 0x0f) << ((Index & 1) * 4)) // The nibble being set
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
NIBBLETYPE GetBlockLight(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))
|
|
|
|
{
|
|
|
|
int Section = a_RelY / CHUNK_SECTION_HEIGHT;
|
2014-05-21 15:08:34 -04:00
|
|
|
if(m_Sections[Section] == NULL)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ);
|
|
|
|
return (m_Sections[Section]->m_BlockLight[Index / 2] >> ((Index & 1) * 4)) & 0x0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2014-05-21 14:58:48 -04:00
|
|
|
ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!");
|
2014-04-26 13:50:23 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
NIBBLETYPE 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))
|
|
|
|
{
|
|
|
|
int Section = a_RelY / CHUNK_SECTION_HEIGHT;
|
2014-05-21 15:08:34 -04:00
|
|
|
if(m_Sections[Section] == NULL)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ);
|
|
|
|
return (m_Sections[Section]->m_BlockLight[Index / 2] >> ((Index & 1) * 4)) & 0x0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-17 10:11:58 -04:00
|
|
|
return 0xF;
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
}
|
2014-05-21 14:58:48 -04:00
|
|
|
ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!");
|
2014-04-26 13:50:23 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
cChunkData Copy() const;
|
2014-04-26 13:50:23 -04:00
|
|
|
void CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx = 0, size_t length = cChunkDef::NumBlocks) const;
|
|
|
|
void CopyMeta (NIBBLETYPE * a_dest) const;
|
|
|
|
void CopyLight (NIBBLETYPE * a_dest) const;
|
|
|
|
void CopySkyLight (NIBBLETYPE * a_dest) const;
|
|
|
|
|
|
|
|
void SetBlocks (const BLOCKTYPE * a_src);
|
|
|
|
void SetMeta (const NIBBLETYPE * a_src);
|
|
|
|
void SetLight (const NIBBLETYPE * a_src);
|
|
|
|
void SetSkyLight (const NIBBLETYPE * a_src);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
#if __cplusplus < 201103L
|
|
|
|
// auto_ptr style interface for memory management
|
2014-04-27 10:10:30 -04:00
|
|
|
mutable bool IsOwner;
|
2014-04-26 13:50:23 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
struct sChunkSection {
|
|
|
|
BLOCKTYPE m_BlockTypes [CHUNK_SECTION_HEIGHT * 16 * 16] ;
|
|
|
|
NIBBLETYPE m_BlockMeta [CHUNK_SECTION_HEIGHT * 16 * 16 / 2];
|
|
|
|
NIBBLETYPE m_BlockLight [CHUNK_SECTION_HEIGHT * 16 * 16 / 2];
|
|
|
|
NIBBLETYPE m_BlockSkyLight[CHUNK_SECTION_HEIGHT * 16 * 16 / 2];
|
|
|
|
};
|
|
|
|
|
|
|
|
sChunkSection *m_Sections[CHUNK_SECTION_NUM];
|
|
|
|
|
|
|
|
sChunkSection * Allocate() const;
|
2014-04-27 11:11:56 -04:00
|
|
|
void Free(sChunkSection * ptr) const;
|
2014-05-11 10:52:02 -04:00
|
|
|
|
|
|
|
void ZeroSection(sChunkSection * ptr) const;
|
2014-04-26 13:50:23 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|