Skip to content

Commit

Permalink
Adjust SOAP links to non-SOAP particles properly when saving
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob1 committed Apr 1, 2018
1 parent dfc5bd1 commit 07868ba
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/simulation/Simulation.cpp
Expand Up @@ -340,8 +340,9 @@ GameSave * Simulation::Save(int fullX, int fullY, int fullX2, int fullY2, bool i
int storedParts = 0;
int elementCount[PT_NUM];
std::fill(elementCount, elementCount+PT_NUM, 0);
// Map of soap particles loaded into this save, new ID -> old ID
std::map<unsigned int, unsigned int> soapList;
// Map of soap particles loaded into this save, old ID -> new ID
// Now stores all particles, not just SOAP (but still only used for soap)
std::map<unsigned int, unsigned int> particleMap;
std::set<int> paletteSet;
for (int i = 0; i < NPART; i++)
{
Expand All @@ -355,8 +356,7 @@ GameSave * Simulation::Save(int fullX, int fullY, int fullX2, int fullY2, bool i
tempPart.y -= blockY*CELL;
if (elements[tempPart.type].Enabled)
{
if (tempPart.type == PT_SOAP)
soapList.insert(std::pair<unsigned int, unsigned int>(i, storedParts));
particleMap.insert(std::pair<unsigned int, unsigned int>(i, storedParts));
*newSave << tempPart;
storedParts++;
elementCount[tempPart.type]++;
Expand All @@ -372,19 +372,21 @@ GameSave * Simulation::Save(int fullX, int fullY, int fullX2, int fullY2, bool i
}
}

if (storedParts)
{
for (int ID : paletteSet)
newSave->palette.push_back(GameSave::PaletteItem(elements[ID].Identifier, ID));
for (int ID : paletteSet)
newSave->palette.push_back(GameSave::PaletteItem(elements[ID].Identifier, ID));

// fix SOAP links using soapList, a map of new particle ID -> old particle ID
if (storedParts && elementCount[PT_SOAP])
{
// fix SOAP links using particleMap, a map of old particle ID -> new particle ID
// loop through every new particle (saved into the save), and convert .tmp / .tmp2
for (std::map<unsigned int, unsigned int>::iterator iter = soapList.begin(), end = soapList.end(); iter != end; ++iter)
for (std::map<unsigned int, unsigned int>::iterator iter = particleMap.begin(), end = particleMap.end(); iter != end; ++iter)
{
int i = (*iter).second;
if (newSave->particles[i].type != PT_SOAP)
continue;
if ((newSave->particles[i].ctype & 0x2) == 2)
{
std::map<unsigned int, unsigned int>::iterator n = soapList.find(newSave->particles[i].tmp);
std::map<unsigned int, unsigned int>::iterator n = particleMap.find(newSave->particles[i].tmp);
if (n != end)
newSave->particles[i].tmp = n->second;
else
Expand All @@ -395,7 +397,7 @@ GameSave * Simulation::Save(int fullX, int fullY, int fullX2, int fullY2, bool i
}
if ((newSave->particles[i].ctype & 0x4) == 4)
{
std::map<unsigned int, unsigned int>::iterator n = soapList.find(newSave->particles[i].tmp2);
std::map<unsigned int, unsigned int>::iterator n = particleMap.find(newSave->particles[i].tmp2);
if (n != end)
newSave->particles[i].tmp2 = n->second;
else
Expand Down

0 comments on commit 07868ba

Please sign in to comment.