Core/Movement: Minor refactors in WaypointMovementGenerator

* Removed unneccessary _loadedFromDB member (handled by variant index already)
* Apply some const
* Replace stringstream formatting in GetDebugInfo with Trinity::StringFormat
* Moved MovementGeneratorCreator::Create implementations to source files whose movement they create
This commit is contained in:
Shauren
2024-04-17 15:58:47 +02:00
parent e5beb25d25
commit b5dceafad7
7 changed files with 38 additions and 67 deletions

View File

@@ -16,14 +16,7 @@
*/
#include "MovementGenerator.h"
#include "Creature.h"
#include "IdleMovementGenerator.h"
#include "MovementDefines.h"
#include "PathGenerator.h"
#include "RandomMovementGenerator.h"
#include "UnitAI.h"
#include "WaypointMovementGenerator.h"
#include <sstream>
#include "StringFormat.h"
MovementGenerator::~MovementGenerator()
{
@@ -33,13 +26,8 @@ MovementGenerator::~MovementGenerator()
std::string MovementGenerator::GetDebugInfo() const
{
std::stringstream sstr;
sstr << std::boolalpha
<< "Mode: " << std::to_string(Mode)
<< " Priority: " << std::to_string(Priority)
<< " Flags: " << Flags
<< " BaseUniteState: " << BaseUnitState;
return sstr.str();
return Trinity::StringFormat("Mode: {} Priority: {} Flags: {} BaseUniteState: {}",
Mode, Priority, Flags, BaseUnitState);
}
void MovementGenerator::SetScriptResult(MovementStopReason reason)
@@ -50,25 +38,3 @@ void MovementGenerator::SetScriptResult(MovementStopReason reason)
ScriptResult.reset();
}
}
IdleMovementFactory::IdleMovementFactory() : MovementGeneratorCreator(IDLE_MOTION_TYPE) { }
MovementGenerator* IdleMovementFactory::Create(Unit* /*object*/) const
{
static IdleMovementGenerator instance;
return &instance;
}
RandomMovementFactory::RandomMovementFactory() : MovementGeneratorCreator(RANDOM_MOTION_TYPE) { }
MovementGenerator* RandomMovementFactory::Create(Unit* /*object*/) const
{
return new RandomMovementGenerator<Creature>();
}
WaypointMovementFactory::WaypointMovementFactory() : MovementGeneratorCreator(WAYPOINT_MOTION_TYPE) { }
MovementGenerator* WaypointMovementFactory::Create(Unit* /*object*/) const
{
return new WaypointMovementGenerator<Creature>(0, true);
}

View File

@@ -125,34 +125,23 @@ class MovementGeneratorMedium : public MovementGenerator
typedef FactoryHolder<MovementGenerator, Unit, MovementGeneratorType> MovementGeneratorCreator;
template<class Movement>
struct MovementGeneratorFactory : public MovementGeneratorCreator
{
MovementGeneratorFactory(MovementGeneratorType movementGeneratorType) : MovementGeneratorCreator(movementGeneratorType) { }
MovementGenerator* Create(Unit* /*object*/) const override
{
return new Movement();
}
};
struct IdleMovementFactory : public MovementGeneratorCreator
{
IdleMovementFactory();
IdleMovementFactory() : MovementGeneratorCreator(IDLE_MOTION_TYPE) { }
MovementGenerator* Create(Unit* object) const override;
};
struct RandomMovementFactory : public MovementGeneratorCreator
{
RandomMovementFactory();
RandomMovementFactory() : MovementGeneratorCreator(RANDOM_MOTION_TYPE) { }
MovementGenerator* Create(Unit* object) const override;
};
struct WaypointMovementFactory : public MovementGeneratorCreator
{
WaypointMovementFactory();
WaypointMovementFactory() : MovementGeneratorCreator(WAYPOINT_MOTION_TYPE) { }
MovementGenerator* Create(Unit* object) const override;
};

View File

@@ -60,6 +60,12 @@ MovementGeneratorType IdleMovementGenerator::GetMovementGeneratorType() const
return IDLE_MOTION_TYPE;
}
MovementGenerator* IdleMovementFactory::Create(Unit* /*object*/) const
{
static IdleMovementGenerator instance;
return &instance;
}
//----------------------------------------------------//
RotateMovementGenerator::RotateMovementGenerator(uint32 id, RotateDirection direction, Optional<Milliseconds> duration,

View File

@@ -25,18 +25,21 @@ template<class Entity, class BasePath>
class PathMovementBase
{
public:
using PathType = BasePath;
PathMovementBase() : _path(), _currentNode(0) { }
virtual ~PathMovementBase() { };
explicit PathMovementBase(PathType&& path) : _path(std::move(path)), _currentNode(0) { }
virtual ~PathMovementBase() = default;
uint32 GetCurrentNode() const { return _currentNode; }
virtual std::string GetDebugInfo() const
{
return "Current Node: " + std::to_string(GetCurrentNode());
};
}
protected:
BasePath _path;
PathType _path;
uint32 _currentNode;
};

View File

@@ -275,3 +275,8 @@ void RandomMovementGenerator<Creature>::DoFinalize(Creature* owner, bool active,
owner->AI()->MovementInform(RANDOM_MOTION_TYPE, 0);
}
}
MovementGenerator* RandomMovementFactory::Create(Unit* /*object*/) const
{
return new RandomMovementGenerator<Creature>();
}

