1
0
cuberite-2a/src/MapManager.cpp

165 lines
2.1 KiB
C++
Raw Normal View History

2014-02-20 09:38:37 -05:00
// MapManager.cpp
#include "Globals.h"
#include "MapManager.h"
#include "World.h"
#include "WorldStorage/MapSerializer.h"
cMapManager::cMapManager(cWorld * a_World)
: m_World(a_World)
{
2014-10-20 16:55:07 -04:00
ASSERT(m_World != nullptr);
2014-02-20 09:38:37 -05:00
}
bool cMapManager::DoWithMap(UInt32 a_ID, cMapCallback a_Callback)
2014-02-20 09:38:37 -05:00
{
cCSLock Lock(m_CS);
cMap * Map = GetMapData(a_ID);
2014-10-20 16:55:07 -04:00
if (Map == nullptr)
2014-02-20 09:38:37 -05:00
{
return false;
}
else
{
a_Callback(*Map);
2014-02-20 09:38:37 -05:00
return true;
}
}
2015-06-30 10:50:15 -04:00
void cMapManager::TickMaps()
2014-02-20 09:38:37 -05:00
{
cCSLock Lock(m_CS);
2015-06-30 10:50:15 -04:00
for (auto & Map : m_MapData)
2014-02-20 09:38:37 -05:00
{
2015-06-30 10:50:15 -04:00
Map.Tick();
}
2014-02-20 09:38:37 -05:00
}
cMap * cMapManager::GetMapData(unsigned int a_ID)
{
if (a_ID < m_MapData.size())
{
return &m_MapData[a_ID];
}
else
{
2014-10-20 16:55:07 -04:00
return nullptr;
2014-02-20 09:38:37 -05:00
}
}
cMap * cMapManager::CreateMap(int a_CenterX, int a_CenterY, unsigned int a_Scale)
2014-02-20 09:38:37 -05:00
{
cCSLock Lock(m_CS);
if (m_MapData.size() >= 65536)
{
LOGWARN("Could not craft map - Too many maps in use");
2014-10-20 16:55:07 -04:00
return nullptr;
2014-02-20 09:38:37 -05:00
}
2015-05-24 07:56:56 -04:00
cMap Map(static_cast<unsigned>(m_MapData.size()), a_CenterX, a_CenterY, m_World, a_Scale);
2014-02-20 09:38:37 -05:00
m_MapData.push_back(Map);
return &m_MapData[Map.GetID()];
}
void cMapManager::LoadMapData(void)
{
cCSLock Lock(m_CS);
cIDCountSerializer IDSerializer(m_World->GetDataPath());
2014-02-20 09:38:37 -05:00
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);
2014-02-20 09:38:37 -05:00
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());
2014-02-20 09:38:37 -05:00
2015-05-24 07:56:56 -04:00
IDSerializer.SetMapCount(static_cast<unsigned>(m_MapData.size()));
2014-02-20 09:38:37 -05:00
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;
cMapSerializer Serializer(m_World->GetDataPath(), &Map);
2014-02-20 09:38:37 -05:00
if (!Serializer.Save())
{
LOGWARN("Could not save map #%i", Map.GetID());
}
}
}