AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
ChaseMovementGenerator< T > Class Template Reference

#include "TargetedMovementGenerator.h"

Inheritance diagram for ChaseMovementGenerator< T >:
MovementGeneratorMedium< T, ChaseMovementGenerator< T > > TargetedMovementGeneratorBase MovementGenerator

Public Member Functions

 ChaseMovementGenerator (Unit *target, Optional< ChaseRange > range={}, Optional< ChaseAngle > angle={})
 
 ~ChaseMovementGenerator ()
 
MovementGeneratorType GetMovementGeneratorType ()
 
bool DoUpdate (T *, uint32)
 
void DoInitialize (T *)
 
void DoFinalize (T *)
 
void DoReset (T *)
 
void MovementInform (T *)
 
bool PositionOkay (T *owner, Unit *target, Optional< float > maxDistance, Optional< ChaseAngle > angle)
 
void unitSpeedChanged ()
 
UnitGetTarget () const
 
bool EnableWalking () const
 
bool HasLostTarget (Unit *unit) const
 
void DoInitialize (Player *owner)
 
void DoInitialize (Creature *owner)
 
- Public Member Functions inherited from MovementGeneratorMedium< T, ChaseMovementGenerator< T > >
void Initialize (Unit *u) override
 
void Finalize (Unit *u) override
 
void Reset (Unit *u) override
 
bool Update (Unit *u, uint32 time_diff) override
 
- Public Member Functions inherited from MovementGenerator
virtual ~MovementGenerator ()
 
virtual uint32 GetSplineId () const
 
virtual void Pause (uint32)
 
virtual void Resume (uint32)
 
virtual bool GetResetPosition (float &, float &, float &)
 
- Public Member Functions inherited from TargetedMovementGeneratorBase
 TargetedMovementGeneratorBase (Unit *target)
 
void stopFollowing ()
 

Private Attributes

TimeTrackerSmall i_leashExtensionTimer
 
std::unique_ptr< PathGeneratori_path
 
TimeTrackerSmall i_recheckDistance
 
bool i_recalculateTravel
 
Optional< Position_lastTargetPosition
 
Optional< ChaseRange > const _range
 
Optional< ChaseAngle > const _angle
 
bool _movingTowards = true
 
bool _mutualChase = true
 

Additional Inherited Members

- Protected Attributes inherited from TargetedMovementGeneratorBase
FollowerReference i_target
 

Detailed Description

template<class T>
class ChaseMovementGenerator< T >

Constructor & Destructor Documentation

◆ ChaseMovementGenerator()

template<class T >
ChaseMovementGenerator< T >::ChaseMovementGenerator ( Unit target,
Optional< ChaseRange range = {},
Optional< ChaseAngle angle = {} 
)
inline
41 {}, Optional<ChaseAngle> angle = {})
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:24
std::unique_ptr< PathGenerator > i_path
Definition TargetedMovementGenerator.h:63
Optional< ChaseAngle > const _angle
Definition TargetedMovementGenerator.h:69
bool i_recalculateTravel
Definition TargetedMovementGenerator.h:65
TimeTrackerSmall i_recheckDistance
Definition TargetedMovementGenerator.h:64
Optional< ChaseRange > const _range
Definition TargetedMovementGenerator.h:68
TimeTrackerSmall i_leashExtensionTimer
Definition TargetedMovementGenerator.h:62
Definition TargetedMovementGenerator.h:29

◆ ~ChaseMovementGenerator()

template<class T >
ChaseMovementGenerator< T >::~ChaseMovementGenerator ( )
inline
43{ }

Member Function Documentation

◆ DoFinalize()

