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;
36class WorldObject;
37struct Position;
38struct SplineChainLink;
40struct WaypointPath;
41enum UnitMoveType : uint8;
42enum class AnimTier : uint8;
43
44namespace Movement
45{
46 class MoveSplineInit;
47 struct SpellEffectExtraData;
48}
49
51{
53 MOTIONMASTER_FLAG_UPDATE = 0x1, // Update in progress
54 MOTIONMASTER_FLAG_STATIC_INITIALIZATION_PENDING = 0x2, // Static movement (MOTION_SLOT_DEFAULT) hasn't been initialized
55 MOTIONMASTER_FLAG_INITIALIZATION_PENDING = 0x4, // MotionMaster is stalled until signaled
56 MOTIONMASTER_FLAG_INITIALIZING = 0x8, // MotionMaster is initializing
57
59};
60
72
77
79{
80 public:
81 bool operator()(MovementGenerator const* a, MovementGenerator const* b) const;
82};
83
92
93static bool EmptyValidator()
94{
95 return true;
96}
97
99{
100 public:
101 typedef std::function<void()> DelayedActionDefine;
102 typedef std::function<bool()> DelayedActionValidator;
103
105 {
106 public:
107 explicit DelayedAction(DelayedActionDefine&& action, DelayedActionValidator&& validator, MotionMasterDelayedActionType type) : Action(std::move(action)), Validator(std::move(validator)), Type(type) { }
108 explicit DelayedAction(DelayedActionDefine&& action, MotionMasterDelayedActionType type) : Action(std::move(action)), Validator(EmptyValidator), Type(type) { }
110
111 void Resolve() { if (Validator()) Action(); }
112
116 };
117
118 explicit MotionMaster(Unit* unit);
120
121 void Initialize();
122 void InitializeDefault();
123 void AddToWorld();
124
125 bool Empty() const;
126 uint32 Size() const;
127 std::vector<MovementGeneratorInformation> GetMovementGeneratorsInformation() const;
128 MovementSlot GetCurrentSlot() const;
129 MovementGenerator* GetCurrentMovementGenerator() const;
130 MovementGeneratorType GetCurrentMovementGeneratorType() const;
131 MovementGeneratorType GetCurrentMovementGeneratorType(MovementSlot slot) const;
132 MovementGenerator* GetCurrentMovementGenerator(MovementSlot slot) const;
133 // Returns first found MovementGenerator that matches the given criteria
134 MovementGenerator* GetMovementGenerator(std::function<bool(MovementGenerator const*)> const& filter, MovementSlot slot = MOTION_SLOT_ACTIVE) const;
135 bool HasMovementGenerator(std::function<bool(MovementGenerator const*)> const& filter, MovementSlot slot = MOTION_SLOT_ACTIVE) const;
136
137 void Update(uint32 diff);
138 void Add(MovementGenerator* movement, MovementSlot slot = MOTION_SLOT_ACTIVE);
139 // NOTE: MOTION_SLOT_DEFAULT will be autofilled with IDLE_MOTION_TYPE
141 // Removes first found movement
142 // NOTE: MOTION_SLOT_DEFAULT will be autofilled with IDLE_MOTION_TYPE
144 // NOTE: MOTION_SLOT_DEFAULT wont be affected
145 void Clear();
146 // Removes all movements for the given MovementSlot
147 // NOTE: MOTION_SLOT_DEFAULT will be autofilled with IDLE_MOTION_TYPE
148 void Clear(MovementSlot slot);
149 // Removes all movements with the given MovementGeneratorPriority
150 // NOTE: MOTION_SLOT_DEFAULT wont be affected
151 void Clear(MovementGeneratorPriority priority);
152 void PropagateSpeedChange();
153 bool GetDestination(float &x, float &y, float &z);
154 bool StopOnDeath();
155 void InterruptOnTeleport();
156
157 void MoveIdle();
158 void MoveTargetedHome();
159 void MoveRandom(float wanderDistance = 0.0f, Optional<Milliseconds> duration = {}, Optional<float> speed = {},
162 void MoveFollow(Unit* target, float dist, Optional<ChaseAngle> angle = {}, Optional<Milliseconds> duration = {}, bool ignoreTargetWalk = false, MovementSlot slot = MOTION_SLOT_ACTIVE,
164 void MoveChase(Unit* target, Optional<ChaseRange> dist = {}, Optional<ChaseAngle> angle = {});
165 void MoveChase(Unit* target, float dist, float angle) { MoveChase(target, ChaseRange(dist), ChaseAngle(angle)); }
166 void MoveConfused();
167 void MoveFleeing(Unit* enemy, Milliseconds time = 0ms,
169 void MovePoint(uint32 id, Position const& pos, bool generatePath = true, Optional<float> finalOrient = {}, Optional<float> speed = {},
171 Optional<MovementFadeObject> fadeObject = {},
173 void MovePoint(uint32 id, float x, float y, float z, bool generatePath = true, Optional<float> finalOrient = {}, Optional<float> speed = {},
175 Optional<MovementFadeObject> fadeObject = {},
177 /*
178 * Makes the unit move toward the target until it is at a certain distance from it. The unit then stops.
179 * Only works in 2D.
180 * This method doesn't account for any movement done by the target. in other words, it only works if the target is stationary.
181 */
182 void MoveCloserAndStop(uint32 id, Unit* target, float distance);
183 // These two movement types should only be used with creatures having landing/takeoff animations
184 void MoveLand(uint32 id, Position const& pos, Optional<int32> tierTransitionId = {}, Optional<float> velocity = {},
187 void MoveTakeoff(uint32 id, Position const& pos, Optional<int32> tierTransitionId = {}, Optional<float> velocity = {},
190 void MoveTierTransition(uint32 id, Position const& pos, AnimTier newAnimTier, Optional<int32> tierTransitionId = {}, Optional<float> velocity = {},
193 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);
194 void MoveCharge(PathGenerator const& path, float speed = SPEED_CHARGE, Unit const* target = nullptr, Movement::SpellEffectExtraData const* spellEffectExtraData = nullptr);
195 void MoveKnockbackFrom(Position const& origin, float speedXY, float speedZ, float angle = M_PI, Movement::SpellEffectExtraData const* spellEffectExtraData = nullptr);
196 void MoveJump(uint32 id, Position const& pos, std::variant<std::monostate, float, Milliseconds> speedOrTime = {},
197 Optional<float> minHeight = {}, Optional<float> maxHeight = {},
198 MovementFacingTarget const& facing = {}, bool orientationFixed = false, bool unlimitedSpeed = false, Optional<float> speedMultiplier = {},
199 JumpArrivalCastArgs const* arrivalCast = nullptr, Movement::SpellEffectExtraData const* spellEffectExtraData = nullptr,
201 void MoveCirclePath(float x, float y, float z, float radius, bool clockwise, uint8 stepCount,
202 Optional<Milliseconds> duration = {}, Optional<float> speed = {},
205 // Walk along spline chain stored in DB (script_spline_chain_meta and script_spline_chain_waypoints)
206 void MoveAlongSplineChain(uint32 pointId, uint16 dbChainId, bool walk);
207 void MoveAlongSplineChain(uint32 pointId, std::vector<SplineChainLink> const& chain, bool walk);
208 void ResumeSplineChain(SplineChainResumeInfo const& info);
209 void MoveFall(uint32 id = 0,
211 void MoveSeekAssistance(float x, float y, float z);
212 void MoveSeekAssistanceDistract(uint32 timer);
213 void MoveTaxiFlight(uint32 path, uint32 pathnode, Optional<float> speed = {},
215 void MoveDistract(uint32 time, float orientation);
216 void MovePath(uint32 pathId, bool repeatable, Optional<Milliseconds> duration = {}, Optional<float> speed = {},
218 Optional<std::pair<Milliseconds, Milliseconds>> waitTimeRangeAtPathEnd = {}, Optional<float> wanderDistanceAtPathEnds = {},
219 Optional<bool> followPathBackwardsFromEndToStart = {}, Optional<bool> exactSplinePath = {}, bool generatePath = true,
220 Optional<MovementFadeObject> fadeObject = {},
222 void MovePath(WaypointPath const& path, bool repeatable, Optional<Milliseconds> duration = {}, Optional<float> speed = {},
224 Optional<std::pair<Milliseconds, Milliseconds>> waitTimeRangeAtPathEnd = {}, Optional<float> wanderDistanceAtPathEnds = {},
225 Optional<bool> followPathBackwardsFromEndToStart = {}, Optional<bool> exactSplinePath = {}, bool generatePath = true,
226 Optional<MovementFadeObject> fadeObject = {},
228
238 void MoveRotate(uint32 id, RotateDirection direction, Optional<Milliseconds> time = {},
239 Optional<float> turnSpeed = {}, Optional<float> totalTurnAngle = {},
241 void MoveFormation(Unit* leader, float range, float angle, uint32 point1, uint32 point2);
242 void MoveFace(float orientation, uint32 id = EVENT_FACE);
243 void MoveFace(WorldObject const* object, uint32 id = EVENT_FACE);
244
245 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 = {});
246
247 private:
248 typedef std::unique_ptr<MovementGenerator, MovementGeneratorDeleter> MovementGeneratorPointer;
249 typedef std::set<MovementGenerator*, MovementGeneratorComparator> MotionMasterContainer;
250 typedef std::unordered_multimap<uint32, MovementGenerator const*> MotionMasterUnitStatesContainer;
251
252 void AddFlag(uint8 const flag) { _flags |= flag; }
253 bool HasFlag(uint8 const flag) const { return (_flags & flag) != 0; }
254 void RemoveFlag(uint8 const flag) { _flags &= ~flag; }
255
256 void ResolveDelayedActions();
257 void Remove(MotionMasterContainer::iterator& iterator, bool active, bool movementInform);
258 void Pop(bool active, bool movementInform);
259 void DirectInitialize();
260 void DirectClear();
261 void DirectClearDefault();
262 void DirectClear(std::function<bool(MovementGenerator*)> const& filter);
263 void DirectAdd(MovementGenerator* movement, MovementSlot slot);
264
265 void Delete(MovementGenerator* movement, bool active, bool movementInform);
266 void DeleteDefault(bool active, bool movementInform);
267 void AddBaseUnitState(MovementGenerator const* movement);
268 void ClearBaseUnitState(MovementGenerator const* movement);
269 void ClearBaseUnitStates();
270
275 std::deque<DelayedAction> _delayedActions;
277};
278
279#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
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_FACE
@ 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
std::set< MovementGenerator *, MovementGeneratorComparator > MotionMasterContainer
bool HasFlag(uint8 const flag) const
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:647
STL namespace.
bool operator()(MovementGenerator const *a, MovementGenerator const *b) const
void operator()(MovementGenerator *a)
MovementGeneratorType Type