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 "ObjectGuid.h"
23#include "Optional.h"
24#include "MovementDefines.h"
25#include "MovementGenerator.h"
26#include "SharedDefines.h"
27#include <deque>
28#include <functional>
29#include <set>
30#include <unordered_map>
31#include <vector>
32
33class PathGenerator;
34class Unit;
35class WorldObject;
36struct Position;
37struct SplineChainLink;
39struct WaypointPath;
40
41namespace Movement
42{
43 class MoveSplineInit;
44}
45
47{
49 MOTIONMASTER_FLAG_UPDATE = 0x1, // Update in progress
50 MOTIONMASTER_FLAG_STATIC_INITIALIZATION_PENDING = 0x2, // Static movement (MOTION_SLOT_DEFAULT) hasn't been initialized
51 MOTIONMASTER_FLAG_INITIALIZATION_PENDING = 0x4, // MotionMaster is stalled until signaled
52 MOTIONMASTER_FLAG_INITIALIZING = 0x8, // MotionMaster is initializing
53
55};
56
68
73
75{
76 public:
77 bool operator()(MovementGenerator const* a, MovementGenerator const* b) const;
78};
79
88
89static bool EmptyValidator()
90{
91 return true;
92}
93
95{
96 public:
97 typedef std::function<void()> DelayedActionDefine;
98 typedef std::function<bool()> DelayedActionValidator;
99
101 {
102 public:
103 explicit DelayedAction(DelayedActionDefine&& action, DelayedActionValidator&& validator, MotionMasterDelayedActionType type) : Action(std::move(action)), Validator(std::move(validator)), Type(type) { }
104 explicit DelayedAction(DelayedActionDefine&& action, MotionMasterDelayedActionType type) : Action(std::move(action)), Validator(EmptyValidator), Type(type) { }
106
107 void Resolve() { if (Validator()) Action(); }
108
112 };
113
114 explicit MotionMaster(Unit* unit);
116
117 void Initialize();
118 void InitializeDefault();
119 void AddToWorld();
120
121 bool Empty() const;
122 uint32 Size() const;
123 std::vector<MovementGeneratorInformation> GetMovementGeneratorsInformation() const;
124 MovementSlot GetCurrentSlot() const;
125 MovementGenerator* GetCurrentMovementGenerator() const;
126 MovementGeneratorType GetCurrentMovementGeneratorType() const;
127 MovementGeneratorType GetCurrentMovementGeneratorType(MovementSlot slot) const;
128 MovementGenerator* GetCurrentMovementGenerator(MovementSlot slot) const;
129 // Returns first found MovementGenerator that matches the given criteria
130 MovementGenerator* GetMovementGenerator(std::function<bool(MovementGenerator const*)> const& filter, MovementSlot slot = MOTION_SLOT_ACTIVE) const;
131 bool HasMovementGenerator(std::function<bool(MovementGenerator const*)> const& filter, MovementSlot slot = MOTION_SLOT_ACTIVE) const;
132
133 void Update(uint32 diff);
134 void Add(MovementGenerator* movement, MovementSlot slot = MOTION_SLOT_ACTIVE);
135 // NOTE: MOTION_SLOT_DEFAULT will be autofilled with IDLE_MOTION_TYPE
136 void Remove(MovementGenerator* movement, MovementSlot slot = MOTION_SLOT_ACTIVE);
137 // Removes first found movement
138 // NOTE: MOTION_SLOT_DEFAULT will be autofilled with IDLE_MOTION_TYPE
139 void Remove(MovementGeneratorType type, MovementSlot slot = MOTION_SLOT_ACTIVE);
140 // NOTE: MOTION_SLOT_DEFAULT wont be affected
141 void Clear();
142 // Removes all movements for the given MovementSlot
143 // NOTE: MOTION_SLOT_DEFAULT will be autofilled with IDLE_MOTION_TYPE
144 void Clear(MovementSlot slot);
145 // Removes all movements with the given MovementGeneratorPriority
146 // NOTE: MOTION_SLOT_DEFAULT wont be affected
147 void Clear(MovementGeneratorPriority priority);
148 void PropagateSpeedChange();
149 bool GetDestination(float &x, float &y, float &z);
150 bool StopOnDeath();
151 void InterruptOnTeleport();
152
153 void MoveIdle();
154 void MoveTargetedHome();
155 void MoveRandom(float wanderDistance = 0.0f);
156 void MoveFollow(Unit* target, float dist, ChaseAngle angle, MovementSlot slot = MOTION_SLOT_ACTIVE);
157 void MoveChase(Unit* target, Optional<ChaseRange> dist = {}, Optional<ChaseAngle> angle = {});
158 void MoveChase(Unit* target, float dist, float angle) { MoveChase(target, ChaseRange(dist), ChaseAngle(angle)); }
159 void MoveChase(Unit* target, float dist) { MoveChase(target, ChaseRange(dist)); }
160 void MoveConfused();
161 void MoveFleeing(Unit* enemy, uint32 time = 0);
162 void MovePoint(uint32 id, Position const& pos, bool generatePath = true, Optional<float> finalOrient = {});
163 void MovePoint(uint32 id, float x, float y, float z, bool generatePath = true, Optional<float> finalOrient = {});
164 /*
165 * Makes the unit move toward the target until it is at a certain distance from it. The unit then stops.
166 * Only works in 2D.
167 * This method doesn't account for any movement done by the target. in other words, it only works if the target is stationary.
168 */
169 void MoveCloserAndStop(uint32 id, Unit* target, float distance);
170 // These two movement types should only be used with creatures having landing/takeoff animations
171 void MoveLand(uint32 id, Position const& pos, Optional<float> velocity = {});
172 void MoveTakeoff(uint32 id, Position const& pos, Optional<float> velocity = {});
173 void MoveCharge(float x, float y, float z, float speed = SPEED_CHARGE, uint32 id = EVENT_CHARGE, bool generatePath = false);
174 void MoveCharge(PathGenerator const& path, float speed = SPEED_CHARGE);
175 void MoveKnockbackFrom(float srcX, float srcY, float speedXY, float speedZ);
176 void MoveJumpTo(float angle, float speedXY, float speedZ);
177 void MoveJump(Position const& pos, float speedXY, float speedZ, uint32 id = EVENT_JUMP, bool hasOrientation = false);
178 void MoveJump(float x, float y, float z, float o, float speedXY, float speedZ, uint32 id = EVENT_JUMP, bool hasOrientation = false);
179 void MoveCirclePath(float x, float y, float z, float radius, bool clockwise, uint8 stepCount);
180 void MoveSmoothPath(uint32 pointId, Position const* pathPoints, size_t pathSize, bool walk);
181 // Walk along spline chain stored in DB (script_spline_chain_meta and script_spline_chain_waypoints)
182 void MoveAlongSplineChain(uint32 pointId, uint16 dbChainId, bool walk);
183 void MoveAlongSplineChain(uint32 pointId, std::vector<SplineChainLink> const& chain, bool walk);
184 void ResumeSplineChain(SplineChainResumeInfo const& info);
185 void MoveFall(uint32 id = 0);
186 void MoveSeekAssistance(float x, float y, float z);
187 void MoveSeekAssistanceDistract(uint32 timer);
188 void MoveTaxiFlight(uint32 path, uint32 pathnode);
189 void MoveDistract(uint32 time, float orientation);
190 void MovePath(uint32 pathId, bool repeatable);
191 void MovePath(WaypointPath& path, bool repeatable);
192 void MoveRotate(uint32 id, uint32 time, RotateDirection direction);
193 void MoveFormation(Unit* leader, float range, float angle, uint32 point1, uint32 point2);
194 void MoveFace(float orientation, uint32 id = EVENT_FACE);
195 void MoveFace(WorldObject const* object, uint32 id = EVENT_FACE);
196
197 void LaunchMoveSpline(std::function<void(Movement::MoveSplineInit& init)>&& initializer, uint32 id = 0, MovementGeneratorPriority priority = MOTION_PRIORITY_NORMAL, MovementGeneratorType type = EFFECT_MOTION_TYPE);
198 private:
199 typedef std::unique_ptr<MovementGenerator, MovementGeneratorDeleter> MovementGeneratorPointer;
200 typedef std::set<MovementGenerator*, MovementGeneratorComparator> MotionMasterContainer;
201 typedef std::unordered_multimap<uint32, MovementGenerator const*> MotionMasterUnitStatesContainer;
202
203 void AddFlag(uint8 const flag) { _flags |= flag; }
204 bool HasFlag(uint8 const flag) const { return (_flags & flag) != 0; }
205 void RemoveFlag(uint8 const flag) { _flags &= ~flag; }
206
207 void ResolveDelayedActions();
208 void Remove(MotionMasterContainer::iterator& iterator, bool active, bool movementInform);
209 void Pop(bool active, bool movementInform);
210 void DirectInitialize();
211 void DirectClear();
212 void DirectClearDefault();
213 void DirectClear(std::function<bool(MovementGenerator*)> const& filter);
214 void DirectAdd(MovementGenerator* movement, MovementSlot slot);
215
216 void Delete(MovementGenerator* movement, bool active, bool movementInform);
217 void DeleteDefault(bool active, bool movementInform);
218 void AddBaseUnitState(MovementGenerator const* movement);
219 void ClearBaseUnitState(MovementGenerator const* movement);
220 void ClearBaseUnitStates();
221
226 std::deque<DelayedAction> _delayedActions;
228};
229
230#endif // MOTIONMASTER_H
#define TC_GAME_API
Definition Define.h:114
uint8_t uint8
Definition Define.h:135
uint16_t uint16
Definition Define.h:134
uint32_t uint32
Definition Define.h:133
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
MovementGeneratorType
@ EFFECT_MOTION_TYPE
#define SPEED_CHARGE
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
@ EVENT_FACE
@ EVENT_CHARGE
@ EVENT_JUMP
Action
uint32 const pathSize
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)
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:769
STL namespace.
bool operator()(MovementGenerator const *a, MovementGenerator const *b) const
void operator()(MovementGenerator *a)
MovementGeneratorType Type