TrinityCore
Loading...
Searching...
No Matches
FleeingMovementGenerator.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
19#include "Creature.h"
20#include "CreatureAI.h"
21#include "MovementDefines.h"
22#include "MoveSpline.h"
23#include "MoveSplineInit.h"
24#include "ObjectAccessor.h"
25#include "PathGenerator.h"
26#include "Player.h"
27#include "Unit.h"
28
29#define MIN_QUIET_DISTANCE 28.0f
30#define MAX_QUIET_DISTANCE 43.0f
31
32template<class T>
33FleeingMovementGenerator<T>::FleeingMovementGenerator(ObjectGuid fleeTargetGUID) : _fleeTargetGUID(fleeTargetGUID), _timer(0)
34{
40template<class T>
42{
44}
45
46template<class T>
48{
51
52 if (!owner || !owner->IsAlive())
53 return false;
54
55 // TODO: UNIT_FIELD_FLAGS should not be handled by generators
56 owner->SetUnitFlag(UNIT_FLAG_FLEEING);
57
58 _path = nullptr;
59 SetTargetLocation(owner);
60 return true;
61}
62
63template<class T>
70
71template<class T>
73{
74 if (!owner || !owner->IsAlive())
75 return false;
76
77 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || owner->IsMovementPreventedByCasting())
78 {
80 owner->StopMoving();
81 _path = nullptr;
82 return true;
83 }
84 else
86
87 _timer.Update(diff);
88 if ((MovementGenerator::HasFlag(MOVEMENTGENERATOR_FLAG_SPEED_UPDATE_PENDING) && !owner->movespline->Finalized()) || (_timer.Passed() && owner->movespline->Finalized()))
89 {
91 SetTargetLocation(owner);
92 }
93
94 return true;
95}
96
97template<class T>
103
104template<class T>
106{
107}
108
109template<>
110void FleeingMovementGenerator<Player>::DoFinalize(Player* owner, bool active, bool/* movementInform*/)
111{
113
114 if (active)
115 {
118 owner->StopMoving();
119 }
120}
121
122template<>
123void FleeingMovementGenerator<Creature>::DoFinalize(Creature* owner, bool active, bool/* movementInform*/)
124{
126
127 if (active)
128 {
131 if (owner->GetVictim())
132 owner->SetTarget(owner->EnsureVictim()->GetGUID());
133 }
134}
135
136template<class T>
138{
139 if (!owner || !owner->IsAlive())
140 return;
141
142 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || owner->IsMovementPreventedByCasting())
143 {
145 owner->StopMoving();
146 _path = nullptr;
147 return;
148 }
149
150 Position destination = owner->GetPosition();
151 GetPoint(owner, destination);
152
153 // Add LOS check for target point
154 if (!owner->IsWithinLOS(destination.GetPositionX(), destination.GetPositionY(), destination.GetPositionZ()))
155 {
156 _timer.Reset(200);
157 return;
158 }
159
160 if (!_path)
161 {
162 _path = std::make_unique<PathGenerator>(owner);
163 _path->SetPathLengthLimit(30.0f);
164 }
165
166 bool result = _path->CalculatePath(destination.GetPositionX(), destination.GetPositionY(), destination.GetPositionZ());
167 if (!result || (_path->GetPathType() & PATHFIND_NOPATH)
168 || (_path->GetPathType() & PATHFIND_SHORTCUT)
169 || (_path->GetPathType() & PATHFIND_FARFROMPOLY))
170 {
171 _timer.Reset(100);
172 return;
173 }
174
175 owner->AddUnitState(UNIT_STATE_FLEEING_MOVE);
176
177 Movement::MoveSplineInit init(owner);
178 init.MovebyPath(_path->GetPath());
179 init.SetWalk(false);
180 int32 traveltime = init.Launch();
181 _timer.Reset(traveltime + urand(800, 1500));
182}
183
184template<class T>
186{
187 float casterDistance, casterAngle;
188 if (Unit* fleeTarget = ObjectAccessor::GetUnit(*owner, _fleeTargetGUID))
189 {
190 casterDistance = fleeTarget->GetDistance(owner);
191 if (casterDistance > 0.2f)
192 casterAngle = fleeTarget->GetAbsoluteAngle(owner);
193 else
194 casterAngle = frand(0.0f, 2.0f * float(M_PI));
195 }
196 else
197 {
198 casterDistance = 0.0f;
199 casterAngle = frand(0.0f, 2.0f * float(M_PI));
200 }
201
202 float distance, angle;
203 if (casterDistance < MIN_QUIET_DISTANCE)
204 {
205 distance = frand(0.4f, 1.3f) * (MIN_QUIET_DISTANCE - casterDistance);
206 angle = casterAngle + frand(-float(M_PI) / 8.0f, float(M_PI) / 8.0f);
207 }
208 else if (casterDistance > MAX_QUIET_DISTANCE)
209 {
210 distance = frand(0.4f, 1.0f) * (MAX_QUIET_DISTANCE - MIN_QUIET_DISTANCE);
211 angle = -casterAngle + frand(-float(M_PI) / 4.0f, float(M_PI) / 4.0f);
212 }
213 else // we are inside quiet range
214 {
215 distance = frand(0.6f, 1.2f) * (MAX_QUIET_DISTANCE - MIN_QUIET_DISTANCE);
216 angle = frand(0.0f, 2.0f * float(M_PI));
217 }
218
219 owner->MovePositionToFirstCollision(position, distance, angle);
220}
221
238
239//---- TimedFleeingMovementGenerator
240
242{
243 if (!owner || !owner->IsAlive())
244 return false;
245
248 return false;
249
251}
252
253void TimedFleeingMovementGenerator::Finalize(Unit* owner, bool active, bool movementInform)
254{
255 FleeingMovementGenerator<Creature>::DoFinalize(static_cast<Creature*>(owner), active, movementInform);
256
257 if (active)
258 owner->StopMoving();
259
260 if (movementInform)
261 {
262 Creature* ownerCreature = owner->ToCreature();
263 if (CreatureAI* AI = ownerCreature ? ownerCreature->AI() : nullptr)
264 AI->MovementInform(TIMED_FLEEING_MOTION_TYPE, 0);
265 }
266}
267
#define M_PI
Definition Common.h:72
int32_t int32
Definition Define.h:129
uint32_t uint32
Definition Define.h:133
#define MAX_QUIET_DISTANCE
#define MIN_QUIET_DISTANCE
@ MOTION_PRIORITY_HIGHEST
MovementGeneratorType
@ TIMED_FLEEING_MOTION_TYPE
@ FLEEING_MOTION_TYPE
@ MOVEMENTGENERATOR_FLAG_INITIALIZATION_PENDING
@ MOVEMENTGENERATOR_FLAG_DEACTIVATED
@ MOVEMENTGENERATOR_FLAG_FINALIZED
@ MOVEMENTGENERATOR_FLAG_TRANSITORY
@ MOVEMENTGENERATOR_FLAG_INTERRUPTED
@ MOVEMENTGENERATOR_FLAG_INITIALIZED
@ MOVEMENTGENERATOR_FLAG_SPEED_UPDATE_PENDING
@ PATHFIND_NOPATH
@ PATHFIND_FARFROMPOLY
@ PATHFIND_SHORTCUT
float frand(float min, float max)
Definition Random.cpp:55
uint32 urand(uint32 min, uint32 max)
Definition Random.cpp:42
@ UNIT_FLAG_FLEEING
@ UNIT_STATE_NOT_MOVE
Definition Unit.h:265
@ UNIT_STATE_FLEEING_MOVE
Definition Unit.h:245
@ UNIT_STATE_FLEEING
Definition Unit.h:227
void SetTarget(ObjectGuid guid) override
CreatureAI * AI() const
Definition Creature.h:154
void GetPoint(T *, Position &position)
FleeingMovementGenerator(ObjectGuid fleeTargetGUID)
MovementGeneratorType GetMovementGeneratorType() const override
void AddFlag(uint16 const flag)
bool HasFlag(uint16 const flag) const
void RemoveFlag(uint16 const flag)
void SetWalk(bool enable)
void MovebyPath(PointsArray const &path, int32 pointId=0)
static Creature * ToCreature(Object *o)
Definition Object.h:186
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:78
bool Update(Unit *, uint32) override
MovementGeneratorType GetMovementGeneratorType() const override
void Finalize(Unit *, bool, bool) override
Definition Unit.h:769
void ClearUnitState(uint32 f)
Definition Unit.h:877
void StopMoving(bool force=false)
Definition Unit.cpp:10312
bool IsAlive() const
Definition Unit.h:1234
Unit * EnsureVictim() const
Definition Unit.h:861
Unit * GetVictim() const
Definition Unit.h:859
void RemoveUnitFlag(UnitFlags flags)
Definition Unit.h:955
TC_GAME_API Unit * GetUnit(WorldObject const &, ObjectGuid const &guid)
float GetPositionZ() const
Definition Position.h:81
float GetPositionX() const
Definition Position.h:79
void GetPosition(float &x, float &y) const
Definition Position.h:84
float GetPositionY() const
Definition Position.h:80
void Update(int32 diff)
Definition Timer.h:121
bool Passed() const
Definition Timer.h:131