TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RandomMovementGenerator< T > Class Template Reference

#include <RandomMovementGenerator.h>

Public Member Functions

 RandomMovementGenerator (float spawn_dist=0.0f)
 
void _setRandomLocation (T *)
 
void DoInitialize (T *)
 
void DoFinalize (T *)
 
void DoReset (T *)
 
bool DoUpdate (T *, const uint32)
 
bool GetResetPos (T *, float &x, float &y, float &z)
 
MovementGeneratorType GetMovementGeneratorType () const override
 
template<>
void _setRandomLocation (Creature *creature)
 
template<>
void DoInitialize (Creature *creature)
 
template<>
void DoReset (Creature *creature)
 
template<>
void DoFinalize (Creature *creature)
 
template<>
bool DoUpdate (Creature *creature, const uint32 diff)
 
template<>
bool GetResetPos (Creature *creature, float &x, float &y, float &z)
 
- Public Member Functions inherited from MovementGeneratorMedium< T, RandomMovementGenerator< T > >
void Initialize (Unit *u) override
 
void Finalize (Unit *u) override
 
void Reset (Unit *u) override
 
bool Update (Unit *u, uint32 time_diff) override
 
- Public Member Functions inherited from MovementGenerator
virtual ~MovementGenerator ()
 
virtual void unitSpeedChanged ()
 
virtual bool GetResetPosition (Unit *, float &, float &, float &)
 

Private Attributes

TimeTrackerSmall i_nextMoveTime
 
float wander_distance
 

Constructor & Destructor Documentation

template<class T >
RandomMovementGenerator< T >::RandomMovementGenerator ( float  spawn_dist = 0.0f)
inline
28 : i_nextMoveTime(0), wander_distance(spawn_dist) { }
TimeTrackerSmall i_nextMoveTime
Definition: RandomMovementGenerator.h:38
float wander_distance
Definition: RandomMovementGenerator.h:40

Member Function Documentation

template<class T >
void RandomMovementGenerator< T >::_setRandomLocation ( T *  )
template<>
void RandomMovementGenerator< Creature >::_setRandomLocation ( Creature creature)
35 {
36  float respX, respY, respZ, respO, destX, destY, destZ, travelDistZ;
37  creature->GetHomePosition(respX, respY, respZ, respO);
38  Map const* map = creature->GetBaseMap();
39 
40  // For 2D/3D system selection
41  //bool is_land_ok = creature.CanWalk(); // not used?
42  //bool is_water_ok = creature.CanSwim(); // not used?
43  bool is_air_ok = creature->CanFly();
44 
45  const float angle = float(rand_norm()) * static_cast<float>(M_PI*2.0f);
46  const float range = float(rand_norm()) * wander_distance;
47  const float distanceX = range * std::cos(angle);
48  const float distanceY = range * std::sin(angle);
49 
50  destX = respX + distanceX;
51  destY = respY + distanceY;
52 
53  // prevent invalid coordinates generation
56 
57  travelDistZ = range; // sin^2+cos^2=1, so travelDistZ=range^2; no need for sqrt below
58 
59  if (is_air_ok) // 3D system above ground and above water (flying mode)
60  {
61  // Limit height change
62  const float distanceZ = float(rand_norm()) * travelDistZ/2.0f;
63  destZ = respZ + distanceZ;
64  float levelZ = map->GetWaterOrGroundLevel(destX, destY, destZ-2.0f);
65 
66  // Problem here, we must fly above the ground and water, not under. Let's try on next tick
67  if (levelZ >= destZ)
68  return;
69  }
70  //else if (is_water_ok) // 3D system under water and above ground (swimming mode)
71  else // 2D only
72  {
73  // 10.0 is the max that vmap high can check (MAX_CAN_FALL_DISTANCE)
74  travelDistZ = travelDistZ >= 10.0f ? 10.0f : travelDistZ;
75 
76  // The fastest way to get an accurate result 90% of the time.
77  // Better result can be obtained like 99% accuracy with a ray light, but the cost is too high and the code is too long.
78  destZ = map->GetHeight(creature->GetPhaseMask(), destX, destY, respZ+travelDistZ-2.0f, false);
79 
80  if (std::fabs(destZ - respZ) > travelDistZ) // Map check
81  {
82  // Vmap Horizontal or above
83  destZ = map->GetHeight(creature->GetPhaseMask(), destX, destY, respZ - 2.0f, true);
84 
85  if (std::fabs(destZ - respZ) > travelDistZ)
86  {
87  // Vmap Higher
88  destZ = map->GetHeight(creature->GetPhaseMask(), destX, destY, respZ+travelDistZ-2.0f, true);
89 
90  // let's forget this bad coords where a z cannot be find and retry at next tick
91  if (std::fabs(destZ - respZ) > travelDistZ)
92  return;
93  }
94  }
95  }
96 
97  if (is_air_ok)
99  else
100  {
101  if (roll_chance_i(50))
102  i_nextMoveTime.Reset(urand(5000, 10000));
103  else
104  i_nextMoveTime.Reset(urand(50, 400));
105  }
106 
108 
109  Movement::MoveSplineInit init(creature);
110  init.MoveTo(destX, destY, destZ);
111  init.SetWalk(true);
112  init.Launch();
113 
114  //Call for creature group update
115  if (creature->GetFormation() && creature->GetFormation()->getLeader() == creature)
116  creature->GetFormation()->LeaderMoveTo(destX, destY, destZ);
117 }
TimeTrackerSmall i_nextMoveTime
Definition: RandomMovementGenerator.h:38
#define M_PI
Definition: Common.h:163
void AddUnitState(uint32 f)
Definition: Unit.h:1394
float GetHeight(float x, float y, float z, bool checkVMap=true, float maxSearchDist=DEFAULT_HEIGHT_SEARCH) const
Definition: Map.cpp:2314
Creature * getLeader() const
Definition: CreatureGroups.h:71
float GetWaterOrGroundLevel(float x, float y, float z, float *ground=NULL, bool swim=false) const
Definition: Map.cpp:2296
float wander_distance
Definition: RandomMovementGenerator.h:40
uint32 GetPhaseMask() const
Definition: Object.h:461
uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:45
bool roll_chance_i(int chance)
Definition: Random.h:53
Definition: Map.h:259
void Reset(uint32 interval)
Definition: Timer.h:153
bool CanFly() const override
Definition: Creature.h:500
void NormalizeMapCoord(float &c)
Definition: GridDefines.h:210
CreatureGroup * GetFormation()
Definition: Creature.h:687
Map const * GetBaseMap() const
Definition: Object.cpp:2199
double rand_norm()
Definition: Random.cpp:69
Definition: Unit.h:572
Definition: MoveSplineInit.h:52
void LeaderMoveTo(float x, float y, float z)
Definition: CreatureGroups.cpp:220
void GetHomePosition(float &x, float &y, float &z, float &ori) const
Definition: Creature.h:672

