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

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
 

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
85 : 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:114
bool _inheritWalkState
Definition TargetedMovementGenerator.h:116
std::unique_ptr< PathGenerator > i_path
Definition TargetedMovementGenerator.h:108
bool _inheritSpeed
Definition TargetedMovementGenerator.h:117
bool i_recheckPredictedDistance
Definition TargetedMovementGenerator.h:110
ChaseAngle _angle
Definition TargetedMovementGenerator.h:115
TimeTrackerSmall i_recheckPredictedDistanceTimer
Definition TargetedMovementGenerator.h:109

◆ ~FollowMovementGenerator()

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

Member Function Documentation

◆ _addUnitStateMove()

template<class T >
static void FollowMovementGenerator< T >::_addUnitStateMove ( T *  u)
inlinestatic
103{ 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
102{ u->ClearUnitState(UNIT_STATE_FOLLOW_MOVE); }

References UNIT_STATE_FOLLOW_MOVE.

◆ DoFinalize()

template<class T >
template void FollowMovementGenerator< T >::DoFinalize ( T *  )
729{
730 owner->ClearUnitState(UNIT_STATE_FOLLOW | UNIT_STATE_FOLLOW_MOVE);
731}
@ 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 *  )
721{
722 i_path = nullptr;
723 _lastTargetPosition.reset();
724 owner->AddUnitState(UNIT_STATE_FOLLOW);
725}
Optional< Position > _lastTargetPosition
Definition TargetedMovementGenerator.h:112

References UNIT_STATE_FOLLOW.

◆ DoReset()

template<class T >
template void FollowMovementGenerator< T >::DoReset ( T *  )
735{
736 DoInitialize(owner);
737}
void DoInitialize(T *)
Definition TargetedMovementGenerator.cpp:720

◆ DoUpdate()

