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 > > AbstractFollower 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 *)
 
Unit * GetTarget () 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 AbstractFollower
 AbstractFollower (Unit *target=nullptr)
 
virtual ~AbstractFollower ()
 
void SetTarget (Unit *unit)
 
Unit * GetTarget () const
 

Static Public Member Functions

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

Private Attributes

std::unique_ptr< PathGenerator > i_path
 
TimeTrackerSmall i_recheckPredictedDistanceTimer
 
bool i_recheckPredictedDistance
 
Optional< Position > _lastTargetPosition
 
Optional< Position > _lastPredictedPosition
 
float _range
 
ChaseAngle _angle
 
bool _inheritWalkState
 
bool _inheritSpeed
 

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
86 : AbstractFollower(target), i_path(nullptr), i_recheckPredictedDistanceTimer(0), i_recheckPredictedDistance(false), _range(range), _angle(angle), _inheritWalkState(inheritWalkState), _inheritSpeed(inheritSpeed) {}
Definition AbstractFollower.h:24
float _range
Definition TargetedMovementGenerator.h:115
bool _inheritWalkState
Definition TargetedMovementGenerator.h:117
std::unique_ptr< PathGenerator > i_path
Definition TargetedMovementGenerator.h:109
bool _inheritSpeed
Definition TargetedMovementGenerator.h:118
bool i_recheckPredictedDistance
Definition TargetedMovementGenerator.h:111
ChaseAngle _angle
Definition TargetedMovementGenerator.h:116
TimeTrackerSmall i_recheckPredictedDistanceTimer
Definition TargetedMovementGenerator.h:110

◆ ~FollowMovementGenerator()

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

Member Function Documentation

◆ _addUnitStateMove()

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

References UNIT_STATE_FOLLOW_MOVE.

◆ _clearUnitStateMove()

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

References UNIT_STATE_FOLLOW_MOVE.

◆ DoFinalize()

template<class T >
template void FollowMovementGenerator< T >::DoFinalize ( T *  )
753{
754 owner->ClearUnitState(UNIT_STATE_FOLLOW | UNIT_STATE_FOLLOW_MOVE);
755}
@ UNIT_STATE_FOLLOW
Definition UnitDefines.h:182

References UNIT_STATE_FOLLOW, and UNIT_STATE_FOLLOW_MOVE.

◆ DoInitialize()

template<class T >
template void FollowMovementGenerator< T >::DoInitialize ( T *  )
745{
746 i_path = nullptr;
747 _lastTargetPosition.reset();
748 owner->AddUnitState(UNIT_STATE_FOLLOW);
749}
Optional< Position > _lastTargetPosition
Definition TargetedMovementGenerator.h:113

References UNIT_STATE_FOLLOW.

◆ DoReset()

template<class T >
template void FollowMovementGenerator< T >::DoReset ( T *  )
759{
760 DoInitialize(owner);
761}
void DoInitialize(T *)
Definition TargetedMovementGenerator.cpp:744

◆ DoUpdate()

