From dcdba227f70fc6304687063cdf157ce519c116c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Fri, 25 Mar 2022 20:42:01 +0200 Subject: [PATCH] Use vector for mUsedPowers for deterministic order --- apps/openmw/mwmechanics/spells.cpp | 12 +++++++++--- apps/openmw/mwmechanics/spells.hpp | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apps/openmw/mwmechanics/spells.cpp b/apps/openmw/mwmechanics/spells.cpp index 133fc5e931..09390425f6 100644 --- a/apps/openmw/mwmechanics/spells.cpp +++ b/apps/openmw/mwmechanics/spells.cpp @@ -185,13 +185,19 @@ namespace MWMechanics bool Spells::canUsePower(const ESM::Spell* spell) const { - const auto it = mUsedPowers.find(spell); + const auto it = std::find_if(std::begin(mUsedPowers), std::end(mUsedPowers), [&](auto& pair) { return pair.first == spell; }); return it == mUsedPowers.end() || it->second + 24 <= MWBase::Environment::get().getWorld()->getTimeStamp(); } void Spells::usePower(const ESM::Spell* spell) { - mUsedPowers[spell] = MWBase::Environment::get().getWorld()->getTimeStamp(); + // Updates or inserts a new entry with the current timestamp. + const auto it = std::find_if(std::begin(mUsedPowers), std::end(mUsedPowers), [&](auto& pair) { return pair.first == spell; }); + const auto timestamp = MWBase::Environment::get().getWorld()->getTimeStamp(); + if (it == mUsedPowers.end()) + mUsedPowers.emplace_back(spell, timestamp); + else + it->second = timestamp; } void Spells::readState(const ESM::SpellState &state, CreatureStats* creatureStats) @@ -223,7 +229,7 @@ namespace MWMechanics const ESM::Spell* spell = MWBase::Environment::get().getWorld()->getStore().get().search(it->first); if (!spell) continue; - mUsedPowers[spell] = MWWorld::TimeStamp(it->second); + mUsedPowers.emplace_back(spell, MWWorld::TimeStamp(it->second)); } // Permanent effects are used only to keep the custom magnitude of corprus spells effects (after cure too), and only in old saves. Convert data to the new approach. diff --git a/apps/openmw/mwmechanics/spells.hpp b/apps/openmw/mwmechanics/spells.hpp index 29f505d369..89e24c4eb5 100644 --- a/apps/openmw/mwmechanics/spells.hpp +++ b/apps/openmw/mwmechanics/spells.hpp @@ -34,7 +34,7 @@ namespace MWMechanics // Note: this is the spell that's about to be cast, *not* the spell selected in the GUI (which may be different) std::string mSelectedSpell; - std::map mUsedPowers; + std::vector> mUsedPowers; bool hasDisease(const ESM::Spell::SpellType type) const;