From 82ac99ec560e595ebeb33b4e08d1d94cd00b000f Mon Sep 17 00:00:00 2001
From: capostrophic <alexdobrohotov@yandex.ru>
Date: Fri, 2 Aug 2019 21:26:47 +0300
Subject: [PATCH] Apply instant FX in AddSpell immediately (bug #5055)

---
 CHANGELOG.md                             |  1 +
 apps/openmw/mwscript/statsextensions.cpp | 11 +++++++++++
 2 files changed, 12 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 29e310b581..d20afba30b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -105,6 +105,7 @@
     Bug #5038: Enchanting success chance calculations are blatantly wrong
     Bug #5047: # in cell names sets color
     Bug #5050: Invalid spell effects are not handled gracefully
+    Bug #5055: Mark, Recall, Intervention magic effect abilities have no effect when added and removed in the same frame
     Bug #5056: Calling Cast function on player doesn't equip the spell but casts it
     Bug #5060: Magic effect visuals stop when death animation begins instead of when it ends
     Bug #5063: Shape named "Tri Shadow" in creature mesh is visible if it isn't hidden
diff --git a/apps/openmw/mwscript/statsextensions.cpp b/apps/openmw/mwscript/statsextensions.cpp
index aa1da8b475..847fe0bfc2 100644
--- a/apps/openmw/mwscript/statsextensions.cpp
+++ b/apps/openmw/mwscript/statsextensions.cpp
@@ -25,6 +25,7 @@
 #include "../mwmechanics/creaturestats.hpp"
 #include "../mwmechanics/npcstats.hpp"
 #include "../mwmechanics/actorutil.hpp"
+#include "../mwmechanics/spellcasting.hpp"
 
 #include "ref.hpp"
 
@@ -464,6 +465,16 @@ namespace MWScript
                     {
                         // Apply looping particles immediately for constant effects
                         MWBase::Environment::get().getWorld()->applyLoopingParticles(ptr);
+
+                        // The spell may have an instant effect which must be handled immediately.
+                        for (const auto& effect : creatureStats.getSpells().getMagicEffects())
+                        {
+                            if (effect.second.getMagnitude() <= 0)
+                               continue;
+                            MWMechanics::CastSpell cast(ptr, ptr);
+                            if (cast.applyInstantEffect(ptr, ptr, effect.first, effect.second.getMagnitude()))
+                                creatureStats.getSpells().purgeEffect(effect.first.mId);
+                        }
                     }
                 }
         };