From ea71bfa9b645cda80b7d4364c675ebaee8db8353 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 8 Feb 2014 21:55:21 +0100 Subject: Initial ChunkStay code. --- src/ChunkStay.cpp | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 src/ChunkStay.cpp (limited to 'src/ChunkStay.cpp') diff --git a/src/ChunkStay.cpp b/src/ChunkStay.cpp new file mode 100644 index 000000000..a64a2a3e4 --- /dev/null +++ b/src/ChunkStay.cpp @@ -0,0 +1,136 @@ + +// ChunkStay.cpp + +// Implements the cChunkStay class representing a base for classes that keep chunks loaded + +#include "Globals.h" +#include "ChunkStay.h" +#include "ChunkMap.h" + + + + + +cChunkStay::cChunkStay(void) : + m_ChunkMap(NULL) +{ +} + + + + + +cChunkStay::~cChunkStay() +{ + Clear(); +} + + + + + +void cChunkStay::Clear(void) +{ + if (m_ChunkMap != NULL) + { + Disable(); + } + m_Chunks.clear(); +} + + + + + +void cChunkStay::Add(int a_ChunkX, int a_ChunkZ) +{ + ASSERT(m_ChunkMap == NULL); + + for (cChunkCoordsVector::const_iterator itr = m_Chunks.begin(); itr != m_Chunks.end(); ++itr) + { + if ((itr->m_ChunkX == a_ChunkX) && (itr->m_ChunkZ == a_ChunkZ)) + { + // Already present + return; + } + } // for itr - Chunks[] + m_Chunks.push_back(cChunkCoords(a_ChunkX, ZERO_CHUNK_Y, a_ChunkZ)); +} + + + + + +void cChunkStay::Remove(int a_ChunkX, int a_ChunkZ) +{ + ASSERT(m_ChunkMap == NULL); + + for (cChunkCoordsVector::iterator itr = m_Chunks.begin(); itr != m_Chunks.end(); ++itr) + { + if ((itr->m_ChunkX == a_ChunkX) && (itr->m_ChunkZ == a_ChunkZ)) + { + // Found, un-"stay" + m_Chunks.erase(itr); + return; + } + } // for itr - m_Chunks[] +} + + + + + +void cChunkStay::Enable(cChunkMap & a_ChunkMap) +{ + ASSERT(m_ChunkMap == NULL); + + m_ChunkMap = &a_ChunkMap; + a_ChunkMap.AddChunkStay(*this); + m_OutstandingChunks = m_Chunks; +} + + + + + +void cChunkStay::Disable(void) +{ + ASSERT(m_ChunkMap != NULL); + + m_ChunkMap->DelChunkStay(*this); + m_ChunkMap = NULL; +} + + + + + +void cChunkStay::ChunkAvailable(int a_ChunkX, int a_ChunkZ) +{ + // Check if this is a chunk that we want: + bool IsMine = false; + for (cChunkCoordsVector::const_iterator itr = m_OutstandingChunks.begin(), end = m_OutstandingChunks.end(); itr != end; ++itr) + { + if ((itr->m_ChunkX == a_ChunkX) && (itr->m_ChunkZ == a_ChunkZ)) + { + m_OutstandingChunks.erase(itr); + IsMine = true; + break; + } + } // for itr - m_OutstandingChunks[] + if (!IsMine) + { + return; + } + + // Call the appropriate callbacks: + OnChunkAvailable(a_ChunkX, a_ChunkZ); + if (m_OutstandingChunks.empty()) + { + OnAllChunksAvailable(); + } +} + + + + -- cgit v1.2.3 From a4bf44858dd1dc0c986cc1ed18cf8c37487207ff Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 8 Feb 2014 22:01:04 +0100 Subject: Fixed gcc compilation. --- src/ChunkStay.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ChunkStay.cpp') diff --git a/src/ChunkStay.cpp b/src/ChunkStay.cpp index a64a2a3e4..e86501c4e 100644 --- a/src/ChunkStay.cpp +++ b/src/ChunkStay.cpp @@ -109,7 +109,7 @@ void cChunkStay::ChunkAvailable(int a_ChunkX, int a_ChunkZ) { // Check if this is a chunk that we want: bool IsMine = false; - for (cChunkCoordsVector::const_iterator itr = m_OutstandingChunks.begin(), end = m_OutstandingChunks.end(); itr != end; ++itr) + for (cChunkCoordsVector::iterator itr = m_OutstandingChunks.begin(), end = m_OutstandingChunks.end(); itr != end; ++itr) { if ((itr->m_ChunkX == a_ChunkX) && (itr->m_ChunkZ == a_ChunkZ)) { -- cgit v1.2.3 From 7432d2f74d8d2cc5a002b9c414b006ecb9cdfcfd Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sat, 8 Feb 2014 22:23:38 +0100 Subject: Fixed ChunkStay initialization. --- src/ChunkStay.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ChunkStay.cpp') diff --git a/src/ChunkStay.cpp b/src/ChunkStay.cpp index e86501c4e..cce8d5f4d 100644 --- a/src/ChunkStay.cpp +++ b/src/ChunkStay.cpp @@ -84,9 +84,9 @@ void cChunkStay::Enable(cChunkMap & a_ChunkMap) { ASSERT(m_ChunkMap == NULL); + m_OutstandingChunks = m_Chunks; m_ChunkMap = &a_ChunkMap; a_ChunkMap.AddChunkStay(*this); - m_OutstandingChunks = m_Chunks; } -- cgit v1.2.3 From 9cebc9157cf43ba639227b9d79b980b3613dda1e Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Mon, 10 Feb 2014 22:47:10 +0100 Subject: Rewritten Lua ChunkStay API into a single function, cWorld:ChunkStay(). This fixes problems with indeterminate class object lifespan (Lua-GC) and forgetting to disable it or keep it until ready. --- src/ChunkStay.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/ChunkStay.cpp') diff --git a/src/ChunkStay.cpp b/src/ChunkStay.cpp index cce8d5f4d..6b1d5ee34 100644 --- a/src/ChunkStay.cpp +++ b/src/ChunkStay.cpp @@ -105,7 +105,7 @@ void cChunkStay::Disable(void) -void cChunkStay::ChunkAvailable(int a_ChunkX, int a_ChunkZ) +bool cChunkStay::ChunkAvailable(int a_ChunkX, int a_ChunkZ) { // Check if this is a chunk that we want: bool IsMine = false; @@ -120,15 +120,16 @@ void cChunkStay::ChunkAvailable(int a_ChunkX, int a_ChunkZ) } // for itr - m_OutstandingChunks[] if (!IsMine) { - return; + return false; } // Call the appropriate callbacks: OnChunkAvailable(a_ChunkX, a_ChunkZ); if (m_OutstandingChunks.empty()) { - OnAllChunksAvailable(); + return OnAllChunksAvailable(); } + return false; } -- cgit v1.2.3