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 "MoveSpline.h"
22#include "MoveSplineInit.h"
23#include "ObjectAccessor.h"
24#include "PathGenerator.h"
25
26#define MIN_QUIET_DISTANCE 28.0f
27#define MAX_QUIET_DISTANCE 43.0f
28
38
40
45
57
64
66{
67 if (!owner || !owner->IsAlive())
68 return false;
69
71 {
73 owner->StopMoving();
74 _path = nullptr;
75 return true;
76 }
77 else
79
80 _timer.Update(diff);
81 if ((HasFlag(MOVEMENTGENERATOR_FLAG_SPEED_UPDATE_PENDING) && !owner->movespline->Finalized()) || (_timer.Passed() && owner->movespline->Finalized()))
82 {
84 SetTargetLocation(owner);
85 }
86
87 return true;
88}
89
95
96void FleeingMovementGenerator::Finalize(Unit* owner, bool active, bool movementInform)
97{
99
100 if (active)
101 {
103
104 if (owner->IsCreature())
105 {
106 if (Unit const* victim = owner->GetVictim())
107 owner->SetTarget(victim->GetGUID());
108 }
109 else if (owner->IsPlayer())
110 owner->StopMoving();
111 }
112
113 if (movementInform)
115}
116
118{
119 if (!owner || !owner->IsAlive())
120 return;
121
123 {
125 owner->StopMoving();
126 _path = nullptr;
127 return;
128 }
129
130 Position destination = owner->GetPosition();
131 GetPoint(owner, destination);
132
133 // Add LOS check for target point
134 if (!owner->IsWithinLOS(destination.GetPositionX(), destination.GetPositionY(), destination.GetPositionZ()))
135 {
136 _timer.Reset(200);
137 return;
138 }
139
140 if (!_path)
141 {
142 _path = std::make_unique<PathGenerator>(owner);
143 _path->SetPathLengthLimit(30.0f);
144 }
145
146 bool result = _path->CalculatePath(destination.GetPositionX(), destination.GetPositionY(), destination.GetPositionZ());
147 if (!result || (_path->GetPathType() & PATHFIND_NOPATH)
148 || (_path->GetPathType() & PATHFIND_SHORTCUT)
149 || (_path->GetPathType() & PATHFIND_FARFROMPOLY))
150 {
151 _timer.Reset(100);
152 return;
153 }
154
156
157 Movement::MoveSplineInit init(owner);
158 init.MovebyPath(_path->GetPath());
159 init.SetWalk(false);
160 int32 traveltime = init.Launch();
161 _timer.Reset(traveltime + urand(800, 1500));
162}
163
165{
166 float casterDistance, casterAngle;
167 if (Unit* fleeTarget = ObjectAccessor::GetUnit(*owner, _fleeTargetGUID))
168 {
169 casterDistance = fleeTarget->GetDistance(owner);
170 if (casterDistance > 0.2f)
171 casterAngle = fleeTarget->GetAbsoluteAngle(owner);
172 else
173 casterAngle = frand(0.0f, 2.0f * float(M_PI));
174 }
175 else
176 {
177 casterDistance = 0.0f;
178 casterAngle = frand(0.0f, 2.0f * float(M_PI));
179 }
180
181 float distance, angle;
182 if (casterDistance < MIN_QUIET_DISTANCE)
183 {
184 distance = frand(0.4f, 1.3f) * (MIN_QUIET_DISTANCE - casterDistance);
185 angle = casterAngle + frand(-float(M_PI) / 8.0f, float(M_PI) / 8.0f);
186 }
187 else if (casterDistance > MAX_QUIET_DISTANCE)
188 {
189 distance = frand(0.4f, 1.0f) * (MAX_QUIET_DISTANCE - MIN_QUIET_DISTANCE);
190 angle = -casterAngle + frand(-float(M_PI) / 4.0f, float(M_PI) / 4.0f);
191 }
192 else // we are inside quiet range
193 {
194 distance = frand(0.6f, 1.2f) * (MAX_QUIET_DISTANCE - MIN_QUIET_DISTANCE);
195 angle = frand(0.0f, 2.0f * float(M_PI));
196 }
197
198 owner->MovePositionToFirstCollision(position, distance, angle);
199}
200
201//---- TimedFleeingMovementGenerator
202
204{
205 if (!owner || !owner->IsAlive())
206 return false;
207
210 return false;
211
212 return FleeingMovementGenerator::Update(owner, diff);
213}
214
215void TimedFleeingMovementGenerator::Finalize(Unit* owner, bool active, bool movementInform)
216{
218 if (!active)
219 return;
220
221 owner->StopMoving();
222 if (owner->IsCreature() && owner->IsAlive())
223 {
224 if (Unit* victim = owner->GetVictim())
225 {
226 owner->AttackStop();
227 owner->GetAI()->AttackStart(victim);
228 }
229 }
230
231 if (movementInform)
232 {
234
235 Creature* ownerCreature = owner->ToCreature();
236 if (CreatureAI* AI = ownerCreature ? ownerCreature->AI() : nullptr)
237 AI->MovementInform(TIMED_FLEEING_MOTION_TYPE, 0);
238 }
239}
240
#define M_PI
Definition Common.h:118
int32_t int32
Definition Define.h:150
uint32_t uint32
Definition Define.h:154
#define MAX_QUIET_DISTANCE
#define MIN_QUIET_DISTANCE
@ MOTION_MODE_DEFAULT
@ 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_STATE_NOT_MOVE
Definition Unit.h:306
@ UNIT_STATE_FLEEING_MOVE
Definition Unit.h:286
@ UNIT_STATE_FLEEING
Definition Unit.h:268
bool IsCreature() const
Definition BaseEntity.h:172
bool IsPlayer() const
Definition BaseEntity.h:173
CreatureAI * AI() const
Definition Creature.h:228
void GetPoint(Unit *owner, Position &position) const
void Deactivate(Unit *owner) override
void Reset(Unit *owner) override
bool Update(Unit *owner, uint32 diff) override
std::unique_ptr< PathGenerator > _path
void Initialize(Unit *owner) override
FleeingMovementGenerator(ObjectGuid fleeTargetGUID, Scripting::v2::ActionResultSetter< MovementStopReason > &&scriptResult={})
MovementGeneratorType GetMovementGeneratorType() const override
void Finalize(Unit *owner, bool, bool) override
void SetScriptResult(MovementStopReason reason)
void AddFlag(uint16 const flag)
Scripting::v2::ActionResultSetter< MovementStopReason > ScriptResult
bool HasFlag(uint16 const flag) const
void RemoveFlag(uint16 const flag)
void SetWalk(bool enable)
void MovebyPath(std::span< Vector3 const > path, int32 pointId=0)
Creature * ToCreature()
Definition Object.h:121
bool Update(Unit *, uint32) override
MovementGeneratorType GetMovementGeneratorType() const override
void Finalize(Unit *, bool, bool) override
virtual void AttackStart(Unit *victim)
Definition UnitAI.cpp:29
Definition Unit.h:635
void ClearUnitState(uint32 f)
Definition Unit.h:744
virtual bool IsMovementPreventedByCasting() const
Definition Unit.cpp:3261
bool IsAlive() const
Definition Unit.h:1185
void StopMoving()
Definition Unit.cpp:10680
UnitAI * GetAI() const
Definition Unit.h:668
void AddUnitState(uint32 f)
Definition Unit.h:742
virtual void SetTarget(ObjectGuid const &)=0
Unit * GetVictim() const
Definition Unit.h:726
bool HasUnitState(const uint32 f) const
Definition Unit.h:743
std::unique_ptr< Movement::MoveSpline > movespline
Definition Unit.h:1838
bool AttackStop()
Definition Unit.cpp:5965
bool IsWithinLOS(float x, float y, float z, LineOfSightChecks checks=LINEOFSIGHT_ALL_CHECKS, VMAP::ModelIgnoreFlags ignoreFlags=VMAP::ModelIgnoreFlags::Nothing) const
Definition Object.cpp:515
void MovePositionToFirstCollision(Position &pos, float dist, float angle) const
Definition Object.cpp:2828
TC_GAME_API Unit * GetUnit(WorldObject const &, ObjectGuid const &guid)
constexpr float GetPositionX() const
Definition Position.h:87
constexpr float GetPositionY() const
Definition Position.h:88
constexpr void GetPosition(float &x, float &y) const
Definition Position.h:92
constexpr float GetPositionZ() const
Definition Position.h:89
void Update(int32 diff)
Definition Timer.h:121
bool Passed() const
Definition Timer.h:131
void Reset(int32 expiry)
Definition Timer.h:136