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

#include "TargetedMovementGenerator.h"

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

Public Member Functions

 FollowMovementGenerator (Unit *target, float range, ChaseAngle angle, bool inheritWalkState)
 
 ~FollowMovementGenerator ()
 
MovementGeneratorType GetMovementGeneratorType ()
 
bool DoUpdate (T *, uint32)
 
void DoInitialize (T *)
 
void DoFinalize (T *)
 
void DoReset (T *)
 
void MovementInform (T *)
 
UnitGetTarget () const
 
void unitSpeedChanged ()
 
bool PositionOkay (Unit *target, bool isPlayerPet, bool &targetIsMoving, uint32 diff)
 
float GetFollowRange () const
 
- Public Member Functions inherited from MovementGeneratorMedium< T, FollowMovementGenerator< 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 void Initialize (Unit *)=0
 
virtual void Finalize (Unit *)=0
 
virtual void Reset (Unit *)=0
 
virtual bool Update (Unit *, uint32 time_diff)=0
 
virtual MovementGeneratorType GetMovementGeneratorType ()=0
 
virtual uint32 GetSplineId () const
 
virtual void unitSpeedChanged ()
 
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 ()
 

Static Public Member Functions

static void _clearUnitStateMove (T *u)
 
static void _addUnitStateMove (T *u)
 

Private Attributes

std::unique_ptr< PathGeneratori_path
 
TimeTrackerSmall i_recheckPredictedDistanceTimer
 
bool i_recheckPredictedDistance
 
Optional< Position_lastTargetPosition
 
Optional< Position_lastPredictedPosition
 
float _range
 
ChaseAngle _angle
 
bool _inheritWalkState
 

Additional Inherited Members

- Protected Attributes inherited from TargetedMovementGeneratorBase
FollowerReference i_target
 

Detailed Description

template<class T>
class FollowMovementGenerator< T >

Constructor & Destructor Documentation

◆ FollowMovementGenerator()

template<class T >
FollowMovementGenerator< T >::FollowMovementGenerator ( Unit target,
float  range,
ChaseAngle  angle,
bool  inheritWalkState 
)
inline
Definition: TargetedMovementGenerator.h:29
float _range
Definition: TargetedMovementGenerator.h:108
bool _inheritWalkState
Definition: TargetedMovementGenerator.h:110
std::unique_ptr< PathGenerator > i_path
Definition: TargetedMovementGenerator.h:102
bool i_recheckPredictedDistance
Definition: TargetedMovementGenerator.h:104
ChaseAngle _angle
Definition: TargetedMovementGenerator.h:109
TimeTrackerSmall i_recheckPredictedDistanceTimer
Definition: TargetedMovementGenerator.h:103

◆ ~FollowMovementGenerator()

template<class T >
FollowMovementGenerator< T >::~FollowMovementGenerator ( )
inline
80{ }

Member Function Documentation

◆ _addUnitStateMove()

template<class T >
static void FollowMovementGenerator< T >::_addUnitStateMove ( T *  u)
inlinestatic
97{ u->AddUnitState(UNIT_STATE_FOLLOW_MOVE); }
@ UNIT_STATE_FOLLOW_MOVE
Definition: UnitDefines.h:175

References UNIT_STATE_FOLLOW_MOVE.

◆ _clearUnitStateMove()

template<class T >
static void FollowMovementGenerator< T >::_clearUnitStateMove ( T *  u)
inlinestatic
96{ u->ClearUnitState(UNIT_STATE_FOLLOW_MOVE); }

References UNIT_STATE_FOLLOW_MOVE.

◆ DoFinalize()

template<class T >
template void FollowMovementGenerator< T >::DoFinalize ( T *  )
560{
561 owner->ClearUnitState(UNIT_STATE_FOLLOW | UNIT_STATE_FOLLOW_MOVE);
562}
@ UNIT_STATE_FOLLOW
Definition: UnitDefines.h:158

References UNIT_STATE_FOLLOW, and UNIT_STATE_FOLLOW_MOVE.

◆ DoInitialize()

template<class T >
template void FollowMovementGenerator< T >::DoInitialize ( T *  )
552{
553 i_path = nullptr;
554 _lastTargetPosition.reset();
555 owner->AddUnitState(UNIT_STATE_FOLLOW);
556}
Optional< Position > _lastTargetPosition
Definition: TargetedMovementGenerator.h:106

References UNIT_STATE_FOLLOW.

◆ DoReset()

template<class T >
template void FollowMovementGenerator< T >::DoReset ( T *  )
566{
567 DoInitialize(owner);
568}
void DoInitialize(T *)
Definition: TargetedMovementGenerator.cpp:551

◆ DoUpdate()

