Cuberite
A lightweight, fast and extensible game server for Minecraft
ChunkStay.cpp
Go to the documentation of this file.
1 
2 // ChunkStay.cpp
3 
4 // Implements the cChunkStay class representing a base for classes that keep chunks loaded
5 
6 #include "Globals.h"
7 #include "ChunkStay.h"
8 #include "ChunkMap.h"
9 
10 
11 
12 
13 
15  m_ChunkMap(nullptr)
16 {
17 }
18 
19 
20 
21 
22 
24 {
25  Clear();
26 }
27 
28 
29 
30 
31 
33 {
34  ASSERT(m_ChunkMap == nullptr);
35  m_Chunks.clear();
36 }
37 
38 
39 
40 
41 
42 void cChunkStay::Add(int a_ChunkX, int a_ChunkZ)
43 {
44  ASSERT(m_ChunkMap == nullptr);
45 
46  for (cChunkCoordsVector::const_iterator itr = m_Chunks.begin(); itr != m_Chunks.end(); ++itr)
47  {
48  if ((itr->m_ChunkX == a_ChunkX) && (itr->m_ChunkZ == a_ChunkZ))
49  {
50  // Already present
51  return;
52  }
53  } // for itr - Chunks[]
54  m_Chunks.emplace_back(a_ChunkX, a_ChunkZ);
55 }
56 
57 
58 
59 
60 
61 void cChunkStay::Remove(int a_ChunkX, int a_ChunkZ)
62 {
63  ASSERT(m_ChunkMap == nullptr);
64 
65  for (cChunkCoordsVector::iterator itr = m_Chunks.begin(); itr != m_Chunks.end(); ++itr)
66  {
67  if ((itr->m_ChunkX == a_ChunkX) && (itr->m_ChunkZ == a_ChunkZ))
68  {
69  // Found, un-"stay"
70  m_Chunks.erase(itr);
71  return;
72  }
73  } // for itr - m_Chunks[]
74 }
75 
76 
77 
78 
79 
80 void cChunkStay::Enable(cChunkMap & a_ChunkMap)
81 {
82  ASSERT(m_ChunkMap == nullptr);
83 
85  m_ChunkMap = &a_ChunkMap;
86  a_ChunkMap.AddChunkStay(*this);
87 }
88 
89 
90 
91 
92 
94 {
95  ASSERT(m_ChunkMap != nullptr);
96 
97  cChunkMap * ChunkMap = m_ChunkMap;
98  m_ChunkMap = nullptr;
99  ChunkMap->DelChunkStay(*this);
100 }
101 
102 
103 
104 
105 
106 bool cChunkStay::ChunkAvailable(int a_ChunkX, int a_ChunkZ)
107 {
108  // Check if this is a chunk that we want:
109  bool IsMine = false;
110  for (cChunkCoordsVector::iterator itr = m_OutstandingChunks.begin(), end = m_OutstandingChunks.end(); itr != end; ++itr)
111  {
112  if ((itr->m_ChunkX == a_ChunkX) && (itr->m_ChunkZ == a_ChunkZ))
113  {
114  m_OutstandingChunks.erase(itr);
115  IsMine = true;
116  break;
117  }
118  } // for itr - m_OutstandingChunks[]
119  if (!IsMine)
120  {
121  return false;
122  }
123 
124  // Call the appropriate callbacks:
125  OnChunkAvailable(a_ChunkX, a_ChunkZ);
126  if (m_OutstandingChunks.empty())
127  {
128  return OnAllChunksAvailable();
129  }
130  return false;
131 }
132 
133 
134 
135 
#define ASSERT(x)
Definition: Globals.h:276
void AddChunkStay(cChunkStay &a_ChunkStay)
Adds a new cChunkStay descendant to the internal list of ChunkStays; loads its chunks.
Definition: ChunkMap.cpp:1495
void DelChunkStay(cChunkStay &a_ChunkStay)
Removes the specified cChunkStay descendant from the internal list of ChunkStays.
Definition: ChunkMap.cpp:1526
virtual void Disable(void)
Disables the ChunkStay, the chunks are released and the ChunkStay object can be edited with Add() and...
Definition: ChunkStay.cpp:93
void Clear(void)
Clears all the chunks that have been added.
Definition: ChunkStay.cpp:32
virtual bool OnAllChunksAvailable(void)=0
Called once all of the contained chunks are available.
void Remove(int a_ChunkX, int a_ChunkZ)
Releases the chunk so that it's no longer locked from unloading.
Definition: ChunkStay.cpp:61
void Add(int a_ChunkX, int a_ChunkZ)
Adds a chunk to be locked from unloading.
Definition: ChunkStay.cpp:42
void Enable(cChunkMap &a_ChunkMap)
Enables the ChunkStay on the specified chunkmap, causing it to load and generate chunks.
Definition: ChunkStay.cpp:80
bool ChunkAvailable(int a_ChunkX, int a_ChunkZ)
Called by cChunkMap when a chunk is available, checks m_NumLoaded and triggers the appropriate callba...
Definition: ChunkStay.cpp:106
cChunkStay(void)
Definition: ChunkStay.cpp:14
cChunkCoordsVector m_Chunks
The list of chunks to lock from unloading.
Definition: ChunkStay.h:85
virtual void OnChunkAvailable(int a_ChunkX, int a_ChunkZ)=0
Called when a specific chunk become available.
cChunkMap * m_ChunkMap
The chunkmap where the object is enabled.
Definition: ChunkStay.h:82
virtual ~cChunkStay()
Deletes the object.
Definition: ChunkStay.cpp:23
cChunkCoordsVector m_OutstandingChunks
The chunks that still need loading.
Definition: ChunkStay.h:88