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,12 +105,10 @@ 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;
} }
public override Activity Tick( Actor self ) public override Activity Tick(Actor self)
{ {
var mobile = self.Trait<Mobile>(); var mobile = self.Trait<Mobile>();
var info = self.Info.Traits.Get<MobileInfo>(); var info = self.Info.Traits.Get<MobileInfo>();
@@ -110,13 +117,14 @@ namespace OpenRA.Mods.RA.Move
{ {
if (mobile.Altitude < info.Altitude) if (mobile.Altitude < info.Altitude)
++mobile.Altitude; ++mobile.Altitude;
return this; return this;
} }
if (destination == mobile.toCell) if (destination == mobile.toCell)
return NextActivity; return NextActivity;
if( path == null ) if (path == null)
{ {
if (mobile.ticksBeforePathing > 0) if (mobile.ticksBeforePathing > 0)
{ {
@@ -128,54 +136,51 @@ namespace OpenRA.Mods.RA.Move
SanityCheckPath( mobile ); SanityCheckPath( mobile );
} }
if( path.Count == 0 ) if (path.Count == 0)
{ {
destination = mobile.toCell; destination = mobile.toCell;
return this; return this;
} }
destination = path[ 0 ]; destination = path[0];
var nextCell = PopPath( self, mobile ); var nextCell = PopPath(self, mobile);
if( nextCell == null ) if (nextCell == null)
return this; return this;
var dir = nextCell.Value.First - mobile.fromCell; var dir = nextCell.Value.First - mobile.fromCell;
var firstFacing = Util.GetFacing( dir, mobile.Facing ); var firstFacing = Util.GetFacing(dir, mobile.Facing);
if( firstFacing != mobile.Facing ) if (firstFacing != mobile.Facing)
{ {
path.Add( nextCell.Value.First ); path.Add( nextCell.Value.First );
return Util.SequenceActivities( new Turn( firstFacing ), this ); return Util.SequenceActivities(new Turn(firstFacing), this);
} }
else else
{ {
mobile.SetLocation( mobile.fromCell, mobile.fromSubCell, nextCell.Value.First, nextCell.Value.Second ); mobile.SetLocation(mobile.fromCell, mobile.fromSubCell, nextCell.Value.First, nextCell.Value.Second);
var move = new MoveFirstHalf( var move = new MoveFirstHalf(
this, this,
Util.CenterOfCell( mobile.fromCell ) + MobileInfo.SubCellOffsets[mobile.fromSubCell], Util.CenterOfCell(mobile.fromCell) + MobileInfo.SubCellOffsets[mobile.fromSubCell],
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;
} }
} }
[Conditional( "SANITY_CHECKS")] [Conditional("SANITY_CHECKS")]
void SanityCheckPath( Mobile mobile ) void SanityCheckPath(Mobile mobile)
{ {
if( path.Count == 0 ) if (path.Count == 0)
return; return;
var d = path[path.Count-1] - mobile.toCell; var d = path[path.Count - 1] - mobile.toCell;
if( d.LengthSquared > 2 ) if (d.LengthSquared > 2)
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)
var nextCell = path[ path.Count - 1 ]; return null;
if( !mobile.CanEnterCell( nextCell, ignoreBuilding, true ) )
var nextCell = path[path.Count - 1];
// Next cell in the move is blocked by another actor
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,9 +247,10 @@ 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);
var subCell = mobile.GetDesiredSubcell(nextCell, ignoreBuilding); var subCell = mobile.GetDesiredSubcell(nextCell, ignoreBuilding);
return Pair.New(nextCell, subCell); return Pair.New(nextCell, subCell);
@@ -247,11 +262,11 @@ namespace OpenRA.Mods.RA.Move
base.Cancel(self); base.Cancel(self);
} }
public override IEnumerable<Target> GetTargets( Actor self ) public override IEnumerable<Target> GetTargets(Actor self)
{ {
if( path != null ) if (path != null)
return Enumerable.Reverse(path).Select( c => Target.FromCell(c) ); return Enumerable.Reverse(path).Select(c => Target.FromCell(c));
if( destination != null ) if (destination != null)
return new Target[] { Target.FromCell(destination.Value) }; return new Target[] { Target.FromCell(destination.Value) };
return Target.NoTargets; return Target.NoTargets;
} }
@@ -272,59 +287,59 @@ 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)
{ {
move.Cancel( self ); move.Cancel(self);
base.Cancel( self ); base.Cancel(self);
} }
public override void Queue( Activity activity ) public override void Queue(Activity activity)
{ {
move.Queue( activity ); move.Queue(activity);
} }
public override Activity Tick( Actor self ) public override Activity Tick(Actor self)
{ {
var mobile = self.Trait<Mobile>(); var mobile = self.Trait<Mobile>();
var ret = InnerTick( self, mobile ); var ret = InnerTick(self, mobile);
mobile.IsMoving = ( ret is MovePart ); mobile.IsMoving = (ret is MovePart);
if( moveFraction > moveFractionTotal ) if (moveFraction > moveFractionTotal)
moveFraction = moveFractionTotal; moveFraction = moveFractionTotal;
UpdateCenterLocation( self, mobile ); UpdateCenterLocation(self, mobile);
return ret; return ret;
} }
Activity InnerTick( Actor self, Mobile mobile ) Activity InnerTick(Actor self, Mobile mobile)
{ {
moveFraction += mobile.MovementSpeedForCell(self, mobile.toCell); moveFraction += mobile.MovementSpeedForCell(self, mobile.toCell);
if( moveFraction <= moveFractionTotal ) if (moveFraction <= moveFractionTotal)
return this; return this;
var next = OnComplete( self, mobile, move ); var next = OnComplete(self, mobile, move);
if( next != null ) if (next != null)
return next; return next;
return move; return move;
} }
void UpdateCenterLocation( Actor self, Mobile mobile ) void UpdateCenterLocation(Actor self, Mobile mobile)
{ {
mobile.PxPosition = PPos.Lerp(from, to, moveFraction, moveFractionTotal); mobile.PxPosition = PPos.Lerp(from, to, moveFraction, moveFractionTotal);
if( moveFraction >= moveFractionTotal ) if (moveFraction >= moveFractionTotal)
mobile.Facing = toFacing & 0xFF; mobile.Facing = toFacing & 0xFF;
else else
mobile.Facing = int2.Lerp( fromFacing, toFacing, moveFraction, moveFractionTotal ) & 0xFF; mobile.Facing = int2.Lerp(fromFacing, toFacing, moveFraction, moveFractionTotal) & 0xFF;
} }
protected abstract MovePart OnComplete( Actor self, Mobile mobile, Move parent ); protected abstract MovePart OnComplete(Actor self, Mobile mobile, Move parent);
public override IEnumerable<Target> GetTargets( Actor self ) public override IEnumerable<Target> GetTargets(Actor self)
{ {
return move.GetTargets(self); return move.GetTargets(self);
} }
@@ -333,50 +348,52 @@ namespace OpenRA.Mods.RA.Move
class MoveFirstHalf : MovePart class MoveFirstHalf : MovePart
{ {
public MoveFirstHalf(Move move, PPos from, PPos to, int fromFacing, int toFacing, int startingFraction) public MoveFirstHalf(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) { }
static bool IsTurn( Mobile mobile, CPos nextCell ) static bool IsTurn(Mobile mobile, CPos nextCell)
{ {
return nextCell - mobile.toCell != return nextCell - mobile.toCell !=
mobile.toCell - mobile.fromCell; mobile.toCell - mobile.fromCell;
} }
protected override MovePart OnComplete( Actor self, Mobile mobile, Move parent ) protected override MovePart OnComplete(Actor self, Mobile mobile, Move parent)
{ {
var fromSubcellOffset = MobileInfo.SubCellOffsets[mobile.fromSubCell]; var fromSubcellOffset = MobileInfo.SubCellOffsets[mobile.fromSubCell];
var toSubcellOffset = MobileInfo.SubCellOffsets[mobile.toSubCell]; var toSubcellOffset = MobileInfo.SubCellOffsets[mobile.toSubCell];
var nextCell = parent.PopPath( self, mobile ); var nextCell = parent.PopPath(self, mobile);
if( nextCell != null ) if (nextCell != null)
{ {
if(IsTurn(mobile, nextCell.Value.First)) if (IsTurn(mobile, nextCell.Value.First))
{ {
var nextSubcellOffset = MobileInfo.SubCellOffsets[nextCell.Value.Second]; var nextSubcellOffset = MobileInfo.SubCellOffsets[nextCell.Value.Second];
var ret = new MoveFirstHalf( var ret = new MoveFirstHalf(
move, move,
Util.BetweenCells( mobile.fromCell, mobile.toCell ) + (fromSubcellOffset + toSubcellOffset) / 2, Util.BetweenCells(mobile.fromCell, mobile.toCell) + (fromSubcellOffset + toSubcellOffset) / 2,
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;
} }
parent.path.Add( nextCell.Value.First ); parent.path.Add(nextCell.Value.First);
} }
var ret2 = new MoveSecondHalf( var ret2 = new MoveSecondHalf(
move, move,
Util.BetweenCells( mobile.fromCell, mobile.toCell ) + (fromSubcellOffset + toSubcellOffset) / 2, Util.BetweenCells(mobile.fromCell, mobile.toCell) + (fromSubcellOffset + toSubcellOffset) / 2,
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);
return ret2; return ret2;
} }
} }
@@ -384,14 +401,12 @@ 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)
{ {
mobile.PxPosition = Util.CenterOfCell( mobile.toCell ); mobile.PxPosition = Util.CenterOfCell(mobile.toCell);
mobile.SetLocation( mobile.toCell, mobile.toSubCell, mobile.toCell, mobile.toSubCell ); mobile.SetLocation(mobile.toCell, mobile.toSubCell, mobile.toCell, mobile.toSubCell);
mobile.FinishedMoving(self); mobile.FinishedMoving(self);
return null; return null;
} }
@@ -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);