Skip to content

Commit

Permalink
Scripts/Spells: Use Spell::GetPowerTypeCostAmount where possible inst…
Browse files Browse the repository at this point in the history
…ead of iterating Spell::GetPowerCost
  • Loading branch information
Shauren committed Jan 20, 2025
1 parent 0e36fd9 commit 26376d8
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 29 deletions.
3 changes: 1 addition & 2 deletions src/server/game/Spells/Auras/SpellAuraEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1389,8 +1389,7 @@ bool AuraEffect::CheckEffectProc(AuraApplication* aurApp, ProcEventInfo& eventIn

// Costs Check
std::vector<SpellPowerCost> const& costs = eventInfo.GetProcSpell()->GetPowerCost();
auto m = std::find_if(costs.begin(), costs.end(), [](SpellPowerCost const& cost) { return cost.Amount > 0; });
if (m == costs.end())
if (std::ranges::none_of(costs, [](SpellPowerCost const& cost) { return cost.Amount > 0; }))
return false;
break;
}
Expand Down
12 changes: 4 additions & 8 deletions src/server/game/Spells/Spell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4771,7 +4771,7 @@ void Spell::SendSpellStart()

if ((m_caster->GetTypeId() == TYPEID_PLAYER ||
(m_caster->GetTypeId() == TYPEID_UNIT && m_caster->ToCreature()->IsPet()))
&& std::find_if(m_powerCost.begin(), m_powerCost.end(), [](SpellPowerCost const& cost) { return cost.Power != POWER_HEALTH; }) != m_powerCost.end())
&& std::ranges::any_of(m_powerCost, [](SpellPowerCost const& cost) { return cost.Power != POWER_HEALTH; }))
castFlags |= CAST_FLAG_POWER_LEFT_SELF;

if (HasPowerTypeCost(POWER_RUNES))
Expand Down Expand Up @@ -4870,7 +4870,7 @@ void Spell::SendSpellGo()

if ((m_caster->GetTypeId() == TYPEID_PLAYER ||
(m_caster->GetTypeId() == TYPEID_UNIT && m_caster->ToCreature()->IsPet()))
&& std::find_if(m_powerCost.begin(), m_powerCost.end(), [](SpellPowerCost const& cost) { return cost.Power != POWER_HEALTH; }) != m_powerCost.end())
&& std::ranges::any_of(m_powerCost, [](SpellPowerCost const& cost) { return cost.Power != POWER_HEALTH; }))
castFlags |= CAST_FLAG_POWER_LEFT_SELF;

if ((m_caster->GetTypeId() == TYPEID_PLAYER)
Expand Down Expand Up @@ -8150,11 +8150,7 @@ bool Spell::HasPowerTypeCost(Powers power) const

Optional<int32> Spell::GetPowerTypeCostAmount(Powers power) const
{
auto itr = std::find_if(m_powerCost.cbegin(), m_powerCost.cend(), [power](SpellPowerCost const& cost)
{
return cost.Power == power;
});

auto itr = std::ranges::find(m_powerCost, power, &SpellPowerCost::Power);
if (itr == m_powerCost.cend())
return { };

Expand Down Expand Up @@ -9590,7 +9586,7 @@ void SelectRandomInjuredTargets(std::list<WorldObject*>& targets, size_t maxTarg
tempTargets.resize(targets.size());

// categorize each target
std::transform(targets.begin(), targets.end(), tempTargets.begin(), [&](WorldObject* target)
std::ranges::transform(targets, tempTargets.begin(), [&](WorldObject* target)
{
int32 negativePoints = 0;
if (prioritizeGroupMembersOf && (!target->IsUnit() || target->ToUnit()->IsInRaidWith(prioritizeGroupMembersOf)))
Expand Down
3 changes: 1 addition & 2 deletions src/server/game/Spells/SpellMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,7 @@ bool SpellMgr::CanSpellTriggerProcOnEvent(SpellProcEntry const& procEntry, ProcE
return false;

std::vector<SpellPowerCost> const& costs = eventInfo.GetProcSpell()->GetPowerCost();
auto m = std::find_if(costs.begin(), costs.end(), [](SpellPowerCost const& cost) { return cost.Amount > 0; });
if (m == costs.end())
if (std::ranges::none_of(costs, [](SpellPowerCost const& cost) { return cost.Amount > 0; }))
return false;
}

Expand Down
7 changes: 3 additions & 4 deletions src/server/scripts/Spells/spell_druid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1846,12 +1846,11 @@ class spell_dru_t3_8p_bonus : public AuraScript
return;

Unit* caster = eventInfo.GetActor();
std::vector<SpellPowerCost> const& costs = spell->GetPowerCost();
auto m = std::find_if(costs.begin(), costs.end(), [](SpellPowerCost const& cost) { return cost.Power == POWER_MANA; });
if (m == costs.end())
Optional<int32> manaCost = spell->GetPowerTypeCostAmount(POWER_MANA);
if (!manaCost)
return;

int32 amount = CalculatePct(m->Amount, aurEff->GetAmount());
int32 amount = CalculatePct(*manaCost, aurEff->GetAmount());
CastSpellExtraArgs args(aurEff);
args.AddSpellBP0(amount);
caster->CastSpell(nullptr, SPELL_DRUID_EXHILARATE, args);
Expand Down
6 changes: 1 addition & 5 deletions src/server/scripts/Spells/spell_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1658,12 +1658,8 @@ class spell_item_pendant_of_the_violet_eye : public AuraScript
bool CheckProc(ProcEventInfo& eventInfo)
{
if (Spell const* spell = eventInfo.GetProcSpell())
{
std::vector<SpellPowerCost> const& costs = spell->GetPowerCost();
auto m = std::find_if(costs.begin(), costs.end(), [](SpellPowerCost const& cost) { return cost.Power == POWER_MANA && cost.Amount > 0; });
if (m != costs.end())
if (spell->GetPowerTypeCostAmount(POWER_MANA) > 0)
return true;
}

return false;
}
Expand Down
6 changes: 2 additions & 4 deletions src/server/scripts/Spells/spell_rogue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1031,10 +1031,8 @@ class spell_rog_sinister_strike : public SpellScript
damagePerCombo += t5->GetAmount();

int32 finalDamage = damagePerCombo;
std::vector<SpellPowerCost> const& costs = GetSpell()->GetPowerCost();
auto c = std::find_if(costs.begin(), costs.end(), [](SpellPowerCost const& cost) { return cost.Power == POWER_COMBO_POINTS; });
if (c != costs.end())
finalDamage *= c->Amount;
if (Optional<int32> comboPointCost = GetSpell()->GetPowerTypeCostAmount(POWER_COMBO_POINTS))
finalDamage *= *comboPointCost;

SetHitDamage(finalDamage);
}
Expand Down
6 changes: 2 additions & 4 deletions src/server/scripts/Spells/spell_shaman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1012,11 +1012,9 @@ class spell_sha_item_mana_surge : public AuraScript
{
PreventDefaultAction();

std::vector<SpellPowerCost> const& costs = eventInfo.GetProcSpell()->GetPowerCost();
auto m = std::find_if(costs.begin(), costs.end(), [](SpellPowerCost const& cost) { return cost.Power == POWER_MANA; });
if (m != costs.end())
if (Optional<int32> manaCost = eventInfo.GetProcSpell()->GetPowerTypeCostAmount(POWER_MANA))
{
int32 mana = CalculatePct(m->Amount, 35);
int32 mana = CalculatePct(*manaCost, 35);
if (mana > 0)
{
CastSpellExtraArgs args(aurEff);
Expand Down

0 comments on commit 26376d8

Please sign in to comment.