TrinityCore
Loading...
Searching...
No Matches
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;
41enum class AnimTier : uint8;
42
43namespace Movement
44{
45 class MoveSplineInit;
46 struct SpellEffectExtraData;
47}
48
50{
52 MOTIONMASTER_FLAG_UPDATE = 0x1, // Update in progress
53 MOTIONMASTER_FLAG_STATIC_INITIALIZATION_PENDING = 0x2, // Static movement (MOTION_SLOT_DEFAULT) hasn't been initialized
54 MOTIONMASTER_FLAG_INITIALIZATION_PENDING = 0x4, // MotionMaster is stalled until signaled
55 MOTIONMASTER_FLAG_INITIALIZING = 0x8, // MotionMaster is initializing
56
58};
59
71
76
78{
79 public:
80 bool operator()(MovementGenerator const* a, MovementGenerator const* b) const;
81};
82
91
92static bool EmptyValidator()
93{
94 return true;
95}
96
98{
99 public:
100 typedef std::function<void()> DelayedActionDefine;
101 typedef std::function<bool()> DelayedActionValidator;
102
104 {
105 public:
106 explicit DelayedAction(DelayedActionDefine&& action, DelayedActionValidator&& validator, MotionMasterDelayedActionType type) : Action(std::move(action)), Validator(std::move(validator)), Type(type) { }
107 explicit DelayedAction(DelayedActionDefine&& action, MotionMasterDelayedActionType type) : Action(std::move(action)), Validator(EmptyValidator), Type(type) { }
109
110 void Resolve() { if (Validator()) Action(); }
111
115 };
116
117 explicit MotionMaster(Unit* unit);
119
120 void Initialize();
121 void InitializeDefault();
122 void AddToWorld();
123
124 bool Empty() const;
125 uint32 Size() const;
126 std::vector<MovementGeneratorInformation> GetMovementGeneratorsInformation() const;
127 MovementSlot GetCurrentSlot() const;
128 MovementGenerator* GetCurrentMovementGenerator() const;
129 MovementGeneratorType GetCurrentMovementGeneratorType() const;
130 MovementGeneratorType GetCurrentMovementGeneratorType(MovementSlot slot) const;
131 MovementGenerator* GetCurrentMovementGenerator(MovementSlot slot) const;
132 // Returns first found MovementGenerator that matches the given criteria
133 MovementGenerator* GetMovementGenerator(std::function<bool(MovementGenerator const*)> const& filter, MovementSlot slot = MOTION_SLOT_ACTIVE) const;
134 bool HasMovementGenerator(std::function<bool(MovementGenerator const*)> const& filter, MovementSlot slot = MOTION_SLOT_ACTIVE) const;
135
136 void Update(uint32 diff);
137 void Add(MovementGenerator* movement, MovementSlot slot = MOTION_SLOT_ACTIVE);
138 // NOTE: MOTION_SLOT_DEFAULT will be autofilled with IDLE_MOTION_TYPE
140 // Removes first found movement
141 // NOTE: MOTION_SLOT_DEFAULT will be autofilled with IDLE_MOTION_TYPE
143 // NOTE: MOTION_SLOT_DEFAULT wont be affected
144 void Clear();
145 // Removes all movements for the given MovementSlot
146 // NOTE: MOTION_SLOT_DEFAULT will be autofilled with IDLE_MOTION_TYPE
147 void Clear(MovementSlot slot);
148 // Removes all movements with the given MovementGeneratorMode
149 // NOTE: MOTION_SLOT_DEFAULT wont be affected
150 void Clear(MovementGeneratorMode mode);
151 // Removes all movements with the given MovementGeneratorPriority
152 // NOTE: MOTION_SLOT_DEFAULT wont be affected
153 void Clear(MovementGeneratorPriority priority);
154 void PropagateSpeedChange();
155 bool GetDestination(float &x, float &y, float &z);
156 bool StopOnDeath();
157
158 void MoveIdle();
159 void MoveTargetedHome();
160 void MoveRandom(float wanderDistance = 0.0f, Optional<Milliseconds> duration = {}, Optional<float> speed = {},
163 void MoveFollow(Unit* target, float dist, Optional<ChaseAngle> angle = {}, Optional<Milliseconds> duration = {}, bool ignoreTargetWalk = false, MovementSlot slot = MOTION_SLOT_ACTIVE,
165 void MoveChase(Unit* target, Optional<ChaseRange> dist = {}, Optional<ChaseAngle> angle = {});
166 void MoveChase(Unit* target, float dist, float angle) { MoveChase(target, ChaseRange(dist), ChaseAngle(angle)); }
167 void MoveConfused();
168 void MoveFleeing(Unit* enemy, Milliseconds time = 0ms,
170 void MovePoint(uint32 id, Position const& pos, bool generatePath = true, Optional<float> finalOrient = {}, Optional<float> speed = {},
172 Optional<MovementFadeObject> fadeObject = {},
174 void MovePoint(uint32 id, float x, float y, float z, bool generatePath = true, Optional<float> finalOrient = {}, Optional<float> speed = {},
176 Optional<MovementFadeObject> fadeObject = {},
178 /*
179 * Makes the unit move toward the target until it is at a certain distance from it. The unit then stops.
180 * Only works in 2D.
181 * This method doesn't account for any movement done by the target. in other words, it only works if the target is stationary.
182 */
183 void MoveCloserAndStop(uint32 id, Unit* target, float distance);
184 // These two movement types should only be used with creatures having landing/takeoff animations
185 void MoveLand(uint32 id, Position const& pos, Optional<int32> tierTransitionId = {}, Optional<float> velocity = {},
188 void MoveTakeoff(uint32 id, Position const& pos, Optional<int32> tierTransitionId = {}, Optional<float> velocity = {},
191 void MoveTierTransition(uint32 id, Position const& pos, AnimTier newAnimTier, Optional<int32> tierTransitionId = {}, Optional<float> velocity = {},
194 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);
195 void MoveCharge(PathGenerator const& path, float speed = SPEED_CHARGE, Unit const* target = nullptr, Movement::SpellEffectExtraData const* spellEffectExtraData = nullptr);
196 void MoveKnockbackFrom(Position const& origin, float speedXY, float speedZ, float angle = M_PI, Movement::SpellEffectExtraData const* spellEffectExtraData = nullptr);
197 void MoveJump(uint32 id, Position const& pos, std::variant<std::monostate, float, Milliseconds> speedOrTime = {},
198 Optional<float> minHeight = {}, Optional<float> maxHeight = {},
199 MovementFacingTarget const& facing = {}, bool orientationFixed = false, bool unlimitedSpeed = false, Optional<float> speedMultiplier = {},
200 JumpArrivalCastArgs const* arrivalCast = nullptr, Movement::SpellEffectExtraData const* spellEffectExtraData = nullptr,
202 void MoveCirclePath(float x, float y, float z, float radius, bool clockwise, uint8 stepCount,
203 Optional<Milliseconds> duration = {}, Optional<float> speed = {},
206 // Walk along spline chain stored in DB (script_spline_chain_meta and script_spline_chain_waypoints)
207 void MoveAlongSplineChain(uint32 pointId, uint16 dbChainId, bool walk);
208 void MoveAlongSplineChain(uint32 pointId, std::vector<SplineChainLink> const& chain, bool walk);
209 void ResumeSplineChain(SplineChainResumeInfo const& info);
210 void MoveFall(uint32 id = 0,
212 void MoveSeekAssistance(float x, float y, float z);
213 void MoveSeekAssistanceDistract(uint32 timer);
214 void MoveTaxiFlight(uint32 path, uint32 pathnode, Optional<float> speed = {},
216 void MoveDistract(uint32 time, float orientation);
217 void MovePath(uint32 pathId, bool repeatable, Optional<Milliseconds> duration = {}, Optional<float> speed = {},
219 Optional<std::pair<Milliseconds, Milliseconds>> waitTimeRangeAtPathEnd = {}, Optional<float> wanderDistanceAtPathEnds = {},
220 Optional<bool> followPathBackwardsFromEndToStart = {}, Optional<bool> exactSplinePath = {}, bool generatePath = true,
221 Optional<MovementFadeObject> fadeObject = {},
223 void MovePath(WaypointPath const& path, bool repeatable, Optional<Milliseconds> duration = {}, Optional<float> speed = {},
225 Optional<std::pair<Milliseconds, Milliseconds>> waitTimeRangeAtPathEnd = {}, Optional<float> wanderDistanceAtPathEnds = {},
226 Optional<bool> followPathBackwardsFromEndToStart = {}, Optional<bool> exactSplinePath = {}, bool generatePath = true,
227 Optional<MovementFadeObject> fadeObject = {},
229
239 void MoveRotate(uint32 id, RotateDirection direction, Optional<Milliseconds> time = {},
240 Optional<float> turnSpeed = {}, Optional<float> totalTurnAngle = {},
242 void MoveFormation(Unit* leader, float range, float angle, uint32 point1, uint32 point2);
243
244 void LaunchMoveSpline(std::function<void(Movement::MoveSplineInit& init)>&& initializer, uint32 id = 0, MovementGeneratorPriority priority = MOTION_PRIORITY_NORMAL, MovementGeneratorType type = EFFECT_MOTION_TYPE, Scripting::v2::ActionResultSetter<MovementStopReason>&& scriptResult = {});
245
246 private:
247 typedef std::unique_ptr<MovementGenerator, MovementGeneratorDeleter> MovementGeneratorPointer;
248 typedef std::multiset<MovementGenerator*, MovementGeneratorComparator> MotionMasterContainer;
249 typedef std::unordered_multimap<uint32, MovementGenerator const*> MotionMasterUnitStatesContainer;
250
251 void AddFlag(uint8 const flag) { _flags |= flag; }
252 bool HasFlag(uint8 const flag) const { return (_flags & flag) != 0; }
253 void RemoveFlag(uint8 const flag) { _flags &= ~flag; }
254
255 void ResolveDelayedActions();
256 void Remove(MotionMasterContainer::iterator iterator, bool active, bool movementInform);
257 void Pop(bool active, bool movementInform);
258 void DirectInitialize();
259 void DirectClear();
260 void DirectClearDefault();
261 void DirectClear(std::function<bool(MovementGenerator*)> const& filter);
262 void DirectAdd(MovementGenerator* movement, MovementSlot slot);
263
264 void Delete(MovementGenerator* movement, bool active, bool movementInform);
265 void DeleteDefault(bool active, bool movementInform);
266 void AddBaseUnitState(MovementGenerator const* movement);
267 void ClearBaseUnitState(MovementGenerator const* movement);
268 void ClearBaseUnitStates();
269
274 std::deque<DelayedAction> _delayedActions;
276};
277
278#endif // MOTIONMASTER_H
#define M_PI
Definition Common.h:118
#define TC_GAME_API
Definition Define.h:129
uint8_t uint8
Definition Define.h:156
uint16_t uint16
Definition Define.h:155
uint32_t uint32
Definition Define.h:154
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition Duration.h:24
static bool EmptyValidator()
MotionMasterDelayedActionType
@ MOTIONMASTER_DELAYED_ADD
@ MOTIONMASTER_DELAYED_REMOVE
@ MOTIONMASTER_DELAYED_REMOVE_TYPE
@ MOTIONMASTER_DELAYED_INITIALIZE
@ MOTIONMASTER_DELAYED_CLEAR_PRIORITY
@ MOTIONMASTER_DELAYED_CLEAR
@ MOTIONMASTER_DELAYED_CLEAR_SLOT
@ MOTIONMASTER_DELAYED_CLEAR_MODE
MotionMasterFlags
@ MOTIONMASTER_FLAG_DELAYED
@ MOTIONMASTER_FLAG_NONE
@ MOTIONMASTER_FLAG_INITIALIZING
@ MOTIONMASTER_FLAG_UPDATE
@ MOTIONMASTER_FLAG_INITIALIZATION_PENDING
@ MOTIONMASTER_FLAG_STATIC_INITIALIZATION_PENDING
MovementGeneratorMode
RotateDirection
MovementGeneratorPriority
@ MOTION_PRIORITY_NORMAL
MovementSlot
@ MOTION_SLOT_ACTIVE
@ MOTION_SLOT_DEFAULT
MovementWalkRunSpeedSelectionMode
MovementGeneratorType
@ EFFECT_MOTION_TYPE
#define SPEED_CHARGE
std::variant< std::monostate, Position, Unit const *, float > MovementFacingTarget
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
@ EVENT_CHARGE
UnitMoveType
AnimTier
Definition UnitDefines.h:69
Action
DelayedAction(DelayedActionDefine &&action, DelayedActionValidator &&validator, MotionMasterDelayedActionType type)
DelayedAction(DelayedActionDefine &&action, MotionMasterDelayedActionType type)
DelayedActionValidator Validator
DelayedActionDefine Action
bool HasFlag(uint8 const flag) const
std::multiset< MovementGenerator *, MovementGeneratorComparator > MotionMasterContainer
void MoveChase(Unit *target, float dist, float angle)
std::unique_ptr< MovementGenerator, MovementGeneratorDeleter > MovementGeneratorPointer
void AddFlag(uint8 const flag)
MotionMasterUnitStatesContainer _baseUnitStatesMap
void RemoveFlag(uint8 const flag)
std::function< void()> DelayedActionDefine
std::unordered_multimap< uint32, MovementGenerator const * > MotionMasterUnitStatesContainer
std::deque< DelayedAction > _delayedActions
MovementGeneratorPointer _defaultGenerator
std::function< bool()> DelayedActionValidator
MotionMasterContainer _generators
Definition Unit.h:635
STL namespace.
bool operator()(MovementGenerator const *a, MovementGenerator const *b) const
void operator()(MovementGenerator *a)
MovementGeneratorType Type