template<class T >
template void ChaseMovementGenerator< T >::DoFinalize ( T *  )
322{
323 owner->ClearUnitState(UNIT_STATE_CHASE | UNIT_STATE_CHASE_MOVE);
324 if (Creature* cOwner = owner->ToCreature())
325 {
326 cOwner->SetCannotReachTarget();
327 }
328}
@ UNIT_STATE_CHASE
Definition UnitDefines.h:175
@ UNIT_STATE_CHASE_MOVE
Definition UnitDefines.h:195
Definition Creature.h:43
Creature * ToCreature()
Definition Object.h:206

References Object::ToCreature(), UNIT_STATE_CHASE, and UNIT_STATE_CHASE_MOVE.

◆ DoInitialize() [1/3]

void ChaseMovementGenerator< Creature >::DoInitialize ( Creature owner)
311{
312 i_path = nullptr;
313 _lastTargetPosition.reset();
316 owner->SetWalk(false);
318}
@ BASE_ATTACK
Definition Unit.h:210
Optional< Position > _lastTargetPosition
Definition TargetedMovementGenerator.h:67
bool SetWalk(bool enable) override
Enable or disable the creature's walk mode by removing: MOVEMENTFLAG_WALKING. Infom also the client.
Definition Creature.cpp:3206
void AddUnitState(uint32 f)
Definition Unit.h:690
uint32 GetAttackTime(WeaponAttackType att) const
Definition Unit.h:859
void Reset(int32 interval)
Definition Timer.h:249

References Unit::AddUnitState(), BASE_ATTACK, Unit::GetAttackTime(), Creature::SetWalk(), and UNIT_STATE_CHASE.

◆ DoInitialize() [2/3]

void ChaseMovementGenerator< Player >::DoInitialize ( Player owner)
302{
303 i_path = nullptr;
304 _lastTargetPosition.reset();
305 owner->StopMoving();
307}
void StopMoving()
Definition Unit.cpp:16724

References Unit::AddUnitState(), Unit::StopMoving(), and UNIT_STATE_CHASE.

◆ DoInitialize() [3/3]

template<class T >
void ChaseMovementGenerator< T >::DoInitialize ( T *  )

◆ DoReset()

template<class T >
template void ChaseMovementGenerator< T >::DoReset ( T *  )
332{
333 DoInitialize(owner);
334}

◆ DoUpdate()

