TrinityCore
MotionMaster.h
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
18#ifndef MOTIONMASTER_H
19#define MOTIONMASTER_H
20
21#include "Common.h"
22#include "Duration.h"
23#include "ObjectGuid.h"
24#include "Optional.h"
25#include "MovementDefines.h"
26#include "MovementGenerator.h"
27#include "SharedDefines.h"
28#include <deque>
29#include <functional>
30#include <set>
31#include <unordered_map>
32#include <vector>
33
34class PathGenerator;
35class Unit;
36struct Position;
37struct SplineChainLink;
39struct WaypointPath;
40enum UnitMoveType : uint8;
41
42namespace Movement
43{
44 class MoveSplineInit;
45 struct SpellEffectExtraData;
46}
47
49{
51 MOTIONMASTER_FLAG_UPDATE = 0x1, // Update in progress
52 MOTIONMASTER_FLAG_STATIC_INITIALIZATION_PENDING = 0x2, // Static movement (MOTION_SLOT_DEFAULT) hasn't been initialized
53 MOTIONMASTER_FLAG_INITIALIZATION_PENDING = 0x4, // MotionMaster is stalled until signaled
54 MOTIONMASTER_FLAG_INITIALIZING = 0x8, // MotionMaster is initializing
55
57};
58
60{
69};
70
72{
74};
75
77{
78 public:
79 bool operator()(MovementGenerator const* a, MovementGenerator const* b) const;
80};
81
83{
84 MovementGeneratorInformation(MovementGeneratorType type, ObjectGuid targetGUID, std::string const& targetName);
85
88 std::string TargetName;
89};
90
91static bool EmptyValidator()
92{
93 return true;
94}
95
97{
98 public:
99 typedef std::function<void()> DelayedActionDefine;
100 typedef std::function<bool()> DelayedActionValidator;
101
103 {
104 public:
105 explicit DelayedAction(DelayedActionDefine&& action, DelayedActionValidator&& validator, MotionMasterDelayedActionType type) : Action(std::move(action)), Validator(std::move(validator)), Type(type) { }
106 explicit DelayedAction(DelayedActionDefine&& action, MotionMasterDelayedActionType type) : Action(std::move(action)), Validator(EmptyValidator), Type(type) { }
108
109 void Resolve() { if (Validator()) Action(); }
110
114 };
115
116 explicit MotionMaster(Unit* unit);
118
119 void Initialize();
120 void InitializeDefault();
121 void AddToWorld();
122
123 bool Empty() const;
124 uint32 Size() const;
125 std::vector<MovementGeneratorInformation> GetMovementGeneratorsInformation() const;
126 MovementSlot GetCurrentSlot() const;
127 MovementGenerator* GetCurrentMovementGenerator() const;
128 MovementGeneratorType GetCurrentMovementGeneratorType() const;
129 MovementGeneratorType GetCurrentMovementGeneratorType(MovementSlot slot) const;
130 MovementGenerator* GetCurrentMovementGenerator(MovementSlot slot) const;
131 // Returns first found MovementGenerator that matches the given criteria
132 MovementGenerator* GetMovementGenerator(std::function<bool(MovementGenerator const*)> const& filter, MovementSlot slot = MOTION_SLOT_ACTIVE) const;
133 bool HasMovementGenerator(std::function<bool(MovementGenerator const*)> const& filter, MovementSlot slot = MOTION_SLOT_ACTIVE) const;
134
135 void Update(uint32 diff);
136 void Add(MovementGenerator* movement, MovementSlot slot = MOTION_SLOT_ACTIVE);
137 // NOTE: MOTION_SLOT_DEFAULT will be autofilled with IDLE_MOTION_TYPE
139 // Removes first found movement
140 // NOTE: MOTION_SLOT_DEFAULT will be autofilled with IDLE_MOTION_TYPE
142 // NOTE: MOTION_SLOT_DEFAULT wont be affected
143 void Clear();
144 // Removes all movements for the given MovementSlot
145 // NOTE: MOTION_SLOT_DEFAULT will be autofilled with IDLE_MOTION_TYPE
146 void Clear(MovementSlot slot);
147 // Removes all movements with the given MovementGeneratorMode
148 // NOTE: MOTION_SLOT_DEFAULT wont be affected
149 void Clear(MovementGeneratorMode mode);
150 // Removes all movements with the given MovementGeneratorPriority
151 // NOTE: MOTION_SLOT_DEFAULT wont be affected
152 void Clear(MovementGeneratorPriority priority);
153 void PropagateSpeedChange();
154 bool GetDestination(float &x, float &y, float &z);
155 bool StopOnDeath();
156
157 void MoveIdle();
158 void MoveTargetedHome();
159 void MoveRandom(float wanderDistance = 0.0f, Optional<Milliseconds> duration = {}, MovementSlot slot = MOTION_SLOT_DEFAULT);
160 void MoveFollow(Unit* target, float dist, ChaseAngle angle, Optional<Milliseconds> duration = {}, MovementSlot slot = MOTION_SLOT_ACTIVE);
161 void MoveChase(Unit* target, Optional<ChaseRange> dist = {}, Optional<ChaseAngle> angle = {});
162 void MoveChase(Unit* target, float dist, float angle) { MoveChase(target, ChaseRange(dist), ChaseAngle(angle)); }
163 void MoveConfused();
164 void MoveFleeing(Unit* enemy, Milliseconds time = 0ms);
165 void MovePoint(uint32 id, Position const& pos, bool generatePath = true, Optional<float> finalOrient = {}, Optional<float> speed = {},
167 void MovePoint(uint32 id, float x, float y, float z, bool generatePath = true, Optional<float> finalOrient = {}, Optional<float> speed = {},
169 /*
170 * Makes the unit move toward the target until it is at a certain distance from it. The unit then stops.
171 * Only works in 2D.
172 * This method doesn't account for any movement done by the target. in other words, it only works if the target is stationary.
173 */
174 void MoveCloserAndStop(uint32 id, Unit* target, float distance);
175 // These two movement types should only be used with creatures having landing/takeoff animations
176 void MoveLand(uint32 id, Position const& pos, Optional<int32> tierTransitionId = {}, Optional<float> velocity = {},
178 void MoveTakeoff(uint32 id, Position const& pos, Optional<int32> tierTransitionId = {}, Optional<float> velocity = {},
180 void MoveCharge(float x, float y, float z, float speed = SPEED_CHARGE, uint32 id = EVENT_CHARGE, bool generatePath = false, Unit const* target = nullptr, Movement::SpellEffectExtraData const* spellEffectExtraData = nullptr);
181 void MoveCharge(PathGenerator const& path, float speed = SPEED_CHARGE, Unit const* target = nullptr, Movement::SpellEffectExtraData const* spellEffectExtraData = nullptr);
182 void MoveKnockbackFrom(Position const& origin, float speedXY, float speedZ, Movement::SpellEffectExtraData const* spellEffectExtraData = nullptr);
183 void MoveJumpTo(float angle, float speedXY, float speedZ);
184 void MoveJump(Position const& pos, float speedXY, float speedZ, uint32 id = EVENT_JUMP, bool hasOrientation = false, JumpArrivalCastArgs const* arrivalCast = nullptr, Movement::SpellEffectExtraData const* spellEffectExtraData = nullptr);
185 void MoveJump(float x, float y, float z, float o, float speedXY, float speedZ, uint32 id = EVENT_JUMP, bool hasOrientation = false, JumpArrivalCastArgs const* arrivalCast = nullptr, Movement::SpellEffectExtraData const* spellEffectExtraData = nullptr);
186 void MoveJumpWithGravity(Position const& pos, float speedXY, float gravity, uint32 id = EVENT_JUMP, bool hasOrientation = false, JumpArrivalCastArgs const* arrivalCast = nullptr, Movement::SpellEffectExtraData const* spellEffectExtraData = nullptr);
187 void MoveCirclePath(float x, float y, float z, float radius, bool clockwise, uint8 stepCount,
188 Optional<Milliseconds> duration = {}, Optional<float> speed = {},
190 void MoveSmoothPath(uint32 pointId, Position const* pathPoints, size_t pathSize, bool walk = false, bool fly = false);
191 // Walk along spline chain stored in DB (script_spline_chain_meta and script_spline_chain_waypoints)
192 void MoveAlongSplineChain(uint32 pointId, uint16 dbChainId, bool walk);
193 void MoveAlongSplineChain(uint32 pointId, std::vector<SplineChainLink> const& chain, bool walk);
194 void ResumeSplineChain(SplineChainResumeInfo const& info);
195 void MoveFall(uint32 id = 0);
196 void MoveSeekAssistance(float x, float y, float z);
197 void MoveSeekAssistanceDistract(uint32 timer);
198 void MoveTaxiFlight(uint32 path, uint32 pathnode);
199 void MoveDistract(uint32 time, float orientation);
200 void MovePath(uint32 pathId, bool repeatable, Optional<Milliseconds> duration = {}, Optional<float> speed = {},
202 Optional<std::pair<Milliseconds, Milliseconds>> waitTimeRangeAtPathEnd = {}, Optional<float> wanderDistanceAtPathEnds = {},
203 Optional<bool> followPathBackwardsFromEndToStart = {}, bool generatePath = true);
204 void MovePath(WaypointPath const& path, bool repeatable, Optional<Milliseconds> duration = {}, Optional<float> speed = {},
206 Optional<std::pair<Milliseconds, Milliseconds>> waitTimeRangeAtPathEnd = {}, Optional<float> wanderDistanceAtPathEnds = {},
207 Optional<bool> followPathBackwardsFromEndToStart = {}, bool generatePath = true);
208
217 void MoveRotate(uint32 id, RotateDirection direction, Optional<Milliseconds> time = {},
218 Optional<float> turnSpeed = {}, Optional<float> totalTurnAngle = {});
219 void MoveFormation(Unit* leader, float range, float angle, uint32 point1, uint32 point2);
220
221 void LaunchMoveSpline(std::function<void(Movement::MoveSplineInit& init)>&& initializer, uint32 id = 0, MovementGeneratorPriority priority = MOTION_PRIORITY_NORMAL, MovementGeneratorType type = EFFECT_MOTION_TYPE);
222
223 void CalculateJumpSpeeds(float dist, UnitMoveType moveType, float speedMultiplier, float minHeight, float maxHeight, float& speedXY, float& speedZ) const;
224
225 private:
226 typedef std::unique_ptr<MovementGenerator, MovementGeneratorDeleter> MovementGeneratorPointer;
227 typedef std::multiset<MovementGenerator*, MovementGeneratorComparator> MotionMasterContainer;
228 typedef std::unordered_multimap<uint32, MovementGenerator const*> MotionMasterUnitStatesContainer;
229
230 void AddFlag(uint8 const flag) { _flags |= flag; }
231 bool HasFlag(uint8 const flag) const { return (_flags & flag) != 0; }
232 void RemoveFlag(uint8 const flag) { _flags &= ~flag; }
233
234 void ResolveDelayedActions();
235 void Remove(MotionMasterContainer::iterator iterator, bool active, bool movementInform);
236 void Pop(bool active, bool movementInform);
237 void DirectInitialize();
238 void DirectClear();
239 void DirectClearDefault();
240 void DirectClear(std::function<bool(MovementGenerator*)> const& filter);
241 void DirectAdd(MovementGenerator* movement, MovementSlot slot);
242
243 void Delete(MovementGenerator* movement, bool active, bool movementInform);
244 void DeleteDefault(bool active, bool movementInform);
245 void AddBaseUnitState(MovementGenerator const* movement);
246 void ClearBaseUnitState(MovementGenerator const* movement);
247 void ClearBaseUnitStates();
248
253 std::deque<DelayedAction> _delayedActions;
255};
256
257#endif // MOTIONMASTER_H
#define TC_GAME_API
Definition: Define.h:123
uint8_t uint8
Definition: Define.h:144
uint16_t uint16
Definition: Define.h:143
uint32_t uint32
Definition: Define.h:142
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition: Duration.h:29
static bool EmptyValidator()
Definition: MotionMaster.h:91
MotionMasterDelayedActionType
Definition: MotionMaster.h:60
@ MOTIONMASTER_DELAYED_ADD
Definition: MotionMaster.h:65
@ MOTIONMASTER_DELAYED_REMOVE
Definition: MotionMaster.h:66
@ MOTIONMASTER_DELAYED_REMOVE_TYPE
Definition: MotionMaster.h:67
@ MOTIONMASTER_DELAYED_INITIALIZE
Definition: MotionMaster.h:68
@ MOTIONMASTER_DELAYED_CLEAR_PRIORITY
Definition: MotionMaster.h:64
@ MOTIONMASTER_DELAYED_CLEAR
Definition: MotionMaster.h:61
@ MOTIONMASTER_DELAYED_CLEAR_SLOT
Definition: MotionMaster.h:62
@ MOTIONMASTER_DELAYED_CLEAR_MODE
Definition: MotionMaster.h:63
MotionMasterFlags
Definition: MotionMaster.h:49
@ MOTIONMASTER_FLAG_DELAYED
Definition: MotionMaster.h:56
@ MOTIONMASTER_FLAG_NONE
Definition: MotionMaster.h:50
@ MOTIONMASTER_FLAG_INITIALIZING
Definition: MotionMaster.h:54
@ MOTIONMASTER_FLAG_UPDATE
Definition: MotionMaster.h:51
@ MOTIONMASTER_FLAG_INITIALIZATION_PENDING
Definition: MotionMaster.h:53
@ MOTIONMASTER_FLAG_STATIC_INITIALIZATION_PENDING
Definition: MotionMaster.h:52
MovementGeneratorMode
RotateDirection
MovementGeneratorPriority
@ MOTION_PRIORITY_NORMAL
MovementSlot
@ MOTION_SLOT_ACTIVE
@ MOTION_SLOT_DEFAULT
MovementGeneratorType
@ EFFECT_MOTION_TYPE
#define SPEED_CHARGE
MovementWalkRunSpeedSelectionMode
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
@ EVENT_CHARGE
@ EVENT_JUMP
UnitMoveType
Definition: UnitDefines.h:116
Action
DelayedAction(DelayedActionDefine &&action, DelayedActionValidator &&validator, MotionMasterDelayedActionType type)
Definition: MotionMaster.h:105
DelayedAction(DelayedActionDefine &&action, MotionMasterDelayedActionType type)
Definition: MotionMaster.h:106
DelayedActionValidator Validator
Definition: MotionMaster.h:112
DelayedActionDefine Action
Definition: MotionMaster.h:111
bool HasFlag(uint8 const flag) const
Definition: MotionMaster.h:231
std::multiset< MovementGenerator *, MovementGeneratorComparator > MotionMasterContainer
Definition: MotionMaster.h:227
void MoveChase(Unit *target, float dist, float angle)
Definition: MotionMaster.h:162
std::unique_ptr< MovementGenerator, MovementGeneratorDeleter > MovementGeneratorPointer
Definition: MotionMaster.h:226
void AddFlag(uint8 const flag)
Definition: MotionMaster.h:230
MotionMasterUnitStatesContainer _baseUnitStatesMap
Definition: MotionMaster.h:252
void RemoveFlag(uint8 const flag)
Definition: MotionMaster.h:232
std::function< void()> DelayedActionDefine
Definition: MotionMaster.h:99
std::unordered_multimap< uint32, MovementGenerator const * > MotionMasterUnitStatesContainer
Definition: MotionMaster.h:228
std::deque< DelayedAction > _delayedActions
Definition: MotionMaster.h:253
MovementGeneratorPointer _defaultGenerator
Definition: MotionMaster.h:250
std::function< bool()> DelayedActionValidator
Definition: MotionMaster.h:100
MotionMasterContainer _generators
Definition: MotionMaster.h:251
Definition: Unit.h:627
float constexpr gravity
bool Size(ContainerUnorderedMap< TypeList< H, T >, KEY_TYPE > const &elements, std::size_t *size, SPECIFIC_TYPE *obj)
void Update(VignetteData &vignette, WorldObject const *owner)
Definition: Vignette.cpp:90
void Remove(VignetteData &vignette, WorldObject const *owner)
Definition: Vignette.cpp:100
STL namespace.
bool operator()(MovementGenerator const *a, MovementGenerator const *b) const
void operator()(MovementGenerator *a)
MovementGeneratorInformation(MovementGeneratorType type, ObjectGuid targetGUID, std::string const &targetName)
MovementGeneratorType Type
Definition: MotionMaster.h:86