AzeorthCore 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 ()
 
 ~RandomMovementGenerator ()
 
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 void Initialize (Unit *)=0
 
virtual void Finalize (Unit *)=0
 
virtual void Reset (Unit *)=0
 
virtual bool Update (Unit *, uint32 time_diff)=0
 
virtual MovementGeneratorType GetMovementGeneratorType ()=0
 
virtual uint32 GetSplineId () const
 
virtual void unitSpeedChanged ()
 
virtual void Pause (uint32)
 
virtual void Resume (uint32)
 
virtual bool GetResetPosition (float &, float &, float &)
 

Private Attributes

TimeTrackerSmall _nextMoveTime
 
uint8 _moveCount
 
float _wanderDistance
 
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
35 {
36 _initialPosition.Relocate(0.0f, 0.0f, 0.0f, 0.0f);
38
39 for (uint8 i = 0; i < RANDOM_POINTS_NUMBER; ++i)
40 {
42 for (uint8 j = 0; j < RANDOM_LINKS_COUNT; ++j)
44 }
45
47 for (uint8 i = 0; i < RANDOM_POINTS_NUMBER; ++i)
49 }
std::uint8_t uint8
Definition: Define.h:110
#define RANDOM_POINTS_NUMBER
Definition: RandomMovementGenerator.h:24
#define RANDOM_LINKS_COUNT
Definition: RandomMovementGenerator.h:25
void Relocate(float x, float y)
Definition: Position.h:73
std::vector< G3D::Vector3 > _destinationPoints
Definition: RandomMovementGenerator.h:65
PathGenerator * _pathGenerator
Definition: RandomMovementGenerator.h:64
uint8 _moveCount
Definition: RandomMovementGenerator.h:62
std::vector< uint8 > _validPointsVector[RANDOM_POINTS_NUMBER+1]
Definition: RandomMovementGenerator.h:66
Position _initialPosition
Definition: RandomMovementGenerator.h:69
uint8 _currentPoint
Definition: RandomMovementGenerator.h:67
float _wanderDistance
Definition: RandomMovementGenerator.h:63
TimeTrackerSmall _nextMoveTime
Definition: RandomMovementGenerator.h:61

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

◆ ~RandomMovementGenerator() [1/2]

30{ }

◆ ~RandomMovementGenerator() [2/2]

Member Function Documentation

◆ _setRandomLocation() [1/2]

