diff options
author | Tycho <work.tycho+git@gmail.com> | 2014-04-27 15:35:27 +0200 |
---|---|---|
committer | Tycho <work.tycho+git@gmail.com> | 2014-04-27 15:35:27 +0200 |
commit | 57b8ee9163181920b634e475c781fe7764e11b98 (patch) | |
tree | 7d0675f8cda49a39b0b42eaaa928cfb66b57869a /src/ChunkDef.h | |
parent | Implemented Chunk Sparsing with segments (diff) | |
parent | Merge pull request #863 from mc-server/chunkysparsing (diff) | |
download | cuberite-57b8ee9163181920b634e475c781fe7764e11b98.tar cuberite-57b8ee9163181920b634e475c781fe7764e11b98.tar.gz cuberite-57b8ee9163181920b634e475c781fe7764e11b98.tar.bz2 cuberite-57b8ee9163181920b634e475c781fe7764e11b98.tar.lz cuberite-57b8ee9163181920b634e475c781fe7764e11b98.tar.xz cuberite-57b8ee9163181920b634e475c781fe7764e11b98.tar.zst cuberite-57b8ee9163181920b634e475c781fe7764e11b98.zip |
Diffstat (limited to 'src/ChunkDef.h')
-rw-r--r-- | src/ChunkDef.h | 118 |
1 files changed, 40 insertions, 78 deletions
diff --git a/src/ChunkDef.h b/src/ChunkDef.h index a5eccc9d6..d7b09dd3a 100644 --- a/src/ChunkDef.h +++ b/src/ChunkDef.h @@ -77,13 +77,19 @@ public: idx = x + Width * z // Need to verify this with the protocol spec, currently unknown! */ typedef EMCSBiome BiomeMap[Width * Width]; - + /// The type used for block type operations and storage, AXIS_ORDER ordering typedef BLOCKTYPE BlockTypes[NumBlocks]; - + /// The type used for block data in nibble format, AXIS_ORDER ordering typedef NIBBLETYPE BlockNibbles[NumBlocks / 2]; + /** The storage wrapper used for compressed blockdata residing in RAMz */ + typedef std::vector<BLOCKTYPE> COMPRESSED_BLOCKTYPE; + + /** The storage wrapper used for compressed nibbledata residing in RAMz */ + typedef std::vector<NIBBLETYPE> COMPRESSED_NIBBLETYPE; + /// Converts absolute block coords into relative (chunk + block) coords: inline static void AbsoluteToRelative(/* in-out */ int & a_X, int & a_Y, int & a_Z, /* out */ int & a_ChunkX, int & a_ChunkZ ) @@ -219,24 +225,13 @@ public: ASSERT((a_Z >= 0) && (a_Z <= Width)); a_BiomeMap[a_X + Width * a_Z] = a_Biome; } - - - static NIBBLETYPE GetNibble(const NIBBLETYPE * a_Buffer, int a_BlockIdx) - { - if ((a_BlockIdx > -1) && (a_BlockIdx < NumBlocks)) - { - return (a_Buffer[a_BlockIdx / 2] >> ((a_BlockIdx & 1) * 4)) & 0x0f; - } - ASSERT(!"cChunkDef::GetNibble(): index out of chunk range!"); - return 0; - } - static NIBBLETYPE GetNibble(const std::vector<NIBBLETYPE> & a_Buffer, int a_BlockIdx, bool a_IsSkyLightNibble = false) + static NIBBLETYPE GetNibble(const COMPRESSED_NIBBLETYPE & a_Buffer, int a_BlockIdx, bool a_IsSkyLightNibble = false) { if ((a_BlockIdx > -1) && (a_BlockIdx < NumBlocks)) { - if (a_Buffer.empty() || ((size_t)(a_BlockIdx / 2) > a_Buffer.size() - 1)) + if ((size_t)(a_BlockIdx / 2) >= a_Buffer.size()) { return (a_IsSkyLightNibble ? 0xff : 0); } @@ -245,69 +240,52 @@ public: ASSERT(!"cChunkDef::GetNibble(): index out of chunk range!"); return 0; } - - - static NIBBLETYPE GetNibble(const NIBBLETYPE * a_Buffer, int x, int y, int z) - { - if ((x < Width) && (x > -1) && (y < Height) && (y > -1) && (z < Width) && (z > -1)) - { - int Index = MakeIndexNoCheck(x, y, z); - return (a_Buffer[Index / 2] >> ((Index & 1) * 4)) & 0x0f; - } - ASSERT(!"cChunkDef::GetNibble(): coords out of chunk range!"); - return 0; - } - static NIBBLETYPE GetNibble(const std::vector<NIBBLETYPE> & a_Buffer, int x, int y, int z, bool a_IsSkyLightNibble = false) + static NIBBLETYPE GetNibble(const COMPRESSED_NIBBLETYPE & a_Buffer, int x, int y, int z, bool a_IsSkyLightNibble = false) { if ((x < Width) && (x > -1) && (y < Height) && (y > -1) && (z < Width) && (z > -1)) { int Index = MakeIndexNoCheck(x, y, z); - if (a_Buffer.empty() || ((size_t)(Index / 2) > a_Buffer.size() - 1)) + if ((size_t)(Index / 2) >= a_Buffer.size()) { return (a_IsSkyLightNibble ? 0xff : 0); } - return (a_Buffer[(size_t)(Index / 2)] >> ((Index & 1) * 4)) & 0x0f; + return ExpandNibble(a_Buffer, Index); } ASSERT(!"cChunkDef::GetNibble(): coords out of chunk range!"); return 0; } - static void SetNibble(NIBBLETYPE * a_Buffer, int a_BlockIdx, NIBBLETYPE a_Nibble) + static NIBBLETYPE GetNibble(const NIBBLETYPE * a_Buffer, int x, int y, int z) { - if ((a_BlockIdx < 0) || (a_BlockIdx >= NumBlocks)) + if ((x < Width) && (x > -1) && (y < Height) && (y > -1) && (z < Width) && (z > -1)) { - ASSERT(!"cChunkDef::SetNibble(): index out of range!"); - return; + int Index = MakeIndexNoCheck(x, y, z); + return (a_Buffer[(size_t)(Index / 2)] >> ((Index & 1) * 4)) & 0x0f; } - a_Buffer[a_BlockIdx / 2] = static_cast<NIBBLETYPE>( - (a_Buffer[a_BlockIdx / 2] & (0xf0 >> ((a_BlockIdx & 1) * 4))) | // The untouched nibble - ((a_Nibble & 0x0f) << ((a_BlockIdx & 1) * 4)) // The nibble being set - ); + ASSERT(!"cChunkDef::GetNibble(): coords out of chunk range!"); + return 0; } - static void SetNibble(std::vector<NIBBLETYPE> & a_Buffer, int a_BlockIdx, NIBBLETYPE a_Nibble) + static void SetNibble(COMPRESSED_NIBBLETYPE & a_Buffer, int a_BlockIdx, NIBBLETYPE a_Nibble) { if ((a_BlockIdx < 0) || (a_BlockIdx >= NumBlocks)) { ASSERT(!"cChunkDef::SetNibble(): index out of range!"); return; } - if (a_Buffer.empty() || ((size_t)(a_BlockIdx / 2) > a_Buffer.size() - 1)) + if ((size_t)(a_BlockIdx / 2) >= a_Buffer.size()) { a_Buffer.resize((size_t)((a_BlockIdx / 2) + 1)); } - a_Buffer[(size_t)(a_BlockIdx / 2)] = static_cast<NIBBLETYPE>( - (a_Buffer[a_BlockIdx / 2] & (0xf0 >> ((a_BlockIdx & 1) * 4))) | // The untouched nibble - ((a_Nibble & 0x0f) << ((a_BlockIdx & 1) * 4)) // The nibble being set - ); + a_Buffer[(size_t)(a_BlockIdx / 2)] = PackNibble(a_Buffer, a_BlockIdx, a_Nibble); } - - - static void SetNibble(NIBBLETYPE * a_Buffer, int x, int y, int z, NIBBLETYPE a_Nibble) + + + static void SetNibble(COMPRESSED_NIBBLETYPE & a_Buffer, int x, int y, int z, NIBBLETYPE a_Nibble) { if ( (x >= Width) || (x < 0) || @@ -320,48 +298,32 @@ public: } int Index = MakeIndexNoCheck(x, y, z); - a_Buffer[Index / 2] = static_cast<NIBBLETYPE>( - (a_Buffer[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble - ((a_Nibble & 0x0f) << ((Index & 1) * 4)) // The nibble being set - ); - } - - - static void SetNibble(std::vector<NIBBLETYPE> & a_Buffer, int x, int y, int z, NIBBLETYPE a_Nibble) - { - if ( - (x >= Width) || (x < 0) || - (y >= Height) || (y < 0) || - (z >= Width) || (z < 0) - ) - { - ASSERT(!"cChunkDef::SetNibble(): index out of range!"); - return; - } - - int Index = MakeIndexNoCheck(x, y, z); - if (a_Buffer.empty() || ((size_t)(Index / 2) > a_Buffer.size() - 1)) + if ((size_t)(Index / 2) >= a_Buffer.size()) { a_Buffer.resize((size_t)((Index / 2) + 1)); } - a_Buffer[(size_t)(Index / 2)] = static_cast<NIBBLETYPE>( - (a_Buffer[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble - ((a_Nibble & 0x0f) << ((Index & 1) * 4)) // The nibble being set - ); + a_Buffer[(size_t)(Index / 2)] = PackNibble(a_Buffer, Index, a_Nibble); } - inline static NIBBLETYPE GetNibble(const NIBBLETYPE * a_Buffer, const Vector3i & a_BlockPos) +private: + + + inline static NIBBLETYPE PackNibble(const COMPRESSED_NIBBLETYPE & a_Buffer, int a_Index, NIBBLETYPE a_Nibble) { - return GetNibble(a_Buffer, a_BlockPos.x, a_BlockPos.y, a_BlockPos.z ); + return static_cast<NIBBLETYPE>( + (a_Buffer[a_Index / 2] & (0xf0 >> ((a_Index & 1) * 4))) | // The untouched nibble + ((a_Nibble & 0x0f) << ((a_Index & 1) * 4)) // The nibble being set + ); } - - - inline static void SetNibble(NIBBLETYPE * a_Buffer, const Vector3i & a_BlockPos, NIBBLETYPE a_Value) + + + inline static NIBBLETYPE ExpandNibble(const COMPRESSED_NIBBLETYPE & a_Buffer, int a_Index) { - SetNibble( a_Buffer, a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, a_Value ); + return (a_Buffer[a_Index / 2] >> ((a_Index & 1) * 4)) & 0x0f; } + } ; |