Skip to content

Commit

Permalink
Merge branch 'absorb47' into 'openmw-47'
Browse files Browse the repository at this point in the history
Absorb spells per effect

See merge request OpenMW/openmw!1274
  • Loading branch information
psi29a committed Oct 10, 2021
2 parents b9dc05a + f902efb commit bac679d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
18 changes: 10 additions & 8 deletions apps/openmw/mwmechanics/spellabsorption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,27 @@ namespace MWMechanics
}
};

bool absorbSpell (const std::string& spellId, const MWWorld::Ptr& caster, const MWWorld::Ptr& target)
int getAbsorbChance(const MWWorld::Ptr& caster, const MWWorld::Ptr& target)
{
if (spellId.empty() || target.isEmpty() || caster == target || !target.getClass().isActor())
return false;
if(target.isEmpty() || caster == target || !target.getClass().isActor())
return 0;

CreatureStats& stats = target.getClass().getCreatureStats(target);
if (stats.getMagicEffects().get(ESM::MagicEffect::SpellAbsorption).getMagnitude() <= 0.f)
return false;
return 0;

GetAbsorptionProbability check;
stats.getActiveSpells().visitEffectSources(check);
stats.getSpells().visitEffectSources(check);
if (target.getClass().hasInventoryStore(target))
target.getClass().getInventoryStore(target).visitEffectSources(check);

int chance = check.mProbability * 100;
if (Misc::Rng::roll0to99() >= chance)
return false;
return check.mProbability * 100;
}

void absorbSpell (const std::string& spellId, const MWWorld::Ptr& caster, const MWWorld::Ptr& target)
{
CreatureStats& stats = target.getClass().getCreatureStats(target);

const auto& esmStore = MWBase::Environment::get().getWorld()->getStore();
const ESM::Static* absorbStatic = esmStore.get<ESM::Static>().find("VFX_Absorb");
Expand All @@ -85,7 +88,6 @@ namespace MWMechanics
DynamicStat<float> magicka = stats.getMagicka();
magicka.setCurrent(magicka.getCurrent() + spellCost);
stats.setMagicka(magicka);
return true;
}

}
5 changes: 3 additions & 2 deletions apps/openmw/mwmechanics/spellabsorption.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ namespace MWWorld

namespace MWMechanics
{
// Try to absorb a spell based on the magnitude of every Spell Absorption effect source on the target.
bool absorbSpell(const std::string& spellId, const MWWorld::Ptr& caster, const MWWorld::Ptr& target);
void absorbSpell(const std::string& spellId, const MWWorld::Ptr& caster, const MWWorld::Ptr& target);
// Calculate the chance to absorb a spell based on the magnitude of every Spell Absorption effect source on the target.
int getAbsorbChance(const MWWorld::Ptr& caster, const MWWorld::Ptr& target);
}

#endif
14 changes: 8 additions & 6 deletions apps/openmw/mwmechanics/spellcasting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ namespace MWMechanics
// throughout the iteration of this spell's
// effects, we display a "can't re-cast" message

// Try absorbing the spell. Some handling must still happen for absorbed effects.
bool absorbed = absorbSpell(mId, caster, target);
int absorbChance = getAbsorbChance(caster, target);

int currentEffectIndex = 0;
for (std::vector<ESM::ENAMstruct>::const_iterator effectIt (effects.mList.begin());
Expand All @@ -142,6 +141,13 @@ namespace MWMechanics
}
canCastAnEffect = true;

// Try absorbing the effect
if(absorbChance && Misc::Rng::roll0to99() < absorbChance)
{
absorbSpell(mId, caster, target);
continue;
}

if (!checkEffectTarget(effectIt->mEffectID, target, caster, castByPlayer))
continue;

Expand All @@ -155,10 +161,6 @@ namespace MWMechanics
if (target.getClass().isActor() && target != caster && !caster.isEmpty() && isHarmful)
target.getClass().onHit(target, 0.0f, true, MWWorld::Ptr(), caster, osg::Vec3f(), true);

// Avoid proceeding further for absorbed spells.
if (absorbed)
continue;

// Reflect harmful effects
if (!reflected && reflectEffect(*effectIt, magicEffect, caster, target, reflectedEffects))
continue;
Expand Down

0 comments on commit bac679d

Please sign in to comment.