template<class T >
template bool FollowMovementGenerator< T >::DoUpdate ( T *  ,
uint32   
)
581{
582 if (!GetTarget() || !GetTarget()->IsInWorld() || !owner->IsInMap(GetTarget()))
583 return false;
584
585 if (!owner || !owner->IsAlive())
586 return false;
587
588 Creature* cOwner = owner->ToCreature();
589 Unit* target = GetTarget();
590
591 // the owner might be unable to move (rooted or casting), or we have lost the target, pause movement
592 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || (cOwner && owner->ToCreature()->IsMovementPreventedByCasting()))
593 {
594 i_path = nullptr;
595 owner->StopMoving();
596 _lastTargetPosition.reset();
597 return true;
598 }
599
600 bool followingMaster = false;
601 Pet* oPet = owner->ToPet();
602 if (oPet)
603 {
604 if (target->GetGUID() == oPet->GetOwnerGUID())
605 followingMaster = true;
606 }
607
608 bool forceDest =
609 (followingMaster) || // allow pets following their master to cheat while generating paths
610 (GetTarget()->IsPlayer() && GetTarget()->ToPlayer()->IsGameMaster()) // for .npc follow
611 ; // closes "bool forceDest", that way it is more appropriate, so we can comment out crap whenever we need to
612
613 bool targetIsMoving = false;
614 bool isPlayerPet = owner->IsGuardian() && target->IsPlayer();
615 bool isFollowingPlayer = target->IsPlayer();
616
617 if (PositionOkay(target, isPlayerPet, targetIsMoving, time_diff))
618 {
619 if (owner->HasUnitState(UNIT_STATE_FOLLOW_MOVE) && owner->movespline->Finalized())
620 {
621 owner->ClearUnitState(UNIT_STATE_FOLLOW_MOVE);
622 i_path = nullptr;
623 MovementInform(owner);
624
626 {
628 }
629
630 owner->SetFacingTo(target->GetOrientation());
631 }
632 }
633 else
634 {
635 Position targetPosition = target->GetPosition();
636 _lastTargetPosition = targetPosition;
637
638 // If player is moving and their position is not updated, we need to predict position
639 if (targetIsMoving)
640 {
641 Position predictedPosition = PredictPosition(target);
642 if (_lastPredictedPosition && _lastPredictedPosition->GetExactDistSq(&predictedPosition) < 0.25f)
643 return true;
644
645 if (IsValidPredictedPosition(target, predictedPosition))
646 {
647 _lastPredictedPosition = predictedPosition;
648 targetPosition = predictedPosition;
650 }
651 else
652 {
655 }
656 }
657 else
658 {
662 }
663
664 if (!i_path)
665 i_path = std::make_unique<PathGenerator>(owner);
666 else
667 i_path->Clear();
668
669 target->MovePositionToFirstCollision(targetPosition, owner->GetCombatReach() + _range, target->ToAbsoluteAngle(_angle.RelativeAngle) - target->GetOrientation());
670
671 float x, y, z;
672 targetPosition.GetPosition(x, y, z);
673
674 if (owner->IsHovering())
675 owner->UpdateAllowedPositionZ(x, y, z);
676
677 bool success = i_path->CalculatePath(x, y, z, forceDest);
678 if (!success || (i_path->GetPathType() & PATHFIND_NOPATH && !followingMaster))
679 {
680 if (!owner->IsStopped())
681 owner->StopMoving();
682
683 // Teleport if stuck and too far away
684 if (cOwner && isFollowingPlayer)
685 {
686 float const distance = owner->GetDistance2d(target);
687 if (distance > 20.f)
688 {
689 float teleX;
690 float teleY;
691 float teleZ;
692
693 target->GetClosePoint(teleX, teleY, teleZ, owner->GetCombatReach());
694 teleZ = owner->GetMapHeight(teleX, teleY, teleZ);
695 owner->NearTeleportTo(teleX, teleY, teleZ, target->GetOrientation());
696 _lastTargetPosition.reset();
698 }
699 }
700 return true;
701 }
702
703 owner->AddUnitState(UNIT_STATE_FOLLOW_MOVE);
704
705 Movement::MoveSplineInit init(owner);
706 init.MovebyPath(i_path->GetPath());
708 init.SetWalk(target->IsWalking());
709
710 if (_inheritSpeed)
711 if (Optional<float> velocity = GetVelocity(owner, target, i_path->GetActualEndPosition(), owner->IsGuardian()))
712 init.SetVelocity(*velocity);
713 init.Launch();
714 }
715
716 return true;
717}
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:520
static Optional< float > GetVelocity(Unit *owner, Unit *target, G3D::Vector3 const &dest, bool playerPet)
Definition TargetedMovementGenerator.cpp:461
static Position const PredictPosition(Unit *target)
Definition TargetedMovementGenerator.cpp:488
@ 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:535
Unit * GetTarget() const
Definition TargetedMovementGenerator.h:96
void MovementInform(T *)
Definition TargetedMovementGenerator.cpp:740
Optional< Position > _lastPredictedPosition
Definition TargetedMovementGenerator.h:113
Definition MoveSplineInit.h:63
bool IsPlayer() const
Definition Object.h:201
Player * ToPlayer()
Definition Object.h:202
Creature * ToCreature()
Definition Object.h:206
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:114
Definition Pet.h:41
bool IsGameMaster() const
Definition Player.h:1178
Definition Unit.h:665
Pet * ToPet()
Definition Unit.h:730
bool IsGuardian() const
Definition Unit.h:796
ObjectGuid GetOwnerGUID() const
Definition Unit.h:714
bool IsWalking() const
Definition Unit.h:1711
void MovePositionToFirstCollision(Position &pos, float dist, float angle)
Definition Object.cpp:2979
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:2820
bool IsInMap(WorldObject const *obj) const
Definition Object.cpp:1339
float RelativeAngle
Definition MotionMaster.h:126
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
105{ return _range; }

References FollowMovementGenerator< T >::_range.

◆ GetMovementGeneratorType()

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

Implements MovementGenerator.

88{ 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)
741{
742 if (!owner->IsCreature())
743 return;
744
745 // Pass back the GUIDLow of the target. If it is pet's owner then PetAI will handle
746 if (CreatureAI* AI = owner->ToCreature()->AI())
747 AI->MovementInform(FOLLOW_MOTION_TYPE, GetTarget()->GetGUID().GetCounter());
748}
Definition CreatureAI.h:69

References FOLLOW_MOTION_TYPE.

◆ PositionOkay()

template<class T >
bool FollowMovementGenerator< T >::PositionOkay ( Unit target,
bool  isPlayerPet,
bool &  targetIsMoving,
uint32  diff 
)
536{
538 return false;
539
540 float exactDistSq = target->GetExactDistSq(_lastTargetPosition->GetPositionX(), _lastTargetPosition->GetPositionY(), _lastTargetPosition->GetPositionZ());
541 float distanceTolerance = 0.25f;
542 // For creatures, increase tolerance
543 if (target->IsCreature())
544 {
545 distanceTolerance += _range + _range;
546 }
547
548 if (isPlayerPet)
549 {
551 }
552
553 if (exactDistSq > distanceTolerance)
554 return false;
555
556 if (isPlayerPet)
557 {
558 if (!targetIsMoving)
559 {
561 {
564 {
566 return false;
567 }
568 }
569
570 return true;
571 }
572
573 return false;
574 }
575
576 return true;
577}
@ 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:205
MovementInfo m_movementInfo
Definition Object.h:704
bool HasMovementFlag(uint32 flag) const
Definition Object.h:347
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: