TrinityCore
PointMovementGenerator.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 "MovementDefines.h"
24#include "MoveSpline.h"
25#include "MoveSplineInit.h"
26#include "PathGenerator.h"
27#include "World.h"
28
29//----- Point Movement Generator
30
31PointMovementGenerator::PointMovementGenerator(uint32 id, float x, float y, float z, bool generatePath, Optional<float> speed /*= {}*/, Optional<float> finalOrient /*= {}*/,
32 Unit const* faceTarget /*= nullptr*/, Movement::SpellEffectExtraData const* spellEffectExtraData /*= nullptr*/,
33 MovementWalkRunSpeedSelectionMode speedSelectionMode /*= MovementWalkRunSpeedSelectionMode::Default*/,
34 Optional<float> closeEnoughDistance /*= {}*/)
35 : _movementId(id), _destination(x, y, z), _speed(speed), _generatePath(generatePath), _finalOrient(finalOrient),
36 i_faceTarget(faceTarget), _speedSelectionMode(speedSelectionMode), _closeEnoughDistance(closeEnoughDistance)
37{
42
43 if (spellEffectExtraData)
44 this->i_spellEffectExtra = std::make_unique<Movement::SpellEffectExtraData>(*spellEffectExtraData);
45}
46
48{
49 return POINT_MOTION_TYPE;
50}
51
53{
56
58 {
60 return;
61 }
62
64 {
66 owner->StopMoving();
67 return;
68 }
69
71
72 Movement::MoveSplineInit init(owner);
73 [&]()
74 {
75 if (_generatePath)
76 {
77 PathGenerator path(owner);
78 G3D::Vector3 dest = PositionToVector3(_destination);
79 bool result = path.CalculatePath(dest.x, dest.y, dest.z, false);
80 if (result && !(path.GetPathType() & PATHFIND_NOPATH))
81 {
84
85 init.MovebyPath(path.GetPath());
86 return;
87 }
88 }
89
92 owner->MovePosition(dest, std::min(*_closeEnoughDistance, dest.GetExactDist(owner)), float(M_PI) + owner->GetRelativeAngle(dest));
93
94 init.MoveTo(dest.GetPositionX(), dest.GetPositionY(), dest.GetPositionZ(), false);
95 }();
96 if (_speed)
97 init.SetVelocity(*_speed);
98 if (i_faceTarget)
102 if (_finalOrient)
103 init.SetFacing(*_finalOrient);
104 switch (_speedSelectionMode)
105 {
107 break;
109 init.SetWalk(false);
110 break;
112 init.SetWalk(true);
113 break;
114 default:
115 break;
116 }
117
118 init.Launch();
119
120 // Call for creature group update
121 if (Creature* creature = owner->ToCreature())
122 creature->SignalFormationMovement();
123}
124
126{
128
129 Initialize(owner);
130}
131
133{
134 if (!owner)
135 return false;
136
138 {
139 if (owner->movespline->Finalized())
140 {
142 return false;
143 }
144 return true;
145 }
146
148 {
150 owner->StopMoving();
151 return true;
152 }
153
155 {
157
159
160 Movement::MoveSplineInit init(owner);
162 if (_speed) // Default value for point motion type is 0.0, if 0.0 spline will use GetSpeed on unit
163 init.SetVelocity(*_speed);
164 init.Launch();
165
166 // Call for creature group update
167 if (Creature* creature = owner->ToCreature())
168 creature->SignalFormationMovement();
169 }
170
171 if (owner->movespline->Finalized())
172 {
175 return false;
176 }
177 return true;
178}
179
181{
184}
185
186void PointMovementGenerator::Finalize(Unit* owner, bool active, bool movementInform)
187{
189 if (active)
191
192 if (movementInform && HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED))
193 MovementInform(owner);
194}
195
197{
198 // deliver EVENT_CHARGE to scripts, EVENT_CHARGE_PREPATH is just internal implementation detail of this movement generator
200
201 if (Creature* creature = owner->ToCreature())
202 if (creature->AI())
203 creature->AI()->MovementInform(POINT_MOTION_TYPE, movementId);
204}
205
206//---- AssistanceMovementGenerator
207
208void AssistanceMovementGenerator::Finalize(Unit* owner, bool active, bool movementInform)
209{
211 if (active)
213
214 if (movementInform && HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED) && owner->IsCreature())
215 {
216 Creature* ownerCreature = owner->ToCreature();
217 ownerCreature->SetNoCallAssistance(false);
218 ownerCreature->CallAssistance();
219 if (ownerCreature->IsAlive())
221 }
222}
223
225{
227}
#define M_PI
Definition: Common.h:115
uint32_t uint32
Definition: Define.h:142
@ MOTION_MODE_DEFAULT
@ MOTION_PRIORITY_NORMAL
MovementGeneratorType
@ ASSISTANCE_MOTION_TYPE
@ POINT_MOTION_TYPE
MovementWalkRunSpeedSelectionMode
@ MOVEMENTGENERATOR_FLAG_INITIALIZATION_PENDING
@ MOVEMENTGENERATOR_FLAG_DEACTIVATED
@ MOVEMENTGENERATOR_FLAG_FINALIZED
@ MOVEMENTGENERATOR_FLAG_TRANSITORY
@ MOVEMENTGENERATOR_FLAG_INTERRUPTED
@ MOVEMENTGENERATOR_FLAG_INFORM_ENABLED
@ MOVEMENTGENERATOR_FLAG_INITIALIZED
@ MOVEMENTGENERATOR_FLAG_SPEED_UPDATE_PENDING
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
@ PATHFIND_NOPATH
Definition: PathGenerator.h:47
@ EVENT_CHARGE
@ EVENT_CHARGE_PREPATH
@ UNIT_STATE_NOT_MOVE
Definition: Unit.h:300
@ UNIT_STATE_ROAMING_MOVE
Definition: Unit.h:278
@ UNIT_STATE_ROAMING
Definition: Unit.h:259
void Finalize(Unit *owner, bool active, bool movementInform) override
MovementGeneratorType GetMovementGeneratorType() const override
void SetNoCallAssistance(bool val)
Definition: Creature.h:317
void CallAssistance()
Definition: Creature.cpp:2533
void MoveSeekAssistanceDistract(uint32 timer)
void AddFlag(uint16 const flag)
bool HasFlag(uint16 const flag) const
void RemoveFlag(uint16 const flag)
void MoveTo(Vector3 const &destination, bool generatePath=true, bool forceDestination=false)
void SetWalk(bool enable)
void SetVelocity(float velocity)
void SetFacing(float angle)
void SetSpellEffectExtraData(SpellEffectExtraData const &spellEffectExtraData)
void MovebyPath(PointsArray const &path, int32 pointId=0)
static Creature * ToCreature(Object *o)
Definition: Object.h:219
bool IsCreature() const
Definition: Object.h:218
Movement::PointsArray const & GetPath() const
Definition: PathGenerator.h:81
void ShortenPathUntilDist(G3D::Vector3 const &point, float dist)
PathType GetPathType() const
Definition: PathGenerator.h:84
bool CalculatePath(float destX, float destY, float destZ, bool forceDest=false)
MovementWalkRunSpeedSelectionMode _speedSelectionMode
Optional< float > _closeEnoughDistance
Optional< float > _finalOrient
if set then unit will turn to specified _orient in provided _pos
void Deactivate(Unit *owner) override
void Initialize(Unit *owner) override
PointMovementGenerator(uint32 id, float x, float y, float z, bool generatePath, Optional< float > speed={}, Optional< float > finalOrient={}, Unit const *faceTarget=nullptr, Movement::SpellEffectExtraData const *spellEffectExtraData=nullptr, MovementWalkRunSpeedSelectionMode speedSelectionMode=MovementWalkRunSpeedSelectionMode::Default, Optional< float > closeEnoughDistance={})
MovementGeneratorType GetMovementGeneratorType() const override
std::unique_ptr< Movement::SpellEffectExtraData > i_spellEffectExtra
bool Update(Unit *owner, uint32 diff) override
void Finalize(Unit *owner, bool active, bool movementInform) override
void Reset(Unit *owner) override
Definition: Unit.h:627
void ClearUnitState(uint32 f)
Definition: Unit.h:733
virtual bool IsMovementPreventedByCasting() const
Definition: Unit.cpp:3119
MotionMaster * GetMotionMaster()
Definition: Unit.h:1652
bool IsAlive() const
Definition: Unit.h:1164
void StopMoving()
Definition: Unit.cpp:10049
void AddUnitState(uint32 f)
Definition: Unit.h:731
bool HasUnitState(const uint32 f) const
Definition: Unit.h:732
std::unique_ptr< Movement::MoveSpline > movespline
Definition: Unit.h:1766
void MovePosition(Position &pos, float dist, float angle)
Definition: Object.cpp:3436
#define sWorld
Definition: World.h:931
@ CONFIG_CREATURE_FAMILY_ASSISTANCE_DELAY
Definition: World.h:314
constexpr float GetPositionX() const
Definition: Position.h:76
constexpr float GetPositionY() const
Definition: Position.h:77
float GetRelativeAngle(float x, float y) const
Definition: Position.h:136
float GetExactDist(float x, float y, float z) const
Definition: Position.h:118
constexpr float GetPositionZ() const
Definition: Position.h:78