Core/Movement: WaypointMovementGenerator cleanup

Port From (https://github.com/TrinityCore/TrinityCore/commit/6d79b329acd7ad1d81f1f508726f5fad78b70b0e)
This commit is contained in:
hondacrx
2021-06-23 18:02:44 -04:00
parent ca1afb5c2e
commit dcf8c12e5c
@@ -39,8 +39,10 @@ namespace Game.Movement
_path = path; _path = path;
} }
void LoadPath(Creature creature) public override void DoInitialize(Creature creature)
{ {
_done = false;
if (_loadedFromDB) if (_loadedFromDB)
{ {
if (_pathId == 0) if (_pathId == 0)
@@ -52,7 +54,7 @@ namespace Game.Movement
if (_path == null) if (_path == null)
{ {
// No path id found for entry // No path id found for entry
Log.outError(LogFilter.Sql, $"WaypointMovementGenerator.LoadPath: creature {creature.GetName()} ({creature.GetGUID()} DB GUID: {creature.GetSpawnId()}) doesn't have waypoint path id: {_pathId}"); Log.outError(LogFilter.Sql, $"WaypointMovementGenerator.DoInitialize: creature {creature.GetName()} ({creature.GetGUID()} DB GUID: {creature.GetSpawnId()}) doesn't have waypoint path id: {_pathId}");
return; return;
} }
@@ -63,12 +65,6 @@ namespace Game.Movement
creature.GetAI().WaypointPathStarted(_path.id); creature.GetAI().WaypointPathStarted(_path.id);
} }
public override void DoInitialize(Creature creature)
{
_done = false;
LoadPath(creature);
}
public override void DoFinalize(Creature creature) public override void DoFinalize(Creature creature)
{ {
creature.ClearUnitState(UnitState.Roaming | UnitState.RoamingMove); creature.ClearUnitState(UnitState.Roaming | UnitState.RoamingMove);
@@ -77,8 +73,8 @@ namespace Game.Movement
public override void DoReset(Creature creature) public override void DoReset(Creature creature)
{ {
if (!_done && CanMove(creature)) if (!_done && _nextMoveTime.Passed() && CanMove(creature))
StartMoveNow(creature); StartMove(creature);
else if (_done) else if (_done)
{ {
// mimic IdleMovementGenerator // mimic IdleMovementGenerator
@@ -117,19 +113,19 @@ namespace Game.Movement
creature.UpdateCurrentWaypointInfo(waypoint.id, _path.id); creature.UpdateCurrentWaypointInfo(waypoint.id, _path.id);
} }
bool StartMove(Creature creature) void StartMove(Creature creature)
{ {
if (!creature || !creature.IsAlive()) if (!creature || !creature.IsAlive())
return true; return;
if (_done || _path == null || _path.nodes.Empty()) if (_done || _path == null || _path.nodes.Empty())
return true; return;
// if the owner is the leader of its formation, check members status // if the owner is the leader of its formation, check members status
if (creature.IsFormationLeader() && !creature.IsFormationLeaderMoveAllowed()) if (!CanMove(creature) || (creature.IsFormationLeader() && !creature.IsFormationLeaderMoveAllowed()))
{ {
_nextMoveTime.Reset(1000); _nextMoveTime.Reset(1000);
return true; return;
} }
bool transportPath = creature.GetTransport() != null; bool transportPath = creature.GetTransport() != null;
@@ -167,7 +163,7 @@ namespace Game.Movement
// inform AI // inform AI
if (creature.IsAIEnabled) if (creature.IsAIEnabled)
creature.GetAI().WaypointPathEnded(lastWaypoint.id, _path.id); creature.GetAI().WaypointPathEnded(lastWaypoint.id, _path.id);
return true; return;
} }
_currentNode = (_currentNode + 1) % _path.nodes.Count; _currentNode = (_currentNode + 1) % _path.nodes.Count;
@@ -178,7 +174,7 @@ namespace Game.Movement
} }
Cypher.Assert(_currentNode < _path.nodes.Count, $"WaypointMovementGenerator.StartMove: tried to reference a node id ({_currentNode}) which is not included in path ({_path.id})"); Cypher.Assert(_currentNode < _path.nodes.Count, $"WaypointMovementGenerator.StartMove: tried to reference a node id ({_currentNode}) which is not included in path ({_path.id})");
WaypointNode waypoint = _path.nodes.ElementAt(_currentNode); WaypointNode waypoint = _path.nodes[_currentNode];
Position formationDest = new(waypoint.x, waypoint.y, waypoint.z, (waypoint.orientation != 0 && waypoint.delay != 0) ? waypoint.orientation : 0.0f); Position formationDest = new(waypoint.x, waypoint.y, waypoint.z, (waypoint.orientation != 0 && waypoint.delay != 0) ? waypoint.orientation : 0.0f);
_isArrivalDone = false; _isArrivalDone = false;
@@ -229,7 +225,7 @@ namespace Game.Movement
// inform formation // inform formation
creature.SignalFormationMovement(formationDest, waypoint.id, waypoint.moveType, (waypoint.orientation != 0 && waypoint.delay != 0)); creature.SignalFormationMovement(formationDest, waypoint.id, waypoint.moveType, (waypoint.orientation != 0 && waypoint.delay != 0));
return true; return;
} }
public override bool DoUpdate(Creature creature, uint diff) public override bool DoUpdate(Creature creature, uint diff)
@@ -238,47 +234,31 @@ namespace Game.Movement
return true; return true;
if (_done || _path == null || _path.nodes.Empty()) if (_done || _path == null || _path.nodes.Empty())
return true; return false;
if (_stalled || creature.HasUnitState(UnitState.NotMove) || creature.IsMovementPreventedByCasting()) if (_stalled || creature.HasUnitState(UnitState.NotMove) || creature.IsMovementPreventedByCasting())
{ {
creature.ClearUnitState(UnitState.RoamingMove);
creature.StopMoving(); creature.StopMoving();
return true; return true;
} }
if (!_nextMoveTime.Passed()) if (!_nextMoveTime.Passed())
{ _nextMoveTime.Update((int)diff);
if (creature.MoveSpline.Finalized())
{
_nextMoveTime.Update((int)diff);
if (_nextMoveTime.Passed())
return StartMoveNow(creature);
}
}
else
{
if (creature.MoveSpline.Finalized())
{
OnArrived(creature);
_isArrivalDone = true;
if (_nextMoveTime.Passed()) if (creature.HasUnitState(UnitState.RoamingMove) && creature.MoveSpline.Finalized() && !_isArrivalDone)
return StartMove(creature); {
} OnArrived(creature);
else _isArrivalDone = true;
{
// Set home position at place on waypoint movement.
if (creature.GetTransGUID().IsEmpty())
creature.SetHomePosition(creature.GetPosition());
if (_recalculateSpeed)
{
if (_nextMoveTime.Passed())
StartMove(creature);
}
}
} }
// Set home position at place on waypoint movement.
if (creature.GetTransGUID().IsEmpty())
creature.SetHomePosition(creature.GetPosition());
if (_recalculateSpeed || (_nextMoveTime.Passed() && (!creature.HasUnitState(UnitState.RoamingMove) || creature.MoveSpline.Finalized())))
StartMove(creature);
return true; return true;
} }
@@ -318,21 +298,15 @@ namespace Game.Movement
_nextMoveTime.Reset((int)overrideTimer); _nextMoveTime.Reset((int)overrideTimer);
} }
bool CanMove(Creature creature) static bool CanMove(Creature creature)
{ {
return _nextMoveTime.Passed() && !creature.HasUnitState(UnitState.NotMove) && !creature.IsMovementPreventedByCasting(); return !creature.HasUnitState(UnitState.NotMove) && !creature.IsMovementPreventedByCasting();
} }
public override MovementGeneratorType GetMovementGeneratorType() { return MovementGeneratorType.Waypoint; } public override MovementGeneratorType GetMovementGeneratorType() { return MovementGeneratorType.Waypoint; }
public override void UnitSpeedChanged() { _recalculateSpeed = true; } public override void UnitSpeedChanged() { _recalculateSpeed = true; }
bool StartMoveNow(Creature creature)
{
_nextMoveTime.Reset(0);
return StartMove(creature);
}
TimeTrackerSmall _nextMoveTime; TimeTrackerSmall _nextMoveTime;
bool _recalculateSpeed; bool _recalculateSpeed;
bool _isArrivalDone; bool _isArrivalDone;