Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save changed maps every 5 minutes #5557

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ cMap::cMap(unsigned int a_ID, cWorld * a_World):
m_Scale(3),
m_CenterX(0),
m_CenterZ(0),
m_Dirty(false),
m_World(a_World),
m_Name(fmt::format(FMT_STRING("map_{}"), m_ID))
{
Expand All @@ -40,6 +41,7 @@ cMap::cMap(unsigned int a_ID, int a_CenterX, int a_CenterZ, cWorld * a_World, un
m_Scale(a_Scale),
m_CenterX(a_CenterX),
m_CenterZ(a_CenterZ),
m_Dirty(true),
m_World(a_World),
m_Name(fmt::format(FMT_STRING("map_{}"), m_ID))
{
Expand Down Expand Up @@ -223,7 +225,13 @@ bool cMap::SetPixel(unsigned int a_X, unsigned int a_Z, cMap::ColorID a_Data)
{
if ((a_X < m_Width) && (a_Z < m_Height))
{
m_Data[a_Z * m_Width + a_X] = a_Data;
auto index = a_Z * m_Width + a_X;

if (m_Data[index] != a_Data)
{
m_Data[index] = a_Data;
m_Dirty = true;
}

return true;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Map.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ class cMap
int m_CenterX;
int m_CenterZ;

bool m_Dirty;

/** Column-major array of colours */
cColorList m_Data;

Expand All @@ -196,6 +198,7 @@ class cMap

AString m_Name;

friend class cMapManager;
friend class cMapSerializer;

}; // tolua_export
Expand Down
37 changes: 31 additions & 6 deletions src/MapManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@



cMapManager::cMapManager(cWorld * a_World)
: m_World(a_World)
// 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);
}
Expand Down Expand Up @@ -49,6 +57,16 @@ void cMapManager::TickMaps()
{
Map.Tick();
}

if (m_TicksUntilNextSave == 0)
{
m_TicksUntilNextSave = MAP_DATA_SAVE_INTERVAL;
SaveMapData();
}
else
{
m_TicksUntilNextSave--;
}
}


Expand Down Expand Up @@ -149,11 +167,18 @@ void cMapManager::SaveMapData(void)
{
cMap & Map = *it;

cMapSerializer Serializer(m_World->GetDataPath(), &Map);

if (!Serializer.Save())
if (Map.m_Dirty)
{
LOGWARN("Could not save map #%i", Map.GetID());
cMapSerializer Serializer(m_World->GetDataPath(), &Map);

if (Serializer.Save())
{
Map.m_Dirty = false;
}
else
{
LOGWARN("Could not save map #%i", Map.GetID());
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/MapManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class cMapManager

cWorld * m_World;

/** How long till the map data will be saved
Default save interval is #defined in MAP_DATA_SAVE_INTERVAL */
unsigned int m_TicksUntilNextSave;

}; // tolua_export


Expand Down