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.
125 lines
3.5 KiB
C#
125 lines
3.5 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 HarvestResource : Activity
|
|
{
|
|
readonly Harvester harv;
|
|
readonly HarvesterInfo harvInfo;
|
|
readonly IFacing facing;
|
|
readonly ResourceClaimLayer claimLayer;
|
|
readonly IResourceLayer resourceLayer;
|
|
readonly BodyOrientation body;
|
|
readonly IMove move;
|
|
readonly CPos targetCell;
|
|
readonly INotifyHarvestAction[] notifyHarvestActions;
|
|
readonly MoveCooldownHelper moveCooldownHelper;
|
|
|
|
public HarvestResource(Actor self, CPos targetCell)
|
|
{
|
|
harv = self.Trait<Harvester>();
|
|
harvInfo = self.Info.TraitInfo<HarvesterInfo>();
|
|
facing = self.Trait<IFacing>();
|
|
body = self.Trait<BodyOrientation>();
|
|
move = self.Trait<IMove>();
|
|
claimLayer = self.World.WorldActor.Trait<ResourceClaimLayer>();
|
|
resourceLayer = self.World.WorldActor.Trait<IResourceLayer>();
|
|
this.targetCell = targetCell;
|
|
notifyHarvestActions = self.TraitsImplementing<INotifyHarvestAction>().ToArray();
|
|
moveCooldownHelper = new MoveCooldownHelper(self.World, move as Mobile);
|
|
}
|
|
|
|
protected override void OnFirstRun(Actor self)
|
|
{
|
|
// We can safely assume the claim is successful, since this is only called in the
|
|
// same actor-tick as the targetCell is selected. Therefore no other harvester
|
|
// would have been able to claim.
|
|
claimLayer.TryClaimCell(self, targetCell);
|
|
}
|
|
|
|
public override bool Tick(Actor self)
|
|
{
|
|
if (harv.IsTraitDisabled)
|
|
Cancel(self, true);
|
|
|
|
if (IsCanceling || harv.IsFull)
|
|
return true;
|
|
|
|
var result = moveCooldownHelper.Tick(false);
|
|
if (result != null)
|
|
return result.Value;
|
|
|
|
// Move towards the target cell
|
|
if (self.Location != targetCell)
|
|
{
|
|
foreach (var n in notifyHarvestActions)
|
|
n.MovingToResources(self, targetCell);
|
|
|
|
moveCooldownHelper.NotifyMoveQueued();
|
|
QueueChild(move.MoveTo(targetCell, 0));
|
|
return false;
|
|
}
|
|
|
|
if (!harv.CanHarvestCell(self.Location))
|
|
return true;
|
|
|
|
// Turn to one of the harvestable facings
|
|
if (harvInfo.HarvestFacings != 0)
|
|
{
|
|
var current = facing.Facing;
|
|
var desired = body.QuantizeFacing(current, harvInfo.HarvestFacings);
|
|
if (desired != current)
|
|
{
|
|
QueueChild(new Turn(self, desired));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
var resource = resourceLayer.GetResource(self.Location);
|
|
if (resource.Type == null || resourceLayer.RemoveResource(resource.Type, self.Location) != 1)
|
|
return true;
|
|
|
|
harv.AddResource(self, resource.Type);
|
|
|
|
foreach (var t in notifyHarvestActions)
|
|
t.Harvested(self, resource.Type);
|
|
|
|
QueueChild(new Wait(harvInfo.BaleLoadDelay));
|
|
return false;
|
|
}
|
|
|
|
protected override void OnLastRun(Actor self)
|
|
{
|
|
claimLayer.RemoveClaim(self);
|
|
}
|
|
|
|
public override void Cancel(Actor self, bool keepQueue = false)
|
|
{
|
|
foreach (var n in notifyHarvestActions)
|
|
n.MovementCancelled(self);
|
|
|
|
base.Cancel(self, keepQueue);
|
|
}
|
|
|
|
public override IEnumerable<TargetLineNode> TargetLineNodes(Actor self)
|
|
{
|
|
yield return new TargetLineNode(Target.FromCell(self.World, targetCell), harvInfo.HarvestLineColor);
|
|
}
|
|
}
|
|
}
|