TrinityCore
UnitAICommon.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "UnitAICommon.h"
19#include "Map.h"
20#include "Spell.h"
21#include "SpellInfo.h"
22#include "SpellMgr.h"
23#include "Unit.h"
24
25DefaultTargetSelector::DefaultTargetSelector(Unit const* unit, float dist, bool playerOnly, bool withTank, int32 aura)
26 : _me(unit), _dist(dist), _playerOnly(playerOnly), _exception(!withTank ? unit->GetThreatManager().GetLastVictim() : nullptr), _aura(aura)
27{
28}
29
30bool DefaultTargetSelector::operator()(Unit const* target) const
31{
32 if (!_me)
33 return false;
34
35 if (!target)
36 return false;
37
38 if (_exception && target == _exception)
39 return false;
40
41 if (_playerOnly && (target->GetTypeId() != TYPEID_PLAYER))
42 return false;
43
44 if (_dist > 0.0f && !_me->IsWithinCombatRange(target, _dist))
45 return false;
46
47 if (_dist < 0.0f && _me->IsWithinCombatRange(target, -_dist))
48 return false;
49
50 if (_aura)
51 {
52 if (_aura > 0)
53 {
54 if (!target->HasAura(_aura))
55 return false;
56 }
57 else
58 {
59 if (target->HasAura(-_aura))
60 return false;
61 }
62 }
63
64 return true;
65}
66
68 _caster(caster), _spellInfo(sSpellMgr->GetSpellInfo(spellId, caster->GetMap()->GetDifficultyID()))
69{
71}
72
73bool SpellTargetSelector::operator()(Unit const* target) const
74{
75 if (!target)
76 return false;
77
79 return false;
80
81 // copypasta from Spell::CheckRange
82 float minRange = 0.0f;
83 float maxRange = 0.0f;
84 float rangeMod = 0.0f;
86 {
88 {
89 rangeMod = _caster->GetCombatReach() + 4.0f / 3.0f;
90 rangeMod += target->GetCombatReach();
91
92 rangeMod = std::max(rangeMod, NOMINAL_MELEE_RANGE);
93 }
94 else
95 {
96 float meleeRange = 0.0f;
98 {
99 meleeRange = _caster->GetCombatReach() + 4.0f / 3.0f;
100 meleeRange += target->GetCombatReach();
101
102 meleeRange = std::max(meleeRange, NOMINAL_MELEE_RANGE);
103 }
104
105 minRange = _caster->GetSpellMinRangeForTarget(target, _spellInfo) + meleeRange;
106 maxRange = _caster->GetSpellMaxRangeForTarget(target, _spellInfo);
107
108 rangeMod = _caster->GetCombatReach();
109 rangeMod += target->GetCombatReach();
110
111 if (minRange > 0.0f && !(_spellInfo->RangeEntry->Flags & SPELL_RANGE_RANGED))
112 minRange += rangeMod;
113 }
114
115 if (_caster->isMoving() && target->isMoving() && !_caster->IsWalking() && !target->IsWalking() &&
117 rangeMod += 8.0f / 3.0f;
118 }
119
120 maxRange += rangeMod;
121
122 minRange *= minRange;
123 maxRange *= maxRange;
124
125 if (target != _caster)
126 {
127 if (_caster->GetExactDistSq(target) > maxRange)
128 return false;
129
130 if (minRange > 0.0f && _caster->GetExactDistSq(target) < minRange)
131 return false;
132 }
133
134 return true;
135}
136
138{
139 if (!target)
140 return false;
141
142 if (_playerOnly && target->GetTypeId() != TYPEID_PLAYER)
143 return false;
144
145 if (Unit* currentVictim = _source->GetThreatManager().GetCurrentVictim())
146 return target != currentVictim;
147
148 return target != _source->GetVictim();
149}
150
151bool PowerUsersSelector::operator()(Unit const* target) const
152{
153 if (!_me || !target)
154 return false;
155
156 if (target->GetPowerType() != _power)
157 return false;
158
159 if (_playerOnly && target->GetTypeId() != TYPEID_PLAYER)
160 return false;
161
162 if (_dist > 0.0f && !_me->IsWithinCombatRange(target, _dist))
163 return false;
164
165 if (_dist < 0.0f && _me->IsWithinCombatRange(target, -_dist))
166 return false;
167
168 return true;
169}
170
172{
173 if (!_me || !target)
174 return false;
175
176 if (_playerOnly && target->GetTypeId() != TYPEID_PLAYER)
177 return false;
178
179 if (_dist > 0.0f && !_me->IsWithinCombatRange(target, _dist))
180 return false;
181
182 if (_inLos && !_me->IsWithinLOSInMap(target))
183 return false;
184
185 return true;
186}
int32_t int32
Definition: Define.h:138
uint32_t uint32
Definition: Define.h:142
#define ASSERT
Definition: Errors.h:68
#define NOMINAL_MELEE_RANGE
Definition: ObjectDefines.h:44
@ TYPEID_PLAYER
Definition: ObjectGuid.h:41
@ SPELL_CAST_OK
#define sSpellMgr
Definition: SpellMgr.h:849
@ SPELL_RANGE_MELEE
Definition: Spell.h:165
@ SPELL_RANGE_RANGED
Definition: Spell.h:166
TypeID GetTypeId() const
Definition: Object.h:173
SpellRangeEntry const * RangeEntry
Definition: SpellInfo.h:387
SpellCastResult CheckTarget(WorldObject const *caster, WorldObject const *target, bool implicit=true) const
Definition: SpellInfo.cpp:2175
Unit * GetCurrentVictim()
Definition: Unit.h:627
ThreatManager & GetThreatManager()
Definition: Unit.h:1063
bool IsWithinCombatRange(Unit const *obj, float dist2compare) const
Definition: Unit.cpp:635
Powers GetPowerType() const
Definition: Unit.h:799
float GetCombatReach() const override
Definition: Unit.h:694
bool isMoving() const
Definition: Unit.h:1732
Unit * GetVictim() const
Definition: Unit.h:715
bool HasAura(uint32 spellId, ObjectGuid casterGUID=ObjectGuid::Empty, ObjectGuid itemCasterGUID=ObjectGuid::Empty, uint32 reqEffMask=0) const
Definition: Unit.cpp:4664
bool IsWalking() const
Definition: Unit.h:1136
bool IsWithinLOSInMap(WorldObject const *obj, LineOfSightChecks checks=LINEOFSIGHT_ALL_CHECKS, VMAP::ModelIgnoreFlags ignoreFlags=VMAP::ModelIgnoreFlags::Nothing) const
Definition: Object.cpp:1181
float GetSpellMinRangeForTarget(Unit const *target, SpellInfo const *spellInfo) const
Definition: Object.cpp:2319
float GetSpellMaxRangeForTarget(Unit const *target, SpellInfo const *spellInfo) const
Definition: Object.cpp:2305
DefaultTargetSelector(Unit const *unit, float dist, bool playerOnly, bool withMainTank, int32 aura)
bool operator()(Unit const *target) const
Unit const * _exception
Definition: UnitAICommon.h:63
bool operator()(Unit const *target) const
bool operator()(Unit const *target) const
constexpr float GetExactDistSq(float x, float y, float z) const
Definition: Position.h:110
float const _dist
Definition: UnitAICommon.h:101
Unit const * _me
Definition: UnitAICommon.h:99
Powers const _power
Definition: UnitAICommon.h:100
bool operator()(Unit const *target) const
bool const _playerOnly
Definition: UnitAICommon.h:102
SpellInfo const * _spellInfo
Definition: UnitAICommon.h:76
bool operator()(Unit const *target) const
SpellTargetSelector(Unit *caster, uint32 spellId)
Unit const * _caster
Definition: UnitAICommon.h:75