void RandomMovementGenerator< Creature >::_setRandomLocation ( Creature creature)
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 bool result = _pathGenerator->CalculatePath(x, y, levelZ, false);
138 if (result && !(_pathGenerator->GetPathType() & PATHFIND_NOPATH))
139 {
140 // generated path is too long
141 float pathLen = _pathGenerator->getPathLength();
142 if (pathLen * pathLen > creature->GetExactDistSq(x, y, levelZ) * MAX_PATH_LENGHT_FACTOR * MAX_PATH_LENGHT_FACTOR)
143 {
144 _validPointsVector[_currentPoint].erase(randomIter);
145 _preComputedPaths.erase(pathIdx);
146 return;
147 }
148
149 finalPath = _pathGenerator->GetPath();
150 Movement::PointsArray::iterator itr = finalPath.begin();
151 Movement::PointsArray::iterator itrNext = finalPath.begin() + 1;
152 float zDiff, distDiff;
153
154 for (; itrNext != finalPath.end(); ++itr, ++itrNext)
155 {
156 distDiff = std::sqrt(((*itr).x - (*itrNext).x) * ((*itr).x - (*itrNext).x) + ((*itr).y - (*itrNext).y) * ((*itr).y - (*itrNext).y));
157 zDiff = std::fabs((*itr).z - (*itrNext).z);
158
159 // Xinef: tree climbing, cut as much as we can
160 if (zDiff > 2.0f ||
161 (G3D::fuzzyNe(zDiff, 0.0f) && distDiff / zDiff < 2.15f)) // ~25˚
162 {
163 _validPointsVector[_currentPoint].erase(randomIter);
164 _preComputedPaths.erase(pathIdx);
165 return;
166 }
167
168 if (!map->isInLineOfSight((*itr).x, (*itr).y, (*itr).z + 2.f, (*itrNext).x, (*itrNext).y, (*itrNext).z + 2.f, creature->GetPhaseMask(),
170 {
171 _validPointsVector[_currentPoint].erase(randomIter);
172 _preComputedPaths.erase(pathIdx);
173 return;
174 }
175 }
176
177 // no valid path
178 if (finalPath.size() < 2)
179 {
180 _validPointsVector[_currentPoint].erase(randomIter);
181 _preComputedPaths.erase(pathIdx);
182 return;
183 }
184 }
185 else
186 {
187 _validPointsVector[_currentPoint].erase(randomIter);
188 _preComputedPaths.erase(pathIdx);
189 return;
190 }
191 }
192 }
193
194 _currentPoint = newPoint;
195 G3D::Vector3& finalPoint = finalPath[finalPath.size() - 1];
196 _currDestPosition.Relocate(finalPoint.x, finalPoint.y, finalPoint.z);
197
199 bool walk = true;
200 switch (creature->GetMovementTemplate().GetRandom())
201 {
202 case CreatureRandomMovementType::CanRun:
203 walk = creature->IsWalking();
204 break;
205 case CreatureRandomMovementType::AlwaysRun:
206 walk = false;
207 break;
208 default:
209 break;
210 }
211
212 Movement::MoveSplineInit init(creature);
213 init.MovebyPath(finalPath);
214 init.SetWalk(walk);
215 init.Launch();
216
217 ++_moveCount;
218 if (roll_chance_i((int32) _moveCount * 25 + 10))
219 {
220 _moveCount = 0;
221 _nextMoveTime.Reset(urand(4000, 8000));
222 }
224 _preComputedPaths.erase(pathIdx);
225
226 //Call for creature group update
227 if (creature->GetFormation() && creature->GetFormation()->GetLeader() == creature)
228 creature->GetFormation()->LeaderMoveTo(finalPoint.x, finalPoint.y, finalPoint.z, false);
229}
std::int32_t int32
Definition: Define.h:104
std::uint16_t uint16
Definition: Define.h:109
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:59
@ MAP_OBJECT_CELL_MOVE_NONE
Definition: Object.h:370
@ UNIT_STATE_ROAMING_MOVE
Definition: Unit.h:347
@ LINEOFSIGHT_ALL_CHECKS
Definition: Map.h:197
#define INVALID_HEIGHT
Definition: Map.h:164
@ PATHFIND_NOPATH
Definition: PathGenerator.h:51
#define MAX_PATH_LENGHT_FACTOR
Definition: RandomMovementGenerator.h:28
@ CONFIG_DONT_CACHE_RANDOM_MOVEMENT_PATHS
Definition: IWorld.h:146
#define sWorld
Definition: World.h:458
bool IsValidMapCoord(float c)
Definition: GridDefines.h:216
std::vector< Vector3 > PointsArray
Definition: MoveSplineInitArgs.h:28
void Reset(int32 interval)
Definition: Timer.h:249
bool CanFly() const override
Definition: Creature.h:82
CreatureGroup const * GetFormation() const
Definition: Creature.h:337
bool CanEnterWater() const override
Definition: Creature.cpp:3130
CreatureMovementData const & GetMovementTemplate() const
Definition: Creature.cpp:2856
CreatureRandomMovementType GetRandom() const
Definition: CreatureData.h:161
Creature * GetLeader() const
Definition: CreatureGroups.h:97
void LeaderMoveTo(float x, float y, float z, bool run)
Definition: CreatureGroups.cpp:312
MapObjectCellMoveState _moveState
Definition: Object.h:389
uint32 GetPhaseMask() const
Definition: Object.h:434
Map * GetMap() const
Definition: Object.h:517
void UpdateAllowedPositionZ(float x, float y, float &z, float *groundZ=nullptr) const
Definition: Object.cpp:1554
float GetMapWaterOrGroundLevel(float x, float y, float z, float *ground=nullptr) const
Definition: Object.cpp:3043
bool IsWithinLOS(float x, float y, float z, VMAP::ModelIgnoreFlags ignoreFlags=VMAP::ModelIgnoreFlags::Nothing, LineOfSightChecks checks=LINEOFSIGHT_ALL_CHECKS) const
Definition: Object.cpp:1326
float GetPositionZ() const
Definition: Position.h:119
float GetPositionX() const
Definition: Position.h:117
float GetPositionY() const
Definition: Position.h:118
float GetExactDistSq(float x, float y, float z) const
Definition: Position.h:170
void AddUnitState(uint32 f)
Definition: Unit.h:1397
bool IsWalking() const
Definition: Unit.h:2377
Definition: Map.h:312
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:2441
Movement::PointsArray const & GetPath() const
Definition: PathGenerator.h:91
float getPathLength() const
Definition: PathGenerator.h:98
PathType GetPathType() const
Definition: PathGenerator.h:93
bool CalculatePath(float destX, float destY, float destZ, bool forceDest=false)
Definition: PathGenerator.cpp:52
std::map< uint16, Movement::PointsArray > _preComputedPaths
Definition: RandomMovementGenerator.h:68
Position _currDestPosition
Definition: RandomMovementGenerator.h:69
Definition: MoveSplineInit.h:71

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)
268{
270 creature->SetWalk(false);
271}
@ UNIT_STATE_ROAMING
Definition: Unit.h:329
bool SetWalk(bool enable) override
Definition: Creature.cpp:3053
void ClearUnitState(uint32 f)
Definition: Unit.h:1399

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)
233{
234 if (!creature->IsAlive())
235 return;
236
237 if (!_wanderDistance)
239
240 _nextMoveTime.Reset(creature->GetSpawnId() && creature->GetWanderDistance() == _wanderDistance ? urand(1, 5000) : 0);
241 _wanderDistance = std::max((creature->GetWanderDistance() == _wanderDistance && creature->GetInstanceId() == 0) ? (creature->CanFly() ? MIN_WANDER_DISTANCE_AIR : MIN_WANDER_DISTANCE_GROUND) : 0.0f, _wanderDistance);
242
243 if (G3D::fuzzyEq(_initialPosition.GetExactDist2d(0.0f, 0.0f), 0.0f))
244 {
245 _initialPosition.Relocate(creature);
246 _destinationPoints.clear();
247 for (uint8 i = 0; i < RANDOM_POINTS_NUMBER; ++i)
248 {
249 float angle = (M_PI * 2.0f / (float)RANDOM_POINTS_NUMBER) * i;
250 float factor = 0.5f + rand_norm() * 0.5f;
251 _destinationPoints.push_back(G3D::Vector3(_initialPosition.GetPositionX() + _wanderDistance * cos(angle)*factor, _initialPosition.GetPositionY() + _wanderDistance * std::sin(angle)*factor, _initialPosition.GetPositionZ()));
252 }
253 }
254
255 if (!_pathGenerator)
256 _pathGenerator = new PathGenerator(creature);
258}
#define MIN_WANDER_DISTANCE_AIR
Definition: RandomMovementGenerator.h:27
#define MIN_WANDER_DISTANCE_GROUND
Definition: RandomMovementGenerator.h:26
ObjectGuid::LowType GetSpawnId() const
Definition: Creature.h:66
float GetWanderDistance() const
Definition: Creature.h:286
uint32 GetInstanceId() const
Definition: Object.h:431
float GetExactDist2d(const float x, const float y) const
Definition: Position.h:166
bool IsAlive() const
Definition: Unit.h:1808
Definition: PathGenerator.h:60

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)
262{
263 DoInitialize(creature);
264}

