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.
111 lines
3.1 KiB
C#
111 lines
3.1 KiB
C#
#region Copyright & License Information
|
|
/*
|
|
* Copyright (c) The OpenRA Developers and Contributors
|
|
* This file is part of OpenRA, which is free software. It is made
|
|
* available to you under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation, either version 3 of
|
|
* the License, or (at your option) any later version. For more
|
|
* information, see COPYING.
|
|
*/
|
|
#endregion
|
|
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using OpenRA.Activities;
|
|
using OpenRA.Mods.Common.Traits;
|
|
using OpenRA.Traits;
|
|
|
|
namespace OpenRA.Mods.Common.Activities
|
|
{
|
|
public class MoveToDock : Activity
|
|
{
|
|
readonly DockClientManager dockClient;
|
|
Actor dockHostActor;
|
|
IDockHost dockHost;
|
|
readonly INotifyDockClientMoving[] notifyDockClientMoving;
|
|
readonly MoveCooldownHelper moveCooldownHelper;
|
|
|
|
public MoveToDock(Actor self, Actor dockHostActor = null, IDockHost dockHost = null)
|
|
{
|
|
dockClient = self.Trait<DockClientManager>();
|
|
this.dockHostActor = dockHostActor;
|
|
this.dockHost = dockHost;
|
|
notifyDockClientMoving = self.TraitsImplementing<INotifyDockClientMoving>().ToArray();
|
|
moveCooldownHelper = new MoveCooldownHelper(self.World, self.Trait<IMove>() as Mobile) { RetryIfDestinationBlocked = true };
|
|
}
|
|
|
|
public override bool Tick(Actor self)
|
|
{
|
|
if (IsCanceling)
|
|
return true;
|
|
|
|
if (dockClient.IsTraitDisabled)
|
|
{
|
|
Cancel(self, true);
|
|
return true;
|
|
}
|
|
|
|
// Find the nearest DockHost if not explicitly ordered to a specific dock.
|
|
if (dockHost == null || !dockHost.IsEnabledAndInWorld)
|
|
{
|
|
var host = dockClient.ClosestDock(null);
|
|
if (host.HasValue)
|
|
{
|
|
dockHost = host.Value.Trait;
|
|
dockHostActor = host.Value.Actor;
|
|
}
|
|
else
|
|
{
|
|
// No docks exist; check again after delay defined in dockClient.
|
|
QueueChild(new Wait(dockClient.Info.SearchForDockDelay));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
var result = moveCooldownHelper.Tick(false);
|
|
if (result != null)
|
|
return result.Value;
|
|
|
|
if (dockClient.ReserveHost(dockHostActor, dockHost))
|
|
{
|
|
if (dockHost.QueueMoveActivity(this, dockHostActor, self, dockClient, moveCooldownHelper))
|
|
{
|
|
foreach (var ndcm in notifyDockClientMoving)
|
|
ndcm.MovingToDock(self, dockHostActor, dockHost);
|
|
|
|
return false;
|
|
}
|
|
|
|
dockHost.QueueDockActivity(this, dockHostActor, self, dockClient);
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
// The dock explicitly chosen by the user is currently occupied. Wait and check again.
|
|
QueueChild(new Wait(dockClient.Info.SearchForDockDelay));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public override void Cancel(Actor self, bool keepQueue = false)
|
|
{
|
|
dockClient.UnreserveHost();
|
|
foreach (var ndcm in notifyDockClientMoving)
|
|
ndcm.MovementCancelled(self);
|
|
|
|
base.Cancel(self, keepQueue);
|
|
}
|
|
|
|
public override IEnumerable<TargetLineNode> TargetLineNodes(Actor self)
|
|
{
|
|
if (dockHostActor != null)
|
|
yield return new TargetLineNode(Target.FromActor(dockHostActor), dockClient.DockLineColor);
|
|
else
|
|
{
|
|
if (dockClient.ReservedHostActor != null)
|
|
yield return new TargetLineNode(Target.FromActor(dockClient.ReservedHostActor), dockClient.DockLineColor);
|
|
}
|
|
}
|
|
}
|
|
}
|