summaryrefslogtreecommitdiffstats
path: root/src/ChunkStay.cpp
diff options
context:
space:
mode:
authormadmaxoft <github@xoft.cz>2014-02-08 21:55:21 +0100
committermadmaxoft <github@xoft.cz>2014-02-08 21:55:21 +0100
commitea71bfa9b645cda80b7d4364c675ebaee8db8353 (patch)
tree8db9fc2cd467e32bebd2aae4aaac2208fee60c2f /src/ChunkStay.cpp
parentMerge pull request #653 from mc-server/RedstoneSimulator (diff)
downloadcuberite-ea71bfa9b645cda80b7d4364c675ebaee8db8353.tar
cuberite-ea71bfa9b645cda80b7d4364c675ebaee8db8353.tar.gz
cuberite-ea71bfa9b645cda80b7d4364c675ebaee8db8353.tar.bz2
cuberite-ea71bfa9b645cda80b7d4364c675ebaee8db8353.tar.lz
cuberite-ea71bfa9b645cda80b7d4364c675ebaee8db8353.tar.xz
cuberite-ea71bfa9b645cda80b7d4364c675ebaee8db8353.tar.zst
cuberite-ea71bfa9b645cda80b7d4364c675ebaee8db8353.zip
Diffstat (limited to '')
-rw-r--r--src/ChunkStay.cpp136
1 files changed, 136 insertions, 0 deletions
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();
+ }
+}
+
+
+
+