TrinityCore
Loading...
Searching...
No Matches
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 /*= {}*/, Optional<MovementFadeObject> fadeObject /*= {}*/,
36 : _movementId(id), _destination(x, y, z), _speed(speed), _generatePath(generatePath), _finalOrient(finalOrient),
37 i_faceTarget(faceTarget), _speedSelectionMode(speedSelectionMode), _closeEnoughDistance(closeEnoughDistance), _fadeObject(fadeObject)
38{
43 this->ScriptResult = std::move(scriptResult);
44
45 if (spellEffectExtraData)
46 this->i_spellEffectExtra = std::make_unique<Movement::SpellEffectExtraData>(*spellEffectExtraData);
47}
48
50
55
57{
60
62 {
64 return;
65 }
66
68 {
70 owner->StopMoving();
71 return;
72 }
73
75
76 Movement::MoveSplineInit init(owner);
77 [&]()
78 {
79 if (_generatePath)
80 {
81 PathGenerator path(owner);
82 G3D::Vector3 dest = PositionToVector3(_destination);
83 bool result = path.CalculatePath(dest.x, dest.y, dest.z, false);
84 if (result && !(path.GetPathType() & PATHFIND_NOPATH))
85 {
88
89 init.MovebyPath(path.GetPath());
90 return;
91 }
92 }
93
96 owner->MovePosition(dest, std::min(*_closeEnoughDistance, dest.GetExactDist(owner)), float(M_PI) + owner->GetRelativeAngle(dest));
97
98 init.MoveTo(dest.GetPositionX(), dest.GetPositionY(), dest.GetPositionZ(), false);
99 }();
100 if (_speed)
101 init.SetVelocity(*_speed);
102 if (i_faceTarget)
106 if (_finalOrient)
107 init.SetFacing(*_finalOrient);
108 if (_fadeObject)
109 init.SetFadeObject(_fadeObject->Duration.value_or(1s));
110 switch (_speedSelectionMode)
111 {
113 break;
115 init.SetWalk(false);
116 break;
118 init.SetWalk(true);
119 break;
120 default:
121 break;
122 }
123
124 init.Launch();
125
126 // Call for creature group update
127 if (Creature* creature = owner->ToCreature())
128 creature->SignalFormationMovement();
129}
130
137
139{
140 if (!owner)
141 return false;
142
144 {
145 if (owner->movespline->Finalized())
146 {
148 return false;
149 }
150 return true;
151 }
152
154 {
156 owner->StopMoving();
157 return true;
158 }
159
161 {
163
165
166 Movement::MoveSplineInit init(owner);
168 if (_speed) // Default value for point motion type is 0.0, if 0.0 spline will use GetSpeed on unit
169 init.SetVelocity(*_speed);
170 init.Launch();
171
172 // Call for creature group update
173 if (Creature* creature = owner->ToCreature())
174 creature->SignalFormationMovement();
175 }
176
177 if (owner->movespline->Finalized())
178 {
181 return false;
182 }
183 return true;
184}
185
191
192void PointMovementGenerator::Finalize(Unit* owner, bool active, bool movementInform)
193{
195 if (active)
197
198 if (movementInform && HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED))
199 MovementInform(owner);
200}
201
203{
205
206 // deliver EVENT_CHARGE to scripts, EVENT_CHARGE_PREPATH is just internal implementation detail of this movement generator
208
209 if (Creature* creature = owner->ToCreature())
210 if (creature->AI())
211 creature->AI()->MovementInform(POINT_MOTION_TYPE, movementId);
212}
213
214//---- AssistanceMovementGenerator
215
216void AssistanceMovementGenerator::Finalize(Unit* owner, bool active, bool movementInform)
217{
219 if (active)
221
222 if (movementInform && HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED) && owner->IsCreature())
223 {
224 Creature* ownerCreature = owner->ToCreature();
225 ownerCreature->SetNoCallAssistance(false);
226 ownerCreature->CallAssistance();
227 if (ownerCreature->IsAlive())
229 }
230}
231
#define M_PI
Definition Common.h:118
uint32_t uint32
Definition Define.h:154
@ MOTION_MODE_DEFAULT
@ MOTION_PRIORITY_NORMAL
MovementWalkRunSpeedSelectionMode
MovementGeneratorType
@ ASSISTANCE_MOTION_TYPE
@ POINT_MOTION_TYPE
@ 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
@ EVENT_CHARGE
@ EVENT_CHARGE_PREPATH
@ UNIT_STATE_NOT_MOVE
Definition Unit.h:306
@ UNIT_STATE_ROAMING_MOVE
Definition Unit.h:284
@ UNIT_STATE_ROAMING
Definition Unit.h:265
void Finalize(Unit *owner, bool active, bool movementInform) override
MovementGeneratorType GetMovementGeneratorType() const override
bool IsCreature() const
Definition BaseEntity.h:172
void SetNoCallAssistance(bool val)
Definition Creature.h:339
void CallAssistance()
void MoveSeekAssistanceDistract(uint32 timer)
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 MoveTo(Vector3 const &destination, bool generatePath=true, bool forceDestination=false)
void SetWalk(bool enable)
void MovebyPath(std::span< Vector3 const > path, int32 pointId=0)
void SetVelocity(float velocity)
void SetFacing(float angle)
void SetFadeObject(Milliseconds fadeDuration=1s)
void SetSpellEffectExtraData(SpellEffectExtraData const &spellEffectExtraData)
Creature * ToCreature()
Definition Object.h:121
Movement::PointsArray const & GetPath() const
void ShortenPathUntilDist(G3D::Vector3 const &target, float dist)
PathType GetPathType() const
bool CalculatePath(float srcX, float srcY, float srcZ, float destX, float destY, float destZ, bool forceDest=false)
MovementWalkRunSpeedSelectionMode _speedSelectionMode
Optional< float > _closeEnoughDistance
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={}, Optional< MovementFadeObject > fadeObject={}, Scripting::v2::ActionResultSetter< MovementStopReason > &&scriptResult={})
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
Optional< MovementFadeObject > _fadeObject
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:635
void ClearUnitState(uint32 f)
Definition Unit.h:744
virtual bool IsMovementPreventedByCasting() const
Definition Unit.cpp:3261
MotionMaster * GetMotionMaster()
Definition Unit.h:1723
bool IsAlive() const
Definition Unit.h:1185
void StopMoving()
Definition Unit.cpp:10680
void AddUnitState(uint32 f)
Definition Unit.h:742
bool HasUnitState(const uint32 f) const
Definition Unit.h:743
std::unique_ptr< Movement::MoveSpline > movespline
Definition Unit.h:1838
void MovePosition(Position &pos, float dist, float angle, float maxHeightChange=6.0f) const
Definition Object.cpp:2782
#define sWorld
Definition World.h:916
@ CONFIG_CREATURE_FAMILY_ASSISTANCE_DELAY
Definition World.h:312
constexpr float GetPositionX() const
Definition Position.h:87
constexpr float GetPositionY() const
Definition Position.h:88
float GetRelativeAngle(float x, float y) const
Definition Position.h:147
float GetExactDist(float x, float y, float z) const
Definition Position.h:129
constexpr float GetPositionZ() const
Definition Position.h:89