AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
RandomMovementGenerator< T > Class Template Reference

#include "RandomMovementGenerator.h"

Inheritance diagram for RandomMovementGenerator< T >:
MovementGeneratorMedium< T, RandomMovementGenerator< T > > MovementGenerator

Public Member Functions

 RandomMovementGenerator (float wanderDistance=0.0f)
 
 ~RandomMovementGenerator ()
 
void _setRandomLocation (T *)
 
void DoInitialize (T *)
 
void DoFinalize (T *)
 
void DoReset (T *)
 
bool DoUpdate (T *, const uint32)
 
bool GetResetPosition (float &x, float &y, float &z)
 
MovementGeneratorType GetMovementGeneratorType ()
 
void _setRandomLocation (Creature *creature)
 
void DoInitialize (Creature *creature)
 
void DoReset (Creature *creature)
 
void DoFinalize (Creature *creature)
 
bool DoUpdate (Creature *creature, const uint32 diff)
 
bool GetResetPosition (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 uint32 GetSplineId () const
 
virtual void unitSpeedChanged ()
 
virtual void Pause (uint32)
 
virtual void Resume (uint32)
 

Private Attributes

TimeTrackerSmall _nextMoveTime
 
uint8 _moveCount
 
float _wanderDistance
 
std::unique_ptr< PathGenerator_pathGenerator
 
std::vector< G3D::Vector3 > _destinationPoints
 
std::vector< uint8_validPointsVector [RANDOM_POINTS_NUMBER+1]
 
uint8 _currentPoint
 
std::map< uint16, Movement::PointsArray_preComputedPaths
 
Position _initialPosition
 
Position _currDestPosition
 

Detailed Description

template<class T>
class RandomMovementGenerator< T >

Constructor & Destructor Documentation

◆ RandomMovementGenerator()

template<class T >
RandomMovementGenerator< T >::RandomMovementGenerator ( float  wanderDistance = 0.0f)
inline
36 {
37 _initialPosition.Relocate(0.0f, 0.0f, 0.0f, 0.0f);
39
40 for (uint8 i = 0; i < RANDOM_POINTS_NUMBER; ++i)
41 {
43 for (uint8 j = 0; j < RANDOM_LINKS_COUNT; ++j)
45 }
46
48 for (uint8 i = 0; i < RANDOM_POINTS_NUMBER; ++i)
50 }
std::uint8_t uint8
Definition Define.h:109
#define RANDOM_POINTS_NUMBER
Definition RandomMovementGenerator.h:25
#define RANDOM_LINKS_COUNT
Definition RandomMovementGenerator.h:26
std::vector< G3D::Vector3 > _destinationPoints
Definition RandomMovementGenerator.h:66
uint8 _moveCount
Definition RandomMovementGenerator.h:63
std::vector< uint8 > _validPointsVector[RANDOM_POINTS_NUMBER+1]
Definition RandomMovementGenerator.h:67
Position _initialPosition
Definition RandomMovementGenerator.h:70
std::unique_ptr< PathGenerator > _pathGenerator
Definition RandomMovementGenerator.h:65
uint8 _currentPoint
Definition RandomMovementGenerator.h:68
float _wanderDistance
Definition RandomMovementGenerator.h:64
TimeTrackerSmall _nextMoveTime
Definition RandomMovementGenerator.h:62
void Relocate(float x, float y)
Definition Position.h:77

References RandomMovementGenerator< T >::_destinationPoints, RandomMovementGenerator< T >::_initialPosition, RandomMovementGenerator< T >::_validPointsVector, RANDOM_LINKS_COUNT, RANDOM_POINTS_NUMBER, and Position::Relocate().

◆ ~RandomMovementGenerator()

template<class T >
template RandomMovementGenerator< T >::~RandomMovementGenerator ( )
31{ }

Member Function Documentation

◆ _setRandomLocation() [1/2]

void RandomMovementGenerator< Creature >::_setRandomLocation ( Creature creature)
37{
38 if (!creature)
39 return;
40
42 return;
43
45 {
46 if (_currentPoint == RANDOM_POINTS_NUMBER) // cant go anywhere from initial position, lets stay
47 return;
48 // go back to initial position and will never return to this point
52 Movement::MoveSplineInit init(creature);
54 init.SetWalk(true);
55 init.Launch();
56 if (creature->GetFormation() && creature->GetFormation()->GetLeader() == creature)
58 return;
59 }
60
61 uint8 random = urand(0, _validPointsVector[_currentPoint].size() - 1);
62 std::vector<uint8>::iterator randomIter = _validPointsVector[_currentPoint].begin() + random;
63 uint8 newPoint = *randomIter;
64 uint16 pathIdx = uint16(_currentPoint * RANDOM_POINTS_NUMBER + newPoint);
65
66 // cant go anywhere from new point, so dont go there to not be stuck
67 if (_validPointsVector[newPoint].empty())
68 {
69 _validPointsVector[_currentPoint].erase(randomIter);
70 return;
71 }
72
73 Movement::PointsArray& finalPath = _preComputedPaths[pathIdx];
74 if (finalPath.empty())
75 {
76 Map* map = creature->GetMap();
77 float x = _destinationPoints[newPoint].x, y = _destinationPoints[newPoint].y, z = _destinationPoints[newPoint].z;
78 // invalid coordinates
79 if (!Acore::IsValidMapCoord(x, y))
80 {
81 _validPointsVector[_currentPoint].erase(randomIter);
82 _preComputedPaths.erase(pathIdx);
83 return;
84 }
85
86 float ground = INVALID_HEIGHT;
87 float levelZ = creature->GetMapWaterOrGroundLevel(x, y, z, &ground);
88 float newZ = INVALID_HEIGHT;
89
90 // flying creature
91 if (creature->CanFly())
92 newZ = std::max<float>(levelZ, z + rand_norm() * _wanderDistance / 2.0f);
93 // point underwater
94 else if (ground < levelZ)
95 {
96 if (!creature->CanEnterWater())
97 {
98 _validPointsVector[_currentPoint].erase(randomIter);
99 _preComputedPaths.erase(pathIdx);
100 return;
101 }
102 else
103 {
104 if (levelZ > INVALID_HEIGHT)
105 newZ = std::min<float>(levelZ - 2.0f, z + rand_norm() * _wanderDistance / 2.0f);
106 newZ = std::max<float>(ground, newZ);
107 }
108 }
109 // point on ground
110 else
111 {
112 if (levelZ <= INVALID_HEIGHT || !creature->CanWalk())
113 {
114 _validPointsVector[_currentPoint].erase(randomIter);
115 _preComputedPaths.erase(pathIdx);
116 return;
117 }
118 }
119
120 creature->UpdateAllowedPositionZ(x, y, newZ);
121
122 if (newZ > INVALID_HEIGHT)
123 {
124 // flying / swiming creature - dest not in los
125 if (!creature->IsWithinLOS(x, y, newZ))
126 {
127 _validPointsVector[_currentPoint].erase(randomIter);
128 _preComputedPaths.erase(pathIdx);
129 return;
130 }
131
132 finalPath.push_back(G3D::Vector3(creature->GetPositionX(), creature->GetPositionY(), creature->GetPositionZ()));
133 finalPath.push_back(G3D::Vector3(x, y, newZ));
134 }
135 else // ground
136 {
137 if (!_pathGenerator)
138 _pathGenerator = std::make_unique<PathGenerator>(creature);
139 else
140 _pathGenerator->Clear();
141
142 bool result = _pathGenerator->CalculatePath(x, y, levelZ, false);
143 if (result && !(_pathGenerator->GetPathType() & PATHFIND_NOPATH))
144 {
145 // generated path is too long
146 float pathLen = _pathGenerator->getPathLength();
147 if (pathLen * pathLen > creature->GetExactDistSq(x, y, levelZ) * MAX_PATH_LENGHT_FACTOR * MAX_PATH_LENGHT_FACTOR)
148 {
149 _validPointsVector[_currentPoint].erase(randomIter);
150 _preComputedPaths.erase(pathIdx);
151 return;
152 }
153
154 finalPath = _pathGenerator->GetPath();
155 Movement::PointsArray::iterator itr = finalPath.begin();
156 Movement::PointsArray::iterator itrNext = finalPath.begin() + 1;
157 float zDiff, distDiff;
158
159 for (; itrNext != finalPath.end(); ++itr, ++itrNext)
160 {
161 distDiff = std::sqrt(((*itr).x - (*itrNext).x) * ((*itr).x - (*itrNext).x) + ((*itr).y - (*itrNext).y) * ((*itr).y - (*itrNext).y));
162 zDiff = std::fabs((*itr).z - (*itrNext).z);
163
164 // Xinef: tree climbing, cut as much as we can
165 if (zDiff > 2.0f ||
166 (G3D::fuzzyNe(zDiff, 0.0f) && distDiff / zDiff < 2.15f)) // ~25˚
167 {
168 _validPointsVector[_currentPoint].erase(randomIter);
169 _preComputedPaths.erase(pathIdx);
170 return;
171 }
172
173 if (!map->isInLineOfSight((*itr).x, (*itr).y, (*itr).z + 2.f, (*itrNext).x, (*itrNext).y, (*itrNext).z + 2.f, creature->GetPhaseMask(),
175 {
176 _validPointsVector[_currentPoint].erase(randomIter);
177 _preComputedPaths.erase(pathIdx);
178 return;
179 }
180 }
181
182 // no valid path
183 if (finalPath.size() < 2)
184 {
185 _validPointsVector[_currentPoint].erase(randomIter);
186 _preComputedPaths.erase(pathIdx);
187 return;
188 }
189 }
190 else
191 {
192 _validPointsVector[_currentPoint].erase(randomIter);
193 _preComputedPaths.erase(pathIdx);
194 return;
195 }
196 }
197 }
198
199 _currentPoint = newPoint;
200 G3D::Vector3& finalPoint = finalPath[finalPath.size() - 1];
201 _currDestPosition.Relocate(finalPoint.x, finalPoint.y, finalPoint.z);
202
204 bool walk = true;
205 switch (creature->GetMovementTemplate().GetRandom())
206 {
208 walk = creature->IsWalking();
209 break;
211 walk = false;
212 break;
213 default:
214 break;
215 }
216
217 Movement::MoveSplineInit init(creature);
218 init.MovebyPath(finalPath);
219 init.SetWalk(walk);
220 init.Launch();
221
222 ++_moveCount;
223 if (roll_chance_i((int32) _moveCount * 25 + 10))
224 {
225 _moveCount = 0;
226 _nextMoveTime.Reset(urand(4000, 8000));
227 }
229 _preComputedPaths.erase(pathIdx);
230
231 //Call for creature group update
232 if (creature->GetFormation() && creature->GetFormation()->GetLeader() == creature)
233 creature->GetFormation()->LeaderMoveTo(finalPoint.x, finalPoint.y, finalPoint.z, 0);
234}
std::int32_t int32
Definition Define.h:103
std::uint16_t uint16
Definition Define.h:108
#define INVALID_HEIGHT
Definition GridTerrainData.h:27
@ CONFIG_DONT_CACHE_RANDOM_MOVEMENT_PATHS
Definition IWorld.h:141
@ LINEOFSIGHT_ALL_CHECKS
Definition Map.h:106
@ MAP_OBJECT_CELL_MOVE_NONE
Definition Object.h:387
@ PATHFIND_NOPATH
Definition PathGenerator.h:50
#define MAX_PATH_LENGHT_FACTOR
Definition RandomMovementGenerator.h:29
uint32 urand(uint32 min, uint32 max)
Definition Random.cpp:44
double rand_norm()
Definition Random.cpp:77
bool roll_chance_i(int chance)
Definition Random.h:60
@ UNIT_STATE_ROAMING_MOVE
Definition UnitDefines.h:192
Creature * GetLeader() const
Definition CreatureGroups.h:99
void LeaderMoveTo(float x, float y, float z, uint32 move_type)
Definition CreatureGroups.cpp:347
bool CanFly() const override
Definition Creature.h:82
CreatureGroup const * GetFormation() const
Definition Creature.h:357
bool CanEnterWater() const override
Definition Creature.cpp:3288
CreatureMovementData const & GetMovementTemplate() const
Definition Creature.cpp:2997
Definition Map.h:156
bool isInLineOfSight(float x1, float y1, float z1, float x2, float y2, float z2, uint32 phasemask, LineOfSightChecks checks, VMAP::ModelIgnoreFlags ignoreFlags) const
Definition Map.cpp:1592
MapObjectCellMoveState _moveState
Definition Object.h:406
Definition MoveSplineInit.h:71
std::map< uint16, Movement::PointsArray > _preComputedPaths
Definition RandomMovementGenerator.h:69
Position _currDestPosition
Definition RandomMovementGenerator.h:70
void AddUnitState(uint32 f)
Definition Unit.h:706
bool IsWalking() const
Definition Unit.h:1627
uint32 GetPhaseMask() const
Definition Object.h:451
Map * GetMap() const
Definition Object.h:536
void UpdateAllowedPositionZ(float x, float y, float &z, float *groundZ=nullptr) const
Definition Object.cpp:1565
bool IsWithinLOS(float x, float y, float z, VMAP::ModelIgnoreFlags ignoreFlags=VMAP::ModelIgnoreFlags::Nothing, LineOfSightChecks checks=LINEOFSIGHT_ALL_CHECKS) const
Definition Object.cpp:1337
float GetMapWaterOrGroundLevel(Position pos, float *ground=nullptr) const
Definition Object.h:614
#define sWorld
Definition World.h:363
bool IsValidMapCoord(float c)
Definition GridDefines.h:206
std::vector< Vector3 > PointsArray
Definition MoveSplineInitArgs.h:28
CreatureRandomMovementType GetRandom() const
Definition CreatureData.h:155
float GetPositionZ() const
Definition Position.h:123
float GetPositionX() const
Definition Position.h:121
float GetPositionY() const
Definition Position.h:122
float GetExactDistSq(float x, float y, float z) const
Definition Position.h:174
void Reset(int32 interval)
Definition Timer.h:249

References MovableMapObject::_moveState, Unit::AddUnitState(), AlwaysRun, Creature::CanEnterWater(), Creature::CanFly(), CanRun, CanWalk, CONFIG_DONT_CACHE_RANDOM_MOVEMENT_PATHS, Position::GetExactDistSq(), Creature::GetFormation(), CreatureGroup::GetLeader(), WorldObject::GetMap(), WorldObject::GetMapWaterOrGroundLevel(), Creature::GetMovementTemplate(), WorldObject::GetPhaseMask(), Position::GetPositionX(), Position::GetPositionY(), Position::GetPositionZ(), CreatureMovementData::GetRandom(), INVALID_HEIGHT, Map::isInLineOfSight(), Acore::IsValidMapCoord(), Unit::IsWalking(), WorldObject::IsWithinLOS(), Movement::MoveSplineInit::Launch(), CreatureGroup::LeaderMoveTo(), LINEOFSIGHT_ALL_CHECKS, MAP_OBJECT_CELL_MOVE_NONE, MAX_PATH_LENGHT_FACTOR, Movement::MoveSplineInit::MovebyPath(), Movement::MoveSplineInit::MoveTo(), VMAP::Nothing, PATHFIND_NOPATH, rand_norm(), RANDOM_POINTS_NUMBER, roll_chance_i(), Movement::MoveSplineInit::SetWalk(), sWorld, UNIT_STATE_ROAMING_MOVE, WorldObject::UpdateAllowedPositionZ(), and urand().

◆ _setRandomLocation() [2/2]

template<class T >
void RandomMovementGenerator< T >::_setRandomLocation ( T *  )

◆ DoFinalize() [1/2]

void RandomMovementGenerator< Creature >::DoFinalize ( Creature creature)
271{
273 creature->SetWalk(false);
274}
@ UNIT_STATE_ROAMING
Definition UnitDefines.h:174
bool SetWalk(bool enable) override
Enable or disable the creature's walk mode by removing: MOVEMENTFLAG_WALKING. Infom also the client.
Definition Creature.cpp:3208
void ClearUnitState(uint32 f)
Definition Unit.h:708

References Unit::ClearUnitState(), Creature::SetWalk(), UNIT_STATE_ROAMING, and UNIT_STATE_ROAMING_MOVE.

◆ DoFinalize() [2/2]

template<class T >
void RandomMovementGenerator< T >::DoFinalize ( T *  )

◆ DoInitialize() [1/2]

void RandomMovementGenerator< Creature >::DoInitialize ( Creature creature)
238{
239 if (!creature->IsAlive())
240 return;
241
242 if (!_wanderDistance)
244
245 _nextMoveTime.Reset(creature->GetSpawnId() && creature->GetWanderDistance() == _wanderDistance ? urand(1, 5000) : 0);
246 _wanderDistance = std::max((creature->GetWanderDistance() == _wanderDistance && creature->GetInstanceId() == 0) ? (creature->CanFly() ? MIN_WANDER_DISTANCE_AIR : MIN_WANDER_DISTANCE_GROUND) : 0.0f, _wanderDistance);
247
248 if (G3D::fuzzyEq(_initialPosition.GetExactDist2d(0.0f, 0.0f), 0.0f))
249 {
250 _initialPosition.Relocate(creature);
251 _destinationPoints.clear();
252 for (uint8 i = 0; i < RANDOM_POINTS_NUMBER; ++i)
253 {
254 float angle = (M_PI * 2.0f / (float)RANDOM_POINTS_NUMBER) * i;
255 float factor = 0.5f + rand_norm() * 0.5f;
256 _destinationPoints.push_back(G3D::Vector3(_initialPosition.GetPositionX() + _wanderDistance * cos(angle)*factor, _initialPosition.GetPositionY() + _wanderDistance * std::sin(angle)*factor, _initialPosition.GetPositionZ()));
257 }
258 }
259
261}
#define MIN_WANDER_DISTANCE_AIR
Definition RandomMovementGenerator.h:28
#define MIN_WANDER_DISTANCE_GROUND
Definition RandomMovementGenerator.h:27
ObjectGuid::LowType GetSpawnId() const
Definition Creature.h:65
float GetWanderDistance() const
Definition Creature.h:306
bool IsAlive() const
Definition Unit.h:1707
uint32 GetInstanceId() const
Definition Object.h:448
float GetExactDist2d(const float x, const float y) const
Definition Position.h:170

References Unit::AddUnitState(), Creature::CanFly(), WorldObject::GetInstanceId(), Creature::GetSpawnId(), Creature::GetWanderDistance(), Unit::IsAlive(), MIN_WANDER_DISTANCE_AIR, MIN_WANDER_DISTANCE_GROUND, rand_norm(), RANDOM_POINTS_NUMBER, UNIT_STATE_ROAMING, UNIT_STATE_ROAMING_MOVE, and urand().

◆ DoInitialize() [2/2]

template<class T >
void RandomMovementGenerator< T >::DoInitialize ( T *  )

◆ DoReset() [1/2]

void RandomMovementGenerator< Creature >::DoReset ( Creature creature)
265{
266 DoInitialize(creature);
267}

◆ DoReset() [2/2]

template<class T >
void RandomMovementGenerator< T >::DoReset ( T *  )

◆ DoUpdate() [1/2]

bool RandomMovementGenerator< Creature >::DoUpdate ( Creature creature,
const uint32  diff 
)
278{
280 {
281 _nextMoveTime.Reset(0); // Expire the timer
282 creature->StopMoving();
283 return true;
284 }
285
286 // xinef: if we got disable move flag, do not remove default generator - just prevent movement
287 if (creature->HasUnitFlag(UNIT_FLAG_DISABLE_MOVE))
288 {
289 _nextMoveTime.Reset(0); // Expire the timer
291 return true;
292 }
293
294 if (creature->movespline->Finalized())
295 {
296 _nextMoveTime.Update(diff);
297 if (_nextMoveTime.Passed())
298 _setRandomLocation(creature);
299 }
300 return true;
301}
@ UNIT_STATE_NOT_MOVE
Definition UnitDefines.h:218
@ UNIT_FLAG_DISABLE_MOVE
Definition UnitDefines.h:252
bool IsMovementPreventedByCasting() const override
Definition Creature.cpp:3655
bool Finalized() const
Definition MoveSpline.h:116
Movement::MoveSpline * movespline
Definition Unit.h:2029
bool HasUnitFlag(UnitFlags flags) const
Definition Unit.h:718
void StopMoving()
Definition Unit.cpp:16702
bool HasUnitState(const uint32 f) const
Definition Unit.h:707
void Update(int32 diff)
Definition Timer.h:239
bool Passed() const
Definition Timer.h:244

References Unit::ClearUnitState(), Movement::MoveSpline::Finalized(), Unit::HasUnitFlag(), Unit::HasUnitState(), Creature::IsMovementPreventedByCasting(), Unit::movespline, Unit::StopMoving(), UNIT_FLAG_DISABLE_MOVE, UNIT_STATE_NOT_MOVE, and UNIT_STATE_ROAMING_MOVE.

◆ DoUpdate() [2/2]

template<class T >
bool RandomMovementGenerator< T >::DoUpdate ( T *  ,
const uint32   
)

◆ GetMovementGeneratorType()

template<class T >
MovementGeneratorType RandomMovementGenerator< T >::GetMovementGeneratorType ( )
inlinevirtual

Implements MovementGenerator.

59{ return RANDOM_MOTION_TYPE; }
@ RANDOM_MOTION_TYPE
Definition MotionMaster.h:40

References RANDOM_MOTION_TYPE.

◆ GetResetPosition() [1/2]

bool RandomMovementGenerator< Creature >::GetResetPosition ( float &  x,
float &  y,
float &  z 
)
virtual

Reimplemented from MovementGenerator.

305{
308 else if (G3D::fuzzyNe(_initialPosition.GetExactDist2d(0.0f, 0.0f), 0.0f)) // if initial position is not 0.0f, 0.0f
310 else
311 return false;
312 return true;
313}
void GetPosition(float &x, float &y) const
Definition Position.h:126

References RANDOM_POINTS_NUMBER.

◆ GetResetPosition() [2/2]

template<class T >
bool RandomMovementGenerator< T >::GetResetPosition ( float &  x,
float &  y,
float &  z 
)
virtual

Reimplemented from MovementGenerator.

Member Data Documentation

◆ _currDestPosition

template<class T >
Position RandomMovementGenerator< T >::_currDestPosition
private

◆ _currentPoint

template<class T >
uint8 RandomMovementGenerator< T >::_currentPoint
private

◆ _destinationPoints

template<class T >
std::vector<G3D::Vector3> RandomMovementGenerator< T >::_destinationPoints
private

◆ _initialPosition

template<class T >
Position RandomMovementGenerator< T >::_initialPosition
private

◆ _moveCount

template<class T >
uint8 RandomMovementGenerator< T >::_moveCount
private

◆ _nextMoveTime

template<class T >
TimeTrackerSmall RandomMovementGenerator< T >::_nextMoveTime
private

◆ _pathGenerator

template<class T >
std::unique_ptr<PathGenerator> RandomMovementGenerator< T >::_pathGenerator
private

◆ _preComputedPaths

template<class T >
std::map<uint16, Movement::PointsArray> RandomMovementGenerator< T >::_preComputedPaths
private

◆ _validPointsVector

template<class T >
std::vector<uint8> RandomMovementGenerator< T >::_validPointsVector[RANDOM_POINTS_NUMBER+1]
private

◆ _wanderDistance

template<class T >
float RandomMovementGenerator< T >::_wanderDistance
private

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