From 360c7f863cbfeed5cee9bc4ee34616605d70375a Mon Sep 17 00:00:00 2001 From: scrawl Date: Sun, 17 Aug 2014 04:58:58 +0200 Subject: [PATCH] Store magic effect Base values in savegame (Fixes #1489) --- apps/openmw/mwmechanics/creaturestats.cpp | 2 ++ apps/openmw/mwmechanics/magiceffects.cpp | 32 +++++++++++++++++++++++ apps/openmw/mwmechanics/magiceffects.hpp | 7 +++++ components/CMakeLists.txt | 2 +- components/esm/creaturestats.cpp | 2 ++ components/esm/creaturestats.hpp | 3 +++ components/esm/magiceffects.cpp | 29 ++++++++++++++++++++ components/esm/magiceffects.hpp | 23 ++++++++++++++++ 8 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 components/esm/magiceffects.cpp create mode 100644 components/esm/magiceffects.hpp diff --git a/apps/openmw/mwmechanics/creaturestats.cpp b/apps/openmw/mwmechanics/creaturestats.cpp index 324cad863..6c0356a04 100644 --- a/apps/openmw/mwmechanics/creaturestats.cpp +++ b/apps/openmw/mwmechanics/creaturestats.cpp @@ -516,6 +516,7 @@ namespace MWMechanics mSpells.writeState(state.mSpells); mActiveSpells.writeState(state.mActiveSpells); mAiSequence.writeState(state.mAiSequence); + mMagicEffects.writeState(state.mMagicEffects); state.mSummonedCreatureMap = mSummonedCreatures; state.mSummonGraveyard = mSummonGraveyard; @@ -564,6 +565,7 @@ namespace MWMechanics mSpells.readState(state.mSpells); mActiveSpells.readState(state.mActiveSpells); mAiSequence.readState(state.mAiSequence); + mMagicEffects.readState(state.mMagicEffects); mSummonedCreatures = state.mSummonedCreatureMap; mSummonGraveyard = state.mSummonGraveyard; diff --git a/apps/openmw/mwmechanics/magiceffects.cpp b/apps/openmw/mwmechanics/magiceffects.cpp index 12fc78e1e..0b19df0a8 100644 --- a/apps/openmw/mwmechanics/magiceffects.cpp +++ b/apps/openmw/mwmechanics/magiceffects.cpp @@ -6,6 +6,7 @@ #include #include +#include namespace MWMechanics { @@ -50,6 +51,16 @@ namespace MWMechanics mBase += diff; } + int EffectParam::getBase() const + { + return mBase; + } + + void EffectParam::setBase(int base) + { + mBase = base; + } + void EffectParam::setModifier(float mod) { mModifier = mod; @@ -182,4 +193,25 @@ namespace MWMechanics return result; } + + void MagicEffects::writeState(ESM::MagicEffects &state) const + { + // Don't need to save Modifiers, they are recalculated every frame anyway. + for (Collection::const_iterator iter (begin()); iter!=end(); ++iter) + { + if (iter->second.getBase() != 0) + { + // Don't worry about mArg, never used by magic effect script instructions + state.mEffects.insert(std::make_pair(iter->first.mId, iter->second.getBase())); + } + } + } + + void MagicEffects::readState(const ESM::MagicEffects &state) + { + for (std::map::const_iterator it = state.mEffects.begin(); it != state.mEffects.end(); ++it) + { + mCollection[EffectKey(it->first)].setBase(it->second); + } + } } diff --git a/apps/openmw/mwmechanics/magiceffects.hpp b/apps/openmw/mwmechanics/magiceffects.hpp index 9833c531f..0a8392dab 100644 --- a/apps/openmw/mwmechanics/magiceffects.hpp +++ b/apps/openmw/mwmechanics/magiceffects.hpp @@ -8,6 +8,8 @@ namespace ESM { struct ENAMstruct; struct EffectList; + + struct MagicEffects; } namespace MWMechanics @@ -43,6 +45,8 @@ namespace MWMechanics /// Change mBase by \a diff void modifyBase(int diff); + void setBase(int base); + int getBase() const; EffectParam(); @@ -90,6 +94,9 @@ namespace MWMechanics Collection::const_iterator end() const { return mCollection.end(); } + void readState (const ESM::MagicEffects& state); + void writeState (ESM::MagicEffects& state) const; + void add (const EffectKey& key, const EffectParam& param); void remove (const EffectKey& key); diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index fff5b1080..96ccc281a 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -42,7 +42,7 @@ add_component_dir (esm loadweap records aipackage effectlist spelllist variant variantimp loadtes3 cellref filter savedgame journalentry queststate locals globalscript player objectstate cellid cellstate globalmap lightstate inventorystate containerstate npcstate creaturestate dialoguestate statstate npcstats creaturestats weatherstate quickkeys fogstate spellstate activespells creaturelevliststate doorstate projectilestate - aisequence + aisequence magiceffects ) add_component_dir (esmterrain diff --git a/components/esm/creaturestats.cpp b/components/esm/creaturestats.cpp index 5326b7f77..21803e02f 100644 --- a/components/esm/creaturestats.cpp +++ b/components/esm/creaturestats.cpp @@ -86,6 +86,7 @@ void ESM::CreatureStats::load (ESMReader &esm) mSpells.load(esm); mActiveSpells.load(esm); mAiSequence.load(esm); + mMagicEffects.load(esm); while (esm.isNextSub("SUMM")) { @@ -196,6 +197,7 @@ void ESM::CreatureStats::save (ESMWriter &esm) const mSpells.save(esm); mActiveSpells.save(esm); mAiSequence.save(esm); + mMagicEffects.save(esm); for (std::map::const_iterator it = mSummonedCreatureMap.begin(); it != mSummonedCreatureMap.end(); ++it) { diff --git a/components/esm/creaturestats.hpp b/components/esm/creaturestats.hpp index 610be0246..8f4d4df7b 100644 --- a/components/esm/creaturestats.hpp +++ b/components/esm/creaturestats.hpp @@ -11,6 +11,7 @@ #include "spellstate.hpp" #include "activespells.hpp" +#include "magiceffects.hpp" #include "aisequence.hpp" namespace ESM @@ -24,6 +25,8 @@ namespace ESM StatState mAttributes[8]; StatState mDynamic[3]; + MagicEffects mMagicEffects; + AiSequence::AiSequence mAiSequence; bool mHasAiSettings; diff --git a/components/esm/magiceffects.cpp b/components/esm/magiceffects.cpp new file mode 100644 index 000000000..898e7e4b1 --- /dev/null +++ b/components/esm/magiceffects.cpp @@ -0,0 +1,29 @@ +#include "magiceffects.hpp" + +#include "esmwriter.hpp" +#include "esmreader.hpp" + +namespace ESM +{ + +void MagicEffects::save(ESMWriter &esm) const +{ + for (std::map::const_iterator it = mEffects.begin(); it != mEffects.end(); ++it) + { + esm.writeHNT("EFID", it->first); + esm.writeHNT("BASE", it->second); + } +} + +void MagicEffects::load(ESMReader &esm) +{ + while (esm.isNextSub("EFID")) + { + int id, base; + esm.getHT(id); + esm.getHNT(base, "BASE"); + mEffects.insert(std::make_pair(id, base)); + } +} + +} diff --git a/components/esm/magiceffects.hpp b/components/esm/magiceffects.hpp new file mode 100644 index 000000000..2a6052caa --- /dev/null +++ b/components/esm/magiceffects.hpp @@ -0,0 +1,23 @@ +#ifndef COMPONENTS_ESM_MAGICEFFECTS_H +#define COMPONENTS_ESM_MAGICEFFECTS_H + +#include + +namespace ESM +{ + class ESMReader; + class ESMWriter; + + // format 0, saved games only + struct MagicEffects + { + // + std::map mEffects; + + void load (ESMReader &esm); + void save (ESMWriter &esm) const; + }; + +} + +#endif