Calculate a new path (and possibly new target cell) if the original path is blocked.

This commit is contained in:
Paul Chote
2014-07-19 16:17:08 +12:00
parent e805461be7
commit 5b0cee78cf

View File

@@ -8,6 +8,7 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.RA.Move;
@@ -17,6 +18,8 @@ namespace OpenRA.Mods.RA.Activities
{
public class MoveAdjacentTo : Activity
{
static readonly List<CPos> NoPath = new List<CPos>();
readonly Mobile mobile;
readonly PathFinder pathFinder;
readonly DomainIndex domainIndex;
@@ -52,6 +55,11 @@ namespace OpenRA.Mods.RA.Activities
return targetPosition != oldTargetPosition;
}
protected virtual IEnumerable<CPos> CandidateMovementCells(Actor self)
{
return Util.AdjacentCells(self.World, target);
}
public override Activity Tick(Actor self)
{
var targetIsValid = target.IsValidFor(self);
@@ -65,7 +73,7 @@ namespace OpenRA.Mods.RA.Activities
return NextActivity;
// Target has moved, and MoveAdjacentTo is still valid.
UpdateInnerPath(self);
inner = mobile.MoveTo(() => CalculatePathToTarget(self));
repath = false;
}
@@ -97,12 +105,7 @@ namespace OpenRA.Mods.RA.Activities
return this;
}
protected virtual IEnumerable<CPos> CandidateMovementCells(Actor self)
{
return Util.AdjacentCells(self.World, target);
}
void UpdateInnerPath(Actor self)
List<CPos> CalculatePathToTarget(Actor self)
{
var targetCells = CandidateMovementCells(self);
var searchCells = new List<CPos>();
@@ -113,14 +116,12 @@ namespace OpenRA.Mods.RA.Activities
searchCells.Add(cell);
if (!searchCells.Any())
return;
return NoPath;
var path = pathFinder.FindBidiPath(
PathSearch.FromPoints(self.World, mobile.Info, self, searchCells, loc, true),
PathSearch.FromPoint(self.World, mobile.Info, self, loc, targetPosition, true).Reverse()
);
var fromSrc = PathSearch.FromPoints(self.World, mobile.Info, self, searchCells, loc, true);
var fromDest = PathSearch.FromPoint(self.World, mobile.Info, self, loc, targetPosition, true).Reverse();
inner = mobile.MoveTo(() => path);
return pathFinder.FindBidiPath(fromSrc, fromDest);
}
public override IEnumerable<Target> GetTargets(Actor self)