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-21 14:58:48 -04:00
|
|
|
cChunkData cChunkData::Copy() const
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-21 14:58:48 -04:00
|
|
|
cChunkData copy;
|
2014-04-26 13:50:23 -04:00
|
|
|
for (int i = 0; i < CHUNK_SECTION_NUM; i++)
|
|
|
|
{
|
2014-05-21 16:18:14 -04:00
|
|
|
if(m_Sections[i] != NULL)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
copy.m_Sections[i] = Allocate();
|
|
|
|
*copy.m_Sections[i] = *m_Sections[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
void cChunkData::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length) const
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-04-27 10:38:16 -04:00
|
|
|
for (size_t i = 0; i < CHUNK_SECTION_NUM; i++)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16;
|
|
|
|
if (a_Idx > 0) a_Idx = a_Idx > length ? a_Idx - length : 0;
|
|
|
|
if (a_Idx == 0)
|
|
|
|
{
|
|
|
|
size_t tocopy = length > segment_length ? segment_length : length;
|
|
|
|
length -= tocopy;
|
2014-05-21 16:18:14 -04:00
|
|
|
if(m_Sections[i] != NULL)
|
2014-04-27 10:38:16 -04:00
|
|
|
{
|
|
|
|
memcpy(
|
|
|
|
&a_dest[i * segment_length],
|
|
|
|
&m_Sections[i]->m_BlockTypes,
|
2014-05-10 12:46:49 -04:00
|
|
|
sizeof(BLOCKTYPE) * tocopy
|
2014-04-27 10:38:16 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(
|
|
|
|
&a_dest[i * segment_length],
|
|
|
|
0,
|
2014-05-10 12:46:49 -04:00
|
|
|
sizeof(BLOCKTYPE) * tocopy
|
2014-04-27 10:38:16 -04:00
|
|
|
);
|
|
|
|
}
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
void cChunkData::CopyMeta(NIBBLETYPE * a_dest) const
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-04-27 10:38:16 -04:00
|
|
|
for (size_t i = 0; i < CHUNK_SECTION_NUM; i++)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
|
2014-05-21 16:18:14 -04:00
|
|
|
if(m_Sections[i] != NULL)
|
2014-04-27 10:38:16 -04:00
|
|
|
{
|
|
|
|
memcpy(
|
|
|
|
&a_dest[i * segment_length],
|
|
|
|
&m_Sections[i]->m_BlockMeta,
|
|
|
|
sizeof(NIBBLETYPE) * segment_length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(
|
|
|
|
&a_dest[i * segment_length],
|
|
|
|
0,
|
|
|
|
sizeof(BLOCKTYPE) * segment_length
|
|
|
|
);
|
|
|
|
}
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
void cChunkData::CopyLight(NIBBLETYPE * a_dest) const
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-04-27 10:38:16 -04:00
|
|
|
for (size_t i = 0; i < CHUNK_SECTION_NUM; i++)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
|
2014-05-21 16:18:14 -04:00
|
|
|
if(m_Sections[i] != NULL)
|
2014-04-27 10:38:16 -04:00
|
|
|
{
|
|
|
|
memcpy(
|
|
|
|
&a_dest[i * segment_length],
|
|
|
|
&m_Sections[i]->m_BlockLight,
|
|
|
|
sizeof(NIBBLETYPE) * segment_length
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(
|
|
|
|
&a_dest[i * segment_length],
|
|
|
|
0,
|
|
|
|
sizeof(BLOCKTYPE) * segment_length
|
|
|
|
);
|
|
|
|
}
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
void cChunkData::CopySkyLight(NIBBLETYPE * a_dest) const
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-04-27 10:38:16 -04:00
|
|
|
for (size_t i = 0; i < CHUNK_SECTION_NUM; i++)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
|
2014-05-21 16:18:14 -04:00
|
|
|
if(m_Sections[i] != NULL)
|
2014-04-27 10:38:16 -04:00
|
|
|
{
|
|
|
|
memcpy(
|
|
|
|
&a_dest[i * segment_length],
|
|
|
|
&m_Sections[i]->m_BlockSkyLight,
|
|
|
|
sizeof(NIBBLETYPE) * segment_length
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(
|
|
|
|
&a_dest[i * segment_length],
|
|
|
|
0xFF,
|
|
|
|
sizeof(BLOCKTYPE) * segment_length
|
|
|
|
);
|
|
|
|
}
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
void cChunkData::SetBlocks(const BLOCKTYPE * a_src)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-04-27 10:38:16 -04:00
|
|
|
for (size_t i = 0; i < CHUNK_SECTION_NUM; i++)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-10 12:46:49 -04:00
|
|
|
const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16;
|
2014-05-21 16:18:14 -04:00
|
|
|
if (m_Sections[i] != NULL)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-21 15:26:43 -04:00
|
|
|
memcpy(
|
|
|
|
&m_Sections[i]->m_BlockTypes,
|
|
|
|
&a_src[i * segment_length],
|
|
|
|
sizeof(BLOCKTYPE) * segment_length
|
|
|
|
);
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
2014-04-27 10:38:16 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t j = 0;
|
|
|
|
// do nothing whilst 0
|
|
|
|
for (; j < segment_length && a_src[i * segment_length + j] == 0; j++);
|
2014-05-11 14:24:09 -04:00
|
|
|
if (j != segment_length)
|
2014-04-27 10:38:16 -04:00
|
|
|
{
|
|
|
|
m_Sections[i] = Allocate();
|
|
|
|
memcpy(
|
|
|
|
&m_Sections[i]->m_BlockTypes,
|
|
|
|
&a_src[i * segment_length],
|
|
|
|
sizeof(BLOCKTYPE) * segment_length
|
|
|
|
);
|
2014-05-21 15:26:43 -04:00
|
|
|
memset(
|
|
|
|
m_Sections[i]->m_BlockMeta,
|
|
|
|
0x00,
|
|
|
|
sizeof(m_Sections[i]->m_BlockMeta)
|
|
|
|
);
|
|
|
|
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)
|
|
|
|
);
|
2014-04-27 10:38:16 -04:00
|
|
|
}
|
2014-04-27 11:11:56 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Free(m_Sections[i]);
|
|
|
|
m_Sections[i] = 0;
|
|
|
|
}
|
2014-04-27 10:38:16 -04:00
|
|
|
}
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
void cChunkData::SetMeta(const NIBBLETYPE * a_src)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-04-27 10:38:16 -04:00
|
|
|
for (size_t i = 0; i < CHUNK_SECTION_NUM; i++)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
|
2014-05-21 16:18:14 -04:00
|
|
|
if (m_Sections[i] != NULL)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-21 15:26:43 -04:00
|
|
|
memcpy(
|
|
|
|
&m_Sections[i]->m_BlockMeta,
|
|
|
|
&a_src[i * segment_length],
|
|
|
|
sizeof(NIBBLETYPE) * segment_length
|
|
|
|
);
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
2014-04-27 10:38:16 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t j = 0;
|
|
|
|
// do nothing whilst 0
|
|
|
|
for (; j < segment_length && a_src[i * segment_length + j] == 0; j++);
|
2014-05-11 14:24:09 -04:00
|
|
|
if (j != segment_length)
|
2014-04-27 10:38:16 -04:00
|
|
|
{
|
|
|
|
m_Sections[i] = Allocate();
|
|
|
|
memcpy(
|
2014-04-27 11:11:56 -04:00
|
|
|
&m_Sections[i]->m_BlockMeta,
|
2014-04-27 10:38:16 -04:00
|
|
|
&a_src[i * segment_length],
|
|
|
|
sizeof(BLOCKTYPE) * segment_length
|
|
|
|
);
|
2014-05-21 15:26:43 -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)
|
|
|
|
);
|
2014-04-27 10:38:16 -04:00
|
|
|
}
|
2014-04-27 11:11:56 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Free(m_Sections[i]);
|
|
|
|
m_Sections[i] = 0;
|
|
|
|
}
|
2014-04-27 10:38:16 -04:00
|
|
|
}
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
void cChunkData::SetLight(const NIBBLETYPE * a_src)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-04-27 11:11:56 -04:00
|
|
|
if (!a_src) return;
|
2014-04-27 10:38:16 -04:00
|
|
|
for (size_t i = 0; i < CHUNK_SECTION_NUM; i++)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
|
2014-05-21 16:18:14 -04:00
|
|
|
if (m_Sections[i] != NULL)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-21 15:26:43 -04:00
|
|
|
memcpy(
|
|
|
|
&m_Sections[i]->m_BlockLight,
|
|
|
|
&a_src[i * segment_length],
|
|
|
|
sizeof(NIBBLETYPE) * segment_length
|
|
|
|
);
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
2014-04-27 10:38:16 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t j = 0;
|
|
|
|
// do nothing whilst 0
|
|
|
|
for (; j < segment_length && a_src[i * segment_length + j] == 0; j++);
|
2014-05-11 14:24:09 -04:00
|
|
|
if (j != segment_length)
|
2014-04-27 10:38:16 -04:00
|
|
|
{
|
|
|
|
m_Sections[i] = Allocate();
|
|
|
|
memcpy(
|
2014-04-27 11:11:56 -04:00
|
|
|
&m_Sections[i]->m_BlockLight,
|
2014-04-27 10:38:16 -04:00
|
|
|
&a_src[i * segment_length],
|
|
|
|
sizeof(BLOCKTYPE) * segment_length
|
|
|
|
);
|
2014-05-21 15:26:43 -04:00
|
|
|
memset(
|
|
|
|
m_Sections[i]->m_BlockTypes,
|
|
|
|
0x00,
|
|
|
|
sizeof(m_Sections[i]->m_BlockTypes)
|
|
|
|
);
|
|
|
|
memset(
|
|
|
|
m_Sections[i]->m_BlockMeta,
|
|
|
|
0x00,
|
|
|
|
sizeof(m_Sections[i]->m_BlockMeta)
|
|
|
|
);
|
|
|
|
memset(
|
|
|
|
m_Sections[i]->m_BlockSkyLight,
|
|
|
|
0xFF,
|
|
|
|
sizeof(m_Sections[i]->m_BlockSkyLight)
|
|
|
|
);
|
2014-04-27 10:38:16 -04:00
|
|
|
}
|
2014-04-27 11:11:56 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Free(m_Sections[i]);
|
|
|
|
m_Sections[i] = 0;
|
|
|
|
}
|
2014-04-27 10:38:16 -04:00
|
|
|
}
|
|
|
|
}
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
void cChunkData::SetSkyLight (const NIBBLETYPE * a_src)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-04-27 11:11:56 -04:00
|
|
|
if (!a_src) return;
|
2014-04-27 10:38:16 -04:00
|
|
|
for (size_t i = 0; i < CHUNK_SECTION_NUM; i++)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
|
2014-05-21 16:18:14 -04:00
|
|
|
if (m_Sections[i] != NULL)
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
2014-05-21 15:26:43 -04:00
|
|
|
memcpy(
|
|
|
|
&m_Sections[i]->m_BlockSkyLight,
|
|
|
|
&a_src[i * segment_length],
|
|
|
|
sizeof(NIBBLETYPE) * segment_length
|
|
|
|
);
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
2014-04-27 10:38:16 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t j = 0;
|
|
|
|
// do nothing whilst 0
|
|
|
|
for (; j < segment_length && a_src[i * segment_length + j] == 0xFF; j++);
|
2014-05-11 14:24:09 -04:00
|
|
|
if (j != segment_length)
|
2014-04-27 10:38:16 -04:00
|
|
|
{
|
|
|
|
m_Sections[i] = Allocate();
|
|
|
|
memcpy(
|
2014-04-27 11:11:56 -04:00
|
|
|
&m_Sections[i]->m_BlockSkyLight,
|
2014-04-27 10:38:16 -04:00
|
|
|
&a_src[i * segment_length],
|
|
|
|
sizeof(BLOCKTYPE) * segment_length
|
|
|
|
);
|
2014-05-21 15:26:43 -04:00
|
|
|
memset(
|
|
|
|
m_Sections[i]->m_BlockTypes,
|
|
|
|
0x00,
|
|
|
|
sizeof(m_Sections[i]->m_BlockTypes)
|
|
|
|
);
|
|
|
|
memset(
|
|
|
|
m_Sections[i]->m_BlockMeta,
|
|
|
|
0x00,
|
|
|
|
sizeof(m_Sections[i]->m_BlockMeta)
|
|
|
|
);
|
|
|
|
memset(
|
|
|
|
m_Sections[i]->m_BlockLight,
|
|
|
|
0x00,
|
|
|
|
sizeof(m_Sections[i]->m_BlockLight)
|
|
|
|
);
|
2014-04-27 10:38:16 -04:00
|
|
|
}
|
2014-04-27 11:11:56 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Free(m_Sections[i]);
|
|
|
|
m_Sections[i] = 0;
|
|
|
|
}
|
2014-04-27 10:38:16 -04:00
|
|
|
}
|
|
|
|
}
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
cChunkData::sChunkSection * cChunkData::Allocate() const
|
2014-04-26 13:50:23 -04:00
|
|
|
{
|
|
|
|
// TODO: use a allocation pool
|
2014-05-21 14:58:48 -04:00
|
|
|
return new cChunkData::sChunkSection;
|
2014-04-26 13:50:23 -04:00
|
|
|
}
|
2014-04-27 11:11:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
void cChunkData::Free(cChunkData::sChunkSection * ptr) const
|
2014-04-27 11:11:56 -04:00
|
|
|
{
|
|
|
|
delete ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-21 14:58:48 -04:00
|
|
|
void cChunkData::ZeroSection(cChunkData::sChunkSection * ptr) const
|
2014-05-11 10:52:02 -04:00
|
|
|
{
|
2014-05-21 15:26:43 -04:00
|
|
|
memset(
|
|
|
|
ptr->m_BlockTypes,
|
|
|
|
0x00,
|
|
|
|
sizeof(ptr->m_BlockTypes)
|
|
|
|
);
|
|
|
|
memset(
|
|
|
|
ptr->m_BlockMeta,
|
|
|
|
0x00,
|
|
|
|
sizeof(ptr->m_BlockMeta)
|
|
|
|
);
|
|
|
|
memset(
|
|
|
|
ptr->m_BlockLight,
|
|
|
|
0x00,
|
|
|
|
sizeof(ptr->m_BlockLight)
|
|
|
|
);
|
|
|
|
memset(
|
|
|
|
ptr->m_BlockSkyLight,
|
|
|
|
0xFF,
|
|
|
|
sizeof(ptr->m_BlockSkyLight)
|
|
|
|
);
|
2014-05-11 10:52:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|