View File

@@ -26,13 +26,12 @@
#include "MovementDefines.h"
#include "Transport.h"
#include "WaypointManager.h"
#include <sstream>
WaypointMovementGenerator<Creature>::WaypointMovementGenerator(uint32 pathId, bool repeating, Optional<Milliseconds> duration, Optional<float> speed,
MovementWalkRunSpeedSelectionMode speedSelectionMode, Optional<std::pair<Milliseconds, Milliseconds>> waitTimeRangeAtPathEnd,
Optional<float> wanderDistanceAtPathEnds, Optional<bool> followPathBackwardsFromEndToStart, bool generatePath,
Optional<Scripting::v2::ActionResultSetter<MovementStopReason>>&& scriptResult /*= {}*/)
: _nextMoveTime(0), _pathId(pathId), _repeating(repeating), _loadedFromDB(true),
: PathMovementBase(PathType(std::in_place_type<WaypointPath const*>)), _nextMoveTime(0), _pathId(pathId), _repeating(repeating),
_speed(speed), _speedSelectionMode(speedSelectionMode), _waitTimeRangeAtPathEnd(std::move(waitTimeRangeAtPathEnd)),
_wanderDistanceAtPathEnds(wanderDistanceAtPathEnds), _followPathBackwardsFromEndToStart(followPathBackwardsFromEndToStart), _isReturningToStart(false),
_generatePath(generatePath)
@@ -50,13 +49,11 @@ WaypointMovementGenerator<Creature>::WaypointMovementGenerator(WaypointPath cons
MovementWalkRunSpeedSelectionMode speedSelectionMode, Optional<std::pair<Milliseconds, Milliseconds>> waitTimeRangeAtPathEnd,
Optional<float> wanderDistanceAtPathEnds, Optional<bool> followPathBackwardsFromEndToStart, bool generatePath,
Optional<Scripting::v2::ActionResultSetter<MovementStopReason>>&& scriptResult /*= {}*/)
: _nextMoveTime(0), _pathId(0), _repeating(repeating), _loadedFromDB(false),
: PathMovementBase(std::make_unique<WaypointPath>(path)), _nextMoveTime(0), _pathId(0), _repeating(repeating),
_speed(speed), _speedSelectionMode(speedSelectionMode), _waitTimeRangeAtPathEnd(std::move(waitTimeRangeAtPathEnd)),
_wanderDistanceAtPathEnds(wanderDistanceAtPathEnds), _followPathBackwardsFromEndToStart(followPathBackwardsFromEndToStart), _isReturningToStart(false),
_generatePath(generatePath)
{
_path = std::make_unique<WaypointPath>(path);
Mode = MOTION_MODE_DEFAULT;
Priority = MOTION_PRIORITY_NORMAL;
Flags = MOVEMENTGENERATOR_FLAG_INITIALIZATION_PENDING;
@@ -123,7 +120,7 @@ void WaypointMovementGenerator<Creature>::DoInitialize(Creature* owner)
{
RemoveFlag(MOVEMENTGENERATOR_FLAG_INITIALIZATION_PENDING | MOVEMENTGENERATOR_FLAG_TRANSITORY | MOVEMENTGENERATOR_FLAG_DEACTIVATED);
if (_loadedFromDB)
if (IsLoadedFromDB())
{
if (!_pathId)
_pathId = owner->GetWaypointPathId();
@@ -270,7 +267,7 @@ void WaypointMovementGenerator<Creature>::DoFinalize(Creature* owner, bool activ
SetScriptResult(MovementStopReason::Finished);
}
void WaypointMovementGenerator<Creature>::MovementInform(Creature* owner)
void WaypointMovementGenerator<Creature>::MovementInform(Creature const* owner) const
{
WaypointNode const& waypoint = GetPath()->Nodes[_currentNode];
if (CreatureAI* AI = owner->AI())
@@ -481,8 +478,12 @@ bool WaypointMovementGenerator<Creature>::IsFollowingPathBackwardsFromEndToStart
std::string WaypointMovementGenerator<Creature>::GetDebugInfo() const
{
std::stringstream sstr;
sstr << PathMovementBase::GetDebugInfo() << "\n"
<< MovementGeneratorMedium::GetDebugInfo();
return sstr.str();
return Trinity::StringFormat("{}\n{}",
PathMovementBase::GetDebugInfo(),
MovementGeneratorMedium::GetDebugInfo());
}
MovementGenerator* WaypointMovementFactory::Create(Unit* /*object*/) const
{
return new WaypointMovementGenerator<Creature>(0, true);
}

View File

@@ -65,7 +65,7 @@ class WaypointMovementGenerator<Creature> : public MovementGeneratorMedium<Creat
std::string GetDebugInfo() const override;
private:
void MovementInform(Creature*);
void MovementInform(Creature const*) const;
void OnArrived(Creature*);
void StartMove(Creature*, bool relaunch = false);
bool ComputeNextNode();
@@ -82,10 +82,11 @@ class WaypointMovementGenerator<Creature> : public MovementGeneratorMedium<Creat
bool IsFollowingPathBackwardsFromEndToStart() const;
bool IsLoadedFromDB() const { return std::holds_alternative<WaypointPath const*>(_path); }
TimeTracker _nextMoveTime;
uint32 _pathId;
bool _repeating;
bool _loadedFromDB;
Optional<TimeTracker> _duration;
Optional<float> _speed;