From e40f9d6e5b93e840e3d67e79f5ba49da1fbb75f0 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 26 Apr 2014 10:50:23 -0700 Subject: Implemented Chunk Sparsing with segments --- src/BlockArea.cpp | 166 +++++++---------- src/BlockArea.h | 5 +- src/Chunk.cpp | 186 ++++--------------- src/Chunk.h | 32 ++-- src/ChunkBuffer.cpp | 146 +++++++++++++++ src/ChunkBuffer.h | 310 ++++++++++++++++++++++++++++++++ src/ChunkDef.h | 100 +---------- src/ChunkMap.cpp | 16 +- src/ChunkSender.h | 1 + src/Entities/FallingBlock.cpp | 5 +- src/LightingThread.cpp | 11 +- src/MobProximityCounter.cpp | 3 +- src/Simulator/FireSimulator.cpp | 10 +- src/WorldStorage/NBTChunkSerializer.cpp | 3 +- src/WorldStorage/NBTChunkSerializer.h | 4 +- src/WorldStorage/WSSCompact.cpp | 10 +- src/WorldStorage/WSSCompact.h | 3 +- 17 files changed, 610 insertions(+), 401 deletions(-) create mode 100644 src/ChunkBuffer.cpp create mode 100644 src/ChunkBuffer.h (limited to 'src') diff --git a/src/BlockArea.cpp b/src/BlockArea.cpp index 40cca8882..b4b519bc7 100644 --- a/src/BlockArea.cpp +++ b/src/BlockArea.cpp @@ -9,7 +9,7 @@ #include "OSSupport/GZipFile.h" #include "Blocks/BlockHandler.h" #include "Cuboid.h" - +#include "ChunkBuffer.h" @@ -1909,116 +1909,88 @@ bool cBlockArea::cChunkReader::Coords(int a_ChunkX, int a_ChunkZ) -void cBlockArea::cChunkReader::BlockTypes(const BLOCKTYPE * a_BlockTypes) +void cBlockArea::cChunkReader::ChunkBuffer(const cChunkBuffer & a_BlockBuffer) { - if (m_Area.m_BlockTypes == NULL) - { - // Don't want BlockTypes - return; - } - - int SizeY = m_Area.m_Size.y; - int MinY = m_Origin.y; - - // SizeX, SizeZ are the dmensions of the block data to copy from the current chunk (size of the geometric union) - // OffX, OffZ are the offsets of the current chunk data from the area origin - // BaseX, BaseZ are the offsets of the area data within the current chunk from the chunk borders - int SizeX = cChunkDef::Width; - int SizeZ = cChunkDef::Width; - int OffX, OffZ; - int BaseX, BaseZ; - OffX = m_CurrentChunkX * cChunkDef::Width - m_Origin.x; - if (OffX < 0) - { - BaseX = -OffX; - SizeX += OffX; // SizeX is decreased, OffX is negative - OffX = 0; - } - else - { - BaseX = 0; - } - OffZ = m_CurrentChunkZ * cChunkDef::Width - m_Origin.z; - if (OffZ < 0) - { - BaseZ = -OffZ; - SizeZ += OffZ; // SizeZ is decreased, OffZ is negative - OffZ = 0; - } - else - { - BaseZ = 0; - } - // If the chunk extends beyond the area in the X or Z axis, cut off the Size: - if ((m_CurrentChunkX + 1) * cChunkDef::Width > m_Origin.x + m_Area.m_Size.x) - { - SizeX -= (m_CurrentChunkX + 1) * cChunkDef::Width - (m_Origin.x + m_Area.m_Size.x); - } - if ((m_CurrentChunkZ + 1) * cChunkDef::Width > m_Origin.z + m_Area.m_Size.z) - { - SizeZ -= (m_CurrentChunkZ + 1) * cChunkDef::Width - (m_Origin.z + m_Area.m_Size.z); - } - - for (int y = 0; y < SizeY; y++) - { - int ChunkY = MinY + y; - int AreaY = y; - for (int z = 0; z < SizeZ; z++) + { // BlockTypes + if (!(m_Area.m_BlockTypes == NULL)) { - int ChunkZ = BaseZ + z; - int AreaZ = OffZ + z; - for (int x = 0; x < SizeX; x++) + int SizeY = m_Area.m_Size.y; + int MinY = m_Origin.y; + + // SizeX, SizeZ are the dmensions of the block data to copy from the current chunk (size of the geometric union) + // OffX, OffZ are the offsets of the current chunk data from the area origin + // BaseX, BaseZ are the offsets of the area data within the current chunk from the chunk borders + int SizeX = cChunkDef::Width; + int SizeZ = cChunkDef::Width; + int OffX, OffZ; + int BaseX, BaseZ; + OffX = m_CurrentChunkX * cChunkDef::Width - m_Origin.x; + if (OffX < 0) { - int ChunkX = BaseX + x; - int AreaX = OffX + x; - m_Area.m_BlockTypes[m_Area.MakeIndex(AreaX, AreaY, AreaZ)] = cChunkDef::GetBlock(a_BlockTypes, ChunkX, ChunkY, ChunkZ); - } // for x - } // for z - } // for y -} - - - + BaseX = -OffX; + SizeX += OffX; // SizeX is decreased, OffX is negative + OffX = 0; + } + else + { + BaseX = 0; + } + OffZ = m_CurrentChunkZ * cChunkDef::Width - m_Origin.z; + if (OffZ < 0) + { + BaseZ = -OffZ; + SizeZ += OffZ; // SizeZ is decreased, OffZ is negative + OffZ = 0; + } + else + { + BaseZ = 0; + } + // If the chunk extends beyond the area in the X or Z axis, cut off the Size: + if ((m_CurrentChunkX + 1) * cChunkDef::Width > m_Origin.x + m_Area.m_Size.x) + { + SizeX -= (m_CurrentChunkX + 1) * cChunkDef::Width - (m_Origin.x + m_Area.m_Size.x); + } + if ((m_CurrentChunkZ + 1) * cChunkDef::Width > m_Origin.z + m_Area.m_Size.z) + { + SizeZ -= (m_CurrentChunkZ + 1) * cChunkDef::Width - (m_Origin.z + m_Area.m_Size.z); + } + for (int y = 0; y < SizeY; y++) + { + int ChunkY = MinY + y; + int AreaY = y; + for (int z = 0; z < SizeZ; z++) + { + int ChunkZ = BaseZ + z; + int AreaZ = OffZ + z; + for (int x = 0; x < SizeX; x++) + { + int ChunkX = BaseX + x; + int AreaX = OffX + x; + m_Area.m_BlockTypes[m_Area.MakeIndex(AreaX, AreaY, AreaZ)] = a_BlockBuffer.GetBlock(ChunkX, ChunkY, ChunkZ); + } // for x + } // for z + } // for y + } + } -void cBlockArea::cChunkReader::BlockMeta(const NIBBLETYPE * a_BlockMetas) -{ - if (m_Area.m_BlockMetas == NULL) + if (m_Area.m_BlockMetas) { - // Don't want metas - return; + a_BlockBuffer.CopyMeta(m_Area.m_BlockMetas); } - CopyNibbles(m_Area.m_BlockMetas, a_BlockMetas); -} - - - - -void cBlockArea::cChunkReader::BlockLight(const NIBBLETYPE * a_BlockLight) -{ - if (m_Area.m_BlockLight == NULL) + if (m_Area.m_BlockLight) { - // Don't want light - return; + a_BlockBuffer.CopyLight(m_Area.m_BlockLight); } - CopyNibbles(m_Area.m_BlockLight, a_BlockLight); -} - - - - -void cBlockArea::cChunkReader::BlockSkyLight(const NIBBLETYPE * a_BlockSkyLight) -{ - if (m_Area.m_BlockSkyLight == NULL) + if (m_Area.m_BlockSkyLight) { - // Don't want skylight - return; + a_BlockBuffer.CopySkyLight(m_Area.m_BlockSkyLight); } - CopyNibbles(m_Area.m_BlockSkyLight, a_BlockSkyLight); -} +} diff --git a/src/BlockArea.h b/src/BlockArea.h index c48175b8c..d17a68fbb 100644 --- a/src/BlockArea.h +++ b/src/BlockArea.h @@ -317,10 +317,7 @@ protected: // cChunkDataCallback overrides: virtual bool Coords (int a_ChunkX, int a_ChunkZ) override; - virtual void BlockTypes (const BLOCKTYPE * a_BlockTypes) override; - virtual void BlockMeta (const NIBBLETYPE * a_BlockMetas) override; - virtual void BlockLight (const NIBBLETYPE * a_BlockLight) override; - virtual void BlockSkyLight(const NIBBLETYPE * a_BlockSkyLight) override; + virtual void ChunkBuffer (const cChunkBuffer & a_BlockTypes) override; } ; typedef NIBBLETYPE * NIBBLEARRAY; diff --git a/src/Chunk.cpp b/src/Chunk.cpp index ee1531b5c..171f329fb 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -241,23 +241,9 @@ void cChunk::GetAllData(cChunkDataCallback & a_Callback) a_Callback.HeightMap (&m_HeightMap); a_Callback.BiomeData (&m_BiomeMap); - std::vector Blocks = m_BlockTypes; - Blocks.resize(NumBlocks); - a_Callback.BlockTypes (&Blocks[0]); - - std::vector Metas = m_BlockMeta; - Metas.resize(NumBlocks / 2); - a_Callback.BlockMeta (&Metas[0]); - a_Callback.LightIsValid (m_IsLightValid); - std::vector BlockLights = m_BlockLight; - BlockLights.resize(NumBlocks / 2); - a_Callback.BlockLight (&BlockLights[0]); - - std::vector BlockSkyLights = m_BlockSkyLight; - BlockSkyLights.resize(NumBlocks / 2, 0xff); - a_Callback.BlockSkyLight(&BlockSkyLights[0]); + a_Callback.ChunkBuffer (m_ChunkBuffer); for (cEntityList::iterator itr = m_Entities.begin(); itr != m_Entities.end(); ++itr) { @@ -296,48 +282,10 @@ void cChunk::SetAllData( CalculateHeightmap(a_BlockTypes); } - 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++) - { - unsigned char Height = m_HeightMap[x + z * Width]; - if (Height > Highest) - { - Highest = Height; - X = x; Z = z; - } - } - } - - 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 - m_BlockLight.clear(); - m_BlockLight.insert(m_BlockLight.end(), &a_BlockLight[0], &a_BlockLight[IdxWhereNonEmptyStarts / 2]); - } - - if (a_BlockSkyLight != NULL) - { - // Compress skylight - m_BlockSkyLight.clear(); - m_BlockSkyLight.insert(m_BlockSkyLight.end(), &a_BlockSkyLight[0], &a_BlockSkyLight[IdxWhereNonEmptyStarts / 2]); - } + m_ChunkBuffer.SetBlocks (a_BlockTypes); + m_ChunkBuffer.SetMeta (a_BlockMeta); + m_ChunkBuffer.SetLight (a_BlockLight); + m_ChunkBuffer.SetSkyLight (a_BlockSkyLight); m_IsLightValid = (a_BlockLight != NULL) && (a_BlockSkyLight != NULL); @@ -378,39 +326,9 @@ 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 :) - { // 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(); + m_ChunkBuffer.SetLight (a_BlockLight); - 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_ChunkBuffer.SetSkyLight (a_SkyLight); m_IsLightValid = true; } @@ -421,10 +339,7 @@ void cChunk::SetLight( void cChunk::GetBlockTypes(BLOCKTYPE * a_BlockTypes) { - std::vector Blocks = m_BlockTypes; - Blocks.resize(NumBlocks); - - memcpy(a_BlockTypes, &Blocks[0], NumBlocks); + m_ChunkBuffer.CopyBlocks(a_BlockTypes); } @@ -710,8 +625,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(GetBlock(Index)); + cBlockHandler * Handler = BlockHandler(GetBlock(a_RelX, a_RelY, a_RelZ)); ASSERT(Handler != NULL); // Happenned on server restart, FS #243 cChunkInterface ChunkInterface(this->GetWorld()->GetChunkMap()); cBlockInServerPluginInterface PluginInterface(*this->GetWorld()); @@ -836,19 +750,18 @@ void cChunk::CheckBlocks() { return; } - std::vector ToTickBlocks; + std::vector ToTickBlocks; std::swap(m_ToTickBlocks, ToTickBlocks); cChunkInterface ChunkInterface(m_World->GetChunkMap()); cBlockInServerPluginInterface PluginInterface(*m_World); - for (std::vector::const_iterator itr = ToTickBlocks.begin(), end = ToTickBlocks.end(); itr != end; ++itr) + for (std::vector::const_iterator itr = ToTickBlocks.begin(), end = ToTickBlocks.end(); itr != end; ++itr) { - unsigned int index = (*itr); - Vector3i BlockPos = IndexToCoordinate(index); + Vector3i Pos = (*itr); - cBlockHandler * Handler = BlockHandler(GetBlock(index)); - Handler->Check(ChunkInterface, PluginInterface, BlockPos.x, BlockPos.y, BlockPos.z, *this); + cBlockHandler * Handler = BlockHandler(GetBlock(Pos)); + Handler->Check(ChunkInterface, PluginInterface, Pos.x, Pos.y, Pos.z, *this); } // for itr - ToTickBlocks[] } @@ -891,8 +804,7 @@ void cChunk::TickBlocks(void) continue; // It's all air up here } - unsigned int Index = MakeIndexNoCheck(m_BlockTickX, m_BlockTickY, m_BlockTickZ); - cBlockHandler * Handler = BlockHandler(GetBlock(Index)); + cBlockHandler * Handler = BlockHandler(GetBlock(m_BlockTickX, m_BlockTickY, m_BlockTickZ)); 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 @@ -1284,9 +1196,8 @@ bool cChunk::UnboundedRelGetBlockLights(int a_RelX, int a_RelY, int a_RelZ, NIBB // The chunk is not available, bail out return false; } - int idx = Chunk->MakeIndex(a_RelX, a_RelY, a_RelZ); - a_BlockLight = Chunk->GetBlockLight(idx); - a_SkyLight = Chunk->GetSkyLight(idx); + a_BlockLight = Chunk->GetBlockLight(a_RelX, a_RelY, a_RelZ); + a_SkyLight = Chunk->GetSkyLight(a_RelX, a_RelY, a_RelZ); return true; } @@ -1490,11 +1401,9 @@ void cChunk::CalculateHeightmap(const BLOCKTYPE * a_BlockTypes) void cChunk::SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) { FastSetBlock(a_RelX, a_RelY, a_RelZ, a_BlockType, a_BlockMeta); - - const int index = MakeIndexNoCheck(a_RelX, a_RelY, a_RelZ); // Tick this block and its neighbors: - m_ToTickBlocks.push_back(index); + m_ToTickBlocks.push_back(Vector3i(a_RelX, a_RelY, a_RelZ)); QueueTickBlockNeighbors(a_RelX, a_RelY, a_RelZ); // If there was a block entity, remove it: @@ -1557,7 +1466,7 @@ void cChunk::QueueTickBlock(int a_RelX, int a_RelY, int a_RelZ) return; } - m_ToTickBlocks.push_back(MakeIndexNoCheck(a_RelX, a_RelY, a_RelZ)); + m_ToTickBlocks.push_back(Vector3i(a_RelX, a_RelY, a_RelZ)); } @@ -1595,9 +1504,8 @@ 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 = GetBlock(index); - const BLOCKTYPE OldBlockMeta = GetNibble(m_BlockMeta, index); + const BLOCKTYPE OldBlockType = GetBlock(a_RelX, a_RelY, a_RelZ); + const BLOCKTYPE OldBlockMeta = m_ChunkBuffer.GetMeta(a_RelX, a_RelY, a_RelZ); if ((OldBlockType == a_BlockType) && (OldBlockMeta == a_BlockMeta)) { return; @@ -1605,11 +1513,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 */) - { - m_BlockTypes.resize(index + 1); - } - m_BlockTypes[index] = a_BlockType; + m_ChunkBuffer.SetBlock(a_RelX, a_RelY, a_RelZ, a_BlockType); // The client doesn't need to distinguish between stationary and nonstationary fluids: if ( @@ -1625,7 +1529,7 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT m_PendingSendBlocks.push_back(sSetBlock(m_PosX, m_PosZ, a_RelX, a_RelY, a_RelZ, a_BlockType, a_BlockMeta)); } - SetNibble(m_BlockMeta, index, a_BlockMeta); + m_ChunkBuffer.SetMeta(a_RelX, a_RelY, a_RelZ, a_BlockMeta); // ONLY recalculate lighting if it's necessary! if ( @@ -1648,7 +1552,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 (GetBlock(MakeIndexNoCheck(a_RelX, y, a_RelZ)) != E_BLOCK_AIR) + if (GetBlock(a_RelX, y, a_RelZ) != E_BLOCK_AIR) { m_HeightMap[a_RelX + a_RelZ * Width] = (unsigned char)y; break; @@ -1665,18 +1569,16 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT void cChunk::SendBlockTo(int a_RelX, int a_RelY, int a_RelZ, cClientHandle * a_Client) { // The coords must be valid, because the upper level already does chunk lookup. No need to check them again. - // There's an debug-time assert in MakeIndexNoCheck anyway - unsigned int index = MakeIndexNoCheck(a_RelX, a_RelY, a_RelZ); if (a_Client == NULL) { // Queue the block for all clients in the chunk (will be sent in Tick()) - m_PendingSendBlocks.push_back(sSetBlock(m_PosX, m_PosZ, a_RelX, a_RelY, a_RelZ, GetBlock(index), GetMeta(index))); + m_PendingSendBlocks.push_back(sSetBlock(m_PosX, m_PosZ, a_RelX, a_RelY, a_RelZ, GetBlock(a_RelX, a_RelY, a_RelZ), GetMeta(a_RelX, a_RelY, a_RelZ))); return; } Vector3i wp = PositionToWorldPosition(a_RelX, a_RelY, a_RelZ); - a_Client->SendBlockChange(wp.x, wp.y, wp.z, GetBlock(index), GetMeta(index)); + a_Client->SendBlockChange(wp.x, wp.y, wp.z, GetBlock(a_RelX, a_RelY, a_RelZ), GetMeta(a_RelX, a_RelY, a_RelZ)); // FS #268 - if a BlockEntity digging is cancelled by a plugin, the entire block entity must be re-sent to the client: for (cBlockEntityList::iterator itr = m_BlockEntities.begin(), end = m_BlockEntities.end(); itr != end; ++itr) @@ -2535,27 +2437,7 @@ BLOCKTYPE cChunk::GetBlock(int a_RelX, int a_RelY, int a_RelZ) const return 0; // Clip } - return GetBlock(MakeIndexNoCheck(a_RelX, a_RelY, a_RelZ)); -} - - - - - -BLOCKTYPE cChunk::GetBlock(int a_BlockIdx) const -{ - if ((a_BlockIdx < 0) || (a_BlockIdx >= NumBlocks)) - { - ASSERT(!"GetBlock(idx) out of bounds!"); - return 0; - } - - if (m_BlockTypes.empty() || ((size_t)a_BlockIdx > m_BlockTypes.size() - 1) /* Vector starts from zero, .size() starts from 1 */) - { - return E_BLOCK_AIR; - } - - return m_BlockTypes[a_BlockIdx]; + return m_ChunkBuffer.GetBlock(a_RelX, a_RelY, a_RelZ); } @@ -2564,9 +2446,8 @@ 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 = GetBlock(Idx); - a_BlockMeta = cChunkDef::GetNibble(m_BlockMeta, Idx); + a_BlockType = GetBlock(a_RelX, a_RelY, a_RelZ); + a_BlockMeta = m_ChunkBuffer.GetMeta(a_RelX, a_RelY, a_RelZ); } @@ -2575,11 +2456,10 @@ 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 = GetBlock(Idx); - a_Meta = cChunkDef::GetNibble(m_BlockMeta, Idx); - a_SkyLight = cChunkDef::GetNibble(m_BlockSkyLight, Idx); - a_BlockLight = cChunkDef::GetNibble(m_BlockLight, Idx); + a_BlockType = GetBlock(a_RelX, a_RelY, a_RelZ); + a_Meta = m_ChunkBuffer.GetMeta(a_RelX, a_RelY, a_RelZ); + a_SkyLight = m_ChunkBuffer.GetSkyLight(a_RelX, a_RelY, a_RelZ); + a_BlockLight = m_ChunkBuffer.GetBlockLight(a_RelX, a_RelY, a_RelZ); } diff --git a/src/Chunk.h b/src/Chunk.h index 9100eec58..ea3e035ad 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -3,6 +3,7 @@ #include "Entities/Entity.h" #include "ChunkDef.h" +#include "ChunkBuffer.h" #include "Simulator/FireSimulator.h" #include "Simulator/SandSimulator.h" @@ -66,6 +67,7 @@ public: cChunkMap * a_ChunkMap, cWorld * a_World, // Parent objects cChunk * a_NeighborXM, cChunk * a_NeighborXP, cChunk * a_NeighborZM, cChunk * a_NeighborZP // Neighbor chunks ); + cChunk(cChunk& other); ~cChunk(); bool IsValid(void) const {return m_IsValid; } // Returns true if the chunk block data is valid (loaded / generated) @@ -154,7 +156,7 @@ public: void FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, BLOCKTYPE a_BlockMeta ); // Doesn't force block updates on neighbors, use for simple changes such as grass growing etc. BLOCKTYPE GetBlock(int a_RelX, int a_RelY, int a_RelZ) const; - BLOCKTYPE GetBlock(int a_BlockIdx) const; + BLOCKTYPE GetBlock(Vector3i a_cords) const { return GetBlock(a_cords.x,a_cords.y,a_cords.z);} void GetBlockTypeMeta(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta); void GetBlockInfo (int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_Meta, NIBBLETYPE & a_SkyLight, NIBBLETYPE & a_BlockLight); @@ -320,15 +322,17 @@ public: m_BlockTickZ = a_RelZ; } - inline NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const {return cChunkDef::GetNibble(m_BlockMeta, a_RelX, a_RelY, a_RelZ); } - inline NIBBLETYPE GetMeta(int a_BlockIdx) const {return cChunkDef::GetNibble(m_BlockMeta, a_BlockIdx); } - inline void SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Meta) { cChunkDef::SetNibble(m_BlockMeta, a_RelX, a_RelY, a_RelZ, a_Meta); } - inline void SetMeta(int a_BlockIdx, NIBBLETYPE a_Meta) { cChunkDef::SetNibble(m_BlockMeta, a_BlockIdx, a_Meta); } + inline NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const + { + return m_ChunkBuffer.GetMeta(a_RelX, a_RelY, a_RelZ); + } + inline void SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Meta) + { + m_ChunkBuffer.SetMeta(a_RelX, a_RelY, a_RelZ, 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, 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, true); } + inline NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const {return m_ChunkBuffer.GetBlockLight(a_RelX, a_RelY, a_RelZ); } + inline NIBBLETYPE GetSkyLight (int a_RelX, int a_RelY, int a_RelZ) const {return m_ChunkBuffer.GetSkyLight(a_RelX, a_RelY, a_RelZ); } /** 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; @@ -403,8 +407,8 @@ private: bool m_IsSaving; // True if the chunk is being saved bool m_HasLoadFailed; // True if chunk failed to load and hasn't been generated yet since then - std::vector m_ToTickBlocks; - sSetBlockVector m_PendingSendBlocks; ///< Blocks that have changed and need to be sent to all clients + std::vector m_ToTickBlocks; + sSetBlockVector m_PendingSendBlocks; ///< Blocks that have changed and need to be sent to all clients sSetBlockQueueVector m_SetBlockQueue; ///< Block changes that are queued to a specific tick @@ -420,11 +424,7 @@ private: cWorld * m_World; 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; + cChunkBuffer m_ChunkBuffer; cChunkDef::HeightMap m_HeightMap; cChunkDef::BiomeMap m_BiomeMap; diff --git a/src/ChunkBuffer.cpp b/src/ChunkBuffer.cpp new file mode 100644 index 000000000..8e87d3049 --- /dev/null +++ b/src/ChunkBuffer.cpp @@ -0,0 +1,146 @@ + +#include "Globals.h" +#include "ChunkBuffer.h" + +cChunkBuffer cChunkBuffer::Copy() const +{ + cChunkBuffer copy; + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + if(m_Sections[i]) + { + copy.m_Sections[i] = Allocate(); + *copy.m_Sections[i] = *m_Sections[i]; + } + } + return copy; +} + + + + + +void cChunkBuffer::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length) const +{ + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + 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; + memcpy(&a_dest[i * segment_length], &m_Sections[i]->m_BlockTypes, sizeof(BLOCKTYPE) * length); + } + } +} + + + + + +void cChunkBuffer::CopyMeta(NIBBLETYPE * a_dest) const +{ + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + memcpy(&a_dest[i * segment_length], &m_Sections[i]->m_BlockMeta, sizeof(NIBBLETYPE) * segment_length); + } +} + + + + + +void cChunkBuffer::CopyLight(NIBBLETYPE * a_dest) const +{ + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + memcpy(&a_dest[i * segment_length], &m_Sections[i]->m_BlockLight, sizeof(NIBBLETYPE) * segment_length); + } +} + + + + + +void cChunkBuffer::CopySkyLight(NIBBLETYPE * a_dest) const +{ + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + memcpy(&a_dest[i * segment_length], &m_Sections[i]->m_BlockSkyLight, sizeof(NIBBLETYPE) * segment_length); + } +} + + + + + +void cChunkBuffer::SetBlocks(const BLOCKTYPE * a_src) +{ + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + if (m_Sections[i]) + { + memcpy(&m_Sections[i]->m_BlockTypes, &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length); + } + } +} + + + + +void cChunkBuffer::SetMeta(const NIBBLETYPE * a_src) +{ + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + if (m_Sections[i]) + { + memcpy(&m_Sections[i]->m_BlockMeta, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); + } + } +} + + + + +void cChunkBuffer::SetLight(const NIBBLETYPE * a_src) +{ + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + if (m_Sections[i]) + { + memcpy(&m_Sections[i]->m_BlockLight, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); + } + } +} + + + + +void cChunkBuffer::SetSkyLight (const NIBBLETYPE * a_src) +{ + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + if (m_Sections[i]) + { + memcpy(&m_Sections[i]->m_BlockSkyLight, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); + } + } +} + + + + + +cChunkBuffer::sChunkSection * cChunkBuffer::Allocate() const +{ + // TODO: use a allocation pool + return new cChunkBuffer::sChunkSection; +} diff --git a/src/ChunkBuffer.h b/src/ChunkBuffer.h new file mode 100644 index 000000000..44e447c82 --- /dev/null +++ b/src/ChunkBuffer.h @@ -0,0 +1,310 @@ + +#pragma once + +#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 + +class cChunkBuffer +{ +public: + + cChunkBuffer() + #if __cplusplus < 201103L + // auto_ptr style interface for memory management + : IsOwner(true) + #endif + { + memset(m_Sections, 0, sizeof(m_Sections)); + } + ~cChunkBuffer() + { + #if __cplusplus < 201103L + // auto_ptr style interface for memory management + if(!IsOwner) return; + #endif + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + if(m_Sections[i]) delete m_Sections[i]; + } + } + + #if __cplusplus < 201103L + // auto_ptr style interface for memory management + cChunkBuffer(cChunkBuffer& other) : + IsOwner(true); + { + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + m_Sections[i] = other.m_Sections[i]; + } + other.IsOwner = false; + } + void operator=(cChunkBuffer& other) + { + if(IsOwner) + { + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + if(m_Sections[i]) delete m_Sections[i]; + } + } + IsOwner = true; + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + m_Sections[i] = other.m_Sections[i]; + } + other.IsOwner = false; + } + #else + // unique_ptr style interface for memory management + cChunkBuffer(cChunkBuffer&& other) + { + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + m_Sections[i] = other.m_Sections[i]; + } + } + + void operator=(cChunkBuffer&& other) + { + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + if(m_Sections[i]) delete m_Sections[i]; + m_Sections[i] = other.m_Sections[i]; + } + } + #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; + if(m_Sections[Section]) + { + 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) + ) + { + ASSERT(!"cChunkBuffer::SetMeta(): index out of range!"); + return; + } + + int Section = a_RelY / CHUNK_SECTION_HEIGHT; + if(!m_Sections[Section]) + { + m_Sections[Section] = Allocate(); + if(!m_Sections[Section]) + { + ASSERT("Failed to allocate a new section in Chunkbuffer"); + return; + } + } + 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; + if(m_Sections[Section]) + { + 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; + } + } + ASSERT(!"cChunkBuffer::GetMeta(): coords out of chunk range!"); + 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) + ) + { + ASSERT(!"cChunkBuffer::SetMeta(): index out of range!"); + return; + } + + int Section = a_RelY / CHUNK_SECTION_HEIGHT; + if(!m_Sections[Section]) + { + m_Sections[Section] = Allocate(); + if(!m_Sections[Section]) + { + ASSERT("Failed to allocate a new section in Chunkbuffer"); + return; + } + } + int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); + m_Sections[Section]->m_BlockMeta[Index / 2] = static_cast( + (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; + if(m_Sections[Section]) + { + 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; + } + } + ASSERT(!"cChunkBuffer::GetMeta(): coords out of chunk range!"); + 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; + if(m_Sections[Section]) + { + 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 0xFF; + } + } + ASSERT(!"cChunkBuffer::GetMeta(): coords out of chunk range!"); + return 0; + } + + cChunkBuffer Copy() const; + 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 + bool IsOwner; + #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; +}; + + + + +/** A simple implementation of the cChunkDataCallback interface that collects all block data into a buffer +*/ +class cChunkBufferCollector : + public cChunkDataCallback +{ +public: + + cChunkBuffer m_BlockData; + +protected: + + virtual void ChunkBuffer(const cChunkBuffer & a_BlockData) override + { + m_BlockData = a_BlockData.Copy(); + } +}; + + +/** A simple implementation of the cChunkDataCallback interface that collects all block data into a single buffer +*/ +class cChunkDataCollector : +public cChunkDataCallback +{ +public: + + // Must be unsigned char instead of BLOCKTYPE or NIBBLETYPE, because it houses both. + unsigned char m_BlockData[cChunkDef::BlockDataSize]; + +protected: + + virtual void ChunkBuffer(const cChunkBuffer & a_ChunkBuffer) override + { + a_ChunkBuffer.CopyBlocks(m_BlockData); + a_ChunkBuffer.CopyMeta(m_BlockData + cChunkDef::NumBlocks); + a_ChunkBuffer.CopyLight(m_BlockData + 3 * cChunkDef::NumBlocks / 2); + a_ChunkBuffer.CopySkyLight(m_BlockData + 2 * cChunkDef::NumBlocks); + } +}; + +/** A simple implementation of the cChunkDataCallback interface that collects all block data into a separate buffers +*/ +class cChunkDataSeparateCollector : +public cChunkDataCallback +{ +public: + + cChunkDef::BlockTypes m_BlockTypes; + cChunkDef::BlockNibbles m_BlockMetas; + cChunkDef::BlockNibbles m_BlockLight; + cChunkDef::BlockNibbles m_BlockSkyLight; + +protected: + + virtual void ChunkBuffer(const cChunkBuffer & a_ChunkBuffer) override + { + a_ChunkBuffer.CopyBlocks(m_BlockTypes); + a_ChunkBuffer.CopyMeta(m_BlockMetas); + a_ChunkBuffer.CopyLight(m_BlockLight); + a_ChunkBuffer.CopySkyLight(m_BlockSkyLight); + } +} ; + + + + diff --git a/src/ChunkDef.h b/src/ChunkDef.h index bb9f14bbe..a5eccc9d6 100644 --- a/src/ChunkDef.h +++ b/src/ChunkDef.h @@ -366,6 +366,7 @@ public: +class cChunkBuffer; /** Interface class used for getting data out of a chunk using the GetAllData() function. @@ -390,20 +391,11 @@ public: /// Called once to provide biome data virtual void BiomeData (const cChunkDef::BiomeMap * a_BiomeMap) {UNUSED(a_BiomeMap); }; - /// Called once to export block types - virtual void BlockTypes (const BLOCKTYPE * a_Type) {UNUSED(a_Type); }; + /// Called once to let know if the chunk lighting is valid. Return value is ignored + virtual void LightIsValid(bool a_IsLightValid) {UNUSED(a_IsLightValid); }; - /// Called once to export block meta - virtual void BlockMeta (const NIBBLETYPE * a_Meta) {UNUSED(a_Meta); }; - - /// Called once to let know if the chunk lighting is valid. Return value is used to control if BlockLight() and BlockSkyLight() are called next (if true) - virtual bool LightIsValid(bool a_IsLightValid) {UNUSED(a_IsLightValid); return true; }; - - /// Called once to export block light - virtual void BlockLight (const NIBBLETYPE * a_BlockLight) {UNUSED(a_BlockLight); }; - - /// Called once to export sky light - virtual void BlockSkyLight(const NIBBLETYPE * a_SkyLight) {UNUSED(a_SkyLight); }; + /// Called once to export block info + virtual void ChunkBuffer (const cChunkBuffer & a_Buffer) {UNUSED(a_Buffer); }; /// Called for each entity in the chunk virtual void Entity(cEntity * a_Entity) {UNUSED(a_Entity); }; @@ -416,88 +408,6 @@ public: -/** A simple implementation of the cChunkDataCallback interface that collects all block data into a single buffer -*/ -class cChunkDataCollector : - public cChunkDataCallback -{ -public: - - // Must be unsigned char instead of BLOCKTYPE or NIBBLETYPE, because it houses both. - unsigned char m_BlockData[cChunkDef::BlockDataSize]; - -protected: - - virtual void BlockTypes(const BLOCKTYPE * a_BlockTypes) override - { - memcpy(m_BlockData, a_BlockTypes, sizeof(cChunkDef::BlockTypes)); - } - - - virtual void BlockMeta(const NIBBLETYPE * a_BlockMeta) override - { - memcpy(m_BlockData + cChunkDef::NumBlocks, a_BlockMeta, cChunkDef::NumBlocks / 2); - } - - - virtual void BlockLight(const NIBBLETYPE * a_BlockLight) override - { - memcpy(m_BlockData + 3 * cChunkDef::NumBlocks / 2, a_BlockLight, cChunkDef::NumBlocks / 2); - } - - - virtual void BlockSkyLight(const NIBBLETYPE * a_BlockSkyLight) override - { - memcpy(m_BlockData + 2 * cChunkDef::NumBlocks, a_BlockSkyLight, cChunkDef::NumBlocks / 2); - } -} ; - - - - - -/** A simple implementation of the cChunkDataCallback interface that collects all block data into a separate buffers -*/ -class cChunkDataSeparateCollector : - public cChunkDataCallback -{ -public: - - cChunkDef::BlockTypes m_BlockTypes; - cChunkDef::BlockNibbles m_BlockMetas; - cChunkDef::BlockNibbles m_BlockLight; - cChunkDef::BlockNibbles m_BlockSkyLight; - -protected: - - virtual void BlockTypes(const BLOCKTYPE * a_BlockTypes) override - { - memcpy(m_BlockTypes, a_BlockTypes, sizeof(m_BlockTypes)); - } - - - virtual void BlockMeta(const NIBBLETYPE * a_BlockMeta) override - { - memcpy(m_BlockMetas, a_BlockMeta, sizeof(m_BlockMetas)); - } - - - virtual void BlockLight(const NIBBLETYPE * a_BlockLight) override - { - memcpy(m_BlockLight, a_BlockLight, sizeof(m_BlockLight)); - } - - - virtual void BlockSkyLight(const NIBBLETYPE * a_BlockSkyLight) override - { - memcpy(m_BlockSkyLight, a_BlockSkyLight, sizeof(m_BlockSkyLight)); - } -} ; - - - - - /** Interface class used for comparing clients of two chunks. Used primarily for entity moving while both chunks are locked. */ diff --git a/src/ChunkMap.cpp b/src/ChunkMap.cpp index e695f0ab2..2b47f25f8 100644 --- a/src/ChunkMap.cpp +++ b/src/ChunkMap.cpp @@ -219,9 +219,8 @@ bool cChunkMap::LockedGetBlock(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTY return false; } - int Index = cChunkDef::MakeIndexNoCheck(a_BlockX, a_BlockY, a_BlockZ); - a_BlockType = Chunk->GetBlock(Index); - a_BlockMeta = Chunk->GetMeta(Index); + a_BlockType = Chunk->GetBlock(a_BlockX, a_BlockY, a_BlockZ); + a_BlockMeta = Chunk->GetMeta(a_BlockX, a_BlockY, a_BlockZ); return true; } @@ -242,8 +241,7 @@ bool cChunkMap::LockedGetBlockType(int a_BlockX, int a_BlockY, int a_BlockZ, BLO return false; } - int Index = cChunkDef::MakeIndexNoCheck(a_BlockX, a_BlockY, a_BlockZ); - a_BlockType = Chunk->GetBlock(Index); + a_BlockType = Chunk->GetBlock(a_BlockX, a_BlockY, a_BlockZ); return true; } @@ -264,8 +262,7 @@ bool cChunkMap::LockedGetBlockMeta(int a_BlockX, int a_BlockY, int a_BlockZ, NIB return false; } - int Index = cChunkDef::MakeIndexNoCheck(a_BlockX, a_BlockY, a_BlockZ); - a_BlockMeta = Chunk->GetMeta(Index); + a_BlockMeta = Chunk->GetMeta(a_BlockX, a_BlockY, a_BlockZ); return true; } @@ -1486,9 +1483,8 @@ bool cChunkMap::GetBlocks(sSetBlockVector & a_Blocks, bool a_ContinueOnFailure) res = false; continue; } - int idx = cChunkDef::MakeIndexNoCheck(itr->x, itr->y, itr->z); - itr->BlockType = Chunk->GetBlock(idx); - itr->BlockMeta = Chunk->GetMeta(idx); + itr->BlockType = Chunk->GetBlock(itr->x, itr->y, itr->z); + itr->BlockMeta = Chunk->GetMeta(itr->x, itr->y, itr->z); } return res; } diff --git a/src/ChunkSender.h b/src/ChunkSender.h index a26f764a7..81b298a55 100644 --- a/src/ChunkSender.h +++ b/src/ChunkSender.h @@ -27,6 +27,7 @@ Note that it may be called by world's BroadcastToChunk() if the client is still #include "OSSupport/IsThread.h" #include "ChunkDef.h" +#include "ChunkBuffer.h" diff --git a/src/Entities/FallingBlock.cpp b/src/Entities/FallingBlock.cpp index a66c7e4ae..e57c45eaf 100644 --- a/src/Entities/FallingBlock.cpp +++ b/src/Entities/FallingBlock.cpp @@ -55,9 +55,8 @@ void cFallingBlock::Tick(float a_Dt, cChunk & a_Chunk) return; } - int idx = a_Chunk.MakeIndexNoCheck(BlockX - a_Chunk.GetPosX() * cChunkDef::Width, BlockY, BlockZ - a_Chunk.GetPosZ() * cChunkDef::Width); - BLOCKTYPE BlockBelow = a_Chunk.GetBlock(idx); - NIBBLETYPE BelowMeta = a_Chunk.GetMeta(idx); + BLOCKTYPE BlockBelow = a_Chunk.GetBlock(BlockX - a_Chunk.GetPosX() * cChunkDef::Width, BlockY, BlockZ - a_Chunk.GetPosZ() * cChunkDef::Width); + NIBBLETYPE BelowMeta = a_Chunk.GetMeta(BlockX - a_Chunk.GetPosX() * cChunkDef::Width, BlockY, BlockZ - a_Chunk.GetPosZ() * cChunkDef::Width); if (cSandSimulator::DoesBreakFallingThrough(BlockBelow, BelowMeta)) { // Fallen onto a block that breaks this into pickups (e. g. half-slab) diff --git a/src/LightingThread.cpp b/src/LightingThread.cpp index 302473d71..56d5dba22 100644 --- a/src/LightingThread.cpp +++ b/src/LightingThread.cpp @@ -18,20 +18,17 @@ class cReader : public cChunkDataCallback { - virtual void BlockTypes(const BLOCKTYPE * a_Type) override + virtual void ChunkBuffer(const cChunkBuffer & a_ChunkBuffer) override { - // ROW is a block of 16 Blocks, one whole row is copied at a time (hopefully the compiler will optimize that) - // C++ doesn't permit copying arrays, but arrays as a part of a struct is ok :) - typedef struct {BLOCKTYPE m_Row[16]; } ROW; - ROW * InputRows = (ROW *)a_Type; - ROW * OutputRows = (ROW *)m_BlockTypes; + BLOCKTYPE * OutputRows = m_BlockTypes; int InputIdx = 0; int OutputIdx = m_ReadingChunkX + m_ReadingChunkZ * cChunkDef::Width * 3; for (int y = 0; y < cChunkDef::Height; y++) { for (int z = 0; z < cChunkDef::Width; z++) { - OutputRows[OutputIdx] = InputRows[InputIdx++]; + a_ChunkBuffer.CopyBlocks(OutputRows + OutputIdx * 16, InputIdx * 16, 16); + InputIdx++; OutputIdx += 3; } // for z // Skip into the next y-level in the 3x3 chunk blob; each level has cChunkDef::Width * 9 rows diff --git a/src/MobProximityCounter.cpp b/src/MobProximityCounter.cpp index 6c44ea458..e7493dd0f 100644 --- a/src/MobProximityCounter.cpp +++ b/src/MobProximityCounter.cpp @@ -24,7 +24,8 @@ void cMobProximityCounter::CollectMob(cEntity& a_Monster, cChunk& a_Chunk, doubl if (a_Distance < it->second.m_Distance) { it->second.m_Distance = a_Distance; - it->second.m_Chunk = a_Chunk; + ASSERT(false); + //it->second.m_Chunk = a_Chunk; } } diff --git a/src/Simulator/FireSimulator.cpp b/src/Simulator/FireSimulator.cpp index 470dfc791..4c1008897 100644 --- a/src/Simulator/FireSimulator.cpp +++ b/src/Simulator/FireSimulator.cpp @@ -95,8 +95,10 @@ void cFireSimulator::SimulateChunk(float a_Dt, int a_ChunkX, int a_ChunkZ, cChun int NumMSecs = (int)a_Dt; for (cCoordWithIntList::iterator itr = Data.begin(); itr != Data.end();) { - int idx = cChunkDef::MakeIndexNoCheck(itr->x, itr->y, itr->z); - BLOCKTYPE BlockType = a_Chunk->GetBlock(idx); + int x = itr->x; + int y = itr->y; + int z = itr->z; + BLOCKTYPE BlockType = a_Chunk->GetBlock(x,y,z); if (!IsAllowedBlock(BlockType)) { @@ -125,7 +127,7 @@ void cFireSimulator::SimulateChunk(float a_Dt, int a_ChunkX, int a_ChunkZ, cChun itr->x + a_ChunkX * cChunkDef::Width, itr->y, itr->z + a_ChunkZ * cChunkDef::Width ); */ - NIBBLETYPE BlockMeta = a_Chunk->GetMeta(idx); + NIBBLETYPE BlockMeta = a_Chunk->GetMeta(x, y, z); if (BlockMeta == 0x0f) { // The fire burnt out completely @@ -140,7 +142,7 @@ void cFireSimulator::SimulateChunk(float a_Dt, int a_ChunkX, int a_ChunkZ, cChun if((itr->y > 0) && (!DoesBurnForever(a_Chunk->GetBlock(itr->x, itr->y - 1, itr->z)))) { - a_Chunk->SetMeta(idx, BlockMeta + 1); + a_Chunk->SetMeta(x, y, z, BlockMeta + 1); } itr->Data = GetBurnStepTime(a_Chunk, itr->x, itr->y, itr->z); // TODO: Add some randomness into this } // for itr - Data[] diff --git a/src/WorldStorage/NBTChunkSerializer.cpp b/src/WorldStorage/NBTChunkSerializer.cpp index 415693ae2..10acfb537 100644 --- a/src/WorldStorage/NBTChunkSerializer.cpp +++ b/src/WorldStorage/NBTChunkSerializer.cpp @@ -692,10 +692,9 @@ void cNBTChunkSerializer::AddMinecartChestContents(cMinecartWithChest * a_Mineca -bool cNBTChunkSerializer::LightIsValid(bool a_IsLightValid) +void cNBTChunkSerializer::LightIsValid(bool a_IsLightValid) { m_IsLightValid = a_IsLightValid; - return a_IsLightValid; // We want lighting only if it's valid, otherwise don't bother } diff --git a/src/WorldStorage/NBTChunkSerializer.h b/src/WorldStorage/NBTChunkSerializer.h index 51d104970..6da2bc6dd 100644 --- a/src/WorldStorage/NBTChunkSerializer.h +++ b/src/WorldStorage/NBTChunkSerializer.h @@ -9,7 +9,7 @@ #pragma once -#include "../ChunkDef.h" +#include "../ChunkBuffer.h" @@ -121,7 +121,7 @@ protected: void AddMinecartChestContents(cMinecartWithChest * a_Minecart); // cChunkDataSeparateCollector overrides: - virtual bool LightIsValid(bool a_IsLightValid) override; + virtual void LightIsValid(bool a_IsLightValid) override; virtual void BiomeData(const cChunkDef::BiomeMap * a_BiomeMap) override; virtual void Entity(cEntity * a_Entity) override; virtual void BlockEntity(cBlockEntity * a_Entity) override; diff --git a/src/WorldStorage/WSSCompact.cpp b/src/WorldStorage/WSSCompact.cpp index bb9d4b9e6..b44dd02c5 100644 --- a/src/WorldStorage/WSSCompact.cpp +++ b/src/WorldStorage/WSSCompact.cpp @@ -107,15 +107,13 @@ void cJsonChunkSerializer::BlockEntity(cBlockEntity * a_BlockEntity) -bool cJsonChunkSerializer::LightIsValid(bool a_IsLightValid) +void cJsonChunkSerializer::LightIsValid(bool a_IsLightValid) { - if (!a_IsLightValid) + if (a_IsLightValid) { - return false; + m_Root["IsLightValid"] = true; + m_HasJsonData = true; } - m_Root["IsLightValid"] = true; - m_HasJsonData = true; - return true; } diff --git a/src/WorldStorage/WSSCompact.h b/src/WorldStorage/WSSCompact.h index 4df146ec3..49a897984 100644 --- a/src/WorldStorage/WSSCompact.h +++ b/src/WorldStorage/WSSCompact.h @@ -14,6 +14,7 @@ #include "WorldStorage.h" #include "../Vector3.h" #include "json/json.h" +#include "ChunkBuffer.h" @@ -42,7 +43,7 @@ protected: // cChunkDataCollector overrides: virtual void Entity (cEntity * a_Entity) override; virtual void BlockEntity (cBlockEntity * a_Entity) override; - virtual bool LightIsValid (bool a_IsLightValid) override; + virtual void LightIsValid (bool a_IsLightValid) override; } ; -- cgit v1.2.3 From dcb2a590e364301aa1919546ad33a33e5dc34042 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 27 Apr 2014 06:45:33 -0700 Subject: Fixed bad merge --- src/Chunk.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 5a82ca66a..6b0058303 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -1468,7 +1468,7 @@ void cChunk::QueueTickBlock(int a_RelX, int a_RelY, int a_RelZ) return; } - m_ToTickBlocks.push_back(MakeIndexNoCheck(a_RelX, a_RelY, a_RelZ)); + m_ToTickBlocks.push_back(Vector3i(a_RelX, a_RelY, a_RelZ)); } @@ -1531,7 +1531,7 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT m_PendingSendBlocks.push_back(sSetBlock(m_PosX, m_PosZ, a_RelX, a_RelY, a_RelZ, a_BlockType, a_BlockMeta)); } - SetNibble(m_BlockMeta, index, a_BlockMeta); + m_ChunkBuffer.SetMeta(a_RelX, a_RelY, a_RelZ, a_BlockMeta); // ONLY recalculate lighting if it's necessary! if ( @@ -1554,7 +1554,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 (GetBlock(MakeIndexNoCheck(a_RelX, y, a_RelZ)) != E_BLOCK_AIR) + if (GetBlock(a_RelX, y, a_RelZ) != E_BLOCK_AIR) { m_HeightMap[a_RelX + a_RelZ * Width] = (unsigned char)y; break; @@ -1570,19 +1570,16 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT void cChunk::SendBlockTo(int a_RelX, int a_RelY, int a_RelZ, cClientHandle * a_Client) { - // The coords must be valid, because the upper level already does chunk lookup. No need to check them again. - // There's an debug-time assert in MakeIndexNoCheck anyway - unsigned int index = MakeIndexNoCheck(a_RelX, a_RelY, a_RelZ); if (a_Client == NULL) { // Queue the block for all clients in the chunk (will be sent in Tick()) - m_PendingSendBlocks.push_back(sSetBlock(m_PosX, m_PosZ, a_RelX, a_RelY, a_RelZ, GetBlock(index), GetMeta(index))); + m_PendingSendBlocks.push_back(sSetBlock(m_PosX, m_PosZ, a_RelX, a_RelY, a_RelZ, GetBlock(a_RelX, a_RelY, a_RelZ), GetMeta(a_RelX, a_RelY, a_RelZ))); return; } Vector3i wp = PositionToWorldPosition(a_RelX, a_RelY, a_RelZ); - a_Client->SendBlockChange(wp.x, wp.y, wp.z, GetBlock(index), GetMeta(index)); + a_Client->SendBlockChange(wp.x, wp.y, wp.z, GetBlock(a_RelX, a_RelY, a_RelZ), GetMeta(a_RelX, a_RelY, a_RelZ)); // FS #268 - if a BlockEntity digging is cancelled by a plugin, the entire block entity must be re-sent to the client: for (cBlockEntityList::iterator itr = m_BlockEntities.begin(), end = m_BlockEntities.end(); itr != end; ++itr) @@ -2460,11 +2457,10 @@ 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 = GetBlock(Idx); - a_Meta = cChunkDef::GetNibble(m_BlockMeta, Idx); - a_SkyLight = cChunkDef::GetNibble(m_BlockSkyLight, Idx); - a_BlockLight = cChunkDef::GetNibble(m_BlockLight, Idx); + a_BlockType = GetBlock(a_RelX, a_RelY, a_RelZ); + a_Meta = m_ChunkBuffer.GetMeta(a_RelX, a_RelY, a_RelZ); + a_SkyLight = m_ChunkBuffer.GetSkyLight(a_RelX, a_RelY, a_RelZ); + a_BlockLight = m_ChunkBuffer.GetBlockLight(a_RelX, a_RelY, a_RelZ); } -- cgit v1.2.3 From e3bdc81ca04fbad284eb0c3b53678f0fb357eb64 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 27 Apr 2014 06:46:13 -0700 Subject: Fixed MobProximity Counter to remove chunk copys --- src/MobProximityCounter.cpp | 5 ++--- src/MobProximityCounter.h | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/MobProximityCounter.cpp b/src/MobProximityCounter.cpp index c7e91e18b..ce20bf56b 100644 --- a/src/MobProximityCounter.cpp +++ b/src/MobProximityCounter.cpp @@ -24,8 +24,7 @@ void cMobProximityCounter::CollectMob(cEntity& a_Monster, cChunk& a_Chunk, doubl if (a_Distance < it->second.m_Distance) { it->second.m_Distance = a_Distance; - ASSERT(false); - //it->second.m_Chunk = a_Chunk; + it->second.m_Chunk = &a_Chunk; } } @@ -37,7 +36,7 @@ void cMobProximityCounter::convertMaps() { for(tMonsterToDistance::const_iterator itr = m_MonsterToDistance.begin(); itr != m_MonsterToDistance.end(); ++itr) { - m_DistanceToMonster.insert(tDistanceToMonster::value_type(itr->second.m_Distance,sMonsterAndChunk(*itr->first,itr->second.m_Chunk))); + m_DistanceToMonster.insert(tDistanceToMonster::value_type(itr->second.m_Distance,sMonsterAndChunk(*itr->first,*itr->second.m_Chunk))); } } diff --git a/src/MobProximityCounter.h b/src/MobProximityCounter.h index 8a67139aa..79429eb60 100644 --- a/src/MobProximityCounter.h +++ b/src/MobProximityCounter.h @@ -16,9 +16,9 @@ protected : // structs used for later maps (see m_MonsterToDistance and m_DistanceToMonster) struct sDistanceAndChunk { - sDistanceAndChunk(double a_Distance, cChunk& a_Chunk) : m_Distance(a_Distance), m_Chunk(a_Chunk) {} + sDistanceAndChunk(double a_Distance, cChunk& a_Chunk) : m_Distance(a_Distance), m_Chunk(&a_Chunk) {} double m_Distance; - cChunk& m_Chunk; + cChunk* m_Chunk; }; struct sMonsterAndChunk { -- cgit v1.2.3 From 2730a41946b5236894d65ec9cc001ed41b6a5589 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 27 Apr 2014 07:10:30 -0700 Subject: Fixed issues with gcc --- src/ChunkBuffer.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/ChunkBuffer.h b/src/ChunkBuffer.h index 44e447c82..a6a6da013 100644 --- a/src/ChunkBuffer.h +++ b/src/ChunkBuffer.h @@ -36,8 +36,8 @@ public: #if __cplusplus < 201103L // auto_ptr style interface for memory management - cChunkBuffer(cChunkBuffer& other) : - IsOwner(true); + cChunkBuffer(const cChunkBuffer& other) : + IsOwner(true) { for (int i = 0; i < CHUNK_SECTION_NUM; i++) { @@ -45,7 +45,8 @@ public: } other.IsOwner = false; } - void operator=(cChunkBuffer& other) + + void operator=(const cChunkBuffer& other) { if(IsOwner) { @@ -63,7 +64,7 @@ public: } #else // unique_ptr style interface for memory management - cChunkBuffer(cChunkBuffer&& other) + cChunkBuffer(const cChunkBuffer&& other) { for (int i = 0; i < CHUNK_SECTION_NUM; i++) { @@ -71,7 +72,7 @@ public: } } - void operator=(cChunkBuffer&& other) + void operator=(const cChunkBuffer&& other) { for (int i = 0; i < CHUNK_SECTION_NUM; i++) { @@ -225,7 +226,7 @@ private: #if __cplusplus < 201103L // auto_ptr style interface for memory management - bool IsOwner; + mutable bool IsOwner; #endif struct sChunkSection { -- cgit v1.2.3 From 48a2488477023b5427381863585fd1448743d32e Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 27 Apr 2014 07:38:16 -0700 Subject: Added other half of implementation --- src/ChunkBuffer.cpp | 147 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 133 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/ChunkBuffer.cpp b/src/ChunkBuffer.cpp index 8e87d3049..7946fba1e 100644 --- a/src/ChunkBuffer.cpp +++ b/src/ChunkBuffer.cpp @@ -22,7 +22,7 @@ cChunkBuffer cChunkBuffer::Copy() const void cChunkBuffer::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length) const { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16; if (a_Idx > 0) a_Idx = a_Idx > length ? a_Idx - length : 0; @@ -30,7 +30,22 @@ void cChunkBuffer::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length { size_t tocopy = length > segment_length ? segment_length : length; length -= tocopy; - memcpy(&a_dest[i * segment_length], &m_Sections[i]->m_BlockTypes, sizeof(BLOCKTYPE) * length); + if(m_Sections[i]) + { + memcpy( + &a_dest[i * segment_length], + &m_Sections[i]->m_BlockTypes, + sizeof(BLOCKTYPE) * length + ); + } + else + { + memset( + &a_dest[i * segment_length], + 0, + sizeof(BLOCKTYPE) * length + ); + } } } } @@ -41,10 +56,24 @@ void cChunkBuffer::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length void cChunkBuffer::CopyMeta(NIBBLETYPE * a_dest) const { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - memcpy(&a_dest[i * segment_length], &m_Sections[i]->m_BlockMeta, sizeof(NIBBLETYPE) * segment_length); + if(m_Sections[i]) + { + 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 + ); + } } } @@ -54,10 +83,25 @@ void cChunkBuffer::CopyMeta(NIBBLETYPE * a_dest) const void cChunkBuffer::CopyLight(NIBBLETYPE * a_dest) const { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - memcpy(&a_dest[i * segment_length], &m_Sections[i]->m_BlockLight, sizeof(NIBBLETYPE) * segment_length); + if(m_Sections[i]) + { + 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 + ); + } } } @@ -67,10 +111,25 @@ void cChunkBuffer::CopyLight(NIBBLETYPE * a_dest) const void cChunkBuffer::CopySkyLight(NIBBLETYPE * a_dest) const { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - memcpy(&a_dest[i * segment_length], &m_Sections[i]->m_BlockSkyLight, sizeof(NIBBLETYPE) * segment_length); + if(m_Sections[i]) + { + 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 + ); + } } } @@ -80,13 +139,28 @@ void cChunkBuffer::CopySkyLight(NIBBLETYPE * a_dest) const void cChunkBuffer::SetBlocks(const BLOCKTYPE * a_src) { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i]) { memcpy(&m_Sections[i]->m_BlockTypes, &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length); } + else + { + size_t j = 0; + // do nothing whilst 0 + for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); + if (j != segment_length) + { + m_Sections[i] = Allocate(); + memcpy( + &m_Sections[i]->m_BlockTypes, + &a_src[i * segment_length], + sizeof(BLOCKTYPE) * segment_length + ); + } + } } } @@ -95,13 +169,28 @@ void cChunkBuffer::SetBlocks(const BLOCKTYPE * a_src) void cChunkBuffer::SetMeta(const NIBBLETYPE * a_src) { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i]) { memcpy(&m_Sections[i]->m_BlockMeta, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); } + else + { + size_t j = 0; + // do nothing whilst 0 + for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); + if (j != segment_length) + { + m_Sections[i] = Allocate(); + memcpy( + &m_Sections[i]->m_BlockTypes, + &a_src[i * segment_length], + sizeof(BLOCKTYPE) * segment_length + ); + } + } } } @@ -110,14 +199,29 @@ void cChunkBuffer::SetMeta(const NIBBLETYPE * a_src) void cChunkBuffer::SetLight(const NIBBLETYPE * a_src) { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i]) { memcpy(&m_Sections[i]->m_BlockLight, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); } - } + else + { + size_t j = 0; + // do nothing whilst 0 + for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); + if (j != segment_length) + { + m_Sections[i] = Allocate(); + memcpy( + &m_Sections[i]->m_BlockTypes, + &a_src[i * segment_length], + sizeof(BLOCKTYPE) * segment_length + ); + } + } + } } @@ -125,14 +229,29 @@ void cChunkBuffer::SetLight(const NIBBLETYPE * a_src) void cChunkBuffer::SetSkyLight (const NIBBLETYPE * a_src) { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i]) { memcpy(&m_Sections[i]->m_BlockSkyLight, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); } - } + else + { + size_t j = 0; + // do nothing whilst 0 + for (; j < segment_length && a_src[i * segment_length + j] == 0xFF; j++); + if (j != segment_length) + { + m_Sections[i] = Allocate(); + memcpy( + &m_Sections[i]->m_BlockTypes, + &a_src[i * segment_length], + sizeof(BLOCKTYPE) * segment_length + ); + } + } + } } -- cgit v1.2.3 From d412630904c1de6c0d9ef00fbc75b5558f931e8b Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 27 Apr 2014 08:11:56 -0700 Subject: Fixed a couple of segfaults and made Free a seperate function --- src/ChunkBuffer.cpp | 38 +++++++++++++++++++++++++++++++++++--- src/ChunkBuffer.h | 7 ++++--- 2 files changed, 39 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/ChunkBuffer.cpp b/src/ChunkBuffer.cpp index 7946fba1e..a41b8f61a 100644 --- a/src/ChunkBuffer.cpp +++ b/src/ChunkBuffer.cpp @@ -160,6 +160,11 @@ void cChunkBuffer::SetBlocks(const BLOCKTYPE * a_src) sizeof(BLOCKTYPE) * segment_length ); } + else + { + Free(m_Sections[i]); + m_Sections[i] = 0; + } } } } @@ -185,11 +190,16 @@ void cChunkBuffer::SetMeta(const NIBBLETYPE * a_src) { m_Sections[i] = Allocate(); memcpy( - &m_Sections[i]->m_BlockTypes, + &m_Sections[i]->m_BlockMeta, &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length ); } + else + { + Free(m_Sections[i]); + m_Sections[i] = 0; + } } } } @@ -199,6 +209,7 @@ void cChunkBuffer::SetMeta(const NIBBLETYPE * a_src) void cChunkBuffer::SetLight(const NIBBLETYPE * a_src) { + if (!a_src) return; for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; @@ -215,11 +226,16 @@ void cChunkBuffer::SetLight(const NIBBLETYPE * a_src) { m_Sections[i] = Allocate(); memcpy( - &m_Sections[i]->m_BlockTypes, + &m_Sections[i]->m_BlockLight, &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length ); } + else + { + Free(m_Sections[i]); + m_Sections[i] = 0; + } } } } @@ -229,6 +245,7 @@ void cChunkBuffer::SetLight(const NIBBLETYPE * a_src) void cChunkBuffer::SetSkyLight (const NIBBLETYPE * a_src) { + if (!a_src) return; for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; @@ -245,11 +262,16 @@ void cChunkBuffer::SetSkyLight (const NIBBLETYPE * a_src) { m_Sections[i] = Allocate(); memcpy( - &m_Sections[i]->m_BlockTypes, + &m_Sections[i]->m_BlockSkyLight, &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length ); } + else + { + Free(m_Sections[i]); + m_Sections[i] = 0; + } } } } @@ -263,3 +285,13 @@ cChunkBuffer::sChunkSection * cChunkBuffer::Allocate() const // TODO: use a allocation pool return new cChunkBuffer::sChunkSection; } + + + +void cChunkBuffer::Free(cChunkBuffer::sChunkSection * ptr) const +{ + delete ptr; +} + + + diff --git a/src/ChunkBuffer.h b/src/ChunkBuffer.h index a6a6da013..c23f8971d 100644 --- a/src/ChunkBuffer.h +++ b/src/ChunkBuffer.h @@ -30,7 +30,7 @@ public: #endif for (int i = 0; i < CHUNK_SECTION_NUM; i++) { - if(m_Sections[i]) delete m_Sections[i]; + if(m_Sections[i]) Free(m_Sections[i]);; } } @@ -52,7 +52,7 @@ public: { for (int i = 0; i < CHUNK_SECTION_NUM; i++) { - if(m_Sections[i]) delete m_Sections[i]; + if(m_Sections[i]) Free(m_Sections[i]);; } } IsOwner = true; @@ -76,7 +76,7 @@ public: { for (int i = 0; i < CHUNK_SECTION_NUM; i++) { - if(m_Sections[i]) delete m_Sections[i]; + if(m_Sections[i]) Free(m_Sections[i]);; m_Sections[i] = other.m_Sections[i]; } } @@ -239,6 +239,7 @@ private: sChunkSection *m_Sections[CHUNK_SECTION_NUM]; sChunkSection * Allocate() const; + void Free(sChunkSection * ptr) const; }; -- cgit v1.2.3 From 6f1fea759e3595d5538330ecf8f05b8edcf0882e Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 27 Apr 2014 08:14:56 -0700 Subject: Fixed bad comment --- src/BlockArea.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/BlockArea.cpp b/src/BlockArea.cpp index b4b519bc7..68976ab7a 100644 --- a/src/BlockArea.cpp +++ b/src/BlockArea.cpp @@ -1911,7 +1911,7 @@ bool cBlockArea::cChunkReader::Coords(int a_ChunkX, int a_ChunkZ) void cBlockArea::cChunkReader::ChunkBuffer(const cChunkBuffer & a_BlockBuffer) { - { // BlockTypes + { if (!(m_Area.m_BlockTypes == NULL)) { int SizeY = m_Area.m_Size.y; -- cgit v1.2.3 From 616ddf5ca50450279198775a598796bb367cf8ce Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 3 May 2014 06:02:51 -0700 Subject: cCHunkBuffer that compiles with TestGlobals.h --- src/BiomeDef.h | 2 +- src/ChunkBuffer.h | 7 +++++++ src/StringUtils.h | 3 +++ src/Vector3.h | 2 ++ 4 files changed, 13 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/BiomeDef.h b/src/BiomeDef.h index 67916890d..f929596e9 100644 --- a/src/BiomeDef.h +++ b/src/BiomeDef.h @@ -10,7 +10,7 @@ #pragma once - +#include "StringUtils.h" // tolua_begin diff --git a/src/ChunkBuffer.h b/src/ChunkBuffer.h index c23f8971d..410532232 100644 --- a/src/ChunkBuffer.h +++ b/src/ChunkBuffer.h @@ -1,6 +1,13 @@ #pragma once + +#include + + +#include "ChunkDef.h" + + #define CHUNK_SECTION_HEIGHT 16 #define CHUNK_SECTION_NUM (256 / CHUNK_SECTION_HEIGHT) diff --git a/src/StringUtils.h b/src/StringUtils.h index b69e47d3c..347fbe909 100644 --- a/src/StringUtils.h +++ b/src/StringUtils.h @@ -11,6 +11,9 @@ +#include + + typedef std::string AString; diff --git a/src/Vector3.h b/src/Vector3.h index 2c79f9ff1..53fdcf6df 100644 --- a/src/Vector3.h +++ b/src/Vector3.h @@ -5,6 +5,8 @@ #define _USE_MATH_DEFINES // Enable non-standard math defines (MSVC) #include +#include +#include -- cgit v1.2.3 From cb6200345cdfcf34ab4cd6b50cc268152324f9dd Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 10 May 2014 15:19:06 +0100 Subject: Fixed bug in setting metas --- src/Chunk.cpp | 18 ------------------ src/Chunk.h | 8 +++++++- 2 files changed, 7 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 0303e1502..a986ac076 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -1568,24 +1568,6 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT -void cChunk::SetMeta(int a_BlockIdx, NIBBLETYPE a_Meta) -{ - if (GetNibble(m_BlockMeta, a_BlockIdx) == a_Meta) - { - return; - } - - MarkDirty(); - SetNibble(m_BlockMeta, a_BlockIdx, a_Meta); - Vector3i Coords(IndexToCoordinate(a_BlockIdx)); - - m_PendingSendBlocks.push_back(sSetBlock(m_PosX, m_PosZ, Coords.x, Coords.y, Coords.z, GetBlock(a_BlockIdx), a_Meta)); -} - - - - - void cChunk::SendBlockTo(int a_RelX, int a_RelY, int a_RelZ, cClientHandle * a_Client) { diff --git a/src/Chunk.h b/src/Chunk.h index d2328971f..26f1e9d10 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -328,7 +328,13 @@ public: } inline void SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Meta) { - m_ChunkBuffer.SetMeta(a_RelX, a_RelY, a_RelZ, a_Meta); + if (!GetMeta(a_RelX, a_RelY, a_RelZ) == a_Meta) + { + MarkDirty(); + m_ChunkBuffer.SetMeta(a_RelX, a_RelY, a_RelZ, a_Meta); + + m_PendingSendBlocks.push_back(sSetBlock(m_PosX, m_PosZ, a_RelX, a_RelY, a_RelZ, GetBlock(a_RelX, a_RelY, a_RelZ), a_Meta)); + } } inline NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const {return m_ChunkBuffer.GetBlockLight(a_RelX, a_RelY, a_RelZ); } -- cgit v1.2.3 From 0adb5c94b8230b7a65fc06f90c253576755cedcd Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 10 May 2014 17:46:49 +0100 Subject: Fixed stupid buffer overflow in array setblocks --- src/ChunkBuffer.cpp | 6 +++--- src/ChunkBuffer.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/ChunkBuffer.cpp b/src/ChunkBuffer.cpp index a41b8f61a..baeeff890 100644 --- a/src/ChunkBuffer.cpp +++ b/src/ChunkBuffer.cpp @@ -35,7 +35,7 @@ void cChunkBuffer::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length memcpy( &a_dest[i * segment_length], &m_Sections[i]->m_BlockTypes, - sizeof(BLOCKTYPE) * length + sizeof(BLOCKTYPE) * tocopy ); } else @@ -43,7 +43,7 @@ void cChunkBuffer::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length memset( &a_dest[i * segment_length], 0, - sizeof(BLOCKTYPE) * length + sizeof(BLOCKTYPE) * tocopy ); } } @@ -141,7 +141,7 @@ void cChunkBuffer::SetBlocks(const BLOCKTYPE * a_src) { for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16; if (m_Sections[i]) { memcpy(&m_Sections[i]->m_BlockTypes, &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length); diff --git a/src/ChunkBuffer.h b/src/ChunkBuffer.h index 410532232..b1bd024d5 100644 --- a/src/ChunkBuffer.h +++ b/src/ChunkBuffer.h @@ -124,7 +124,7 @@ public: m_Sections[Section] = Allocate(); if(!m_Sections[Section]) { - ASSERT("Failed to allocate a new section in Chunkbuffer"); + ASSERT(!"Failed to allocate a new section in Chunkbuffer"); return; } } @@ -169,7 +169,7 @@ public: m_Sections[Section] = Allocate(); if(!m_Sections[Section]) { - ASSERT("Failed to allocate a new section in Chunkbuffer"); + ASSERT(!"Failed to allocate a new section in Chunkbuffer"); return; } } -- cgit v1.2.3 From 7ca33bd832575ea94f8409087c804feb86280c15 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 10 May 2014 17:56:59 +0100 Subject: Fixed stupid error --- src/Chunk.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/Chunk.h b/src/Chunk.h index 26f1e9d10..038be42de 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -328,7 +328,7 @@ public: } inline void SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Meta) { - if (!GetMeta(a_RelX, a_RelY, a_RelZ) == a_Meta) + if (!(GetMeta(a_RelX, a_RelY, a_RelZ) == a_Meta)) { MarkDirty(); m_ChunkBuffer.SetMeta(a_RelX, a_RelY, a_RelZ, a_Meta); -- cgit v1.2.3 From 9278bb732d115251776b12ebb45d0192c7fdd916 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 11 May 2014 15:52:02 +0100 Subject: Fixed a bug in writting zeros to a non-allocated section --- src/ChunkBuffer.cpp | 11 +++++++++++ src/ChunkBuffer.h | 12 ++++++++++++ 2 files changed, 23 insertions(+) (limited to 'src') diff --git a/src/ChunkBuffer.cpp b/src/ChunkBuffer.cpp index baeeff890..96077b966 100644 --- a/src/ChunkBuffer.cpp +++ b/src/ChunkBuffer.cpp @@ -295,3 +295,14 @@ void cChunkBuffer::Free(cChunkBuffer::sChunkSection * ptr) const +void cChunkBuffer::ZeroSection(cChunkBuffer::sChunkSection * ptr) const +{ + 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,0x00,sizeof(ptr->m_BlockSkyLight)); +} + + + + diff --git a/src/ChunkBuffer.h b/src/ChunkBuffer.h index b1bd024d5..e16575bb2 100644 --- a/src/ChunkBuffer.h +++ b/src/ChunkBuffer.h @@ -121,12 +121,17 @@ public: int Section = a_RelY / CHUNK_SECTION_HEIGHT; if(!m_Sections[Section]) { + if(a_Block == 0x00) + { + return; + } m_Sections[Section] = Allocate(); if(!m_Sections[Section]) { ASSERT(!"Failed to allocate a new section in Chunkbuffer"); return; } + ZeroSection(m_Sections[Section]); } int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); m_Sections[Section]->m_BlockTypes[Index] = a_Block; @@ -166,12 +171,17 @@ public: int Section = a_RelY / CHUNK_SECTION_HEIGHT; if(!m_Sections[Section]) { + if((a_Nibble & 0xf) == 0x00) + { + return; + } m_Sections[Section] = Allocate(); if(!m_Sections[Section]) { ASSERT(!"Failed to allocate a new section in Chunkbuffer"); return; } + ZeroSection(m_Sections[Section]); } int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); m_Sections[Section]->m_BlockMeta[Index / 2] = static_cast( @@ -247,6 +257,8 @@ private: sChunkSection * Allocate() const; void Free(sChunkSection * ptr) const; + + void ZeroSection(sChunkSection * ptr) const; }; -- cgit v1.2.3 From 9568c223ff5b507ab436931ddf716e5978cbc35e Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 11 May 2014 18:24:21 +0100 Subject: SkyLight defaults to 0xFF --- src/ChunkBuffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/ChunkBuffer.cpp b/src/ChunkBuffer.cpp index 96077b966..73ccd9c02 100644 --- a/src/ChunkBuffer.cpp +++ b/src/ChunkBuffer.cpp @@ -300,7 +300,7 @@ void cChunkBuffer::ZeroSection(cChunkBuffer::sChunkSection * ptr) const 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,0x00,sizeof(ptr->m_BlockSkyLight)); + memset(ptr->m_BlockSkyLight,0xFF,sizeof(ptr->m_BlockSkyLight)); } -- cgit v1.2.3 From 5d39ecc64b3c690b2da05936c73c9442511b9509 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 11 May 2014 18:42:38 +0100 Subject: Fixed bug that caused Array Setters to always create segments --- src/ChunkBuffer.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/ChunkBuffer.cpp b/src/ChunkBuffer.cpp index 73ccd9c02..141601113 100644 --- a/src/ChunkBuffer.cpp +++ b/src/ChunkBuffer.cpp @@ -151,7 +151,7 @@ void cChunkBuffer::SetBlocks(const BLOCKTYPE * a_src) size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); - if (j != segment_length) + if (j != (segment_length - 1)) { m_Sections[i] = Allocate(); memcpy( @@ -186,7 +186,7 @@ void cChunkBuffer::SetMeta(const NIBBLETYPE * a_src) size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); - if (j != segment_length) + if (j != (segment_length - 1)) { m_Sections[i] = Allocate(); memcpy( @@ -222,7 +222,7 @@ void cChunkBuffer::SetLight(const NIBBLETYPE * a_src) size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); - if (j != segment_length) + if (j != (segment_length - 1)) { m_Sections[i] = Allocate(); memcpy( @@ -258,7 +258,7 @@ void cChunkBuffer::SetSkyLight (const NIBBLETYPE * a_src) size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0xFF; j++); - if (j != segment_length) + if (j != (segment_length -1)) { m_Sections[i] = Allocate(); memcpy( -- cgit v1.2.3 From c46f240d818e5e6aa7c1ffae95bd03f979aae8ba Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 11 May 2014 19:24:09 +0100 Subject: Added several more testsfor arrays and coordinates --- src/ChunkBuffer.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/ChunkBuffer.cpp b/src/ChunkBuffer.cpp index 141601113..99bcdebef 100644 --- a/src/ChunkBuffer.cpp +++ b/src/ChunkBuffer.cpp @@ -151,7 +151,7 @@ void cChunkBuffer::SetBlocks(const BLOCKTYPE * a_src) size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); - if (j != (segment_length - 1)) + if (j != segment_length) { m_Sections[i] = Allocate(); memcpy( @@ -159,6 +159,9 @@ void cChunkBuffer::SetBlocks(const BLOCKTYPE * a_src) &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length ); + 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)); } else { @@ -186,7 +189,7 @@ void cChunkBuffer::SetMeta(const NIBBLETYPE * a_src) size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); - if (j != (segment_length - 1)) + if (j != segment_length) { m_Sections[i] = Allocate(); memcpy( @@ -194,6 +197,9 @@ void cChunkBuffer::SetMeta(const NIBBLETYPE * a_src) &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length ); + 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)); } else { @@ -222,7 +228,7 @@ void cChunkBuffer::SetLight(const NIBBLETYPE * a_src) size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); - if (j != (segment_length - 1)) + if (j != segment_length) { m_Sections[i] = Allocate(); memcpy( @@ -230,6 +236,9 @@ void cChunkBuffer::SetLight(const NIBBLETYPE * a_src) &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length ); + 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)); } else { @@ -258,7 +267,7 @@ void cChunkBuffer::SetSkyLight (const NIBBLETYPE * a_src) size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0xFF; j++); - if (j != (segment_length -1)) + if (j != segment_length) { m_Sections[i] = Allocate(); memcpy( @@ -266,6 +275,9 @@ void cChunkBuffer::SetSkyLight (const NIBBLETYPE * a_src) &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length ); + 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)); } else { -- cgit v1.2.3 From 7777da379d35b10bee38b2faf218810552452130 Mon Sep 17 00:00:00 2001 From: worktycho Date: Sat, 17 May 2014 14:04:44 +0100 Subject: Fix assignment operators --- src/ChunkBuffer.h | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/ChunkBuffer.h b/src/ChunkBuffer.h index e16575bb2..1f170abba 100644 --- a/src/ChunkBuffer.h +++ b/src/ChunkBuffer.h @@ -53,21 +53,25 @@ public: other.IsOwner = false; } - void operator=(const cChunkBuffer& other) + cChunkBuffer& operator=(const cChunkBuffer& other) { - if(IsOwner) + if(&other == this) { + if(IsOwner) + { + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + if(m_Sections[i]) Free(m_Sections[i]);; + } + } + IsOwner = true; for (int i = 0; i < CHUNK_SECTION_NUM; i++) { - if(m_Sections[i]) Free(m_Sections[i]);; + m_Sections[i] = other.m_Sections[i]; } + other.IsOwner = false; } - IsOwner = true; - for (int i = 0; i < CHUNK_SECTION_NUM; i++) - { - m_Sections[i] = other.m_Sections[i]; - } - other.IsOwner = false; + } #else // unique_ptr style interface for memory management @@ -79,7 +83,7 @@ public: } } - void operator=(const cChunkBuffer&& other) + cChunkBuffer& operator=(const cChunkBuffer&& other) { for (int i = 0; i < CHUNK_SECTION_NUM; i++) { -- cgit v1.2.3 From 4e841146b89523b0fe3f8f75b75c97a29b194e21 Mon Sep 17 00:00:00 2001 From: worktycho Date: Sat, 17 May 2014 14:29:15 +0100 Subject: Fix assignment ops 2 --- src/ChunkBuffer.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/ChunkBuffer.h b/src/ChunkBuffer.h index 1f170abba..66edce867 100644 --- a/src/ChunkBuffer.h +++ b/src/ChunkBuffer.h @@ -71,6 +71,7 @@ public: } other.IsOwner = false; } + return *this; } #else @@ -85,11 +86,15 @@ public: cChunkBuffer& operator=(const cChunkBuffer&& other) { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + if(&other != this) { - if(m_Sections[i]) Free(m_Sections[i]);; - m_Sections[i] = other.m_Sections[i]; + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + if(m_Sections[i]) Free(m_Sections[i]);; + m_Sections[i] = other.m_Sections[i]; + } } + return *this; } #endif -- cgit v1.2.3 From 1f98f21dd1c1664102dfb480a69c8d1901c3ae5f Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 17 May 2014 15:11:58 +0100 Subject: fixed assignment bugs and Skylight bug --- src/ChunkBuffer.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/ChunkBuffer.h b/src/ChunkBuffer.h index 66edce867..4086c4ae9 100644 --- a/src/ChunkBuffer.h +++ b/src/ChunkBuffer.h @@ -76,15 +76,16 @@ public: } #else // unique_ptr style interface for memory management - cChunkBuffer(const cChunkBuffer&& other) + cChunkBuffer(cChunkBuffer&& other) { for (int i = 0; i < CHUNK_SECTION_NUM; i++) { m_Sections[i] = other.m_Sections[i]; + other.m_Sections[i] = 0; } } - cChunkBuffer& operator=(const cChunkBuffer&& other) + cChunkBuffer& operator=(cChunkBuffer&& other) { if(&other != this) { @@ -92,6 +93,7 @@ public: { if(m_Sections[i]) Free(m_Sections[i]);; m_Sections[i] = other.m_Sections[i]; + other.m_Sections[i] = 0; } } return *this; @@ -230,7 +232,7 @@ public: } else { - return 0xFF; + return 0xF; } } ASSERT(!"cChunkBuffer::GetMeta(): coords out of chunk range!"); -- cgit v1.2.3 From f451075c1f60dbea9521113269cc8e69e0730841 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 17 May 2014 15:32:28 +0100 Subject: derp --- src/ChunkBuffer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/ChunkBuffer.h b/src/ChunkBuffer.h index 4086c4ae9..bd178beaa 100644 --- a/src/ChunkBuffer.h +++ b/src/ChunkBuffer.h @@ -55,7 +55,7 @@ public: cChunkBuffer& operator=(const cChunkBuffer& other) { - if(&other == this) + if(&other != this) { if(IsOwner) { -- cgit v1.2.3 From 8a0d3f79219a324c11930c8f22763f34e16a96f6 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sun, 18 May 2014 15:10:12 +0100 Subject: Fixed issue with types not being defined for an unused parameter --- src/BlockArea.h | 1 + src/ChunkBuffer.h | 63 -------------------- src/ChunkDataCallback.h | 105 ++++++++++++++++++++++++++++++++++ src/ChunkDef.h | 40 ------------- src/ChunkMap.h | 3 +- src/ChunkSender.h | 2 +- src/WorldStorage/NBTChunkSerializer.h | 2 +- src/WorldStorage/WSSCompact.h | 2 +- 8 files changed, 111 insertions(+), 107 deletions(-) create mode 100644 src/ChunkDataCallback.h (limited to 'src') diff --git a/src/BlockArea.h b/src/BlockArea.h index 50c28aaf2..9c01719c3 100644 --- a/src/BlockArea.h +++ b/src/BlockArea.h @@ -14,6 +14,7 @@ #include "ForEachChunkProvider.h" #include "Vector3.h" +#include "ChunkDataCallback.h" diff --git a/src/ChunkBuffer.h b/src/ChunkBuffer.h index bd178beaa..266df2332 100644 --- a/src/ChunkBuffer.h +++ b/src/ChunkBuffer.h @@ -275,68 +275,5 @@ private: -/** A simple implementation of the cChunkDataCallback interface that collects all block data into a buffer -*/ -class cChunkBufferCollector : - public cChunkDataCallback -{ -public: - - cChunkBuffer m_BlockData; - -protected: - - virtual void ChunkBuffer(const cChunkBuffer & a_BlockData) override - { - m_BlockData = a_BlockData.Copy(); - } -}; - - -/** A simple implementation of the cChunkDataCallback interface that collects all block data into a single buffer -*/ -class cChunkDataCollector : -public cChunkDataCallback -{ -public: - - // Must be unsigned char instead of BLOCKTYPE or NIBBLETYPE, because it houses both. - unsigned char m_BlockData[cChunkDef::BlockDataSize]; - -protected: - - virtual void ChunkBuffer(const cChunkBuffer & a_ChunkBuffer) override - { - a_ChunkBuffer.CopyBlocks(m_BlockData); - a_ChunkBuffer.CopyMeta(m_BlockData + cChunkDef::NumBlocks); - a_ChunkBuffer.CopyLight(m_BlockData + 3 * cChunkDef::NumBlocks / 2); - a_ChunkBuffer.CopySkyLight(m_BlockData + 2 * cChunkDef::NumBlocks); - } -}; - -/** A simple implementation of the cChunkDataCallback interface that collects all block data into a separate buffers -*/ -class cChunkDataSeparateCollector : -public cChunkDataCallback -{ -public: - - cChunkDef::BlockTypes m_BlockTypes; - cChunkDef::BlockNibbles m_BlockMetas; - cChunkDef::BlockNibbles m_BlockLight; - cChunkDef::BlockNibbles m_BlockSkyLight; - -protected: - - virtual void ChunkBuffer(const cChunkBuffer & a_ChunkBuffer) override - { - a_ChunkBuffer.CopyBlocks(m_BlockTypes); - a_ChunkBuffer.CopyMeta(m_BlockMetas); - a_ChunkBuffer.CopyLight(m_BlockLight); - a_ChunkBuffer.CopySkyLight(m_BlockSkyLight); - } -} ; - - diff --git a/src/ChunkDataCallback.h b/src/ChunkDataCallback.h new file mode 100644 index 000000000..76c45040e --- /dev/null +++ b/src/ChunkDataCallback.h @@ -0,0 +1,105 @@ + + +#pragma once + + +#include "ChunkBuffer.h" + + +/** Interface class used for getting data out of a chunk using the GetAllData() function. +Implementation must use the pointers immediately and NOT store any of them for later use +The virtual methods are called in the same order as they're declared here. +*/ +class cChunkDataCallback abstract +{ +public: + + virtual ~cChunkDataCallback() {} + + /** Called before any other callbacks to inform of the current coords + (only in processes where multiple chunks can be processed, such as cWorld::ForEachChunkInRect()). + If false is returned, the chunk is skipped. + */ + virtual bool Coords(int a_ChunkX, int a_ChunkZ) { UNUSED(a_ChunkX); UNUSED(a_ChunkZ); return true; }; + + /// Called once to provide heightmap data + virtual void HeightMap(const cChunkDef::HeightMap * a_HeightMap) {UNUSED(a_HeightMap); }; + + /// Called once to provide biome data + virtual void BiomeData (const cChunkDef::BiomeMap * a_BiomeMap) {UNUSED(a_BiomeMap); }; + + /// Called once to let know if the chunk lighting is valid. Return value is ignored + virtual void LightIsValid(bool a_IsLightValid) {UNUSED(a_IsLightValid); }; + + /// Called once to export block info + virtual void ChunkBuffer (const cChunkBuffer & a_Buffer) {UNUSED(a_Buffer); }; + + /// Called for each entity in the chunk + virtual void Entity(cEntity * a_Entity) {UNUSED(a_Entity); }; + + /// Called for each blockentity in the chunk + virtual void BlockEntity(cBlockEntity * a_Entity) {UNUSED(a_Entity); }; +} ; + +/** A simple implementation of the cChunkDataCallback interface that collects all block data into a buffer +*/ +class cChunkBufferCollector : + public cChunkDataCallback +{ +public: + + cChunkBuffer m_BlockData; + +protected: + + virtual void ChunkBuffer(const cChunkBuffer & a_BlockData) override + { + m_BlockData = a_BlockData.Copy(); + } +}; + + +/** A simple implementation of the cChunkDataCallback interface that collects all block data into a single buffer +*/ +class cChunkDataCollector : +public cChunkDataCallback +{ +public: + + // Must be unsigned char instead of BLOCKTYPE or NIBBLETYPE, because it houses both. + unsigned char m_BlockData[cChunkDef::BlockDataSize]; + +protected: + + virtual void ChunkBuffer(const cChunkBuffer & a_ChunkBuffer) override + { + a_ChunkBuffer.CopyBlocks(m_BlockData); + a_ChunkBuffer.CopyMeta(m_BlockData + cChunkDef::NumBlocks); + a_ChunkBuffer.CopyLight(m_BlockData + 3 * cChunkDef::NumBlocks / 2); + a_ChunkBuffer.CopySkyLight(m_BlockData + 2 * cChunkDef::NumBlocks); + } +}; + +/** A simple implementation of the cChunkDataCallback interface that collects all block data into a separate buffers +*/ +class cChunkDataSeparateCollector : +public cChunkDataCallback +{ +public: + + cChunkDef::BlockTypes m_BlockTypes; + cChunkDef::BlockNibbles m_BlockMetas; + cChunkDef::BlockNibbles m_BlockLight; + cChunkDef::BlockNibbles m_BlockSkyLight; + +protected: + + virtual void ChunkBuffer(const cChunkBuffer & a_ChunkBuffer) override + { + a_ChunkBuffer.CopyBlocks(m_BlockTypes); + a_ChunkBuffer.CopyMeta(m_BlockMetas); + a_ChunkBuffer.CopyLight(m_BlockLight); + a_ChunkBuffer.CopySkyLight(m_BlockSkyLight); + } +} ; + diff --git a/src/ChunkDef.h b/src/ChunkDef.h index d79f4b92b..f89e16ed1 100644 --- a/src/ChunkDef.h +++ b/src/ChunkDef.h @@ -328,46 +328,6 @@ private: -class cChunkBuffer; - - -/** Interface class used for getting data out of a chunk using the GetAllData() function. -Implementation must use the pointers immediately and NOT store any of them for later use -The virtual methods are called in the same order as they're declared here. -*/ -class cChunkDataCallback abstract -{ -public: - - virtual ~cChunkDataCallback() {} - - /** Called before any other callbacks to inform of the current coords - (only in processes where multiple chunks can be processed, such as cWorld::ForEachChunkInRect()). - If false is returned, the chunk is skipped. - */ - virtual bool Coords(int a_ChunkX, int a_ChunkZ) { UNUSED(a_ChunkX); UNUSED(a_ChunkZ); return true; }; - - /// Called once to provide heightmap data - virtual void HeightMap(const cChunkDef::HeightMap * a_HeightMap) {UNUSED(a_HeightMap); }; - - /// Called once to provide biome data - virtual void BiomeData (const cChunkDef::BiomeMap * a_BiomeMap) {UNUSED(a_BiomeMap); }; - - /// Called once to let know if the chunk lighting is valid. Return value is ignored - virtual void LightIsValid(bool a_IsLightValid) {UNUSED(a_IsLightValid); }; - - /// Called once to export block info - virtual void ChunkBuffer (const cChunkBuffer & a_Buffer) {UNUSED(a_Buffer); }; - - /// Called for each entity in the chunk - virtual void Entity(cEntity * a_Entity) {UNUSED(a_Entity); }; - - /// Called for each blockentity in the chunk - virtual void BlockEntity(cBlockEntity * a_Entity) {UNUSED(a_Entity); }; -} ; - - - /** Interface class used for comparing clients of two chunks. diff --git a/src/ChunkMap.h b/src/ChunkMap.h index 9d973f2a9..c3deda088 100644 --- a/src/ChunkMap.h +++ b/src/ChunkMap.h @@ -5,7 +5,8 @@ #pragma once -#include "ChunkDef.h" + +#include "ChunkDataCallback.h" diff --git a/src/ChunkSender.h b/src/ChunkSender.h index 81b298a55..00565d7c3 100644 --- a/src/ChunkSender.h +++ b/src/ChunkSender.h @@ -27,7 +27,7 @@ Note that it may be called by world's BroadcastToChunk() if the client is still #include "OSSupport/IsThread.h" #include "ChunkDef.h" -#include "ChunkBuffer.h" +#include "ChunkDataCallback.h" diff --git a/src/WorldStorage/NBTChunkSerializer.h b/src/WorldStorage/NBTChunkSerializer.h index 6da2bc6dd..112afc27e 100644 --- a/src/WorldStorage/NBTChunkSerializer.h +++ b/src/WorldStorage/NBTChunkSerializer.h @@ -9,7 +9,7 @@ #pragma once -#include "../ChunkBuffer.h" +#include "ChunkDataCallback.h" diff --git a/src/WorldStorage/WSSCompact.h b/src/WorldStorage/WSSCompact.h index 606853a16..6c363d5ac 100644 --- a/src/WorldStorage/WSSCompact.h +++ b/src/WorldStorage/WSSCompact.h @@ -14,7 +14,7 @@ #include "WorldStorage.h" #include "../Vector3.h" #include "json/json.h" -#include "ChunkBuffer.h" +#include "ChunkDataCallback.h" -- cgit v1.2.3 From cdd3d11496d51ce6f444a2e637fee1d38e07fd09 Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 21 May 2014 18:33:54 +0100 Subject: Fixed minor style issues --- src/BlockArea.cpp | 2 +- src/BlockArea.h | 4 ++-- src/Chunk.cpp | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/BlockArea.cpp b/src/BlockArea.cpp index fd2f3c9b4..5e41b135c 100644 --- a/src/BlockArea.cpp +++ b/src/BlockArea.cpp @@ -1843,7 +1843,7 @@ void cBlockArea::cChunkReader::ChunkBuffer(const cChunkBuffer & a_BlockBuffer) int SizeY = m_Area.m_Size.y; int MinY = m_Origin.y; - // SizeX, SizeZ are the dmensions of the block data to copy from the current chunk (size of the geometric union) + // SizeX, SizeZ are the dimensions of the block data to copy from the current chunk (size of the geometric union) // OffX, OffZ are the offsets of the current chunk data from the area origin // BaseX, BaseZ are the offsets of the area data within the current chunk from the chunk borders int SizeX = cChunkDef::Width; diff --git a/src/BlockArea.h b/src/BlockArea.h index 9c01719c3..6b3bdf337 100644 --- a/src/BlockArea.h +++ b/src/BlockArea.h @@ -317,8 +317,8 @@ protected: void CopyNibbles(NIBBLETYPE * a_AreaDst, const NIBBLETYPE * a_ChunkSrc); // cChunkDataCallback overrides: - virtual bool Coords (int a_ChunkX, int a_ChunkZ) override; - virtual void ChunkBuffer (const cChunkBuffer & a_BlockTypes) override; + virtual bool Coords(int a_ChunkX, int a_ChunkZ) override; + virtual void ChunkBuffer(const cChunkBuffer & a_BlockTypes) override; } ; typedef NIBBLETYPE * NIBBLEARRAY; diff --git a/src/Chunk.cpp b/src/Chunk.cpp index a986ac076..00ea33e16 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -282,10 +282,10 @@ void cChunk::SetAllData( CalculateHeightmap(a_BlockTypes); } - m_ChunkBuffer.SetBlocks (a_BlockTypes); - m_ChunkBuffer.SetMeta (a_BlockMeta); - m_ChunkBuffer.SetLight (a_BlockLight); - m_ChunkBuffer.SetSkyLight (a_BlockSkyLight); + m_ChunkBuffer.SetBlocks(a_BlockTypes); + m_ChunkBuffer.SetMeta(a_BlockMeta); + m_ChunkBuffer.SetLight(a_BlockLight); + m_ChunkBuffer.SetSkyLight(a_BlockSkyLight); m_IsLightValid = (a_BlockLight != NULL) && (a_BlockSkyLight != NULL); -- cgit v1.2.3 From 024027db89ca833406147b79b7be74fc92906bbe Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 21 May 2014 19:58:48 +0100 Subject: Renamed cChunkBuffer to cChunkData --- src/BlockArea.cpp | 4 +- src/BlockArea.h | 2 +- src/Chunk.cpp | 38 ++--- src/Chunk.h | 12 +- src/ChunkBuffer.cpp | 320 ------------------------------------------ src/ChunkBuffer.h | 279 ------------------------------------ src/ChunkData.cpp | 320 ++++++++++++++++++++++++++++++++++++++++++ src/ChunkData.h | 279 ++++++++++++++++++++++++++++++++++++ src/ChunkDataCallback.h | 18 +-- src/LightingThread.cpp | 2 +- src/WorldStorage/WSSCompact.h | 2 +- 11 files changed, 638 insertions(+), 638 deletions(-) delete mode 100644 src/ChunkBuffer.cpp delete mode 100644 src/ChunkBuffer.h create mode 100644 src/ChunkData.cpp create mode 100644 src/ChunkData.h (limited to 'src') diff --git a/src/BlockArea.cpp b/src/BlockArea.cpp index 5e41b135c..0c46e59e5 100644 --- a/src/BlockArea.cpp +++ b/src/BlockArea.cpp @@ -9,7 +9,7 @@ #include "OSSupport/GZipFile.h" #include "Blocks/BlockHandler.h" #include "Cuboid.h" -#include "ChunkBuffer.h" +#include "ChunkData.h" @@ -1835,7 +1835,7 @@ bool cBlockArea::cChunkReader::Coords(int a_ChunkX, int a_ChunkZ) -void cBlockArea::cChunkReader::ChunkBuffer(const cChunkBuffer & a_BlockBuffer) +void cBlockArea::cChunkReader::ChunkData(const cChunkData & a_BlockBuffer) { { if (!(m_Area.m_BlockTypes == NULL)) diff --git a/src/BlockArea.h b/src/BlockArea.h index 6b3bdf337..2bd26facd 100644 --- a/src/BlockArea.h +++ b/src/BlockArea.h @@ -318,7 +318,7 @@ protected: // cChunkDataCallback overrides: virtual bool Coords(int a_ChunkX, int a_ChunkZ) override; - virtual void ChunkBuffer(const cChunkBuffer & a_BlockTypes) override; + virtual void ChunkData(const cChunkData & a_BlockTypes) override; } ; typedef NIBBLETYPE * NIBBLEARRAY; diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 00ea33e16..a45ed32c1 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -238,12 +238,12 @@ void cChunk::MarkLoadFailed(void) void cChunk::GetAllData(cChunkDataCallback & a_Callback) { - a_Callback.HeightMap (&m_HeightMap); - a_Callback.BiomeData (&m_BiomeMap); + a_Callback.HeightMap(&m_HeightMap); + a_Callback.BiomeData(&m_BiomeMap); - a_Callback.LightIsValid (m_IsLightValid); + a_Callback.LightIsValid(m_IsLightValid); - a_Callback.ChunkBuffer (m_ChunkBuffer); + a_Callback.ChunkData(m_ChunkData); for (cEntityList::iterator itr = m_Entities.begin(); itr != m_Entities.end(); ++itr) { @@ -282,10 +282,10 @@ void cChunk::SetAllData( CalculateHeightmap(a_BlockTypes); } - m_ChunkBuffer.SetBlocks(a_BlockTypes); - m_ChunkBuffer.SetMeta(a_BlockMeta); - m_ChunkBuffer.SetLight(a_BlockLight); - m_ChunkBuffer.SetSkyLight(a_BlockSkyLight); + m_ChunkData.SetBlocks(a_BlockTypes); + m_ChunkData.SetMeta(a_BlockMeta); + m_ChunkData.SetLight(a_BlockLight); + m_ChunkData.SetSkyLight(a_BlockSkyLight); m_IsLightValid = (a_BlockLight != NULL) && (a_BlockSkyLight != NULL); @@ -326,9 +326,9 @@ 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 :) - m_ChunkBuffer.SetLight (a_BlockLight); + m_ChunkData.SetLight (a_BlockLight); - m_ChunkBuffer.SetSkyLight (a_SkyLight); + m_ChunkData.SetSkyLight (a_SkyLight); m_IsLightValid = true; } @@ -339,7 +339,7 @@ void cChunk::SetLight( void cChunk::GetBlockTypes(BLOCKTYPE * a_BlockTypes) { - m_ChunkBuffer.CopyBlocks(a_BlockTypes); + m_ChunkData.CopyBlocks(a_BlockTypes); } @@ -1507,7 +1507,7 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT ASSERT(IsValid()); const BLOCKTYPE OldBlockType = GetBlock(a_RelX, a_RelY, a_RelZ); - const BLOCKTYPE OldBlockMeta = m_ChunkBuffer.GetMeta(a_RelX, a_RelY, a_RelZ); + const BLOCKTYPE OldBlockMeta = m_ChunkData.GetMeta(a_RelX, a_RelY, a_RelZ); if ((OldBlockType == a_BlockType) && (OldBlockMeta == a_BlockMeta)) { return; @@ -1515,7 +1515,7 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT MarkDirty(); - m_ChunkBuffer.SetBlock(a_RelX, a_RelY, a_RelZ, a_BlockType); + m_ChunkData.SetBlock(a_RelX, a_RelY, a_RelZ, a_BlockType); // The client doesn't need to distinguish between stationary and nonstationary fluids: if ( @@ -1531,7 +1531,7 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT m_PendingSendBlocks.push_back(sSetBlock(m_PosX, m_PosZ, a_RelX, a_RelY, a_RelZ, a_BlockType, a_BlockMeta)); } - m_ChunkBuffer.SetMeta(a_RelX, a_RelY, a_RelZ, a_BlockMeta); + m_ChunkData.SetMeta(a_RelX, a_RelY, a_RelZ, a_BlockMeta); // ONLY recalculate lighting if it's necessary! if ( @@ -2438,7 +2438,7 @@ BLOCKTYPE cChunk::GetBlock(int a_RelX, int a_RelY, int a_RelZ) const return 0; // Clip } - return m_ChunkBuffer.GetBlock(a_RelX, a_RelY, a_RelZ); + return m_ChunkData.GetBlock(a_RelX, a_RelY, a_RelZ); } @@ -2448,7 +2448,7 @@ BLOCKTYPE cChunk::GetBlock(int a_RelX, int a_RelY, int a_RelZ) const void cChunk::GetBlockTypeMeta(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta) { a_BlockType = GetBlock(a_RelX, a_RelY, a_RelZ); - a_BlockMeta = m_ChunkBuffer.GetMeta(a_RelX, a_RelY, a_RelZ); + a_BlockMeta = m_ChunkData.GetMeta(a_RelX, a_RelY, a_RelZ); } @@ -2458,9 +2458,9 @@ 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) { a_BlockType = GetBlock(a_RelX, a_RelY, a_RelZ); - a_Meta = m_ChunkBuffer.GetMeta(a_RelX, a_RelY, a_RelZ); - a_SkyLight = m_ChunkBuffer.GetSkyLight(a_RelX, a_RelY, a_RelZ); - a_BlockLight = m_ChunkBuffer.GetBlockLight(a_RelX, a_RelY, a_RelZ); + a_Meta = m_ChunkData.GetMeta(a_RelX, a_RelY, a_RelZ); + a_SkyLight = m_ChunkData.GetSkyLight(a_RelX, a_RelY, a_RelZ); + a_BlockLight = m_ChunkData.GetBlockLight(a_RelX, a_RelY, a_RelZ); } diff --git a/src/Chunk.h b/src/Chunk.h index 038be42de..4f6c4cf0a 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -3,7 +3,7 @@ #include "Entities/Entity.h" #include "ChunkDef.h" -#include "ChunkBuffer.h" +#include "ChunkData.h" #include "Simulator/FireSimulator.h" #include "Simulator/SandSimulator.h" @@ -324,21 +324,21 @@ public: inline NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const { - return m_ChunkBuffer.GetMeta(a_RelX, a_RelY, a_RelZ); + return m_ChunkData.GetMeta(a_RelX, a_RelY, a_RelZ); } inline void SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Meta) { if (!(GetMeta(a_RelX, a_RelY, a_RelZ) == a_Meta)) { MarkDirty(); - m_ChunkBuffer.SetMeta(a_RelX, a_RelY, a_RelZ, a_Meta); + m_ChunkData.SetMeta(a_RelX, a_RelY, a_RelZ, a_Meta); m_PendingSendBlocks.push_back(sSetBlock(m_PosX, m_PosZ, a_RelX, a_RelY, a_RelZ, GetBlock(a_RelX, a_RelY, a_RelZ), a_Meta)); } } - inline NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const {return m_ChunkBuffer.GetBlockLight(a_RelX, a_RelY, a_RelZ); } - inline NIBBLETYPE GetSkyLight (int a_RelX, int a_RelY, int a_RelZ) const {return m_ChunkBuffer.GetSkyLight(a_RelX, a_RelY, a_RelZ); } + inline NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const {return m_ChunkData.GetBlockLight(a_RelX, a_RelY, a_RelZ); } + inline NIBBLETYPE GetSkyLight (int a_RelX, int a_RelY, int a_RelZ) const {return m_ChunkData.GetSkyLight(a_RelX, a_RelY, a_RelZ); } /** 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; @@ -430,7 +430,7 @@ private: cWorld * m_World; cChunkMap * m_ChunkMap; - cChunkBuffer m_ChunkBuffer; + cChunkData m_ChunkData; cChunkDef::HeightMap m_HeightMap; cChunkDef::BiomeMap m_BiomeMap; diff --git a/src/ChunkBuffer.cpp b/src/ChunkBuffer.cpp deleted file mode 100644 index 99bcdebef..000000000 --- a/src/ChunkBuffer.cpp +++ /dev/null @@ -1,320 +0,0 @@ - -#include "Globals.h" -#include "ChunkBuffer.h" - -cChunkBuffer cChunkBuffer::Copy() const -{ - cChunkBuffer copy; - for (int i = 0; i < CHUNK_SECTION_NUM; i++) - { - if(m_Sections[i]) - { - copy.m_Sections[i] = Allocate(); - *copy.m_Sections[i] = *m_Sections[i]; - } - } - return copy; -} - - - - - -void cChunkBuffer::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length) const -{ - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) - { - 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; - if(m_Sections[i]) - { - memcpy( - &a_dest[i * segment_length], - &m_Sections[i]->m_BlockTypes, - sizeof(BLOCKTYPE) * tocopy - ); - } - else - { - memset( - &a_dest[i * segment_length], - 0, - sizeof(BLOCKTYPE) * tocopy - ); - } - } - } -} - - - - - -void cChunkBuffer::CopyMeta(NIBBLETYPE * a_dest) const -{ - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) - { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if(m_Sections[i]) - { - 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 - ); - } - } -} - - - - - -void cChunkBuffer::CopyLight(NIBBLETYPE * a_dest) const -{ - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) - { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if(m_Sections[i]) - { - 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 - ); - } - } -} - - - - - -void cChunkBuffer::CopySkyLight(NIBBLETYPE * a_dest) const -{ - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) - { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if(m_Sections[i]) - { - 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 - ); - } - } -} - - - - - -void cChunkBuffer::SetBlocks(const BLOCKTYPE * a_src) -{ - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) - { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16; - if (m_Sections[i]) - { - memcpy(&m_Sections[i]->m_BlockTypes, &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length); - } - else - { - size_t j = 0; - // do nothing whilst 0 - for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); - if (j != segment_length) - { - m_Sections[i] = Allocate(); - memcpy( - &m_Sections[i]->m_BlockTypes, - &a_src[i * segment_length], - sizeof(BLOCKTYPE) * segment_length - ); - 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)); - } - else - { - Free(m_Sections[i]); - m_Sections[i] = 0; - } - } - } -} - - - - -void cChunkBuffer::SetMeta(const NIBBLETYPE * a_src) -{ - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) - { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if (m_Sections[i]) - { - memcpy(&m_Sections[i]->m_BlockMeta, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); - } - else - { - size_t j = 0; - // do nothing whilst 0 - for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); - if (j != segment_length) - { - m_Sections[i] = Allocate(); - memcpy( - &m_Sections[i]->m_BlockMeta, - &a_src[i * segment_length], - sizeof(BLOCKTYPE) * segment_length - ); - 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)); - } - else - { - Free(m_Sections[i]); - m_Sections[i] = 0; - } - } - } -} - - - - -void cChunkBuffer::SetLight(const NIBBLETYPE * a_src) -{ - if (!a_src) return; - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) - { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if (m_Sections[i]) - { - memcpy(&m_Sections[i]->m_BlockLight, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); - } - else - { - size_t j = 0; - // do nothing whilst 0 - for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); - if (j != segment_length) - { - m_Sections[i] = Allocate(); - memcpy( - &m_Sections[i]->m_BlockLight, - &a_src[i * segment_length], - sizeof(BLOCKTYPE) * segment_length - ); - 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)); - } - else - { - Free(m_Sections[i]); - m_Sections[i] = 0; - } - } - } -} - - - - -void cChunkBuffer::SetSkyLight (const NIBBLETYPE * a_src) -{ - if (!a_src) return; - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) - { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if (m_Sections[i]) - { - memcpy(&m_Sections[i]->m_BlockSkyLight, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); - } - else - { - size_t j = 0; - // do nothing whilst 0 - for (; j < segment_length && a_src[i * segment_length + j] == 0xFF; j++); - if (j != segment_length) - { - m_Sections[i] = Allocate(); - memcpy( - &m_Sections[i]->m_BlockSkyLight, - &a_src[i * segment_length], - sizeof(BLOCKTYPE) * segment_length - ); - 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)); - } - else - { - Free(m_Sections[i]); - m_Sections[i] = 0; - } - } - } -} - - - - - -cChunkBuffer::sChunkSection * cChunkBuffer::Allocate() const -{ - // TODO: use a allocation pool - return new cChunkBuffer::sChunkSection; -} - - - -void cChunkBuffer::Free(cChunkBuffer::sChunkSection * ptr) const -{ - delete ptr; -} - - - -void cChunkBuffer::ZeroSection(cChunkBuffer::sChunkSection * ptr) const -{ - 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)); -} - - - - diff --git a/src/ChunkBuffer.h b/src/ChunkBuffer.h deleted file mode 100644 index 266df2332..000000000 --- a/src/ChunkBuffer.h +++ /dev/null @@ -1,279 +0,0 @@ - -#pragma once - - -#include - - -#include "ChunkDef.h" - - -#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 - -class cChunkBuffer -{ -public: - - cChunkBuffer() - #if __cplusplus < 201103L - // auto_ptr style interface for memory management - : IsOwner(true) - #endif - { - memset(m_Sections, 0, sizeof(m_Sections)); - } - ~cChunkBuffer() - { - #if __cplusplus < 201103L - // auto_ptr style interface for memory management - if(!IsOwner) return; - #endif - for (int i = 0; i < CHUNK_SECTION_NUM; i++) - { - if(m_Sections[i]) Free(m_Sections[i]);; - } - } - - #if __cplusplus < 201103L - // auto_ptr style interface for memory management - cChunkBuffer(const cChunkBuffer& other) : - IsOwner(true) - { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) - { - m_Sections[i] = other.m_Sections[i]; - } - other.IsOwner = false; - } - - cChunkBuffer& operator=(const cChunkBuffer& other) - { - if(&other != this) - { - if(IsOwner) - { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) - { - if(m_Sections[i]) Free(m_Sections[i]);; - } - } - IsOwner = true; - for (int i = 0; i < CHUNK_SECTION_NUM; i++) - { - m_Sections[i] = other.m_Sections[i]; - } - other.IsOwner = false; - } - return *this; - - } - #else - // unique_ptr style interface for memory management - cChunkBuffer(cChunkBuffer&& other) - { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) - { - m_Sections[i] = other.m_Sections[i]; - other.m_Sections[i] = 0; - } - } - - cChunkBuffer& operator=(cChunkBuffer&& other) - { - if(&other != this) - { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) - { - if(m_Sections[i]) Free(m_Sections[i]);; - m_Sections[i] = other.m_Sections[i]; - other.m_Sections[i] = 0; - } - } - return *this; - } - #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; - if(m_Sections[Section]) - { - 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) - ) - { - ASSERT(!"cChunkBuffer::SetMeta(): index out of range!"); - return; - } - - int Section = a_RelY / CHUNK_SECTION_HEIGHT; - if(!m_Sections[Section]) - { - if(a_Block == 0x00) - { - return; - } - m_Sections[Section] = Allocate(); - if(!m_Sections[Section]) - { - ASSERT(!"Failed to allocate a new section in Chunkbuffer"); - return; - } - ZeroSection(m_Sections[Section]); - } - 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; - if(m_Sections[Section]) - { - 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; - } - } - ASSERT(!"cChunkBuffer::GetMeta(): coords out of chunk range!"); - 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) - ) - { - ASSERT(!"cChunkBuffer::SetMeta(): index out of range!"); - return; - } - - int Section = a_RelY / CHUNK_SECTION_HEIGHT; - if(!m_Sections[Section]) - { - if((a_Nibble & 0xf) == 0x00) - { - return; - } - m_Sections[Section] = Allocate(); - if(!m_Sections[Section]) - { - ASSERT(!"Failed to allocate a new section in Chunkbuffer"); - return; - } - ZeroSection(m_Sections[Section]); - } - int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); - m_Sections[Section]->m_BlockMeta[Index / 2] = static_cast( - (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; - if(m_Sections[Section]) - { - 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; - } - } - ASSERT(!"cChunkBuffer::GetMeta(): coords out of chunk range!"); - 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; - if(m_Sections[Section]) - { - 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 0xF; - } - } - ASSERT(!"cChunkBuffer::GetMeta(): coords out of chunk range!"); - return 0; - } - - cChunkBuffer Copy() const; - 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 - mutable bool IsOwner; - #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; - void Free(sChunkSection * ptr) const; - - void ZeroSection(sChunkSection * ptr) const; -}; - - - - - - diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp new file mode 100644 index 000000000..160d118ad --- /dev/null +++ b/src/ChunkData.cpp @@ -0,0 +1,320 @@ + +#include "Globals.h" +#include "ChunkData.h" + +cChunkData cChunkData::Copy() const +{ + cChunkData copy; + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + if(m_Sections[i]) + { + copy.m_Sections[i] = Allocate(); + *copy.m_Sections[i] = *m_Sections[i]; + } + } + return copy; +} + + + + + +void cChunkData::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length) const +{ + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + { + 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; + if(m_Sections[i]) + { + memcpy( + &a_dest[i * segment_length], + &m_Sections[i]->m_BlockTypes, + sizeof(BLOCKTYPE) * tocopy + ); + } + else + { + memset( + &a_dest[i * segment_length], + 0, + sizeof(BLOCKTYPE) * tocopy + ); + } + } + } +} + + + + + +void cChunkData::CopyMeta(NIBBLETYPE * a_dest) const +{ + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + { + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + if(m_Sections[i]) + { + 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 + ); + } + } +} + + + + + +void cChunkData::CopyLight(NIBBLETYPE * a_dest) const +{ + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + { + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + if(m_Sections[i]) + { + 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 + ); + } + } +} + + + + + +void cChunkData::CopySkyLight(NIBBLETYPE * a_dest) const +{ + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + { + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + if(m_Sections[i]) + { + 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 + ); + } + } +} + + + + + +void cChunkData::SetBlocks(const BLOCKTYPE * a_src) +{ + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + { + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16; + if (m_Sections[i]) + { + memcpy(&m_Sections[i]->m_BlockTypes, &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length); + } + else + { + size_t j = 0; + // do nothing whilst 0 + for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); + if (j != segment_length) + { + m_Sections[i] = Allocate(); + memcpy( + &m_Sections[i]->m_BlockTypes, + &a_src[i * segment_length], + sizeof(BLOCKTYPE) * segment_length + ); + 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)); + } + else + { + Free(m_Sections[i]); + m_Sections[i] = 0; + } + } + } +} + + + + +void cChunkData::SetMeta(const NIBBLETYPE * a_src) +{ + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + { + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + if (m_Sections[i]) + { + memcpy(&m_Sections[i]->m_BlockMeta, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); + } + else + { + size_t j = 0; + // do nothing whilst 0 + for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); + if (j != segment_length) + { + m_Sections[i] = Allocate(); + memcpy( + &m_Sections[i]->m_BlockMeta, + &a_src[i * segment_length], + sizeof(BLOCKTYPE) * segment_length + ); + 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)); + } + else + { + Free(m_Sections[i]); + m_Sections[i] = 0; + } + } + } +} + + + + +void cChunkData::SetLight(const NIBBLETYPE * a_src) +{ + if (!a_src) return; + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + { + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + if (m_Sections[i]) + { + memcpy(&m_Sections[i]->m_BlockLight, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); + } + else + { + size_t j = 0; + // do nothing whilst 0 + for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); + if (j != segment_length) + { + m_Sections[i] = Allocate(); + memcpy( + &m_Sections[i]->m_BlockLight, + &a_src[i * segment_length], + sizeof(BLOCKTYPE) * segment_length + ); + 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)); + } + else + { + Free(m_Sections[i]); + m_Sections[i] = 0; + } + } + } +} + + + + +void cChunkData::SetSkyLight (const NIBBLETYPE * a_src) +{ + if (!a_src) return; + for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + { + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + if (m_Sections[i]) + { + memcpy(&m_Sections[i]->m_BlockSkyLight, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); + } + else + { + size_t j = 0; + // do nothing whilst 0 + for (; j < segment_length && a_src[i * segment_length + j] == 0xFF; j++); + if (j != segment_length) + { + m_Sections[i] = Allocate(); + memcpy( + &m_Sections[i]->m_BlockSkyLight, + &a_src[i * segment_length], + sizeof(BLOCKTYPE) * segment_length + ); + 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)); + } + else + { + Free(m_Sections[i]); + m_Sections[i] = 0; + } + } + } +} + + + + + +cChunkData::sChunkSection * cChunkData::Allocate() const +{ + // TODO: use a allocation pool + return new cChunkData::sChunkSection; +} + + + +void cChunkData::Free(cChunkData::sChunkSection * ptr) const +{ + delete ptr; +} + + + +void cChunkData::ZeroSection(cChunkData::sChunkSection * ptr) const +{ + 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)); +} + + + + diff --git a/src/ChunkData.h b/src/ChunkData.h new file mode 100644 index 000000000..809f3cdf2 --- /dev/null +++ b/src/ChunkData.h @@ -0,0 +1,279 @@ + +#pragma once + + +#include + + +#include "ChunkDef.h" + + +#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 + +class cChunkData +{ +public: + + cChunkData() + #if __cplusplus < 201103L + // auto_ptr style interface for memory management + : IsOwner(true) + #endif + { + memset(m_Sections, 0, sizeof(m_Sections)); + } + ~cChunkData() + { + #if __cplusplus < 201103L + // auto_ptr style interface for memory management + if(!IsOwner) return; + #endif + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + if(m_Sections[i]) Free(m_Sections[i]);; + } + } + + #if __cplusplus < 201103L + // auto_ptr style interface for memory management + cChunkData(const cChunkData& other) : + IsOwner(true) + { + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + m_Sections[i] = other.m_Sections[i]; + } + other.IsOwner = false; + } + + cChunkData& operator=(const cChunkData& other) + { + if(&other != this) + { + if(IsOwner) + { + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + if(m_Sections[i]) Free(m_Sections[i]);; + } + } + IsOwner = true; + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + m_Sections[i] = other.m_Sections[i]; + } + other.IsOwner = false; + } + return *this; + + } + #else + // unique_ptr style interface for memory management + cChunkData(cChunkData&& other) + { + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + m_Sections[i] = other.m_Sections[i]; + other.m_Sections[i] = 0; + } + } + + cChunkData& operator=(cChunkData&& other) + { + if(&other != this) + { + for (int i = 0; i < CHUNK_SECTION_NUM; i++) + { + if(m_Sections[i]) Free(m_Sections[i]);; + m_Sections[i] = other.m_Sections[i]; + other.m_Sections[i] = 0; + } + } + return *this; + } + #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; + if(m_Sections[Section]) + { + 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) + ) + { + ASSERT(!"cChunkData::SetMeta(): index out of range!"); + return; + } + + int Section = a_RelY / CHUNK_SECTION_HEIGHT; + if(!m_Sections[Section]) + { + if(a_Block == 0x00) + { + return; + } + m_Sections[Section] = Allocate(); + if(!m_Sections[Section]) + { + ASSERT(!"Failed to allocate a new section in Chunkbuffer"); + return; + } + ZeroSection(m_Sections[Section]); + } + 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; + if(m_Sections[Section]) + { + 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; + } + } + ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!"); + 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) + ) + { + ASSERT(!"cChunkData::SetMeta(): index out of range!"); + return; + } + + int Section = a_RelY / CHUNK_SECTION_HEIGHT; + if(!m_Sections[Section]) + { + if((a_Nibble & 0xf) == 0x00) + { + return; + } + m_Sections[Section] = Allocate(); + if(!m_Sections[Section]) + { + ASSERT(!"Failed to allocate a new section in Chunkbuffer"); + return; + } + ZeroSection(m_Sections[Section]); + } + int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); + m_Sections[Section]->m_BlockMeta[Index / 2] = static_cast( + (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; + if(m_Sections[Section]) + { + 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; + } + } + ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!"); + 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; + if(m_Sections[Section]) + { + 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 0xF; + } + } + ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!"); + return 0; + } + + cChunkData Copy() const; + 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 + mutable bool IsOwner; + #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; + void Free(sChunkSection * ptr) const; + + void ZeroSection(sChunkSection * ptr) const; +}; + + + + + + diff --git a/src/ChunkDataCallback.h b/src/ChunkDataCallback.h index 76c45040e..340582885 100644 --- a/src/ChunkDataCallback.h +++ b/src/ChunkDataCallback.h @@ -3,7 +3,7 @@ #pragma once -#include "ChunkBuffer.h" +#include "ChunkData.h" /** Interface class used for getting data out of a chunk using the GetAllData() function. @@ -26,13 +26,13 @@ public: virtual void HeightMap(const cChunkDef::HeightMap * a_HeightMap) {UNUSED(a_HeightMap); }; /// Called once to provide biome data - virtual void BiomeData (const cChunkDef::BiomeMap * a_BiomeMap) {UNUSED(a_BiomeMap); }; + virtual void BiomeData(const cChunkDef::BiomeMap * a_BiomeMap) {UNUSED(a_BiomeMap); }; /// Called once to let know if the chunk lighting is valid. Return value is ignored virtual void LightIsValid(bool a_IsLightValid) {UNUSED(a_IsLightValid); }; /// Called once to export block info - virtual void ChunkBuffer (const cChunkBuffer & a_Buffer) {UNUSED(a_Buffer); }; + virtual void ChunkData(const cChunkData & a_Buffer) {UNUSED(a_Buffer); }; /// Called for each entity in the chunk virtual void Entity(cEntity * a_Entity) {UNUSED(a_Entity); }; @@ -43,16 +43,16 @@ public: /** A simple implementation of the cChunkDataCallback interface that collects all block data into a buffer */ -class cChunkBufferCollector : +class cChunkDataCollector : public cChunkDataCallback { public: - cChunkBuffer m_BlockData; + cChunkData m_BlockData; protected: - virtual void ChunkBuffer(const cChunkBuffer & a_BlockData) override + virtual void ChunkData(const cChunkData & a_BlockData) override { m_BlockData = a_BlockData.Copy(); } @@ -61,7 +61,7 @@ protected: /** A simple implementation of the cChunkDataCallback interface that collects all block data into a single buffer */ -class cChunkDataCollector : +class cChunkDataArrayCollector : public cChunkDataCallback { public: @@ -71,7 +71,7 @@ public: protected: - virtual void ChunkBuffer(const cChunkBuffer & a_ChunkBuffer) override + virtual void ChunkData(const cChunkData & a_ChunkBuffer) override { a_ChunkBuffer.CopyBlocks(m_BlockData); a_ChunkBuffer.CopyMeta(m_BlockData + cChunkDef::NumBlocks); @@ -94,7 +94,7 @@ public: protected: - virtual void ChunkBuffer(const cChunkBuffer & a_ChunkBuffer) override + virtual void ChunkData(const cChunkData & a_ChunkBuffer) override { a_ChunkBuffer.CopyBlocks(m_BlockTypes); a_ChunkBuffer.CopyMeta(m_BlockMetas); diff --git a/src/LightingThread.cpp b/src/LightingThread.cpp index f961e35c6..879252c34 100644 --- a/src/LightingThread.cpp +++ b/src/LightingThread.cpp @@ -18,7 +18,7 @@ class cReader : public cChunkDataCallback { - virtual void ChunkBuffer(const cChunkBuffer & a_ChunkBuffer) override + virtual void ChunkData(const cChunkData & a_ChunkBuffer) override { BLOCKTYPE * OutputRows = m_BlockTypes; int InputIdx = 0; diff --git a/src/WorldStorage/WSSCompact.h b/src/WorldStorage/WSSCompact.h index 6c363d5ac..b148005f6 100644 --- a/src/WorldStorage/WSSCompact.h +++ b/src/WorldStorage/WSSCompact.h @@ -22,7 +22,7 @@ /// Helper class for serializing a chunk into Json class cJsonChunkSerializer : - public cChunkDataCollector + public cChunkDataArrayCollector { public: -- cgit v1.2.3 From 5929ffbc40d24f4e69cf12c8495d194407547c9c Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 21 May 2014 20:08:34 +0100 Subject: Fixed stylistic issues --- src/BlockArea.cpp | 8 ++++---- src/Chunk.cpp | 4 ++-- src/ChunkData.cpp | 18 +++++++++--------- src/ChunkData.h | 20 ++++++++++---------- 4 files changed, 25 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/BlockArea.cpp b/src/BlockArea.cpp index 0c46e59e5..abbfca767 100644 --- a/src/BlockArea.cpp +++ b/src/BlockArea.cpp @@ -1838,7 +1838,7 @@ bool cBlockArea::cChunkReader::Coords(int a_ChunkX, int a_ChunkZ) void cBlockArea::cChunkReader::ChunkData(const cChunkData & a_BlockBuffer) { { - if (!(m_Area.m_BlockTypes == NULL)) + if (m_Area.m_BlockTypes != NULL) { int SizeY = m_Area.m_Size.y; int MinY = m_Origin.y; @@ -1901,17 +1901,17 @@ void cBlockArea::cChunkReader::ChunkData(const cChunkData & a_BlockBuffer) } } - if (m_Area.m_BlockMetas) + if (m_Area.m_BlockMetas != NULL) { a_BlockBuffer.CopyMeta(m_Area.m_BlockMetas); } - if (m_Area.m_BlockLight) + if (m_Area.m_BlockLight != NULL) { a_BlockBuffer.CopyLight(m_Area.m_BlockLight); } - if (m_Area.m_BlockSkyLight) + if (m_Area.m_BlockSkyLight != NULL) { a_BlockBuffer.CopySkyLight(m_Area.m_BlockSkyLight); } diff --git a/src/Chunk.cpp b/src/Chunk.cpp index a45ed32c1..d85b44607 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -326,9 +326,9 @@ 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 :) - m_ChunkData.SetLight (a_BlockLight); + m_ChunkData.SetLight(a_BlockLight); - m_ChunkData.SetSkyLight (a_SkyLight); + m_ChunkData.SetSkyLight(a_SkyLight); m_IsLightValid = true; } diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 160d118ad..7194eca92 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -7,7 +7,7 @@ cChunkData cChunkData::Copy() const cChunkData copy; for (int i = 0; i < CHUNK_SECTION_NUM; i++) { - if(m_Sections[i]) + if(m_Sections[i] == NULL) { copy.m_Sections[i] = Allocate(); *copy.m_Sections[i] = *m_Sections[i]; @@ -30,7 +30,7 @@ void cChunkData::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length) { size_t tocopy = length > segment_length ? segment_length : length; length -= tocopy; - if(m_Sections[i]) + if(m_Sections[i] == NULL) { memcpy( &a_dest[i * segment_length], @@ -59,7 +59,7 @@ void cChunkData::CopyMeta(NIBBLETYPE * a_dest) const for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if(m_Sections[i]) + if(m_Sections[i] == NULL) { memcpy( &a_dest[i * segment_length], @@ -86,7 +86,7 @@ void cChunkData::CopyLight(NIBBLETYPE * a_dest) const for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if(m_Sections[i]) + if(m_Sections[i] == NULL) { memcpy( &a_dest[i * segment_length], @@ -114,7 +114,7 @@ void cChunkData::CopySkyLight(NIBBLETYPE * a_dest) const for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if(m_Sections[i]) + if(m_Sections[i] == NULL) { memcpy( &a_dest[i * segment_length], @@ -142,7 +142,7 @@ void cChunkData::SetBlocks(const BLOCKTYPE * a_src) for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16; - if (m_Sections[i]) + if (m_Sections[i] == NULL) { memcpy(&m_Sections[i]->m_BlockTypes, &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length); } @@ -180,7 +180,7 @@ void cChunkData::SetMeta(const NIBBLETYPE * a_src) for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if (m_Sections[i]) + if (m_Sections[i] == NULL) { memcpy(&m_Sections[i]->m_BlockMeta, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); } @@ -219,7 +219,7 @@ void cChunkData::SetLight(const NIBBLETYPE * a_src) for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if (m_Sections[i]) + if (m_Sections[i] == NULL) { memcpy(&m_Sections[i]->m_BlockLight, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); } @@ -258,7 +258,7 @@ void cChunkData::SetSkyLight (const NIBBLETYPE * a_src) for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if (m_Sections[i]) + if (m_Sections[i] == NULL) { memcpy(&m_Sections[i]->m_BlockSkyLight, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); } diff --git a/src/ChunkData.h b/src/ChunkData.h index 809f3cdf2..24a437629 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -37,7 +37,7 @@ public: #endif for (int i = 0; i < CHUNK_SECTION_NUM; i++) { - if(m_Sections[i]) Free(m_Sections[i]);; + if(m_Sections[i] == NULL) Free(m_Sections[i]);; } } @@ -91,7 +91,7 @@ public: { for (int i = 0; i < CHUNK_SECTION_NUM; i++) { - if(m_Sections[i]) Free(m_Sections[i]);; + Free(m_Sections[i]);; m_Sections[i] = other.m_Sections[i]; other.m_Sections[i] = 0; } @@ -106,7 +106,7 @@ public: ASSERT((a_Y >= 0) && (a_Y < cChunkDef::Height)); ASSERT((a_Z >= 0) && (a_Z < cChunkDef::Width)); int Section = a_Y / CHUNK_SECTION_HEIGHT; - if(m_Sections[Section]) + if(m_Sections[Section] == NULL) { int Index = cChunkDef::MakeIndexNoCheck(a_X, a_Y - (Section * CHUNK_SECTION_HEIGHT), a_Z); return m_Sections[Section]->m_BlockTypes[Index]; @@ -130,14 +130,14 @@ public: } int Section = a_RelY / CHUNK_SECTION_HEIGHT; - if(!m_Sections[Section]) + if(m_Sections[Section] != NULL) { if(a_Block == 0x00) { return; } m_Sections[Section] = Allocate(); - if(!m_Sections[Section]) + if(m_Sections[Section] != NULL) { ASSERT(!"Failed to allocate a new section in Chunkbuffer"); return; @@ -153,7 +153,7 @@ public: 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; - if(m_Sections[Section]) + if(m_Sections[Section] == NULL) { 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; @@ -180,14 +180,14 @@ public: } int Section = a_RelY / CHUNK_SECTION_HEIGHT; - if(!m_Sections[Section]) + if(m_Sections[Section] != NULL) { if((a_Nibble & 0xf) == 0x00) { return; } m_Sections[Section] = Allocate(); - if(!m_Sections[Section]) + if(m_Sections[Section] != NULL) { ASSERT(!"Failed to allocate a new section in Chunkbuffer"); return; @@ -206,7 +206,7 @@ public: 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; - if(m_Sections[Section]) + if(m_Sections[Section] == NULL) { 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; @@ -225,7 +225,7 @@ public: 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; - if(m_Sections[Section]) + if(m_Sections[Section] == NULL) { 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; -- cgit v1.2.3 From bd880603a560160d247d79a2fdeb3fbab26994f0 Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 21 May 2014 20:18:09 +0100 Subject: Changed cChunkData::SetMeta to return a bool indicating whether the value changed --- src/Chunk.h | 6 +++--- src/ChunkData.h | 10 ++++++---- 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/Chunk.h b/src/Chunk.h index 4f6c4cf0a..2de45919e 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -328,11 +328,11 @@ public: } inline void SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Meta) { - if (!(GetMeta(a_RelX, a_RelY, a_RelZ) == a_Meta)) + bool hasChanged = m_ChunkData.SetMeta(a_RelX, a_RelY, a_RelZ, a_Meta); + if (hasChanged) { MarkDirty(); - m_ChunkData.SetMeta(a_RelX, a_RelY, a_RelZ, a_Meta); - + m_PendingSendBlocks.push_back(sSetBlock(m_PosX, m_PosZ, a_RelX, a_RelY, a_RelZ, GetBlock(a_RelX, a_RelY, a_RelZ), a_Meta)); } } diff --git a/src/ChunkData.h b/src/ChunkData.h index 24a437629..73b1e8c6a 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -167,7 +167,7 @@ public: return 0; } - void SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble) + bool SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble) { if ( (a_RelX >= cChunkDef::Width) || (a_RelX < 0) || @@ -176,7 +176,7 @@ public: ) { ASSERT(!"cChunkData::SetMeta(): index out of range!"); - return; + return false; } int Section = a_RelY / CHUNK_SECTION_HEIGHT; @@ -184,21 +184,23 @@ public: { if((a_Nibble & 0xf) == 0x00) { - return; + return false; } m_Sections[Section] = Allocate(); if(m_Sections[Section] != NULL) { ASSERT(!"Failed to allocate a new section in Chunkbuffer"); - return; + return false; } ZeroSection(m_Sections[Section]); } int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); + NIBBLETYPE oldval = m_Sections[Section]->m_BlockMeta[Index / 2] >> ((Index & 1) * 4) & 0xf; m_Sections[Section]->m_BlockMeta[Index / 2] = static_cast( (m_Sections[Section]->m_BlockMeta[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble ((a_Nibble & 0x0f) << ((Index & 1) * 4)) // The nibble being set ); + return oldval == a_Nibble; } NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const -- cgit v1.2.3 From 93c0dcb1feb6daebbea586e750b586ec6588bfa2 Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 21 May 2014 20:26:43 +0100 Subject: Added space to ChunkData.cpp --- src/ChunkData.cpp | 120 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 100 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 7194eca92..f8c4a851d 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -144,7 +144,11 @@ void cChunkData::SetBlocks(const BLOCKTYPE * a_src) const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16; if (m_Sections[i] == NULL) { - memcpy(&m_Sections[i]->m_BlockTypes, &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length); + memcpy( + &m_Sections[i]->m_BlockTypes, + &a_src[i * segment_length], + sizeof(BLOCKTYPE) * segment_length + ); } else { @@ -159,9 +163,21 @@ void cChunkData::SetBlocks(const BLOCKTYPE * a_src) &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length ); - 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)); + 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) + ); } else { @@ -182,7 +198,11 @@ void cChunkData::SetMeta(const NIBBLETYPE * a_src) const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] == NULL) { - memcpy(&m_Sections[i]->m_BlockMeta, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); + memcpy( + &m_Sections[i]->m_BlockMeta, + &a_src[i * segment_length], + sizeof(NIBBLETYPE) * segment_length + ); } else { @@ -197,9 +217,21 @@ void cChunkData::SetMeta(const NIBBLETYPE * a_src) &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length ); - 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)); + 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) + ); } else { @@ -221,7 +253,11 @@ void cChunkData::SetLight(const NIBBLETYPE * a_src) const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] == NULL) { - memcpy(&m_Sections[i]->m_BlockLight, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); + memcpy( + &m_Sections[i]->m_BlockLight, + &a_src[i * segment_length], + sizeof(NIBBLETYPE) * segment_length + ); } else { @@ -236,9 +272,21 @@ void cChunkData::SetLight(const NIBBLETYPE * a_src) &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length ); - 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)); + 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) + ); } else { @@ -260,7 +308,11 @@ void cChunkData::SetSkyLight (const NIBBLETYPE * a_src) const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] == NULL) { - memcpy(&m_Sections[i]->m_BlockSkyLight, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length); + memcpy( + &m_Sections[i]->m_BlockSkyLight, + &a_src[i * segment_length], + sizeof(NIBBLETYPE) * segment_length + ); } else { @@ -275,9 +327,21 @@ void cChunkData::SetSkyLight (const NIBBLETYPE * a_src) &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length ); - 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)); + 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) + ); } else { @@ -309,10 +373,26 @@ void cChunkData::Free(cChunkData::sChunkSection * ptr) const void cChunkData::ZeroSection(cChunkData::sChunkSection * ptr) const { - 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)); + 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) + ); } -- cgit v1.2.3 From 88c61a2e96af9c7d1b173c0b580cef2c348850d7 Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 21 May 2014 21:18:14 +0100 Subject: Fixed reversed comparisons to null --- src/ChunkData.cpp | 18 +++++++++--------- src/ChunkData.h | 16 ++++++++-------- 2 files changed, 17 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index f8c4a851d..0cacaf02d 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -7,7 +7,7 @@ cChunkData cChunkData::Copy() const cChunkData copy; for (int i = 0; i < CHUNK_SECTION_NUM; i++) { - if(m_Sections[i] == NULL) + if(m_Sections[i] != NULL) { copy.m_Sections[i] = Allocate(); *copy.m_Sections[i] = *m_Sections[i]; @@ -30,7 +30,7 @@ void cChunkData::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length) { size_t tocopy = length > segment_length ? segment_length : length; length -= tocopy; - if(m_Sections[i] == NULL) + if(m_Sections[i] != NULL) { memcpy( &a_dest[i * segment_length], @@ -59,7 +59,7 @@ void cChunkData::CopyMeta(NIBBLETYPE * a_dest) const for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if(m_Sections[i] == NULL) + if(m_Sections[i] != NULL) { memcpy( &a_dest[i * segment_length], @@ -86,7 +86,7 @@ void cChunkData::CopyLight(NIBBLETYPE * a_dest) const for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if(m_Sections[i] == NULL) + if(m_Sections[i] != NULL) { memcpy( &a_dest[i * segment_length], @@ -114,7 +114,7 @@ void cChunkData::CopySkyLight(NIBBLETYPE * a_dest) const for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if(m_Sections[i] == NULL) + if(m_Sections[i] != NULL) { memcpy( &a_dest[i * segment_length], @@ -142,7 +142,7 @@ void cChunkData::SetBlocks(const BLOCKTYPE * a_src) for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16; - if (m_Sections[i] == NULL) + if (m_Sections[i] != NULL) { memcpy( &m_Sections[i]->m_BlockTypes, @@ -196,7 +196,7 @@ void cChunkData::SetMeta(const NIBBLETYPE * a_src) for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if (m_Sections[i] == NULL) + if (m_Sections[i] != NULL) { memcpy( &m_Sections[i]->m_BlockMeta, @@ -251,7 +251,7 @@ void cChunkData::SetLight(const NIBBLETYPE * a_src) for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if (m_Sections[i] == NULL) + if (m_Sections[i] != NULL) { memcpy( &m_Sections[i]->m_BlockLight, @@ -306,7 +306,7 @@ void cChunkData::SetSkyLight (const NIBBLETYPE * a_src) for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if (m_Sections[i] == NULL) + if (m_Sections[i] != NULL) { memcpy( &m_Sections[i]->m_BlockSkyLight, diff --git a/src/ChunkData.h b/src/ChunkData.h index 73b1e8c6a..51244225b 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -106,7 +106,7 @@ public: ASSERT((a_Y >= 0) && (a_Y < cChunkDef::Height)); ASSERT((a_Z >= 0) && (a_Z < cChunkDef::Width)); int Section = a_Y / CHUNK_SECTION_HEIGHT; - if(m_Sections[Section] == NULL) + if(m_Sections[Section] != NULL) { int Index = cChunkDef::MakeIndexNoCheck(a_X, a_Y - (Section * CHUNK_SECTION_HEIGHT), a_Z); return m_Sections[Section]->m_BlockTypes[Index]; @@ -130,14 +130,14 @@ public: } int Section = a_RelY / CHUNK_SECTION_HEIGHT; - if(m_Sections[Section] != NULL) + if(m_Sections[Section] == NULL) { if(a_Block == 0x00) { return; } m_Sections[Section] = Allocate(); - if(m_Sections[Section] != NULL) + if(m_Sections[Section] == NULL) { ASSERT(!"Failed to allocate a new section in Chunkbuffer"); return; @@ -153,7 +153,7 @@ public: 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; - if(m_Sections[Section] == NULL) + if(m_Sections[Section] != NULL) { 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; @@ -180,14 +180,14 @@ public: } int Section = a_RelY / CHUNK_SECTION_HEIGHT; - if(m_Sections[Section] != NULL) + if(m_Sections[Section] == NULL) { if((a_Nibble & 0xf) == 0x00) { return false; } m_Sections[Section] = Allocate(); - if(m_Sections[Section] != NULL) + if(m_Sections[Section] == NULL) { ASSERT(!"Failed to allocate a new section in Chunkbuffer"); return false; @@ -208,7 +208,7 @@ public: 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; - if(m_Sections[Section] == NULL) + if(m_Sections[Section] != NULL) { 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; @@ -227,7 +227,7 @@ public: 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; - if(m_Sections[Section] == NULL) + if(m_Sections[Section] != NULL) { 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; -- cgit v1.2.3 From 4b23472097fa2daee464b6c5d73da56a0706731c Mon Sep 17 00:00:00 2001 From: Tycho Date: Wed, 21 May 2014 21:46:20 +0100 Subject: Fixed if spaces --- src/ChunkData.cpp | 13 +++++++------ src/ChunkData.h | 32 ++++++++++++++++---------------- 2 files changed, 23 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 0cacaf02d..86b0c431c 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -7,7 +7,7 @@ cChunkData cChunkData::Copy() const cChunkData copy; for (int i = 0; i < CHUNK_SECTION_NUM; i++) { - if(m_Sections[i] != NULL) + if (m_Sections[i] != NULL) { copy.m_Sections[i] = Allocate(); *copy.m_Sections[i] = *m_Sections[i]; @@ -30,7 +30,7 @@ void cChunkData::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length) { size_t tocopy = length > segment_length ? segment_length : length; length -= tocopy; - if(m_Sections[i] != NULL) + if (m_Sections[i] != NULL) { memcpy( &a_dest[i * segment_length], @@ -59,12 +59,13 @@ void cChunkData::CopyMeta(NIBBLETYPE * a_dest) const for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if(m_Sections[i] != NULL) + if (m_Sections[i] != NULL) { memcpy( &a_dest[i * segment_length], &m_Sections[i]->m_BlockMeta, - sizeof(NIBBLETYPE) * segment_length); + sizeof(NIBBLETYPE) * segment_length + ); } else { @@ -86,7 +87,7 @@ void cChunkData::CopyLight(NIBBLETYPE * a_dest) const for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if(m_Sections[i] != NULL) + if (m_Sections[i] != NULL) { memcpy( &a_dest[i * segment_length], @@ -114,7 +115,7 @@ void cChunkData::CopySkyLight(NIBBLETYPE * a_dest) const for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; - if(m_Sections[i] != NULL) + if (m_Sections[i] != NULL) { memcpy( &a_dest[i * segment_length], diff --git a/src/ChunkData.h b/src/ChunkData.h index 51244225b..9c852ee24 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -33,11 +33,11 @@ public: { #if __cplusplus < 201103L // auto_ptr style interface for memory management - if(!IsOwner) return; + if (!IsOwner) return; #endif for (int i = 0; i < CHUNK_SECTION_NUM; i++) { - if(m_Sections[i] == NULL) Free(m_Sections[i]);; + if (m_Sections[i] == NULL) Free(m_Sections[i]);; } } @@ -55,13 +55,13 @@ public: cChunkData& operator=(const cChunkData& other) { - if(&other != this) + if (&other != this) { - if(IsOwner) + if (IsOwner) { for (int i = 0; i < CHUNK_SECTION_NUM; i++) { - if(m_Sections[i]) Free(m_Sections[i]);; + if (m_Sections[i]) Free(m_Sections[i]);; } } IsOwner = true; @@ -87,7 +87,7 @@ public: cChunkData& operator=(cChunkData&& other) { - if(&other != this) + if (&other != this) { for (int i = 0; i < CHUNK_SECTION_NUM; i++) { @@ -106,7 +106,7 @@ public: ASSERT((a_Y >= 0) && (a_Y < cChunkDef::Height)); ASSERT((a_Z >= 0) && (a_Z < cChunkDef::Width)); int Section = a_Y / CHUNK_SECTION_HEIGHT; - if(m_Sections[Section] != NULL) + if (m_Sections[Section] != NULL) { int Index = cChunkDef::MakeIndexNoCheck(a_X, a_Y - (Section * CHUNK_SECTION_HEIGHT), a_Z); return m_Sections[Section]->m_BlockTypes[Index]; @@ -130,14 +130,14 @@ public: } int Section = a_RelY / CHUNK_SECTION_HEIGHT; - if(m_Sections[Section] == NULL) + if (m_Sections[Section] == NULL) { - if(a_Block == 0x00) + if (a_Block == 0x00) { return; } m_Sections[Section] = Allocate(); - if(m_Sections[Section] == NULL) + if (m_Sections[Section] == NULL) { ASSERT(!"Failed to allocate a new section in Chunkbuffer"); return; @@ -153,7 +153,7 @@ public: 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; - if(m_Sections[Section] != NULL) + if (m_Sections[Section] != NULL) { 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; @@ -180,14 +180,14 @@ public: } int Section = a_RelY / CHUNK_SECTION_HEIGHT; - if(m_Sections[Section] == NULL) + if (m_Sections[Section] == NULL) { - if((a_Nibble & 0xf) == 0x00) + if ((a_Nibble & 0xf) == 0x00) { return false; } m_Sections[Section] = Allocate(); - if(m_Sections[Section] == NULL) + if (m_Sections[Section] == NULL) { ASSERT(!"Failed to allocate a new section in Chunkbuffer"); return false; @@ -208,7 +208,7 @@ public: 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; - if(m_Sections[Section] != NULL) + if (m_Sections[Section] != NULL) { 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; @@ -227,7 +227,7 @@ public: 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; - if(m_Sections[Section] != NULL) + if (m_Sections[Section] != NULL) { 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; -- cgit v1.2.3 From 8f964886e0ccbf51dac07227f0ac4c739b47d3a5 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 24 May 2014 13:33:40 +0100 Subject: Implemented style changes --- src/BlockArea.cpp | 2 +- src/Chunk.cpp | 4 +- src/Chunk.h | 4 +- src/ChunkData.cpp | 61 +++++++++++++-------------- src/ChunkData.h | 108 ++++++++++++++++++++++++++---------------------- src/ChunkDataCallback.h | 8 ++-- 6 files changed, 95 insertions(+), 92 deletions(-) (limited to 'src') diff --git a/src/BlockArea.cpp b/src/BlockArea.cpp index 87236957a..11bd76e6c 100644 --- a/src/BlockArea.cpp +++ b/src/BlockArea.cpp @@ -1908,7 +1908,7 @@ void cBlockArea::cChunkReader::ChunkData(const cChunkData & a_BlockBuffer) if (m_Area.m_BlockLight != NULL) { - a_BlockBuffer.CopyLight(m_Area.m_BlockLight); + a_BlockBuffer.CopyBlockLight(m_Area.m_BlockLight); } if (m_Area.m_BlockSkyLight != NULL) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index d85b44607..e5f8f1e29 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -284,7 +284,7 @@ void cChunk::SetAllData( m_ChunkData.SetBlocks(a_BlockTypes); m_ChunkData.SetMeta(a_BlockMeta); - m_ChunkData.SetLight(a_BlockLight); + m_ChunkData.SetBlockLight(a_BlockLight); m_ChunkData.SetSkyLight(a_BlockSkyLight); m_IsLightValid = (a_BlockLight != NULL) && (a_BlockSkyLight != NULL); @@ -326,7 +326,7 @@ 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 :) - m_ChunkData.SetLight(a_BlockLight); + m_ChunkData.SetBlockLight(a_BlockLight); m_ChunkData.SetSkyLight(a_SkyLight); diff --git a/src/Chunk.h b/src/Chunk.h index 2de45919e..d95537acf 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -67,7 +67,7 @@ public: cChunkMap * a_ChunkMap, cWorld * a_World, // Parent objects cChunk * a_NeighborXM, cChunk * a_NeighborXP, cChunk * a_NeighborZM, cChunk * a_NeighborZP // Neighbor chunks ); - cChunk(cChunk& other); + cChunk(cChunk & other); ~cChunk(); bool IsValid(void) const {return m_IsValid; } // Returns true if the chunk block data is valid (loaded / generated) @@ -156,7 +156,7 @@ public: void FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, BLOCKTYPE a_BlockMeta ); // Doesn't force block updates on neighbors, use for simple changes such as grass growing etc. BLOCKTYPE GetBlock(int a_RelX, int a_RelY, int a_RelZ) const; - BLOCKTYPE GetBlock(Vector3i a_cords) const { return GetBlock(a_cords.x,a_cords.y,a_cords.z);} + BLOCKTYPE GetBlock(Vector3i a_cords) const { return GetBlock(a_cords.x, a_cords.y, a_cords.z);} void GetBlockTypeMeta(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta); void GetBlockInfo (int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_Meta, NIBBLETYPE & a_SkyLight, NIBBLETYPE & a_BlockLight); diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 86b0c431c..098f436f8 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -5,7 +5,7 @@ cChunkData cChunkData::Copy() const { cChunkData copy; - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { if (m_Sections[i] != NULL) { @@ -22,13 +22,16 @@ cChunkData cChunkData::Copy() const void cChunkData::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length) const { - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { 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) + { + a_Idx = std::max(a_Idx - length, (size_t) 0); + } if (a_Idx == 0) { - size_t tocopy = length > segment_length ? segment_length : length; + size_t tocopy = std::min(segment_length, length); length -= tocopy; if (m_Sections[i] != NULL) { @@ -56,7 +59,7 @@ void cChunkData::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length) void cChunkData::CopyMeta(NIBBLETYPE * a_dest) const { - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) @@ -82,9 +85,9 @@ void cChunkData::CopyMeta(NIBBLETYPE * a_dest) const -void cChunkData::CopyLight(NIBBLETYPE * a_dest) const +void cChunkData::CopyBlockLight(NIBBLETYPE * a_dest) const { - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) @@ -112,7 +115,7 @@ void cChunkData::CopyLight(NIBBLETYPE * a_dest) const void cChunkData::CopySkyLight(NIBBLETYPE * a_dest) const { - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) @@ -140,7 +143,7 @@ void cChunkData::CopySkyLight(NIBBLETYPE * a_dest) const void cChunkData::SetBlocks(const BLOCKTYPE * a_src) { - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16; if (m_Sections[i] != NULL) @@ -153,6 +156,9 @@ void cChunkData::SetBlocks(const BLOCKTYPE * a_src) } else { + // j counts how many of leading zeros the buffer has + // if j == segment_length then the buffer is all zeros so there is no point + // creating the buffer. size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); @@ -180,11 +186,6 @@ void cChunkData::SetBlocks(const BLOCKTYPE * a_src) sizeof(m_Sections[i]->m_BlockSkyLight) ); } - else - { - Free(m_Sections[i]); - m_Sections[i] = 0; - } } } } @@ -194,7 +195,7 @@ void cChunkData::SetBlocks(const BLOCKTYPE * a_src) void cChunkData::SetMeta(const NIBBLETYPE * a_src) { - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) @@ -207,6 +208,9 @@ void cChunkData::SetMeta(const NIBBLETYPE * a_src) } else { + // j counts how many of leading zeros the buffer has + // if j == segment_length then the buffer is all zeros so there is no point + // creating the buffer. size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); @@ -234,11 +238,6 @@ void cChunkData::SetMeta(const NIBBLETYPE * a_src) sizeof(m_Sections[i]->m_BlockSkyLight) ); } - else - { - Free(m_Sections[i]); - m_Sections[i] = 0; - } } } } @@ -246,10 +245,10 @@ void cChunkData::SetMeta(const NIBBLETYPE * a_src) -void cChunkData::SetLight(const NIBBLETYPE * a_src) +void cChunkData::SetBlockLight(const NIBBLETYPE * a_src) { if (!a_src) return; - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) @@ -262,6 +261,9 @@ void cChunkData::SetLight(const NIBBLETYPE * a_src) } else { + // j counts how many of leading zeros the buffer has + // if j == segment_length then the buffer is all zeros so there is no point + // creating the buffer. size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); @@ -289,11 +291,6 @@ void cChunkData::SetLight(const NIBBLETYPE * a_src) sizeof(m_Sections[i]->m_BlockSkyLight) ); } - else - { - Free(m_Sections[i]); - m_Sections[i] = 0; - } } } } @@ -304,7 +301,7 @@ void cChunkData::SetLight(const NIBBLETYPE * a_src) void cChunkData::SetSkyLight (const NIBBLETYPE * a_src) { if (!a_src) return; - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) @@ -317,6 +314,9 @@ void cChunkData::SetSkyLight (const NIBBLETYPE * a_src) } else { + // j counts how many of leading zeros the buffer has + // if j == segment_length then the buffer is all zeros so there is no point + // creating the buffer. size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0xFF; j++); @@ -344,11 +344,6 @@ void cChunkData::SetSkyLight (const NIBBLETYPE * a_src) sizeof(m_Sections[i]->m_BlockLight) ); } - else - { - Free(m_Sections[i]); - m_Sections[i] = 0; - } } } } diff --git a/src/ChunkData.h b/src/ChunkData.h index 9c852ee24..5a149f95f 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -8,8 +8,7 @@ #include "ChunkDef.h" -#define CHUNK_SECTION_HEIGHT 16 -#define CHUNK_SECTION_NUM (256 / CHUNK_SECTION_HEIGHT) + #if __cplusplus < 201103L // auto_ptr style interface for memory management @@ -23,8 +22,8 @@ public: cChunkData() #if __cplusplus < 201103L - // auto_ptr style interface for memory management - : IsOwner(true) + // auto_ptr style interface for memory management + : IsOwner(true) #endif { memset(m_Sections, 0, sizeof(m_Sections)); @@ -32,72 +31,75 @@ public: ~cChunkData() { #if __cplusplus < 201103L - // auto_ptr style interface for memory management - if (!IsOwner) return; + // auto_ptr style interface for memory management + if (!IsOwner) + { + return; + } #endif - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { if (m_Sections[i] == NULL) Free(m_Sections[i]);; } } #if __cplusplus < 201103L - // auto_ptr style interface for memory management - cChunkData(const cChunkData& other) : - IsOwner(true) - { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + // auto_ptr style interface for memory management + cChunkData(const cChunkData& other) : + IsOwner(true) { - m_Sections[i] = other.m_Sections[i]; + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + m_Sections[i] = other.m_Sections[i]; + } + other.IsOwner = false; } - other.IsOwner = false; - } - cChunkData& operator=(const cChunkData& other) - { - if (&other != this) + cChunkData& operator=(const cChunkData& other) { - if (IsOwner) + if (&other != this) { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + if (IsOwner) { - if (m_Sections[i]) Free(m_Sections[i]);; + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + if (m_Sections[i]) Free(m_Sections[i]);; + } } + IsOwner = true; + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + m_Sections[i] = other.m_Sections[i]; + } + other.IsOwner = false; } - IsOwner = true; - for (int i = 0; i < CHUNK_SECTION_NUM; i++) - { - m_Sections[i] = other.m_Sections[i]; - } - other.IsOwner = false; - } - return *this; + return *this; - } + } #else - // unique_ptr style interface for memory management - cChunkData(cChunkData&& other) - { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + // unique_ptr style interface for memory management + cChunkData(cChunkData&& other) { - m_Sections[i] = other.m_Sections[i]; - other.m_Sections[i] = 0; + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + m_Sections[i] = other.m_Sections[i]; + other.m_Sections[i] = NULL; + } } - } - cChunkData& operator=(cChunkData&& other) - { - if (&other != this) + cChunkData& operator=(cChunkData&& other) { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + if (&other != this) { - Free(m_Sections[i]);; - m_Sections[i] = other.m_Sections[i]; - other.m_Sections[i] = 0; + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + Free(m_Sections[i]);; + m_Sections[i] = other.m_Sections[i]; + other.m_Sections[i] = NULL; + } } + return *this; } - return *this; - } #endif BLOCKTYPE GetBlock(int a_X, int a_Y, int a_Z) const @@ -150,7 +152,10 @@ public: 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)) + 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; if (m_Sections[Section] != NULL) @@ -244,16 +249,19 @@ public: cChunkData Copy() const; 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 CopyBlockLight (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 SetBlockLight (const NIBBLETYPE * a_src); void SetSkyLight (const NIBBLETYPE * a_src); private: + static const size_t CHUNK_SECTION_HEIGHT = 16; + static const size_t CHUNK_SECTION_COUNT = (256 / CHUNK_SECTION_HEIGHT); + #if __cplusplus < 201103L // auto_ptr style interface for memory management mutable bool IsOwner; @@ -266,7 +274,7 @@ private: NIBBLETYPE m_BlockSkyLight[CHUNK_SECTION_HEIGHT * 16 * 16 / 2]; }; - sChunkSection *m_Sections[CHUNK_SECTION_NUM]; + sChunkSection *m_Sections[CHUNK_SECTION_COUNT]; sChunkSection * Allocate() const; void Free(sChunkSection * ptr) const; diff --git a/src/ChunkDataCallback.h b/src/ChunkDataCallback.h index 340582885..e916d6486 100644 --- a/src/ChunkDataCallback.h +++ b/src/ChunkDataCallback.h @@ -62,7 +62,7 @@ protected: /** A simple implementation of the cChunkDataCallback interface that collects all block data into a single buffer */ class cChunkDataArrayCollector : -public cChunkDataCallback + public cChunkDataCallback { public: @@ -75,7 +75,7 @@ protected: { a_ChunkBuffer.CopyBlocks(m_BlockData); a_ChunkBuffer.CopyMeta(m_BlockData + cChunkDef::NumBlocks); - a_ChunkBuffer.CopyLight(m_BlockData + 3 * cChunkDef::NumBlocks / 2); + a_ChunkBuffer.CopyBlockLight(m_BlockData + 3 * cChunkDef::NumBlocks / 2); a_ChunkBuffer.CopySkyLight(m_BlockData + 2 * cChunkDef::NumBlocks); } }; @@ -83,7 +83,7 @@ protected: /** A simple implementation of the cChunkDataCallback interface that collects all block data into a separate buffers */ class cChunkDataSeparateCollector : -public cChunkDataCallback + public cChunkDataCallback { public: @@ -98,7 +98,7 @@ protected: { a_ChunkBuffer.CopyBlocks(m_BlockTypes); a_ChunkBuffer.CopyMeta(m_BlockMetas); - a_ChunkBuffer.CopyLight(m_BlockLight); + a_ChunkBuffer.CopyBlockLight(m_BlockLight); a_ChunkBuffer.CopySkyLight(m_BlockSkyLight); } } ; -- cgit v1.2.3 From 7fac63cffbe226ab025435a1e01e5f2430e52806 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 24 May 2014 13:37:25 +0100 Subject: Moved accessors to cpp file --- src/ChunkData.cpp | 230 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/ChunkData.h | 227 +++-------------------------------------------------- 2 files changed, 241 insertions(+), 216 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 098f436f8..162803292 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -2,7 +2,235 @@ #include "Globals.h" #include "ChunkData.h" -cChunkData cChunkData::Copy() const +cChunkData::cChunkData() +#if __cplusplus < 201103L + // auto_ptr style interface for memory management + : IsOwner(true) +#endif +{ + memset(m_Sections, 0, sizeof(m_Sections)); +} + + +cChunkData::~cChunkData() +{ + #if __cplusplus < 201103L + // auto_ptr style interface for memory management + if (!IsOwner) + { + return; + } + #endif + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + if (m_Sections[i] == NULL) Free(m_Sections[i]);; + } +} + +#if __cplusplus < 201103L + // auto_ptr style interface for memory management + cChunkData::cChunkData(const cChunkData& other) : + IsOwner(true) + { + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + m_Sections[i] = other.m_Sections[i]; + } + other.IsOwner = false; + } + + cChunkData::cChunkData& operator=(const cChunkData& other) + { + if (&other != this) + { + if (IsOwner) + { + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + if (m_Sections[i]) Free(m_Sections[i]);; + } + } + IsOwner = true; + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + m_Sections[i] = other.m_Sections[i]; + } + other.IsOwner = false; + } + return *this; + + } +#else + // unique_ptr style interface for memory management + cChunkData::cChunkData(cChunkData&& other) + { + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + m_Sections[i] = other.m_Sections[i]; + other.m_Sections[i] = NULL; + } + } + + cChunkData::cChunkData& operator=(cChunkData&& other) + { + if (&other != this) + { + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + Free(m_Sections[i]);; + m_Sections[i] = other.m_Sections[i]; + other.m_Sections[i] = NULL; + } + } + return *this; + } +#endif + +BLOCKTYPE cChunkData::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; + if (m_Sections[Section] != NULL) + { + 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 cChunkData::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) + ) + { + ASSERT(!"cChunkData::SetMeta(): index out of range!"); + return; + } + + int Section = a_RelY / CHUNK_SECTION_HEIGHT; + if (m_Sections[Section] == NULL) + { + if (a_Block == 0x00) + { + return; + } + m_Sections[Section] = Allocate(); + if (m_Sections[Section] == NULL) + { + ASSERT(!"Failed to allocate a new section in Chunkbuffer"); + return; + } + ZeroSection(m_Sections[Section]); + } + int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); + m_Sections[Section]->m_BlockTypes[Index] = a_Block; +} + +NIBBLETYPE cChunkData::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; + if (m_Sections[Section] != NULL) + { + 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; + } + } + ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!"); + return 0; +} + +bool cChunkData::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) + ) + { + ASSERT(!"cChunkData::SetMeta(): index out of range!"); + return false; + } + + int Section = a_RelY / CHUNK_SECTION_HEIGHT; + if (m_Sections[Section] == NULL) + { + if ((a_Nibble & 0xf) == 0x00) + { + return false; + } + m_Sections[Section] = Allocate(); + if (m_Sections[Section] == NULL) + { + ASSERT(!"Failed to allocate a new section in Chunkbuffer"); + return false; + } + ZeroSection(m_Sections[Section]); + } + int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); + NIBBLETYPE oldval = m_Sections[Section]->m_BlockMeta[Index / 2] >> ((Index & 1) * 4) & 0xf; + m_Sections[Section]->m_BlockMeta[Index / 2] = static_cast( + (m_Sections[Section]->m_BlockMeta[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble + ((a_Nibble & 0x0f) << ((Index & 1) * 4)) // The nibble being set + ); + return oldval == a_Nibble; +} + +NIBBLETYPE cChunkData::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; + if (m_Sections[Section] != NULL) + { + 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; + } + } + ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!"); + return 0; +} + +NIBBLETYPE cChunkData::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; + if (m_Sections[Section] != NULL) + { + 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 0xF; + } + } + ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!"); + return 0; +} + +cChunkData cChunkData::cChunkData::Copy() const { cChunkData copy; for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) diff --git a/src/ChunkData.h b/src/ChunkData.h index 5a149f95f..16fcc4d37 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -20,231 +20,28 @@ class cChunkData { public: - cChunkData() - #if __cplusplus < 201103L - // auto_ptr style interface for memory management - : IsOwner(true) - #endif - { - memset(m_Sections, 0, sizeof(m_Sections)); - } - ~cChunkData() - { - #if __cplusplus < 201103L - // auto_ptr style interface for memory management - if (!IsOwner) - { - return; - } - #endif - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) - { - if (m_Sections[i] == NULL) Free(m_Sections[i]);; - } - } + cChunkData(); + ~cChunkData(); #if __cplusplus < 201103L // auto_ptr style interface for memory management - cChunkData(const cChunkData& other) : - IsOwner(true) - { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) - { - m_Sections[i] = other.m_Sections[i]; - } - other.IsOwner = false; - } - - cChunkData& operator=(const cChunkData& other) - { - if (&other != this) - { - if (IsOwner) - { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) - { - if (m_Sections[i]) Free(m_Sections[i]);; - } - } - IsOwner = true; - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) - { - m_Sections[i] = other.m_Sections[i]; - } - other.IsOwner = false; - } - return *this; - - } + cChunkData(const cChunkData& other); + cChunkData& operator=(const cChunkData& other); #else // unique_ptr style interface for memory management - cChunkData(cChunkData&& other) - { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) - { - m_Sections[i] = other.m_Sections[i]; - other.m_Sections[i] = NULL; - } - } - - cChunkData& operator=(cChunkData&& other) - { - if (&other != this) - { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) - { - Free(m_Sections[i]);; - m_Sections[i] = other.m_Sections[i]; - other.m_Sections[i] = NULL; - } - } - return *this; - } + cChunkData(cChunkData&& other); + cChunkData& operator=(cChunkData&& other); #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; - if (m_Sections[Section] != NULL) - { - 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) - ) - { - ASSERT(!"cChunkData::SetMeta(): index out of range!"); - return; - } - - int Section = a_RelY / CHUNK_SECTION_HEIGHT; - if (m_Sections[Section] == NULL) - { - if (a_Block == 0x00) - { - return; - } - m_Sections[Section] = Allocate(); - if (m_Sections[Section] == NULL) - { - ASSERT(!"Failed to allocate a new section in Chunkbuffer"); - return; - } - ZeroSection(m_Sections[Section]); - } - 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; - if (m_Sections[Section] != NULL) - { - 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; - } - } - ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!"); - return 0; - } - - bool 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) - ) - { - ASSERT(!"cChunkData::SetMeta(): index out of range!"); - return false; - } + 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); - int Section = a_RelY / CHUNK_SECTION_HEIGHT; - if (m_Sections[Section] == NULL) - { - if ((a_Nibble & 0xf) == 0x00) - { - return false; - } - m_Sections[Section] = Allocate(); - if (m_Sections[Section] == NULL) - { - ASSERT(!"Failed to allocate a new section in Chunkbuffer"); - return false; - } - ZeroSection(m_Sections[Section]); - } - int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); - NIBBLETYPE oldval = m_Sections[Section]->m_BlockMeta[Index / 2] >> ((Index & 1) * 4) & 0xf; - m_Sections[Section]->m_BlockMeta[Index / 2] = static_cast( - (m_Sections[Section]->m_BlockMeta[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble - ((a_Nibble & 0x0f) << ((Index & 1) * 4)) // The nibble being set - ); - return oldval == a_Nibble; - } + 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); - 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; - if (m_Sections[Section] != NULL) - { - 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; - } - } - ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!"); - return 0; - } + NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const; - 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; - if (m_Sections[Section] != NULL) - { - 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 0xF; - } - } - ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!"); - return 0; - } + NIBBLETYPE GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const; cChunkData Copy() const; void CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx = 0, size_t length = cChunkDef::NumBlocks) const; -- cgit v1.2.3 From 944d29c0ae3929471a11ea11aa98441bc31f4d7d Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 24 May 2014 14:09:51 +0100 Subject: inject TestGlobals.h correctly --- src/ChunkData.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 162803292..79029f0cf 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -1,5 +1,9 @@ +#ifdef TEST_GLOBALS +#include "TestGlobals.h" +#else #include "Globals.h" +#endif #include "ChunkData.h" cChunkData::cChunkData() @@ -39,7 +43,7 @@ cChunkData::~cChunkData() other.IsOwner = false; } - cChunkData::cChunkData& operator=(const cChunkData& other) + cChunkData& cChunkData::operator=(const cChunkData& other) { if (&other != this) { @@ -71,7 +75,7 @@ cChunkData::~cChunkData() } } - cChunkData::cChunkData& operator=(cChunkData&& other) + cChunkData& cChunkData::operator=(cChunkData&& other) { if (&other != this) { @@ -230,7 +234,7 @@ NIBBLETYPE cChunkData::GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const return 0; } -cChunkData cChunkData::cChunkData::Copy() const +cChunkData cChunkData::Copy() const { cChunkData copy; for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) -- cgit v1.2.3 From 19df18c46199f06f3bf2058cc0efee9126e7670a Mon Sep 17 00:00:00 2001 From: Tycho Date: Tue, 27 May 2014 12:44:56 +0100 Subject: Fixed test globals to work with precompiled headers --- src/ChunkData.cpp | 4 ---- src/Globals.h | 48 ++++++++++++++++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 79029f0cf..c41dcb265 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -1,9 +1,5 @@ -#ifdef TEST_GLOBALS -#include "TestGlobals.h" -#else #include "Globals.h" -#endif #include "ChunkData.h" cChunkData::cChunkData() diff --git a/src/Globals.h b/src/Globals.h index 71e9191e4..85cfd2f18 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -225,16 +225,28 @@ template class SizeChecker; +#ifndef TEST_GLOBALS + // Common headers (part 1, without macros): + #include "StringUtils.h" + #include "OSSupport/Sleep.h" + #include "OSSupport/CriticalSection.h" + #include "OSSupport/Semaphore.h" + #include "OSSupport/Event.h" + #include "OSSupport/Thread.h" + #include "OSSupport/File.h" + #include "MCLogger.h" +#else + // Logging functions +void inline LOGERROR(const char* a_Format, ...) FORMATSTRING(1,2); -// Common headers (part 1, without macros): -#include "StringUtils.h" -#include "OSSupport/Sleep.h" -#include "OSSupport/CriticalSection.h" -#include "OSSupport/Semaphore.h" -#include "OSSupport/Event.h" -#include "OSSupport/Thread.h" -#include "OSSupport/File.h" -#include "MCLogger.h" +void inline LOGERROR(const char* a_Format, ...) +{ + va_list argList; + va_start(argList, a_Format); + vprintf(a_Format, argList); + va_end(argList); +} +#endif @@ -253,10 +265,22 @@ template class SizeChecker; #define FAST_FLOOR_DIV( x, div ) (((x) - (((x) < 0) ? ((div) - 1) : 0)) / (div)) // Own version of assert() that writes failed assertions to the log for review -#ifdef _DEBUG - #define ASSERT( x ) ( !!(x) || ( LOGERROR("Assertion failed: %s, file %s, line %i", #x, __FILE__, __LINE__ ), assert(0), 0 ) ) +#ifdef TEST_GLOBALS + + class cAssertFailure + { + }; + + #define ASSERT(x) do { if (!(x)) { throw cAssertFailure();} } while (0) + #define testassert(x) do { if(!(x)) { exit(1); } } while (0) + #define CheckAsserts(x) do { try {x} catch (cAssertFailure) { break; } exit(1); } while (0) + #else - #define ASSERT(x) ((void)(x)) + #ifdef _DEBUG + #define ASSERT( x ) ( !!(x) || ( LOGERROR("Assertion failed: %s, file %s, line %i", #x, __FILE__, __LINE__ ), assert(0), 0 ) ) + #else + #define ASSERT(x) ((void)(x)) + #endif #endif // Pretty much the same as ASSERT() but stays in Release builds -- cgit v1.2.3 From 7ec44951a0841734be53e81088dcdbc79a104d02 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 28 May 2014 11:35:55 +0200 Subject: Fixed cChunkData formatting. --- src/ChunkData.cpp | 118 +++++++++++++++++++++++++++++++++++++++++------------- src/ChunkData.h | 22 +++++----- 2 files changed, 101 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index c41dcb265..8aed62000 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -5,64 +5,86 @@ cChunkData::cChunkData() #if __cplusplus < 201103L // auto_ptr style interface for memory management - : IsOwner(true) + : m_IsOwner(true) #endif { - memset(m_Sections, 0, sizeof(m_Sections)); + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + m_Sections[i] = NULL; + } } + + + cChunkData::~cChunkData() { #if __cplusplus < 201103L // auto_ptr style interface for memory management - if (!IsOwner) + if (!m_IsOwner) { return; } #endif for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { - if (m_Sections[i] == NULL) Free(m_Sections[i]);; + Free(m_Sections[i]); } } + + + + #if __cplusplus < 201103L // auto_ptr style interface for memory management - cChunkData::cChunkData(const cChunkData& other) : - IsOwner(true) + cChunkData::cChunkData(const cChunkData & a_Other) : + m_IsOwner(true) { + // Move contents and ownership from a_Other to this, pointer-wise: for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { - m_Sections[i] = other.m_Sections[i]; + m_Sections[i] = a_Other.m_Sections[i]; } - other.IsOwner = false; + a_Other.m_IsOwner = false; } - cChunkData& cChunkData::operator=(const cChunkData& other) + + + + + cChunkData & cChunkData::operator =(const cChunkData & a_Other) { - if (&other != this) + // If assigning to self, no-op + if (&a_Other == this) + { + return *this; + } + + // Free any currently held contents: + if (m_IsOwner) { - if (IsOwner) - { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) - { - if (m_Sections[i]) Free(m_Sections[i]);; - } - } - IsOwner = true; for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { - m_Sections[i] = other.m_Sections[i]; + Free(m_Sections[i]); } - other.IsOwner = false; } + + // Move contents and ownership from a_Other to this, pointer-wise: + m_IsOwner = true; + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + m_Sections[i] = a_Other.m_Sections[i]; + } + a_Other.m_IsOwner = false; return *this; - } + #else + // unique_ptr style interface for memory management - cChunkData::cChunkData(cChunkData&& other) + cChunkData::cChunkData(cChunkData && other) { for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { @@ -70,14 +92,18 @@ cChunkData::~cChunkData() other.m_Sections[i] = NULL; } } + + + + - cChunkData& cChunkData::operator=(cChunkData&& other) + cChunkData & cChunkData::operator =(cChunkData && other) { if (&other != this) { for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { - Free(m_Sections[i]);; + Free(m_Sections[i]); m_Sections[i] = other.m_Sections[i]; other.m_Sections[i] = NULL; } @@ -86,6 +112,10 @@ cChunkData::~cChunkData() } #endif + + + + BLOCKTYPE cChunkData::GetBlock(int a_X, int a_Y, int a_Z) const { ASSERT((a_X >= 0) && (a_X < cChunkDef::Width)); @@ -103,6 +133,10 @@ BLOCKTYPE cChunkData::GetBlock(int a_X, int a_Y, int a_Z) const } } + + + + void cChunkData::SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_Block) { if ( @@ -134,6 +168,10 @@ void cChunkData::SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_Block) m_Sections[Section]->m_BlockTypes[Index] = a_Block; } + + + + NIBBLETYPE cChunkData::GetMeta(int a_RelX, int a_RelY, int a_RelZ) const { if ( @@ -156,6 +194,10 @@ NIBBLETYPE cChunkData::GetMeta(int a_RelX, int a_RelY, int a_RelZ) const return 0; } + + + + bool cChunkData::SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble) { if ( @@ -192,9 +234,17 @@ bool cChunkData::SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble return oldval == a_Nibble; } + + + + NIBBLETYPE cChunkData::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)) + 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; if (m_Sections[Section] != NULL) @@ -211,6 +261,10 @@ NIBBLETYPE cChunkData::GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const return 0; } + + + + NIBBLETYPE cChunkData::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)) @@ -230,7 +284,11 @@ NIBBLETYPE cChunkData::GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const return 0; } -cChunkData cChunkData::Copy() const + + + + +cChunkData cChunkData::Copy(void) const { cChunkData copy; for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) @@ -248,11 +306,11 @@ cChunkData cChunkData::Copy() const -void cChunkData::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length) const +void cChunkData::CopyBlocks(BLOCKTYPE * a_dest, size_t a_Idx, size_t length) const { for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16; + const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16; if (a_Idx > 0) { a_Idx = std::max(a_Idx - length, (size_t) 0); @@ -588,6 +646,8 @@ cChunkData::sChunkSection * cChunkData::Allocate() const + + void cChunkData::Free(cChunkData::sChunkSection * ptr) const { delete ptr; @@ -595,6 +655,8 @@ void cChunkData::Free(cChunkData::sChunkSection * ptr) const + + void cChunkData::ZeroSection(cChunkData::sChunkSection * ptr) const { memset( diff --git a/src/ChunkData.h b/src/ChunkData.h index 16fcc4d37..6544b246e 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -25,12 +25,12 @@ public: #if __cplusplus < 201103L // auto_ptr style interface for memory management - cChunkData(const cChunkData& other); - cChunkData& operator=(const cChunkData& other); + cChunkData(const cChunkData & other); + cChunkData & operator =(const cChunkData & other); #else // unique_ptr style interface for memory management - cChunkData(cChunkData&& other); - cChunkData& operator=(cChunkData&& other); + cChunkData(cChunkData && other); + cChunkData & operator =(cChunkData && other); #endif BLOCKTYPE GetBlock(int a_X, int a_Y, int a_Z) const; @@ -43,15 +43,15 @@ public: NIBBLETYPE GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const; - cChunkData Copy() const; + cChunkData Copy(void) const; void CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx = 0, size_t length = cChunkDef::NumBlocks) const; void CopyMeta (NIBBLETYPE * a_dest) const; - void CopyBlockLight (NIBBLETYPE * a_dest) const; + void CopyBlockLight(NIBBLETYPE * a_dest) const; void CopySkyLight (NIBBLETYPE * a_dest) const; void SetBlocks (const BLOCKTYPE * a_src); void SetMeta (const NIBBLETYPE * a_src); - void SetBlockLight (const NIBBLETYPE * a_src); + void SetBlockLight(const NIBBLETYPE * a_src); void SetSkyLight (const NIBBLETYPE * a_src); private: @@ -61,19 +61,19 @@ private: #if __cplusplus < 201103L // auto_ptr style interface for memory management - mutable bool IsOwner; + mutable bool m_IsOwner; #endif struct sChunkSection { - BLOCKTYPE m_BlockTypes [CHUNK_SECTION_HEIGHT * 16 * 16] ; + 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_COUNT]; + sChunkSection * m_Sections[CHUNK_SECTION_COUNT]; - sChunkSection * Allocate() const; + sChunkSection * Allocate(void) const; void Free(sChunkSection * ptr) const; void ZeroSection(sChunkSection * ptr) const; -- cgit v1.2.3 From 81f756cbda76507cc676fd50f20f33483c4ce6f8 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 28 May 2014 22:40:19 +0200 Subject: cChunkData: Normalized code style. --- src/ChunkData.cpp | 168 +++++++++++++++++++++++++++++------------------------- src/ChunkData.h | 27 +++++---- 2 files changed, 106 insertions(+), 89 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 8aed62000..beee6c64a 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -306,33 +306,33 @@ cChunkData cChunkData::Copy(void) const -void cChunkData::CopyBlocks(BLOCKTYPE * a_dest, size_t a_Idx, size_t length) const +void cChunkData::CopyBlocks(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Length) const { for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16; + const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16; if (a_Idx > 0) { - a_Idx = std::max(a_Idx - length, (size_t) 0); + a_Idx = std::max(a_Idx - a_Length, (size_t) 0); } if (a_Idx == 0) { - size_t tocopy = std::min(segment_length, length); - length -= tocopy; + size_t ToCopy = std::min(SegmentLength, a_Length); + length -= ToCopy; if (m_Sections[i] != NULL) { memcpy( - &a_dest[i * segment_length], + &a_Dest[i * SegmentLength], &m_Sections[i]->m_BlockTypes, - sizeof(BLOCKTYPE) * tocopy + sizeof(BLOCKTYPE) * ToCopy ); } else { memset( - &a_dest[i * segment_length], + &a_Dest[i * SegmentLength], 0, - sizeof(BLOCKTYPE) * tocopy + sizeof(BLOCKTYPE) * ToCopy ); } } @@ -343,25 +343,25 @@ void cChunkData::CopyBlocks(BLOCKTYPE * a_dest, size_t a_Idx, size_t length) co -void cChunkData::CopyMeta(NIBBLETYPE * a_dest) const +void cChunkData::CopyMeta(NIBBLETYPE * a_Dest) const { for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) { memcpy( - &a_dest[i * segment_length], + &a_Dest[i * SegmentLength], &m_Sections[i]->m_BlockMeta, - sizeof(NIBBLETYPE) * segment_length + sizeof(NIBBLETYPE) * SegmentLength ); } else { memset( - &a_dest[i * segment_length], + &a_Dest[i * SegmentLength], 0, - sizeof(BLOCKTYPE) * segment_length + sizeof(BLOCKTYPE) * SegmentLength ); } } @@ -371,25 +371,25 @@ void cChunkData::CopyMeta(NIBBLETYPE * a_dest) const -void cChunkData::CopyBlockLight(NIBBLETYPE * a_dest) const +void cChunkData::CopyBlockLight(NIBBLETYPE * a_Dest) const { for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) { memcpy( - &a_dest[i * segment_length], + &a_Dest[i * SegmentLength], &m_Sections[i]->m_BlockLight, - sizeof(NIBBLETYPE) * segment_length + sizeof(NIBBLETYPE) * SegmentLength ); } else { memset( - &a_dest[i * segment_length], + &a_Dest[i * SegmentLength], 0, - sizeof(BLOCKTYPE) * segment_length + sizeof(BLOCKTYPE) * SegmentLength ); } } @@ -399,25 +399,25 @@ void cChunkData::CopyBlockLight(NIBBLETYPE * a_dest) const -void cChunkData::CopySkyLight(NIBBLETYPE * a_dest) const +void cChunkData::CopySkyLight(NIBBLETYPE * a_Dest) const { for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) { memcpy( - &a_dest[i * segment_length], + &a_Dest[i * SegmentLength], &m_Sections[i]->m_BlockSkyLight, - sizeof(NIBBLETYPE) * segment_length + sizeof(NIBBLETYPE) * SegmentLength ); } else { memset( - &a_dest[i * segment_length], + &a_Dest[i * SegmentLength], 0xFF, - sizeof(BLOCKTYPE) * segment_length + sizeof(BLOCKTYPE) * SegmentLength ); } } @@ -427,34 +427,36 @@ void cChunkData::CopySkyLight(NIBBLETYPE * a_dest) const -void cChunkData::SetBlocks(const BLOCKTYPE * a_src) +void cChunkData::SetBlocks(const BLOCKTYPE * a_Src) { + ASSERT(a_Src != NULL); + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16; + const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16; if (m_Sections[i] != NULL) { memcpy( &m_Sections[i]->m_BlockTypes, - &a_src[i * segment_length], - sizeof(BLOCKTYPE) * segment_length + &a_Src[i * SegmentLength], + sizeof(BLOCKTYPE) * SegmentLength ); } else { // j counts how many of leading zeros the buffer has - // if j == segment_length then the buffer is all zeros so there is no point + // if j == SegmentLength then the buffer is all zeros so there is no point // creating the buffer. size_t j = 0; // do nothing whilst 0 - for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); - if (j != segment_length) + for (; (j < SegmentLength) && (a_Src[i * SegmentLength + j] == 0); j++); + if (j != SegmentLength) { m_Sections[i] = Allocate(); memcpy( &m_Sections[i]->m_BlockTypes, - &a_src[i * segment_length], - sizeof(BLOCKTYPE) * segment_length + &a_Src[i * SegmentLength], + sizeof(BLOCKTYPE) * SegmentLength ); memset( m_Sections[i]->m_BlockMeta, @@ -479,34 +481,36 @@ void cChunkData::SetBlocks(const BLOCKTYPE * a_src) -void cChunkData::SetMeta(const NIBBLETYPE * a_src) +void cChunkData::SetMeta(const NIBBLETYPE * a_Src) { + ASSERT(a_Src != NULL); + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) { memcpy( &m_Sections[i]->m_BlockMeta, - &a_src[i * segment_length], - sizeof(NIBBLETYPE) * segment_length + &a_Src[i * SegmentLength], + sizeof(NIBBLETYPE) * SegmentLength ); } else { // j counts how many of leading zeros the buffer has - // if j == segment_length then the buffer is all zeros so there is no point + // if j == SegmentLength then the buffer is all zeros so there is no point // creating the buffer. size_t j = 0; // do nothing whilst 0 - for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); - if (j != segment_length) + for (; (j < SegmentLength) && (a_Src[i * SegmentLength + j] == 0); j++); + if (j != SegmentLength) { m_Sections[i] = Allocate(); memcpy( &m_Sections[i]->m_BlockMeta, - &a_src[i * segment_length], - sizeof(BLOCKTYPE) * segment_length + &a_Src[i * SegmentLength], + sizeof(BLOCKTYPE) * SegmentLength ); memset( m_Sections[i]->m_BlockTypes, @@ -531,35 +535,38 @@ void cChunkData::SetMeta(const NIBBLETYPE * a_src) -void cChunkData::SetBlockLight(const NIBBLETYPE * a_src) +void cChunkData::SetBlockLight(const NIBBLETYPE * a_Src) { - if (!a_src) return; + if (a_Src == NULL) + { + return; + } for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) { memcpy( &m_Sections[i]->m_BlockLight, - &a_src[i * segment_length], - sizeof(NIBBLETYPE) * segment_length + &a_Src[i * SegmentLength], + sizeof(NIBBLETYPE) * SegmentLength ); } else { // j counts how many of leading zeros the buffer has - // if j == segment_length then the buffer is all zeros so there is no point + // if j == SegmentLength then the buffer is all zeros so there is no point // creating the buffer. size_t j = 0; // do nothing whilst 0 - for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); - if (j != segment_length) + for (; (j < SegmentLength) && (a_Src[i * SegmentLength + j] == 0); j++); + if (j != SegmentLength) { m_Sections[i] = Allocate(); memcpy( &m_Sections[i]->m_BlockLight, - &a_src[i * segment_length], - sizeof(BLOCKTYPE) * segment_length + &a_Src[i * SegmentLength], + sizeof(BLOCKTYPE) * SegmentLength ); memset( m_Sections[i]->m_BlockTypes, @@ -584,35 +591,39 @@ void cChunkData::SetBlockLight(const NIBBLETYPE * a_src) -void cChunkData::SetSkyLight (const NIBBLETYPE * a_src) +void cChunkData::SetSkyLight(const NIBBLETYPE * a_Src) { - if (!a_src) return; + if (a_Src == NULL) + { + return; + } + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { - const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) { memcpy( &m_Sections[i]->m_BlockSkyLight, - &a_src[i * segment_length], - sizeof(NIBBLETYPE) * segment_length + &a_Src[i * SegmentLength], + sizeof(NIBBLETYPE) * SegmentLength ); } else { // j counts how many of leading zeros the buffer has - // if j == segment_length then the buffer is all zeros so there is no point + // if j == SegmentLength then the buffer is all zeros so there is no point // creating the buffer. size_t j = 0; // do nothing whilst 0 - for (; j < segment_length && a_src[i * segment_length + j] == 0xFF; j++); - if (j != segment_length) + for (; (j < SegmentLength) && (a_Src[i * SegmentLength + j]) == 0xFF; j++); + if (j != SegmentLength) { m_Sections[i] = Allocate(); memcpy( &m_Sections[i]->m_BlockSkyLight, - &a_src[i * segment_length], - sizeof(BLOCKTYPE) * segment_length + &a_Src[i * SegmentLength], + sizeof(BLOCKTYPE) * SegmentLength ); memset( m_Sections[i]->m_BlockTypes, @@ -638,9 +649,9 @@ void cChunkData::SetSkyLight (const NIBBLETYPE * a_src) -cChunkData::sChunkSection * cChunkData::Allocate() const +cChunkData::sChunkSection * cChunkData::Allocate(void) const { - // TODO: use a allocation pool + // TODO: Use an allocation pool return new cChunkData::sChunkSection; } @@ -648,36 +659,37 @@ cChunkData::sChunkSection * cChunkData::Allocate() const -void cChunkData::Free(cChunkData::sChunkSection * ptr) const +void cChunkData::Free(cChunkData::sChunkSection * a_Section) const { - delete ptr; + // TODO: Use an allocation pool + delete a_Section; } -void cChunkData::ZeroSection(cChunkData::sChunkSection * ptr) const +void cChunkData::ZeroSection(cChunkData::sChunkSection * a_Section) const { memset( - ptr->m_BlockTypes, + a_Section->m_BlockTypes, 0x00, - sizeof(ptr->m_BlockTypes) + sizeof(a_Section->m_BlockTypes) ); memset( - ptr->m_BlockMeta, + a_Section->m_BlockMeta, 0x00, - sizeof(ptr->m_BlockMeta) + sizeof(a_Section->m_BlockMeta) ); memset( - ptr->m_BlockLight, + a_Section->m_BlockLight, 0x00, - sizeof(ptr->m_BlockLight) + sizeof(a_Section->m_BlockLight) ); memset( - ptr->m_BlockSkyLight, + a_Section->m_BlockSkyLight, 0xFF, - sizeof(ptr->m_BlockSkyLight) + sizeof(a_Section->m_BlockSkyLight) ); } diff --git a/src/ChunkData.h b/src/ChunkData.h index 6544b246e..09b2cb563 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -25,12 +25,12 @@ public: #if __cplusplus < 201103L // auto_ptr style interface for memory management - cChunkData(const cChunkData & other); - cChunkData & operator =(const cChunkData & other); + cChunkData(const cChunkData & a_Other); + cChunkData & operator =(const cChunkData & a_Other); #else // unique_ptr style interface for memory management - cChunkData(cChunkData && other); - cChunkData & operator =(cChunkData && other); + cChunkData(cChunkData && a_Other); + cChunkData & operator =(cChunkData && a_ther); #endif BLOCKTYPE GetBlock(int a_X, int a_Y, int a_Z) const; @@ -44,10 +44,10 @@ public: NIBBLETYPE GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const; cChunkData Copy(void) const; - void CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx = 0, size_t length = cChunkDef::NumBlocks) const; - void CopyMeta (NIBBLETYPE * a_dest) const; - void CopyBlockLight(NIBBLETYPE * a_dest) const; - void CopySkyLight (NIBBLETYPE * a_dest) const; + void CopyBlocks (BLOCKTYPE * a_Dest, size_t a_Idx = 0, size_t a_Length = cChunkDef::NumBlocks) const; + void CopyMeta (NIBBLETYPE * a_Dest) const; + void CopyBlockLight(NIBBLETYPE * a_Dest) const; + void CopySkyLight (NIBBLETYPE * a_Dest) const; void SetBlocks (const BLOCKTYPE * a_src); void SetMeta (const NIBBLETYPE * a_src); @@ -73,10 +73,15 @@ private: sChunkSection * m_Sections[CHUNK_SECTION_COUNT]; - sChunkSection * Allocate(void) const; - void Free(sChunkSection * ptr) const; + /** Allocates a new section. Entry-point to custom allocators. */ + static sChunkSection * Allocate(void); - void ZeroSection(sChunkSection * ptr) const; + /** Frees the specified section, previously allocated using Allocate(). + Note that a_Section may be NULL. */ + static void Free(sChunkSection * a_Section); + + /** Sets the data in the specified section to their default values. */ + void ZeroSection(sChunkSection * a_Section) const; }; -- cgit v1.2.3 From 999662503a90e6acd4528be50f8b6232b357d9c3 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 29 May 2014 09:09:11 +0200 Subject: Fixed forgotten changes. --- src/ChunkData.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index beee6c64a..172858e4e 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -318,7 +318,7 @@ void cChunkData::CopyBlocks(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Length) if (a_Idx == 0) { size_t ToCopy = std::min(SegmentLength, a_Length); - length -= ToCopy; + a_Length -= ToCopy; if (m_Sections[i] != NULL) { memcpy( @@ -649,7 +649,7 @@ void cChunkData::SetSkyLight(const NIBBLETYPE * a_Src) -cChunkData::sChunkSection * cChunkData::Allocate(void) const +cChunkData::sChunkSection * cChunkData::Allocate(void) { // TODO: Use an allocation pool return new cChunkData::sChunkSection; @@ -659,7 +659,7 @@ cChunkData::sChunkSection * cChunkData::Allocate(void) const -void cChunkData::Free(cChunkData::sChunkSection * a_Section) const +void cChunkData::Free(cChunkData::sChunkSection * a_Section) { // TODO: Use an allocation pool delete a_Section; -- cgit v1.2.3 From d9c667d28f556e489b2779b7510f3b12034c3fad Mon Sep 17 00:00:00 2001 From: worktycho Date: Thu, 29 May 2014 12:04:37 +0100 Subject: Add comment --- src/ChunkData.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/ChunkData.h b/src/ChunkData.h index 09b2cb563..e6aaa99b8 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -44,6 +44,10 @@ public: NIBBLETYPE GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const; cChunkData Copy(void) const; + + // Copys data from this object into the buffer in the a_Dest param + // CopyBlocks also povides the optional parameters a_Idx and a_Length which specify an offset and length for + // copying part of the BlockTypes array. void CopyBlocks (BLOCKTYPE * a_Dest, size_t a_Idx = 0, size_t a_Length = cChunkDef::NumBlocks) const; void CopyMeta (NIBBLETYPE * a_Dest) const; void CopyBlockLight(NIBBLETYPE * a_Dest) const; -- cgit v1.2.3 From f7777e8c7559964f126a64af7673276e356dcedc Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 29 May 2014 18:25:08 +0200 Subject: Added comments, reformatted code. --- src/BlockArea.cpp | 2 +- src/Chunk.cpp | 6 +- src/ChunkData.cpp | 391 ++++++++++++++++++------------------------------ src/ChunkData.h | 65 +++++--- src/ChunkDataCallback.h | 40 +++-- src/LightingThread.cpp | 2 +- 6 files changed, 226 insertions(+), 280 deletions(-) (limited to 'src') diff --git a/src/BlockArea.cpp b/src/BlockArea.cpp index 11bd76e6c..40fdd68c0 100644 --- a/src/BlockArea.cpp +++ b/src/BlockArea.cpp @@ -1903,7 +1903,7 @@ void cBlockArea::cChunkReader::ChunkData(const cChunkData & a_BlockBuffer) if (m_Area.m_BlockMetas != NULL) { - a_BlockBuffer.CopyMeta(m_Area.m_BlockMetas); + a_BlockBuffer.CopyMetas(m_Area.m_BlockMetas); } if (m_Area.m_BlockLight != NULL) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index e5f8f1e29..23412a4c3 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -282,8 +282,8 @@ void cChunk::SetAllData( CalculateHeightmap(a_BlockTypes); } - m_ChunkData.SetBlocks(a_BlockTypes); - m_ChunkData.SetMeta(a_BlockMeta); + m_ChunkData.SetBlockTypes(a_BlockTypes); + m_ChunkData.SetMetas(a_BlockMeta); m_ChunkData.SetBlockLight(a_BlockLight); m_ChunkData.SetSkyLight(a_BlockSkyLight); @@ -339,7 +339,7 @@ void cChunk::SetLight( void cChunk::GetBlockTypes(BLOCKTYPE * a_BlockTypes) { - m_ChunkData.CopyBlocks(a_BlockTypes); + m_ChunkData.CopyBlockTypes(a_BlockTypes); } diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 172858e4e..cc4793ec3 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -1,14 +1,39 @@ +// ChunkData.cpp + +// Implements the cChunkData class that represents the block's type, meta, blocklight and skylight storage for a chunk + #include "Globals.h" #include "ChunkData.h" -cChunkData::cChunkData() + + + + +/** Returns true if all a_Array's elements between [0] and [a_NumElements - 1] are equal to a_Value. */ +template inline bool IsAllValue(const T * a_Array, size_t a_NumElements, T a_Value) +{ + for (size_t i = 0; i < a_NumElements; i++) + { + if (a_Array[i] != a_Value) + { + return false; + } + } + return true; +} + + + + + +cChunkData::cChunkData(void) #if __cplusplus < 201103L // auto_ptr style interface for memory management : m_IsOwner(true) #endif { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + for (size_t i = 0; i < NumSections; i++) { m_Sections[i] = NULL; } @@ -27,7 +52,7 @@ cChunkData::~cChunkData() return; } #endif - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + for (size_t i = 0; i < NumSections; i++) { Free(m_Sections[i]); } @@ -43,7 +68,7 @@ cChunkData::~cChunkData() m_IsOwner(true) { // Move contents and ownership from a_Other to this, pointer-wise: - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + for (size_t i = 0; i < NumSections; i++) { m_Sections[i] = a_Other.m_Sections[i]; } @@ -65,7 +90,7 @@ cChunkData::~cChunkData() // Free any currently held contents: if (m_IsOwner) { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + for (size_t i = 0; i < NumSections; i++) { Free(m_Sections[i]); } @@ -73,7 +98,7 @@ cChunkData::~cChunkData() // Move contents and ownership from a_Other to this, pointer-wise: m_IsOwner = true; - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + for (size_t i = 0; i < NumSections; i++) { m_Sections[i] = a_Other.m_Sections[i]; } @@ -86,7 +111,7 @@ cChunkData::~cChunkData() // unique_ptr style interface for memory management cChunkData::cChunkData(cChunkData && other) { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + for (size_t i = 0; i < NumSections; i++) { m_Sections[i] = other.m_Sections[i]; other.m_Sections[i] = NULL; @@ -101,7 +126,7 @@ cChunkData::~cChunkData() { if (&other != this) { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + for (size_t i = 0; i < NumSections; i++) { Free(m_Sections[i]); m_Sections[i] = other.m_Sections[i]; @@ -121,10 +146,10 @@ BLOCKTYPE cChunkData::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; + int Section = a_Y / SectionHeight; if (m_Sections[Section] != NULL) { - int Index = cChunkDef::MakeIndexNoCheck(a_X, a_Y - (Section * CHUNK_SECTION_HEIGHT), a_Z); + int Index = cChunkDef::MakeIndexNoCheck(a_X, a_Y - (Section * SectionHeight), a_Z); return m_Sections[Section]->m_BlockTypes[Index]; } else @@ -149,7 +174,7 @@ void cChunkData::SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_Block) return; } - int Section = a_RelY / CHUNK_SECTION_HEIGHT; + int Section = a_RelY / SectionHeight; if (m_Sections[Section] == NULL) { if (a_Block == 0x00) @@ -164,7 +189,7 @@ void cChunkData::SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_Block) } ZeroSection(m_Sections[Section]); } - int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); + int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * SectionHeight), a_RelZ); m_Sections[Section]->m_BlockTypes[Index] = a_Block; } @@ -179,10 +204,10 @@ NIBBLETYPE cChunkData::GetMeta(int a_RelX, int a_RelY, int a_RelZ) const (a_RelY < cChunkDef::Height) && (a_RelY > -1) && (a_RelZ < cChunkDef::Width) && (a_RelZ > -1)) { - int Section = a_RelY / CHUNK_SECTION_HEIGHT; + int Section = a_RelY / SectionHeight; if (m_Sections[Section] != NULL) { - int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); + int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * SectionHeight), a_RelZ); return (m_Sections[Section]->m_BlockMeta[Index / 2] >> ((Index & 1) * 4)) & 0x0f; } else @@ -210,7 +235,7 @@ bool cChunkData::SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble return false; } - int Section = a_RelY / CHUNK_SECTION_HEIGHT; + int Section = a_RelY / SectionHeight; if (m_Sections[Section] == NULL) { if ((a_Nibble & 0xf) == 0x00) @@ -225,7 +250,7 @@ bool cChunkData::SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble } ZeroSection(m_Sections[Section]); } - int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); + int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * SectionHeight), a_RelZ); NIBBLETYPE oldval = m_Sections[Section]->m_BlockMeta[Index / 2] >> ((Index & 1) * 4) & 0xf; m_Sections[Section]->m_BlockMeta[Index / 2] = static_cast( (m_Sections[Section]->m_BlockMeta[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble @@ -246,10 +271,10 @@ NIBBLETYPE cChunkData::GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const (a_RelZ < cChunkDef::Width) && (a_RelZ > -1) ) { - int Section = a_RelY / CHUNK_SECTION_HEIGHT; + int Section = a_RelY / SectionHeight; if (m_Sections[Section] != NULL) { - int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); + int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * SectionHeight), a_RelZ); return (m_Sections[Section]->m_BlockLight[Index / 2] >> ((Index & 1) * 4)) & 0x0f; } else @@ -269,10 +294,10 @@ NIBBLETYPE cChunkData::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; + int Section = a_RelY / SectionHeight; if (m_Sections[Section] != NULL) { - int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); + int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * SectionHeight), a_RelZ); return (m_Sections[Section]->m_BlockLight[Index / 2] >> ((Index & 1) * 4)) & 0x0f; } else @@ -291,7 +316,7 @@ NIBBLETYPE cChunkData::GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const cChunkData cChunkData::Copy(void) const { cChunkData copy; - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + for (size_t i = 0; i < NumSections; i++) { if (m_Sections[i] != NULL) { @@ -306,11 +331,12 @@ cChunkData cChunkData::Copy(void) const -void cChunkData::CopyBlocks(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Length) const +void cChunkData::CopyBlockTypes(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Length) const { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + // TODO: This code seems wrong. It always copies into a_Dest[i * SegmentLength], even when a_Idx > 0 + for (size_t i = 0; i < NumSections; i++) { - const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16; + const size_t SegmentLength = SectionHeight * 16 * 16; if (a_Idx > 0) { a_Idx = std::max(a_Idx - a_Length, (size_t) 0); @@ -321,19 +347,11 @@ void cChunkData::CopyBlocks(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Length) a_Length -= ToCopy; if (m_Sections[i] != NULL) { - memcpy( - &a_Dest[i * SegmentLength], - &m_Sections[i]->m_BlockTypes, - sizeof(BLOCKTYPE) * ToCopy - ); + memcpy(&a_Dest[i * SegmentLength], &m_Sections[i]->m_BlockTypes, sizeof(BLOCKTYPE) * ToCopy); } else { - memset( - &a_Dest[i * SegmentLength], - 0, - sizeof(BLOCKTYPE) * ToCopy - ); + memset(&a_Dest[i * SegmentLength], 0, sizeof(BLOCKTYPE) * ToCopy); } } } @@ -343,26 +361,18 @@ void cChunkData::CopyBlocks(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Length) -void cChunkData::CopyMeta(NIBBLETYPE * a_Dest) const +void cChunkData::CopyMetas(NIBBLETYPE * a_Dest) const { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + for (size_t i = 0; i < NumSections; i++) { - const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + const size_t SegmentLength = SectionHeight * 16 * 16 / 2; if (m_Sections[i] != NULL) { - memcpy( - &a_Dest[i * SegmentLength], - &m_Sections[i]->m_BlockMeta, - sizeof(NIBBLETYPE) * SegmentLength - ); + memcpy(&a_Dest[i * SegmentLength], &m_Sections[i]->m_BlockMeta, sizeof(NIBBLETYPE) * SegmentLength); } else { - memset( - &a_Dest[i * SegmentLength], - 0, - sizeof(BLOCKTYPE) * SegmentLength - ); + memset(&a_Dest[i * SegmentLength], 0, sizeof(BLOCKTYPE) * SegmentLength); } } } @@ -373,24 +383,16 @@ void cChunkData::CopyMeta(NIBBLETYPE * a_Dest) const void cChunkData::CopyBlockLight(NIBBLETYPE * a_Dest) const { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + for (size_t i = 0; i < NumSections; i++) { - const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + const size_t SegmentLength = SectionHeight * 16 * 16 / 2; if (m_Sections[i] != NULL) { - memcpy( - &a_Dest[i * SegmentLength], - &m_Sections[i]->m_BlockLight, - sizeof(NIBBLETYPE) * SegmentLength - ); + memcpy(&a_Dest[i * SegmentLength], &m_Sections[i]->m_BlockLight, sizeof(NIBBLETYPE) * SegmentLength); } else { - memset( - &a_Dest[i * SegmentLength], - 0, - sizeof(BLOCKTYPE) * SegmentLength - ); + memset(&a_Dest[i * SegmentLength], 0, sizeof(BLOCKTYPE) * SegmentLength); } } } @@ -401,24 +403,16 @@ void cChunkData::CopyBlockLight(NIBBLETYPE * a_Dest) const void cChunkData::CopySkyLight(NIBBLETYPE * a_Dest) const { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + for (size_t i = 0; i < NumSections; i++) { - const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + const size_t SegmentLength = SectionHeight * 16 * 16 / 2; if (m_Sections[i] != NULL) { - memcpy( - &a_Dest[i * SegmentLength], - &m_Sections[i]->m_BlockSkyLight, - sizeof(NIBBLETYPE) * SegmentLength - ); + memcpy(&a_Dest[i * SegmentLength], &m_Sections[i]->m_BlockSkyLight, sizeof(NIBBLETYPE) * SegmentLength); } else { - memset( - &a_Dest[i * SegmentLength], - 0xFF, - sizeof(BLOCKTYPE) * SegmentLength - ); + memset(&a_Dest[i * SegmentLength], 0xff, sizeof(BLOCKTYPE) * SegmentLength); } } } @@ -427,165 +421,102 @@ void cChunkData::CopySkyLight(NIBBLETYPE * a_Dest) const -void cChunkData::SetBlocks(const BLOCKTYPE * a_Src) +void cChunkData::SetBlockTypes(const BLOCKTYPE * a_Src) { ASSERT(a_Src != NULL); - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + static const size_t SegmentLength = SectionHeight * 16 * 16; + for (size_t i = 0; i < NumSections; i++) { - const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16; + // If the section is already allocated, copy the data into it: if (m_Sections[i] != NULL) { - memcpy( - &m_Sections[i]->m_BlockTypes, - &a_Src[i * SegmentLength], - sizeof(BLOCKTYPE) * SegmentLength - ); + memcpy(&m_Sections[i]->m_BlockTypes, &a_Src[i * SegmentLength], sizeof(BLOCKTYPE) * SegmentLength); + continue; } - else + + // The section doesn't exist, find out if it is needed: + if (IsAllValue(a_Src + i * SegmentLength, SegmentLength, (const BLOCKTYPE)0)) { - // j counts how many of leading zeros the buffer has - // if j == SegmentLength then the buffer is all zeros so there is no point - // creating the buffer. - size_t j = 0; - // do nothing whilst 0 - for (; (j < SegmentLength) && (a_Src[i * SegmentLength + j] == 0); j++); - if (j != SegmentLength) - { - m_Sections[i] = Allocate(); - memcpy( - &m_Sections[i]->m_BlockTypes, - &a_Src[i * SegmentLength], - sizeof(BLOCKTYPE) * SegmentLength - ); - 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) - ); - } + // No need for the section, the data is all-air + continue; } - } + + // Allocate the section and copy the data into it: + m_Sections[i] = Allocate(); + memcpy(&m_Sections[i]->m_BlockTypes, &a_Src[i * SegmentLength], sizeof(BLOCKTYPE) * SegmentLength); + 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)); + } // for i - m_Sections[] } -void cChunkData::SetMeta(const NIBBLETYPE * a_Src) +void cChunkData::SetMetas(const NIBBLETYPE * a_Src) { ASSERT(a_Src != NULL); - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + for (size_t i = 0; i < NumSections; i++) { - const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + // If the section is already allocated, copy the data into it: if (m_Sections[i] != NULL) { - memcpy( - &m_Sections[i]->m_BlockMeta, - &a_Src[i * SegmentLength], - sizeof(NIBBLETYPE) * SegmentLength - ); + memcpy(&m_Sections[i]->m_BlockMeta, &a_Src[i * SegmentLength], sizeof(NIBBLETYPE) * SegmentLength); + continue; } - else + + // The section doesn't exist, find out if it is needed: + if (IsAllValue(a_Src + i * SegmentLength, SegmentLength, (NIBBLETYPE)0)) { - // j counts how many of leading zeros the buffer has - // if j == SegmentLength then the buffer is all zeros so there is no point - // creating the buffer. - size_t j = 0; - // do nothing whilst 0 - for (; (j < SegmentLength) && (a_Src[i * SegmentLength + j] == 0); j++); - if (j != SegmentLength) - { - m_Sections[i] = Allocate(); - memcpy( - &m_Sections[i]->m_BlockMeta, - &a_Src[i * SegmentLength], - sizeof(BLOCKTYPE) * SegmentLength - ); - 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) - ); - } + // No need for the section, the data is all zeroes + continue; } - } + + // Allocate the section and copy the data into it: + m_Sections[i] = Allocate(); + memcpy(&m_Sections[i]->m_BlockMeta, &a_Src[i * SegmentLength], sizeof(BLOCKTYPE) * SegmentLength); + 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)); + } // for i - m_Sections[] } + void cChunkData::SetBlockLight(const NIBBLETYPE * a_Src) { if (a_Src == NULL) { return; } - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + + for (size_t i = 0; i < NumSections; i++) { - const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + // If the section is already allocated, copy the data into it: if (m_Sections[i] != NULL) { - memcpy( - &m_Sections[i]->m_BlockLight, - &a_Src[i * SegmentLength], - sizeof(NIBBLETYPE) * SegmentLength - ); + memcpy(&m_Sections[i]->m_BlockLight, &a_Src[i * SegmentLength], sizeof(NIBBLETYPE) * SegmentLength); + continue; } - else + + // The section doesn't exist, find out if it is needed: + if (IsAllValue(a_Src + i * SegmentLength, SegmentLength, (NIBBLETYPE)0)) { - // j counts how many of leading zeros the buffer has - // if j == SegmentLength then the buffer is all zeros so there is no point - // creating the buffer. - size_t j = 0; - // do nothing whilst 0 - for (; (j < SegmentLength) && (a_Src[i * SegmentLength + j] == 0); j++); - if (j != SegmentLength) - { - m_Sections[i] = Allocate(); - memcpy( - &m_Sections[i]->m_BlockLight, - &a_Src[i * SegmentLength], - sizeof(BLOCKTYPE) * SegmentLength - ); - 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) - ); - } + // No need for the section, the data is all zeroes + continue; } - } + + // Allocate the section and copy the data into it: + m_Sections[i] = Allocate(); + memcpy(&m_Sections[i]->m_BlockLight, &a_Src[i * SegmentLength], sizeof(BLOCKTYPE) * SegmentLength); + 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)); + } // for i - m_Sections[] } @@ -598,51 +529,29 @@ void cChunkData::SetSkyLight(const NIBBLETYPE * a_Src) return; } - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + for (size_t i = 0; i < NumSections; i++) { - const size_t SegmentLength = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; + // If the section is already allocated, copy the data into it: if (m_Sections[i] != NULL) { - memcpy( - &m_Sections[i]->m_BlockSkyLight, - &a_Src[i * SegmentLength], - sizeof(NIBBLETYPE) * SegmentLength - ); + memcpy(&m_Sections[i]->m_BlockSkyLight, &a_Src[i * SegmentLength], sizeof(NIBBLETYPE) * SegmentLength); + continue; } - else + + // The section doesn't exist, find out if it is needed: + if (IsAllValue(a_Src + i * SegmentLength, SegmentLength, (NIBBLETYPE)0xff)) { - // j counts how many of leading zeros the buffer has - // if j == SegmentLength then the buffer is all zeros so there is no point - // creating the buffer. - size_t j = 0; - // do nothing whilst 0 - for (; (j < SegmentLength) && (a_Src[i * SegmentLength + j]) == 0xFF; j++); - if (j != SegmentLength) - { - m_Sections[i] = Allocate(); - memcpy( - &m_Sections[i]->m_BlockSkyLight, - &a_Src[i * SegmentLength], - sizeof(BLOCKTYPE) * SegmentLength - ); - 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) - ); - } + // No need for the section, the data is all zeroes + continue; } - } + + // Allocate the section and copy the data into it: + m_Sections[i] = Allocate(); + memcpy(&m_Sections[i]->m_BlockSkyLight, &a_Src[i * SegmentLength], sizeof(BLOCKTYPE) * SegmentLength); + 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)); + } // for i - m_Sections[] } @@ -671,26 +580,10 @@ void cChunkData::Free(cChunkData::sChunkSection * a_Section) void cChunkData::ZeroSection(cChunkData::sChunkSection * a_Section) const { - memset( - a_Section->m_BlockTypes, - 0x00, - sizeof(a_Section->m_BlockTypes) - ); - memset( - a_Section->m_BlockMeta, - 0x00, - sizeof(a_Section->m_BlockMeta) - ); - memset( - a_Section->m_BlockLight, - 0x00, - sizeof(a_Section->m_BlockLight) - ); - memset( - a_Section->m_BlockSkyLight, - 0xFF, - sizeof(a_Section->m_BlockSkyLight) - ); + memset(a_Section->m_BlockTypes, 0x00, sizeof(a_Section->m_BlockTypes)); + memset(a_Section->m_BlockMeta, 0x00, sizeof(a_Section->m_BlockMeta)); + memset(a_Section->m_BlockLight, 0x00, sizeof(a_Section->m_BlockLight)); + memset(a_Section->m_BlockSkyLight, 0xff, sizeof(a_Section->m_BlockSkyLight)); } diff --git a/src/ChunkData.h b/src/ChunkData.h index e6aaa99b8..341c15301 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -1,4 +1,12 @@ +// ChunkData.h + +// Declares the cChunkData class that represents the block's type, meta, blocklight and skylight storage for a chunk + + + + + #pragma once @@ -20,7 +28,7 @@ class cChunkData { public: - cChunkData(); + cChunkData(void); ~cChunkData(); #if __cplusplus < 201103L @@ -43,25 +51,48 @@ public: NIBBLETYPE GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const; + /** Creates a (deep) copy of self. */ cChunkData Copy(void) const; - // Copys data from this object into the buffer in the a_Dest param - // CopyBlocks also povides the optional parameters a_Idx and a_Length which specify an offset and length for - // copying part of the BlockTypes array. - void CopyBlocks (BLOCKTYPE * a_Dest, size_t a_Idx = 0, size_t a_Length = cChunkDef::NumBlocks) const; - void CopyMeta (NIBBLETYPE * a_Dest) const; + /** 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; + + /** Copies the metadata into the specified flat array. */ + void CopyMetas(NIBBLETYPE * a_Dest) const; + + /** Copies the block light data into the specified flat array. */ void CopyBlockLight(NIBBLETYPE * a_Dest) const; + + /** Copies the skylight data into the specified flat array. */ void CopySkyLight (NIBBLETYPE * a_Dest) const; - void SetBlocks (const BLOCKTYPE * a_src); - void SetMeta (const NIBBLETYPE * a_src); - void SetBlockLight(const NIBBLETYPE * a_src); - void SetSkyLight (const NIBBLETYPE * a_src); + /** 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); + + /** 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); + + /** Copies the blocklight data from the specified flat array into the internal representation. + Allocates sectios that are needed for the operation. + Allows a_Src to be NULL, in which case it doesn't do anything. */ + 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. + Allows a_Src to be NULL, in which case it doesn't do anything. */ + void SetSkyLight(const NIBBLETYPE * a_Src); private: - static const size_t CHUNK_SECTION_HEIGHT = 16; - static const size_t CHUNK_SECTION_COUNT = (256 / CHUNK_SECTION_HEIGHT); + static const size_t SectionHeight = 16; + static const size_t NumSections = (cChunkDef::Height / SectionHeight); + static const size_t SegmentLength = cChunkDef::Width * cChunkDef::Height * SectionHeight; + static const size_t SectionBlockCount = SectionHeight * cChunkDef::Width * cChunkDef::Width; #if __cplusplus < 201103L // auto_ptr style interface for memory management @@ -69,13 +100,13 @@ private: #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]; + BLOCKTYPE m_BlockTypes [SectionBlockCount]; + NIBBLETYPE m_BlockMeta [SectionBlockCount / 2]; + NIBBLETYPE m_BlockLight [SectionBlockCount / 2]; + NIBBLETYPE m_BlockSkyLight[SectionBlockCount / 2]; }; - sChunkSection * m_Sections[CHUNK_SECTION_COUNT]; + sChunkSection * m_Sections[NumSections]; /** Allocates a new section. Entry-point to custom allocators. */ static sChunkSection * Allocate(void); diff --git a/src/ChunkDataCallback.h b/src/ChunkDataCallback.h index e916d6486..0c8b1098f 100644 --- a/src/ChunkDataCallback.h +++ b/src/ChunkDataCallback.h @@ -1,11 +1,20 @@ +// ChunkDataCallback.h + +// Declares the cChunkDataCallback interface and several trivial descendants, for reading chunk data + -#pragma once + +#pragma once + #include "ChunkData.h" + + + /** Interface class used for getting data out of a chunk using the GetAllData() function. Implementation must use the pointers immediately and NOT store any of them for later use The virtual methods are called in the same order as they're declared here. @@ -41,6 +50,10 @@ public: virtual void BlockEntity(cBlockEntity * a_Entity) {UNUSED(a_Entity); }; } ; + + + + /** A simple implementation of the cChunkDataCallback interface that collects all block data into a buffer */ class cChunkDataCollector : @@ -59,6 +72,9 @@ protected: }; + + + /** A simple implementation of the cChunkDataCallback interface that collects all block data into a single buffer */ class cChunkDataArrayCollector : @@ -73,15 +89,18 @@ protected: virtual void ChunkData(const cChunkData & a_ChunkBuffer) override { - a_ChunkBuffer.CopyBlocks(m_BlockData); - a_ChunkBuffer.CopyMeta(m_BlockData + cChunkDef::NumBlocks); + a_ChunkBuffer.CopyBlockTypes(m_BlockData); + a_ChunkBuffer.CopyMetas(m_BlockData + cChunkDef::NumBlocks); a_ChunkBuffer.CopyBlockLight(m_BlockData + 3 * cChunkDef::NumBlocks / 2); a_ChunkBuffer.CopySkyLight(m_BlockData + 2 * cChunkDef::NumBlocks); } }; -/** A simple implementation of the cChunkDataCallback interface that collects all block data into a separate buffers -*/ + + + + +/** A simple implementation of the cChunkDataCallback interface that collects all block data into separate buffers */ class cChunkDataSeparateCollector : public cChunkDataCallback { @@ -96,10 +115,13 @@ protected: virtual void ChunkData(const cChunkData & a_ChunkBuffer) override { - a_ChunkBuffer.CopyBlocks(m_BlockTypes); - a_ChunkBuffer.CopyMeta(m_BlockMetas); - a_ChunkBuffer.CopyBlockLight(m_BlockLight); - a_ChunkBuffer.CopySkyLight(m_BlockSkyLight); + a_ChunkBuffer.CopyBlockTypes(m_BlockTypes); + a_ChunkBuffer.CopyMetas(m_BlockMetas); + a_ChunkBuffer.CopyBlockLight(m_BlockLight); + a_ChunkBuffer.CopySkyLight(m_BlockSkyLight); } } ; + + + diff --git a/src/LightingThread.cpp b/src/LightingThread.cpp index 879252c34..dc19bf500 100644 --- a/src/LightingThread.cpp +++ b/src/LightingThread.cpp @@ -27,7 +27,7 @@ class cReader : { for (int z = 0; z < cChunkDef::Width; z++) { - a_ChunkBuffer.CopyBlocks(OutputRows + OutputIdx * 16, InputIdx * 16, 16); + a_ChunkBuffer.CopyBlockTypes(OutputRows + OutputIdx * 16, InputIdx * 16, 16); InputIdx++; OutputIdx += 3; } // for z -- cgit v1.2.3 From 80fe19c0e2e4433d8332dbe5a9c2fcba0be06f68 Mon Sep 17 00:00:00 2001 From: worktycho Date: Thu, 29 May 2014 17:41:07 +0100 Subject: Fixed overflow bug --- src/ChunkData.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index cc4793ec3..13be65fd7 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -333,25 +333,28 @@ cChunkData cChunkData::Copy(void) const void cChunkData::CopyBlockTypes(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Length) const { - // TODO: This code seems wrong. It always copies into a_Dest[i * SegmentLength], even when a_Idx > 0 + size_t toskip = a_Idx; + for (size_t i = 0; i < NumSections; i++) { const size_t SegmentLength = SectionHeight * 16 * 16; - if (a_Idx > 0) + size_t startpos = 0; + if (toskip > 0) { - a_Idx = std::max(a_Idx - a_Length, (size_t) 0); + toskip = std::max(toskip - SegmentLength, (size_t) 0); + startpos = SegmentLength - toskip; } - if (a_Idx == 0) + if (toskip == 0) { size_t ToCopy = std::min(SegmentLength, a_Length); a_Length -= ToCopy; if (m_Sections[i] != NULL) { - memcpy(&a_Dest[i * SegmentLength], &m_Sections[i]->m_BlockTypes, sizeof(BLOCKTYPE) * ToCopy); + memcpy(&a_Dest[(i * SegmentLength) - a_Idx], (&m_Sections[i]->m_BlockTypes) + startpos, sizeof(BLOCKTYPE) * (ToCopy - startpos)); } else { - memset(&a_Dest[i * SegmentLength], 0, sizeof(BLOCKTYPE) * ToCopy); + memset(&a_Dest[(i * SegmentLength) - a_Idx], 0, sizeof(BLOCKTYPE) * (ToCopy - startpos)); } } } -- cgit v1.2.3 From 0cfee5d141f63d0d9ee339e4fab3ad8ac15f884d Mon Sep 17 00:00:00 2001 From: worktycho Date: Thu, 29 May 2014 19:10:35 +0100 Subject: Fixed Wrong types in nibble sizeofs --- src/ChunkData.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 13be65fd7..46bd0b946 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -375,7 +375,7 @@ void cChunkData::CopyMetas(NIBBLETYPE * a_Dest) const } else { - memset(&a_Dest[i * SegmentLength], 0, sizeof(BLOCKTYPE) * SegmentLength); + memset(&a_Dest[i * SegmentLength], 0, sizeof(NIBBLETYPE) * SegmentLength); } } } @@ -395,7 +395,7 @@ void cChunkData::CopyBlockLight(NIBBLETYPE * a_Dest) const } else { - memset(&a_Dest[i * SegmentLength], 0, sizeof(BLOCKTYPE) * SegmentLength); + memset(&a_Dest[i * SegmentLength], 0, sizeof(NIBBLETYPE) * SegmentLength); } } } @@ -415,7 +415,7 @@ void cChunkData::CopySkyLight(NIBBLETYPE * a_Dest) const } else { - memset(&a_Dest[i * SegmentLength], 0xff, sizeof(BLOCKTYPE) * SegmentLength); + memset(&a_Dest[i * SegmentLength], 0xff, sizeof(NIBBLETYPE) * SegmentLength); } } } -- cgit v1.2.3 From 59068b77b7215461b4aaf11c2db91c1657b7f91d Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 29 May 2014 20:18:37 +0200 Subject: Fixed wrong block sizes for copying / setting. --- src/ChunkData.cpp | 75 ++++++++++++++++++++++++++----------------------------- src/ChunkData.h | 3 +-- 2 files changed, 36 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 46bd0b946..2628b3410 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -208,7 +208,7 @@ NIBBLETYPE cChunkData::GetMeta(int a_RelX, int a_RelY, int a_RelZ) const if (m_Sections[Section] != NULL) { int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * SectionHeight), a_RelZ); - return (m_Sections[Section]->m_BlockMeta[Index / 2] >> ((Index & 1) * 4)) & 0x0f; + return (m_Sections[Section]->m_BlockMetas[Index / 2] >> ((Index & 1) * 4)) & 0x0f; } else { @@ -251,9 +251,9 @@ bool cChunkData::SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble ZeroSection(m_Sections[Section]); } int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * SectionHeight), a_RelZ); - NIBBLETYPE oldval = m_Sections[Section]->m_BlockMeta[Index / 2] >> ((Index & 1) * 4) & 0xf; - m_Sections[Section]->m_BlockMeta[Index / 2] = static_cast( - (m_Sections[Section]->m_BlockMeta[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble + NIBBLETYPE oldval = m_Sections[Section]->m_BlockMetas[Index / 2] >> ((Index & 1) * 4) & 0xf; + m_Sections[Section]->m_BlockMetas[Index / 2] = static_cast( + (m_Sections[Section]->m_BlockMetas[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble ((a_Nibble & 0x0f) << ((Index & 1) * 4)) // The nibble being set ); return oldval == a_Nibble; @@ -333,28 +333,27 @@ cChunkData cChunkData::Copy(void) const void cChunkData::CopyBlockTypes(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Length) const { - size_t toskip = a_Idx; + size_t ToSkip = a_Idx; for (size_t i = 0; i < NumSections; i++) { - const size_t SegmentLength = SectionHeight * 16 * 16; - size_t startpos = 0; - if (toskip > 0) + size_t StartPos = 0; + if (ToSkip > 0) { - toskip = std::max(toskip - SegmentLength, (size_t) 0); - startpos = SegmentLength - toskip; + ToSkip = std::max(ToSkip - SectionBlockCount, (size_t) 0); // TODO: Still can underflow + StartPos = SectionBlockCount - ToSkip; } - if (toskip == 0) + if (ToSkip == 0) { - size_t ToCopy = std::min(SegmentLength, a_Length); + size_t ToCopy = std::min(SectionBlockCount, a_Length); a_Length -= ToCopy; if (m_Sections[i] != NULL) { - memcpy(&a_Dest[(i * SegmentLength) - a_Idx], (&m_Sections[i]->m_BlockTypes) + startpos, sizeof(BLOCKTYPE) * (ToCopy - startpos)); + memcpy(&a_Dest[(i * SectionBlockCount) - a_Idx], (&m_Sections[i]->m_BlockTypes) + StartPos, sizeof(BLOCKTYPE) * (ToCopy - StartPos)); } else { - memset(&a_Dest[(i * SegmentLength) - a_Idx], 0, sizeof(BLOCKTYPE) * (ToCopy - startpos)); + memset(&a_Dest[(i * SectionBlockCount) - a_Idx], 0, sizeof(BLOCKTYPE) * (ToCopy - StartPos)); } } } @@ -368,14 +367,13 @@ void cChunkData::CopyMetas(NIBBLETYPE * a_Dest) const { for (size_t i = 0; i < NumSections; i++) { - const size_t SegmentLength = SectionHeight * 16 * 16 / 2; if (m_Sections[i] != NULL) { - memcpy(&a_Dest[i * SegmentLength], &m_Sections[i]->m_BlockMeta, sizeof(NIBBLETYPE) * SegmentLength); + memcpy(&a_Dest[i * SectionBlockCount / 2], &m_Sections[i]->m_BlockMetas, sizeof(m_Sections[i]->m_BlockMetas)); } else { - memset(&a_Dest[i * SegmentLength], 0, sizeof(NIBBLETYPE) * SegmentLength); + memset(&a_Dest[i * SectionBlockCount / 2], 0, sizeof(m_Sections[i]->m_BlockMetas)); } } } @@ -388,14 +386,13 @@ void cChunkData::CopyBlockLight(NIBBLETYPE * a_Dest) const { for (size_t i = 0; i < NumSections; i++) { - const size_t SegmentLength = SectionHeight * 16 * 16 / 2; if (m_Sections[i] != NULL) { - memcpy(&a_Dest[i * SegmentLength], &m_Sections[i]->m_BlockLight, sizeof(NIBBLETYPE) * SegmentLength); + memcpy(&a_Dest[i * SectionBlockCount / 2], &m_Sections[i]->m_BlockLight, sizeof(m_Sections[i]->m_BlockLight)); } else { - memset(&a_Dest[i * SegmentLength], 0, sizeof(NIBBLETYPE) * SegmentLength); + memset(&a_Dest[i * SectionBlockCount / 2], 0, sizeof(m_Sections[i]->m_BlockLight)); } } } @@ -408,14 +405,13 @@ void cChunkData::CopySkyLight(NIBBLETYPE * a_Dest) const { for (size_t i = 0; i < NumSections; i++) { - const size_t SegmentLength = SectionHeight * 16 * 16 / 2; if (m_Sections[i] != NULL) { - memcpy(&a_Dest[i * SegmentLength], &m_Sections[i]->m_BlockSkyLight, sizeof(NIBBLETYPE) * SegmentLength); + memcpy(&a_Dest[i * SectionBlockCount / 2], &m_Sections[i]->m_BlockSkyLight, sizeof(m_Sections[i]->m_BlockSkyLight)); } else { - memset(&a_Dest[i * SegmentLength], 0xff, sizeof(NIBBLETYPE) * SegmentLength); + memset(&a_Dest[i * SectionBlockCount / 2], 0xff, sizeof(m_Sections[i]->m_BlockSkyLight)); } } } @@ -428,18 +424,17 @@ void cChunkData::SetBlockTypes(const BLOCKTYPE * a_Src) { ASSERT(a_Src != NULL); - static const size_t SegmentLength = SectionHeight * 16 * 16; for (size_t i = 0; i < NumSections; i++) { // If the section is already allocated, copy the data into it: if (m_Sections[i] != NULL) { - memcpy(&m_Sections[i]->m_BlockTypes, &a_Src[i * SegmentLength], sizeof(BLOCKTYPE) * SegmentLength); + memcpy(&m_Sections[i]->m_BlockTypes, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockTypes)); continue; } // The section doesn't exist, find out if it is needed: - if (IsAllValue(a_Src + i * SegmentLength, SegmentLength, (const BLOCKTYPE)0)) + if (IsAllValue(a_Src + i * SectionBlockCount, SectionBlockCount, (const BLOCKTYPE)0)) { // No need for the section, the data is all-air continue; @@ -447,8 +442,8 @@ void cChunkData::SetBlockTypes(const BLOCKTYPE * a_Src) // Allocate the section and copy the data into it: m_Sections[i] = Allocate(); - memcpy(&m_Sections[i]->m_BlockTypes, &a_Src[i * SegmentLength], sizeof(BLOCKTYPE) * SegmentLength); - memset(m_Sections[i]->m_BlockMeta, 0x00, sizeof(m_Sections[i]->m_BlockMeta)); + memcpy(&m_Sections[i]->m_BlockTypes, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockTypes)); + memset(m_Sections[i]->m_BlockMetas, 0x00, sizeof(m_Sections[i]->m_BlockMetas)); 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)); } // for i - m_Sections[] @@ -466,12 +461,12 @@ void cChunkData::SetMetas(const NIBBLETYPE * a_Src) // If the section is already allocated, copy the data into it: if (m_Sections[i] != NULL) { - memcpy(&m_Sections[i]->m_BlockMeta, &a_Src[i * SegmentLength], sizeof(NIBBLETYPE) * SegmentLength); + memcpy(&m_Sections[i]->m_BlockMetas, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockMetas)); continue; } // The section doesn't exist, find out if it is needed: - if (IsAllValue(a_Src + i * SegmentLength, SegmentLength, (NIBBLETYPE)0)) + if (IsAllValue(a_Src + i * SectionBlockCount, SectionBlockCount, (NIBBLETYPE)0)) { // No need for the section, the data is all zeroes continue; @@ -479,7 +474,7 @@ void cChunkData::SetMetas(const NIBBLETYPE * a_Src) // Allocate the section and copy the data into it: m_Sections[i] = Allocate(); - memcpy(&m_Sections[i]->m_BlockMeta, &a_Src[i * SegmentLength], sizeof(BLOCKTYPE) * SegmentLength); + memcpy(&m_Sections[i]->m_BlockMetas, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockMetas)); 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)); @@ -502,12 +497,12 @@ void cChunkData::SetBlockLight(const NIBBLETYPE * a_Src) // If the section is already allocated, copy the data into it: if (m_Sections[i] != NULL) { - memcpy(&m_Sections[i]->m_BlockLight, &a_Src[i * SegmentLength], sizeof(NIBBLETYPE) * SegmentLength); + memcpy(&m_Sections[i]->m_BlockLight, &a_Src[i * SectionBlockCount], sizeof(NIBBLETYPE) * SectionBlockCount); continue; } // The section doesn't exist, find out if it is needed: - if (IsAllValue(a_Src + i * SegmentLength, SegmentLength, (NIBBLETYPE)0)) + if (IsAllValue(a_Src + i * SectionBlockCount, SectionBlockCount, (NIBBLETYPE)0)) { // No need for the section, the data is all zeroes continue; @@ -515,9 +510,9 @@ void cChunkData::SetBlockLight(const NIBBLETYPE * a_Src) // Allocate the section and copy the data into it: m_Sections[i] = Allocate(); - memcpy(&m_Sections[i]->m_BlockLight, &a_Src[i * SegmentLength], sizeof(BLOCKTYPE) * SegmentLength); + memcpy(&m_Sections[i]->m_BlockLight, &a_Src[i * SectionBlockCount], sizeof(BLOCKTYPE) * SectionBlockCount); 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_BlockMetas, 0x00, sizeof(m_Sections[i]->m_BlockMetas)); memset(m_Sections[i]->m_BlockSkyLight, 0xff, sizeof(m_Sections[i]->m_BlockSkyLight)); } // for i - m_Sections[] } @@ -537,12 +532,12 @@ void cChunkData::SetSkyLight(const NIBBLETYPE * a_Src) // If the section is already allocated, copy the data into it: if (m_Sections[i] != NULL) { - memcpy(&m_Sections[i]->m_BlockSkyLight, &a_Src[i * SegmentLength], sizeof(NIBBLETYPE) * SegmentLength); + memcpy(&m_Sections[i]->m_BlockSkyLight, &a_Src[i * SectionBlockCount], sizeof(NIBBLETYPE) * SectionBlockCount); continue; } // The section doesn't exist, find out if it is needed: - if (IsAllValue(a_Src + i * SegmentLength, SegmentLength, (NIBBLETYPE)0xff)) + if (IsAllValue(a_Src + i * SectionBlockCount, SectionBlockCount, (NIBBLETYPE)0xff)) { // No need for the section, the data is all zeroes continue; @@ -550,9 +545,9 @@ void cChunkData::SetSkyLight(const NIBBLETYPE * a_Src) // Allocate the section and copy the data into it: m_Sections[i] = Allocate(); - memcpy(&m_Sections[i]->m_BlockSkyLight, &a_Src[i * SegmentLength], sizeof(BLOCKTYPE) * SegmentLength); + memcpy(&m_Sections[i]->m_BlockSkyLight, &a_Src[i * SectionBlockCount], sizeof(BLOCKTYPE) * SectionBlockCount); 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_BlockMetas, 0x00, sizeof(m_Sections[i]->m_BlockMetas)); memset(m_Sections[i]->m_BlockLight, 0x00, sizeof(m_Sections[i]->m_BlockLight)); } // for i - m_Sections[] } @@ -584,7 +579,7 @@ void cChunkData::Free(cChunkData::sChunkSection * a_Section) void cChunkData::ZeroSection(cChunkData::sChunkSection * a_Section) const { memset(a_Section->m_BlockTypes, 0x00, sizeof(a_Section->m_BlockTypes)); - memset(a_Section->m_BlockMeta, 0x00, sizeof(a_Section->m_BlockMeta)); + memset(a_Section->m_BlockMetas, 0x00, sizeof(a_Section->m_BlockMetas)); memset(a_Section->m_BlockLight, 0x00, sizeof(a_Section->m_BlockLight)); memset(a_Section->m_BlockSkyLight, 0xff, sizeof(a_Section->m_BlockSkyLight)); } diff --git a/src/ChunkData.h b/src/ChunkData.h index 341c15301..fef31b5ad 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -91,7 +91,6 @@ private: static const size_t SectionHeight = 16; static const size_t NumSections = (cChunkDef::Height / SectionHeight); - static const size_t SegmentLength = cChunkDef::Width * cChunkDef::Height * SectionHeight; static const size_t SectionBlockCount = SectionHeight * cChunkDef::Width * cChunkDef::Width; #if __cplusplus < 201103L @@ -101,7 +100,7 @@ private: struct sChunkSection { BLOCKTYPE m_BlockTypes [SectionBlockCount]; - NIBBLETYPE m_BlockMeta [SectionBlockCount / 2]; + NIBBLETYPE m_BlockMetas [SectionBlockCount / 2]; NIBBLETYPE m_BlockLight [SectionBlockCount / 2]; NIBBLETYPE m_BlockSkyLight[SectionBlockCount / 2]; }; -- cgit v1.2.3 From b50181e3614286b9624cf3b8f296d7b25509a55d Mon Sep 17 00:00:00 2001 From: worktycho Date: Thu, 29 May 2014 19:29:06 +0100 Subject: fix underflow Wish c++ could specify saturating unsigned underflow. --- src/ChunkData.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 2628b3410..72187f393 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -340,7 +340,7 @@ void cChunkData::CopyBlockTypes(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Lengt size_t StartPos = 0; if (ToSkip > 0) { - ToSkip = std::max(ToSkip - SectionBlockCount, (size_t) 0); // TODO: Still can underflow + ToSkip = std::max((ssize_t)ToSkip - (ssize_t)SectionBlockCount, (size_t) 0); StartPos = SectionBlockCount - ToSkip; } if (ToSkip == 0) -- cgit v1.2.3 From 901e3ec4940f2a11f7866194f02732c813ce71b9 Mon Sep 17 00:00:00 2001 From: worktycho Date: Thu, 29 May 2014 19:35:47 +0100 Subject: Derp --- src/ChunkData.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 72187f393..382e7a5a8 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -340,7 +340,7 @@ void cChunkData::CopyBlockTypes(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Lengt size_t StartPos = 0; if (ToSkip > 0) { - ToSkip = std::max((ssize_t)ToSkip - (ssize_t)SectionBlockCount, (size_t) 0); + ToSkip = std::max((ssize_t)ToSkip - (ssize_t)SectionBlockCount, (ssize_t) 0); StartPos = SectionBlockCount - ToSkip; } if (ToSkip == 0) -- cgit v1.2.3 From be10f07db02565fad77937bc6e9ccc6580f19158 Mon Sep 17 00:00:00 2001 From: worktycho Date: Thu, 29 May 2014 19:44:36 +0100 Subject: Fix bug when a_Idx is not a multiple of SectionBLockCount --- src/ChunkData.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 382e7a5a8..be96be051 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -343,7 +343,7 @@ void cChunkData::CopyBlockTypes(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Lengt ToSkip = std::max((ssize_t)ToSkip - (ssize_t)SectionBlockCount, (ssize_t) 0); StartPos = SectionBlockCount - ToSkip; } - if (ToSkip == 0) + if (ToSkip < SectionBlockCount) { size_t ToCopy = std::min(SectionBlockCount, a_Length); a_Length -= ToCopy; -- cgit v1.2.3 From 8c4dd5dcfd91f8223e78ff7fd24f8e4e73d1f509 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 29 May 2014 21:41:44 +0200 Subject: Attempt at fixing an unresolved symbol in gcc / clang. --- src/ChunkData.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index be96be051..74f5d78ba 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -345,7 +345,7 @@ void cChunkData::CopyBlockTypes(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Lengt } if (ToSkip < SectionBlockCount) { - size_t ToCopy = std::min(SectionBlockCount, a_Length); + size_t ToCopy = std::min(+SectionBlockCount, a_Length); a_Length -= ToCopy; if (m_Sections[i] != NULL) { -- cgit v1.2.3 From 0e2138736cd969a0dc931bbef31262da906cb832 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 30 May 2014 09:17:17 +0200 Subject: Fixed wrong copy sizes in cChunkData. --- src/ChunkData.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 74f5d78ba..5c11212ea 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -333,14 +333,14 @@ cChunkData cChunkData::Copy(void) const void cChunkData::CopyBlockTypes(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Length) const { - size_t ToSkip = a_Idx; + int ToSkip = a_Idx; for (size_t i = 0; i < NumSections; i++) { size_t StartPos = 0; if (ToSkip > 0) { - ToSkip = std::max((ssize_t)ToSkip - (ssize_t)SectionBlockCount, (ssize_t) 0); + ToSkip = std::max(ToSkip - (int)SectionBlockCount, 0); StartPos = SectionBlockCount - ToSkip; } if (ToSkip < SectionBlockCount) @@ -429,7 +429,7 @@ void cChunkData::SetBlockTypes(const BLOCKTYPE * a_Src) // If the section is already allocated, copy the data into it: if (m_Sections[i] != NULL) { - memcpy(&m_Sections[i]->m_BlockTypes, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockTypes)); + memcpy(m_Sections[i]->m_BlockTypes, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockTypes)); continue; } @@ -442,7 +442,7 @@ void cChunkData::SetBlockTypes(const BLOCKTYPE * a_Src) // Allocate the section and copy the data into it: m_Sections[i] = Allocate(); - memcpy(&m_Sections[i]->m_BlockTypes, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockTypes)); + memcpy(m_Sections[i]->m_BlockTypes, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockTypes)); memset(m_Sections[i]->m_BlockMetas, 0x00, sizeof(m_Sections[i]->m_BlockMetas)); 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)); @@ -461,7 +461,7 @@ void cChunkData::SetMetas(const NIBBLETYPE * a_Src) // If the section is already allocated, copy the data into it: if (m_Sections[i] != NULL) { - memcpy(&m_Sections[i]->m_BlockMetas, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockMetas)); + memcpy(m_Sections[i]->m_BlockMetas, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockMetas)); continue; } @@ -474,7 +474,7 @@ void cChunkData::SetMetas(const NIBBLETYPE * a_Src) // Allocate the section and copy the data into it: m_Sections[i] = Allocate(); - memcpy(&m_Sections[i]->m_BlockMetas, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockMetas)); + memcpy(m_Sections[i]->m_BlockMetas, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockMetas)); 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)); @@ -497,7 +497,7 @@ void cChunkData::SetBlockLight(const NIBBLETYPE * a_Src) // If the section is already allocated, copy the data into it: if (m_Sections[i] != NULL) { - memcpy(&m_Sections[i]->m_BlockLight, &a_Src[i * SectionBlockCount], sizeof(NIBBLETYPE) * SectionBlockCount); + memcpy(m_Sections[i]->m_BlockLight, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockLight)); continue; } @@ -510,7 +510,7 @@ void cChunkData::SetBlockLight(const NIBBLETYPE * a_Src) // Allocate the section and copy the data into it: m_Sections[i] = Allocate(); - memcpy(&m_Sections[i]->m_BlockLight, &a_Src[i * SectionBlockCount], sizeof(BLOCKTYPE) * SectionBlockCount); + memcpy(m_Sections[i]->m_BlockLight, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockLight)); memset(m_Sections[i]->m_BlockTypes, 0x00, sizeof(m_Sections[i]->m_BlockTypes)); memset(m_Sections[i]->m_BlockMetas, 0x00, sizeof(m_Sections[i]->m_BlockMetas)); memset(m_Sections[i]->m_BlockSkyLight, 0xff, sizeof(m_Sections[i]->m_BlockSkyLight)); @@ -532,7 +532,7 @@ void cChunkData::SetSkyLight(const NIBBLETYPE * a_Src) // If the section is already allocated, copy the data into it: if (m_Sections[i] != NULL) { - memcpy(&m_Sections[i]->m_BlockSkyLight, &a_Src[i * SectionBlockCount], sizeof(NIBBLETYPE) * SectionBlockCount); + memcpy(m_Sections[i]->m_BlockSkyLight, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockSkyLight)); continue; } @@ -545,7 +545,7 @@ void cChunkData::SetSkyLight(const NIBBLETYPE * a_Src) // Allocate the section and copy the data into it: m_Sections[i] = Allocate(); - memcpy(&m_Sections[i]->m_BlockSkyLight, &a_Src[i * SectionBlockCount], sizeof(BLOCKTYPE) * SectionBlockCount); + memcpy(m_Sections[i]->m_BlockSkyLight, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockSkyLight)); memset(m_Sections[i]->m_BlockTypes, 0x00, sizeof(m_Sections[i]->m_BlockTypes)); memset(m_Sections[i]->m_BlockMetas, 0x00, sizeof(m_Sections[i]->m_BlockMetas)); memset(m_Sections[i]->m_BlockLight, 0x00, sizeof(m_Sections[i]->m_BlockLight)); -- cgit v1.2.3 From 5368c5dd79bce465d32517eec62701abc9b380d9 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 30 May 2014 09:49:57 +0200 Subject: Fixed sign comparison. --- src/ChunkData.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 5c11212ea..6ccb28fe4 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -343,7 +343,7 @@ void cChunkData::CopyBlockTypes(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Lengt ToSkip = std::max(ToSkip - (int)SectionBlockCount, 0); StartPos = SectionBlockCount - ToSkip; } - if (ToSkip < SectionBlockCount) + if (ToSkip < (int)SectionBlockCount) { size_t ToCopy = std::min(+SectionBlockCount, a_Length); a_Length -= ToCopy; -- cgit v1.2.3 From 0b60caac4ae8e7762cc530a0a7b6a7abe8c33262 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 30 May 2014 10:56:12 +0200 Subject: Test failures are reported verbosely and into the debug console on Win. --- src/Globals.h | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/Globals.h b/src/Globals.h index 85cfd2f18..7b7a34541 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -271,9 +271,25 @@ void inline LOGERROR(const char* a_Format, ...) { }; + #ifdef _WIN32 + #define REPORT_ERROR(FMT, ...) \ + { \ + AString msg = Printf(FMT, __VA_ARGS__); \ + puts(msg.c_str()); \ + fflush(stdout); \ + OutputDebugStringA(msg.c_str()); \ + } + #else + #define REPORT_ERROR(FMT, ...) \ + { \ + AString msg = Printf(FMT, __VA_ARGS__); \ + puts(msg.c_str()); \ + fflush(stdout); \ + } + #endif #define ASSERT(x) do { if (!(x)) { throw cAssertFailure();} } while (0) - #define testassert(x) do { if(!(x)) { exit(1); } } while (0) - #define CheckAsserts(x) do { try {x} catch (cAssertFailure) { break; } exit(1); } while (0) + #define testassert(x) do { if(!(x)) { REPORT_ERROR("Test failure: %s, file %s, line %d\n", #x, __FILE__, __LINE__); exit(1); } } while (0) + #define CheckAsserts(x) do { try {x} catch (cAssertFailure) { break; } REPORT_ERROR("Test failure: assert didn't fire for %s, file %s, line %d\n", #x, __FILE__, __LINE__); exit(1); } while (0) #else #ifdef _DEBUG -- cgit v1.2.3 From 730e36844e152794cf85c4d1e9b7a84fd8be86ee Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 30 May 2014 11:01:13 +0200 Subject: Test failures break into MSVC debugger. --- src/Globals.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src') diff --git a/src/Globals.h b/src/Globals.h index 7b7a34541..c5768facf 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -272,12 +272,18 @@ void inline LOGERROR(const char* a_Format, ...) }; #ifdef _WIN32 + #if (defined(_MSC_VER) && defined(_DEBUG)) + #define DBG_BREAK _CrtDbgBreak() + #else + #define DBG_BREAK + #endif #define REPORT_ERROR(FMT, ...) \ { \ AString msg = Printf(FMT, __VA_ARGS__); \ puts(msg.c_str()); \ fflush(stdout); \ OutputDebugStringA(msg.c_str()); \ + DBG_BREAK; \ } #else #define REPORT_ERROR(FMT, ...) \ -- cgit v1.2.3 From e136f6e0f5b41ee7c9d784009ef40c9ac2e2e6fe Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 30 May 2014 11:35:29 +0200 Subject: Fixed cChunkData nibble copying. --- src/ChunkData.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 6ccb28fe4..6178dbc0d 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -461,12 +461,12 @@ void cChunkData::SetMetas(const NIBBLETYPE * a_Src) // If the section is already allocated, copy the data into it: if (m_Sections[i] != NULL) { - memcpy(m_Sections[i]->m_BlockMetas, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockMetas)); + memcpy(m_Sections[i]->m_BlockMetas, &a_Src[i * SectionBlockCount / 2], sizeof(m_Sections[i]->m_BlockMetas)); continue; } // The section doesn't exist, find out if it is needed: - if (IsAllValue(a_Src + i * SectionBlockCount, SectionBlockCount, (NIBBLETYPE)0)) + if (IsAllValue(a_Src + i * SectionBlockCount / 2, SectionBlockCount / 2, (NIBBLETYPE)0)) { // No need for the section, the data is all zeroes continue; @@ -474,7 +474,7 @@ void cChunkData::SetMetas(const NIBBLETYPE * a_Src) // Allocate the section and copy the data into it: m_Sections[i] = Allocate(); - memcpy(m_Sections[i]->m_BlockMetas, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockMetas)); + memcpy(m_Sections[i]->m_BlockMetas, &a_Src[i * SectionBlockCount / 2], sizeof(m_Sections[i]->m_BlockMetas)); 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)); @@ -497,12 +497,12 @@ void cChunkData::SetBlockLight(const NIBBLETYPE * a_Src) // If the section is already allocated, copy the data into it: if (m_Sections[i] != NULL) { - memcpy(m_Sections[i]->m_BlockLight, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockLight)); + memcpy(m_Sections[i]->m_BlockLight, &a_Src[i * SectionBlockCount / 2], sizeof(m_Sections[i]->m_BlockLight)); continue; } // The section doesn't exist, find out if it is needed: - if (IsAllValue(a_Src + i * SectionBlockCount, SectionBlockCount, (NIBBLETYPE)0)) + if (IsAllValue(a_Src + i * SectionBlockCount / 2, SectionBlockCount / 2, (NIBBLETYPE)0)) { // No need for the section, the data is all zeroes continue; @@ -510,7 +510,7 @@ void cChunkData::SetBlockLight(const NIBBLETYPE * a_Src) // Allocate the section and copy the data into it: m_Sections[i] = Allocate(); - memcpy(m_Sections[i]->m_BlockLight, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockLight)); + memcpy(m_Sections[i]->m_BlockLight, &a_Src[i * SectionBlockCount / 2], sizeof(m_Sections[i]->m_BlockLight)); memset(m_Sections[i]->m_BlockTypes, 0x00, sizeof(m_Sections[i]->m_BlockTypes)); memset(m_Sections[i]->m_BlockMetas, 0x00, sizeof(m_Sections[i]->m_BlockMetas)); memset(m_Sections[i]->m_BlockSkyLight, 0xff, sizeof(m_Sections[i]->m_BlockSkyLight)); @@ -532,12 +532,12 @@ void cChunkData::SetSkyLight(const NIBBLETYPE * a_Src) // If the section is already allocated, copy the data into it: if (m_Sections[i] != NULL) { - memcpy(m_Sections[i]->m_BlockSkyLight, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockSkyLight)); + memcpy(m_Sections[i]->m_BlockSkyLight, &a_Src[i * SectionBlockCount / 2], sizeof(m_Sections[i]->m_BlockSkyLight)); continue; } // The section doesn't exist, find out if it is needed: - if (IsAllValue(a_Src + i * SectionBlockCount, SectionBlockCount, (NIBBLETYPE)0xff)) + if (IsAllValue(a_Src + i * SectionBlockCount / 2, SectionBlockCount / 2, (NIBBLETYPE)0xff)) { // No need for the section, the data is all zeroes continue; @@ -545,7 +545,7 @@ void cChunkData::SetSkyLight(const NIBBLETYPE * a_Src) // Allocate the section and copy the data into it: m_Sections[i] = Allocate(); - memcpy(m_Sections[i]->m_BlockSkyLight, &a_Src[i * SectionBlockCount], sizeof(m_Sections[i]->m_BlockSkyLight)); + memcpy(m_Sections[i]->m_BlockSkyLight, &a_Src[i * SectionBlockCount / 2], sizeof(m_Sections[i]->m_BlockSkyLight)); memset(m_Sections[i]->m_BlockTypes, 0x00, sizeof(m_Sections[i]->m_BlockTypes)); memset(m_Sections[i]->m_BlockMetas, 0x00, sizeof(m_Sections[i]->m_BlockMetas)); memset(m_Sections[i]->m_BlockLight, 0x00, sizeof(m_Sections[i]->m_BlockLight)); -- cgit v1.2.3 From d854d3af1c724304997488d2b61518c85acb076a Mon Sep 17 00:00:00 2001 From: Tycho Date: Fri, 30 May 2014 14:43:33 +0100 Subject: removed unneded addressof --- src/ChunkData.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 6178dbc0d..258853ea0 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -55,6 +55,7 @@ cChunkData::~cChunkData() for (size_t i = 0; i < NumSections; i++) { Free(m_Sections[i]); + m_Sections[i] = NULL; } } @@ -93,6 +94,7 @@ cChunkData::~cChunkData() for (size_t i = 0; i < NumSections; i++) { Free(m_Sections[i]); + m_Sections[i] = NULL; } } @@ -101,6 +103,7 @@ cChunkData::~cChunkData() for (size_t i = 0; i < NumSections; i++) { m_Sections[i] = a_Other.m_Sections[i]; + a_Other.m_Sections[i] = NULL; } a_Other.m_IsOwner = false; return *this; @@ -333,27 +336,28 @@ cChunkData cChunkData::Copy(void) const void cChunkData::CopyBlockTypes(BLOCKTYPE * a_Dest, size_t a_Idx, size_t a_Length) const { - int ToSkip = a_Idx; + size_t ToSkip = a_Idx; for (size_t i = 0; i < NumSections; i++) { size_t StartPos = 0; if (ToSkip > 0) { - ToSkip = std::max(ToSkip - (int)SectionBlockCount, 0); - StartPos = SectionBlockCount - ToSkip; + StartPos = std::min(ToSkip, +SectionBlockCount); + ToSkip -= StartPos; } - if (ToSkip < (int)SectionBlockCount) + if (StartPos < SectionBlockCount) { - size_t ToCopy = std::min(+SectionBlockCount, a_Length); + size_t ToCopy = std::min(+SectionBlockCount - StartPos, a_Length); a_Length -= ToCopy; if (m_Sections[i] != NULL) { - memcpy(&a_Dest[(i * SectionBlockCount) - a_Idx], (&m_Sections[i]->m_BlockTypes) + StartPos, sizeof(BLOCKTYPE) * (ToCopy - StartPos)); + BLOCKTYPE * blockbuffer = m_Sections[i]->m_BlockTypes; + memcpy(&a_Dest[(i * SectionBlockCount) + StartPos - a_Idx], blockbuffer + StartPos, sizeof(BLOCKTYPE) * ToCopy); } else { - memset(&a_Dest[(i * SectionBlockCount) - a_Idx], 0, sizeof(BLOCKTYPE) * (ToCopy - StartPos)); + memset(&a_Dest[(i * SectionBlockCount) - a_Idx], 0, sizeof(BLOCKTYPE) * ToCopy); } } } -- cgit v1.2.3 From f26ddac61944ccae3223be5c4403171e04e306b9 Mon Sep 17 00:00:00 2001 From: Tycho Date: Fri, 30 May 2014 14:50:30 +0100 Subject: removed NULL assignment to const value --- src/ChunkData.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 258853ea0..142c141c4 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -103,7 +103,6 @@ cChunkData::~cChunkData() for (size_t i = 0; i < NumSections; i++) { m_Sections[i] = a_Other.m_Sections[i]; - a_Other.m_Sections[i] = NULL; } a_Other.m_IsOwner = false; return *this; -- cgit v1.2.3