template<class T >
template bool ChaseMovementGenerator< T >::DoUpdate ( T *  ,
uint32   
)
Todo:
: Research how it should actually work.
67{
68 if (!i_target.isValid() || !i_target->IsInWorld() || !owner->IsInMap(i_target.getTarget()))
69 return false;
70
71 if (!owner || !owner->IsAlive())
72 return false;
73
74 Creature* cOwner = owner->ToCreature();
75 bool isStoppedBecauseOfCasting = cOwner && cOwner->IsMovementPreventedByCasting();
76
77 // the owner might be unable to move (rooted or casting), or we have lost the target, pause movement
78 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || HasLostTarget(owner) || isStoppedBecauseOfCasting)
79 {
80 owner->StopMoving();
81 _lastTargetPosition.reset();
82 if (cOwner)
83 {
84 if (isStoppedBecauseOfCasting)
85 {
86 // Don't reset leash timer if it's a spell like Shoot with a short cast time.
88 Spell *spell = cOwner->GetFirstCurrentCastingSpell();
89 bool spellHasLongCast = spell && spell->GetCastTime() > 1 * SECOND * IN_MILLISECONDS;
90 if (spellHasLongCast)
92 }
93 else
95
96 cOwner->SetCannotReachTarget();
97 }
98 return true;
99 }
100
101 bool forceDest =
102 //(cOwner && (cOwner->isWorldBoss() || cOwner->IsDungeonBoss())) || // force for all bosses, even not in instances
103 (i_target->IsPlayer() && i_target->ToPlayer()->IsGameMaster()) || // for .npc follow
104 (owner->CanFly())
105 ; // closes "bool forceDest", that way it is more appropriate, so we can comment out crap whenever we need to
106
107 Unit* target = i_target.getTarget();
108
109 bool mutualChase = IsMutualChase(owner, target);
110 bool const mutualTarget = target->GetVictim() == owner;
111 float const chaseRange = GetChaseRange(owner, target);
112 float const meleeRange = owner->GetMeleeRange(target);
113 float const minTarget = (_range ? _range->MinTolerance : 0.0f) + chaseRange;
114 float const maxRange = _range ? _range->MaxRange + chaseRange : meleeRange; // melee range already includes hitboxes
115 float const maxTarget = _range ? _range->MaxTolerance + chaseRange : CONTACT_DISTANCE + chaseRange;
116
117 Optional<ChaseAngle> angle = mutualChase ? Optional<ChaseAngle>() : _angle;
118
119 // Prevent almost infinite spinning of mutual targets.
120 if (angle && !mutualChase && _mutualChase && mutualTarget && chaseRange < meleeRange)
121 {
122 angle = Optional<ChaseAngle>();
123 mutualChase = true;
124 }
125
126 // Prevent almost infinite spinning for pets with mutualTarget
127 // _mutualChase is false for previous check
128 if (angle && !mutualChase && !_mutualChase && mutualTarget && chaseRange < meleeRange && cOwner && cOwner->IsPet())
129 {
130 angle = Optional<ChaseAngle>();
131 mutualChase = true;
132 }
133
134 // periodically check if we're already in the expected range...
135 i_recheckDistance.Update(time_diff);
137 {
138 i_recheckDistance.Reset(400); // Sniffed value
139
140 if (i_recalculateTravel && PositionOkay(owner, target, _movingTowards ? maxTarget : Optional<float>(), angle))
141 {
142 if ((owner->HasUnitState(UNIT_STATE_CHASE_MOVE) && !target->isMoving() && !mutualChase) || _range)
143 {
144 i_recalculateTravel = false;
145 i_path = nullptr;
146 if (cOwner)
147 cOwner->SetCannotReachTarget();
148 owner->StopMoving();
149 owner->SetInFront(target);
150 MovementInform(owner);
151 return true;
152 }
153 }
154 }
155
156 // if we're done moving, we want to clean up
157 if (owner->HasUnitState(UNIT_STATE_CHASE_MOVE) && owner->movespline->Finalized())
158 {
159 i_recalculateTravel = false;
160 i_path = nullptr;
161 if (cOwner)
162 cOwner->SetCannotReachTarget();
163 owner->ClearUnitState(UNIT_STATE_CHASE_MOVE);
164 owner->SetInFront(target);
165 MovementInform(owner);
166 }
167
168 if (cOwner)
169 {
170 if (owner->movespline->Finalized() && cOwner->IsWithinMeleeRange(target))
171 { // Mobs should chase you infinitely if you stop and wait every few seconds.
172 i_leashExtensionTimer.Update(time_diff);
174 {
176 cOwner->UpdateLeashExtensionTime();
177 }
178 }
179 else if (i_recalculateTravel)
181 }
182
183 // if the target moved, we have to consider whether to adjust
184 if (!_lastTargetPosition || target->GetPosition() != _lastTargetPosition.value() || mutualChase != _mutualChase || !owner->IsWithinLOSInMap(target))
185 {
187 _mutualChase = mutualChase;
188 if (owner->HasUnitState(UNIT_STATE_CHASE_MOVE) || !PositionOkay(owner, target, maxTarget, angle))
189 {
190 // can we get to the target?
191 if (cOwner && !target->isInAccessiblePlaceFor(cOwner))
192 {
193 cOwner->SetCannotReachTarget(target->GetGUID());
194 cOwner->StopMoving();
195 i_path = nullptr;
196 return true;
197 }
198
199 // figure out which way we want to move
200 float x, y, z;
201 target->GetPosition(x, y, z);
202 bool withinRange = owner->IsInDist(target, maxRange);
203 bool withinLOS = owner->IsWithinLOS(x, y, z);
204 bool moveToward = !(withinRange && withinLOS);
205
206 // make a new path if we have to...
207 if (!i_path || moveToward != _movingTowards)
208 i_path = std::make_unique<PathGenerator>(owner);
209 else
210 i_path->Clear();
211
212 // Predict chase destination to keep up with chase target
213 float additionalRange = 0;
214 bool predictDestination = !mutualChase && target->isMoving();
215 if (predictDestination)
216 {
217 UnitMoveType moveType = MOVE_RUN;
218 if (target->CanFly())
220 else
221 {
222 if (target->IsWalking())
223 moveType = MOVE_WALK;
224 else
226 }
227 float speed = target->GetSpeed(moveType) * 0.5f;
228 additionalRange = owner->GetExactDistSq(target) < G3D::square(speed) ? 0 : speed;
229 }
230
231 bool shortenPath;
232
233 // if we want to move toward the target and there's no fixed angle...
234 if (moveToward && !angle)
235 {
236 // ...we'll pathfind to the center, then shorten the path
237 shortenPath = true;
238 }
239 else
240 {
241 // otherwise, we fall back to nearpoint finding
242 target->GetNearPoint(owner, x, y, z, (moveToward ? maxTarget : minTarget) - chaseRange - additionalRange, 0, angle ? target->ToAbsoluteAngle(angle->RelativeAngle) : target->GetAngle(owner));
243 shortenPath = false;
244 }
245
246 if (owner->IsHovering())
247 owner->UpdateAllowedPositionZ(x, y, z);
248
249 bool success = i_path->CalculatePath(x, y, z, forceDest);
250 if (!success || i_path->GetPathType() & PATHFIND_NOPATH)
251 {
252 if (cOwner)
253 {
254 cOwner->SetCannotReachTarget(target->GetGUID());
255 }
256
257 owner->StopMoving();
258 return true;
259 }
260
261 if (shortenPath)
262 i_path->ShortenPathUntilDist(G3D::Vector3(x, y, z), maxTarget);
263
264 if (cOwner)
265 {
266 cOwner->SetCannotReachTarget();
267 }
268
269 bool walk = false;
270 if (cOwner && !cOwner->IsPet())
271 {
272 switch (cOwner->GetMovementTemplate().GetChase())
273 {
275 walk = owner->IsWalking();
276 break;
278 walk = true;
279 break;
280 default:
281 break;
282 }
283 }
284
285 owner->AddUnitState(UNIT_STATE_CHASE_MOVE);
286 i_recalculateTravel = true;
287
288 Movement::MoveSplineInit init(owner);
289 init.MovebyPath(i_path->GetPath());
290 init.SetFacing(target);
291 init.SetWalk(walk);
292 init.Launch();
293 }
294 }
295
296 return true;
297}
constexpr auto SECOND
Definition Common.h:46
constexpr auto IN_MILLISECONDS
Definition Common.h:53
#define CONTACT_DISTANCE
Definition ObjectDefines.h:23
@ PATHFIND_NOPATH
Definition PathGenerator.h:50
float GetChaseRange(Unit const *owner, Unit const *target)
Definition TargetedMovementGenerator.cpp:35
static bool IsMutualChase(Unit *owner, Unit *target)
Definition TargetedMovementGenerator.cpp:27
@ MOVEMENTFLAG_BACKWARD
Definition UnitDefines.h:366
@ UNIT_STATE_NOT_MOVE
Definition UnitDefines.h:218
UnitMoveType
Definition UnitDefines.h:348
@ MOVE_FLIGHT
Definition UnitDefines.h:355
@ MOVE_FLIGHT_BACK
Definition UnitDefines.h:356
@ MOVE_RUN
Definition UnitDefines.h:350
@ MOVE_RUN_BACK
Definition UnitDefines.h:351
@ MOVE_WALK
Definition UnitDefines.h:349
bool _mutualChase
Definition TargetedMovementGenerator.h:71
bool PositionOkay(T *owner, Unit *target, Optional< float > maxDistance, Optional< ChaseAngle > angle)
Definition TargetedMovementGenerator.cpp:47
void MovementInform(T *)
Definition TargetedMovementGenerator.cpp:337
bool HasLostTarget(Unit *unit) const
Definition TargetedMovementGenerator.h:59
bool _movingTowards
Definition TargetedMovementGenerator.h:70
void SetCannotReachTarget(ObjectGuid const &target=ObjectGuid::Empty)
Definition Creature.cpp:3675
void UpdateLeashExtensionTime()
Definition Creature.cpp:3728
CreatureMovementData const & GetMovementTemplate() const
Definition Creature.cpp:2995
bool IsMovementPreventedByCasting() const override
Definition Creature.cpp:3653
Definition MoveSplineInit.h:71
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:113
TO * getTarget() const
Definition Reference.h:95
bool isValid() const
Definition Reference.h:79
Definition Spell.h:287
int32 GetCastTime() const
Definition Spell.h:550
FollowerReference i_target
Definition TargetedMovementGenerator.h:34
Definition Unit.h:620
float GetSpeed(UnitMoveType mtype) const
Definition Unit.cpp:14516
virtual bool CanFly() const =0
bool IsPet() const
Definition Unit.h:747
bool IsWithinMeleeRange(Unit const *obj, float dist=0.f) const
Definition Unit.cpp:665
bool isInAccessiblePlaceFor(Creature const *c) const
Definition Unit.cpp:4249
bool HasUnitMovementFlag(uint32 f) const
Definition Unit.h:729
bool isMoving() const
Definition Unit.h:1622
Unit * GetVictim() const
Definition Unit.h:848
Spell * GetFirstCurrentCastingSpell() const
Definition Unit.cpp:4191
bool IsWalking() const
Definition Unit.h:1621
void GetNearPoint(WorldObject const *searcher, float &x, float &y, float &z, float searcher_size, float distance2d, float absAngle, float controlZ=0, Position const *startPos=nullptr) const
Definition Object.cpp:2635
CreatureChaseMovementType GetChase() const
Definition CreatureData.h:150
void GetPosition(float &x, float &y) const
Definition Position.h:126
void Update(int32 diff)
Definition Timer.h:239
bool Passed() const
Definition Timer.h:244

