From 945631ba06fcba24c84017f36544667299c591fb Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 23 Mar 2014 14:34:19 +0000 Subject: Sort of implementation of chunk sparsing Issues: * Chunks are flipped * Slow/inefficient/badly coded * Only blocktypes are 'compressed' --- src/Chunk.cpp | 109 +++++++++++++++++++++++++++++++++++++++++++++++++--------- src/Chunk.h | 2 +- 2 files changed, 93 insertions(+), 18 deletions(-) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 957d7d575..adbe94cd0 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -239,8 +239,18 @@ void cChunk::MarkLoadFailed(void) void cChunk::GetAllData(cChunkDataCallback & a_Callback) { a_Callback.HeightMap (&m_HeightMap); - a_Callback.BiomeData (&m_BiomeMap); - a_Callback.BlockTypes (m_BlockTypes); + a_Callback.BiomeData(&m_BiomeMap); + + std::vector Blocks; + Blocks.reserve(cChunkDef::NumBlocks); + for (std::vector>::const_iterator itr = m_BlockTypes.begin(); itr != m_BlockTypes.end(); ++itr) + { + for (std::vector::const_iterator dataitr = itr->begin(); dataitr != itr->end(); ++dataitr) + { + Blocks.push_back(*dataitr); + } + } + a_Callback.BlockTypes (Blocks.data()); a_Callback.BlockMeta (m_BlockMeta); a_Callback.LightIsValid (m_IsLightValid); a_Callback.BlockLight (m_BlockLight); @@ -277,8 +287,42 @@ void cChunk::SetAllData( { memcpy(m_HeightMap, a_HeightMap, sizeof(m_HeightMap)); } + + bool FoundNonAir = false; + m_BlockTypes.clear(); + for (int y = cChunkDef::Height - 1; y >= 0; y--) + { + if (!FoundNonAir) + { + for (int x = 0; x < cChunkDef::Width; x++) + { + for (int z = 0; z < cChunkDef::Width; z++) + { + int Index = cChunkDef::MakeIndexNoCheck(x, y, z); + + if (!FoundNonAir && (a_BlockTypes[Index] != E_BLOCK_AIR)) + { + FoundNonAir = true; + } + } + } + } + + if (FoundNonAir) + { + std::vector Blocks; + for (int x = 0; x < cChunkDef::Width; x++) + { + for (int z = 0; z < cChunkDef::Width; z++) + { + int Index = cChunkDef::MakeIndexNoCheck(x, y, z); + Blocks.insert(Blocks.begin(), a_BlockTypes[Index]); + } + } + m_BlockTypes.insert(m_BlockTypes.begin(), Blocks); + } + } // for y - memcpy(m_BlockTypes, a_BlockTypes, sizeof(m_BlockTypes)); memcpy(m_BlockMeta, a_BlockMeta, sizeof(m_BlockMeta)); if (a_BlockLight != NULL) { @@ -343,7 +387,17 @@ void cChunk::SetLight( void cChunk::GetBlockTypes(BLOCKTYPE * a_BlockTypes) { - memcpy(a_BlockTypes, m_BlockTypes, NumBlocks); + std::vector Blocks; + Blocks.reserve(cChunkDef::NumBlocks); + for (std::vector>::const_iterator itr = m_BlockTypes.begin(); itr != m_BlockTypes.end(); ++itr) + { + for (std::vector::const_iterator dataitr = itr->begin(); dataitr != itr->end(); ++dataitr) + { + Blocks.push_back(*dataitr); + } + } + + memcpy(a_BlockTypes, Blocks.data(), NumBlocks); } @@ -630,7 +684,7 @@ void cChunk::Tick(float a_Dt) void cChunk::TickBlock(int a_RelX, int a_RelY, int a_RelZ) { unsigned Index = MakeIndex(a_RelX, a_RelY, a_RelZ); - cBlockHandler * Handler = BlockHandler(m_BlockTypes[Index]); + cBlockHandler * Handler = BlockHandler(GetBlock(Index)); ASSERT(Handler != NULL); // Happenned on server restart, FS #243 cChunkInterface ChunkInterface(this->GetWorld()->GetChunkMap()); cBlockInServerPluginInterface PluginInterface(*this->GetWorld()); @@ -811,7 +865,7 @@ void cChunk::TickBlocks(void) } unsigned int Index = MakeIndexNoCheck(m_BlockTickX, m_BlockTickY, m_BlockTickZ); - cBlockHandler * Handler = BlockHandler(m_BlockTypes[Index]); + cBlockHandler * Handler = BlockHandler(GetBlock(Index)); ASSERT(Handler != NULL); // Happenned on server restart, FS #243 Handler->OnUpdate(ChunkInterface, *this->GetWorld(), PluginInterface, *this, m_BlockTickX, m_BlockTickY, m_BlockTickZ); } // for i - tickblocks @@ -1296,7 +1350,7 @@ void cChunk::CreateBlockEntities(void) { for (int y = 0; y < Height; y++) { - BLOCKTYPE BlockType = cChunkDef::GetBlock(m_BlockTypes, x, y, z); + BLOCKTYPE BlockType = GetBlock(x, y, z); switch (BlockType) { case E_BLOCK_CHEST: @@ -1348,7 +1402,7 @@ void cChunk::WakeUpSimulators(void) int BlockZ = z + BaseZ; for (int y = GetHeight(x, z); y >= 0; y--) { - BLOCKTYPE Block = cChunkDef::GetBlock(m_BlockTypes, x, y, z); + BLOCKTYPE Block = GetBlock(x, y, z); // The redstone sim takes multiple blocks, use the inbuilt checker if (RedstoneSimulator->IsAllowedBlock(Block)) @@ -1392,7 +1446,7 @@ void cChunk::CalculateHeightmap() for (int y = Height - 1; y > -1; y--) { int index = MakeIndex( x, y, z ); - if (m_BlockTypes[index] != E_BLOCK_AIR) + if (GetBlock(index) != E_BLOCK_AIR) { m_HeightMap[x + z * Width] = (unsigned char)y; break; @@ -1515,7 +1569,7 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT ASSERT(IsValid()); const int index = MakeIndexNoCheck(a_RelX, a_RelY, a_RelZ); - const BLOCKTYPE OldBlockType = cChunkDef::GetBlock(m_BlockTypes, index); + const BLOCKTYPE OldBlockType = GetBlock(index); const BLOCKTYPE OldBlockMeta = GetNibble(m_BlockMeta, index); if ((OldBlockType == a_BlockType) && (OldBlockMeta == a_BlockMeta)) { @@ -1524,7 +1578,20 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT MarkDirty(); - m_BlockTypes[index] = a_BlockType; + int Layer = (int)index / (cChunkDef::Width * cChunkDef::Width); + int SubBlock = index % ((cChunkDef::Width * cChunkDef::Width) - 1); + + if (m_BlockTypes.empty() || (Layer > m_BlockTypes.size() - 1) /* Vector starts from zero, .size() starts from 1 */) + { + m_BlockTypes.reserve(Layer - ((int)m_BlockTypes.size() - 1)); + std::vector EmptyBlocks(cChunkDef::Width * cChunkDef::Width); + for (int lyr = ((int)m_BlockTypes.size() - 1); lyr <= Layer; ++lyr) + { + m_BlockTypes.push_back(EmptyBlocks); + } + } + + m_BlockTypes[Layer][SubBlock] = a_BlockType; // The client doesn't need to distinguish between stationary and nonstationary fluids: if ( @@ -1563,7 +1630,7 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT { for (int y = a_RelY - 1; y > 0; --y) { - if (m_BlockTypes[MakeIndexNoCheck(a_RelX, y, a_RelZ)] != E_BLOCK_AIR) + if (GetBlock(MakeIndexNoCheck(a_RelX, y, a_RelZ)) != E_BLOCK_AIR) { m_HeightMap[a_RelX + a_RelZ * Width] = (unsigned char)y; break; @@ -2450,7 +2517,7 @@ BLOCKTYPE cChunk::GetBlock(int a_RelX, int a_RelY, int a_RelZ) const return 0; // Clip } - return m_BlockTypes[MakeIndexNoCheck(a_RelX, a_RelY, a_RelZ)]; + return GetBlock(MakeIndexNoCheck(a_RelX, a_RelY, a_RelZ)); } @@ -2464,8 +2531,16 @@ BLOCKTYPE cChunk::GetBlock(int a_BlockIdx) const ASSERT(!"GetBlock(idx) out of bounds!"); return 0; } - - return m_BlockTypes[ a_BlockIdx ]; + + int Layer = (int)a_BlockIdx / (cChunkDef::Width * cChunkDef::Width); + int SubBlock = a_BlockIdx % ((cChunkDef::Width * cChunkDef::Width) - 1); + + if (m_BlockTypes.empty() || (Layer > m_BlockTypes.size() - 1) /* Vector starts from zero, .size() starts from 1 */) + { + return E_BLOCK_AIR; + } + + return m_BlockTypes[Layer][SubBlock]; } @@ -2475,7 +2550,7 @@ BLOCKTYPE cChunk::GetBlock(int a_BlockIdx) const void cChunk::GetBlockTypeMeta(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta) { int Idx = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY, a_RelZ); - a_BlockType = cChunkDef::GetBlock (m_BlockTypes, Idx); + a_BlockType = GetBlock(Idx); a_BlockMeta = cChunkDef::GetNibble(m_BlockMeta, Idx); } @@ -2486,7 +2561,7 @@ void cChunk::GetBlockTypeMeta(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_ void cChunk::GetBlockInfo(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_Meta, NIBBLETYPE & a_SkyLight, NIBBLETYPE & a_BlockLight) { int Idx = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY, a_RelZ); - a_BlockType = cChunkDef::GetBlock (m_BlockTypes, Idx); + a_BlockType = GetBlock(Idx); a_Meta = cChunkDef::GetNibble(m_BlockMeta, Idx); a_SkyLight = cChunkDef::GetNibble(m_BlockSkyLight, Idx); a_BlockLight = cChunkDef::GetNibble(m_BlockLight, Idx); diff --git a/src/Chunk.h b/src/Chunk.h index b3fa563cc..1b83bc540 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -421,7 +421,7 @@ private: cChunkMap * m_ChunkMap; // TODO: Make these pointers and don't allocate what isn't needed - BLOCKTYPE m_BlockTypes [cChunkDef::NumBlocks]; + std::vector> m_BlockTypes; NIBBLETYPE m_BlockMeta [cChunkDef::NumBlocks / 2]; NIBBLETYPE m_BlockLight [cChunkDef::NumBlocks / 2]; NIBBLETYPE m_BlockSkyLight[cChunkDef::NumBlocks / 2]; -- cgit v1.2.3 From 357411a48978d6a672e4694edfa1ff87d57cfcfb Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Wed, 2 Apr 2014 22:53:03 +0100 Subject: Performance improvements and chunk flipping fixed --- src/Chunk.cpp | 62 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index adbe94cd0..a73bf7ca9 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -239,17 +239,15 @@ void cChunk::MarkLoadFailed(void) void cChunk::GetAllData(cChunkDataCallback & a_Callback) { a_Callback.HeightMap (&m_HeightMap); - a_Callback.BiomeData(&m_BiomeMap); + a_Callback.BiomeData (&m_BiomeMap); std::vector Blocks; Blocks.reserve(cChunkDef::NumBlocks); for (std::vector>::const_iterator itr = m_BlockTypes.begin(); itr != m_BlockTypes.end(); ++itr) { - for (std::vector::const_iterator dataitr = itr->begin(); dataitr != itr->end(); ++dataitr) - { - Blocks.push_back(*dataitr); - } + Blocks.insert(Blocks.end(), itr->begin(), itr->end()); } + a_Callback.BlockTypes (Blocks.data()); a_Callback.BlockMeta (m_BlockMeta); a_Callback.LightIsValid (m_IsLightValid); @@ -272,56 +270,63 @@ void cChunk::GetAllData(cChunkDataCallback & a_Callback) void cChunk::SetAllData( - const BLOCKTYPE * a_BlockTypes, + const BLOCKTYPE * a_BlockTypes, const NIBBLETYPE * a_BlockMeta, const NIBBLETYPE * a_BlockLight, const NIBBLETYPE * a_BlockSkyLight, const HeightMap * a_HeightMap, const BiomeMap & a_BiomeMap, cBlockEntityList & a_BlockEntities -) + ) { memcpy(m_BiomeMap, a_BiomeMap, sizeof(m_BiomeMap)); - + if (a_HeightMap != NULL) { memcpy(m_HeightMap, a_HeightMap, sizeof(m_HeightMap)); } bool FoundNonAir = false; + int PosYWhereNonEmptyStarts = 0; m_BlockTypes.clear(); - for (int y = cChunkDef::Height - 1; y >= 0; y--) + m_BlockTypes.reserve(Height / 2); + + for (int y = Height; y >= 0; y--) { - if (!FoundNonAir) + for (int z = 0; z < Width; z++) { - for (int x = 0; x < cChunkDef::Width; x++) + for (int x = 0; x < Width; x++) { - for (int z = 0; z < cChunkDef::Width; z++) - { - int Index = cChunkDef::MakeIndexNoCheck(x, y, z); + int Index = MakeIndexNoCheck(x, y, z); - if (!FoundNonAir && (a_BlockTypes[Index] != E_BLOCK_AIR)) - { - FoundNonAir = true; - } + if (!FoundNonAir && (a_BlockTypes[Index] != E_BLOCK_AIR)) + { + FoundNonAir = true; + PosYWhereNonEmptyStarts = y; + goto foundair; } } } + } - if (FoundNonAir) +foundair: + if (FoundNonAir) + { + for (int y = 0; y <= PosYWhereNonEmptyStarts; y++) { std::vector Blocks; - for (int x = 0; x < cChunkDef::Width; x++) + Blocks.reserve(Width * Width); + for (int z = 0; z < Width; z++) { - for (int z = 0; z < cChunkDef::Width; z++) + for (int x = 0; x < Width; x++) { - int Index = cChunkDef::MakeIndexNoCheck(x, y, z); - Blocks.insert(Blocks.begin(), a_BlockTypes[Index]); + int Index = MakeIndexNoCheck(x, y, z); + Blocks.push_back(a_BlockTypes[Index]); } } - m_BlockTypes.insert(m_BlockTypes.begin(), Blocks); - } - } // for y + m_BlockTypes.push_back(Blocks); + } // for y + } memcpy(m_BlockMeta, a_BlockMeta, sizeof(m_BlockMeta)); if (a_BlockLight != NULL) @@ -391,10 +396,7 @@ void cChunk::GetBlockTypes(BLOCKTYPE * a_BlockTypes) Blocks.reserve(cChunkDef::NumBlocks); for (std::vector>::const_iterator itr = m_BlockTypes.begin(); itr != m_BlockTypes.end(); ++itr) { - for (std::vector::const_iterator dataitr = itr->begin(); dataitr != itr->end(); ++dataitr) - { - Blocks.push_back(*dataitr); - } + Blocks.insert(Blocks.end(), itr->begin(), itr->end()); } memcpy(a_BlockTypes, Blocks.data(), NumBlocks); -- cgit v1.2.3 From d5faf5a38ebe164d705cc67baaa4b3c9d7eb6159 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Thu, 3 Apr 2014 19:17:04 +0100 Subject: Fixed some bugs * Fixed undefined behaviour * Fixed compression failure --- src/Chunk.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index a73bf7ca9..d80e93702 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -242,11 +242,12 @@ void cChunk::GetAllData(cChunkDataCallback & a_Callback) a_Callback.BiomeData (&m_BiomeMap); std::vector Blocks; - Blocks.reserve(cChunkDef::NumBlocks); + Blocks.reserve(NumBlocks); for (std::vector>::const_iterator itr = m_BlockTypes.begin(); itr != m_BlockTypes.end(); ++itr) { Blocks.insert(Blocks.end(), itr->begin(), itr->end()); } + Blocks.resize(NumBlocks); a_Callback.BlockTypes (Blocks.data()); a_Callback.BlockMeta (m_BlockMeta); @@ -291,7 +292,7 @@ void cChunk::SetAllData( m_BlockTypes.clear(); m_BlockTypes.reserve(Height / 2); - for (int y = Height; y >= 0; y--) + for (int y = Height - 1; y >= 0; y--) { for (int z = 0; z < Width; z++) { @@ -393,11 +394,12 @@ void cChunk::SetLight( void cChunk::GetBlockTypes(BLOCKTYPE * a_BlockTypes) { std::vector Blocks; - Blocks.reserve(cChunkDef::NumBlocks); + Blocks.reserve(NumBlocks); for (std::vector>::const_iterator itr = m_BlockTypes.begin(); itr != m_BlockTypes.end(); ++itr) { Blocks.insert(Blocks.end(), itr->begin(), itr->end()); } + Blocks.resize(NumBlocks); memcpy(a_BlockTypes, Blocks.data(), NumBlocks); } -- cgit v1.2.3 From 15a0ceec2687c04191c41f7b103c26aeb98705ca Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 4 Apr 2014 13:01:05 +0100 Subject: Speed and memory improvements * Changed array to be continuous, so no more layer splitting --- src/Chunk.cpp | 75 +++++++++++------------------------------------------------ src/Chunk.h | 2 +- 2 files changed, 15 insertions(+), 62 deletions(-) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 6e6b7ed20..c2caafe45 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -241,12 +241,7 @@ void cChunk::GetAllData(cChunkDataCallback & a_Callback) a_Callback.HeightMap (&m_HeightMap); a_Callback.BiomeData (&m_BiomeMap); - std::vector Blocks; - Blocks.reserve(NumBlocks); - for (std::vector>::const_iterator itr = m_BlockTypes.begin(); itr != m_BlockTypes.end(); ++itr) - { - Blocks.insert(Blocks.end(), itr->begin(), itr->end()); - } + std::vector Blocks = m_BlockTypes; Blocks.resize(NumBlocks); a_Callback.BlockTypes (Blocks.data()); @@ -288,46 +283,20 @@ void cChunk::SetAllData( } bool FoundNonAir = false; - int PosYWhereNonEmptyStarts = 0; + int IdxWhereNonEmptyStarts = 0; m_BlockTypes.clear(); - m_BlockTypes.reserve(Height / 2); - for (int y = Height - 1; y >= 0; y--) + for (int Idx = NumBlocks - 1; Idx >= 0; Idx--) { - for (int z = 0; z < Width; z++) + if (a_BlockTypes[Idx] != E_BLOCK_AIR) { - for (int x = 0; x < Width; x++) - { - int Index = MakeIndexNoCheck(x, y, z); - - if (!FoundNonAir && (a_BlockTypes[Index] != E_BLOCK_AIR)) - { - FoundNonAir = true; - PosYWhereNonEmptyStarts = y; - goto foundair; - } - } + FoundNonAir = true; + IdxWhereNonEmptyStarts = Idx; + break; } } -foundair: - if (FoundNonAir) - { - for (int y = 0; y <= PosYWhereNonEmptyStarts; y++) - { - std::vector Blocks; - Blocks.reserve(Width * Width); - for (int z = 0; z < Width; z++) - { - for (int x = 0; x < Width; x++) - { - int Index = MakeIndexNoCheck(x, y, z); - Blocks.push_back(a_BlockTypes[Index]); - } - } - m_BlockTypes.push_back(Blocks); - } // for y - } + m_BlockTypes.insert(m_BlockTypes.end(), &a_BlockTypes[0], &a_BlockTypes[IdxWhereNonEmptyStarts + 1]); memcpy(m_BlockMeta, a_BlockMeta, sizeof(m_BlockMeta)); if (a_BlockLight != NULL) @@ -393,12 +362,7 @@ void cChunk::SetLight( void cChunk::GetBlockTypes(BLOCKTYPE * a_BlockTypes) { - std::vector Blocks; - Blocks.reserve(NumBlocks); - for (std::vector>::const_iterator itr = m_BlockTypes.begin(); itr != m_BlockTypes.end(); ++itr) - { - Blocks.insert(Blocks.end(), itr->begin(), itr->end()); - } + std::vector Blocks = m_BlockTypes; Blocks.resize(NumBlocks); memcpy(a_BlockTypes, Blocks.data(), NumBlocks); @@ -1581,21 +1545,13 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT } MarkDirty(); - - int Layer = (int)index / (cChunkDef::Width * cChunkDef::Width); - int SubBlock = index % ((cChunkDef::Width * cChunkDef::Width) - 1); - if (m_BlockTypes.empty() || (Layer > m_BlockTypes.size() - 1) /* Vector starts from zero, .size() starts from 1 */) + if (m_BlockTypes.empty() || (index > m_BlockTypes.size() - 1) /* Vector starts from zero, .size() starts from 1 */) { - m_BlockTypes.reserve(Layer - ((int)m_BlockTypes.size() - 1)); - std::vector EmptyBlocks(cChunkDef::Width * cChunkDef::Width); - for (int lyr = ((int)m_BlockTypes.size() - 1); lyr <= Layer; ++lyr) - { - m_BlockTypes.push_back(EmptyBlocks); - } + m_BlockTypes.resize(index); } - m_BlockTypes[Layer][SubBlock] = a_BlockType; + m_BlockTypes[index] = a_BlockType; // The client doesn't need to distinguish between stationary and nonstationary fluids: if ( @@ -2536,15 +2492,12 @@ BLOCKTYPE cChunk::GetBlock(int a_BlockIdx) const return 0; } - int Layer = (int)a_BlockIdx / (cChunkDef::Width * cChunkDef::Width); - int SubBlock = a_BlockIdx % ((cChunkDef::Width * cChunkDef::Width) - 1); - - if (m_BlockTypes.empty() || (Layer > m_BlockTypes.size() - 1) /* Vector starts from zero, .size() starts from 1 */) + if (m_BlockTypes.empty() || (a_BlockIdx > m_BlockTypes.size() - 1) /* Vector starts from zero, .size() starts from 1 */) { return E_BLOCK_AIR; } - return m_BlockTypes[Layer][SubBlock]; + return m_BlockTypes[a_BlockIdx]; } diff --git a/src/Chunk.h b/src/Chunk.h index 1b83bc540..ef6a11006 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -421,7 +421,7 @@ private: cChunkMap * m_ChunkMap; // TODO: Make these pointers and don't allocate what isn't needed - std::vector> m_BlockTypes; + std::vector m_BlockTypes; NIBBLETYPE m_BlockMeta [cChunkDef::NumBlocks / 2]; NIBBLETYPE m_BlockLight [cChunkDef::NumBlocks / 2]; NIBBLETYPE m_BlockSkyLight[cChunkDef::NumBlocks / 2]; -- cgit v1.2.3 From 3201d1bf169ca99e4cdbfad7601f6aed89b3c76c Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 4 Apr 2014 23:16:52 +0100 Subject: Nibbletypes are compressed + Added nibble compression * Fixed an off by one --- src/Chunk.cpp | 49 +++++++++++++++++++++++++------------ src/Chunk.h | 2 +- src/ChunkDef.h | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 110 insertions(+), 18 deletions(-) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index c2caafe45..97822048d 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -243,9 +243,12 @@ void cChunk::GetAllData(cChunkDataCallback & a_Callback) std::vector Blocks = m_BlockTypes; Blocks.resize(NumBlocks); - a_Callback.BlockTypes (Blocks.data()); - a_Callback.BlockMeta (m_BlockMeta); + + std::vector Metas = m_BlockMeta; + Metas.resize(NumBlocks / 2); + a_Callback.BlockMeta (Metas.data()); + a_Callback.LightIsValid (m_IsLightValid); a_Callback.BlockLight (m_BlockLight); a_Callback.BlockSkyLight(m_BlockSkyLight); @@ -282,23 +285,40 @@ void cChunk::SetAllData( memcpy(m_HeightMap, a_HeightMap, sizeof(m_HeightMap)); } - bool FoundNonAir = false; - int IdxWhereNonEmptyStarts = 0; - m_BlockTypes.clear(); + { // Blocktype compression + bool FoundNonAir = false; + int IdxWhereNonEmptyStarts = 0; + m_BlockTypes.clear(); - for (int Idx = NumBlocks - 1; Idx >= 0; Idx--) - { - if (a_BlockTypes[Idx] != E_BLOCK_AIR) + for (int Idx = NumBlocks - 1; Idx >= 0; Idx--) { - FoundNonAir = true; - IdxWhereNonEmptyStarts = Idx; - break; + if (a_BlockTypes[Idx] != E_BLOCK_AIR) + { + FoundNonAir = true; + IdxWhereNonEmptyStarts = Idx; + break; + } } + m_BlockTypes.insert(m_BlockTypes.end(), &a_BlockTypes[0], &a_BlockTypes[IdxWhereNonEmptyStarts + 1]); } - m_BlockTypes.insert(m_BlockTypes.end(), &a_BlockTypes[0], &a_BlockTypes[IdxWhereNonEmptyStarts + 1]); + { // Blockmeta compression + bool FoundNonEmpty = false; + int IdxWhereNonEmptyStarts = 0; + m_BlockMeta.clear(); + + for (int Idx = (NumBlocks / 2) - 1; Idx >= 0; Idx--) + { + if (a_BlockMeta[Idx] != 0) + { + FoundNonEmpty = true; + IdxWhereNonEmptyStarts = Idx; + break; + } + } + m_BlockMeta.insert(m_BlockMeta.end(), &a_BlockMeta[0], &a_BlockMeta[IdxWhereNonEmptyStarts + 1]); + } - memcpy(m_BlockMeta, a_BlockMeta, sizeof(m_BlockMeta)); if (a_BlockLight != NULL) { memcpy(m_BlockLight, a_BlockLight, sizeof(m_BlockLight)); @@ -1548,9 +1568,8 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT if (m_BlockTypes.empty() || (index > m_BlockTypes.size() - 1) /* Vector starts from zero, .size() starts from 1 */) { - m_BlockTypes.resize(index); + m_BlockTypes.resize(index + 1); } - m_BlockTypes[index] = a_BlockType; // The client doesn't need to distinguish between stationary and nonstationary fluids: diff --git a/src/Chunk.h b/src/Chunk.h index ef6a11006..d8e4cf6e4 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -422,7 +422,7 @@ private: // TODO: Make these pointers and don't allocate what isn't needed std::vector m_BlockTypes; - NIBBLETYPE m_BlockMeta [cChunkDef::NumBlocks / 2]; + std::vector m_BlockMeta; NIBBLETYPE m_BlockLight [cChunkDef::NumBlocks / 2]; NIBBLETYPE m_BlockSkyLight[cChunkDef::NumBlocks / 2]; diff --git a/src/ChunkDef.h b/src/ChunkDef.h index 9c7753820..3793bd95a 100644 --- a/src/ChunkDef.h +++ b/src/ChunkDef.h @@ -230,6 +230,21 @@ public: ASSERT(!"cChunkDef::GetNibble(): index out of chunk range!"); return 0; } + + + static NIBBLETYPE GetNibble(const std::vector & a_Buffer, int a_BlockIdx) + { + if ((a_BlockIdx > -1) && (a_BlockIdx < NumBlocks)) + { + if (a_Buffer.empty() || (a_BlockIdx / 2 > a_Buffer.size() - 1)) + { + return 0; + } + 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 NIBBLETYPE * a_Buffer, int x, int y, int z) @@ -244,6 +259,22 @@ public: } + static NIBBLETYPE GetNibble(const std::vector & 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); + if (a_Buffer.empty() || (Index / 2 > a_Buffer.size() - 1)) + { + return 0; + } + return (a_Buffer[Index / 2] >> ((Index & 1) * 4)) & 0x0f; + } + ASSERT(!"cChunkDef::GetNibble(): coords out of chunk range!"); + return 0; + } + + static void SetNibble(NIBBLETYPE * a_Buffer, int a_BlockIdx, NIBBLETYPE a_Nibble) { if ((a_BlockIdx < 0) || (a_BlockIdx >= NumBlocks)) @@ -256,6 +287,24 @@ public: ((a_Nibble & 0x0f) << ((a_BlockIdx & 1) * 4)) // The nibble being set ); } + + + static void SetNibble(std::vector & 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() || (a_BlockIdx / 2 > a_Buffer.size() - 1)) + { + a_Buffer.resize((a_BlockIdx / 2) + 1); + } + a_Buffer[a_BlockIdx / 2] = static_cast( + (a_Buffer[a_BlockIdx / 2] & (0xf0 >> ((a_BlockIdx & 1) * 4))) | // The untouched nibble + ((a_Nibble & 0x0f) << ((a_BlockIdx & 1) * 4)) // The nibble being set + ); + } static void SetNibble(NIBBLETYPE * a_Buffer, int x, int y, int z, NIBBLETYPE a_Nibble) @@ -278,13 +327,37 @@ public: } - inline static NIBBLETYPE GetNibble(const NIBBLETYPE * a_Buffer, const Vector3i & a_BlockPos ) + static void SetNibble(std::vector & 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() || (Index / 2 > a_Buffer.size() - 1)) + { + a_Buffer.resize((Index / 2) + 1); + } + a_Buffer[Index / 2] = static_cast( + (a_Buffer[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble + ((a_Nibble & 0x0f) << ((Index & 1) * 4)) // The nibble being set + ); + } + + + inline static NIBBLETYPE GetNibble(const NIBBLETYPE * a_Buffer, const Vector3i & a_BlockPos) { return GetNibble(a_Buffer, a_BlockPos.x, a_BlockPos.y, a_BlockPos.z ); } - inline static void SetNibble(NIBBLETYPE * a_Buffer, const Vector3i & a_BlockPos, NIBBLETYPE a_Value ) + inline static void SetNibble(NIBBLETYPE * a_Buffer, const Vector3i & a_BlockPos, NIBBLETYPE a_Value) { SetNibble( a_Buffer, a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, a_Value ); } -- cgit v1.2.3 From a42d1f851748c0cca542d1b0038318d0cbff9fd3 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 6 Apr 2014 23:30:21 +0100 Subject: Blocklight and skylight now compressed --- src/Chunk.cpp | 106 ++++++++++++++++++++++++++++++++++++++++++++------------- src/Chunk.h | 8 ++--- src/ChunkDef.h | 8 ++--- 3 files changed, 90 insertions(+), 32 deletions(-) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 97822048d..7a3e63bd0 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -250,8 +250,14 @@ void cChunk::GetAllData(cChunkDataCallback & a_Callback) a_Callback.BlockMeta (Metas.data()); a_Callback.LightIsValid (m_IsLightValid); - a_Callback.BlockLight (m_BlockLight); - a_Callback.BlockSkyLight(m_BlockSkyLight); + + std::vector BlockLights = m_BlockLight; + BlockLights.resize(NumBlocks / 2); + a_Callback.BlockLight (BlockLights.data()); + + std::vector BlockSkyLights = m_BlockSkyLight; + BlockSkyLights.resize(NumBlocks / 2, 0xff); + a_Callback.BlockSkyLight(BlockSkyLights.data()); for (cEntityList::iterator itr = m_Entities.begin(); itr != m_Entities.end(); ++itr) { @@ -285,47 +291,66 @@ void cChunk::SetAllData( memcpy(m_HeightMap, a_HeightMap, sizeof(m_HeightMap)); } - { // Blocktype compression - bool FoundNonAir = false; + { + int IdxWhereNonEmptyStarts = 0; + { // Blocktype compression + bool FoundNonAir = false; + m_BlockTypes.clear(); + + for (int Idx = NumBlocks - 1; Idx >= 0; Idx--) + { + if (a_BlockTypes[Idx] != E_BLOCK_AIR) + { + FoundNonAir = true; + IdxWhereNonEmptyStarts = Idx; + break; + } + } + m_BlockTypes.insert(m_BlockTypes.end(), &a_BlockTypes[0], &a_BlockTypes[IdxWhereNonEmptyStarts + 1]); + } + + { // Blockmeta compression + m_BlockMeta.clear(); + m_BlockMeta.insert(m_BlockMeta.end(), &a_BlockMeta[0], &a_BlockMeta[(IdxWhereNonEmptyStarts + 1) / 2]); + } + } + + if (a_BlockLight != NULL) + { + // Compress blocklight + bool FoundNonEmpty = false; int IdxWhereNonEmptyStarts = 0; - m_BlockTypes.clear(); + m_BlockLight.clear(); - for (int Idx = NumBlocks - 1; Idx >= 0; Idx--) + for (int Idx = (NumBlocks / 2) - 1; Idx >= 0; Idx--) { - if (a_BlockTypes[Idx] != E_BLOCK_AIR) + if (a_BlockLight[Idx] != 0) { - FoundNonAir = true; + FoundNonEmpty = true; IdxWhereNonEmptyStarts = Idx; break; } } - m_BlockTypes.insert(m_BlockTypes.end(), &a_BlockTypes[0], &a_BlockTypes[IdxWhereNonEmptyStarts + 1]); + m_BlockLight.insert(m_BlockLight.end(), &a_BlockLight[0], &a_BlockLight[IdxWhereNonEmptyStarts + 1]); } - { // Blockmeta compression + if (a_BlockSkyLight != NULL) + { + // Compress skylight bool FoundNonEmpty = false; int IdxWhereNonEmptyStarts = 0; - m_BlockMeta.clear(); + m_BlockSkyLight.clear(); for (int Idx = (NumBlocks / 2) - 1; Idx >= 0; Idx--) { - if (a_BlockMeta[Idx] != 0) + if (a_BlockSkyLight[Idx] != 0xff) { FoundNonEmpty = true; IdxWhereNonEmptyStarts = Idx; break; } } - m_BlockMeta.insert(m_BlockMeta.end(), &a_BlockMeta[0], &a_BlockMeta[IdxWhereNonEmptyStarts + 1]); - } - - if (a_BlockLight != NULL) - { - memcpy(m_BlockLight, a_BlockLight, sizeof(m_BlockLight)); - } - if (a_BlockSkyLight != NULL) - { - memcpy(m_BlockSkyLight, a_BlockSkyLight, sizeof(m_BlockSkyLight)); + m_BlockSkyLight.insert(m_BlockSkyLight.end(), &a_BlockSkyLight[0], &a_BlockSkyLight[IdxWhereNonEmptyStarts + 1]); } m_IsLightValid = (a_BlockLight != NULL) && (a_BlockSkyLight != NULL); @@ -371,8 +396,41 @@ void cChunk::SetLight( { // TODO: We might get cases of wrong lighting when a chunk changes in the middle of a lighting calculation. // Postponing until we see how bad it is :) - memcpy(m_BlockLight, a_BlockLight, sizeof(m_BlockLight)); - memcpy(m_BlockSkyLight, a_SkyLight, sizeof(m_BlockSkyLight)); + + { // Compress blocklight + bool FoundNonEmpty = false; + int IdxWhereNonEmptyStarts = 0; + m_BlockLight.clear(); + + for (int Idx = (NumBlocks / 2) - 1; Idx >= 0; Idx--) + { + if (a_BlockLight[Idx] != 0) + { + FoundNonEmpty = true; + IdxWhereNonEmptyStarts = Idx; + break; + } + } + m_BlockLight.insert(m_BlockLight.end(), &a_BlockLight[0], &a_BlockLight[IdxWhereNonEmptyStarts + 1]); + } + + { // Compress skylight + bool FoundNonEmpty = false; + int IdxWhereNonEmptyStarts = 0; + m_BlockSkyLight.clear(); + + for (int Idx = (NumBlocks / 2) - 1; Idx >= 0; Idx--) + { + if (a_SkyLight[Idx] != 0xff) + { + FoundNonEmpty = true; + IdxWhereNonEmptyStarts = Idx; + break; + } + } + m_BlockSkyLight.insert(m_BlockSkyLight.end(), &a_SkyLight[0], &a_SkyLight[IdxWhereNonEmptyStarts + 1]); + } + m_IsLightValid = true; } diff --git a/src/Chunk.h b/src/Chunk.h index d8e4cf6e4..6e2933ff8 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -326,9 +326,9 @@ public: inline void SetMeta(int a_BlockIdx, NIBBLETYPE a_Meta) { cChunkDef::SetNibble(m_BlockMeta, a_BlockIdx, a_Meta); } inline NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const {return cChunkDef::GetNibble(m_BlockLight, a_RelX, a_RelY, a_RelZ); } - inline NIBBLETYPE GetSkyLight (int a_RelX, int a_RelY, int a_RelZ) const {return cChunkDef::GetNibble(m_BlockSkyLight, a_RelX, a_RelY, a_RelZ); } + inline NIBBLETYPE GetSkyLight (int a_RelX, int a_RelY, int a_RelZ) const {return cChunkDef::GetNibble(m_BlockSkyLight, a_RelX, a_RelY, a_RelZ, true); } inline NIBBLETYPE GetBlockLight(int a_Idx) const {return cChunkDef::GetNibble(m_BlockLight, a_Idx); } - inline NIBBLETYPE GetSkyLight (int a_Idx) const {return cChunkDef::GetNibble(m_BlockSkyLight, a_Idx); } + inline NIBBLETYPE GetSkyLight (int a_Idx) const {return cChunkDef::GetNibble(m_BlockSkyLight, a_Idx, true); } /** Same as GetBlock(), but relative coords needn't be in this chunk (uses m_Neighbor-s or m_ChunkMap in such a case); returns true on success */ bool UnboundedRelGetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta) const; @@ -423,8 +423,8 @@ private: // TODO: Make these pointers and don't allocate what isn't needed std::vector m_BlockTypes; std::vector m_BlockMeta; - NIBBLETYPE m_BlockLight [cChunkDef::NumBlocks / 2]; - NIBBLETYPE m_BlockSkyLight[cChunkDef::NumBlocks / 2]; + std::vector m_BlockLight; + std::vector m_BlockSkyLight; cChunkDef::HeightMap m_HeightMap; cChunkDef::BiomeMap m_BiomeMap; diff --git a/src/ChunkDef.h b/src/ChunkDef.h index 3793bd95a..d744d24b2 100644 --- a/src/ChunkDef.h +++ b/src/ChunkDef.h @@ -232,13 +232,13 @@ public: } - static NIBBLETYPE GetNibble(const std::vector & a_Buffer, int a_BlockIdx) + static NIBBLETYPE GetNibble(const std::vector & a_Buffer, int a_BlockIdx, bool a_IsSkyLightNibble = false) { if ((a_BlockIdx > -1) && (a_BlockIdx < NumBlocks)) { if (a_Buffer.empty() || (a_BlockIdx / 2 > a_Buffer.size() - 1)) { - return 0; + return (a_IsSkyLightNibble ? 0xff : 0); } return (a_Buffer[a_BlockIdx / 2] >> ((a_BlockIdx & 1) * 4)) & 0x0f; } @@ -259,14 +259,14 @@ public: } - static NIBBLETYPE GetNibble(const std::vector & a_Buffer, int x, int y, int z) + static NIBBLETYPE GetNibble(const std::vector & 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() || (Index / 2 > a_Buffer.size() - 1)) { - return 0; + return (a_IsSkyLightNibble ? 0xff : 0); } return (a_Buffer[Index / 2] >> ((Index & 1) * 4)) & 0x0f; } -- cgit v1.2.3 From 74c4789c6f8f42c6df45ed1e355e562bdced908d Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 7 Apr 2014 12:43:43 +0100 Subject: Attempt to fix errors --- src/Chunk.cpp | 4 ++-- src/ChunkDef.h | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 7a3e63bd0..ede01a6cc 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -1624,7 +1624,7 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT MarkDirty(); - if (m_BlockTypes.empty() || (index > m_BlockTypes.size() - 1) /* Vector starts from zero, .size() starts from 1 */) + if (m_BlockTypes.empty() || ((size_t)index > m_BlockTypes.size() - 1) /* Vector starts from zero, .size() starts from 1 */) { m_BlockTypes.resize(index + 1); } @@ -2569,7 +2569,7 @@ BLOCKTYPE cChunk::GetBlock(int a_BlockIdx) const return 0; } - if (m_BlockTypes.empty() || (a_BlockIdx > m_BlockTypes.size() - 1) /* Vector starts from zero, .size() starts from 1 */) + if (m_BlockTypes.empty() || ((size_t)a_BlockIdx > m_BlockTypes.size() - 1) /* Vector starts from zero, .size() starts from 1 */) { return E_BLOCK_AIR; } diff --git a/src/ChunkDef.h b/src/ChunkDef.h index d744d24b2..bb9f14bbe 100644 --- a/src/ChunkDef.h +++ b/src/ChunkDef.h @@ -236,11 +236,11 @@ public: { if ((a_BlockIdx > -1) && (a_BlockIdx < NumBlocks)) { - if (a_Buffer.empty() || (a_BlockIdx / 2 > a_Buffer.size() - 1)) + if (a_Buffer.empty() || ((size_t)(a_BlockIdx / 2) > a_Buffer.size() - 1)) { return (a_IsSkyLightNibble ? 0xff : 0); } - return (a_Buffer[a_BlockIdx / 2] >> ((a_BlockIdx & 1) * 4)) & 0x0f; + return (a_Buffer[(size_t)(a_BlockIdx / 2)] >> ((a_BlockIdx & 1) * 4)) & 0x0f; } ASSERT(!"cChunkDef::GetNibble(): index out of chunk range!"); return 0; @@ -264,11 +264,11 @@ public: if ((x < Width) && (x > -1) && (y < Height) && (y > -1) && (z < Width) && (z > -1)) { int Index = MakeIndexNoCheck(x, y, z); - if (a_Buffer.empty() || (Index / 2 > a_Buffer.size() - 1)) + if (a_Buffer.empty() || ((size_t)(Index / 2) > a_Buffer.size() - 1)) { return (a_IsSkyLightNibble ? 0xff : 0); } - return (a_Buffer[Index / 2] >> ((Index & 1) * 4)) & 0x0f; + return (a_Buffer[(size_t)(Index / 2)] >> ((Index & 1) * 4)) & 0x0f; } ASSERT(!"cChunkDef::GetNibble(): coords out of chunk range!"); return 0; @@ -296,11 +296,11 @@ public: ASSERT(!"cChunkDef::SetNibble(): index out of range!"); return; } - if (a_Buffer.empty() || (a_BlockIdx / 2 > a_Buffer.size() - 1)) + if (a_Buffer.empty() || ((size_t)(a_BlockIdx / 2) > a_Buffer.size() - 1)) { - a_Buffer.resize((a_BlockIdx / 2) + 1); + a_Buffer.resize((size_t)((a_BlockIdx / 2) + 1)); } - a_Buffer[a_BlockIdx / 2] = static_cast( + a_Buffer[(size_t)(a_BlockIdx / 2)] = static_cast( (a_Buffer[a_BlockIdx / 2] & (0xf0 >> ((a_BlockIdx & 1) * 4))) | // The untouched nibble ((a_Nibble & 0x0f) << ((a_BlockIdx & 1) * 4)) // The nibble being set ); @@ -340,11 +340,11 @@ public: } int Index = MakeIndexNoCheck(x, y, z); - if (a_Buffer.empty() || (Index / 2 > a_Buffer.size() - 1)) + if (a_Buffer.empty() || ((size_t)(Index / 2) > a_Buffer.size() - 1)) { - a_Buffer.resize((Index / 2) + 1); + a_Buffer.resize((size_t)((Index / 2) + 1)); } - a_Buffer[Index / 2] = static_cast( + a_Buffer[(size_t)(Index / 2)] = static_cast( (a_Buffer[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble ((a_Nibble & 0x0f) << ((Index & 1) * 4)) // The nibble being set ); -- cgit v1.2.3 From f13cf1a0217437113ad5372ef971c3903d55a9c6 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 7 Apr 2014 20:57:14 +0100 Subject: Maybe speed improvements? * Use a single index to determine from when to begin copying data * Use heightmap to determine first nonair block --- src/Chunk.cpp | 75 ++++++++++++++++++++++------------------------------------- src/Chunk.h | 2 +- 2 files changed, 29 insertions(+), 48 deletions(-) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index ede01a6cc..7b49862f0 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -291,74 +291,55 @@ void cChunk::SetAllData( memcpy(m_HeightMap, a_HeightMap, sizeof(m_HeightMap)); } + if (a_HeightMap == NULL) { - int IdxWhereNonEmptyStarts = 0; - { // Blocktype compression - bool FoundNonAir = false; - m_BlockTypes.clear(); + CalculateHeightmap(a_BlockTypes); + } - for (int Idx = NumBlocks - 1; Idx >= 0; Idx--) + int IdxWhereNonEmptyStarts = 0; + { // Blocktype compression + unsigned char Highest = 0; + int X = 0, Z = 0; + m_BlockTypes.clear(); + + for (int x = 0; x < Width; x++) + { + for (int z = 0; z < Width; z++) { - if (a_BlockTypes[Idx] != E_BLOCK_AIR) + unsigned char Height = m_HeightMap[x + z * Width]; + if (Height > Highest) { - FoundNonAir = true; - IdxWhereNonEmptyStarts = Idx; - break; + Highest = Height; + X = x; Z = z; } } - m_BlockTypes.insert(m_BlockTypes.end(), &a_BlockTypes[0], &a_BlockTypes[IdxWhereNonEmptyStarts + 1]); } - { // Blockmeta compression - m_BlockMeta.clear(); - m_BlockMeta.insert(m_BlockMeta.end(), &a_BlockMeta[0], &a_BlockMeta[(IdxWhereNonEmptyStarts + 1) / 2]); - } + IdxWhereNonEmptyStarts = MakeIndexNoCheck(X, Highest + 1, Z); + + m_BlockTypes.insert(m_BlockTypes.end(), &a_BlockTypes[0], &a_BlockTypes[IdxWhereNonEmptyStarts]); + } + + { // Blockmeta compression + m_BlockMeta.clear(); + m_BlockMeta.insert(m_BlockMeta.end(), &a_BlockMeta[0], &a_BlockMeta[IdxWhereNonEmptyStarts / 2]); } if (a_BlockLight != NULL) { // Compress blocklight - bool FoundNonEmpty = false; - int IdxWhereNonEmptyStarts = 0; m_BlockLight.clear(); - - for (int Idx = (NumBlocks / 2) - 1; Idx >= 0; Idx--) - { - if (a_BlockLight[Idx] != 0) - { - FoundNonEmpty = true; - IdxWhereNonEmptyStarts = Idx; - break; - } - } - m_BlockLight.insert(m_BlockLight.end(), &a_BlockLight[0], &a_BlockLight[IdxWhereNonEmptyStarts + 1]); + m_BlockLight.insert(m_BlockLight.end(), &a_BlockLight[0], &a_BlockLight[IdxWhereNonEmptyStarts / 2]); } if (a_BlockSkyLight != NULL) { // Compress skylight - bool FoundNonEmpty = false; - int IdxWhereNonEmptyStarts = 0; m_BlockSkyLight.clear(); - - for (int Idx = (NumBlocks / 2) - 1; Idx >= 0; Idx--) - { - if (a_BlockSkyLight[Idx] != 0xff) - { - FoundNonEmpty = true; - IdxWhereNonEmptyStarts = Idx; - break; - } - } - m_BlockSkyLight.insert(m_BlockSkyLight.end(), &a_BlockSkyLight[0], &a_BlockSkyLight[IdxWhereNonEmptyStarts + 1]); + m_BlockSkyLight.insert(m_BlockSkyLight.end(), &a_BlockSkyLight[0], &a_BlockSkyLight[IdxWhereNonEmptyStarts / 2]); } m_IsLightValid = (a_BlockLight != NULL) && (a_BlockSkyLight != NULL); - - if (a_HeightMap == NULL) - { - CalculateHeightmap(); - } // Clear the block entities present - either the loader / saver has better, or we'll create empty ones: for (cBlockEntityList::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end(); ++itr) @@ -1483,7 +1464,7 @@ void cChunk::WakeUpSimulators(void) -void cChunk::CalculateHeightmap() +void cChunk::CalculateHeightmap(const BLOCKTYPE * a_BlockTypes) { for (int x = 0; x < Width; x++) { @@ -1492,7 +1473,7 @@ void cChunk::CalculateHeightmap() for (int y = Height - 1; y > -1; y--) { int index = MakeIndex( x, y, z ); - if (GetBlock(index) != E_BLOCK_AIR) + if (a_BlockTypes[index] != E_BLOCK_AIR) { m_HeightMap[x + z * Width] = (unsigned char)y; break; diff --git a/src/Chunk.h b/src/Chunk.h index 6e2933ff8..9100eec58 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -267,7 +267,7 @@ public: void UseBlockEntity(cPlayer * a_Player, int a_X, int a_Y, int a_Z); // [x, y, z] in world block coords void CalculateLighting(); // Recalculate right now - void CalculateHeightmap(); + void CalculateHeightmap(const BLOCKTYPE * a_BlockTypes); // Broadcast various packets to all clients of this chunk: // (Please keep these alpha-sorted) -- cgit v1.2.3 From 01546020fc70a940f2644e9c8f58c26b754bc653 Mon Sep 17 00:00:00 2001 From: Tycho Date: Thu, 10 Apr 2014 12:12:36 -0700 Subject: Replaced all the .data() calls so the code compiles in VS2008 --- src/Chunk.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 7b49862f0..ee1531b5c 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -243,21 +243,21 @@ void cChunk::GetAllData(cChunkDataCallback & a_Callback) std::vector Blocks = m_BlockTypes; Blocks.resize(NumBlocks); - a_Callback.BlockTypes (Blocks.data()); + a_Callback.BlockTypes (&Blocks[0]); std::vector Metas = m_BlockMeta; Metas.resize(NumBlocks / 2); - a_Callback.BlockMeta (Metas.data()); + a_Callback.BlockMeta (&Metas[0]); a_Callback.LightIsValid (m_IsLightValid); std::vector BlockLights = m_BlockLight; BlockLights.resize(NumBlocks / 2); - a_Callback.BlockLight (BlockLights.data()); + a_Callback.BlockLight (&BlockLights[0]); std::vector BlockSkyLights = m_BlockSkyLight; BlockSkyLights.resize(NumBlocks / 2, 0xff); - a_Callback.BlockSkyLight(BlockSkyLights.data()); + a_Callback.BlockSkyLight(&BlockSkyLights[0]); for (cEntityList::iterator itr = m_Entities.begin(); itr != m_Entities.end(); ++itr) { @@ -424,7 +424,7 @@ void cChunk::GetBlockTypes(BLOCKTYPE * a_BlockTypes) std::vector Blocks = m_BlockTypes; Blocks.resize(NumBlocks); - memcpy(a_BlockTypes, Blocks.data(), NumBlocks); + memcpy(a_BlockTypes, &Blocks[0], NumBlocks); } -- cgit v1.2.3 From ffce8d6907b8b93a2764a67766a40901bcad0835 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Thu, 24 Apr 2014 21:49:56 +0100 Subject: Implemented suggestions --- src/Chunk.cpp | 28 +++++---------------- src/ChunkDef.h | 77 ++++++++-------------------------------------------------- 2 files changed, 16 insertions(+), 89 deletions(-) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index ee1531b5c..6c35f7f47 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -378,16 +378,14 @@ void cChunk::SetLight( // TODO: We might get cases of wrong lighting when a chunk changes in the middle of a lighting calculation. // Postponing until we see how bad it is :) + int IdxWhereNonEmptyStarts = 0; { // Compress blocklight - bool FoundNonEmpty = false; - int IdxWhereNonEmptyStarts = 0; m_BlockLight.clear(); for (int Idx = (NumBlocks / 2) - 1; Idx >= 0; Idx--) { if (a_BlockLight[Idx] != 0) { - FoundNonEmpty = true; IdxWhereNonEmptyStarts = Idx; break; } @@ -396,19 +394,7 @@ void cChunk::SetLight( } { // Compress skylight - bool FoundNonEmpty = false; - int IdxWhereNonEmptyStarts = 0; m_BlockSkyLight.clear(); - - for (int Idx = (NumBlocks / 2) - 1; Idx >= 0; Idx--) - { - if (a_SkyLight[Idx] != 0xff) - { - FoundNonEmpty = true; - IdxWhereNonEmptyStarts = Idx; - break; - } - } m_BlockSkyLight.insert(m_BlockSkyLight.end(), &a_SkyLight[0], &a_SkyLight[IdxWhereNonEmptyStarts + 1]); } @@ -421,10 +407,8 @@ void cChunk::SetLight( void cChunk::GetBlockTypes(BLOCKTYPE * a_BlockTypes) { - std::vector Blocks = m_BlockTypes; - Blocks.resize(NumBlocks); - - memcpy(a_BlockTypes, &Blocks[0], NumBlocks); + std::copy(m_BlockTypes.begin(), m_BlockTypes.end(), a_BlockTypes); + std::fill_n(&a_BlockTypes[m_BlockTypes.size()], NumBlocks - m_BlockTypes.size(), E_BLOCK_AIR); } @@ -832,7 +816,7 @@ void cChunk::BroadcastPendingBlockChanges(void) void cChunk::CheckBlocks() { - if (m_ToTickBlocks.size() == 0) + if (m_ToTickBlocks.empty()) { return; } @@ -1605,7 +1589,7 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT MarkDirty(); - if (m_BlockTypes.empty() || ((size_t)index > m_BlockTypes.size() - 1) /* Vector starts from zero, .size() starts from 1 */) + if (m_BlockTypes.empty() || ((size_t)index >= m_BlockTypes.size())) { m_BlockTypes.resize(index + 1); } @@ -2550,7 +2534,7 @@ BLOCKTYPE cChunk::GetBlock(int a_BlockIdx) const return 0; } - if (m_BlockTypes.empty() || ((size_t)a_BlockIdx > m_BlockTypes.size() - 1) /* Vector starts from zero, .size() starts from 1 */) + if (m_BlockTypes.empty() || ((size_t)a_BlockIdx >= m_BlockTypes.size())) { return E_BLOCK_AIR; } diff --git a/src/ChunkDef.h b/src/ChunkDef.h index bb9f14bbe..1b3dd3ee0 100644 --- a/src/ChunkDef.h +++ b/src/ChunkDef.h @@ -219,24 +219,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 & 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 (a_Buffer.empty() || ((size_t)(a_BlockIdx / 2) >= a_Buffer.size())) { return (a_IsSkyLightNibble ? 0xff : 0); } @@ -245,18 +234,6 @@ 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 & a_Buffer, int x, int y, int z, bool a_IsSkyLightNibble = false) @@ -264,7 +241,7 @@ public: 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 (a_Buffer.empty() || ((size_t)(Index / 2) >= a_Buffer.size())) { return (a_IsSkyLightNibble ? 0xff : 0); } @@ -275,17 +252,15 @@ public: } - 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[Index / 2] >> ((Index & 1) * 4)) & 0x0f; } - a_Buffer[a_BlockIdx / 2] = static_cast( - (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; } @@ -296,7 +271,7 @@ public: ASSERT(!"cChunkDef::SetNibble(): index out of range!"); return; } - if (a_Buffer.empty() || ((size_t)(a_BlockIdx / 2) > a_Buffer.size() - 1)) + if (a_Buffer.empty() || ((size_t)(a_BlockIdx / 2) >= a_Buffer.size())) { a_Buffer.resize((size_t)((a_BlockIdx / 2) + 1)); } @@ -305,26 +280,6 @@ public: ((a_Nibble & 0x0f) << ((a_BlockIdx & 1) * 4)) // The nibble being set ); } - - - static void SetNibble(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); - a_Buffer[Index / 2] = static_cast( - (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 & a_Buffer, int x, int y, int z, NIBBLETYPE a_Nibble) @@ -340,7 +295,7 @@ public: } int Index = MakeIndexNoCheck(x, y, z); - if (a_Buffer.empty() || ((size_t)(Index / 2) > a_Buffer.size() - 1)) + if (a_Buffer.empty() || ((size_t)(Index / 2) >= a_Buffer.size())) { a_Buffer.resize((size_t)((Index / 2) + 1)); } @@ -350,18 +305,6 @@ public: ); } - - inline static NIBBLETYPE GetNibble(const NIBBLETYPE * a_Buffer, const Vector3i & a_BlockPos) - { - return GetNibble(a_Buffer, a_BlockPos.x, a_BlockPos.y, a_BlockPos.z ); - } - - - inline static void SetNibble(NIBBLETYPE * a_Buffer, const Vector3i & a_BlockPos, NIBBLETYPE a_Value) - { - SetNibble( a_Buffer, a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, a_Value ); - } - } ; -- cgit v1.2.3 From acbd4e6503a8da6645c97dbe010eed7f37386194 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Thu, 24 Apr 2014 21:52:01 +0100 Subject: Another small speed improvement? --- src/Chunk.cpp | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 6c35f7f47..7af1820d2 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -378,24 +378,14 @@ void cChunk::SetLight( // TODO: We might get cases of wrong lighting when a chunk changes in the middle of a lighting calculation. // Postponing until we see how bad it is :) - int IdxWhereNonEmptyStarts = 0; { // Compress blocklight m_BlockLight.clear(); - - for (int Idx = (NumBlocks / 2) - 1; Idx >= 0; Idx--) - { - if (a_BlockLight[Idx] != 0) - { - IdxWhereNonEmptyStarts = Idx; - break; - } - } - m_BlockLight.insert(m_BlockLight.end(), &a_BlockLight[0], &a_BlockLight[IdxWhereNonEmptyStarts + 1]); + m_BlockLight.insert(m_BlockLight.end(), &a_BlockLight[0], &a_BlockLight[m_BlockTypes.size()]); } { // Compress skylight m_BlockSkyLight.clear(); - m_BlockSkyLight.insert(m_BlockSkyLight.end(), &a_SkyLight[0], &a_SkyLight[IdxWhereNonEmptyStarts + 1]); + m_BlockSkyLight.insert(m_BlockSkyLight.end(), &a_SkyLight[0], &a_SkyLight[m_BlockTypes.size()]); } m_IsLightValid = true; -- cgit v1.2.3 From 3397f9faec43ac288fa9e7f1f555a1309a4e47a5 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Thu, 24 Apr 2014 22:07:20 +0100 Subject: Fixed indent --- src/Chunk.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 7af1820d2..97a8ba88b 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -282,7 +282,7 @@ void cChunk::SetAllData( const HeightMap * a_HeightMap, const BiomeMap & a_BiomeMap, cBlockEntityList & a_BlockEntities - ) +) { memcpy(m_BiomeMap, a_BiomeMap, sizeof(m_BiomeMap)); -- cgit v1.2.3 From 05f52192c9389f0c28cc6d772f3625c9588273a1 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 25 Apr 2014 21:22:43 +0100 Subject: Implemented comments --- src/Chunk.cpp | 12 +++++------ src/Chunk.h | 8 ++++---- src/ChunkDef.h | 65 +++++++++++++++++++++++++++++++++++++--------------------- 3 files changed, 52 insertions(+), 33 deletions(-) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 97a8ba88b..652f56905 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -241,21 +241,21 @@ void cChunk::GetAllData(cChunkDataCallback & a_Callback) a_Callback.HeightMap (&m_HeightMap); a_Callback.BiomeData (&m_BiomeMap); - std::vector Blocks = m_BlockTypes; + COMPRESSED_BLOCKTYPE Blocks = m_BlockTypes; Blocks.resize(NumBlocks); a_Callback.BlockTypes (&Blocks[0]); - std::vector Metas = m_BlockMeta; + COMPRESSED_NIBBLETYPE Metas = m_BlockMeta; Metas.resize(NumBlocks / 2); a_Callback.BlockMeta (&Metas[0]); a_Callback.LightIsValid (m_IsLightValid); - std::vector BlockLights = m_BlockLight; + COMPRESSED_NIBBLETYPE BlockLights = m_BlockLight; BlockLights.resize(NumBlocks / 2); a_Callback.BlockLight (&BlockLights[0]); - std::vector BlockSkyLights = m_BlockSkyLight; + COMPRESSED_NIBBLETYPE BlockSkyLights = m_BlockSkyLight; BlockSkyLights.resize(NumBlocks / 2, 0xff); a_Callback.BlockSkyLight(&BlockSkyLights[0]); @@ -1579,7 +1579,7 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT MarkDirty(); - if (m_BlockTypes.empty() || ((size_t)index >= m_BlockTypes.size())) + if ((size_t)index >= m_BlockTypes.size()) { m_BlockTypes.resize(index + 1); } @@ -2524,7 +2524,7 @@ BLOCKTYPE cChunk::GetBlock(int a_BlockIdx) const return 0; } - if (m_BlockTypes.empty() || ((size_t)a_BlockIdx >= m_BlockTypes.size())) + if ((size_t)a_BlockIdx >= m_BlockTypes.size()) { return E_BLOCK_AIR; } diff --git a/src/Chunk.h b/src/Chunk.h index 9100eec58..a9c9be861 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -421,10 +421,10 @@ private: cChunkMap * m_ChunkMap; // TODO: Make these pointers and don't allocate what isn't needed - std::vector m_BlockTypes; - std::vector m_BlockMeta; - std::vector m_BlockLight; - std::vector m_BlockSkyLight; + COMPRESSED_BLOCKTYPE m_BlockTypes; + COMPRESSED_NIBBLETYPE m_BlockMeta; + COMPRESSED_NIBBLETYPE m_BlockLight; + COMPRESSED_NIBBLETYPE m_BlockSkyLight; cChunkDef::HeightMap m_HeightMap; cChunkDef::BiomeMap m_BiomeMap; diff --git a/src/ChunkDef.h b/src/ChunkDef.h index 1b3dd3ee0..054168bdd 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 COMPRESSED_BLOCKTYPE; + + /** The storage wrapper used for compressed nibbledata residing in RAMz */ + typedef std::vector 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 ) @@ -221,11 +227,11 @@ public: } - static NIBBLETYPE GetNibble(const std::vector & 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())) + if ((size_t)(a_BlockIdx / 2) >= a_Buffer.size()) { return (a_IsSkyLightNibble ? 0xff : 0); } @@ -236,16 +242,16 @@ public: } - static NIBBLETYPE GetNibble(const std::vector & 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())) + 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; @@ -257,54 +263,67 @@ public: 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; + return (a_Buffer[(size_t)(Index / 2)] >> ((Index & 1) * 4)) & 0x0f; } ASSERT(!"cChunkDef::GetNibble(): coords out of chunk range!"); return 0; } - static void SetNibble(std::vector & 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())) + 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( - (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(std::vector & 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) || + (x >= Width) || (x < 0) || (y >= Height) || (y < 0) || - (z >= Width) || (z < 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())) + if ((size_t)(Index / 2) >= a_Buffer.size()) { a_Buffer.resize((size_t)((Index / 2) + 1)); } - a_Buffer[(size_t)(Index / 2)] = static_cast( - (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); + } + + +private: + + + inline static NIBBLETYPE PackNibble(const COMPRESSED_NIBBLETYPE & a_Buffer, int a_Index, NIBBLETYPE a_Nibble) + { + return static_cast( + (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 NIBBLETYPE ExpandNibble(const COMPRESSED_NIBBLETYPE & a_Buffer, int a_Index) + { + return (a_Buffer[a_Index / 2] >> ((a_Index & 1) * 4)) & 0x0f; + } + + } ; -- cgit v1.2.3