New types for cell and pixel coordinate position/vectors.
This commit is contained in:
@@ -15,12 +15,12 @@ namespace OpenRA.Mods.RA.Move
|
||||
{
|
||||
public class Drag : Activity
|
||||
{
|
||||
int2 endLocation;
|
||||
int2 startLocation;
|
||||
PPos endLocation;
|
||||
PPos startLocation;
|
||||
int length;
|
||||
int ticks = 0;
|
||||
|
||||
public Drag(int2 start, int2 end, int length)
|
||||
public Drag(PPos start, PPos end, int length)
|
||||
{
|
||||
startLocation = start;
|
||||
endLocation = end;
|
||||
@@ -31,7 +31,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
{
|
||||
var mobile = self.Trait<Mobile>();
|
||||
mobile.PxPosition = length > 1
|
||||
? int2.Lerp(startLocation, endLocation, ticks, length - 1)
|
||||
? PPos.Lerp(startLocation, endLocation, ticks, length - 1)
|
||||
: endLocation;
|
||||
|
||||
if (++ticks >= length)
|
||||
|
||||
@@ -55,7 +55,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
public decimal Speed = 0;
|
||||
}
|
||||
|
||||
public int MovementCostForCell(World world, int2 cell)
|
||||
public int MovementCostForCell(World world, CPos cell)
|
||||
{
|
||||
if (!world.Map.IsInMap(cell.X, cell.Y))
|
||||
return int.MaxValue;
|
||||
@@ -67,17 +67,17 @@ namespace OpenRA.Mods.RA.Move
|
||||
return TerrainSpeeds[type].Cost;
|
||||
}
|
||||
|
||||
public readonly Dictionary<SubCell, int2> SubCellOffsets = new Dictionary<SubCell, int2>()
|
||||
public readonly Dictionary<SubCell, PVecInt> SubCellOffsets = new Dictionary<SubCell, PVecInt>()
|
||||
{
|
||||
{SubCell.TopLeft, new int2(-7,-6)},
|
||||
{SubCell.TopRight, new int2(6,-6)},
|
||||
{SubCell.Center, new int2(0,0)},
|
||||
{SubCell.BottomLeft, new int2(-7,6)},
|
||||
{SubCell.BottomRight, new int2(6,6)},
|
||||
{SubCell.FullCell, new int2(0,0)},
|
||||
{SubCell.TopLeft, new PVecInt(-7,-6)},
|
||||
{SubCell.TopRight, new PVecInt(6,-6)},
|
||||
{SubCell.Center, new PVecInt(0,0)},
|
||||
{SubCell.BottomLeft, new PVecInt(-7,6)},
|
||||
{SubCell.BottomRight, new PVecInt(6,6)},
|
||||
{SubCell.FullCell, new PVecInt(0,0)},
|
||||
};
|
||||
|
||||
public bool CanEnterCell(World world, Player owner, int2 cell, Actor ignoreActor, bool checkTransientActors)
|
||||
public bool CanEnterCell(World world, Player owner, CPos cell, Actor ignoreActor, bool checkTransientActors)
|
||||
{
|
||||
if (MovementCostForCell(world, cell) == int.MaxValue)
|
||||
return false;
|
||||
@@ -108,35 +108,29 @@ namespace OpenRA.Mods.RA.Move
|
||||
public bool IsMoving { get; internal set; }
|
||||
|
||||
int __facing;
|
||||
int2 __fromCell, __toCell;
|
||||
CPos __fromCell, __toCell;
|
||||
public SubCell fromSubCell, toSubCell;
|
||||
|
||||
//int __altitude;
|
||||
|
||||
[Sync]
|
||||
public int Facing
|
||||
[Sync] public int Facing
|
||||
{
|
||||
get { return __facing; }
|
||||
set { __facing = value; }
|
||||
}
|
||||
|
||||
[Sync]
|
||||
public int Altitude { get; set; }
|
||||
[Sync] public int Altitude { get; set; }
|
||||
|
||||
public int ROT { get { return Info.ROT; } }
|
||||
public int InitialFacing { get { return Info.InitialFacing; } }
|
||||
|
||||
[Sync]
|
||||
public int2 PxPosition { get; set; }
|
||||
[Sync]
|
||||
public int2 fromCell { get { return __fromCell; } }
|
||||
[Sync]
|
||||
public int2 toCell { get { return __toCell; } }
|
||||
[Sync] public PPos PxPosition { get; set; }
|
||||
[Sync] public CPos fromCell { get { return __fromCell; } }
|
||||
[Sync] public CPos toCell { get { return __toCell; } }
|
||||
|
||||
[Sync]
|
||||
public int PathHash; // written by Move.EvalPath, to temporarily debug this crap.
|
||||
[Sync] public int PathHash; // written by Move.EvalPath, to temporarily debug this crap.
|
||||
|
||||
public void SetLocation(int2 from, SubCell fromSub, int2 to, SubCell toSub)
|
||||
public void SetLocation(CPos from, SubCell fromSub, CPos to, SubCell toSub)
|
||||
{
|
||||
if (fromCell == from && toCell == to) return;
|
||||
RemoveInfluence();
|
||||
@@ -164,7 +158,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
|
||||
if (init.Contains<LocationInit>())
|
||||
{
|
||||
this.__fromCell = this.__toCell = init.Get<LocationInit, int2>();
|
||||
this.__fromCell = this.__toCell = init.Get<LocationInit, CPos>();
|
||||
this.PxPosition = Util.CenterOfCell(fromCell) + info.SubCellOffsets[fromSubCell];
|
||||
}
|
||||
|
||||
@@ -172,22 +166,22 @@ namespace OpenRA.Mods.RA.Move
|
||||
this.Altitude = init.Contains<AltitudeInit>() ? init.Get<AltitudeInit, int>() : 0;
|
||||
}
|
||||
|
||||
public void SetPosition(Actor self, int2 cell)
|
||||
public void SetPosition(Actor self, CPos cell)
|
||||
{
|
||||
SetLocation(cell,fromSubCell, cell,fromSubCell);
|
||||
PxPosition = Util.CenterOfCell(fromCell) + Info.SubCellOffsets[fromSubCell];
|
||||
FinishedMoving(self);
|
||||
}
|
||||
|
||||
public void SetPxPosition(Actor self, int2 px)
|
||||
public void SetPxPosition(Actor self, PPos px)
|
||||
{
|
||||
var cell = Util.CellContaining(px);
|
||||
var cell = px.ToCPos();
|
||||
SetLocation(cell,fromSubCell, cell,fromSubCell);
|
||||
PxPosition = px;
|
||||
FinishedMoving(self);
|
||||
}
|
||||
|
||||
public void AdjustPxPosition(Actor self, int2 px) /* visual hack only */
|
||||
public void AdjustPxPosition(Actor self, PPos px) /* visual hack only */
|
||||
{
|
||||
PxPosition = px;
|
||||
}
|
||||
@@ -200,17 +194,17 @@ namespace OpenRA.Mods.RA.Move
|
||||
if (order is MoveOrderTargeter)
|
||||
{
|
||||
if (Info.OnRails) return null;
|
||||
return new Order("Move", self, queued) { TargetLocation = Util.CellContaining(target.CenterLocation) };
|
||||
return new Order("Move", self, queued) { TargetLocation = target.CenterLocation.ToCPos() };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int2 NearestMoveableCell(int2 target)
|
||||
public CPos NearestMoveableCell(CPos target)
|
||||
{
|
||||
if (CanEnterCell(target))
|
||||
return target;
|
||||
|
||||
var searched = new List<int2>() { };
|
||||
var searched = new List<CPos>();
|
||||
// Limit search to a radius of 10 tiles
|
||||
for (int r = 1; r < 10; r++)
|
||||
foreach (var tile in self.World.FindTilesInCircle(target, r).Except(searched))
|
||||
@@ -225,9 +219,9 @@ namespace OpenRA.Mods.RA.Move
|
||||
return target;
|
||||
}
|
||||
|
||||
void PerformMoveInner(Actor self, int2 targetLocation, bool queued)
|
||||
void PerformMoveInner(Actor self, CPos targetLocation, bool queued)
|
||||
{
|
||||
int2 currentLocation = NearestMoveableCell(targetLocation);
|
||||
var currentLocation = NearestMoveableCell(targetLocation);
|
||||
|
||||
if (!CanEnterCell(currentLocation))
|
||||
{
|
||||
@@ -244,12 +238,12 @@ namespace OpenRA.Mods.RA.Move
|
||||
self.SetTargetLine(Target.FromCell(currentLocation), Color.Green);
|
||||
}
|
||||
|
||||
protected void PerformMove(Actor self, int2 targetLocation, bool queued)
|
||||
protected void PerformMove(Actor self, CPos targetLocation, bool queued)
|
||||
{
|
||||
if (queued)
|
||||
self.QueueActivity(new CallFunc(() => PerformMoveInner(self, targetLocation, queued)));
|
||||
self.QueueActivity(new CallFunc(() => PerformMoveInner(self, targetLocation, true)));
|
||||
else
|
||||
PerformMoveInner(self, targetLocation, queued);
|
||||
PerformMoveInner(self, targetLocation, false);
|
||||
}
|
||||
|
||||
public void ResolveOrder(Actor self, Order order)
|
||||
@@ -278,9 +272,9 @@ namespace OpenRA.Mods.RA.Move
|
||||
}
|
||||
}
|
||||
|
||||
public int2 TopLeft { get { return toCell; } }
|
||||
public CPos TopLeft { get { return toCell; } }
|
||||
|
||||
public IEnumerable<Pair<int2, SubCell>> OccupiedCells()
|
||||
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells()
|
||||
{
|
||||
if (fromCell == toCell)
|
||||
yield return Pair.New(fromCell, fromSubCell);
|
||||
@@ -293,7 +287,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
}
|
||||
}
|
||||
|
||||
public SubCell GetDesiredSubcell(int2 a, Actor ignoreActor)
|
||||
public SubCell GetDesiredSubcell(CPos a, Actor ignoreActor)
|
||||
{
|
||||
if (!Info.SharesCell)
|
||||
return SubCell.FullCell;
|
||||
@@ -303,7 +297,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
SubCell.BottomLeft, SubCell.BottomRight}.First(b =>
|
||||
{
|
||||
var blockingActors = self.World.ActorMap.GetUnitsAt(a,b).Where(c => c != ignoreActor);
|
||||
if (blockingActors.Count() > 0)
|
||||
if (blockingActors.Any())
|
||||
{
|
||||
// Non-sharable unit can enter a cell with shareable units only if it can crush all of them
|
||||
if (Info.Crushes == null)
|
||||
@@ -317,12 +311,12 @@ namespace OpenRA.Mods.RA.Move
|
||||
});
|
||||
}
|
||||
|
||||
public bool CanEnterCell(int2 p)
|
||||
public bool CanEnterCell(CPos p)
|
||||
{
|
||||
return CanEnterCell(p, null, true);
|
||||
}
|
||||
|
||||
public bool CanEnterCell(int2 cell, Actor ignoreActor, bool checkTransientActors)
|
||||
public bool CanEnterCell(CPos cell, Actor ignoreActor, bool checkTransientActors)
|
||||
{
|
||||
return Info.CanEnterCell(self.World, self.Owner, cell, ignoreActor, checkTransientActors);
|
||||
}
|
||||
@@ -349,7 +343,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
}
|
||||
}
|
||||
|
||||
public int MovementSpeedForCell(Actor self, int2 cell)
|
||||
public int MovementSpeedForCell(Actor self, CPos cell)
|
||||
{
|
||||
var type = self.World.GetTerrainType(cell);
|
||||
|
||||
@@ -385,13 +379,13 @@ namespace OpenRA.Mods.RA.Move
|
||||
return; /* don't nudge if we're busy doing something! */
|
||||
|
||||
// pick an adjacent available cell.
|
||||
var availCells = new List<int2>();
|
||||
var notStupidCells = new List<int2>();
|
||||
var availCells = new List<CPos>();
|
||||
var notStupidCells = new List<CPos>();
|
||||
|
||||
for (var i = -1; i < 2; i++)
|
||||
for (var j = -1; j < 2; j++)
|
||||
{
|
||||
var p = toCell + new int2(i, j);
|
||||
var p = toCell + new CVec(i, j);
|
||||
if (CanEnterCell(p))
|
||||
availCells.Add(p);
|
||||
else
|
||||
@@ -400,7 +394,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
}
|
||||
|
||||
var moveTo = availCells.Any() ? availCells.Random(self.World.SharedRandom) :
|
||||
notStupidCells.Any() ? notStupidCells.Random(self.World.SharedRandom) : (int2?)null;
|
||||
notStupidCells.Any() ? notStupidCells.Random(self.World.SharedRandom) : (CPos?)null;
|
||||
|
||||
if (moveTo.HasValue)
|
||||
{
|
||||
@@ -434,7 +428,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool CanTargetLocation(Actor self, int2 location, List<Actor> actorsAtLocation, bool forceAttack, bool forceQueued, ref string cursor)
|
||||
public bool CanTargetLocation(Actor self, CPos location, List<Actor> actorsAtLocation, bool forceAttack, bool forceQueued, ref string cursor)
|
||||
{
|
||||
IsQueued = forceQueued;
|
||||
cursor = "move";
|
||||
@@ -446,10 +440,10 @@ namespace OpenRA.Mods.RA.Move
|
||||
}
|
||||
}
|
||||
|
||||
public Activity ScriptedMove(int2 cell) { return new Move(cell); }
|
||||
public Activity MoveTo(int2 cell, int nearEnough) { return new Move(cell, nearEnough); }
|
||||
public Activity MoveTo(int2 cell, Actor ignoredActor) { return new Move(cell, ignoredActor); }
|
||||
public Activity ScriptedMove(CPos cell) { return new Move(cell); }
|
||||
public Activity MoveTo(CPos cell, int nearEnough) { return new Move(cell, nearEnough); }
|
||||
public Activity MoveTo(CPos cell, Actor ignoredActor) { return new Move(cell, ignoredActor); }
|
||||
public Activity MoveWithinRange(Target target, int range) { return new Move(target, range); }
|
||||
public Activity MoveTo(Func<List<int2>> pathFunc) { return new Move(pathFunc); }
|
||||
public Activity MoveTo(Func<List<CPos>> pathFunc) { return new Move(pathFunc); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,15 +20,15 @@ namespace OpenRA.Mods.RA.Move
|
||||
{
|
||||
class Move : Activity
|
||||
{
|
||||
int2? destination;
|
||||
CPos? destination;
|
||||
int nearEnough;
|
||||
public List<int2> path;
|
||||
Func<Actor, Mobile, List<int2>> getPath;
|
||||
public List<CPos> path;
|
||||
Func<Actor, Mobile, List<CPos>> getPath;
|
||||
public Actor ignoreBuilding;
|
||||
|
||||
// Scriptable move order
|
||||
// Ignores lane bias and nearby units
|
||||
public Move( int2 destination )
|
||||
public Move(CPos destination)
|
||||
{
|
||||
this.getPath = (self,mobile) =>
|
||||
self.World.WorldActor.Trait<PathFinder>().FindPath(
|
||||
@@ -38,14 +38,14 @@ namespace OpenRA.Mods.RA.Move
|
||||
this.nearEnough = 0;
|
||||
}
|
||||
|
||||
public Move( int2 destination, int nearEnough )
|
||||
public Move(CPos destination, int nearEnough)
|
||||
{
|
||||
this.getPath = (self,mobile) => self.World.WorldActor.Trait<PathFinder>().FindUnitPath( mobile.toCell, destination, self );
|
||||
this.destination = destination;
|
||||
this.nearEnough = nearEnough;
|
||||
}
|
||||
|
||||
public Move(int2 destination, Actor ignoreBuilding)
|
||||
public Move(CPos destination, Actor ignoreBuilding)
|
||||
{
|
||||
this.getPath = (self,mobile) =>
|
||||
self.World.WorldActor.Trait<PathFinder>().FindPath(
|
||||
@@ -60,13 +60,13 @@ namespace OpenRA.Mods.RA.Move
|
||||
public Move(Target target, int range)
|
||||
{
|
||||
this.getPath = (self,mobile) => self.World.WorldActor.Trait<PathFinder>().FindUnitPathToRange(
|
||||
mobile.toCell, Util.CellContaining(target.CenterLocation),
|
||||
mobile.toCell, target.CenterLocation.ToCPos(),
|
||||
range, self);
|
||||
this.destination = null;
|
||||
this.nearEnough = range;
|
||||
}
|
||||
|
||||
public Move(Func<List<int2>> getPath)
|
||||
public Move(Func<List<CPos>> getPath)
|
||||
{
|
||||
this.getPath = (_1,_2) => getPath();
|
||||
this.destination = null;
|
||||
@@ -83,7 +83,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
return hash;
|
||||
}
|
||||
|
||||
List<int2> EvalPath( Actor self, Mobile mobile )
|
||||
List<CPos> EvalPath(Actor self, Mobile mobile)
|
||||
{
|
||||
var path = getPath(self, mobile).TakeWhile(a => a != mobile.toCell).ToList();
|
||||
mobile.PathHash = HashList(path);
|
||||
@@ -131,7 +131,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
if( nextCell == null )
|
||||
return this;
|
||||
|
||||
int2 dir = nextCell.Value.First - mobile.fromCell;
|
||||
var dir = nextCell.Value.First - mobile.fromCell;
|
||||
var firstFacing = Util.GetFacing( dir, mobile.Facing );
|
||||
if( firstFacing != mobile.Facing )
|
||||
{
|
||||
@@ -167,7 +167,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
bool hasNudged;
|
||||
int waitTicksRemaining;
|
||||
|
||||
void NudgeBlocker(Actor self, int2 nextCell)
|
||||
void NudgeBlocker(Actor self, CPos nextCell)
|
||||
{
|
||||
var blocker = self.World.ActorMap.GetUnitsAt(nextCell).FirstOrDefault();
|
||||
if (blocker == null) return;
|
||||
@@ -180,7 +180,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
nudge.OnNudge(blocker, self, false);
|
||||
}
|
||||
|
||||
Pair<int2, SubCell>? PopPath( Actor self, Mobile mobile )
|
||||
Pair<CPos, SubCell>? PopPath(Actor self, Mobile mobile)
|
||||
{
|
||||
if( path.Count == 0 ) return null;
|
||||
var nextCell = path[ path.Count - 1 ];
|
||||
@@ -233,7 +233,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
|
||||
public override void Cancel( Actor self )
|
||||
{
|
||||
path = new List<int2>();
|
||||
path = new List<CPos>();
|
||||
base.Cancel(self);
|
||||
}
|
||||
|
||||
@@ -242,19 +242,19 @@ namespace OpenRA.Mods.RA.Move
|
||||
if( path != null )
|
||||
return Enumerable.Reverse(path).Select( c => Target.FromCell(c) );
|
||||
if( destination != null )
|
||||
return new Target[] { Target.FromPos(destination.Value) };
|
||||
return new Target[] { Target.FromCell(destination.Value) };
|
||||
return Target.NoTargets;
|
||||
}
|
||||
|
||||
abstract class MovePart : Activity
|
||||
{
|
||||
public readonly Move move;
|
||||
public readonly int2 from, to;
|
||||
public readonly PPos from, to;
|
||||
public readonly int fromFacing, toFacing;
|
||||
public int moveFraction;
|
||||
public readonly int moveFractionTotal;
|
||||
|
||||
public MovePart( Move move, int2 from, int2 to, int fromFacing, int toFacing, int startingFraction )
|
||||
public MovePart(Move move, PPos from, PPos to, int fromFacing, int toFacing, int startingFraction)
|
||||
{
|
||||
this.move = move;
|
||||
this.from = from;
|
||||
@@ -304,7 +304,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
|
||||
void UpdateCenterLocation( Actor self, Mobile mobile )
|
||||
{
|
||||
mobile.PxPosition = int2.Lerp( from, to, moveFraction, moveFractionTotal );
|
||||
mobile.PxPosition = PPos.Lerp(from, to, moveFraction, moveFractionTotal);
|
||||
|
||||
if( moveFraction >= moveFractionTotal )
|
||||
mobile.Facing = toFacing & 0xFF;
|
||||
@@ -322,10 +322,10 @@ namespace OpenRA.Mods.RA.Move
|
||||
|
||||
class MoveFirstHalf : MovePart
|
||||
{
|
||||
public MoveFirstHalf( Move move, int2 from, int2 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 ) { }
|
||||
|
||||
static bool IsTurn( Mobile mobile, int2 nextCell )
|
||||
static bool IsTurn( Mobile mobile, CPos nextCell )
|
||||
{
|
||||
return nextCell - mobile.toCell !=
|
||||
mobile.toCell - mobile.fromCell;
|
||||
@@ -373,7 +373,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
|
||||
class MoveSecondHalf : MovePart
|
||||
{
|
||||
public MoveSecondHalf( Move move, int2 from, int2 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 )
|
||||
{
|
||||
}
|
||||
|
||||
@@ -29,9 +29,9 @@ namespace OpenRA.Mods.RA.Move
|
||||
|
||||
class CachedPath
|
||||
{
|
||||
public int2 from;
|
||||
public int2 to;
|
||||
public List<int2> result;
|
||||
public CPos from;
|
||||
public CPos to;
|
||||
public List<CPos> result;
|
||||
public int tick;
|
||||
public Actor actor;
|
||||
}
|
||||
@@ -39,7 +39,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
List<CachedPath> CachedPaths = new List<CachedPath>();
|
||||
const int MaxPathAge = 50; /* x 40ms ticks */
|
||||
|
||||
public List<int2> FindUnitPath(int2 from, int2 target, Actor self)
|
||||
public List<CPos> FindUnitPath(CPos from, CPos target, Actor self)
|
||||
{
|
||||
using (new PerfSample("Pathfinder"))
|
||||
{
|
||||
@@ -48,25 +48,25 @@ namespace OpenRA.Mods.RA.Move
|
||||
{
|
||||
Log.Write("debug", "Actor {0} asked for a path from {1} tick(s) ago", self.ActorID, world.FrameNumber - cached.tick);
|
||||
cached.tick = world.FrameNumber;
|
||||
return new List<int2>(cached.result);
|
||||
return new List<CPos>(cached.result);
|
||||
}
|
||||
|
||||
var mi = self.Info.Traits.Get<MobileInfo>();
|
||||
|
||||
var pb = FindBidiPath(
|
||||
PathSearch.FromPoint(world, mi, self.Owner, target, from, true),
|
||||
PathSearch.FromPoint(world, mi, self.Owner, from, target, true)
|
||||
.InReverse());
|
||||
PathSearch.FromPoint(world, mi, self.Owner, from, target, true).InReverse()
|
||||
);
|
||||
|
||||
CheckSanePath2(pb, from, target);
|
||||
|
||||
CachedPaths.RemoveAll(p => world.FrameNumber - p.tick > MaxPathAge);
|
||||
CachedPaths.Add(new CachedPath { from = from, to = target, actor = self, result = pb, tick = world.FrameNumber });
|
||||
return new List<int2>(pb);
|
||||
return new List<CPos>(pb);
|
||||
}
|
||||
}
|
||||
|
||||
public List<int2> FindUnitPathToRange( int2 src, int2 target, int range, Actor self )
|
||||
public List<CPos> FindUnitPathToRange(CPos src, CPos target, int range, Actor self)
|
||||
{
|
||||
using( new PerfSample( "Pathfinder" ) )
|
||||
{
|
||||
@@ -76,18 +76,18 @@ namespace OpenRA.Mods.RA.Move
|
||||
|
||||
var path = FindBidiPath(
|
||||
PathSearch.FromPoints(world, mi, self.Owner, tilesInRange, src, true),
|
||||
PathSearch.FromPoint(world, mi, self.Owner, src, target, true)
|
||||
.InReverse());
|
||||
PathSearch.FromPoint(world, mi, self.Owner, src, target, true).InReverse()
|
||||
);
|
||||
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
public List<int2> FindPath( PathSearch search )
|
||||
public List<CPos> FindPath(PathSearch search)
|
||||
{
|
||||
using (new PerfSample("Pathfinder"))
|
||||
{
|
||||
using(search)
|
||||
using (search)
|
||||
while (!search.queue.Empty)
|
||||
{
|
||||
var p = search.Expand(world);
|
||||
@@ -96,14 +96,14 @@ namespace OpenRA.Mods.RA.Move
|
||||
}
|
||||
|
||||
// no path exists
|
||||
return new List<int2>();
|
||||
return new List<CPos>(0);
|
||||
}
|
||||
}
|
||||
|
||||
static List<int2> MakePath( CellInfo[ , ] cellInfo, int2 destination )
|
||||
static List<CPos> MakePath(CellInfo[,] cellInfo, CPos destination)
|
||||
{
|
||||
List<int2> ret = new List<int2>();
|
||||
int2 pathNode = destination;
|
||||
var ret = new List<CPos>();
|
||||
CPos pathNode = destination;
|
||||
|
||||
while( cellInfo[ pathNode.X, pathNode.Y ].Path != pathNode )
|
||||
{
|
||||
@@ -116,9 +116,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<int2> FindBidiPath( /* searches from both ends toward each other */
|
||||
public List<CPos> FindBidiPath( /* searches from both ends toward each other */
|
||||
PathSearch fromSrc,
|
||||
PathSearch fromDest)
|
||||
{
|
||||
@@ -141,16 +139,16 @@ namespace OpenRA.Mods.RA.Move
|
||||
return MakeBidiPath(fromSrc, fromDest, q);
|
||||
}
|
||||
|
||||
return new List<int2>();
|
||||
return new List<CPos>(0);
|
||||
}
|
||||
}
|
||||
|
||||
static List<int2> MakeBidiPath(PathSearch a, PathSearch b, int2 p)
|
||||
static List<CPos> MakeBidiPath(PathSearch a, PathSearch b, CPos p)
|
||||
{
|
||||
var ca = a.cellInfo;
|
||||
var cb = b.cellInfo;
|
||||
|
||||
var ret = new List<int2>();
|
||||
var ret = new List<CPos>();
|
||||
|
||||
var q = p;
|
||||
while (ca[q.X, q.Y].Path != q)
|
||||
@@ -174,7 +172,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
}
|
||||
|
||||
[Conditional( "SANITY_CHECKS" )]
|
||||
static void CheckSanePath( List<int2> path )
|
||||
static void CheckSanePath(List<CPos> path)
|
||||
{
|
||||
if( path.Count == 0 )
|
||||
return;
|
||||
@@ -189,7 +187,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
}
|
||||
|
||||
[Conditional("SANITY_CHECKS")]
|
||||
static void CheckSanePath2(List<int2> path, int2 src, int2 dest)
|
||||
static void CheckSanePath2(List<CPos> path, CPos src, CPos dest)
|
||||
{
|
||||
if (path.Count == 0)
|
||||
return;
|
||||
@@ -204,10 +202,10 @@ namespace OpenRA.Mods.RA.Move
|
||||
public struct CellInfo
|
||||
{
|
||||
public int MinCost;
|
||||
public int2 Path;
|
||||
public CPos Path;
|
||||
public bool Seen;
|
||||
|
||||
public CellInfo( int minCost, int2 path, bool seen )
|
||||
public CellInfo(int minCost, CPos path, bool seen)
|
||||
{
|
||||
MinCost = minCost;
|
||||
Path = path;
|
||||
@@ -218,9 +216,9 @@ namespace OpenRA.Mods.RA.Move
|
||||
public struct PathDistance : IComparable<PathDistance>
|
||||
{
|
||||
public int EstTotal;
|
||||
public int2 Location;
|
||||
public CPos Location;
|
||||
|
||||
public PathDistance(int estTotal, int2 location)
|
||||
public PathDistance(int estTotal, CPos location)
|
||||
{
|
||||
EstTotal = estTotal;
|
||||
Location = location;
|
||||
|
||||
@@ -20,8 +20,8 @@ namespace OpenRA.Mods.RA.Move
|
||||
World world;
|
||||
public CellInfo[ , ] cellInfo;
|
||||
public PriorityQueue<PathDistance> queue;
|
||||
public Func<int2, int> heuristic;
|
||||
Func<int2, bool> customBlock;
|
||||
public Func<CPos, int> heuristic;
|
||||
Func<CPos, bool> customBlock;
|
||||
public bool checkForBlocked;
|
||||
public Actor ignoreBuilding;
|
||||
public bool inReverse;
|
||||
@@ -44,7 +44,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
return this;
|
||||
}
|
||||
|
||||
public PathSearch WithCustomBlocker(Func<int2, bool> customBlock)
|
||||
public PathSearch WithCustomBlocker(Func<CPos, bool> customBlock)
|
||||
{
|
||||
this.customBlock = customBlock;
|
||||
return this;
|
||||
@@ -56,7 +56,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
return this;
|
||||
}
|
||||
|
||||
public PathSearch WithHeuristic(Func<int2, int> h)
|
||||
public PathSearch WithHeuristic(Func<CPos, int> h)
|
||||
{
|
||||
heuristic = h;
|
||||
return this;
|
||||
@@ -68,7 +68,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
return this;
|
||||
}
|
||||
|
||||
public PathSearch FromPoint(int2 from)
|
||||
public PathSearch FromPoint(CPos from)
|
||||
{
|
||||
AddInitialCell( from );
|
||||
return this;
|
||||
@@ -76,7 +76,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
|
||||
int LaneBias = 1;
|
||||
|
||||
public int2 Expand( World world )
|
||||
public CPos Expand(World world)
|
||||
{
|
||||
var p = queue.Pop();
|
||||
while (cellInfo[p.Location.X, p.Location.Y].Seen)
|
||||
@@ -92,9 +92,9 @@ namespace OpenRA.Mods.RA.Move
|
||||
if (thisCost == int.MaxValue)
|
||||
return p.Location;
|
||||
|
||||
foreach( int2 d in directions )
|
||||
foreach( CVec d in directions )
|
||||
{
|
||||
int2 newHere = p.Location + d;
|
||||
CPos newHere = p.Location + d;
|
||||
|
||||
if (!world.Map.IsInMap(newHere.X, newHere.Y)) continue;
|
||||
if( cellInfo[ newHere.X, newHere.Y ].Seen )
|
||||
@@ -141,19 +141,19 @@ namespace OpenRA.Mods.RA.Move
|
||||
return p.Location;
|
||||
}
|
||||
|
||||
static readonly int2[] directions =
|
||||
static readonly CVec[] directions =
|
||||
{
|
||||
new int2( -1, -1 ),
|
||||
new int2( -1, 0 ),
|
||||
new int2( -1, 1 ),
|
||||
new int2( 0, -1 ),
|
||||
new int2( 0, 1 ),
|
||||
new int2( 1, -1 ),
|
||||
new int2( 1, 0 ),
|
||||
new int2( 1, 1 ),
|
||||
new CVec( -1, -1 ),
|
||||
new CVec( -1, 0 ),
|
||||
new CVec( -1, 1 ),
|
||||
new CVec( 0, -1 ),
|
||||
new CVec( 0, 1 ),
|
||||
new CVec( 1, -1 ),
|
||||
new CVec( 1, 0 ),
|
||||
new CVec( 1, 1 ),
|
||||
};
|
||||
|
||||
public void AddInitialCell( int2 location )
|
||||
public void AddInitialCell(CPos location)
|
||||
{
|
||||
if (!world.Map.IsInMap(location.X, location.Y))
|
||||
return;
|
||||
@@ -169,7 +169,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
return search;
|
||||
}
|
||||
|
||||
public static PathSearch FromPoint( World world, MobileInfo mi, Player owner, int2 from, int2 target, bool checkForBlocked )
|
||||
public static PathSearch FromPoint(World world, MobileInfo mi, Player owner, CPos from, CPos target, bool checkForBlocked)
|
||||
{
|
||||
var search = new PathSearch(world, mi, owner) {
|
||||
heuristic = DefaultEstimator( target ),
|
||||
@@ -179,7 +179,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
return search;
|
||||
}
|
||||
|
||||
public static PathSearch FromPoints(World world, MobileInfo mi, Player owner, IEnumerable<int2> froms, int2 target, bool checkForBlocked)
|
||||
public static PathSearch FromPoints(World world, MobileInfo mi, Player owner, IEnumerable<CPos> froms, CPos target, bool checkForBlocked)
|
||||
{
|
||||
var search = new PathSearch(world, mi, owner)
|
||||
{
|
||||
@@ -229,16 +229,16 @@ namespace OpenRA.Mods.RA.Move
|
||||
|
||||
for( int x = 0 ; x < world.Map.MapSize.X ; x++ )
|
||||
for( int y = 0 ; y < world.Map.MapSize.Y ; y++ )
|
||||
result[ x, y ] = new CellInfo( int.MaxValue, new int2( x, y ), false );
|
||||
result[ x, y ] = new CellInfo( int.MaxValue, new CPos( x, y ), false );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Func<int2, int> DefaultEstimator( int2 destination )
|
||||
public static Func<CPos, int> DefaultEstimator(CPos destination)
|
||||
{
|
||||
return here =>
|
||||
{
|
||||
int2 d = ( here - destination ).Abs();
|
||||
CVec d = (here - destination).Abs();
|
||||
int diag = Math.Min( d.X, d.Y );
|
||||
int straight = Math.Abs( d.X - d.Y );
|
||||
return (3400 * diag / 24) + (100 * straight);
|
||||
|
||||
Reference in New Issue
Block a user