Repath around blocking units or structures.

This commit is contained in:
Bob
2009-10-28 17:59:09 +13:00
parent 58f9347cb7
commit 222b72a679
4 changed files with 245 additions and 181 deletions

View File

@@ -42,6 +42,32 @@ namespace OpenRa.Game
return path;
}
public List<int2> FindPathToPath( int2 from, List<int2> path, UnitMovementType umt )
{
var offset = map.Offset;
var cellInfo = InitCellInfo();
var queue = new PriorityQueue<PathDistance>();
var estimator = DefaultEstimator( from );
var cost = 0.0;
var prev = path[ 0 ] + offset;
for( int i = 0 ; i < path.Count ; i++ )
{
var sl = path[ i ] + offset;
if( Game.BuildingInfluence.GetBuildingAt( path[ i ] ) == null & Game.UnitInfluence.GetUnitAt( path[ i ] ) == null )
{
queue.Add( new PathDistance( estimator( sl - offset ), sl ) );
cellInfo[ sl.X, sl.Y ] = new CellInfo( cost, prev, false );
}
var d = sl - prev;
cost += ( ( d.X * d.Y != 0 ) ? 1.414213563 : 1.0 ) * passableCost[ (int)umt ][ sl.X, sl.Y ];
prev = sl;
}
var ret = FindPath( cellInfo, queue, estimator, umt, true );
ret.Reverse();
return ret;
}
List<int2> FindUnitPath( int2 unitLocation, Func<int2,double> estimator, UnitMovementType umt )
{
var startLocation = unitLocation + map.Offset;
@@ -50,13 +76,8 @@ namespace OpenRa.Game
List<int2> FindUnitPath(IEnumerable<int2> startLocations, Func<int2, double> estimator, UnitMovementType umt)
{
var cellInfo = new CellInfo[128, 128];
var offset = map.Offset;
for (int x = 0; x < 128; x++)
for (int y = 0; y < 128; y++)
cellInfo[x, y] = new CellInfo(double.PositiveInfinity, new int2(x, y), false);
var cellInfo = InitCellInfo();
var queue = new PriorityQueue<PathDistance>();
foreach (var sl in startLocations)
@@ -65,6 +86,13 @@ namespace OpenRa.Game
cellInfo[sl.X, sl.Y].MinCost = 0;
}
return FindPath( cellInfo, queue, estimator, umt, false );
}
List<int2> FindPath( CellInfo[ , ] cellInfo, PriorityQueue<PathDistance> queue, Func<int2, double> estimator, UnitMovementType umt, bool checkForBlock )
{
var offset = map.Offset;
while( !queue.Empty )
{
PathDistance p = queue.Pop();
@@ -84,6 +112,8 @@ namespace OpenRa.Game
continue;
if (Game.BuildingInfluence.GetBuildingAt(newHere - offset) != null)
continue;
if( checkForBlock && Game.UnitInfluence.GetUnitAt( newHere - offset ) != null )
continue;
double cellCost = ( ( d.X * d.Y != 0 ) ? 1.414213563 : 1.0 ) * passableCost[(int)umt][ newHere.X, newHere.Y ];
double newCost = cellInfo[ here.X, here.Y ].MinCost + cellCost;
@@ -102,6 +132,15 @@ namespace OpenRa.Game
return new List<int2>();
}
static CellInfo[ , ] InitCellInfo()
{
var cellInfo = new CellInfo[ 128, 128 ];
for( int x = 0 ; x < 128 ; x++ )
for( int y = 0 ; y < 128 ; y++ )
cellInfo[ x, y ] = new CellInfo( double.PositiveInfinity, new int2( x, y ), false );
return cellInfo;
}
List<int2> MakePath( CellInfo[ , ] cellInfo, int2 destination, int2 offset )
{
List<int2> ret = new List<int2>();