Cuberite
A lightweight, fast and extensible game server for Minecraft
MapManager.cpp
Go to the documentation of this file.
1 
2 // MapManager.cpp
3 
4 #include "Globals.h"
5 
6 #include "MapManager.h"
7 
8 #include "World.h"
10 
11 
12 
13 
14 
16  : m_World(a_World)
17 {
18  ASSERT(m_World != nullptr);
19 }
20 
21 
22 
23 
24 
26 {
27  cCSLock Lock(m_CS);
28  cMap * Map = GetMapData(a_ID);
29 
30  if (Map == nullptr)
31  {
32  return false;
33  }
34  else
35  {
36  a_Callback(*Map);
37  return true;
38  }
39 }
40 
41 
42 
43 
44 
46 {
47  cCSLock Lock(m_CS);
48  for (auto & Map : m_MapData)
49  {
50  Map.Tick();
51  }
52 }
53 
54 
55 
56 
57 
58 cMap * cMapManager::GetMapData(unsigned int a_ID)
59 {
60  if (a_ID < m_MapData.size())
61  {
62  return &m_MapData[a_ID];
63  }
64  else
65  {
66  return nullptr;
67  }
68 }
69 
70 
71 
72 
73 
74 cMap * cMapManager::CreateMap(int a_CenterX, int a_CenterY, unsigned int a_Scale)
75 {
76  cCSLock Lock(m_CS);
77 
78  if (m_MapData.size() >= 65536)
79  {
80  LOGWARN("Could not craft map - Too many maps in use");
81  return nullptr;
82  }
83 
84  cMap Map(static_cast<unsigned>(m_MapData.size()), a_CenterX, a_CenterY, m_World, a_Scale);
85 
86  m_MapData.push_back(Map);
87 
88  return &m_MapData[Map.GetID()];
89 }
90 
91 
92 
93 
94 
96 {
97  cCSLock Lock(m_CS);
98 
99  cIDCountSerializer IDSerializer(m_World->GetDataPath());
100 
101  if (!IDSerializer.Load())
102  {
103  return;
104  }
105 
106  unsigned int MapCount = IDSerializer.GetMapCount();
107 
108  m_MapData.clear();
109 
110  for (unsigned int i = 0; i < MapCount; ++i)
111  {
112  cMap Map(i, m_World);
113 
114  cMapSerializer Serializer(m_World->GetDataPath(), &Map);
115 
116  if (!Serializer.Load())
117  {
118  LOGWARN("Could not load map #%i", Map.GetID());
119  }
120 
121  m_MapData.push_back(Map);
122  }
123 }
124 
125 
126 
127 
128 
130 {
131  cCSLock Lock(m_CS);
132 
133  if (m_MapData.empty())
134  {
135  return;
136  }
137 
138  cIDCountSerializer IDSerializer(m_World->GetDataPath());
139 
140  IDSerializer.SetMapCount(static_cast<unsigned>(m_MapData.size()));
141 
142  if (!IDSerializer.Save())
143  {
144  LOGERROR("Could not save idcounts.dat");
145  return;
146  }
147 
148  for (cMapList::iterator it = m_MapData.begin(); it != m_MapData.end(); ++it)
149  {
150  cMap & Map = *it;
151 
152  cMapSerializer Serializer(m_World->GetDataPath(), &Map);
153 
154  if (!Serializer.Save())
155  {
156  LOGWARN("Could not save map #%i", Map.GetID());
157  }
158  }
159 }
160 
161 
162 
163 
164 
unsigned int UInt32
Definition: Globals.h:157
#define ASSERT(x)
Definition: Globals.h:276
void LOGERROR(std::string_view a_Format, const Args &... args)
Definition: LoggerSimple.h:73
#define LOGWARN
Definition: LoggerSimple.h:88
Encapsulates an in-game world map.
Definition: Map.h:83
void LoadMapData(void)
Loads the map data from the disk.
Definition: MapManager.cpp:95
cMapManager(cWorld *a_World)
Definition: MapManager.cpp:15
void TickMaps(void)
Ticks each registered map.
Definition: MapManager.cpp:45
cMap * GetMapData(unsigned int a_ID)
Returns the map with the specified ID, nullptr if out of range.
Definition: MapManager.cpp:58
cWorld * m_World
Definition: MapManager.h:65
cCriticalSection m_CS
Definition: MapManager.h:61
cMapList m_MapData
Definition: MapManager.h:63
cMap * CreateMap(int a_CenterX, int a_CenterY, unsigned int a_Scale=3)
Creates a new map.
Definition: MapManager.cpp:74
bool DoWithMap(UInt32 a_ID, cMapCallback a_Callback)
Calls the callback for the map with the specified ID.
Definition: MapManager.cpp:25
void SaveMapData(void)
Saves the map data to the disk.
Definition: MapManager.cpp:129
RAII for cCriticalSection - locks the CS on creation, unlocks on destruction.
Definition: World.h:53
const AString & GetDataPath(void) const
Returns the data path to the world data.
Definition: World.h:694
Utility class used to serialize maps.
Definition: MapSerializer.h:26
bool Load(void)
Try to load the map.
bool Save(void)
Try to save the map.
Utility class used to serialize item ID counts.
Definition: MapSerializer.h:59
bool Save(void)
Try to save the ID counts.
bool Load(void)
Try to load the ID counts.
void SetMapCount(unsigned int a_MapCount)
Definition: MapSerializer.h:72
unsigned int GetMapCount(void) const
Definition: MapSerializer.h:70