1
0

Replace custom ChunkCoordinate with cChunkCoords

This commit is contained in:
Tiger Wang 2020-12-21 15:51:43 +00:00
parent f3ec768dfb
commit 53ae358d8c
2 changed files with 6 additions and 20 deletions

View File

@ -63,7 +63,7 @@ cChunk & cChunkMap::ConstructChunk(int a_ChunkX, int a_ChunkZ)
{ {
// If not exists insert. Then, return the chunk at these coordinates: // If not exists insert. Then, return the chunk at these coordinates:
return m_Chunks.try_emplace( return m_Chunks.try_emplace(
ChunkCoordinate{ a_ChunkX, a_ChunkZ }, { a_ChunkX, a_ChunkZ },
a_ChunkX, a_ChunkZ, this, m_World, *m_Pool a_ChunkX, a_ChunkZ, this, m_World, *m_Pool
).first->second; ).first->second;
} }
@ -1548,7 +1548,7 @@ bool cChunkMap::ForEachLoadedChunk(cFunctionRef<bool(int, int)> a_Callback) cons
{ {
if (Chunk.second.IsValid()) if (Chunk.second.IsValid())
{ {
if (a_Callback(Chunk.first.ChunkX, Chunk.first.ChunkZ)) if (a_Callback(Chunk.first.m_ChunkX, Chunk.first.m_ChunkZ))
{ {
return false; return false;
} }
@ -1729,11 +1729,11 @@ void cChunkMap::UnloadUnusedChunks(void)
{ {
if ( if (
itr->second.CanUnload() && // Can unload itr->second.CanUnload() && // Can unload
!cPluginManager::Get()->CallHookChunkUnloading(*GetWorld(), itr->first.ChunkX, itr->first.ChunkZ) // Plugins agree !cPluginManager::Get()->CallHookChunkUnloading(*GetWorld(), itr->first.m_ChunkX, itr->first.m_ChunkZ) // Plugins agree
) )
{ {
// First notify plugins: // First notify plugins:
cPluginManager::Get()->CallHookChunkUnloaded(*m_World, itr->first.ChunkX, itr->first.ChunkZ); cPluginManager::Get()->CallHookChunkUnloaded(*m_World, itr->first.m_ChunkX, itr->first.m_ChunkZ);
// Notify entities within the chunk, while everything's still valid: // Notify entities within the chunk, while everything's still valid:
itr->second.OnUnload(); itr->second.OnUnload();
@ -1759,7 +1759,7 @@ void cChunkMap::SaveAllChunks(void) const
{ {
if (Chunk.second.IsValid() && Chunk.second.IsDirty()) if (Chunk.second.IsValid() && Chunk.second.IsDirty())
{ {
GetWorld()->GetStorage().QueueSaveChunk(Chunk.first.ChunkX, Chunk.first.ChunkZ); GetWorld()->GetStorage().QueueSaveChunk(Chunk.first.m_ChunkX, Chunk.first.m_ChunkZ);
} }
} }
} }

View File

@ -421,27 +421,13 @@ private:
} }
}; };
struct ChunkCoordinate
{
struct Comparer
{
bool operator() (const ChunkCoordinate & a_Lhs, const ChunkCoordinate & a_Rhs) const
{
return ((a_Lhs.ChunkX == a_Rhs.ChunkX) ? (a_Lhs.ChunkZ < a_Rhs.ChunkZ) : (a_Lhs.ChunkX < a_Rhs.ChunkX));
}
};
int ChunkX;
int ChunkZ;
};
typedef std::list<cChunkStay *> cChunkStays; typedef std::list<cChunkStay *> cChunkStays;
mutable cCriticalSection m_CSChunks; mutable cCriticalSection m_CSChunks;
/** A map of chunk coordinates to chunks. /** A map of chunk coordinates to chunks.
Uses a map (as opposed to unordered_map) because sorted maps are apparently faster. */ Uses a map (as opposed to unordered_map) because sorted maps are apparently faster. */
std::map<ChunkCoordinate, cChunk, ChunkCoordinate::Comparer> m_Chunks; std::map<cChunkCoords, cChunk> m_Chunks;
cEvent m_evtChunkValid; // Set whenever any chunk becomes valid, via ChunkValidated() cEvent m_evtChunkValid; // Set whenever any chunk becomes valid, via ChunkValidated()