Simplify MoveAdjacentTo logic. Fixes #5445.

This commit is contained in:
Paul Chote
2014-01-13 21:43:26 +13:00
parent 4ef82f2863
commit 40eff95a10

View File

@@ -21,11 +21,10 @@ namespace OpenRA.Mods.RA.Activities
readonly Mobile mobile; readonly Mobile mobile;
readonly PathFinder pathFinder; readonly PathFinder pathFinder;
readonly DomainIndex domainIndex; readonly DomainIndex domainIndex;
readonly int movementClass; readonly uint movementClass;
Activity inner; Activity inner;
CPos cachedTargetPosition; CPos targetPosition;
CPos[] adjacentCells;
bool repath; bool repath;
public MoveAdjacentTo(Actor self, Target target) public MoveAdjacentTo(Actor self, Target target)
@@ -35,36 +34,66 @@ namespace OpenRA.Mods.RA.Activities
mobile = self.Trait<Mobile>(); mobile = self.Trait<Mobile>();
pathFinder = self.World.WorldActor.Trait<PathFinder>(); pathFinder = self.World.WorldActor.Trait<PathFinder>();
domainIndex = self.World.WorldActor.TraitOrDefault<DomainIndex>(); domainIndex = self.World.WorldActor.TraitOrDefault<DomainIndex>();
movementClass = mobile.Info.GetMovementClass(self.World.TileSet); movementClass = (uint)mobile.Info.GetMovementClass(self.World.TileSet);
repath = true; repath = true;
} }
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
if (IsCanceled || !target.IsValidFor(self)) var targetIsValid = target.IsValidFor(self);
// Inner move order has completed.
if (inner == null)
{
// We are done here if the order was cancelled for any
// reason except the target moving.
if (IsCanceled || !repath || !targetIsValid)
return NextActivity; return NextActivity;
var targetPosition = target.CenterPosition.ToCPos(); // Target has moved, and MoveAdjacentTo is still valid.
UpdateInnerPath(self);
// Calculate path to target
if (inner == null && repath)
{
cachedTargetPosition = targetPosition;
adjacentCells = Util.AdjacentCells(target).ToArray();
repath = false; repath = false;
var loc = self.Location;
var searchCells = new List<CPos>();
foreach (var cell in adjacentCells)
{
if (cell == loc)
return NextActivity;
else if (domainIndex == null || domainIndex.IsPassable(loc, cell, (uint)movementClass))
searchCells.Add(cell);
} }
if (targetIsValid)
{
// Check if the target has moved
var oldPosition = targetPosition;
targetPosition = target.CenterPosition.ToCPos();
if (!repath && targetPosition != oldPosition)
{
// Finish moving into the next cell and then repath.
if (inner != null)
inner.Cancel(self);
repath = true;
}
}
else
{
// Target became invalid. Cancel the inner order,
// and then wait for it to move into the next cell
// before finishing this order (handled above).
inner.Cancel(self);
}
// Ticks the inner move activity to actually move the actor.
inner = Util.RunActivity(self, inner);
return this;
}
void UpdateInnerPath(Actor self)
{
var targetCells = Util.AdjacentCells(target);
var searchCells = new List<CPos>();
var loc = self.Location;
foreach (var cell in targetCells)
if (mobile.CanEnterCell(cell) && (domainIndex == null || domainIndex.IsPassable(loc, cell, movementClass)))
searchCells.Add(cell);
if (searchCells.Any()) if (searchCells.Any())
{ {
var ps1 = new PathSearch(self.World, mobile.Info, self) var ps1 = new PathSearch(self.World, mobile.Info, self)
@@ -78,31 +107,13 @@ namespace OpenRA.Mods.RA.Activities
ps1.AddInitialCell(cell); ps1.AddInitialCell(cell);
ps1.heuristic = PathSearch.DefaultEstimator(mobile.toCell); ps1.heuristic = PathSearch.DefaultEstimator(mobile.toCell);
var ps2 = PathSearch.FromPoint(self.World, mobile.Info, self, mobile.toCell, target.CenterPosition.ToCPos(), true); var ps2 = PathSearch.FromPoint(self.World, mobile.Info, self, mobile.toCell, targetPosition, true);
var ret = pathFinder.FindBidiPath(ps1, ps2); var ret = pathFinder.FindBidiPath(ps1, ps2);
inner = mobile.MoveTo(() => ret); inner = mobile.MoveTo(() => ret);
} }
} }
// Force a repath once the actor reaches the next cell
if (!repath && cachedTargetPosition != targetPosition)
{
if (inner != null)
inner.Cancel(self);
repath = true;
}
inner = Util.RunActivity(self, inner);
// Move completed
if (inner == null && adjacentCells.Contains(self.Location))
return NextActivity;
return this;
}
public override IEnumerable<Target> GetTargets(Actor self) public override IEnumerable<Target> GetTargets(Actor self)
{ {
if (inner != null) if (inner != null)
@@ -110,5 +121,13 @@ namespace OpenRA.Mods.RA.Activities
return Target.None; return Target.None;
} }
public override void Cancel(Actor self)
{
if (inner != null)
inner.Cancel(self);
base.Cancel(self);
}
} }
} }