TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MotionMaster.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TRINITY_MOTIONMASTER_H
20 #define TRINITY_MOTIONMASTER_H
21 
22 #include "Common.h"
23 #include <vector>
24 #include "SharedDefines.h"
25 #include "Object.h"
26 
27 class MovementGenerator;
28 class Unit;
29 class PathGenerator;
30 
31 // Creature Entry ID used for waypoints show, visible only for GMs
32 #define VISUAL_WAYPOINT 1
33 
34 // values 0 ... MAX_DB_MOTION_TYPE-1 used in DB
36 {
37  IDLE_MOTION_TYPE = 0, // IdleMovementGenerator.h
38  RANDOM_MOTION_TYPE = 1, // RandomMovementGenerator.h
39  WAYPOINT_MOTION_TYPE = 2, // WaypointMovementGenerator.h
40  MAX_DB_MOTION_TYPE = 3, // *** this and below motion types can't be set in DB.
41  ANIMAL_RANDOM_MOTION_TYPE = MAX_DB_MOTION_TYPE, // AnimalRandomMovementGenerator.h
42  CONFUSED_MOTION_TYPE = 4, // ConfusedMovementGenerator.h
43  CHASE_MOTION_TYPE = 5, // TargetedMovementGenerator.h
44  HOME_MOTION_TYPE = 6, // HomeMovementGenerator.h
45  FLIGHT_MOTION_TYPE = 7, // WaypointMovementGenerator.h
46  POINT_MOTION_TYPE = 8, // PointMovementGenerator.h
47  FLEEING_MOTION_TYPE = 9, // FleeingMovementGenerator.h
48  DISTRACT_MOTION_TYPE = 10, // IdleMovementGenerator.h
49  ASSISTANCE_MOTION_TYPE= 11, // PointMovementGenerator.h (first part of flee for assistance)
50  ASSISTANCE_DISTRACT_MOTION_TYPE = 12, // IdleMovementGenerator.h (second part of flee for assistance)
51  TIMED_FLEEING_MOTION_TYPE = 13, // FleeingMovementGenerator.h (alt.second part of flee for assistance)
56 };
57 
59 {
64 };
65 
67 {
68  MMCF_NONE = 0,
69  MMCF_UPDATE = 1, // Clear or Expire called from update
70  MMCF_RESET = 2 // Flag if need top()->Reset()
71 };
72 
74 {
77 };
78 
79 // assume it is 25 yard per 0.6 second
80 #define SPEED_CHARGE 42.0f
81 
82 class TC_GAME_API MotionMaster //: private std::stack<MovementGenerator *>
83 {
84  private:
85  //typedef std::stack<MovementGenerator *> Impl;
87 
88  void pop()
89  {
90  if (empty())
91  return;
92 
93  Impl[_top] = NULL;
94  while (!empty() && !top())
95  --_top;
96  }
97  void push(_Ty _Val) { ++_top; Impl[_top] = _Val; }
98 
99  bool needInitTop() const
100  {
101  if (empty())
102  return false;
103  return _needInit[_top];
104  }
105  void InitTop();
106  public:
107 
108  explicit MotionMaster(Unit* unit) : _expList(NULL), _top(-1), _owner(unit), _cleanFlag(MMCF_NONE)
109  {
110  for (uint8 i = 0; i < MAX_MOTION_SLOT; ++i)
111  {
112  Impl[i] = NULL;
113  _needInit[i] = true;
114  }
115  }
116  ~MotionMaster();
117 
118  void Initialize();
119  void InitDefault();
120 
121  bool empty() const { return (_top < 0); }
122  int size() const { return _top + 1; }
123  _Ty top() const
124  {
125  ASSERT(!empty());
126  return Impl[_top];
127  }
128  _Ty GetMotionSlot(int slot) const
129  {
130  ASSERT(slot >= 0);
131  return Impl[slot];
132  }
133 
134  void DirectDelete(_Ty curr);
135  void DelayedDelete(_Ty curr);
136 
137  void UpdateMotion(uint32 diff);
138  void Clear(bool reset = true)
139  {
140  if (_cleanFlag & MMCF_UPDATE)
141  {
142  if (reset)
143  _cleanFlag |= MMCF_RESET;
144  else
145  _cleanFlag &= ~MMCF_RESET;
146  DelayedClean();
147  }
148  else
149  DirectClean(reset);
150  }
151  void MovementExpired(bool reset = true)
152  {
153  if (_cleanFlag & MMCF_UPDATE)
154  {
155  if (reset)
156  _cleanFlag |= MMCF_RESET;
157  else
158  _cleanFlag &= ~MMCF_RESET;
159  DelayedExpire();
160  }
161  else
162  DirectExpire(reset);
163  }
164 
165  void MoveIdle();
166  void MoveTargetedHome();
167  void MoveRandom(float spawndist = 0.0f);
168  void MoveFollow(Unit* target, float dist, float angle, MovementSlot slot = MOTION_SLOT_ACTIVE);
169  void MoveChase(Unit* target, float dist = 0.0f, float angle = 0.0f);
170  void MoveConfused();
171  void MoveFleeing(Unit* enemy, uint32 time = 0);
172  void MovePoint(uint32 id, Position const& pos, bool generatePath = true)
173  { MovePoint(id, pos.m_positionX, pos.m_positionY, pos.m_positionZ, generatePath); }
174  void MovePoint(uint32 id, float x, float y, float z, bool generatePath = true);
175 
176  // These two movement types should only be used with creatures having landing/takeoff animations
177  void MoveLand(uint32 id, Position const& pos);
178  void MoveTakeoff(uint32 id, Position const& pos);
179 
180  void MoveCharge(float x, float y, float z, float speed = SPEED_CHARGE, uint32 id = EVENT_CHARGE, bool generatePath = false);
181  void MoveCharge(PathGenerator const& path, float speed = SPEED_CHARGE);
182  void MoveKnockbackFrom(float srcX, float srcY, float speedXY, float speedZ);
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, uint32 arrivalSpellId = 0, ObjectGuid const& arrivalSpellTargetGuid = ObjectGuid::Empty)
185  {
186  MoveJump(pos.GetPositionX(), pos.GetPositionY(), pos.GetPositionZ(), pos.GetOrientation(), speedXY, speedZ, id, hasOrientation, arrivalSpellId, arrivalSpellTargetGuid);
187  }
188  void MoveJump(float x, float y, float z, float o, float speedXY, float speedZ, uint32 id = EVENT_JUMP, bool hasOrientation = false, uint32 arrivalSpellId = 0, ObjectGuid const& arrivalSpellTargetGuid = ObjectGuid::Empty);
189  void MoveCirclePath(float x, float y, float z, float radius, bool clockwise, uint8 stepCount);
190  void MoveSmoothPath(uint32 pointId, G3D::Vector3 const* pathPoints, size_t pathSize, bool walk);
191  void MoveFall(uint32 id = 0);
192 
193  void MoveSeekAssistance(float x, float y, float z);
194  void MoveSeekAssistanceDistract(uint32 timer);
195  void MoveTaxiFlight(uint32 path, uint32 pathnode);
196  void MoveDistract(uint32 time);
197  void MovePath(uint32 path_id, bool repeatable);
198  void MoveRotate(uint32 time, RotateDirection direction);
199 
200  MovementGeneratorType GetCurrentMovementGeneratorType() const;
201  MovementGeneratorType GetMotionSlotType(int slot) const;
202 
203  void propagateSpeedChange();
204 
205  bool GetDestination(float &x, float &y, float &z);
206  private:
207  void Mutate(MovementGenerator *m, MovementSlot slot); // use Move* functions instead
208 
209  void DirectClean(bool reset);
210  void DelayedClean();
211 
212  void DirectExpire(bool reset);
213  void DelayedExpire();
214 
215  typedef std::vector<_Ty> ExpireList;
216  ExpireList* _expList;
217  _Ty Impl[MAX_MOTION_SLOT];
218  int _top;
220  bool _needInit[MAX_MOTION_SLOT];
222 };
223 #endif
MMCleanFlag
Definition: MotionMaster.h:66
Definition: MotionMaster.h:76
Definition: MotionMaster.h:37
Definition: MotionMaster.h:53
Definition: MotionMaster.h:49
static ObjectGuid const Empty
Definition: ObjectGuid.h:196
uint8 _cleanFlag
Definition: MotionMaster.h:221
void push(_Ty _Val)
Definition: MotionMaster.h:97
void pop()
Definition: MotionMaster.h:88
Definition: MotionMaster.h:44
void Clear(bool reset=true)
Definition: MotionMaster.h:138
float m_positionY
Definition: Position.h:53
Definition: MotionMaster.h:68
Definition: MotionMaster.h:75
Definition: MotionMaster.h:62
Definition: MotionMaster.h:60
ExpireList * _expList
Definition: MotionMaster.h:216
Definition: MotionMaster.h:45
arena_t NULL
Definition: jemalloc_internal.h:624
Definition: MotionMaster.h:38
float m_positionX
Definition: Position.h:52
Definition: MotionMaster.h:40
Definition: MotionMaster.h:47
Definition: MotionMaster.h:61
Definition: MotionMaster.h:48
_Ty top() const
Definition: MotionMaster.h:123
Definition: Vector3.h:58
_Ty GetMotionSlot(int slot) const
Definition: MotionMaster.h:128
void MovePoint(uint32 id, Position const &pos, bool generatePath=true)
Definition: MotionMaster.h:172
int _top
Definition: MotionMaster.h:218
std::vector< _Ty > ExpireList
Definition: MotionMaster.h:215
float GetOrientation() const
Definition: Position.h:107
Definition: MotionMaster.h:69
Definition: MotionMaster.h:51
MovementGenerator * _Ty
Definition: MotionMaster.h:86
void MoveJump(Position const &pos, float speedXY, float speedZ, uint32 id=EVENT_JUMP, bool hasOrientation=false, uint32 arrivalSpellId=0, ObjectGuid const &arrivalSpellTargetGuid=ObjectGuid::Empty)
Definition: MotionMaster.h:184
Definition: MotionMaster.h:82
bool needInitTop() const
Definition: MotionMaster.h:99
float GetPositionY() const
Definition: Position.h:105
G3D::int16 z
Definition: Vector3int16.h:46
float GetPositionZ() const
Definition: Position.h:106
Definition: MotionMaster.h:54
uint32_t uint32
Definition: Define.h:150
static void Initialize(PCASC_SALSA20 pState, LPBYTE pbKey, DWORD cbKeyLength, LPBYTE pbVector)
Definition: CascDecrypt.cpp:81
G3D::int16 y
Definition: Vector2int16.h:38
Definition: PathGenerator.h:52
float m_positionZ
Definition: Position.h:54
Definition: MotionMaster.h:55
Definition: MotionMaster.h:63
Definition: MotionMaster.h:50
MovementSlot
Definition: MotionMaster.h:58
int size() const
Definition: MotionMaster.h:122
RotateDirection
Definition: MotionMaster.h:73
Definition: MotionMaster.h:70
#define TC_GAME_API
Definition: Define.h:134
#define SPEED_CHARGE
Definition: MotionMaster.h:80
MovementGeneratorType
Definition: MotionMaster.h:35
uint8_t uint8
Definition: Define.h:152
Definition: Position.h:27
#define ASSERT
Definition: Errors.h:55
Definition: SharedDefines.h:4414
Definition: SharedDefines.h:4413
Definition: ObjectGuid.h:189
Definition: MotionMaster.h:41
G3D::int16 x
Definition: Vector2int16.h:37
Definition: MotionMaster.h:42
Definition: MotionMaster.h:43
Definition: MovementGenerator.h:30
float GetPositionX() const
Definition: Position.h:104
void MovementExpired(bool reset=true)
Definition: MotionMaster.h:151
Unit * _owner
Definition: MotionMaster.h:219
Definition: Unit.h:1305
Definition: MotionMaster.h:39
Definition: MotionMaster.h:46
Definition: MotionMaster.h:52
bool empty() const
Definition: MotionMaster.h:121
MotionMaster(Unit *unit)
Definition: MotionMaster.h:108