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

#include "FleeingMovementGenerator.h"

Inheritance diagram for FleeingMovementGenerator< T >:
MovementGeneratorMedium< T, FleeingMovementGenerator< T > > MovementGenerator

Public Member Functions

 FleeingMovementGenerator (ObjectGuid fleeTargetGUID)
 
MovementGeneratorType GetMovementGeneratorType () override
 
void DoInitialize (T *)
 
void DoFinalize (T *)
 
void DoReset (T *)
 
bool DoUpdate (T *, uint32)
 
void DoFinalize (Player *owner)
 
- Public Member Functions inherited from MovementGeneratorMedium< T, FleeingMovementGenerator< 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 &)
 

Private Member Functions

void SetTargetLocation (T *)
 
void GetPoint (T *, Position &position)
 

Private Attributes

std::unique_ptr< PathGenerator_path
 
ObjectGuid _fleeTargetGUID
 
TimeTracker _timer
 
bool _interrupt
 
uint8 _shortPathsCount
 

Detailed Description

template<class T>
class FleeingMovementGenerator< T >

Constructor & Destructor Documentation

◆ FleeingMovementGenerator()

template<class T >
FleeingMovementGenerator< T >::FleeingMovementGenerator ( ObjectGuid  fleeTargetGUID)
inlineexplicit
27: _path(nullptr), _fleeTargetGUID(fleeTargetGUID), _timer(0), _interrupt(false), _shortPathsCount(0) { }
bool _interrupt
Definition: FleeingMovementGenerator.h:43
TimeTracker _timer
Definition: FleeingMovementGenerator.h:42
std::unique_ptr< PathGenerator > _path
Definition: FleeingMovementGenerator.h:40
uint8 _shortPathsCount
Definition: FleeingMovementGenerator.h:44
ObjectGuid _fleeTargetGUID
Definition: FleeingMovementGenerator.h:41

Member Function Documentation

◆ DoFinalize() [1/2]

void FleeingMovementGenerator< Player >::DoFinalize ( Player owner)
53{
56 owner->StopMoving();
57}
@ UNIT_STATE_FLEEING
Definition: UnitDefines.h:156
@ UNIT_FLAG_FLEEING
Definition: UnitDefines.h:252
void ClearUnitState(uint32 f)
Definition: Unit.h:674
void StopMoving()
Definition: Unit.cpp:16606
void RemoveUnitFlag(UnitFlags flags)
UnitFlags available in UnitDefines.h.
Definition: Unit.h:684

References Unit::ClearUnitState(), Unit::RemoveUnitFlag(), Unit::StopMoving(), UNIT_FLAG_FLEEING, and UNIT_STATE_FLEEING.

◆ DoFinalize() [2/2]

template<class T >
void FleeingMovementGenerator< T >::DoFinalize ( T *  )
48{
49}

◆ DoInitialize()

template<class T >
template void FleeingMovementGenerator< T >::DoInitialize ( T *  )
33{
34 if (!owner)
35 {
36 return;
37 }
38
39 owner->StopMoving();
40 _path = nullptr;
41 owner->SetUnitFlag(UNIT_FLAG_FLEEING);
42 owner->AddUnitState(UNIT_STATE_FLEEING);
43 SetTargetLocation(owner);
44}
void SetTargetLocation(T *)
Definition: FleeingMovementGenerator.cpp:105

◆ DoReset()

template<class T >
template void FleeingMovementGenerator< T >::DoReset ( T *  )
73{
74 DoInitialize(owner);
75}
void DoInitialize(T *)
Definition: FleeingMovementGenerator.cpp:32

◆ DoUpdate()

