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, bool inheritSpeed)
 
 ~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 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 ()
 

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
 
bool _inheritSpeed
 

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,
bool  inheritSpeed 
)
inline
94 : TargetedMovementGeneratorBase(target), i_path(nullptr), i_recheckPredictedDistanceTimer(0), i_recheckPredictedDistance(false), _range(range), _angle(angle), _inheritWalkState(inheritWalkState), _inheritSpeed(inheritSpeed) {}
float _range
Definition TargetedMovementGenerator.h:123
bool _inheritWalkState
Definition TargetedMovementGenerator.h:125
std::unique_ptr< PathGenerator > i_path
Definition TargetedMovementGenerator.h:117
bool _inheritSpeed
Definition TargetedMovementGenerator.h:126
bool i_recheckPredictedDistance
Definition TargetedMovementGenerator.h:119
ChaseAngle _angle
Definition TargetedMovementGenerator.h:124
TimeTrackerSmall i_recheckPredictedDistanceTimer
Definition TargetedMovementGenerator.h:118
Definition TargetedMovementGenerator.h:29

◆ ~FollowMovementGenerator()

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

Member Function Documentation

◆ _addUnitStateMove()

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

References UNIT_STATE_FOLLOW_MOVE.

◆ _clearUnitStateMove()

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

References UNIT_STATE_FOLLOW_MOVE.

◆ DoFinalize()

template<class T >
template void FollowMovementGenerator< T >::DoFinalize ( T *  )
701{
702 owner->ClearUnitState(UNIT_STATE_FOLLOW | UNIT_STATE_FOLLOW_MOVE);
703}
@ UNIT_STATE_FOLLOW
Definition UnitDefines.h:179

References UNIT_STATE_FOLLOW, and UNIT_STATE_FOLLOW_MOVE.

◆ DoInitialize()

template<class T >
template void FollowMovementGenerator< T >::DoInitialize ( T *  )
693{
694 i_path = nullptr;
695 _lastTargetPosition.reset();
696 owner->AddUnitState(UNIT_STATE_FOLLOW);
697}
Optional< Position > _lastTargetPosition
Definition TargetedMovementGenerator.h:121

References UNIT_STATE_FOLLOW.

◆ DoReset()

template<class T >
template void FollowMovementGenerator< T >::DoReset ( T *  )
707{
708 DoInitialize(owner);
709}
void DoInitialize(T *)
Definition TargetedMovementGenerator.cpp:692

◆ DoUpdate()