template<class T >
template bool FollowMovementGenerator< T >::DoUpdate ( T *  ,
uint32   
)
442{
443 if (!i_target.isValid() || !i_target->IsInWorld() || !owner->IsInMap(i_target.getTarget()))
444 return false;
445
446 if (!owner || !owner->IsAlive())
447 return false;
448
449 Creature* cOwner = owner->ToCreature();
450 Unit* target = i_target.getTarget();
451
452 // the owner might be unable to move (rooted or casting), or we have lost the target, pause movement
453 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || (cOwner && owner->ToCreature()->IsMovementPreventedByCasting()))
454 {
455 i_path = nullptr;
456 owner->StopMoving();
457 _lastTargetPosition.reset();
458 return true;
459 }
460
461 bool followingMaster = false;
462 Pet* oPet = owner->ToPet();
463 if (oPet)
464 {
465 if (target->GetGUID() == oPet->GetOwnerGUID())
466 followingMaster = true;
467 }
468
469 bool forceDest =
470 (followingMaster) || // allow pets following their master to cheat while generating paths
471 (i_target->IsPlayer() && i_target->ToPlayer()->IsGameMaster()) // for .npc follow
472 ; // closes "bool forceDest", that way it is more appropriate, so we can comment out crap whenever we need to
473
474 bool targetIsMoving = false;
475 if (PositionOkay(target, owner->IsGuardian() && target->IsPlayer(), targetIsMoving, time_diff))
476 {
477 if (owner->HasUnitState(UNIT_STATE_FOLLOW_MOVE) && owner->movespline->Finalized())
478 {
480 i_path = nullptr;
481 MovementInform(owner);
482
484 {
486 }
487
488 owner->SetFacingTo(target->GetOrientation());
489 }
490 }
491 else
492 {
493 Position targetPosition = target->GetPosition();
494 _lastTargetPosition = targetPosition;
495
496 // If player is moving and their position is not updated, we need to predict position
497 if (targetIsMoving)
498 {
499 Position predictedPosition = PredictPosition(target);
500 if (_lastPredictedPosition && _lastPredictedPosition->GetExactDistSq(&predictedPosition) < 0.25f)
501 return true;
502
503 _lastPredictedPosition = predictedPosition;
504 targetPosition = predictedPosition;
506 }
507 else
508 {
511 }
512
513 if (!i_path)
514 i_path = std::make_unique<PathGenerator>(owner);
515 else
516 i_path->Clear();
517
518 target->MovePositionToFirstCollision(targetPosition, owner->GetCombatReach() + _range, target->ToAbsoluteAngle(_angle.RelativeAngle) - target->GetOrientation());
519
520 float x, y, z;
521 targetPosition.GetPosition(x, y, z);
522
523 if (owner->IsHovering())
524 owner->UpdateAllowedPositionZ(x, y, z);
525
526 bool success = i_path->CalculatePath(x, y, z, forceDest);
527 if (!success || (i_path->GetPathType() & PATHFIND_NOPATH && !followingMaster))
528 {
529 if (!owner->IsStopped())
530 owner->StopMoving();
531
532 return true;
533 }
534
535 owner->AddUnitState(UNIT_STATE_FOLLOW_MOVE);
536
537 Movement::MoveSplineInit init(owner);
538 init.MovebyPath(i_path->GetPath());
540 init.SetWalk(target->IsWalking() || target->movespline->isWalking());
541
542 if (Optional<float> velocity = GetVelocity(owner, target, i_path->GetActualEndPosition(), owner->IsGuardian()))
543 init.SetVelocity(*velocity);
544 init.Launch();
545 }
546
547 return true;
548}
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:24
@ PATHFIND_NOPATH
Definition: PathGenerator.h:51
static Optional< float > GetVelocity(Unit *owner, Unit *target, G3D::Vector3 const &dest, bool playerPet)
Definition: TargetedMovementGenerator.cpp:334
static Position const PredictPosition(Unit *target)
Definition: TargetedMovementGenerator.cpp:362
@ UNIT_STATE_NOT_MOVE
Definition: UnitDefines.h:197
TO * getTarget() const
Definition: Reference.h:95
bool isValid() const
Definition: Reference.h:79
void Reset(int32 interval)
Definition: Timer.h:249
Definition: Creature.h:46
bool IsPlayer() const
Definition: Object.h:197
Creature * ToCreature()
Definition: Object.h:202
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:109
void MovePositionToFirstCollision(Position &pos, float dist, float angle)
Definition: Object.cpp:2858
Definition: Position.h:28
float ToAbsoluteAngle(float relAngle) const
Definition: Position.h:203
float GetOrientation() const
Definition: Position.h:120
void GetPosition(float &x, float &y) const
Definition: Position.h:122
Definition: Pet.h:41
Definition: Unit.h:630
void ClearUnitState(uint32 f)
Definition: Unit.h:674
Pet * ToPet()
Definition: Unit.h:1714
Movement::MoveSpline * movespline
Definition: Unit.h:1818
ObjectGuid GetOwnerGUID() const
Definition: Unit.h:1210
bool IsWalking() const
Definition: Unit.h:1695
float RelativeAngle
Definition: MotionMaster.h:98
FollowerReference i_target
Definition: TargetedMovementGenerator.h:34
bool PositionOkay(Unit *target, bool isPlayerPet, bool &targetIsMoving, uint32 diff)
Definition: TargetedMovementGenerator.cpp:396
void MovementInform(T *)
Definition: TargetedMovementGenerator.cpp:571
Optional< Position > _lastPredictedPosition
Definition: TargetedMovementGenerator.h:107
bool isWalking() const
Definition: MoveSpline.h:119
Definition: MoveSplineInit.h:71

