From 8a890cf945cfbd72f6e4b64f8c7b52d2c6ca099e Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Mon, 22 May 2017 21:27:55 +0100 Subject: Store cChunk::m_BlockEntities in a map (#3717) * Store block entities in a map from block index * Cleanup ForEachBlockEntity * Cleanup DoWithBlockEntityAt --- src/Generating/ChunkDesc.cpp | 32 ++++++++++++++++++-------------- src/Generating/ChunkDesc.h | 4 ++-- 2 files changed, 20 insertions(+), 16 deletions(-) (limited to 'src/Generating') diff --git a/src/Generating/ChunkDesc.cpp b/src/Generating/ChunkDesc.cpp index 6ba63d5ce..630a3913c 100644 --- a/src/Generating/ChunkDesc.cpp +++ b/src/Generating/ChunkDesc.cpp @@ -573,23 +573,27 @@ void cChunkDesc::RandomFillRelCuboid( cBlockEntity * cChunkDesc::GetBlockEntity(int a_RelX, int a_RelY, int a_RelZ) { - int AbsX = a_RelX + m_ChunkX * cChunkDef::Width; - int AbsZ = a_RelZ + m_ChunkZ * cChunkDef::Width; - for (cBlockEntityList::iterator itr = m_BlockEntities.begin(), end = m_BlockEntities.end(); itr != end; ++itr) + auto Idx = cChunkDef::MakeIndex(a_RelX, a_RelY, a_RelZ); + auto itr = m_BlockEntities.find(Idx); + + if (itr != m_BlockEntities.end()) { - if (((*itr)->GetPosX() == AbsX) && ((*itr)->GetPosY() == a_RelY) && ((*itr)->GetPosZ() == AbsZ)) + // Already in the list: + cBlockEntity * BlockEntity = itr->second; + if (BlockEntity->GetBlockType() == GetBlockType(a_RelX, a_RelY, a_RelZ)) { - // Already in the list: - if ((*itr)->GetBlockType() != GetBlockType(a_RelX, a_RelY, a_RelZ)) - { - // Wrong type, the block type has been overwritten. Erase and create new: - m_BlockEntities.erase(itr); - break; - } // Correct type, already present. Return it: - return *itr; + return BlockEntity; } - } // for itr - m_BlockEntities[] + else + { + // Wrong type, the block type has been overwritten. Erase and create new: + m_BlockEntities.erase(itr); + } + } + + int AbsX = a_RelX + m_ChunkX * cChunkDef::Width; + int AbsZ = a_RelZ + m_ChunkZ * cChunkDef::Width; // The block entity is not created yet, try to create it and add to list: cBlockEntity * be = cBlockEntity::CreateByBlockType(GetBlockType(a_RelX, a_RelY, a_RelZ), GetBlockMeta(a_RelX, a_RelY, a_RelZ), AbsX, a_RelY, AbsZ); @@ -598,7 +602,7 @@ cBlockEntity * cChunkDesc::GetBlockEntity(int a_RelX, int a_RelY, int a_RelZ) // No block entity for this block type return nullptr; } - m_BlockEntities.push_back(be); + m_BlockEntities.insert({ Idx, be }); return be; } diff --git a/src/Generating/ChunkDesc.h b/src/Generating/ChunkDesc.h index 9e3f4af5e..d1da5992d 100644 --- a/src/Generating/ChunkDesc.h +++ b/src/Generating/ChunkDesc.h @@ -215,7 +215,7 @@ public: inline BlockNibbleBytes & GetBlockMetasUncompressed(void) { return *(reinterpret_cast(m_BlockArea.GetBlockMetas())); } inline cChunkDef::HeightMap & GetHeightMap (void) { return m_HeightMap; } inline cEntityList & GetEntities (void) { return m_Entities; } - inline cBlockEntityList & GetBlockEntities (void) { return m_BlockEntities; } + inline cBlockEntities & GetBlockEntities (void) { return m_BlockEntities; } /** Compresses the metas from the BlockArea format (1 meta per byte) into regular format (2 metas per byte) */ void CompressBlockMetas(cChunkDef::BlockNibbles & a_DestMetas); @@ -233,7 +233,7 @@ private: cBlockArea m_BlockArea; cChunkDef::HeightMap m_HeightMap; cEntityList m_Entities; // Individual entities are NOT owned by this object! - cBlockEntityList m_BlockEntities; // Individual block entities are NOT owned by this object! + cBlockEntities m_BlockEntities; // Individual block entities are NOT owned by this object! bool m_bUseDefaultBiomes; bool m_bUseDefaultHeight; -- cgit v1.2.3