diff options
author | Shauren <shauren.trinity@gmail.com> | 2025-09-06 12:28:12 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2025-09-06 13:19:38 +0200 |
commit | 03d072da463769ff02090e08d12b4da3f6a49222 (patch) | |
tree | 3b67edcd4c0e7df18de425489f688eb621a32235 /src/server/game/Spells/Spell.h | |
parent | 2e3f3fda3fc533daa4064739b633dbb28f5115d3 (diff) |
Core/Spells: Simplify SortTargetsWithPriorityRules
* Remove manul weight assignment
* Removed std::vector alloc
Diffstat (limited to 'src/server/game/Spells/Spell.h')
-rw-r--r-- | src/server/game/Spells/Spell.h | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/src/server/game/Spells/Spell.h b/src/server/game/Spells/Spell.h index d6f968420d8..8555151235b 100644 --- a/src/server/game/Spells/Spell.h +++ b/src/server/game/Spells/Spell.h @@ -18,6 +18,7 @@ #ifndef __SPELL_H #define __SPELL_H +#include "Concepts.h" #include "ConditionMgr.h" #include "DBCEnums.h" #include "Duration.h" @@ -27,6 +28,7 @@ #include "Position.h" #include "SharedDefines.h" #include "SpellDefines.h" +#include "Types.h" #include "UniqueTrackablePtr.h" #include <memory> @@ -1055,15 +1057,35 @@ namespace Trinity TC_GAME_API void SelectRandomInjuredTargets(std::list<WorldObject*>& targets, size_t maxTargets, bool prioritizePlayers, Unit const* prioritizeGroupMembersOf = nullptr); - struct PriorityRules + struct TargetPriorityRule { - int32 weight; - std::function<bool(Unit*)> condition; + template <typename Func> requires (!std::same_as<Func, TargetPriorityRule>) + TargetPriorityRule(Func&& func) : Rule([func = std::forward<Func>(func)]<typename T = WorldObject /*template to avoid Object.h dependency*/>(T* target) + { + if constexpr (invocable_r<Func, bool, WorldObject*>) + return std::invoke(func, target); + else if constexpr (invocable_r<Func, bool, Unit*>) + return target->IsUnit() && std::invoke(func, target->ToUnit()); + else if constexpr (invocable_r<Func, bool, Player*>) + return target->IsPlayer() && std::invoke(func, target->ToPlayer()); + else + static_assert(dependant_false_v<T>, "Unsupported object type, use WorldObject* as your rule argument"); + }) + { + } + + std::function<bool(WorldObject*)> Rule; }; - inline std::vector<PriorityRules> CreatePriorityRules(std::initializer_list<PriorityRules> rules) { return { rules }; } + TC_GAME_API void SortTargetsWithPriorityRules(std::list<WorldObject*>& targets, size_t maxTargets, std::span<TargetPriorityRule const> rules); + + template <std::size_t N> + inline void SortTargetsWithPriorityRules(std::list<WorldObject*>& targets, size_t maxTargets, std::array<TargetPriorityRule, N> const& rules) + { + static_assert(N <= 31); - TC_GAME_API void SortTargetsWithPriorityRules(std::list<WorldObject*>& targets, size_t maxTargets, std::vector<PriorityRules> const& rules); + SortTargetsWithPriorityRules(targets, maxTargets, std::span(rules)); + } } extern template void Spell::SearchTargets<Trinity::WorldObjectListSearcher<Trinity::WorldObjectSpellAreaTargetCheck>>(Trinity::WorldObjectListSearcher<Trinity::WorldObjectSpellAreaTargetCheck>& searcher, uint32 containerMask, WorldObject* referer, Position const* pos, float radius); |