References AlwaysWalk, BASE_ATTACK, Unit::CanFly(), CanWalk, CONTACT_DISTANCE, Position::GetAngle(), Unit::GetAttackTime(), Spell::GetCastTime(), CreatureMovementData::GetChase(), GetChaseRange(), Unit::GetFirstCurrentCastingSpell(), Object::GetGUID(), Creature::GetMovementTemplate(), WorldObject::GetNearPoint(), Position::GetPosition(), Unit::GetSpeed(), Unit::GetVictim(), Unit::HasUnitMovementFlag(), IN_MILLISECONDS, Unit::isInAccessiblePlaceFor(), Creature::IsMovementPreventedByCasting(), Unit::isMoving(), IsMutualChase(), Unit::IsPet(), Unit::IsWalking(), Unit::IsWithinMeleeRange(), Movement::MoveSplineInit::Launch(), MOVE_FLIGHT, MOVE_FLIGHT_BACK, MOVE_RUN, MOVE_RUN_BACK, MOVE_WALK, Movement::MoveSplineInit::MovebyPath(), MOVEMENTFLAG_BACKWARD, PATHFIND_NOPATH, SECOND, Creature::SetCannotReachTarget(), Movement::MoveSplineInit::SetFacing(), Movement::MoveSplineInit::SetWalk(), Unit::StopMoving(), Position::ToAbsoluteAngle(), Object::ToCreature(), UNIT_STATE_CHASE_MOVE, UNIT_STATE_NOT_MOVE, and Creature::UpdateLeashExtensionTime().

