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

134 lines
3.9 KiB
C
Raw Normal View History

2014-05-29 12:25:08 -04:00
// ChunkData.h
// Declares the cChunkData class that represents the block's type, meta, blocklight and skylight storage for a chunk
#pragma once
#include <cstring>
#include "ChunkDef.h"
#include "AllocationPool.h"
2014-05-24 08:33:40 -04:00
#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
{
private:
static const int SectionHeight = 16;
static const size_t NumSections = (cChunkDef::Height / SectionHeight);
static const size_t SectionBlockCount = SectionHeight * cChunkDef::Width * cChunkDef::Width;
public:
struct sChunkSection;
cChunkData(cAllocationPool<cChunkData::sChunkSection> & a_Pool);
2014-05-24 08:37:25 -04:00
~cChunkData();
2016-02-05 16:45:45 -05:00
#if __cplusplus < 201103L
2014-05-24 08:33:40 -04:00
// auto_ptr style interface for memory management
2014-05-28 16:40:19 -04:00
cChunkData(const cChunkData & a_Other);
cChunkData & operator =(const cChunkData & a_Other);
#else
2014-05-24 08:33:40 -04:00
// unique_ptr style interface for memory management
2014-05-28 16:40:19 -04:00
cChunkData(cChunkData && a_Other);
cChunkData & operator =(cChunkData && a_ther);
#endif
2014-05-24 08:37:25 -04:00
BLOCKTYPE GetBlock(int a_X, int a_Y, int a_Z) const;
void SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_Block);
2014-05-24 08:37:25 -04:00
NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const;
bool SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble);
2016-02-05 16:45:45 -05:00
2014-05-24 08:37:25 -04:00
NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const;
2016-02-05 16:45:45 -05:00
2014-05-24 08:37:25 -04:00
NIBBLETYPE GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const;
2016-02-05 16:45:45 -05:00
2014-05-29 12:25:08 -04:00
/** Creates a (deep) copy of self. */
2014-05-28 05:35:55 -04:00
cChunkData Copy(void) const;
2014-05-29 12:25:08 -04:00
/** Copies the blocktype data into the specified flat array.
Optionally, only a part of the data is copied, as specified by the a_Idx and a_Length parameters. */
void CopyBlockTypes(BLOCKTYPE * a_Dest, size_t a_Idx = 0, size_t a_Length = cChunkDef::NumBlocks) const;
2014-05-29 12:25:08 -04:00
/** Copies the metadata into the specified flat array. */
void CopyMetas(NIBBLETYPE * a_Dest) const;
2014-05-29 12:25:08 -04:00
/** Copies the block light data into the specified flat array. */
2014-05-28 16:40:19 -04:00
void CopyBlockLight(NIBBLETYPE * a_Dest) const;
2014-05-29 12:25:08 -04:00
/** Copies the skylight data into the specified flat array. */
2014-05-28 16:40:19 -04:00
void CopySkyLight (NIBBLETYPE * a_Dest) const;
2016-02-05 16:45:45 -05:00
2014-05-29 12:25:08 -04:00
/** Copies the blocktype data from the specified flat array into the internal representation.
Allocates sections that are needed for the operation.
Requires that a_Src is a valid pointer. */
void SetBlockTypes(const BLOCKTYPE * a_Src);
2014-05-29 12:25:08 -04:00
/** Copies the metadata from the specified flat array into the internal representation.
Allocates sectios that are needed for the operation.
Requires that a_Src is a valid pointer. */
void SetMetas(const NIBBLETYPE * a_Src);
2014-05-29 12:25:08 -04:00
/** Copies the blocklight data from the specified flat array into the internal representation.
Allocates sectios that are needed for the operation.
2014-10-20 16:55:07 -04:00
Allows a_Src to be nullptr, in which case it doesn't do anything. */
2014-05-29 12:25:08 -04:00
void SetBlockLight(const NIBBLETYPE * a_Src);
/** Copies the skylight data from the specified flat array into the internal representation.
Allocates sectios that are needed for the operation.
2014-10-20 16:55:07 -04:00
Allows a_Src to be nullptr, in which case it doesn't do anything. */
2014-05-29 12:25:08 -04:00
void SetSkyLight(const NIBBLETYPE * a_Src);
struct sChunkSection
{
BLOCKTYPE m_BlockTypes [SectionHeight * 16 * 16] ;
NIBBLETYPE m_BlockMetas [SectionHeight * 16 * 16 / 2];
NIBBLETYPE m_BlockLight [SectionHeight * 16 * 16 / 2];
NIBBLETYPE m_BlockSkyLight[SectionHeight * 16 * 16 / 2];
};
2016-02-05 16:45:45 -05:00
private:
#if __cplusplus < 201103L
// auto_ptr style interface for memory management
2014-05-28 05:35:55 -04:00
mutable bool m_IsOwner;
#endif
2014-05-29 12:25:08 -04:00
sChunkSection * m_Sections[NumSections];
cAllocationPool<cChunkData::sChunkSection> & m_Pool;
2016-02-05 16:45:45 -05:00
2014-05-28 16:40:19 -04:00
/** Allocates a new section. Entry-point to custom allocators. */
sChunkSection * Allocate(void);
2014-05-28 16:40:19 -04:00
/** Frees the specified section, previously allocated using Allocate().
2014-10-20 16:55:07 -04:00
Note that a_Section may be nullptr. */
void Free(sChunkSection * a_Section);
2016-02-05 16:45:45 -05:00
2014-05-28 16:40:19 -04:00
/** Sets the data in the specified section to their default values. */
void ZeroSection(sChunkSection * a_Section) const;
};