1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
// MapManager.cpp
#include "Globals.h"
#include "MapManager.h"
#include "World.h"
#include "WorldStorage/MapSerializer.h"
// 6000 ticks or 5 minutes
#define MAP_DATA_SAVE_INTERVAL 6000
cMapManager::cMapManager(cWorld * a_World) :
m_World(a_World), m_TicksUntilNextSave(MAP_DATA_SAVE_INTERVAL)
{
ASSERT(m_World != nullptr);
}
bool cMapManager::DoWithMap(UInt32 a_ID, cMapCallback a_Callback)
{
cCSLock Lock(m_CS);
cMap * Map = GetMapData(a_ID);
if (Map == nullptr)
{
return false;
}
else
{
a_Callback(*Map);
return true;
}
}
void cMapManager::TickMaps()
{
cCSLock Lock(m_CS);
for (auto & Map : m_MapData)
{
Map.Tick();
}
if (m_TicksUntilNextSave == 0)
{
m_TicksUntilNextSave = MAP_DATA_SAVE_INTERVAL;
SaveMapData();
}
else
{
m_TicksUntilNextSave--;
}
}
cMap * cMapManager::GetMapData(unsigned int a_ID)
{
if (a_ID < m_MapData.size())
{
return &m_MapData[a_ID];
}
else
{
return nullptr;
}
}
cMap * cMapManager::CreateMap(int a_CenterX, int a_CenterY, unsigned int a_Scale)
{
cCSLock Lock(m_CS);
if (m_MapData.size() >= 65536)
{
LOGWARN("Could not craft map - Too many maps in use");
return nullptr;
}
cMap Map(static_cast<unsigned>(m_MapData.size()), a_CenterX, a_CenterY, m_World, a_Scale);
m_MapData.push_back(Map);
return &m_MapData[Map.GetID()];
}
void cMapManager::LoadMapData(void)
{
cCSLock Lock(m_CS);
cIDCountSerializer IDSerializer(m_World->GetDataPath());
if (!IDSerializer.Load())
{
return;
}
unsigned int MapCount = IDSerializer.GetMapCount();
m_MapData.clear();
for (unsigned int i = 0; i < MapCount; ++i)
{
cMap Map(i, m_World);
cMapSerializer Serializer(m_World->GetDataPath(), &Map);
if (!Serializer.Load())
{
LOGWARN("Could not load map #%i", Map.GetID());
}
m_MapData.push_back(Map);
}
}
void cMapManager::SaveMapData(void)
{
cCSLock Lock(m_CS);
if (m_MapData.empty())
{
return;
}
cIDCountSerializer IDSerializer(m_World->GetDataPath());
IDSerializer.SetMapCount(static_cast<unsigned>(m_MapData.size()));
if (!IDSerializer.Save())
{
LOGERROR("Could not save idcounts.dat");
return;
}
for (cMapList::iterator it = m_MapData.begin(); it != m_MapData.end(); ++it)
{
cMap & Map = *it;
if (Map.m_Dirty)
{
cMapSerializer Serializer(m_World->GetDataPath(), &Map);
if (Serializer.Save())
{
Map.m_Dirty = false;
}
else
{
LOGWARN("Could not save map #%i", Map.GetID());
}
}
}
}
|