+ Here is the call graph for this function:

template<class T >
void RandomMovementGenerator< T >::DoFinalize ( T *  )
template<>
void RandomMovementGenerator< Creature >::DoFinalize ( Creature creature)
140 {
142  creature->SetWalk(false);
143 }
void ClearUnitState(uint32 f)
Definition: Unit.h:1396
Definition: Unit.h:554
Definition: Unit.h:572
bool SetWalk(bool enable)
Definition: Unit.cpp:15800

+ Here is the call graph for this function:

template<class T >
void RandomMovementGenerator< T >::DoInitialize ( T *  )
template<>
void RandomMovementGenerator< Creature >::DoInitialize ( Creature creature)
121 {
122  if (!creature->IsAlive())
123  return;
124 
125  if (!wander_distance)
126  wander_distance = creature->GetRespawnRadius();
127 
129  _setRandomLocation(creature);
130 }
void AddUnitState(uint32 f)
Definition: Unit.h:1394
float wander_distance
Definition: RandomMovementGenerator.h:40
float GetRespawnRadius() const
Definition: Creature.h:640
bool IsAlive() const
Definition: Unit.h:1692
Definition: Unit.h:554
Definition: Unit.h:572

+ Here is the call graph for this function:

template<class T >
void RandomMovementGenerator< T >::DoReset ( T *  )
template<>
void RandomMovementGenerator< Creature >::DoReset ( Creature creature)
134 {
135  DoInitialize(creature);
136 }
template<class T >
bool RandomMovementGenerator< T >::DoUpdate ( T *  ,
const uint32   
)
template<>
bool RandomMovementGenerator< Creature >::DoUpdate ( Creature creature,
const uint32  diff 
)
147 {
149  {
150  i_nextMoveTime.Reset(0); // Expire the timer
152  return true;
153  }
154 
155  if (creature->movespline->Finalized())
156  {
157  i_nextMoveTime.Update(diff);
158  if (i_nextMoveTime.Passed())
159  _setRandomLocation(creature);
160  }
161  return true;
162 }
Definition: Unit.h:562
TimeTrackerSmall i_nextMoveTime
Definition: RandomMovementGenerator.h:38
Movement::MoveSpline * movespline
Definition: Unit.h:2213
void ClearUnitState(uint32 f)
Definition: Unit.h:1396
void Update(int32 diff)
Definition: Timer.h:143
Definition: Unit.h:553
bool Finalized() const
Definition: MoveSpline.h:129
void Reset(uint32 interval)
Definition: Timer.h:153
Definition: Unit.h:560
Definition: Unit.h:572
bool HasUnitState(const uint32 f) const
Definition: Unit.h:1395
bool Passed() const
Definition: Timer.h:148

+ Here is the call graph for this function:

template<class T >
MovementGeneratorType RandomMovementGenerator< T >::GetMovementGeneratorType ( ) const
inlineoverridevirtual

Implements MovementGenerator.

36 { return RANDOM_MOTION_TYPE; }
Definition: MotionMaster.h:38
template<class T >
bool RandomMovementGenerator< T >::GetResetPos ( T *  ,
float &  x,
float &  y,
float &  z 
)
template<>
bool RandomMovementGenerator< Creature >::GetResetPos ( Creature creature,
float &  x,
float &  y,
float &  z 
)
166 {
167  float radius;
168  creature->GetRespawnPosition(x, y, z, NULL, &radius);
169 
170  // use current if in range
171  if (creature->IsWithinDist2d(x, y, radius))
172  creature->GetPosition(x, y, z);
173 
174  return true;
175 }
arena_t NULL
Definition: jemalloc_internal.h:624
bool IsWithinDist2d(float x, float y, float dist) const
Definition: Object.cpp:1648
G3D::int16 z
Definition: Vector3int16.h:46
void GetPosition(float &x, float &y) const
Definition: Position.h:109
G3D::int16 y
Definition: Vector2int16.h:38
void GetRespawnPosition(float &x, float &y, float &z, float *ori=nullptr, float *dist=nullptr) const
Definition: Creature.cpp:2355
G3D::int16 x
Definition: Vector2int16.h:37

+ Here is the call graph for this function:

Member Data Documentation

template<class T >
TimeTrackerSmall RandomMovementGenerator< T >::i_nextMoveTime
private
template<class T >
float RandomMovementGenerator< T >::wander_distance
private

The documentation for this class was generated from the following file: