TrinityCore
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
69 _angle(angle), _rangeCheckTimer(RANGE_CHECK_INTERVAL)
70{
75}
77
79{
82
83 _path = nullptr;
84 _lastTargetPosition.reset();
85}
86
88{
90
91 Initialize(owner);
92}
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 minRange = _range ? _range->MinRange + hitboxSum : CONTACT_DISTANCE;
118 float minTarget = (_range ? _range->MinTolerance : 0.0f) + hitboxSum;
119 float maxRange = _range ? _range->MaxRange + hitboxSum : owner->GetMeleeRange(target); // melee range already includes hitboxes
120 float maxTarget = _range ? _range->MaxTolerance + hitboxSum : CONTACT_DISTANCE + hitboxSum;
121 Optional<ChaseAngle> angle = mutualChase ? Optional<ChaseAngle>() : _angle;
122
123 if (Creature* cOwner = owner->ToCreature())
124 if (cOwner->IsIgnoringChaseRange())
125 minRange = minTarget = maxRange = maxTarget = 0.0f;
126
127 // periodically check if we're already in the expected range...
130 {
132 if (HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED) && PositionOkay(owner, target, _movingTowards ? Optional<float>() : minTarget, _movingTowards ? maxTarget : Optional<float>(), angle))
133 {
135 _path = nullptr;
136 if (Creature* cOwner = owner->ToCreature())
137 cOwner->SetCannotReachTarget(false);
138 owner->StopMoving();
139 owner->SetInFront(target);
140 DoMovementInform(owner, target);
141 return true;
142 }
143 }
144
145 // if we're done moving, we want to clean up
146 if (owner->HasUnitState(UNIT_STATE_CHASE_MOVE) && owner->movespline->Finalized())
147 {
149 _path = nullptr;
150 if (Creature* cOwner = owner->ToCreature())
151 cOwner->SetCannotReachTarget(false);
153 owner->SetInFront(target);
154 DoMovementInform(owner, target);
155 }
156
157 // if the target moved, we have to consider whether to adjust
158 if (!_lastTargetPosition || target->GetPosition() != _lastTargetPosition.value() || mutualChase != _mutualChase)
159 {
161 _mutualChase = mutualChase;
162 if (owner->HasUnitState(UNIT_STATE_CHASE_MOVE) || !PositionOkay(owner, target, minRange, maxRange, angle))
163 {
164 Creature* const cOwner = owner->ToCreature();
165 // can we get to the target?
166 if (cOwner && !target->isInAccessiblePlaceFor(cOwner))
167 {
168 cOwner->SetCannotReachTarget(true);
169 cOwner->StopMoving();
170 _path = nullptr;
171 return true;
172 }
173
174 // figure out which way we want to move
175 bool const moveToward = !owner->IsInDist(target, maxRange);
176
177 // make a new path if we have to...
178 if (!_path || moveToward != _movingTowards)
179 _path = std::make_unique<PathGenerator>(owner);
180
181 float x, y, z;
182 bool shortenPath;
183 // if we want to move toward the target and there's no fixed angle...
184 if (moveToward && !angle)
185 {
186 // ...we'll pathfind to the center, then shorten the path
187 target->GetPosition(x, y, z);
188 shortenPath = true;
189 }
190 else
191 {
192 // otherwise, we fall back to nearpoint finding
193 target->GetNearPoint(owner, x, y, z, (moveToward ? maxTarget : minTarget) - hitboxSum, angle ? target->ToAbsoluteAngle(angle->RelativeAngle) : target->GetAbsoluteAngle(owner));
194 shortenPath = false;
195 }
196
197 if (owner->IsHovering())
198 owner->UpdateAllowedPositionZ(x, y, z);
199
200 bool success = _path->CalculatePath(x, y, z, owner->CanFly());
201 if (!success || (_path->GetPathType() & (PATHFIND_NOPATH /* | PATHFIND_INCOMPLETE*/)))
202 {
203 if (cOwner)
204 cOwner->SetCannotReachTarget(true);
205 owner->StopMoving();
206 return true;
207 }
208
209 if (shortenPath)
210 _path->ShortenPathUntilDist(PositionToVector3(target), maxTarget);
211
212 if (cOwner)
213 cOwner->SetCannotReachTarget(false);
214
215 bool walk = false;
216 if (cOwner && !cOwner->IsPet())
217 {
218 switch (cOwner->GetMovementTemplate().GetChase())
219 {
221 walk = owner->IsWalking();
222 break;
224 walk = true;
225 break;
226 default:
227 break;
228 }
229 }
230
233
234 Movement::MoveSplineInit init(owner);
235 init.MovebyPath(_path->GetPath());
236 init.SetWalk(walk);
237 init.SetFacing(target);
238 init.Launch();
239 }
240 }
241
242 // and then, finally, we're done for the tick
243 return true;
244}
245
247{
251 if (Creature* cOwner = owner->ToCreature())
252 cOwner->SetCannotReachTarget(false);
253}
254
255void ChaseMovementGenerator::Finalize(Unit* owner, bool active, bool/* movementInform*/)
256{
258 if (active)
259 {
261 if (Creature* cOwner = owner->ToCreature())
262 cOwner->SetCannotReachTarget(false);
263 }
264}
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:142
#define ASSERT_NOTNULL(pointer)
Definition: Errors.h:84
@ 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
Definition: ObjectDefines.h:23
@ TYPEID_UNIT
Definition: ObjectGuid.h:40
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
@ PATHFIND_NOPATH
Definition: PathGenerator.h:47
@ UNIT_STATE_NOT_MOVE
Definition: Unit.h:300
@ UNIT_STATE_CHASE
Definition: Unit.h:260
@ UNIT_STATE_CHASE_MOVE
Definition: Unit.h:281
T square(T x)
Definition: Util.h:103
ChaseMovementGenerator(Unit *target, Optional< ChaseRange > range={}, Optional< ChaseAngle > angle={})
void Reset(Unit *) override
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)
Definition: Creature.cpp:3303
CreatureMovementData const & GetMovementTemplate() const
Definition: Creature.cpp:2939
CreatureAI * AI() const
Definition: Creature.h:214
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)
LowType GetCounter() const
Definition: ObjectGuid.h:293
static Creature * ToCreature(Object *o)
Definition: Object.h:219
bool IsInWorld() const
Definition: Object.h:154
TypeID GetTypeId() const
Definition: Object.h:173
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:159
Definition: Unit.h:627
void ClearUnitState(uint32 f)
Definition: Unit.h:733
virtual bool IsMovementPreventedByCasting() const
Definition: Unit.cpp:3119
virtual bool CanFly() const =0
MotionMaster * GetMotionMaster()
Definition: Unit.h:1652
bool IsPet() const
Definition: Unit.h:740
bool IsAlive() const
Definition: Unit.h:1164
float GetCombatReach() const override
Definition: Unit.h:694
void StopMoving()
Definition: Unit.cpp:10049
void AddUnitState(uint32 f)
Definition: Unit.h:731
bool isInAccessiblePlaceFor(Creature const *c) const
Definition: Unit.cpp:3165
bool IsHovering() const
Definition: Unit.h:1137
Unit * GetVictim() const
Definition: Unit.h:715
bool HasUnitState(const uint32 f) const
Definition: Unit.h:732
std::unique_ptr< Movement::MoveSpline > movespline
Definition: Unit.h:1766
float GetMeleeRange(Unit const *target) const
Definition: Unit.cpp:666
void SetInFront(WorldObject const *target)
Definition: Unit.cpp:12647
bool IsWalking() const
Definition: Unit.h:1136
void UpdateAllowedPositionZ(float x, float y, float &z, float *groundZ=nullptr) const
Definition: Object.cpp:1371
void GetNearPoint(WorldObject const *searcher, float &x, float &y, float &z, float distance2d, float absAngle) const
Definition: Object.cpp:3368
bool IsWithinLOSInMap(WorldObject const *obj, LineOfSightChecks checks=LINEOFSIGHT_ALL_CHECKS, VMAP::ModelIgnoreFlags ignoreFlags=VMAP::ModelIgnoreFlags::Nothing) const
Definition: Object.cpp:1181
Unit * GetTarget() const
CreatureChaseMovementType GetChase() const
Definition: CreatureData.h:402
float GetRelativeAngle(float x, float y) const
Definition: Position.h:136
float ToAbsoluteAngle(float relAngle) const
Definition: Position.h:133
float GetAbsoluteAngle(float x, float y) const
Definition: Position.h:125
constexpr void GetPosition(float &x, float &y) const
Definition: Position.h:81
constexpr float GetExactDistSq(float x, float y, float z) const
Definition: Position.h:110
constexpr bool IsInDist(float x, float y, float z, float dist) const
Definition: Position.h:143
void Update(int32 diff)
Definition: Timer.h:121
bool Passed() const
Definition: Timer.h:131
void Reset(int32 expiry)
Definition: Timer.h:136