repath just by pathing again; more sanity checking of simple paths

This commit is contained in:
Chris Forbes
2009-11-08 20:54:24 +13:00
parent f22170800a
commit 55adc19aa9
3 changed files with 50 additions and 36 deletions

View File

@@ -49,10 +49,13 @@ namespace OpenRa.Game
PathSearch.FromPoint(target, from, umt, false), PathSearch.FromPoint(target, from, umt, false),
PathSearch.FromPoint(from, target, umt, false)); PathSearch.FromPoint(from, target, umt, false));
CheckSanePath2(pb, from, target);
return pb; return pb;
} }
} }
public List<int2> FindUnitPathToRange( int2 src, int2 target, UnitMovementType umt, int range ) public List<int2> FindUnitPathToRange( int2 src, int2 target, UnitMovementType umt, int range )
{ {
using( new PerfSample( "find_unit_path_multiple_src" ) ) using( new PerfSample( "find_unit_path_multiple_src" ) )
@@ -77,8 +80,6 @@ namespace OpenRa.Game
PathSearch.FromPoint(from, path[0], umt, true)); PathSearch.FromPoint(from, path[0], umt, true));
} }
public List<int2> FindPath( PathSearch search ) public List<int2> FindPath( PathSearch search )
{ {
int nodesExpanded = 0; int nodesExpanded = 0;
@@ -153,6 +154,7 @@ namespace OpenRa.Game
ret.Add( q ); ret.Add( q );
q = ca[ q.X, q.Y ].Path; q = ca[ q.X, q.Y ].Path;
} }
ret.Add(q);
ret.Reverse(); ret.Reverse();
@@ -183,6 +185,18 @@ namespace OpenRa.Game
prev = path[ i ]; prev = path[ i ];
} }
} }
[System.Diagnostics.Conditional("SANITY_CHECKS")]
static void CheckSanePath2(List<int2> path, int2 src, int2 dest)
{
if (path.Count == 0)
return;
if (path[0] != dest)
throw new InvalidOperationException("(PathFinder) sanity check failed: doesn't go to dest");
if (path[path.Count - 1] != src)
throw new InvalidOperationException("(PathFinder) sanity check failed: doesn't come from src");
}
} }
struct CellInfo struct CellInfo

View File

@@ -72,9 +72,9 @@ namespace OpenRa.Game.Traits.Activities
void PlanMoreHarvesting(Actor self, Mobile mobile) void PlanMoreHarvesting(Actor self, Mobile mobile)
{ {
/* find a nearby patch */ mobile.QueueActivity(new Move(
/* todo: add the queries we need to support this! */ () =>
{
var search = new PathSearch var search = new PathSearch
{ {
heuristic = loc => (Game.map.ContainsResource(loc) ? 0 : 1), heuristic = loc => (Game.map.ContainsResource(loc) ? 0 : 1),
@@ -82,16 +82,9 @@ namespace OpenRa.Game.Traits.Activities
checkForBlocked = true checkForBlocked = true
}; };
search.AddInitialCell(self.Location); search.AddInitialCell(self.Location);
return Game.PathFinder.FindPath(search);
var path = Game.PathFinder.FindPath( search ) }));
.TakeWhile( a => a != self.Location )
.ToList();
if( path.Count != 0 )
{
mobile.QueueActivity( new Move( path ) );
mobile.QueueActivity(new Harvest()); mobile.QueueActivity(new Harvest());
}
mobile.InternalSetActivity( NextActivity ); mobile.InternalSetActivity( NextActivity );
} }

View File

@@ -34,10 +34,11 @@ namespace OpenRa.Game.Traits.Activities
this.nearEnough = range; this.nearEnough = range;
} }
public Move( List<int2> path ) public Move(Func<List<int2>> getPath)
{ {
this.path = path; this.getPath = (_, _2) => getPath();
this.destination = path[ 0 ]; this.destination = null;
this.nearEnough = 0;
} }
static bool CanEnterCell( int2 c, Actor self ) static bool CanEnterCell( int2 c, Actor self )
@@ -114,17 +115,23 @@ namespace OpenRa.Game.Traits.Activities
} }
Game.UnitInfluence.Remove( mobile ); Game.UnitInfluence.Remove( mobile );
var newPath = Game.PathFinder.FindPathToPath( self.Location, path, mobile.GetMovementType() ) var newPath = getPath(self, mobile).TakeWhile(a => a != self.Location).ToList();
.TakeWhile( a => a != self.Location ) //var newPath = Game.PathFinder.FindPathToPath( self.Location, path, mobile.GetMovementType() )
.ToList(); // .TakeWhile( a => a != self.Location )
// .ToList();
Game.UnitInfluence.Add( mobile ); Game.UnitInfluence.Add( mobile );
if (newPath.Count == 0) if (newPath.Count == 0)
return null; return null;
else
{
path = newPath;
return null;
}
while( path.Count != 0 && 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 ] );
if( path.Count == 0 ) if( path.Count == 0 )
return null; return null;