TrinityCore
Loading...
Searching...
No Matches
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
106 minRange = range.Min + meleeRange;
107 maxRange = range.Max;
108
109 rangeMod = _caster->GetCombatReach();
110 rangeMod += target->GetCombatReach();
111
112 if (minRange > 0.0f && !(_spellInfo->RangeEntry->Flags & SPELL_RANGE_RANGED))
113 minRange += rangeMod;
114 }
115
116 if (_caster->isMoving() && target->isMoving() && !_caster->IsWalking() && !target->IsWalking() &&
118 rangeMod += 8.0f / 3.0f;
119 }
120
121 maxRange += rangeMod;
122
123 if (target != _caster)
124 {
125 if (!_caster->IsInDist(target, maxRange))
126 return false;
127
128 if (minRange > 0.0f && _caster->IsInDist(target, minRange))
129 return false;
130 }
131
132 return true;
133}
134
136{
137 if (!target)
138 return false;
139
140 if (_playerOnly && target->GetTypeId() != TYPEID_PLAYER)
141 return false;
142
143 if (Unit* currentVictim = _source->GetThreatManager().GetCurrentVictim())
144 return target != currentVictim;
145
146 return target != _source->GetVictim();
147}
148
149bool PowerUsersSelector::operator()(Unit const* target) const
150{
151 if (!_me || !target)
152 return false;
153
154 if (target->GetPowerType() != _power)
155 return false;
156
157 if (_playerOnly && target->GetTypeId() != TYPEID_PLAYER)
158 return false;
159
160 if (_dist > 0.0f && !_me->IsWithinCombatRange(target, _dist))
161 return false;
162
163 if (_dist < 0.0f && _me->IsWithinCombatRange(target, -_dist))
164 return false;
165
166 return true;
167}
168
170{
171 if (!_me || !target)
172 return false;
173
174 if (_playerOnly && target->GetTypeId() != TYPEID_PLAYER)
175 return false;
176
177 if (_dist > 0.0f && !_me->IsWithinCombatRange(target, _dist))
178 return false;
179
180 if (_inLos && !_me->IsWithinLOSInMap(target))
181 return false;
182
183 return true;
184}
int32_t int32
Definition Define.h:150
uint32_t uint32
Definition Define.h:154
#define ASSERT
Definition Errors.h:80
#define NOMINAL_MELEE_RANGE
@ TYPEID_PLAYER
Definition ObjectGuid.h:44
@ SPELL_CAST_OK
#define sSpellMgr
Definition SpellMgr.h:812
@ SPELL_RANGE_MELEE
Definition Spell.h:187
@ SPELL_RANGE_RANGED
Definition Spell.h:188
TypeID GetTypeId() const
Definition BaseEntity.h:166
SpellRangeEntry const * RangeEntry
Definition SpellInfo.h:392
SpellCastResult CheckTarget(WorldObject const *caster, WorldObject const *target, bool implicit=true) const
Unit * GetCurrentVictim()
Definition Unit.h:635
ThreatManager & GetThreatManager()
Definition Unit.h:1078
bool IsWithinCombatRange(Unit const *obj, float dist2compare) const
Definition Unit.cpp:670
Powers GetPowerType() const
Definition Unit.h:811
float GetCombatReach() const override
Definition Unit.h:705
bool isMoving() const
Definition Unit.h:1804
Unit * GetVictim() const
Definition Unit.h:726
bool HasAura(uint32 spellId, ObjectGuid casterGUID=ObjectGuid::Empty, ObjectGuid itemCasterGUID=ObjectGuid::Empty, uint32 reqEffMask=0) const
Definition Unit.cpp:4804
bool IsWalking() const
Definition Unit.h:1150
SpellRange GetSpellMinMaxRangeForTarget(Unit const *target, SpellInfo const *spellInfo) const
Definition Object.cpp:1665
bool IsWithinLOSInMap(WorldObject const *obj, LineOfSightChecks checks=LINEOFSIGHT_ALL_CHECKS, VMAP::ModelIgnoreFlags ignoreFlags=VMAP::ModelIgnoreFlags::Nothing) const
Definition Object.cpp:535
DefaultTargetSelector(Unit const *unit, float dist, bool playerOnly, bool withMainTank, int32 aura)
bool operator()(Unit const *target) const
Unit const * _exception
bool operator()(Unit const *target) const
bool operator()(Unit const *target) const
constexpr bool IsInDist(float x, float y, float z, float dist) const
Definition Position.h:155
Unit const * _me
Powers const _power
bool operator()(Unit const *target) const
bool const _playerOnly
SpellInfo const * _spellInfo
bool operator()(Unit const *target) const
SpellTargetSelector(Unit *caster, uint32 spellId)
Unit const * _caster