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
55 bool walk = true;
56 switch (creature->GetMovementTemplate().GetRandom())
57 {
59 walk = creature->IsWalking();
60 break;
62 walk = false;
63 break;
64 default:
65 break;
66 }
67
68 init.SetWalk(walk);
69 init.Launch();
70 if (creature->GetFormation() && creature->GetFormation()->GetLeader() == creature)
72 return;
73 }
74
75 uint8 random = urand(0, _validPointsVector[_currentPoint].size() - 1);
76 std::vector<uint8>::iterator randomIter = _validPointsVector[_currentPoint].begin() + random;
77 uint8 newPoint = *randomIter;
78 uint16 pathIdx = uint16(_currentPoint * RANDOM_POINTS_NUMBER + newPoint);
79
80 // cant go anywhere from new point, so dont go there to not be stuck
81 if (_validPointsVector[newPoint].empty())
82 {
83 _validPointsVector[_currentPoint].erase(randomIter);
84 return;
85 }
86
87 Movement::PointsArray& finalPath = _preComputedPaths[pathIdx];
88 if (finalPath.empty())
89 {
90 Map* map = creature->GetMap();
91 float x = _destinationPoints[newPoint].x, y = _destinationPoints[newPoint].y, z = _destinationPoints[newPoint].z;
92 // invalid coordinates
93 if (!Acore::IsValidMapCoord(x, y))
94 {
95 _validPointsVector[_currentPoint].erase(randomIter);
96 _preComputedPaths.erase(pathIdx);
97 return;
98 }
99
100 float ground = INVALID_HEIGHT;
101 float levelZ = creature->GetMapWaterOrGroundLevel(x, y, z, &ground);
102 float newZ = INVALID_HEIGHT;
103
104 // flying creature
105 if (creature->CanFly())
106 newZ = std::max<float>(levelZ, z + rand_norm() * _wanderDistance / 2.0f);
107 // point underwater
108 else if (ground < levelZ)
109 {
110 if (!creature->CanEnterWater())
111 {
112 _validPointsVector[_currentPoint].erase(randomIter);
113 _preComputedPaths.erase(pathIdx);
114 return;
115 }
116 else
117 {
118 if (levelZ > INVALID_HEIGHT)
119 newZ = std::min<float>(levelZ - 2.0f, z + rand_norm() * _wanderDistance / 2.0f);
120 newZ = std::max<float>(ground, newZ);
121 }
122 }
123 // point on ground
124 else
125 {
126 if (levelZ <= INVALID_HEIGHT || !creature->CanWalk())
127 {
128 _validPointsVector[_currentPoint].erase(randomIter);
129 _preComputedPaths.erase(pathIdx);
130 return;
131 }
132 }
133
134 creature->UpdateAllowedPositionZ(x, y, newZ);
135
136 if (newZ > INVALID_HEIGHT)
137 {
138 // flying / swiming creature - dest not in los
139 if (!creature->IsWithinLOS(x, y, newZ))
140 {
141 _validPointsVector[_currentPoint].erase(randomIter);
142 _preComputedPaths.erase(pathIdx);
143 return;
144 }
145
146 finalPath.push_back(G3D::Vector3(creature->GetPositionX(), creature->GetPositionY(), creature->GetPositionZ()));
147 finalPath.push_back(G3D::Vector3(x, y, newZ));
148 }
149 else // ground
150 {
151 if (!_pathGenerator)
152 _pathGenerator = std::make_unique<PathGenerator>(creature);
153 else
154 _pathGenerator->Clear();
155
156 bool result = _pathGenerator->CalculatePath(x, y, levelZ, false);
157 if (result && !(_pathGenerator->GetPathType() & PATHFIND_NOPATH))
158 {
159 // generated path is too long
160 float pathLen = _pathGenerator->getPathLength();
161 if (pathLen * pathLen > creature->GetExactDistSq(x, y, levelZ) * MAX_PATH_LENGHT_FACTOR * MAX_PATH_LENGHT_FACTOR)
162 {
163 _validPointsVector[_currentPoint].erase(randomIter);
164 _preComputedPaths.erase(pathIdx);
165 return;
166 }
167
168 finalPath = _pathGenerator->GetPath();
169 Movement::PointsArray::iterator itr = finalPath.begin();
170 Movement::PointsArray::iterator itrNext = finalPath.begin() + 1;
171 float zDiff, distDiff;
172
173 for (; itrNext != finalPath.end(); ++itr, ++itrNext)
174 {
175 distDiff = std::sqrt(((*itr).x - (*itrNext).x) * ((*itr).x - (*itrNext).x) + ((*itr).y - (*itrNext).y) * ((*itr).y - (*itrNext).y));
176 zDiff = std::fabs((*itr).z - (*itrNext).z);
177
178 // Xinef: tree climbing, cut as much as we can
179 if (zDiff > 2.0f ||
180 (G3D::fuzzyNe(zDiff, 0.0f) && distDiff / zDiff < 2.15f)) // ~25Ëš
181 {
182 _validPointsVector[_currentPoint].erase(randomIter);
183 _preComputedPaths.erase(pathIdx);
184 return;
185 }
186
187 if (!map->isInLineOfSight((*itr).x, (*itr).y, (*itr).z + 2.f, (*itrNext).x, (*itrNext).y, (*itrNext).z + 2.f, creature->GetPhaseMask(),
189 {
190 _validPointsVector[_currentPoint].erase(randomIter);
191 _preComputedPaths.erase(pathIdx);
192 return;
193 }
194 }
195
196 // no valid path
197 if (finalPath.size() < 2)
198 {
199 _validPointsVector[_currentPoint].erase(randomIter);
200 _preComputedPaths.erase(pathIdx);
201 return;
202 }
203 }
204 else
205 {
206 _validPointsVector[_currentPoint].erase(randomIter);
207 _preComputedPaths.erase(pathIdx);
208 return;
209 }
210 }
211 }
212
213 _currentPoint = newPoint;
214 G3D::Vector3& finalPoint = finalPath[finalPath.size() - 1];
215 _currDestPosition.Relocate(finalPoint.x, finalPoint.y, finalPoint.z);
216
218 bool walk = true;
219 switch (creature->GetMovementTemplate().GetRandom())
220 {
222 walk = creature->IsWalking();
223 break;
225 walk = false;
226 break;
227 default:
228 break;
229 }
230
231 Movement::MoveSplineInit init(creature);
232 init.MovebyPath(finalPath);
233 init.SetWalk(walk);
234 init.Launch();
235
236 ++_moveCount;
237 if (roll_chance_i((int32) _moveCount * 25 + 10))
238 {
239 _moveCount = 0;
240 _nextMoveTime.Reset(urand(4000, 8000));
241 }
243 _preComputedPaths.erase(pathIdx);
244
245 //Call for creature group update
246 if (creature->GetFormation() && creature->GetFormation()->GetLeader() == creature)
247 creature->GetFormation()->LeaderMoveTo(finalPoint.x, finalPoint.y, finalPoint.z, 0);
248}
std::int32_t int32
Definition Define.h:103
std::uint16_t uint16
Definition Define.h:108
#define INVALID_HEIGHT
Definition GridTerrainData.h:27
@ LINEOFSIGHT_ALL_CHECKS
Definition Map.h:110
@ MAP_OBJECT_CELL_MOVE_NONE
Definition Object.h:405
@ 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:85
bool roll_chance_i(int chance)
Definition Random.h:63
@ UNIT_STATE_ROAMING_MOVE
Definition UnitDefines.h:192
@ CONFIG_DONT_CACHE_RANDOM_MOVEMENT_PATHS
Definition WorldConfig.h:103
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:86
CreatureGroup const * GetFormation() const
Definition Creature.h:357
bool CanEnterWater() const override
Definition Creature.cpp:3303
CreatureMovementData const & GetMovementTemplate() const
Definition Creature.cpp:2998
Definition Map.h:163
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:1539
MapObjectCellMoveState _moveState
Definition Object.h:425
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:698
bool IsWalking() const
Definition Unit.h:1640
uint32 GetPhaseMask() const
Definition Object.h:513
Map * GetMap() const
Definition Object.h:621
void UpdateAllowedPositionZ(float x, float y, float &z, float *groundZ=nullptr) const
Definition Object.cpp:1583
bool IsWithinLOS(float x, float y, float z, VMAP::ModelIgnoreFlags ignoreFlags=VMAP::ModelIgnoreFlags::Nothing, LineOfSightChecks checks=LINEOFSIGHT_ALL_CHECKS) const
Definition Object.cpp:1355
float GetMapWaterOrGroundLevel(Position pos, float *ground=nullptr) const
Definition Object.h:700
#define sWorld
Definition World.h:316
bool IsValidMapCoord(float c)
Definition GridDefines.h:210
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)
285{
287}
@ UNIT_STATE_ROAMING
Definition UnitDefines.h:174
void ClearUnitState(uint32 f)
Definition Unit.h:700

