TrinityCore
Loading...
Searching...
No Matches
ChaseMovementGenerator.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 "G3DPosition.hpp"
22#include "MotionMaster.h"
23#include "MoveSpline.h"
24#include "MoveSplineInit.h"
25#include "PathGenerator.h"
26#include "Unit.h"
27#include "Util.h"
28
29static bool HasLostTarget(Unit* owner, Unit* target)
30{
31 return owner->GetVictim() != target;
32}
33
34static bool IsMutualChase(Unit* owner, Unit* target)
35{
37 return false;
38
40 return movement->GetTarget() == owner;
41
42 return false;
43}
44
45static bool PositionOkay(Unit* owner, Unit* target, Optional<float> minDistance, Optional<float> maxDistance, Optional<ChaseAngle> angle)
46{
47 float const distSq = owner->GetExactDistSq(target);
48 if (minDistance && distSq < square(*minDistance))
49 return false;
50 if (maxDistance && distSq > square(*maxDistance))
51 return false;
52 if (angle && !angle->IsAngleOkay(target->GetRelativeAngle(owner)))
53 return false;
54 if (!owner->IsWithinLOSInMap(target))
55 return false;
56 return true;
57}
58
59static void DoMovementInform(Unit* owner, Unit* target)
60{
61 if (owner->GetTypeId() != TYPEID_UNIT)
62 return;
63
64 if (CreatureAI* AI = owner->ToCreature()->AI())
65 AI->MovementInform(CHASE_MOTION_TYPE, target->GetGUID().GetCounter());
66}
67
76
86
93
95{
96 // owner might be dead or gone (can we even get nullptr here?)
97 if (!owner || !owner->IsAlive())
98 return false;
99
100 // our target might have gone away
101 Unit* const target = GetTarget();
102 if (!target || !target->IsInWorld())
103 return false;
104
105 // the owner might be unable to move (rooted or casting), or we have lost the target, pause movement
106 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || owner->IsMovementPreventedByCasting() || HasLostTarget(owner, target))
107 {
108 owner->StopMoving();
109 _lastTargetPosition.reset();
110 if (Creature* cOwner = owner->ToCreature())
111 cOwner->SetCannotReachTarget(false);
112 return true;
113 }
114
115 bool const mutualChase = IsMutualChase(owner, target);
116 float const hitboxSum = owner->GetCombatReach() + target->GetCombatReach();
117 float const minRange = _range ? _range->MinRange + hitboxSum : CONTACT_DISTANCE;
118 float const minTarget = (_range ? _range->MinTolerance : 0.0f) + hitboxSum;
119 float const maxRange = _range ? _range->MaxRange + hitboxSum : owner->GetMeleeRange(target); // melee range already includes hitboxes
120 float const maxTarget = _range ? _range->MaxTolerance + hitboxSum : CONTACT_DISTANCE + hitboxSum;
121 Optional<ChaseAngle> angle = mutualChase ? Optional<ChaseAngle>() : _angle;
122
123 // periodically check if we're already in the expected range...
126 {
128 if (HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED) && PositionOkay(owner, target, _movingTowards ? Optional<float>() : minTarget, _movingTowards ? maxTarget : Optional<float>(), angle))
129 {
131 _path = nullptr;
132 if (Creature* cOwner = owner->ToCreature())
133 cOwner->SetCannotReachTarget(false);
134 owner->StopMoving();
135 owner->SetInFront(target);
136 DoMovementInform(owner, target);
137 return true;
138 }
139 }
140
141 // if we're done moving, we want to clean up
143 {
145 _path = nullptr;
146 if (Creature* cOwner = owner->ToCreature())
147 cOwner->SetCannotReachTarget(false);
149 owner->SetInFront(target);
150 DoMovementInform(owner, target);
151 }
152
153 // if the target moved, we have to consider whether to adjust
154 if (!_lastTargetPosition || target->GetPosition() != _lastTargetPosition.value() || mutualChase != _mutualChase)
155 {
157 _mutualChase = mutualChase;
158 if (owner->HasUnitState(UNIT_STATE_CHASE_MOVE) || !PositionOkay(owner, target, minRange, maxRange, angle))
159 {
160 Creature* const cOwner = owner->ToCreature();
161 // can we get to the target?
162 if (cOwner && !target->isInAccessiblePlaceFor(cOwner))
163 {
164 cOwner->SetCannotReachTarget(true);
165 cOwner->StopMoving();
166 _path = nullptr;
167 return true;
168 }
169
170 // figure out which way we want to move
171 bool const moveToward = !owner->IsInDist(target, maxRange);
172
173 // make a new path if we have to...
174 if (!_path || moveToward != _movingTowards)
175 _path = std::make_unique<PathGenerator>(owner);
176
177 float x, y, z;
178 bool shortenPath;
179 // if we want to move toward the target and there's no fixed angle...
180 if (moveToward && !angle)
181 {
182 // ...we'll pathfind to the center, then shorten the path
183 target->GetPosition(x, y, z);
184 shortenPath = true;
185 }
186 else
187 {
188 // otherwise, we fall back to nearpoint finding
189 target->GetNearPoint(owner, x, y, z, (moveToward ? maxTarget : minTarget) - hitboxSum, angle ? target->ToAbsoluteAngle(angle->RelativeAngle) : target->GetAbsoluteAngle(owner));
190 shortenPath = false;
191 }
192
193 if (owner->IsHovering())
194 owner->UpdateAllowedPositionZ(x, y, z);
195
196 bool success = _path->CalculatePath(x, y, z, owner->CanFly());
197 if (!success || (_path->GetPathType() & (PATHFIND_NOPATH /* | PATHFIND_INCOMPLETE*/)))
198 {
199 if (cOwner)
200 cOwner->SetCannotReachTarget(true);
201 owner->StopMoving();
202 return true;
203 }
204
205 if (shortenPath)
206 _path->ShortenPathUntilDist(PositionToVector3(target), maxTarget);
207
208 if (cOwner)
209 cOwner->SetCannotReachTarget(false);
210
211 bool walk = false;
212 if (cOwner && !cOwner->IsPet())
213 {
214 switch (cOwner->GetMovementTemplate().GetChase())
215 {
217 walk = owner->IsWalking();
218 break;
220 walk = true;
221 break;
222 default:
223 break;
224 }
225 }
226
229
230 Movement::MoveSplineInit init(owner);
231 init.MovebyPath(_path->GetPath());
232 init.SetWalk(walk);
233 init.SetFacing(target);
234 init.Launch();
235 }
236 }
237
238 // and then, finally, we're done for the tick
239 return true;
240}
241
250
251void ChaseMovementGenerator::Finalize(Unit* owner, bool active, bool/* movementInform*/)
252{
254 if (active)
255 {
257 if (Creature* cOwner = owner->ToCreature())
258 cOwner->SetCannotReachTarget(false);
259 }
260}
static bool PositionOkay(Unit *owner, Unit *target, Optional< float > minDistance, Optional< float > maxDistance, Optional< ChaseAngle > angle)
static bool HasLostTarget(Unit *owner, Unit *target)
static bool IsMutualChase(Unit *owner, Unit *target)
static void DoMovementInform(Unit *owner, Unit *target)
uint32_t uint32
Definition Define.h:133
#define ASSERT_NOTNULL(pointer)
Definition Errors.h:84
@ MOTION_PRIORITY_NORMAL
@ CHASE_MOTION_TYPE
@ MOVEMENTGENERATOR_FLAG_INITIALIZATION_PENDING
@ MOVEMENTGENERATOR_FLAG_DEACTIVATED
@ MOVEMENTGENERATOR_FLAG_FINALIZED
@ MOVEMENTGENERATOR_FLAG_TRANSITORY
@ MOVEMENTGENERATOR_FLAG_INFORM_ENABLED
@ MOVEMENTGENERATOR_FLAG_INITIALIZED
#define CONTACT_DISTANCE
@ TYPEID_UNIT
Definition ObjectGuid.h:38
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
@ PATHFIND_NOPATH
@ UNIT_STATE_NOT_MOVE
Definition Unit.h:265
@ UNIT_STATE_CHASE
Definition Unit.h:225
@ UNIT_STATE_CHASE_MOVE
Definition Unit.h:246
T square(T x)
Definition Util.h:95
ChaseMovementGenerator(Unit *target, Optional< ChaseRange > range={}, Optional< ChaseAngle > angle={})
Optional< Position > _lastTargetPosition
bool Initialize(Unit *) override
void Finalize(Unit *, bool, bool) override
Optional< ChaseRange > const _range
std::unique_ptr< PathGenerator > _path
bool Update(Unit *, uint32) override
void Deactivate(Unit *) override
Optional< ChaseAngle > const _angle
static constexpr uint32 RANGE_CHECK_INTERVAL
void SetCannotReachTarget(bool cannotReach)
CreatureMovementData const & GetMovementTemplate() const
CreatureAI * AI() const
Definition Creature.h:154
MovementGeneratorType GetCurrentMovementGeneratorType() const
MovementGenerator * GetCurrentMovementGenerator() const
void AddFlag(uint16 const flag)
bool HasFlag(uint16 const flag) const
void RemoveFlag(uint16 const flag)
void SetWalk(bool enable)
void SetFacing(float angle)
void MovebyPath(PointsArray const &path, int32 pointId=0)
bool Finalized() const
Definition MoveSpline.h:121
LowType GetCounter() const
Definition ObjectGuid.h:156
static Creature * ToCreature(Object *o)
Definition Object.h:186
bool IsInWorld() const
Definition Object.h:73
TypeID GetTypeId() const
Definition Object.h:93
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:78
Definition Unit.h:769
void ClearUnitState(uint32 f)
Definition Unit.h:877
virtual bool IsMovementPreventedByCasting() const
Definition Unit.cpp:3123
Movement::MoveSpline * movespline
Definition Unit.h:1804
void StopMoving(bool force=false)
Definition Unit.cpp:10312
virtual bool CanFly() const =0
MotionMaster * GetMotionMaster()
Definition Unit.h:1667
bool IsPet() const
Definition Unit.h:884
bool IsAlive() const
Definition Unit.h:1234
float GetCombatReach() const override
Definition Unit.h:839
void AddUnitState(uint32 f)
Definition Unit.h:875
bool isInAccessiblePlaceFor(Creature const *c) const
Definition Unit.cpp:3154
bool IsHovering() const
Definition Unit.h:1220
Unit * GetVictim() const
Definition Unit.h:859
bool HasUnitState(const uint32 f) const
Definition Unit.h:876
float GetMeleeRange(Unit const *target) const
Definition Unit.cpp:634
void SetInFront(WorldObject const *target)
Definition Unit.cpp:13244
ObjectGuid GetTarget() const
Definition Unit.h:1797
bool IsWalking() const
Definition Unit.h:1219
void UpdateAllowedPositionZ(float x, float y, float &z, float *groundZ=nullptr) const
Definition Object.cpp:1416
void GetNearPoint(WorldObject const *searcher, float &x, float &y, float &z, float distance2d, float absAngle) const
Definition Object.cpp:3209
bool IsWithinLOSInMap(WorldObject const *obj, LineOfSightChecks checks=LINEOFSIGHT_ALL_CHECKS, VMAP::ModelIgnoreFlags ignoreFlags=VMAP::ModelIgnoreFlags::Nothing) const
Definition Object.cpp:1226
Unit * GetTarget() const
CreatureChaseMovementType GetChase() const
float GetRelativeAngle(float x, float y) const
Definition Position.h:139
float ToAbsoluteAngle(float relAngle) const
Definition Position.h:136
bool IsInDist(float x, float y, float z, float dist) const
Definition Position.h:148
float GetAbsoluteAngle(float x, float y) const
Definition Position.h:128
void GetPosition(float &x, float &y) const
Definition Position.h:84
float GetExactDistSq(float x, float y, float z) const
Definition Position.h:113
void Update(int32 diff)
Definition Timer.h:121
bool Passed() const
Definition Timer.h:131
void Reset(int32 expiry)
Definition Timer.h:136