diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 5eccce660c..2533e645a1 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -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 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 particleMap; std::set paletteSet; for (int i = 0; i < NPART; i++) { @@ -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(i, storedParts)); + particleMap.insert(std::pair(i, storedParts)); *newSave << tempPart; storedParts++; elementCount[tempPart.type]++; @@ -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::iterator iter = soapList.begin(), end = soapList.end(); iter != end; ++iter) + for (std::map::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::iterator n = soapList.find(newSave->particles[i].tmp); + std::map::iterator n = particleMap.find(newSave->particles[i].tmp); if (n != end) newSave->particles[i].tmp = n->second; else @@ -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::iterator n = soapList.find(newSave->particles[i].tmp2); + std::map::iterator n = particleMap.find(newSave->particles[i].tmp2); if (n != end) newSave->particles[i].tmp2 = n->second; else