template<class T >
template bool FollowMovementGenerator< T >::DoUpdate ( T *  ,
uint32   
)
605{
606 if (!GetTarget() || !GetTarget()->IsInWorld() || !owner->IsInMap(GetTarget()))
607 return false;
608
609 if (!owner || !owner->IsAlive())
610 return false;
611
612 Creature* cOwner = owner->ToCreature();
613 Unit* target = GetTarget();
614
615 // the owner might be unable to move (rooted or casting), or we have lost the target, pause movement
616 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || (cOwner && owner->ToCreature()->IsMovementPreventedByCasting()))
617 {
618 i_path = nullptr;
619 owner->StopMoving();
620 _lastTargetPosition.reset();
621 return true;
622 }
623
624 bool followingMaster = false;
625 Pet* oPet = owner->ToPet();
626 if (oPet)
627 {
628 if (target->GetGUID() == oPet->GetOwnerGUID())
629 followingMaster = true;
630 }
631
632 bool forceDest =
633 (followingMaster) || // allow pets following their master to cheat while generating paths
634 (GetTarget()->IsPlayer() && GetTarget()->ToPlayer()->IsGameMaster()) // for .npc follow
635 ; // closes "bool forceDest", that way it is more appropriate, so we can comment out crap whenever we need to
636
637 bool targetIsMoving = false;
638 bool isPlayerPet = owner->IsGuardian() && target->IsPlayer();
639 bool isFollowingPlayer = target->IsPlayer();
640
641 if (PositionOkay(target, isPlayerPet, targetIsMoving, time_diff))
642 {
643 if (owner->HasUnitState(UNIT_STATE_FOLLOW_MOVE) && owner->movespline->Finalized())
644 {
645 owner->ClearUnitState(UNIT_STATE_FOLLOW_MOVE);
646 i_path = nullptr;
647 MovementInform(owner);
648
650 {
652 }
653
654 owner->SetFacingTo(target->GetOrientation());
655 }
656 }
657 else
658 {
659 Position targetPosition = target->GetPosition();
660 _lastTargetPosition = targetPosition;
661
662 // If player is moving and their position is not updated, we need to predict position
663 if (targetIsMoving)
664 {
665 Position predictedPosition = PredictPosition(target);
666 if (_lastPredictedPosition && _lastPredictedPosition->GetExactDistSq(&predictedPosition) < 0.25f)
667 return true;
668
669 if (IsValidPredictedPosition(target, predictedPosition))
670 {
671 _lastPredictedPosition = predictedPosition;
672 targetPosition = predictedPosition;
674 }
675 else
676 {
679 }
680 }
681 else
682 {
686 }
687
688 if (!i_path)
689 i_path = std::make_unique<PathGenerator>(owner);
690 else
691 i_path->Clear();
692
693 target->MovePositionToFirstCollision(targetPosition, owner->GetCombatReach() + _range, target->ToAbsoluteAngle(_angle.RelativeAngle) - target->GetOrientation());
694
695 float x, y, z;
696 targetPosition.GetPosition(x, y, z);
697
698 if (owner->IsHovering())
699 owner->UpdateAllowedPositionZ(x, y, z);
700
701 bool success = i_path->CalculatePath(x, y, z, forceDest);
702 if (!success || (i_path->GetPathType() & PATHFIND_NOPATH && !followingMaster))
703 {
704 if (!owner->IsStopped())
705 owner->StopMoving();
706
707 // Teleport if stuck and too far away
708 if (cOwner && isFollowingPlayer)
709 {
710 float const distance = owner->GetDistance2d(target);
711 if (distance > 20.f)
712 {
713 float teleX;
714 float teleY;
715 float teleZ;
716
717 target->GetClosePoint(teleX, teleY, teleZ, owner->GetCombatReach());
718 teleZ = owner->GetMapHeight(teleX, teleY, teleZ);
719 owner->NearTeleportTo(teleX, teleY, teleZ, target->GetOrientation());
720 _lastTargetPosition.reset();
722 }
723 }
724 return true;
725 }
726
727 owner->AddUnitState(UNIT_STATE_FOLLOW_MOVE);
728
729 Movement::MoveSplineInit init(owner);
730 init.MovebyPath(i_path->GetPath());
732 init.SetWalk(target->IsWalking());
733
734 if (_inheritSpeed)
735 if (Optional<float> velocity = GetVelocity(owner, target, i_path->GetActualEndPosition(), owner->IsGuardian()))
736 init.SetVelocity(*velocity);
737 init.Launch();
738 }
739
740 return true;
741}
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:544
static Optional< float > GetVelocity(Unit *owner, Unit *target, G3D::Vector3 const &dest, bool playerPet)
Definition TargetedMovementGenerator.cpp:485
static Position const PredictPosition(Unit *target)
Definition TargetedMovementGenerator.cpp:512
@ UNIT_STATE_NOT_MOVE
Definition UnitDefines.h:225
Definition Creature.h:47
bool PositionOkay(Unit *target, bool isPlayerPet, bool &targetIsMoving, uint32 diff)
Definition TargetedMovementGenerator.cpp:559
Unit * GetTarget() const
Definition TargetedMovementGenerator.h:97
void MovementInform(T *)
Definition TargetedMovementGenerator.cpp:764
Optional< Position > _lastPredictedPosition
Definition TargetedMovementGenerator.h:114
Definition MoveSplineInit.h:63
bool IsPlayer() const
Definition Object.h:207
Player * ToPlayer()
Definition Object.h:208
Creature * ToCreature()
Definition Object.h:212
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:120
Definition Pet.h:41
bool IsGameMaster() const
Definition Player.h:1178
Definition Unit.h:665
Pet * ToPet()
Definition Unit.h:730
ObjectGuid GetOwnerGUID() const override
Definition Unit.h:714
bool IsGuardian() const
Definition Unit.h:796
bool IsWalking() const
Definition Unit.h:1714
void MovePositionToFirstCollision(Position &pos, float dist, float angle)
Definition Object.cpp:2984
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:2825
bool IsInMap(WorldObject const *obj) const
Definition Object.cpp:1344
float RelativeAngle
Definition MotionMaster.h:127
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(), 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
106{ return _range; }

