Merge branch 'topurgeornottopurge' into 'master'

Draft: Improve parity around spell store based effects

See merge request OpenMW/openmw!4827
This commit is contained in:
Evil Eye 2025-08-02 10:54:50 +00:00
commit fcaaf46183
3 changed files with 45 additions and 19 deletions

View File

@ -241,7 +241,25 @@ namespace MWMechanics
// Erase no longer active spells and effects
for (auto spellIt = mSpells.begin(); spellIt != mSpells.end();)
{
if (!spellIt->hasFlag(ESM::ActiveSpells::Flag_Temporary))
if (spellIt->hasFlag(ESM::ActiveSpells::Flag_SpellStore))
{
const ESM::Spell* spell
= MWBase::Environment::get().getESMStore()->get<ESM::Spell>().search(spellIt->mSourceSpellId);
if (spell && ptr.getClass().getCreatureStats(ptr).getSpells().hasSpell(spell))
++spellIt;
else
{
if (spell == nullptr)
Log(Debug::Error) << "Dropping non-existent active effect: " << spellIt->mSourceSpellId;
auto params = *spellIt;
spellIt = mSpells.erase(spellIt);
for (const auto& effect : params.mEffects)
onMagicEffectRemoved(ptr, params, effect);
applyPurges(ptr, &spellIt);
}
continue;
}
else if (!spellIt->hasFlag(ESM::ActiveSpells::Flag_Temporary))
{
++spellIt;
continue;
@ -416,20 +434,7 @@ namespace MWMechanics
if (context.mEraseRemoved)
{
bool remove = false;
if (spellIt->hasFlag(ESM::ActiveSpells::Flag_SpellStore))
{
try
{
auto& spells = ptr.getClass().getCreatureStats(ptr).getSpells();
remove = !spells.hasSpell(spellIt->mSourceSpellId);
}
catch (const std::runtime_error& e)
{
remove = true;
Log(Debug::Error) << "Removing active effect: " << e.what();
}
}
else if (spellIt->hasFlag(ESM::ActiveSpells::Flag_Equipment))
if (spellIt->hasFlag(ESM::ActiveSpells::Flag_Equipment))
{
// Remove effects tied to equipment that has been unequipped
const auto& store = ptr.getClass().getInventoryStore(ptr);

View File

@ -398,6 +398,23 @@ namespace
{ ESM::MagicEffect::BoundShield, "sMagicBoundShieldID" },
{ ESM::MagicEffect::BoundSpear, "sMagicBoundSpearID" },
};
using SpellsPurge = void (MWMechanics::Spells::*)();
void purgePermanent(const MWWorld::Ptr& target, SpellsPurge method, ESM::Spell::SpellType type)
{
MWMechanics::CreatureStats& stats = target.getClass().getCreatureStats(target);
(stats.getSpells().*method)();
stats.getActiveSpells().purge(
[type](const MWMechanics::ActiveSpells::ActiveSpellParams& params) {
if (params.hasFlag(ESM::ActiveSpells::Flag_SpellStore))
{
const ESM::Spell* spell = params.getSpell();
return spell && spell->mData.mType == type;
}
return false;
},
target);
}
}
namespace MWMechanics
@ -412,13 +429,13 @@ namespace MWMechanics
switch (effect.mEffectId)
{
case ESM::MagicEffect::CureCommonDisease:
target.getClass().getCreatureStats(target).getSpells().purgeCommonDisease();
purgePermanent(target, &Spells::purgeCommonDisease, ESM::Spell::ST_Disease);
break;
case ESM::MagicEffect::CureBlightDisease:
target.getClass().getCreatureStats(target).getSpells().purgeBlightDisease();
purgePermanent(target, &Spells::purgeBlightDisease, ESM::Spell::ST_Blight);
break;
case ESM::MagicEffect::RemoveCurse:
target.getClass().getCreatureStats(target).getSpells().purgeCurses();
purgePermanent(target, &Spells::purgeCurses, ESM::Spell::ST_Curse);
break;
case ESM::MagicEffect::CureCorprusDisease:
target.getClass().getCreatureStats(target).getActiveSpells().purgeEffect(

View File

@ -537,7 +537,11 @@ namespace MWScript
return;
MWMechanics::CreatureStats& creatureStats = ptr.getClass().getCreatureStats(ptr);
creatureStats.getSpells().remove(id);
const ESM::Spell* spell = MWBase::Environment::get().getESMStore()->get<ESM::Spell>().find(id);
creatureStats.getSpells().remove(spell);
if (spell->mData.mType == ESM::Spell::ST_Ability || spell->mData.mType == ESM::Spell::ST_Blight
|| spell->mData.mType == ESM::Spell::ST_Curse || spell->mData.mType == ESM::Spell::ST_Disease)
creatureStats.getActiveSpells().removeEffectsBySourceSpellId(ptr, id);
MWBase::WindowManager* wm = MWBase::Environment::get().getWindowManager();