References Unit::ClearUnitState(), 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)
252{
253 if (!creature->IsAlive())
254 return;
255
256 if (!_wanderDistance)
258
259 _nextMoveTime.Reset(creature->GetSpawnId() && creature->GetWanderDistance() == _wanderDistance ? urand(1, 5000) : 0);
260 _wanderDistance = std::max((creature->GetWanderDistance() == _wanderDistance && creature->GetInstanceId() == 0) ? (creature->CanFly() ? MIN_WANDER_DISTANCE_AIR : MIN_WANDER_DISTANCE_GROUND) : 0.0f, _wanderDistance);
261
262 if (G3D::fuzzyEq(_initialPosition.GetExactDist2d(0.0f, 0.0f), 0.0f))
263 {
264 _initialPosition.Relocate(creature);
265 _destinationPoints.clear();
266 for (uint8 i = 0; i < RANDOM_POINTS_NUMBER; ++i)
267 {
268 float angle = (M_PI * 2.0f / (float)RANDOM_POINTS_NUMBER) * i;
269 float factor = 0.5f + rand_norm() * 0.5f;
270 _destinationPoints.push_back(G3D::Vector3(_initialPosition.GetPositionX() + _wanderDistance * cos(angle)*factor, _initialPosition.GetPositionY() + _wanderDistance * std::sin(angle)*factor, _initialPosition.GetPositionZ()));
271 }
272 }
273
275}
#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:69
float GetWanderDistance() const
Definition Creature.h:306
bool IsAlive() const
Definition Unit.h:1726
uint32 GetInstanceId() const
Definition Object.h:510
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)
279{
280 DoInitialize(creature);
281}

