Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
FleeingMovementGenerator.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#include "Creature.h"
7#include "CreatureAI.h"
9#include "MapManager.h"
10#include "MoveSpline.h"
11#include "MoveSplineInit.h"
12#include "ObjectAccessor.h"
13#include "PathGenerator.h"
14#include "Player.h"
15
16#define MIN_QUIET_DISTANCE 28.0f
17#define MAX_QUIET_DISTANCE 43.0f
18
19template<class T>
21{
22 if (!owner)
23 return;
24
25 if (owner->HasUnitState(UNIT_STATE_ROOT | UNIT_STATE_STUNNED))
26 return;
27
28 owner->AddUnitState(UNIT_STATE_FLEEING_MOVE);
29
30 float x, y, z;
31 _getPoint(owner, x, y, z);
32
33 PathGenerator path(owner);
34 path.SetPathLengthLimit(30.0f);
35 bool result = path.CalculatePath(x, y, z);
36 if (!result || (path.GetPathType() & PATHFIND_NOPATH))
37 {
38 i_nextCheckTime.Reset(100);
39 return;
40 }
41
42 Movement::MoveSplineInit init(owner);
43 init.MovebyPath(path.GetPath());
44 init.SetWalk(false);
45 int32 traveltime = init.Launch();
46 i_nextCheckTime.Reset(traveltime + (std::rand() % 1500 + 800));
47}
48
49template<class T>
50void FleeingMovementGenerator<T>::_getPoint(T* owner, float& x, float& y, float& z)
51{
52 float dist_from_caster, angle_to_caster;
53 if (Unit* fright = ObjectAccessor::GetUnit(*owner, i_frightGUID))
54 {
55 dist_from_caster = fright->GetDistance(owner);
56 if (dist_from_caster > 0.2f)
57 angle_to_caster = fright->GetAngle(owner);
58 else
59 angle_to_caster = frand(0, 2 * static_cast<float>(M_PI));
60 }
61 else
62 {
63 dist_from_caster = 0.0f;
64 angle_to_caster = frand(0, 2 * static_cast<float>(M_PI));
65 }
66
67 float dist, angle;
68 if (dist_from_caster < MIN_QUIET_DISTANCE)
69 {
70 dist = frand(0.4f, 1.3f) * (MIN_QUIET_DISTANCE - dist_from_caster);
71 angle = angle_to_caster + frand(-static_cast<float>(M_PI) / 8, static_cast<float>(M_PI) / 8);
72 }
73 else if (dist_from_caster > MAX_QUIET_DISTANCE)
74 {
75 dist = frand(0.4f, 1.0f) * (MAX_QUIET_DISTANCE - MIN_QUIET_DISTANCE);
76 angle = -angle_to_caster + frand(-static_cast<float>(M_PI) / 4, static_cast<float>(M_PI) / 4);
77 }
78 else // we are inside quiet range
79 {
80 dist = frand(0.6f, 1.2f) * (MAX_QUIET_DISTANCE - MIN_QUIET_DISTANCE);
81 angle = frand(0, 2 * static_cast<float>(M_PI));
82 }
83
84 Position pos;
85 owner->GetFirstCollisionPosition(pos, dist, angle);
86 x = pos.m_positionX;
87 y = pos.m_positionY;
88 z = pos.m_positionZ;
89}
90
91template<class T>
93{
94 if (!owner)
95 return;
96
97 owner->SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_FLEEING);
98 owner->AddUnitState(UNIT_STATE_FLEEING | UNIT_STATE_FLEEING_MOVE);
99 _setTargetLocation(owner);
100}
101
102template<>
109
110template<>
118
119template<class T>
121{
122 DoInitialize(owner);
123}
124
125template<class T>
127{
128 if (!owner || !owner->IsAlive())
129 return false;
130
131 if (owner->HasUnitState(UNIT_STATE_ROOT | UNIT_STATE_STUNNED))
132 {
133 owner->ClearUnitState(UNIT_STATE_FLEEING_MOVE);
134 return true;
135 }
136
137 i_nextCheckTime.Update(time_diff);
138 if (i_nextCheckTime.Passed() && owner->movespline->Finalized())
139 _setTargetLocation(owner);
140
141 return true;
142}
143
146template void FleeingMovementGenerator<Player>::_getPoint(Player*, float&, float&, float&);
147template void FleeingMovementGenerator<Creature>::_getPoint(Creature*, float&, float&, float&);
154
156{
159 if (Unit* victim = owner->GetVictim())
160 {
161 if (owner->IsAlive())
162 {
163 owner->AttackStop();
164 owner->ToCreature()->AI()->AttackStart(victim);
165 }
166 }
167}
168
170{
171 if (!owner->IsAlive())
172 return false;
173
175 {
177 return true;
178 }
179
180 i_totalFleeTime.Update(time_diff);
181 if (i_totalFleeTime.Passed())
182 return false;
183
184 // This calls grant-parent Update method hiden by FleeingMovementGenerator::Update(Creature &, uint32) version
185 // This is done instead of casting Unit& to Creature& and call parent method, then we can use Unit directly
187}
#define M_PI
Definition Common.h:192
std::int32_t int32
Definition Define.h:73
std::uint32_t uint32
Definition Define.h:77
#define MAX_QUIET_DISTANCE
#define MIN_QUIET_DISTANCE
@ PATHFIND_NOPATH
@ UNIT_STATE_FLEEING_MOVE
Definition Unit.h:536
@ UNIT_STATE_ROOT
Definition Unit.h:522
@ UNIT_STATE_FLEEING
Definition Unit.h:519
@ UNIT_STATE_STUNNED
Definition Unit.h:515
@ UNIT_FLAG_FLEEING
Definition Unit.h:648
@ UNIT_FIELD_FLAGS
float frand(float min, float max)
Definition Util.cpp:39
void SetTarget(uint64 guid) OVERRIDE
CreatureAI * AI() const
Definition Creature.h:483
void _getPoint(Creature *, float &x, float &y, float &z)
static Unit * GetUnit(WorldObject const &, uint64 guid)
uint64 GetGUID() const
Definition Object.h:119
void RemoveFlag(uint16 index, uint32 oldFlag)
Definition Object.cpp:1280
Creature * ToCreature()
Definition Object.h:207
virtual void AttackStart(Unit *)
Definition UnitAI.cpp:16
Definition Unit.h:1367
void ClearUnitState(uint32 f)
Definition Unit.h:1489
bool IsAlive() const
Definition Unit.h:2032
void StopMoving()
-------—End of Pet responses methods-------—
Definition Unit.cpp:5877
Unit * GetVictim() const
Definition Unit.h:1468
bool HasUnitState(const uint32 f) const
Definition Unit.h:1485
bool AttackStop()
float m_positionZ
Definition Object.h:289
float m_positionX
Definition Object.h:287
float m_positionY
Definition Object.h:288