Skip to content

Commit 1d353b6

Browse files
OvahlordXenphis
authored andcommitted
Core/Units: moved health and power ordering predicates from Unit header into CommonPredicates (#29584)
1 parent d63f8f4 commit 1d353b6

9 files changed

Lines changed: 75 additions & 64 deletions

File tree

src/server/game/Entities/Unit/Unit.h

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,60 +2017,4 @@ class TC_GAME_API Unit : public WorldObject
20172017
/* Player Movement fields END*/
20182018
};
20192019

2020-
namespace Trinity
2021-
{
2022-
// Binary predicate for sorting Units based on percent value of a power
2023-
class PowerPctOrderPred
2024-
{
2025-
public:
2026-
PowerPctOrderPred(Powers power, bool ascending = true) : _power(power), _ascending(ascending) { }
2027-
2028-
bool operator()(WorldObject const* objA, WorldObject const* objB) const
2029-
{
2030-
Unit const* a = objA->ToUnit();
2031-
Unit const* b = objB->ToUnit();
2032-
float rA = a ? a->GetPowerPct(_power) : 0.0f;
2033-
float rB = b ? b->GetPowerPct(_power) : 0.0f;
2034-
return _ascending ? rA < rB : rA > rB;
2035-
}
2036-
2037-
bool operator()(Unit const* a, Unit const* b) const
2038-
{
2039-
float rA = a->GetPowerPct(_power);
2040-
float rB = b->GetPowerPct(_power);
2041-
return _ascending ? rA < rB : rA > rB;
2042-
}
2043-
2044-
private:
2045-
Powers const _power;
2046-
bool const _ascending;
2047-
};
2048-
2049-
// Binary predicate for sorting Units based on percent value of health
2050-
class HealthPctOrderPred
2051-
{
2052-
public:
2053-
HealthPctOrderPred(bool ascending = true) : _ascending(ascending) { }
2054-
2055-
bool operator()(WorldObject const* objA, WorldObject const* objB) const
2056-
{
2057-
Unit const* a = objA->ToUnit();
2058-
Unit const* b = objB->ToUnit();
2059-
float rA = (a && a->GetMaxHealth()) ? float(a->GetHealth()) / float(a->GetMaxHealth()) : 0.0f;
2060-
float rB = (b && b->GetMaxHealth()) ? float(b->GetHealth()) / float(b->GetMaxHealth()) : 0.0f;
2061-
return _ascending ? rA < rB : rA > rB;
2062-
}
2063-
2064-
bool operator() (Unit const* a, Unit const* b) const
2065-
{
2066-
float rA = a->GetMaxHealth() ? float(a->GetHealth()) / float(a->GetMaxHealth()) : 0.0f;
2067-
float rB = b->GetMaxHealth() ? float(b->GetHealth()) / float(b->GetMaxHealth()) : 0.0f;
2068-
return _ascending ? rA < rB : rA > rB;
2069-
}
2070-
2071-
private:
2072-
bool const _ascending;
2073-
};
2074-
}
2075-
20762020
#endif

src/server/game/Miscellaneous/CommonPredicates.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,35 @@
1919
#include "Unit.h"
2020

2121
Trinity::Predicates::IsVictimOf::IsVictimOf(Unit const* attacker) : _victim(attacker ? attacker->GetVictim() : nullptr) { }
22+
23+
bool Trinity::Predicates::PowerPctOrderPred::operator()(WorldObject const* objA, WorldObject const* objB) const
24+
{
25+
Unit const* a = objA->ToUnit();
26+
Unit const* b = objB->ToUnit();
27+
float rA = a ? a->GetPowerPct(_power) : 0.0f;
28+
float rB = b ? b->GetPowerPct(_power) : 0.0f;
29+
return _ascending ? rA < rB : rA > rB;
30+
}
31+
32+
bool Trinity::Predicates::PowerPctOrderPred::operator()(Unit const* a, Unit const* b) const
33+
{
34+
float rA = a->GetPowerPct(_power);
35+
float rB = b->GetPowerPct(_power);
36+
return _ascending ? rA < rB : rA > rB;
37+
}
38+
39+
bool Trinity::Predicates::HealthPctOrderPred::operator()(WorldObject const* objA, WorldObject const* objB) const
40+
{
41+
Unit const* a = objA->ToUnit();
42+
Unit const* b = objB->ToUnit();
43+
float rA = (a && a->GetMaxHealth()) ? float(a->GetHealth()) / float(a->GetMaxHealth()) : 0.0f;
44+
float rB = (b && b->GetMaxHealth()) ? float(b->GetHealth()) / float(b->GetMaxHealth()) : 0.0f;
45+
return _ascending ? rA < rB : rA > rB;
46+
}
47+
48+
bool Trinity::Predicates::HealthPctOrderPred::operator()(Unit const* a, Unit const* b) const
49+
{
50+
float rA = a->GetMaxHealth() ? float(a->GetHealth()) / float(a->GetMaxHealth()) : 0.0f;
51+
float rB = b->GetMaxHealth() ? float(b->GetHealth()) / float(b->GetMaxHealth()) : 0.0f;
52+
return _ascending ? rA < rB : rA > rB;
53+
}

src/server/game/Miscellaneous/CommonPredicates.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
class Unit;
2525
class WorldObject;
2626

27+
enum Powers : int8;
28+
2729
namespace Trinity
2830
{
2931
namespace Predicates
@@ -38,6 +40,33 @@ namespace Trinity
3840
WorldObject const* _victim;
3941
};
4042