template<class T >
template bool FollowMovementGenerator< T >::DoUpdate ( T *  ,
uint32   
)
554{
555 if (!i_target.isValid() || !i_target->IsInWorld() || !owner->IsInMap(i_target.getTarget()))
556 return false;
557
558 if (!owner || !owner->IsAlive())
559 return false;
560
561 Creature* cOwner = owner->ToCreature();
562 Unit* target = i_target.getTarget();
563
564 // the owner might be unable to move (rooted or casting), or we have lost the target, pause movement
565 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || (cOwner && owner->ToCreature()->IsMovementPreventedByCasting()))
566 {
567 i_path = nullptr;
568 owner->StopMoving();
569 _lastTargetPosition.reset();
570 return true;
571 }
572
573 bool followingMaster = false;
574 Pet* oPet = owner->ToPet();
575 if (oPet)
576 {
577 if (target->GetGUID() == oPet->GetOwnerGUID())
578 followingMaster = true;
579 }
580
581 bool forceDest =
582 (followingMaster) || // allow pets following their master to cheat while generating paths
583 (i_target->IsPlayer() && i_target->ToPlayer()->IsGameMaster()) // for .npc follow
584 ; // closes "bool forceDest", that way it is more appropriate, so we can comment out crap whenever we need to
585
586 bool targetIsMoving = false;
587 bool isPlayerPet = owner->IsGuardian() && target->IsPlayer();
588 bool isFollowingPlayer = target->IsPlayer();
589
590 if (PositionOkay(target, isPlayerPet, targetIsMoving, time_diff))
591 {
592 if (owner->HasUnitState(UNIT_STATE_FOLLOW_MOVE) && owner->movespline->Finalized())
593 {
594 owner->ClearUnitState(UNIT_STATE_FOLLOW_MOVE);
595 i_path = nullptr;
596 MovementInform(owner);
597
599 {
601 }
602
603 owner->SetFacingTo(target->GetOrientation());
604 }
605 }
606 else
607 {
608 Position targetPosition = target->GetPosition();
609 _lastTargetPosition = targetPosition;
610
611 // If player is moving and their position is not updated, we need to predict position
612 if (targetIsMoving)
613 {
614 Position predictedPosition = PredictPosition(target);
615 if (_lastPredictedPosition && _lastPredictedPosition->GetExactDistSq(&predictedPosition) < 0.25f)
616 return true;
617
618 if (IsValidPredictedPosition(target, predictedPosition))
619 {
620 _lastPredictedPosition = predictedPosition;
621 targetPosition = predictedPosition;
623 }
624 else
625 {
628 }
629 }
630 else
631 {
635 }
636
637 if (!i_path)
638 i_path = std::make_unique<PathGenerator>(owner);
639 else
640 i_path->Clear();
641
642 target->MovePositionToFirstCollision(targetPosition, owner->GetCombatReach() + _range, target->ToAbsoluteAngle(_angle.RelativeAngle) - target->GetOrientation());
643
644 float x, y, z;
645 targetPosition.GetPosition(x, y, z);
646
647 if (owner->IsHovering())
648 owner->UpdateAllowedPositionZ(x, y, z);
649
650 bool success = i_path->CalculatePath(x, y, z, forceDest);
651 if (!success || (i_path->GetPathType() & PATHFIND_NOPATH && !followingMaster))
652 {
653 if (!owner->IsStopped())
654 owner->StopMoving();
655
656 // Teleport if stuck and too far away
657 if (cOwner && isFollowingPlayer)
658 {
659 float distance = owner->GetDistance(target);
660 if (distance > 20.f)
661 {
662 float teleX;
663 float teleY;
664 float teleZ;
665
666 target->GetClosePoint(teleX, teleY, teleZ, owner->GetCombatReach());
667 owner->NearTeleportTo(teleX, teleY, teleZ, target->GetOrientation());
668 _lastTargetPosition.reset();
670 }
671 }
672 return true;
673 }
674
675 owner->AddUnitState(UNIT_STATE_FOLLOW_MOVE);
676
677 Movement::MoveSplineInit init(owner);
678 init.MovebyPath(i_path->GetPath());
680 init.SetWalk(target->IsWalking());
681
682 if (_inheritSpeed)
683 if (Optional<float> velocity = GetVelocity(owner, target, i_path->GetActualEndPosition(), owner->IsGuardian()))
684 init.SetVelocity(*velocity);
685 init.Launch();
686 }
687
688 return true;
689}
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:24
@ PATHFIND_NOPATH
Definition PathGenerator.h:50
static bool IsValidPredictedPosition(Unit *target, Position const &predicted)
Definition TargetedMovementGenerator.cpp:493
static Optional< float > GetVelocity(Unit *owner, Unit *target, G3D::Vector3 const &dest, bool playerPet)
Definition TargetedMovementGenerator.cpp:433
static Position const PredictPosition(Unit *target)
Definition TargetedMovementGenerator.cpp:461
@ UNIT_STATE_NOT_MOVE
Definition UnitDefines.h:222
Definition Creature.h:47
bool PositionOkay(Unit *target, bool isPlayerPet, bool &targetIsMoving, uint32 diff)
Definition TargetedMovementGenerator.cpp:508
void MovementInform(T *)
Definition TargetedMovementGenerator.cpp:712
Optional< Position > _lastPredictedPosition
Definition TargetedMovementGenerator.h:122
Definition MoveSplineInit.h:63
bool IsPlayer() const
Definition Object.h:200
Creature * ToCreature()
Definition Object.h:205
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:113
Definition Pet.h:41
TO * getTarget() const
Definition Reference.h:95
bool isValid() const
Definition Reference.h:79
FollowerReference i_target
Definition TargetedMovementGenerator.h:34
Definition Unit.h:655
Pet * ToPet()
Definition Unit.h:719
void StopMoving()
Definition Unit.cpp:16879
bool IsGuardian() const
Definition Unit.h:785
ObjectGuid GetOwnerGUID() const
Definition Unit.h:703
bool IsWalking() const
Definition Unit.h:1687
void MovePositionToFirstCollision(Position &pos, float dist, float angle)
Definition Object.cpp:2869
bool GetClosePoint(float &x, float &y, float &z, float size, float distance2d=0, float angle=0, WorldObject const *forWho=nullptr, bool force=false) const
Definition Object.cpp:2710
float RelativeAngle
Definition MotionMaster.h:125
Definition Position.h:27
float ToAbsoluteAngle(float relAngle) const
Definition Position.h:207
float GetOrientation() const
Definition Position.h:124
void GetPosition(float &x, float &y) const
Definition Position.h:126
void Reset(int32 interval)
Definition Timer.h:249