◆ DoReset() [2/2]

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

◆ DoUpdate() [1/2]

bool RandomMovementGenerator< Creature >::DoUpdate ( Creature creature,
const uint32  diff 
)
291{
293 {
294 _nextMoveTime.Reset(0); // Expire the timer
295 creature->StopMoving();
296 return true;
297 }
298
299 // xinef: if we got disable move flag, do not remove default generator - just prevent movement
300 if (creature->HasUnitFlag(UNIT_FLAG_DISABLE_MOVE))
301 {
302 _nextMoveTime.Reset(0); // Expire the timer
304 return true;
305 }
306
307 if (creature->movespline->Finalized())
308 {
309 _nextMoveTime.Update(diff);
310 if (_nextMoveTime.Passed())
311 _setRandomLocation(creature);
312 }
313 return true;
314}
@ UNIT_STATE_NOT_MOVE
Definition UnitDefines.h:222
@ UNIT_FLAG_DISABLE_MOVE
Definition UnitDefines.h:256
bool IsMovementPreventedByCasting() const override
Definition Creature.cpp:3569
bool Finalized() const
Definition MoveSpline.h:116
Movement::MoveSpline * movespline
Definition Unit.h:2049
bool HasUnitFlag(UnitFlags flags) const
Definition Unit.h:710
void StopMoving()
Definition Unit.cpp:16717
bool HasUnitState(const uint32 f) const
Definition Unit.h:699
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.

318{
321 else if (G3D::fuzzyNe(_initialPosition.GetExactDist2d(0.0f, 0.0f), 0.0f)) // if initial position is not 0.0f, 0.0f
323 else
324 return false;
325 return true;
326}
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: