From cc4362211c9ec5cd22f97cc354a1c165c068fe91 Mon Sep 17 00:00:00 2001 From: AlexVinS Date: Tue, 6 Sep 2016 11:03:36 +0300 Subject: [PATCH] Extracted MagicMirror handling --- lib/spells/CDefaultSpellMechanics.cpp | 57 ++++++++++++++------------- lib/spells/CDefaultSpellMechanics.h | 2 + 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/lib/spells/CDefaultSpellMechanics.cpp b/lib/spells/CDefaultSpellMechanics.cpp index ac745dc60..22fc16394 100644 --- a/lib/spells/CDefaultSpellMechanics.cpp +++ b/lib/spells/CDefaultSpellMechanics.cpp @@ -263,35 +263,9 @@ void DefaultSpellMechanics::castNormal(const SpellCastEnvironment * env, const B logGlobal->debugStream() << "will affect: " << ctx.attackedCres.size() << " stacks"; - - //checking if creatures resist handleResistance(env, ctx.attackedCres, ctx.sc); - //reflection is applied only to negative spells - //if it is actual spell and can be reflected to single target, no recurrence - const bool tryMagicMirror = owner->isNegative() && owner->level && owner->getLevelInfo(0).range == "0"; - if(tryMagicMirror) - { - for(auto s : ctx.attackedCres) - { - const int mirrorChance = (s)->valOfBonuses(Bonus::MAGIC_MIRROR); - if(env->getRandomGenerator().nextInt(99) < mirrorChance) - reflected.push_back(s); - } - - vstd::erase_if(ctx.attackedCres, [&reflected](const CStack * s) - { - return vstd::contains(reflected, s); - }); - - for(auto s : reflected) - { - BattleSpellCast::CustomEffect effect; - effect.effect = 3; - effect.stack = s->ID; - ctx.sc.customEffects.push_back(effect); - } - } + handleMagicMirror(env, ctx, reflected); applyBattleEffects(env, parameters, ctx); @@ -764,6 +738,35 @@ void DefaultSpellMechanics::handleImmunities(const CBattleInfoCallback * cb, con vstd::erase_if(stacks, predicate); } +void DefaultSpellMechanics::handleMagicMirror(const SpellCastEnvironment * env, SpellCastContext & ctx, std::vector & reflected) const +{ + //reflection is applied only to negative spells + //if it is actual spell and can be reflected to single target, no recurrence + const bool tryMagicMirror = owner->isNegative() && owner->level && owner->getLevelInfo(0).range == "0"; + if(tryMagicMirror) + { + for(auto s : ctx.attackedCres) + { + const int mirrorChance = (s)->valOfBonuses(Bonus::MAGIC_MIRROR); + if(env->getRandomGenerator().nextInt(99) < mirrorChance) + reflected.push_back(s); + } + + vstd::erase_if(ctx.attackedCres, [&reflected](const CStack * s) + { + return vstd::contains(reflected, s); + }); + + for(auto s : reflected) + { + BattleSpellCast::CustomEffect effect; + effect.effect = 3; + effect.stack = s->ID; + ctx.sc.customEffects.push_back(effect); + } + } +} + void DefaultSpellMechanics::handleResistance(const SpellCastEnvironment * env, std::vector& attackedCres, BattleSpellCast& sc) const { //checking if creatures resist diff --git a/lib/spells/CDefaultSpellMechanics.h b/lib/spells/CDefaultSpellMechanics.h index e856d75ea..eec4700a5 100644 --- a/lib/spells/CDefaultSpellMechanics.h +++ b/lib/spells/CDefaultSpellMechanics.h @@ -67,7 +67,9 @@ protected: private: void castNormal(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters, std::vector & reflected) const; void castMagicMirror(const SpellCastEnvironment * env, const BattleSpellCastParameters & parameters) const; + void handleImmunities(const CBattleInfoCallback * cb, const SpellTargetingContext & ctx, std::vector & stacks) const; + void handleMagicMirror(const SpellCastEnvironment * env, SpellCastContext & ctx, std::vector & reflected) const; void handleResistance(const SpellCastEnvironment * env, std::vector & attackedCres, BattleSpellCast & sc) const; friend class SpellCastContext;