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
28static bool HasLostTarget(Unit* owner, Unit* target)
29{
30 return owner->GetVictim() != target;
31}
32
33static bool IsMutualChase(Unit* owner, Unit* target)
34{
36 return false;
37
39 return movement->GetTarget() == owner;
40
41 return false;
42}
43
44static bool PositionOkay(Unit* owner, Unit* target, Optional<float> minDistance, Optional<float> maxDistance, Optional<ChaseAngle> angle)
45{
46 if (minDistance && owner->IsInDist(target, *minDistance))
47 return false;
48 if (maxDistance && !owner->IsInDist(target, *maxDistance))
49 return false;
50 if (angle && !angle->IsAngleOkay(target->GetRelativeAngle(owner)))
51 return false;
52 if (!owner->IsWithinLOSInMap(target))
53 return false;
54 return true;
55}
56
57static void DoMovementInform(Unit* owner, Unit* target)
58{
59 if (owner->GetTypeId() != TYPEID_UNIT)
60 return;
61
62 if (CreatureAI* AI = owner->ToCreature()->AI())
63 AI->MovementInform(CHASE_MOTION_TYPE, target->GetGUID().GetCounter());
64}
65
75
84
91
93{
94 // owner might be dead or gone (can we even get nullptr here?)
95 if (!owner || !owner->IsAlive())
96 return false;
97
98 // our target might have gone away
99 Unit* const target = GetTarget();
100 if (!target || !target->IsInWorld())
101 return false;
102
103 // the owner might be unable to move (rooted or casting), or we have lost the target, pause movement
104 if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || owner->IsMovementPreventedByCasting() || HasLostTarget(owner, target))
105 {
106 owner->StopMoving();
107 _lastTargetPosition.reset();
108 if (Creature* cOwner = owner->ToCreature())
109 cOwner->SetCannotReachTarget(false);
110 return true;
111 }
112
113 bool const mutualChase = IsMutualChase(owner, target);
114 float const hitboxSum = owner->GetCombatReach() + target->GetCombatReach();
115 float minRange = _range ? _range->MinRange + hitboxSum : CONTACT_DISTANCE;
116 float minTarget = (_range ? _range->MinTolerance : 0.0f) + hitboxSum;
117 float maxRange = _range ? _range->MaxRange + hitboxSum : owner->GetMeleeRange(target); // melee range already includes hitboxes
118 float maxTarget = _range ? _range->MaxTolerance + hitboxSum : CONTACT_DISTANCE + hitboxSum;
119 Optional<ChaseAngle> angle = mutualChase ? Optional<ChaseAngle>() : _angle;
120
121 if (Creature* cOwner = owner->ToCreature())
122 if (cOwner->IsIgnoringChaseRange())
123 minRange = minTarget = maxRange = maxTarget = 0.0f;
124
125 // periodically check if we're already in the expected range...
128 {
130 if (HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED) && PositionOkay(owner, target, _movingTowards ? Optional<float>() : minTarget, _movingTowards ? maxTarget : Optional<float>(), angle))
131 {
133 _path = nullptr;
134 if (Creature* cOwner = owner->ToCreature())
135 cOwner->SetCannotReachTarget(false);
136 owner->StopMoving();
137 owner->SetInFront(target);
138 DoMovementInform(owner, target);
139 return true;
140 }
141 }
142
143 // if we're done moving, we want to clean up
144 if (owner->HasUnitState(UNIT_STATE_CHASE_MOVE) && owner->movespline->Finalized())
145 {
147 _path = nullptr;
148 if (Creature* cOwner = owner->ToCreature())
149 cOwner->SetCannotReachTarget(false);
151 owner->SetInFront(target);
152 DoMovementInform(owner, target);
153 }
154
155 // if the target moved, we have to consider whether to adjust
156 if (!_lastTargetPosition || target->GetPosition() != _lastTargetPosition.value() || mutualChase != _mutualChase)
157 {
159 _mutualChase = mutualChase;
160 if (owner->HasUnitState(UNIT_STATE_CHASE_MOVE) || !PositionOkay(owner, target, minRange, maxRange, angle))
161 {
162 Creature* const cOwner = owner->ToCreature();
163 // can we get to the target?
164 if (cOwner && !target->isInAccessiblePlaceFor(cOwner))
165 {
166 cOwner->SetCannotReachTarget(true);
167 cOwner->StopMoving();
168 _path = nullptr;
169 return true;
170 }
171
172 // figure out which way we want to move
173 bool const moveToward = !owner->IsInDist(target, maxRange);
174
175 // make a new path if we have to...
176 if (!_path || moveToward != _movingTowards)
177 _path = std::make_unique<PathGenerator>(owner);
178
179 float x, y, z;
180 bool shortenPath;
181 // if we want to move toward the target and there's no fixed angle...
182 if (moveToward && !angle)
183 {
184 // ...we'll pathfind to the center, then shorten the path
185 target->GetPosition(x, y, z);
186 shortenPath = true;
187 }
188 else
189 {
190 // otherwise, we fall back to nearpoint finding
191 target->GetNearPoint(owner, x, y, z, (moveToward ? maxTarget : minTarget) - hitboxSum, angle ? target->ToAbsoluteAngle(angle->RelativeAngle) : target->GetAbsoluteAngle(owner));
192 shortenPath = false;
193 }
194
195 if (owner->IsHovering())
196 owner->UpdateAllowedPositionZ(x, y, z);
197
198 bool success = _path->CalculatePath(x, y, z, owner->CanFly());
199 if (!success || (_path->GetPathType() & (PATHFIND_NOPATH /* | PATHFIND_INCOMPLETE*/)))
200 {
201 if (cOwner)
202 cOwner->SetCannotReachTarget(true);
203 owner->StopMoving();
204 return true;
205 }
206
207 if (shortenPath)
208 _path->ShortenPathUntilDist(PositionToVector3(target->GetPosition()), maxTarget);
209
210 if (cOwner)
211 cOwner->SetCannotReachTarget(false);
212
213 bool walk = false;
214 if (cOwner && !cOwner->IsPet())
215 {
216 switch (cOwner->GetMovementTemplate().GetChase())
217 {
219 walk = owner->IsWalking();
220 break;
222 walk = true;
223 break;
224 default:
225 break;
226 }
227 }
228
231
232 Movement::MoveSplineInit init(owner);
233 init.MovebyPath(_path->GetPath());
234 init.SetWalk(walk);
235 init.SetFacing(target);
236 init.Launch();
237 }
238 }
239
240 // and then, finally, we're done for the tick
241 return true;
242}
243
252
253void ChaseMovementGenerator::Finalize(Unit* owner, bool active, bool/* movementInform*/)
254{
256 if (active)
257 {
259 if (Creature* cOwner = owner->ToCreature())
260 cOwner->SetCannotReachTarget(false);
261 }
262}
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:154
#define ASSERT_NOTNULL(pointer)
Definition Errors.h:82
@ MOTION_MODE_DEFAULT
@ 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:43
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:306
@ UNIT_STATE_CHASE
Definition Unit.h:266
@ UNIT_STATE_CHASE_MOVE
Definition Unit.h:287
ObjectGuid const & GetGUID() const
Definition BaseEntity.h:163
bool IsInWorld() const
Definition BaseEntity.h:158
TypeID GetTypeId() const
Definition BaseEntity.h:166
ChaseMovementGenerator(Unit *target, Optional< ChaseRange > range={}, Optional< ChaseAngle > angle={})
Optional< Position > _lastTargetPosition
void 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:228
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 MovebyPath(std::span< Vector3 const > path, int32 pointId=0)
void SetFacing(float angle)
LowType GetCounter() const
Definition ObjectGuid.h:336
Creature * ToCreature()
Definition Object.h:121
Definition Unit.h:635
void ClearUnitState(uint32 f)
Definition Unit.h:744
virtual bool IsMovementPreventedByCasting() const
Definition Unit.cpp:3261
virtual bool CanFly() const =0
MotionMaster * GetMotionMaster()
Definition Unit.h:1723
bool IsPet() const
Definition Unit.h:751
bool IsAlive() const
Definition Unit.h:1185
float GetCombatReach() const override
Definition Unit.h:705
void StopMoving()
Definition Unit.cpp:10680
void AddUnitState(uint32 f)
Definition Unit.h:742
bool isInAccessiblePlaceFor(Creature const *c) const
Definition Unit.cpp:3310
bool IsHovering() const
Definition Unit.h:1151
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
float GetMeleeRange(Unit const *target) const
Definition Unit.cpp:701
void SetInFront(WorldObject const *target)
Definition Unit.cpp:13283
ObjectGuid GetTarget() const
Definition Unit.h:1831
bool IsWalking() const
Definition Unit.h:1150
void UpdateAllowedPositionZ(float x, float y, float &z, float *groundZ=nullptr) const
Definition Object.cpp:711
void GetNearPoint(WorldObject const *searcher, float &x, float &y, float &z, float distance2d, float absAngle) const
Definition Object.cpp:2714
bool IsWithinLOSInMap(WorldObject const *obj, LineOfSightChecks checks=LINEOFSIGHT_ALL_CHECKS, VMAP::ModelIgnoreFlags ignoreFlags=VMAP::ModelIgnoreFlags::Nothing) const
Definition Object.cpp:535
Unit * GetTarget() const
CreatureChaseMovementType GetChase() const
float GetRelativeAngle(float x, float y) const
Definition Position.h:147
float ToAbsoluteAngle(float relAngle) const
Definition Position.h:144
float GetAbsoluteAngle(float x, float y) const
Definition Position.h:136
constexpr void GetPosition(float &x, float &y) const
Definition Position.h:92
constexpr bool IsInDist(float x, float y, float z, float dist) const
Definition Position.h:155
void Update(int32 diff)
Definition Timer.h:121
bool Passed() const
Definition Timer.h:131
void Reset(int32 expiry)
Definition Timer.h:136