◆ EnableWalking()

template<class T >
bool ChaseMovementGenerator< T >::EnableWalking ( ) const
inline
58{ return false; }

◆ GetMovementGeneratorType()

template<class T >
MovementGeneratorType ChaseMovementGenerator< T >::GetMovementGeneratorType ( )
inlinevirtual

Implements MovementGenerator.

45{ return CHASE_MOTION_TYPE; }
@ CHASE_MOTION_TYPE
Definition MotionMaster.h:45

References CHASE_MOTION_TYPE.

◆ GetTarget()

template<class T >
Unit * ChaseMovementGenerator< T >::GetTarget ( ) const
inline

◆ HasLostTarget()

template<class T >
bool ChaseMovementGenerator< T >::HasLostTarget ( Unit unit) const
inline
59{ return unit->GetVictim() != this->GetTarget(); }
Unit * GetTarget() const
Definition TargetedMovementGenerator.h:56

References ChaseMovementGenerator< T >::GetTarget(), and Unit::GetVictim().

◆ MovementInform()

template<class T >
template void ChaseMovementGenerator< T >::MovementInform ( T *  )
338{
339 if (!owner->IsCreature())
340 return;
341
342 // Pass back the GUIDLow of the target. If it is pet's owner then PetAI will handle
343 if (CreatureAI* AI = owner->ToCreature()->AI())
344 AI->MovementInform(CHASE_MOTION_TYPE, i_target.getTarget()->GetGUID().GetCounter());
345}
Definition CreatureAI.h:70
LowType GetCounter() const
Definition ObjectGuid.h:145

