Kinda works. Occasionally crashes (OOM) with a loop in the path.

This commit is contained in:
Bob
2009-11-07 19:18:20 +13:00
parent bb64327568
commit 3ab8ae1afc
2 changed files with 28 additions and 12 deletions

View File

@@ -102,10 +102,12 @@ namespace OpenRa.Game
var otherQueue = new PriorityQueue<PathDistance>(); var otherQueue = new PriorityQueue<PathDistance>();
otherQueue.Add(new PathDistance(h2(from), from)); otherQueue.Add(new PathDistance(h2(from), from));
var ret = FindBidiPath(cellInfo, InitCellInfo(), queue, otherQueue, estimator, h2, umt, true); var otherCellInfo = InitCellInfo();
otherCellInfo[from.X, from.Y] = new CellInfo( 0, from, false );
var ret = FindBidiPath(cellInfo, otherCellInfo, queue, otherQueue, estimator, h2, umt, true);
//var ret = FindPath(cellInfo, queue, estimator, umt, true); //var ret = FindPath(cellInfo, queue, estimator, umt, true);
ret.Reverse(); //ret.Reverse();
return ret; return ret;
} }
} }
@@ -262,27 +264,41 @@ namespace OpenRa.Game
static List<int2> MakeBidiPath(CellInfo[,] ca, CellInfo[,] cb, int2 p) static List<int2> MakeBidiPath(CellInfo[,] ca, CellInfo[,] cb, int2 p)
{ {
var a = new List<int2>(); var a = new List<int2>();
var b = new List<int2>();
var q = p; var q = p;
while (ca[q.X, q.Y].Path != q) while (ca[q.X, q.Y].Path != q)
{ {
q = ca[q.X, q.Y].Path;
a.Add( q ); a.Add( q );
q = ca[ q.X, q.Y ].Path;
} }
a.Reverse();
q = p; q = p;
while (cb[q.X, q.Y].Path != q) while (cb[q.X, q.Y].Path != q)
{ {
q = cb[q.X, q.Y].Path; q = cb[q.X, q.Y].Path;
b.Add(q); a.Add(q);
} }
a.Add(p); CheckSanePath( a );
for (var v = b.Count - 1; v >= 0; v--) a.Add(b[v]);
return a; return a;
} }
[System.Diagnostics.Conditional( "SANITY_CHECKS" )]
static void CheckSanePath( List<int2> path )
{
if( path.Count == 0 )
return;
var prev = path[ 0 ];
for( int i = 0 ; i < path.Count ; i++ )
{
var d = path[ i ] - prev;
if( Math.Abs( d.X ) > 1 || Math.Abs( d.Y ) > 1 )
throw new InvalidOperationException( "(PathFinder) path sanity check failed" );
prev = path[ i ];
}
}
} }
struct CellInfo struct CellInfo

View File

@@ -121,7 +121,7 @@ namespace OpenRa.Game.Traits.Activities
if( newPath.Count == 0 ) if( newPath.Count == 0 )
return null; return null;
while( path[ path.Count - 1 ] != newPath[ 0 ] ) while( path.Count != 0 && path[ path.Count - 1 ] != newPath[ 0 ] )
path.RemoveAt( path.Count - 1 ); path.RemoveAt( path.Count - 1 );
for( int i = 1 ; i < newPath.Count ; i++ ) for( int i = 1 ; i < newPath.Count ; i++ )
path.Add( newPath[ i ] ); path.Add( newPath[ i ] );