References Unit::ClearUnitState(), Object::GetGUID(), Position::GetOrientation(), Unit::GetOwnerGUID(), Position::GetPosition(), GetVelocity(), Unit::IsGuardian(), Creature::IsMovementPreventedByCasting(), Object::IsPlayer(), Unit::IsWalking(), Movement::MoveSpline::isWalking(), Movement::MoveSplineInit::Launch(), Movement::MoveSplineInit::MovebyPath(), WorldObject::MovePositionToFirstCollision(), Unit::movespline, PATHFIND_NOPATH, PredictPosition(), Movement::MoveSplineInit::SetVelocity(), Movement::MoveSplineInit::SetWalk(), Unit::StopMoving(), Position::ToAbsoluteAngle(), Object::ToCreature(), Unit::ToPet(), UNIT_STATE_FOLLOW_MOVE, and UNIT_STATE_NOT_MOVE.

◆ GetFollowRange()

template<class T >
float FollowMovementGenerator< T >::GetFollowRange ( ) const
inline
99{ return _range; }

References FollowMovementGenerator< T >::_range.

◆ GetMovementGeneratorType()

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

Implements MovementGenerator.

82{ return FOLLOW_MOTION_TYPE; }
@ FOLLOW_MOTION_TYPE
Definition: MotionMaster.h:52

References FOLLOW_MOTION_TYPE.

◆ GetTarget()

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

◆ MovementInform()

template<class T >
void FollowMovementGenerator< T >::MovementInform ( T *  owner)
572{
573 if (!owner->IsCreature())
574 return;
575
576 // Pass back the GUIDLow of the target. If it is pet's owner then PetAI will handle
577 if (CreatureAI* AI = owner->ToCreature()->AI())
578 AI->MovementInform(FOLLOW_MOTION_TYPE, i_target.getTarget()->GetGUID().GetCounter());
579}
Definition: CreatureAI.h:71
LowType GetCounter() const
Definition: ObjectGuid.h:145

References FOLLOW_MOTION_TYPE.

◆ PositionOkay()

template<class T >
bool FollowMovementGenerator< T >::PositionOkay ( Unit target,
bool  isPlayerPet,
bool &  targetIsMoving,
uint32  diff 
)
397{
399 return false;
400
401 float exactDistSq = target->GetExactDistSq(_lastTargetPosition->GetPositionX(), _lastTargetPosition->GetPositionY(), _lastTargetPosition->GetPositionZ());
402 float distanceTolerance = 0.25f;
403 // For creatures, increase tolerance
404 if (target->IsCreature())
405 {
406 distanceTolerance += _range + _range;
407 }
408
409 if (isPlayerPet)
410 {
412 }
413
414 if (exactDistSq > distanceTolerance)
415 return false;
416
417 if (isPlayerPet)
418 {
419 if (!targetIsMoving)
420 {
422 {
425 {
427 return false;
428 }
429 }
430
431 return true;
432 }
433
434 return false;
435 }
436
437 return true;
438}
@ MOVEMENTFLAG_FORWARD
Definition: UnitDefines.h:344
@ MOVEMENTFLAG_STRAFE_LEFT
Definition: UnitDefines.h:346
@ MOVEMENTFLAG_BACKWARD
Definition: UnitDefines.h:345
@ MOVEMENTFLAG_STRAFE_RIGHT
Definition: UnitDefines.h:347
int32 GetExpiry() const
Definition: Timer.h:254
void Update(int32 diff)
Definition: Timer.h:239
bool Passed() const
Definition: Timer.h:244
bool IsCreature() const
Definition: Object.h:201
bool HasMovementFlag(uint32 flag) const
Definition: Object.h:334
MovementInfo m_movementInfo
Definition: Object.h:609
float GetExactDistSq(float x, float y, float z) const
Definition: Position.h:170

References Position::GetExactDistSq(), MovementInfo::HasMovementFlag(), Object::IsCreature(), WorldObject::m_movementInfo, MOVEMENTFLAG_BACKWARD, MOVEMENTFLAG_FORWARD, MOVEMENTFLAG_STRAFE_LEFT, and MOVEMENTFLAG_STRAFE_RIGHT.

◆ unitSpeedChanged()

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

Member Data Documentation

◆ _angle

template<class T >
ChaseAngle FollowMovementGenerator< T >::_angle
private

◆ _inheritWalkState

template<class T >
bool FollowMovementGenerator< T >::_inheritWalkState
private

◆ _lastPredictedPosition

template<class T >
Optional<Position> FollowMovementGenerator< T >::_lastPredictedPosition
private

◆ _lastTargetPosition

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

◆ _range

template<class T >
float FollowMovementGenerator< T >::_range
private

◆ i_path

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

◆ i_recheckPredictedDistance

template<class T >
bool FollowMovementGenerator< T >::i_recheckPredictedDistance
private

◆ i_recheckPredictedDistanceTimer

template<class T >
TimeTrackerSmall FollowMovementGenerator< T >::i_recheckPredictedDistanceTimer
private