Skip to content

Commit

Permalink
feat(Core/Scripting): Implement Unit hooks to modify damage before ca… (
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyeriah authored Nov 19, 2023
1 parent ed3737b commit d55b675
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/server/game/AI/CoreAI/UnitAI.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,13 @@ class UnitAI
// Called when the unit heals
virtual void HealDone(Unit* /*done_to*/, uint32& /*addhealth*/) {}

// Called during damage calculations
virtual void OnCalculateMeleeDamageReceived(uint32& /*damage*/, Unit* /*attacker*/) {}
virtual void OnCalculateSpellDamageReceived(int32& /*damage*/, Unit* /*attacker*/) {}

// Called during calculation when receiving periodic healing or damage (DoT or HoT)
virtual void OnCalculatePeriodicTickReceived(uint32& /*damage*/, Unit* /*attacker*/) {}

void AttackStartCaster(Unit* victim, float dist);

SpellCastResult DoAddAuraToAllHostilePlayers(uint32 spellid);
Expand Down
10 changes: 10 additions & 0 deletions src/server/game/Entities/Unit/Unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,11 @@ void Unit::CalculateSpellDamageTaken(SpellNonMeleeDamage* damageInfo, int32 dama
// Script Hook For CalculateSpellDamageTaken -- Allow scripts to change the Damage post class mitigation calculations
sScriptMgr->ModifySpellDamageTaken(damageInfo->target, damageInfo->attacker, damage, spellInfo);

if (victim->GetAI())
{
victim->GetAI()->OnCalculateSpellDamageReceived(damage, this);
}

int32 cleanDamage = 0;
if (Unit::IsDamageReducedByArmor(damageSchoolMask, spellInfo))
{
Expand Down Expand Up @@ -1558,6 +1563,11 @@ void Unit::CalculateMeleeDamage(Unit* victim, CalcDamageInfo* damageInfo, Weapon
// Script Hook For CalculateMeleeDamage -- Allow scripts to change the Damage pre class mitigation calculations
sScriptMgr->ModifyMeleeDamage(damageInfo->target, damageInfo->attacker, damage);

if (victim->GetAI())
{
victim->GetAI()->OnCalculateMeleeDamageReceived(damage, this);
}

// Calculate armor reduction
if (IsDamageReducedByArmor((SpellSchoolMask)(damageInfo->damages[i].damageSchoolMask)))
{
Expand Down
15 changes: 15 additions & 0 deletions src/server/game/Spells/Auras/SpellAuraEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6701,6 +6701,11 @@ void AuraEffect::HandlePeriodicDamageAurasTick(Unit* target, Unit* caster) const
// Script Hook For HandlePeriodicDamageAurasTick -- Allow scripts to change the Damage pre class mitigation calculations
sScriptMgr->ModifyPeriodicDamageAurasTick(target, caster, damage, GetSpellInfo());

if (target->GetAI())
{
target->GetAI()->OnCalculatePeriodicTickReceived(damage, caster);
}

if (GetAuraType() == SPELL_AURA_PERIODIC_DAMAGE)
{
// xinef: leave only target depending bonuses, rest is handled in calculate amount
Expand Down Expand Up @@ -6811,6 +6816,11 @@ void AuraEffect::HandlePeriodicHealthLeechAuraTick(Unit* target, Unit* caster) c
// Script Hook For HandlePeriodicHealthLeechAurasTick -- Allow scripts to change the Damage pre class mitigation calculations
sScriptMgr->ModifyPeriodicDamageAurasTick(target, caster, damage, GetSpellInfo());

if (target->GetAI())
{
target->GetAI()->OnCalculatePeriodicTickReceived(damage, caster);
}

if (GetBase()->GetType() == DYNOBJ_AURA_TYPE)
damage = caster->SpellDamageBonusDone(target, GetSpellInfo(), damage, DOT, GetEffIndex(), 0.0f, GetBase()->GetStackAmount());
damage = target->SpellDamageBonusTaken(caster, GetSpellInfo(), damage, DOT, GetBase()->GetStackAmount());
Expand Down Expand Up @@ -7020,6 +7030,11 @@ void AuraEffect::HandlePeriodicHealAurasTick(Unit* target, Unit* caster) const
sScriptMgr->ModifyPeriodicDamageAurasTick(target, caster, heal, GetSpellInfo());
sScriptMgr->ModifyHealReceived(target, caster, heal, GetSpellInfo());

if (target->GetAI())
{
target->GetAI()->OnCalculatePeriodicTickReceived(heal, caster);
}

HealInfo healInfo(caster, target, heal, GetSpellInfo(), GetSpellInfo()->GetSchoolMask());
Unit::CalcHealAbsorb(healInfo);
int32 gain = Unit::DealHeal(caster, target, healInfo.GetHeal());
Expand Down

0 comments on commit d55b675

Please sign in to comment.