template<class T >
template bool FleeingMovementGenerator< T >::DoUpdate ( T *  ,
uint32   
)
79{
80 if (!owner || !owner->IsAlive())
81 {
82 return false;
83 }
84
85 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || owner->IsMovementPreventedByCasting())
86 {
87 _path = nullptr;
88 _interrupt = true;
89 owner->StopMoving();
90 return true;
91 }
92 else
93 _interrupt = false;
94
95 _timer.Update(diff);
96 if (!_interrupt && _timer.Passed() && owner->movespline->Finalized())
97 {
98 SetTargetLocation(owner);
99 }
100
101 return true;
102}
@ UNIT_STATE_NOT_MOVE
Definition: UnitDefines.h:197
void Update(time_t diff)
Definition: Timer.h:207
bool Passed() const
Definition: Timer.h:212

References UNIT_STATE_NOT_MOVE.

◆ GetMovementGeneratorType()

template<class T >
MovementGeneratorType FleeingMovementGenerator< T >::GetMovementGeneratorType ( )
inlineoverridevirtual

Implements MovementGenerator.

29{ return FLEEING_MOTION_TYPE; }
@ FLEEING_MOTION_TYPE
Definition: MotionMaster.h:47

References FLEEING_MOTION_TYPE.

◆ GetPoint()

template<class T >
template void FleeingMovementGenerator< T >::GetPoint ( T *  ,
Position position 
)
private
172{
173 float casterDistance = 0.f;
174 float casterAngle = 0.f;
175 Unit* fleeTarget = nullptr;
176 if (_shortPathsCount < 5)
177 fleeTarget = ObjectAccessor::GetUnit(*owner, _fleeTargetGUID);
178
179 if (fleeTarget)
180 {
181 casterDistance = fleeTarget->GetDistance(owner);
182 if (casterDistance > 0.2f)
183 {
184 casterAngle = fleeTarget->GetAngle(owner);
185 }
186 else
187 {
188 casterAngle = frand(0.0f, 2.0f * float(M_PI));
189 }
190 }
191 else
192 {
193 casterDistance = 0.0f;
194 casterAngle = frand(0.0f, 2.0f * float(M_PI));
195 }
196
197 float distance = 0.f;
198 float angle = 0.f;
199 if (casterDistance < MIN_QUIET_DISTANCE)
200 {
201 distance = frand(0.4f, 1.3f) * (MIN_QUIET_DISTANCE - casterDistance);
202 angle = casterAngle + frand(-float(M_PI) / 8.0f, float(M_PI) / 8.0f);
203 }
204 else if (casterDistance > MAX_QUIET_DISTANCE)
205 {
206 distance = frand(0.4f, 1.0f) * (MAX_QUIET_DISTANCE - MIN_QUIET_DISTANCE);
207 angle = -casterAngle + frand(-float(M_PI) / 4.0f, float(M_PI) / 4.0f);
208 }
209 else // we are inside quiet range
210 {
211 distance = frand(0.6f, 1.2f) * (MAX_QUIET_DISTANCE - MIN_QUIET_DISTANCE);
212 angle = frand(0.0f, 2.0f * float(M_PI));
213 }
214
215 // In MovePositionToFirstCollision we have added owner's orientation
216 // so now let's subtract it
217 angle -= owner->GetOrientation();
218
219 owner->MovePositionToFirstCollision(position, distance, angle);
220}
float frand(float min, float max)
Definition: Random.cpp:57
#define MAX_QUIET_DISTANCE
Definition: FleeingMovementGenerator.cpp:28
#define MIN_QUIET_DISTANCE
Definition: FleeingMovementGenerator.cpp:27
Unit * GetUnit(WorldObject const &, ObjectGuid const guid)
Definition: ObjectAccessor.cpp:202
float GetDistance(WorldObject const *obj) const
Definition: Object.cpp:1245
float GetAngle(const Position *pos) const
Definition: Position.cpp:77
Definition: Unit.h:630

References frand(), Position::GetAngle(), WorldObject::GetDistance(), ObjectAccessor::GetUnit(), MAX_QUIET_DISTANCE, and MIN_QUIET_DISTANCE.

◆ SetTargetLocation()