43+
/// Binary predicate for sorting Units based on percent value of a power
44+
class TC_GAME_API PowerPctOrderPred
45+
{
46+
public:
47+
PowerPctOrderPred(Powers power, bool ascending = true) : _power(power), _ascending(ascending) { }
48+
49+
bool operator()(WorldObject const* objA, WorldObject const* objB) const;
50+
bool operator()(Unit const* a, Unit const* b) const;
51+
52+
private:
53+
Powers const _power;
54+
bool const _ascending;
55+
};
56+
57+
/// Binary predicate for sorting Units based on percent value of health
58+
class TC_GAME_API HealthPctOrderPred
59+
{
60+
public:
61+
HealthPctOrderPred(bool ascending = true) : _ascending(ascending) { }
62+
63+
bool operator()(WorldObject const* objA, WorldObject const* objB) const;
64+
bool operator() (Unit const* a, Unit const* b) const;
65+
66+
private:
67+
bool const _ascending;
68+
};
69+
4170
template <typename PRED>
4271
class TC_GAME_API Inverter
4372
{

src/server/scripts/Spells/spell_druid.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323

2424
#include "ScriptMgr.h"
25+
#include "CommonPredicates.h"
2526
#include "Containers.h"
2627
#include "GameTime.h"
2728
#include "Optional.h"
@@ -1868,7 +1869,7 @@ class spell_dru_wild_growth : public SpellScript
18681869

18691870
if (targets.size() > maxTargets)
18701871
{
1871-
targets.sort(Trinity::HealthPctOrderPred());
1872+
targets.sort(Trinity::Predicates::HealthPctOrderPred());
18721873
targets.resize(maxTargets);
18731874
}
18741875

src/server/scripts/Spells/spell_generic.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "ScriptMgr.h"
2626
#include "Battleground.h"
2727
#include "CellImpl.h"
28+
#include "CommonPredicates.h"
2829
#include "Containers.h"
2930
#include "CreatureAI.h"
3031
#include "DBCStores.h"
@@ -3302,7 +3303,7 @@ class spell_gen_replenishment : public SpellScript
33023303

33033304
if (targets.size() > maxTargets)
33043305
{
3305-
targets.sort(Trinity::PowerPctOrderPred(POWER_MANA));
3306+
targets.sort(Trinity::Predicates::PowerPctOrderPred(POWER_MANA));
33063307
targets.resize(maxTargets);
33073308
}
33083309
}

src/server/scripts/Spells/spell_item.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "ScriptMgr.h"
2525
#include "Battleground.h"
26+
#include "CommonPredicates.h"
2627
#include "Containers.h"
2728
#include "Creature.h"
2829
#include "CreatureAIImpl.h"
@@ -961,7 +962,7 @@ class spell_item_echoes_of_light : public SpellScript
961962
if (targets.size() < 2)
962963
return;
963964

964-
targets.sort(Trinity::HealthPctOrderPred());
965+
targets.sort(Trinity::Predicates::HealthPctOrderPred());
965966

966967
WorldObject* target = targets.front();
967968
targets.clear();

src/server/scripts/Spells/spell_paladin.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323

2424
#include "ScriptMgr.h"
25+
#include "CommonPredicates.h"
2526
#include "Containers.h"
2627
#include "GameTime.h"
2728
#include "Group.h"
@@ -670,7 +671,7 @@ class spell_pal_glyph_of_holy_light : public SpellScript
670671
targets.remove(GetCaster());
671672
if (targets.size() > maxTargets)
672673
{
673-
targets.sort(Trinity::HealthPctOrderPred());
674+
targets.sort(Trinity::Predicates::HealthPctOrderPred());
674675
targets.resize(maxTargets);
675676
}
676677
}

src/server/scripts/Spells/spell_priest.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323

2424
#include "ScriptMgr.h"
25+
#include "CommonPredicates.h"
2526
#include "Creature.h"
2627
#include "Errors.h"
2728
#include "GridNotifiers.h"
@@ -284,7 +285,7 @@ class spell_pri_circle_of_healing : public SpellScript
284285

285286
if (targets.size() > maxTargets)
286287
{
287-
targets.sort(Trinity::HealthPctOrderPred());
288+
targets.sort(Trinity::Predicates::HealthPctOrderPred());
288289
targets.resize(maxTargets);
289290
}
290291
}
@@ -379,7 +380,7 @@ class spell_pri_divine_hymn : public SpellScript
379380

380381
if (targets.size() > maxTargets)
381382
{
382-
targets.sort(Trinity::HealthPctOrderPred());
383+
targets.sort(Trinity::Predicates::HealthPctOrderPred());
383384
targets.resize(maxTargets);
384385
}
385386
}
@@ -542,7 +543,7 @@ class spell_pri_hymn_of_hope : public SpellScript
542543

543544
if (targets.size() > maxTargets)
544545
{
545-
targets.sort(Trinity::PowerPctOrderPred(POWER_MANA));
546+
targets.sort(Trinity::Predicates::PowerPctOrderPred(POWER_MANA));
546547
targets.resize(maxTargets);
547548
}
548549
}

src/server/scripts/Spells/spell_shaman.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323

2424
#include "ScriptMgr.h"
25+
#include "CommonPredicates.h"
2526
#include "GridNotifiers.h"
2627
#include "Item.h"
2728
#include "ObjectAccessor.h"
@@ -146,7 +147,7 @@ class spell_sha_ancestral_awakening_proc : public SpellScript
146147
if (targets.size() < 2)
147148
return;
148149

149-
targets.sort(Trinity::HealthPctOrderPred());
150+
targets.sort(Trinity::Predicates::HealthPctOrderPred());
150151

151152
WorldObject* target = targets.front();
152153
targets.clear();

0 commit comments

Comments
 (0)