Introduce MoveCooldownHelper to prevent lag spikes from failed pathfinding

Several activities that queue child Move activities can get into a bad scenario where the actor is pathfinding and then gets stuck because the destination is unreachable. When the Move activity then completes, then parent activity sees it has yet to reach the destination and tries to move again. However, the actor is still blocked in the same spot as before and thus the movment finishes immediately. This causes a performance death spiral where the actor attempts to pathfind every tick. The pathfinding attempt can also be very expensive if it must exhaustively check the whole map to determine no route is possible.

In order to prevent blocked actors from running into this scenario, we introduce MoveCooldownHelper. In its default setup it allows the parent activity to bail out if the actor was blocked during a pathfinding attempt. This means the activity will be dropped rather than trying to move endlessly. It also has an option to allow retrying if pathfinding was blocked, but applies a cooldown to avoid the performance penalty. For activities such as Enter, this means the actors will still try and enter their target if it is unreachable, but will only attempt once a second now rather than every tick.

MoveAdjacentTo will now cancel if it fails to reach the destination. This fixes MoveOntoAndTurn to skip the Turn if the move didn't reach the intended destination. Any other derived classes will similarly benefit from skipping follow-up actions.
This commit is contained in:
RoosterDragon
2024-04-07 15:02:53 +01:00
committed by Gustas
parent fe35c42ead
commit 2ed0656d1b
16 changed files with 239 additions and 47 deletions

View File

@@ -38,6 +38,7 @@ namespace OpenRA.Mods.Common.Activities
readonly bool wasRepaired;
readonly PlayerResources playerResources;
readonly int unitCost;
readonly MoveCooldownHelper moveCooldownHelper;
int remainingTicks;
bool played;
@@ -62,6 +63,7 @@ namespace OpenRA.Mods.Common.Activities
aircraft = move as Aircraft;
moveInfo = self.Info.TraitInfo<IMoveInfo>();
playerResources = self.Owner.PlayerActor.Trait<PlayerResources>();
moveCooldownHelper = new MoveCooldownHelper(self.World, move as Mobile) { RetryIfDestinationBlocked = true };
var valued = self.Info.TraitInfoOrDefault<ValuedInfo>();
unitCost = valued != null ? valued.Cost : 0;
@@ -129,12 +131,18 @@ namespace OpenRA.Mods.Common.Activities
return true;
}
else if (activeResupplyTypes != 0 && aircraft == null && !isCloseEnough)
var result = moveCooldownHelper.Tick(false);
if (result != null)
return result.Value;
if (activeResupplyTypes != 0 && aircraft == null && !isCloseEnough)
{
var targetCell = self.World.Map.CellContaining(host.Actor.CenterPosition);
// HACK: Repairable needs the actor to move to host center.
// TODO: Get rid of this or at least replace it with something less hacky.
moveCooldownHelper.NotifyMoveQueued();
if (repairableNear == null)
QueueChild(move.MoveOntoTarget(self, host, WVec.Zero, null, moveInfo.GetTargetLineColor()));
else
@@ -217,8 +225,11 @@ namespace OpenRA.Mods.Common.Activities
if (wasRepaired || isHostInvalid || (!stayOnResupplier && aircraft.Info.TakeOffOnResupply))
{
if (self.CurrentActivity.NextActivity == null && rp != null && rp.Path.Count > 0)
{
moveCooldownHelper.NotifyMoveQueued();
foreach (var cell in rp.Path)
QueueChild(new AttackMoveActivity(self, () => move.MoveTo(cell, 1, ignoreActor: repairableNear != null ? null : host.Actor, targetLineColor: aircraft.Info.TargetLineColor)));
}
else
QueueChild(new TakeOff(self));
@@ -235,6 +246,7 @@ namespace OpenRA.Mods.Common.Activities
// If there's no next activity, move to rallypoint if available, else just leave host if Repairable.
// Do nothing if RepairableNear (RepairableNear actors don't enter their host and will likely remain within closeEnough).
// If there's a next activity and we're not RepairableNear, first leave host if the next activity is not a Move.
moveCooldownHelper.NotifyMoveQueued();
if (self.CurrentActivity.NextActivity == null)
{
if (rp != null && rp.Path.Count > 0)