References CHASE_MOTION_TYPE.

◆ PositionOkay()

template<class T >
bool ChaseMovementGenerator< T >::PositionOkay ( T *  owner,
Unit target,
Optional< float >  maxDistance,
Optional< ChaseAngle angle 
)
48{
49 float const distSq = owner->GetExactDistSq(target);
50
51 // Distance between owner(chaser) and target is greater than the allowed distance.
52 if (maxDistance && distSq > G3D::square(*maxDistance))
53 return false;
54
55 // owner's relative angle to its target is not within boundaries
56 if (angle && !angle->IsAngleOkay(target->GetRelativeAngle(owner)))
57 return false;
58
59 // owner cannot see its target
60 if (!owner->IsWithinLOSInMap(target))
61 return false;
62 return true;
63}
float GetRelativeAngle(const Position *pos) const
Definition Position.h:201

References Position::GetRelativeAngle().

◆ unitSpeedChanged()

template<class T >
void ChaseMovementGenerator< T >::unitSpeedChanged ( )
inlinevirtual

Member Data Documentation

◆ _angle

template<class T >
Optional<ChaseAngle> const ChaseMovementGenerator< T >::_angle
private

◆ _lastTargetPosition

template<class T >
Optional<Position> ChaseMovementGenerator< T >::_lastTargetPosition
private

◆ _movingTowards

template<class T >
bool ChaseMovementGenerator< T >::_movingTowards = true
private

◆ _mutualChase

template<class T >
bool ChaseMovementGenerator< T >::_mutualChase = true
private

◆ _range

template<class T >
Optional<ChaseRange> const ChaseMovementGenerator< T >::_range
private

◆ i_leashExtensionTimer

template<class T >
TimeTrackerSmall ChaseMovementGenerator< T >::i_leashExtensionTimer
private

◆ i_path

template<class T >
std::unique_ptr<PathGenerator> ChaseMovementGenerator< T >::i_path
private

◆ i_recalculateTravel

template<class T >
bool ChaseMovementGenerator< T >::i_recalculateTravel
private

◆ i_recheckDistance

template<class T >
TimeTrackerSmall ChaseMovementGenerator< T >::i_recheckDistance
private

The documentation for this class was generated from the following files: