Skip to content

Commit 54472d5

Browse files
authored
refactor: Improve OnAuraApply() logic
1 parent affba64 commit 54472d5

File tree

1 file changed

+73
-79
lines changed

1 file changed

+73
-79
lines changed

src/mod_zone_difficulty_scripts.cpp

Lines changed: 73 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -32,98 +32,92 @@ class mod_zone_difficulty_unitscript : public UnitScript
3232
UNITHOOK_ON_UNIT_ENTER_COMBAT
3333
}) { }
3434

35-
void OnAuraApply(Unit* target, Aura* aura) override
36-
{
37-
if (!sZoneDifficulty->IsEnabled)
38-
return;
39-
40-
if (!sZoneDifficulty->MythicmodeInNormalDungeons && !target->GetMap()->IsRaidOrHeroicDungeon())
41-
return;
42-
43-
if (sZoneDifficulty->IsValidNerfTarget(target))
44-
{
45-
uint32 mapId = target->GetMapId();
46-
bool nerfInDuel = sZoneDifficulty->ShouldNerfInDuels(target);
47-
48-
//Check if the map of the target is subject of a nerf at all OR if the target is subject of a nerf in a duel
49-
if (sZoneDifficulty->ShouldNerfMap(mapId) || nerfInDuel)
50-
{
51-
if (SpellInfo const* spellInfo = aura->GetSpellInfo())
52-
{
53-
// Skip spells not affected by vulnerability (potions)
54-
if (spellInfo->HasAttribute(SPELL_ATTR0_NO_IMMUNITIES))
55-
return;
56-
57-
if (spellInfo->HasAura(SPELL_AURA_SCHOOL_ABSORB))
58-
{
59-
Unit::AuraEffectList const& AuraEffectList = target->GetAuraEffectsByType(SPELL_AURA_SCHOOL_ABSORB);
35+
void OnAuraApply(Unit* target, Aura* aura) override
36+
{
37+
if (!sZoneDifficulty->IsEnabled)
38+
return;
6039

61-
for (AuraEffect* eff : AuraEffectList)
62-
{
63-
if ((eff->GetAuraType() != SPELL_AURA_SCHOOL_ABSORB) || (eff->GetSpellInfo()->Id != spellInfo->Id))
64-
continue;
40+
if (!sZoneDifficulty->MythicmodeInNormalDungeons && !target->GetMap()->IsRaidOrHeroicDungeon())
41+
return;
6542

66-
if (sZoneDifficulty->IsDebugInfoEnabled && target)
67-
if (Player* player = target->ToPlayer()) // Pointless check? Perhaps.
68-
ChatHandler(player->GetSession()).PSendSysMessage("Spell: {} ({}) Base Value: {}", spellInfo->SpellName[player->GetSession()->GetSessionDbcLocale()], spellInfo->Id, eff->GetAmount());
43+
if (!sZoneDifficulty->IsValidNerfTarget(target))
44+
return;
6945

70-
int32 absorb = eff->GetAmount();
71-
uint32 phaseMask = target->GetPhaseMask();
72-
int matchingPhase = sZoneDifficulty->GetLowestMatchingPhase(mapId, phaseMask);
73-
int8 mode = sZoneDifficulty->NerfInfo[mapId][matchingPhase].Enabled;
46+
SpellInfo const* spellInfo = aura->GetSpellInfo();
47+
if (!spellInfo || spellInfo->HasAttribute(SPELL_ATTR0_NO_IMMUNITIES))
48+
return;
7449

75-
// Ensure that negative values do not scale to 0
76-
auto scaleAbsorb = [](int32 amount, float pct) -> int32
77-
{
78-
float scaled = amount * pct;
79-
return (scaled < 0) ? static_cast<int32>(std::floor(scaled)) : static_cast<int32>(scaled);
80-
};
50+
if (!spellInfo->HasAura(SPELL_AURA_SCHOOL_ABSORB))
51+
return;
8152

82-
if (matchingPhase != -1)
83-
{
84-
Map* map = target->GetMap();
85-
uint32 instanceId = map->GetInstanceId();
86-
bool isMythicMode = sZoneDifficulty->MythicmodeInstanceData[instanceId];
53+
auto& absorbEffects = target->GetAuraEffectsByType(SPELL_AURA_SCHOOL_ABSORB);
54+
for (AuraEffect* eff : absorbEffects)
55+
{
56+
if (eff->GetAuraType() != SPELL_AURA_SCHOOL_ABSORB || eff->GetSpellInfo()->Id != spellInfo->Id)
57+
continue;
58+
59+
int32 absorb = eff->GetAmount();
60+
uint32 mapId = target->GetMapId();
61+
uint32 instanceId = target->GetMap()->GetInstanceId();
62+
uint32 phaseMask = target->GetPhaseMask();
63+
int phase = sZoneDifficulty->GetLowestMatchingPhase(mapId, phaseMask);
64+
bool isMythic = sZoneDifficulty->MythicmodeInstanceData[instanceId];
65+
bool nerfInDuel = sZoneDifficulty->ShouldNerfInDuels(target);
66+
67+
// Lambda: scalable reduction
68+
auto scaleAbsorb = [](int32 amount, float pct) -> int32 {
69+
float scaled = amount * pct;
70+
return (scaled < 0) ? static_cast<int32>(std::floor(scaled)) : static_cast<int32>(scaled);
71+
};
72+
73+
// Apply duel-based nerf if no valid map phase
74+
if (phase == -1 && nerfInDuel && sZoneDifficulty->NerfInfo[DUEL_INDEX][0].Enabled > 0)
75+
{
76+
absorb = scaleAbsorb(absorb, sZoneDifficulty->NerfInfo[DUEL_INDEX][0].AbsorbNerfPct);
77+
}
78+
else if (phase != -1)
79+
{
80+
int8 mode = sZoneDifficulty->NerfInfo[mapId][phase].Enabled;
8781

88-
if (!isMythicMode && sZoneDifficulty->HasNormalMode(mode))
89-
absorb = scaleAbsorb(absorb, sZoneDifficulty->NerfInfo[mapId][matchingPhase].AbsorbNerfPct);
82+
if (!isMythic && sZoneDifficulty->HasNormalMode(mode))
83+
absorb = scaleAbsorb(absorb, sZoneDifficulty->NerfInfo[mapId][phase].AbsorbNerfPct);
9084

91-
if (isMythicMode && sZoneDifficulty->HasMythicmode(mode))
92-
if (map->IsRaid() || (map->IsHeroic() && map->IsDungeon()))
93-
absorb = scaleAbsorb(absorb, sZoneDifficulty->NerfInfo[mapId][matchingPhase].AbsorbNerfPctHard);
94-
}
95-
else if (sZoneDifficulty->NerfInfo[DUEL_INDEX][0].Enabled > 0 && nerfInDuel)
96-
absorb = scaleAbsorb(absorb, sZoneDifficulty->NerfInfo[DUEL_INDEX][0].AbsorbNerfPct);
85+
if (isMythic && sZoneDifficulty->HasMythicmode(mode))
86+
{
87+
Map* map = target->GetMap();
88+
if (map->IsRaid() || (map->IsHeroic() && map->IsDungeon()))
89+
absorb = scaleAbsorb(absorb, sZoneDifficulty->NerfInfo[mapId][phase].AbsorbNerfPctHard);
90+
}
91+
}
9792

98-
//This check must be last and override duel and map adjustments
99-
if (sZoneDifficulty->SpellNerfOverrides.find(spellInfo->Id) != sZoneDifficulty->SpellNerfOverrides.end())
100-
{
101-
if (sZoneDifficulty->SpellNerfOverrides[spellInfo->Id].find(mapId) != sZoneDifficulty->SpellNerfOverrides[spellInfo->Id].end())
102-
{
103-
// Check if the mode of instance and SpellNerfOverride match
104-
if (sZoneDifficulty->OverrideModeMatches(target->GetMap()->GetInstanceId(), spellInfo->Id, mapId))
105-
absorb = scaleAbsorb(absorb, sZoneDifficulty->SpellNerfOverrides[spellInfo->Id][mapId].NerfPct);
106-
}
107-
else if (sZoneDifficulty->SpellNerfOverrides[spellInfo->Id].find(0) != sZoneDifficulty->SpellNerfOverrides[spellInfo->Id].end())
108-
{
109-
if (sZoneDifficulty->OverrideModeMatches(target->GetMap()->GetInstanceId(), spellInfo->Id, mapId))
110-
absorb = scaleAbsorb(absorb, sZoneDifficulty->SpellNerfOverrides[spellInfo->Id][0].NerfPct);
111-
}
112-
}
93+
// Overrides always apply last
94+
auto const& overrideIt = sZoneDifficulty->SpellNerfOverrides.find(spellInfo->Id);
95+
if (overrideIt != sZoneDifficulty->SpellNerfOverrides.end())
96+
{
97+
auto const& overrideMap = overrideIt->second;
98+
if (sZoneDifficulty->OverrideModeMatches(instanceId, spellInfo->Id, mapId))
99+
{
100+
if (overrideMap.count(mapId))
101+
absorb = scaleAbsorb(absorb, overrideMap.at(mapId).NerfPct);
102+
else if (overrideMap.count(0))
103+
absorb = scaleAbsorb(absorb, overrideMap.at(0).NerfPct);
104+
}
105+
}
113106

114-
eff->SetAmount(absorb);
107+
eff->SetAmount(absorb);
115108

116-
if (sZoneDifficulty->IsDebugInfoEnabled && target)
117-
{
118-
if (Player* player = target->ToPlayer()) // Pointless check? Perhaps.
119-
ChatHandler(player->GetSession()).PSendSysMessage("Spell: {} ({}) Post Nerf Value: {}", spellInfo->SpellName[player->GetSession()->GetSessionDbcLocale()], spellInfo->Id, eff->GetAmount());
120-
}
121-
}
122-
}
123-
}
109+
if (sZoneDifficulty->IsDebugInfoEnabled)
110+
{
111+
if (Player* player = target->ToPlayer())
112+
{
113+
ChatHandler(player->GetSession()).PSendSysMessage(
114+
"Spell: {} ({}) Nerfed Value: {}",
115+
spellInfo->SpellName[player->GetSession()->GetSessionDbcLocale()],
116+
spellInfo->Id, absorb);
124117
}
125118
}
126119
}
120+
}
127121

128122
void ModifyHealReceived(Unit* target, Unit* /*healer*/, uint32& heal, SpellInfo const* spellInfo) override
129123
{

0 commit comments

Comments
 (0)