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:
@@ -33,6 +33,7 @@ namespace OpenRA.Mods.Common.Activities
|
||||
readonly IPositionable positionable;
|
||||
readonly bool forceAttack;
|
||||
readonly Color? targetLineColor;
|
||||
readonly MoveCooldownHelper moveCooldownHelper;
|
||||
|
||||
protected Target target;
|
||||
Target lastVisibleTarget;
|
||||
@@ -40,7 +41,6 @@ namespace OpenRA.Mods.Common.Activities
|
||||
BitSet<TargetableType> lastVisibleTargetTypes;
|
||||
Player lastVisibleOwner;
|
||||
bool useLastVisibleTarget;
|
||||
bool wasMovingWithinRange;
|
||||
|
||||
WDist minRange;
|
||||
WDist maxRange;
|
||||
@@ -61,6 +61,8 @@ namespace OpenRA.Mods.Common.Activities
|
||||
mobile = iMove as Mobile;
|
||||
move = allowMovement ? iMove : null;
|
||||
|
||||
moveCooldownHelper = new MoveCooldownHelper(self.World, mobile);
|
||||
|
||||
// The target may become hidden between the initial order request and the first tick (e.g. if queued)
|
||||
// Moving to any position (even if quite stale) is still better than immediately giving up
|
||||
if ((target.Type == TargetType.Actor && target.Actor.CanBeViewedByPlayer(self.Owner))
|
||||
@@ -114,16 +116,14 @@ namespace OpenRA.Mods.Common.Activities
|
||||
|
||||
useLastVisibleTarget = targetIsHiddenActor || !target.IsValidFor(self);
|
||||
|
||||
// If we are ticking again after previously sequencing a MoveWithRange then that move must have completed
|
||||
// Either we are in range and can see the target, or we've lost track of it and should give up
|
||||
if (wasMovingWithinRange && targetIsHiddenActor)
|
||||
return true;
|
||||
var result = moveCooldownHelper.Tick(targetIsHiddenActor);
|
||||
if (result != null)
|
||||
return result.Value;
|
||||
|
||||
// Target is hidden or dead, and we don't have a fallback position to move towards
|
||||
if (useLastVisibleTarget && !lastVisibleTarget.IsValidFor(self))
|
||||
return true;
|
||||
|
||||
wasMovingWithinRange = false;
|
||||
var pos = self.CenterPosition;
|
||||
var checkTarget = useLastVisibleTarget ? lastVisibleTarget : target;
|
||||
|
||||
@@ -135,7 +135,7 @@ namespace OpenRA.Mods.Common.Activities
|
||||
return true;
|
||||
|
||||
// Move towards the last known position
|
||||
wasMovingWithinRange = true;
|
||||
moveCooldownHelper.NotifyMoveQueued();
|
||||
QueueChild(move.MoveWithinRange(target, WDist.Zero, lastVisibleMaximumRange, checkTarget.CenterPosition, Color.Red));
|
||||
return false;
|
||||
}
|
||||
@@ -148,9 +148,6 @@ namespace OpenRA.Mods.Common.Activities
|
||||
attack.IsAiming = status == AttackStatus.Attacking || status == AttackStatus.NeedsToTurn;
|
||||
}
|
||||
|
||||
if (attackStatus.HasFlag(AttackStatus.NeedsToMove))
|
||||
wasMovingWithinRange = true;
|
||||
|
||||
if (attackStatus >= AttackStatus.NeedsToTurn)
|
||||
return false;
|
||||
|
||||
@@ -185,6 +182,7 @@ namespace OpenRA.Mods.Common.Activities
|
||||
var sightRange = rs != null ? rs.Range : WDist.FromCells(2);
|
||||
|
||||
attackStatus |= AttackStatus.NeedsToMove;
|
||||
moveCooldownHelper.NotifyMoveQueued();
|
||||
QueueChild(move.MoveWithinRange(target, sightRange, target.CenterPosition, Color.Red));
|
||||
return AttackStatus.NeedsToMove;
|
||||
}
|
||||
@@ -218,6 +216,7 @@ namespace OpenRA.Mods.Common.Activities
|
||||
return AttackStatus.UnableToAttack;
|
||||
|
||||
attackStatus |= AttackStatus.NeedsToMove;
|
||||
moveCooldownHelper.NotifyMoveQueued();
|
||||
var checkTarget = useLastVisibleTarget ? lastVisibleTarget : target;
|
||||
QueueChild(move.MoveWithinRange(target, minRange, maxRange, checkTarget.CenterPosition, Color.Red));
|
||||
return AttackStatus.NeedsToMove;
|
||||
|
||||
Reference in New Issue
Block a user