References WorldObject::GetClosePoint(), Object::GetGUID(), Position::GetOrientation(), Unit::GetOwnerGUID(), Position::GetPosition(), GetVelocity(), Unit::IsGuardian(), Creature::IsMovementPreventedByCasting(), Object::IsPlayer(), IsValidPredictedPosition(), Unit::IsWalking(), Movement::MoveSplineInit::Launch(), Movement::MoveSplineInit::MovebyPath(), WorldObject::MovePositionToFirstCollision(), 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
114{ return _range; }

References FollowMovementGenerator< T >::_range.

◆ GetMovementGeneratorType()

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

Implements MovementGenerator.

97{ return FOLLOW_MOTION_TYPE; }
@ FOLLOW_MOTION_TYPE
Definition MotionMaster.h:54

References FOLLOW_MOTION_TYPE.

◆ GetTarget()

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

◆ MovementInform()

template<class T >
void FollowMovementGenerator< T >::MovementInform ( T *  owner)
713{
714 if (!owner->IsCreature())
715 return;
716
717 // Pass back the GUIDLow of the target. If it is pet's owner then PetAI will handle
718 if (CreatureAI* AI = owner->ToCreature()->AI())
719 AI->MovementInform(FOLLOW_MOTION_TYPE, i_target.getTarget()->GetGUID().GetCounter());
720}
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 
)
509{
511 return false;
512
513 float exactDistSq = target->GetExactDistSq(_lastTargetPosition->GetPositionX(), _lastTargetPosition->GetPositionY(), _lastTargetPosition->GetPositionZ());
514 float distanceTolerance = 0.25f;
515 // For creatures, increase tolerance
516 if (target->IsCreature())
517 {
518 distanceTolerance += _range + _range;
519 }
520
521 if (isPlayerPet)
522 {
524 }
525
526 if (exactDistSq > distanceTolerance)
527 return false;
528
529 if (isPlayerPet)
530 {
531 if (!targetIsMoving)
532 {
534 {
537 {
539 return false;
540 }
541 }
542
543 return true;
544 }
545
546 return false;
547 }
548
549 return true;
550}
@ MOVEMENTFLAG_FORWARD
Definition UnitDefines.h:369
@ MOVEMENTFLAG_STRAFE_LEFT
Definition UnitDefines.h:371
@ MOVEMENTFLAG_BACKWARD
Definition UnitDefines.h:370
@ MOVEMENTFLAG_STRAFE_RIGHT
Definition UnitDefines.h:372
bool IsCreature() const
Definition Object.h:204
MovementInfo m_movementInfo
Definition Object.h:692
bool HasMovementFlag(uint32 flag) const
Definition Object.h:345
float GetExactDistSq(float x, float y, float z) const
Definition Position.h:174
int32 GetExpiry() const
Definition Timer.h:254
void Update(int32 diff)
Definition Timer.h:239
bool Passed() const
Definition Timer.h:244

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

◆ _inheritSpeed

template<class T >
bool FollowMovementGenerator< T >::_inheritSpeed
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

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