Core/Spells: Add a helper function to sort spell targets based on custom scripted sorting rules

This commit is contained in:
Telegrill
2025-09-06 00:40:47 +02:00
committed by Shauren
parent 52cb4cf9f1
commit 2e3f3fda3f
5 changed files with 89 additions and 43 deletions

View File

@@ -753,7 +753,6 @@ class spell_dru_efflorescence_heal : public SpellScript
{
void FilterTargets(std::list<WorldObject*>& targets) const
{
// Efflorescence became a smart heal which prioritizes players and their pets in their group before any unit outside their group.
Trinity::SelectRandomInjuredTargets(targets, 3, true, GetCaster());
}
@@ -2542,7 +2541,7 @@ class spell_dru_yseras_gift : public AuraScript
// 145110 - Ysera's Gift (heal)
class spell_dru_yseras_gift_group_heal : public SpellScript
{
void SelectTargets(std::list<WorldObject*>& targets)
static void SelectTargets(SpellScript const&, std::list<WorldObject*>& targets)
{
Trinity::SelectRandomInjuredTargets(targets, 1, true);
}

View File

@@ -2115,28 +2115,15 @@ class spell_pri_power_word_radiance : public SpellScript
void FilterTargets(std::list<WorldObject*>& targets)
{
Unit* caster = GetCaster();
Unit* explTarget = GetExplTargetUnit();
// we must add one since explicit target is always chosen.
uint32 maxTargets = GetEffectInfo(EFFECT_2).CalcValue(GetCaster()) + 1;
if (targets.size() > maxTargets)
{
// priority is: a) no Atonement b) injured c) anything else (excluding explicit target which is always added).
targets.sort([this, explTarget](WorldObject* lhs, WorldObject* rhs)
{
if (lhs == explTarget) // explTarget > anything: always true
return true;
if (rhs == explTarget) // anything > explTarget: always false
return false;
SortTargetsWithPriorityRules(targets, maxTargets, GetRadianceRules(caster, explTarget));
return MakeSortTuple(lhs) > MakeSortTuple(rhs);
});
targets.resize(maxTargets);
}
for (WorldObject* target : targets)
for (WorldObject const* target : targets)
{
if (target == explTarget)
continue;
@@ -2152,32 +2139,24 @@ class spell_pri_power_word_radiance : public SpellScript
GetHitUnit()->SendPlaySpellVisual(target, SPELL_VISUAL_PRIEST_POWER_WORD_RADIANCE, 0, 0, 70.0f);
}
static std::vector<Trinity::PriorityRules> GetRadianceRules(Unit const* caster, Unit const* explTarget)
{
return Trinity::CreatePriorityRules
({
{ .weight = 1, .condition = [caster](Unit const* target) { return target->IsInRaidWith(caster); }},
{ .weight = 2, .condition = [](Unit const* target) { return target->IsPlayer() || (target->IsCreature() && target->ToCreature()->IsTreatedAsRaidUnit()); }},
{ .weight = 4, .condition = [](Unit const* target) { return !target->IsFullHealth(); }},
{ .weight = 8, .condition = [caster](Unit const* target) { return !target->HasAura(SPELL_PRIEST_ATONEMENT_EFFECT, caster->GetGUID()); }},
{ .weight = 16, .condition = [explTarget](Unit const* target) { return target == explTarget; }}
});
}
void Register() override
{
OnObjectAreaTargetSelect += SpellObjectAreaTargetSelectFn(spell_pri_power_word_radiance::FilterTargets, EFFECT_1, TARGET_UNIT_DEST_AREA_ALLY);
OnEffectHitTarget += SpellEffectFn(spell_pri_power_word_radiance::HandleEffectHitTarget, EFFECT_0, SPELL_EFFECT_DUMMY);
}
private:
std::tuple<bool, bool> MakeSortTuple(WorldObject* obj) const
{
return std::make_tuple(IsUnitWithNoAtonement(obj), IsUnitInjured(obj));
}
// Returns true if obj is a unit but has no atonement
bool IsUnitWithNoAtonement(WorldObject* obj) const
{
Unit* unit = obj->ToUnit();
return unit && !unit->HasAura(SPELL_PRIEST_ATONEMENT_EFFECT, GetCaster()->GetGUID());
}
// Returns true if obj is a unit and is injured
static bool IsUnitInjured(WorldObject* obj)
{
Unit* unit = obj->ToUnit();
return unit && !unit->IsFullHealth();
}
std::vector<ObjectGuid> _visualTargets;
};

View File

@@ -297,11 +297,6 @@ class spell_sha_ancestral_guidance : public AuraScript
// 114911 - Ancestral Guidance Heal
class spell_sha_ancestral_guidance_heal : public SpellScript
{
bool Validate(SpellInfo const* /*spellInfo*/) override
{
return ValidateSpellInfo({ SPELL_SHAMAN_ANCESTRAL_GUIDANCE });
}
static void ResizeTargets(SpellScript const&, std::list<WorldObject*>& targets)
{
Trinity::SelectRandomInjuredTargets(targets, 3, true);