diff --git a/src/client/GameSave.cpp b/src/client/GameSave.cpp index 00b8066be..27196e45a 100644 --- a/src/client/GameSave.cpp +++ b/src/client/GameSave.cpp @@ -4,6 +4,7 @@ #include "simulation/Simulation.h" #include "simulation/ElementClasses.h" #include "common/tpt-compat.h" +#include "common/Version.h" #include "bson/BSON.h" #include "graphics/Renderer.h" #include "Config.h" @@ -641,6 +642,25 @@ void GameSave::readOPS(const std::vector &data) } } + auto paletteRemap = [this, saveVersion = Version(majorVersion, minorVersion)](auto maxVersion, ByteString from, ByteString to) { + if (saveVersion <= maxVersion) + { + auto it = std::find_if(palette.begin(), palette.end(), [&from](auto &item) { + return item.first == from; + }); + if (it != palette.end()) + { + it->first = to; + } + } + }; + paletteRemap(Version(87, 1), "DEFAULT_PT_TUGN", "DEFAULT_PT_TUNG"); + paletteRemap(Version(90, 1), "DEFAULT_PT_REPL", "DEFAULT_PT_RPEL"); + paletteRemap(Version(92, 0), "DEFAULT_PT_E180", "DEFAULT_PT_HEAC"); + paletteRemap(Version(92, 0), "DEFAULT_PT_E181", "DEFAULT_PT_SAWD"); + paletteRemap(Version(92, 0), "DEFAULT_PT_E182", "DEFAULT_PT_POLO"); + paletteRemap(Version(93, 3), "DEFAULT_PT_RAYT", "DEFAULT_PT_LDTC"); + //Read wall and fan data if(wallData) { diff --git a/src/common/Version.h b/src/common/Version.h new file mode 100644 index 000000000..11c1e2846 --- /dev/null +++ b/src/common/Version.h @@ -0,0 +1,32 @@ +#pragma once +#include +#include + +template +struct Version +{ + std::array components; + + template + constexpr Version(Args ...args) : components{ size_t(args)... } + { + } + + constexpr bool operator <(const Version &other) const + { + return std::lexicographical_compare(components.begin(), components.end(), other.components.begin(), other.components.end()); + } + + constexpr bool operator ==(const Version &other) const + { + return std::equal(components.begin(), components.end(), other.components.begin(), other.components.end()); + } + + constexpr bool operator <=(const Version &other) const + { + return *this < other || *this == other; + } +}; + +template +Version(Args ...args) -> Version; diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 5b1d7d508..fcb8e2e9e 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -32,6 +32,10 @@ std::vector Simulation::Load(const GameSave *save, bool includePress } if(save->palette.size()) { + for(int i = 0; i < PT_NUM; i++) + { + partMap[i] = 0; + } for(auto &pi : save->palette) { if (pi.second > 0 && pi.second < PT_NUM) @@ -40,19 +44,17 @@ std::vector Simulation::Load(const GameSave *save, bool includePress for (int i = 0; i < PT_NUM; i++) { if (elements[i].Enabled && elements[i].Identifier == pi.first) + { myId = i; + } } - // if this is a custom element, set the ID to the ID we found when comparing identifiers in the palette map - // set type to 0 if we couldn't find an element with that identifier present when loading, - // unless this is a default element, in which case keep the current ID, because otherwise when an element is renamed it wouldn't show up anymore in older saves - if (myId != 0) + if (myId) { partMap[pi.second] = myId; } - else if (!pi.first.BeginsWith("DEFAULT_PT_")) + else { missingElementTypes.push_back(pi.first); - partMap[pi.second] = myId; } } }