Tidy up Move.

This commit is contained in:
Paul Chote
2013-07-10 20:34:19 +12:00
parent 0326d2bbd0
commit b10a5d27a0

View File

@@ -21,11 +21,16 @@ namespace OpenRA.Mods.RA.Move
class Move : Activity class Move : Activity
{ {
CPos? destination; CPos? destination;
int nearEnough; WRange nearEnough;
public List<CPos> path; public List<CPos> path;
Func<Actor, Mobile, List<CPos>> getPath; Func<Actor, Mobile, List<CPos>> getPath;
public Actor ignoreBuilding; public Actor ignoreBuilding;
// For dealing with blockers
bool hasWaited;
bool hasNotifiedBlocker;
int waitTicksRemaining;
// Scriptable move order // Scriptable move order
// Ignores lane bias and nearby units // Ignores lane bias and nearby units
public Move(CPos destination) public Move(CPos destination)
@@ -35,10 +40,14 @@ namespace OpenRA.Mods.RA.Move
PathSearch.FromPoint( self.World, mobile.Info, self, mobile.toCell, destination, false ) PathSearch.FromPoint( self.World, mobile.Info, self, mobile.toCell, destination, false )
.WithoutLaneBias()); .WithoutLaneBias());
this.destination = destination; this.destination = destination;
this.nearEnough = 0; this.nearEnough = WRange.Zero;
} }
// Hack for legacy code
public Move(CPos destination, int nearEnough) public Move(CPos destination, int nearEnough)
: this(destination, WRange.FromCells(nearEnough)) {}
public Move(CPos destination, WRange nearEnough)
{ {
this.getPath = (self,mobile) => self.World.WorldActor.Trait<PathFinder>().FindUnitPath( mobile.toCell, destination, self ); this.getPath = (self,mobile) => self.World.WorldActor.Trait<PathFinder>().FindUnitPath( mobile.toCell, destination, self );
this.destination = destination; this.destination = destination;
@@ -54,7 +63,7 @@ namespace OpenRA.Mods.RA.Move
); );
this.destination = destination; this.destination = destination;
this.nearEnough = 0; this.nearEnough = WRange.Zero;
this.ignoreBuilding = ignoreBuilding; this.ignoreBuilding = ignoreBuilding;
} }
@@ -72,14 +81,14 @@ namespace OpenRA.Mods.RA.Move
}; };
this.destination = null; this.destination = null;
this.nearEnough = range.Range / 1024; this.nearEnough = range;
} }
public Move(Func<List<CPos>> getPath) public Move(Func<List<CPos>> getPath)
{ {
this.getPath = (_1,_2) => getPath(); this.getPath = (_1,_2) => getPath();
this.destination = null; this.destination = null;
this.nearEnough = 0; this.nearEnough = WRange.Zero;
} }
static int HashList<T>(List<T> xs) static int HashList<T>(List<T> xs)
@@ -96,8 +105,6 @@ namespace OpenRA.Mods.RA.Move
{ {
var path = getPath(self, mobile).TakeWhile(a => a != mobile.toCell).ToList(); var path = getPath(self, mobile).TakeWhile(a => a != mobile.toCell).ToList();
mobile.PathHash = HashList(path); mobile.PathHash = HashList(path);
Log.Write("debug", "EvalPathHash #{0} {1}",
self.ActorID, mobile.PathHash);
return path; return path;
} }
@@ -110,6 +117,7 @@ namespace OpenRA.Mods.RA.Move
{ {
if (mobile.Altitude < info.Altitude) if (mobile.Altitude < info.Altitude)
++mobile.Altitude; ++mobile.Altitude;
return this; return this;
} }
@@ -156,7 +164,8 @@ namespace OpenRA.Mods.RA.Move
Util.BetweenCells(mobile.fromCell, mobile.toCell) + (MobileInfo.SubCellOffsets[mobile.fromSubCell] + MobileInfo.SubCellOffsets[mobile.toSubCell]) / 2, Util.BetweenCells(mobile.fromCell, mobile.toCell) + (MobileInfo.SubCellOffsets[mobile.fromSubCell] + MobileInfo.SubCellOffsets[mobile.toSubCell]) / 2,
mobile.Facing, mobile.Facing,
mobile.Facing, mobile.Facing,
0 ); 0
);
return move; return move;
} }
@@ -172,10 +181,6 @@ namespace OpenRA.Mods.RA.Move
throw new InvalidOperationException("(Move) Sanity check failed"); throw new InvalidOperationException("(Move) Sanity check failed");
} }
bool hasWaited;
bool hasNotifiedBlocker;
int waitTicksRemaining;
void NotifyBlocker(Actor self, CPos nextCell) void NotifyBlocker(Actor self, CPos nextCell)
{ {
foreach (var blocker in self.World.ActorMap.GetUnitsAt(nextCell)) foreach (var blocker in self.World.ActorMap.GetUnitsAt(nextCell))
@@ -192,22 +197,30 @@ namespace OpenRA.Mods.RA.Move
Pair<CPos, SubCell>? PopPath(Actor self, Mobile mobile) Pair<CPos, SubCell>? PopPath(Actor self, Mobile mobile)
{ {
if( path.Count == 0 ) return null; if (path.Count == 0)
return null;
var nextCell = path[path.Count - 1]; var nextCell = path[path.Count - 1];
// Next cell in the move is blocked by another actor
if (!mobile.CanEnterCell(nextCell, ignoreBuilding, true)) if (!mobile.CanEnterCell(nextCell, ignoreBuilding, true))
{ {
if( ( mobile.toCell - destination.Value ).LengthSquared <= nearEnough ) // Are we close enough?
var cellRange = nearEnough.Range / 1024;
if ((mobile.toCell - destination.Value).LengthSquared <= cellRange*cellRange)
{ {
path.Clear(); path.Clear();
return null; return null;
} }
// See if they will move
if (!hasNotifiedBlocker) if (!hasNotifiedBlocker)
{ {
NotifyBlocker(self, nextCell); NotifyBlocker(self, nextCell);
hasNotifiedBlocker = true; hasNotifiedBlocker = true;
} }
// Wait a bit to see if they leave
if (!hasWaited) if (!hasWaited)
{ {
var info = self.Info.Traits.Get<MobileInfo>(); var info = self.Info.Traits.Get<MobileInfo>();
@@ -224,6 +237,7 @@ namespace OpenRA.Mods.RA.Move
return null; return null;
} }
// Calculate a new path
mobile.RemoveInfluence(); mobile.RemoveInfluence();
var newPath = EvalPath(self, mobile); var newPath = EvalPath(self, mobile);
mobile.AddInfluence(); mobile.AddInfluence();
@@ -233,6 +247,7 @@ namespace OpenRA.Mods.RA.Move
return null; return null;
} }
hasNotifiedBlocker = false; hasNotifiedBlocker = false;
hasWaited = false; hasWaited = false;
path.RemoveAt(path.Count - 1); path.RemoveAt(path.Count - 1);
@@ -272,7 +287,7 @@ namespace OpenRA.Mods.RA.Move
this.fromFacing = fromFacing; this.fromFacing = fromFacing;
this.toFacing = toFacing; this.toFacing = toFacing;
this.moveFraction = startingFraction; this.moveFraction = startingFraction;
this.moveFractionTotal = ( ( to - from ) * 3 ).Length; this.moveFractionTotal = 3*(to - from).Length;
} }
public override void Cancel(Actor self) public override void Cancel(Actor self)
@@ -358,7 +373,8 @@ namespace OpenRA.Mods.RA.Move
Util.BetweenCells(mobile.toCell, nextCell.Value.First) + (toSubcellOffset + nextSubcellOffset) / 2, Util.BetweenCells(mobile.toCell, nextCell.Value.First) + (toSubcellOffset + nextSubcellOffset) / 2,
mobile.Facing, mobile.Facing,
Util.GetNearestFacing(mobile.Facing, Util.GetFacing(nextCell.Value.First - mobile.toCell, mobile.Facing)), Util.GetNearestFacing(mobile.Facing, Util.GetFacing(nextCell.Value.First - mobile.toCell, mobile.Facing)),
moveFraction - moveFractionTotal ); moveFraction - moveFractionTotal
);
mobile.SetLocation(mobile.toCell, mobile.toSubCell, nextCell.Value.First, nextCell.Value.Second); mobile.SetLocation(mobile.toCell, mobile.toSubCell, nextCell.Value.First, nextCell.Value.Second);
return ret; return ret;
@@ -373,7 +389,8 @@ namespace OpenRA.Mods.RA.Move
Util.CenterOfCell(mobile.toCell) + toSubcellOffset, Util.CenterOfCell(mobile.toCell) + toSubcellOffset,
mobile.Facing, mobile.Facing,
mobile.Facing, mobile.Facing,
moveFraction - moveFractionTotal ); moveFraction - moveFractionTotal
);
mobile.EnteringCell(self); mobile.EnteringCell(self);
mobile.SetLocation(mobile.toCell, mobile.toSubCell, mobile.toCell, mobile.toSubCell); mobile.SetLocation(mobile.toCell, mobile.toSubCell, mobile.toCell, mobile.toSubCell);
@@ -384,9 +401,7 @@ namespace OpenRA.Mods.RA.Move
class MoveSecondHalf : MovePart class MoveSecondHalf : MovePart
{ {
public MoveSecondHalf(Move move, PPos from, PPos to, int fromFacing, int toFacing, int startingFraction) public MoveSecondHalf(Move move, PPos from, PPos to, int fromFacing, int toFacing, int startingFraction)
: base( move, from, to, fromFacing, toFacing, startingFraction ) : base(move, from, to, fromFacing, toFacing, startingFraction) {}
{
}
protected override MovePart OnComplete(Actor self, Mobile mobile, Move parent) protected override MovePart OnComplete(Actor self, Mobile mobile, Move parent)
{ {
@@ -402,7 +417,8 @@ namespace OpenRA.Mods.RA.Move
{ {
public static bool IsMoving(this Actor self) public static bool IsMoving(this Actor self)
{ {
if (self.IsIdle) return false; if (self.IsIdle)
return false;
Activity a = self.GetCurrentActivity(); Activity a = self.GetCurrentActivity();
Debug.Assert(a != null); Debug.Assert(a != null);