References FollowMovementGenerator< T >::_range.

◆ GetMovementGeneratorType()

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

Implements MovementGenerator.

89{ 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
Unit * GetTarget() const
Definition AbstractFollower.h:30

References AbstractFollower::GetTarget().

Referenced by npc_commandscript::HandleNpcUnFollowCommand().

◆ MovementInform()

template<class T >
void FollowMovementGenerator< T >::MovementInform ( T *  owner)
765{
766 if (!owner->IsCreature())
767 return;
768
769 // Pass back the GUIDLow of the target. If it is pet's owner then PetAI will handle
770 if (CreatureAI* AI = owner->ToCreature()->AI())
771 AI->MovementInform(FOLLOW_MOTION_TYPE, GetTarget()->GetGUID().GetCounter());
772}
Definition CreatureAI.h:69

References FOLLOW_MOTION_TYPE.

◆ PositionOkay()

template<class T >
bool FollowMovementGenerator< T >::PositionOkay ( Unit *  target,
bool  isPlayerPet,
bool &  targetIsMoving,
uint32  diff 
)
560{
562 return false;
563
564 float exactDistSq = target->GetExactDistSq(_lastTargetPosition->GetPositionX(), _lastTargetPosition->GetPositionY(), _lastTargetPosition->GetPositionZ());
565 float distanceTolerance = 0.25f;
566 // For creatures, increase tolerance
567 if (target->IsCreature())
568 {
569 distanceTolerance += _range + _range;
570 }
571
572 if (isPlayerPet)
573 {
575 }
576
577 if (exactDistSq > distanceTolerance)
578 return false;
579
580 if (isPlayerPet)
581 {
582 if (!targetIsMoving)
583 {
585 {
588 {
590 return false;
591 }
592 }
593
594 return true;
595 }
596
597 return false;
598 }
599
600 return true;
601}
@ MOVEMENTFLAG_FORWARD
Definition UnitDefines.h:372
@ MOVEMENTFLAG_STRAFE_LEFT
Definition UnitDefines.h:374
@ MOVEMENTFLAG_BACKWARD
Definition UnitDefines.h:373
@ MOVEMENTFLAG_STRAFE_RIGHT
Definition UnitDefines.h:375
bool IsCreature() const
Definition Object.h:211
MovementInfo m_movementInfo
Definition Object.h:711
bool HasMovementFlag(uint32 flag) const
Definition Object.h:353
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: