Core/Movement: Allow specifying timeout for random movement

This commit is contained in:
Shauren
2023-03-01 21:58:07 +01:00
parent 0748986835
commit 66b29fba44
4 changed files with 28 additions and 7 deletions

View File

@@ -596,12 +596,12 @@ void MotionMaster::MoveTargetedHome()
}
}
void MotionMaster::MoveRandom(float wanderDistance)
void MotionMaster::MoveRandom(float wanderDistance, Optional<Milliseconds> duration)
{
if (_owner->GetTypeId() == TYPEID_UNIT)
{
TC_LOG_DEBUG("movement.motionmaster", "MotionMaster::MoveRandom: '{}', started random movement (spawnDist: {})", _owner->GetGUID().ToString(), wanderDistance);
Add(new RandomMovementGenerator<Creature>(wanderDistance), MOTION_SLOT_DEFAULT);
Add(new RandomMovementGenerator<Creature>(wanderDistance, duration), MOTION_SLOT_DEFAULT);
}
}

View File

@@ -19,6 +19,7 @@
#define MOTIONMASTER_H
#include "Common.h"
#include "Duration.h"
#include "ObjectGuid.h"
#include "Optional.h"
#include "MovementDefines.h"
@@ -154,7 +155,7 @@ class TC_GAME_API MotionMaster
void MoveIdle();
void MoveTargetedHome();
void MoveRandom(float wanderDistance = 0.0f);
void MoveRandom(float wanderDistance = 0.0f, Optional<Milliseconds> duration = {});
void MoveFollow(Unit* target, float dist, ChaseAngle angle, MovementSlot slot = MOTION_SLOT_ACTIVE);
void MoveChase(Unit* target, Optional<ChaseRange> dist = {}, Optional<ChaseAngle> angle = {});
void MoveChase(Unit* target, float dist, float angle) { MoveChase(target, ChaseRange(dist), ChaseAngle(angle)); }

View File

@@ -17,6 +17,7 @@
#include "RandomMovementGenerator.h"
#include "Creature.h"
#include "CreatureAI.h"
#include "MovementDefines.h"
#include "MoveSpline.h"
#include "MoveSplineInit.h"
@@ -24,15 +25,17 @@
#include "Random.h"
template<class T>
RandomMovementGenerator<T>::RandomMovementGenerator(float distance) : _timer(0), _reference(), _wanderDistance(distance), _wanderSteps(0)
RandomMovementGenerator<T>::RandomMovementGenerator(float distance, Optional<Milliseconds> duration) : _timer(0), _reference(), _wanderDistance(distance), _wanderSteps(0)
{
this->Mode = MOTION_MODE_DEFAULT;
this->Priority = MOTION_PRIORITY_NORMAL;
this->Flags = MOVEMENTGENERATOR_FLAG_INITIALIZATION_PENDING;
this->BaseUnitState = UNIT_STATE_ROAMING;
if (duration)
_duration.emplace(*duration);
}
template RandomMovementGenerator<Creature>::RandomMovementGenerator(float/* distance*/);
template RandomMovementGenerator<Creature>::RandomMovementGenerator(float distance, Optional<Milliseconds> duration);
template<class T>
MovementGeneratorType RandomMovementGenerator<T>::GetMovementGeneratorType() const
@@ -200,6 +203,17 @@ bool RandomMovementGenerator<Creature>::DoUpdate(Creature* owner, uint32 diff)
if (HasFlag(MOVEMENTGENERATOR_FLAG_FINALIZED | MOVEMENTGENERATOR_FLAG_PAUSED))
return true;
if (_duration)
{
_duration->Update(diff);
if (_duration->Passed())
{
RemoveFlag(MOVEMENTGENERATOR_FLAG_TRANSITORY);
AddFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED);
return false;
}
}
if (owner->HasUnitState(UNIT_STATE_NOT_MOVE) || owner->IsMovementPreventedByCasting())
{
AddFlag(MOVEMENTGENERATOR_FLAG_INTERRUPTED);
@@ -231,7 +245,7 @@ template<class T>
void RandomMovementGenerator<T>::DoFinalize(T*, bool, bool) { }
template<>
void RandomMovementGenerator<Creature>::DoFinalize(Creature* owner, bool active, bool/* movementInform*/)
void RandomMovementGenerator<Creature>::DoFinalize(Creature* owner, bool active, bool movementInform)
{
AddFlag(MOVEMENTGENERATOR_FLAG_FINALIZED);
if (active)
@@ -242,4 +256,8 @@ void RandomMovementGenerator<Creature>::DoFinalize(Creature* owner, bool active,
// TODO: Research if this modification is needed, which most likely isnt
owner->SetWalk(false);
}
if (movementInform && HasFlag(MOVEMENTGENERATOR_FLAG_INFORM_ENABLED))
if (owner->IsAIEnabled())
owner->AI()->MovementInform(RANDOM_MOTION_TYPE, 0);
}

View File

@@ -19,6 +19,7 @@
#define TRINITY_RANDOMMOTIONGENERATOR_H
#include "MovementGenerator.h"
#include "Optional.h"
#include "Position.h"
#include "Timer.h"
@@ -28,7 +29,7 @@ template<class T>
class RandomMovementGenerator : public MovementGeneratorMedium<T, RandomMovementGenerator<T>>
{
public:
explicit RandomMovementGenerator(float distance = 0.0f);
explicit RandomMovementGenerator(float distance = 0.0f, Optional<Milliseconds> duration = {});
MovementGeneratorType GetMovementGeneratorType() const override;
@@ -48,6 +49,7 @@ class RandomMovementGenerator : public MovementGeneratorMedium<T, RandomMovement
std::unique_ptr<PathGenerator> _path;
TimeTracker _timer;
Optional<TimeTracker> _duration;
Position _reference;
float _wanderDistance;
uint8 _wanderSteps;