template<class T >
template void FleeingMovementGenerator< T >::SetTargetLocation ( T *  )
private
106{
107 if (!owner)
108 {
109 return;
110 }
111
112 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || owner->IsMovementPreventedByCasting())
113 {
114 _path = nullptr;
115 _interrupt = true;
116 owner->StopMoving();
117 return;
118 }
119
120 owner->AddUnitState(UNIT_STATE_FLEEING_MOVE);
121
122 Position destination = owner->GetPosition();
123 GetPoint(owner, destination);
124
125 // Add LOS check for target point
126 if (!owner->IsWithinLOS(destination.GetPositionX(), destination.GetPositionY(), destination.GetPositionZ()))
127 {
128 _timer.Reset(200);
129 return;
130 }
131
132 if (!_path)
133 {
134 _path = std::make_unique<PathGenerator>(owner);
135 }
136 else
137 {
138 _path->Clear();
139 }
140
141 _path->SetPathLengthLimit(30.0f);
142 bool result = _path->CalculatePath(destination.GetPositionX(), destination.GetPositionY(), destination.GetPositionZ());
143 if (!result || (_path->GetPathType() & PathType(PATHFIND_NOPATH | PATHFIND_SHORTCUT | PATHFIND_FARFROMPOLY)))
144 {
145 _timer.Reset(100);
146 return;
147 }
148
149 // Same position - recheck
150 if (_path->getPathLength() < MIN_PATH_LENGTH)
151 {
152 if (_fleeTargetGUID)
153 {
155 }
156
157 _timer.Reset(100);
158 return;
159 }
160
162
163 Movement::MoveSplineInit init(owner);
164 init.MovebyPath(_path->GetPath());
165 init.SetWalk(false);
166 int32 traveltime = init.Launch();
167 _timer.Reset(traveltime + urand(800, 1500));
168}
uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:44
std::int32_t int32
Definition: Define.h:103
PathType
Definition: PathGenerator.h:46
@ PATHFIND_NOPATH
Definition: PathGenerator.h:51
@ PATHFIND_FARFROMPOLY
Definition: PathGenerator.h:56
@ PATHFIND_SHORTCUT
Definition: PathGenerator.h:49
#define MIN_PATH_LENGTH
Definition: FleeingMovementGenerator.cpp:29
@ UNIT_STATE_FLEEING_MOVE
Definition: UnitDefines.h:173
void Reset(time_t interval)
Definition: Timer.h:217
Definition: Position.h:28
float GetPositionZ() const
Definition: Position.h:119
float GetPositionX() const
Definition: Position.h:117
void GetPosition(float &x, float &y) const
Definition: Position.h:122
float GetPositionY() const
Definition: Position.h:118
void GetPoint(T *, Position &position)
Definition: FleeingMovementGenerator.cpp:171
Definition: MoveSplineInit.h:71

References Position::GetPosition(), Position::GetPositionX(), Position::GetPositionY(), Position::GetPositionZ(), Movement::MoveSplineInit::Launch(), MIN_PATH_LENGTH, Movement::MoveSplineInit::MovebyPath(), PATHFIND_FARFROMPOLY, PATHFIND_NOPATH, PATHFIND_SHORTCUT, Movement::MoveSplineInit::SetWalk(), UNIT_STATE_FLEEING_MOVE, UNIT_STATE_NOT_MOVE, and urand().

Member Data Documentation

◆ _fleeTargetGUID

template<class T >
ObjectGuid FleeingMovementGenerator< T >::_fleeTargetGUID
private

◆ _interrupt

template<class T >
bool FleeingMovementGenerator< T >::_interrupt
private

◆ _path

template<class T >
std::unique_ptr<PathGenerator> FleeingMovementGenerator< T >::_path
private

◆ _shortPathsCount

template<class T >
uint8 FleeingMovementGenerator< T >::_shortPathsCount
private

◆ _timer

template<class T >
TimeTracker FleeingMovementGenerator< T >::_timer
private