◆ DoReset() [2/2]

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

◆ DoUpdate() [1/2]

bool RandomMovementGenerator< Creature >::DoUpdate ( Creature creature,
const uint32  diff 
)
275{
277 {
278 _nextMoveTime.Reset(0); // Expire the timer
279 creature->StopMoving();
280 return true;
281 }
282
283 // xinef: if we got disable move flag, do not remove default generator - just prevent movement
284 if (creature->HasUnitFlag(UNIT_FLAG_DISABLE_MOVE))
285 {
286 _nextMoveTime.Reset(0); // Expire the timer
288 return true;
289 }
290
291 if (creature->movespline->Finalized())
292 {
293 _nextMoveTime.Update(diff);
294 if (_nextMoveTime.Passed())
295 _setRandomLocation(creature);
296 }
297 return true;
298}
@ UNIT_STATE_NOT_MOVE
Definition: Unit.h:371
@ UNIT_FLAG_DISABLE_MOVE
Definition: Unit.h:450
void Update(int32 diff)
Definition: Timer.h:239
bool Passed() const
Definition: Timer.h:244
bool IsMovementPreventedByCasting() const override
Definition: Creature.cpp:3484
Movement::MoveSpline * movespline
Definition: Unit.h:2438
bool HasUnitFlag(UnitFlags flags) const
Definition: Unit.h:1481
void StopMoving()
Definition: Unit.cpp:16754
bool HasUnitState(const uint32 f) const
Definition: Unit.h:1398
bool Finalized() const
Definition: MoveSpline.h:116

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.

58{ return RANDOM_MOTION_TYPE; }
@ RANDOM_MOTION_TYPE
Definition: MotionMaster.h:38

References RANDOM_MOTION_TYPE.

◆ GetResetPosition() [1/2]

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

Reimplemented from MovementGenerator.

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

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 >
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