diff --git a/OpenRa.Game/PathFinder.cs b/OpenRa.Game/PathFinder.cs index e03c6b93db..f582f70dcd 100644 --- a/OpenRa.Game/PathFinder.cs +++ b/OpenRa.Game/PathFinder.cs @@ -49,10 +49,13 @@ namespace OpenRa.Game PathSearch.FromPoint(target, from, umt, false), PathSearch.FromPoint(from, target, umt, false)); + CheckSanePath2(pb, from, target); return pb; } } + + public List FindUnitPathToRange( int2 src, int2 target, UnitMovementType umt, int range ) { using( new PerfSample( "find_unit_path_multiple_src" ) ) @@ -71,14 +74,12 @@ namespace OpenRa.Game if( IsBlocked( from, umt ) ) return new List(); - using( new PerfSample( "find_path_to_path" ) ) + using (new PerfSample("find_path_to_path")) return FindBidiPath( - PathSearch.FromPath( path, from, umt, true ), - PathSearch.FromPoint( from, path[ 0 ], umt, true ) ); + PathSearch.FromPath(path, from, umt, true), + PathSearch.FromPoint(from, path[0], umt, true)); } - - public List FindPath( PathSearch search ) { int nodesExpanded = 0; @@ -153,6 +154,7 @@ namespace OpenRa.Game ret.Add( q ); q = ca[ q.X, q.Y ].Path; } + ret.Add(q); ret.Reverse(); @@ -183,6 +185,18 @@ namespace OpenRa.Game prev = path[ i ]; } } + + [System.Diagnostics.Conditional("SANITY_CHECKS")] + static void CheckSanePath2(List 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 diff --git a/OpenRa.Game/Traits/Activities/Harvest.cs b/OpenRa.Game/Traits/Activities/Harvest.cs index 5ba012d293..f638cf407f 100644 --- a/OpenRa.Game/Traits/Activities/Harvest.cs +++ b/OpenRa.Game/Traits/Activities/Harvest.cs @@ -72,26 +72,19 @@ namespace OpenRa.Game.Traits.Activities void PlanMoreHarvesting(Actor self, Mobile mobile) { - /* find a nearby patch */ - /* todo: add the queries we need to support this! */ - - var search = new PathSearch - { - heuristic = loc => ( Game.map.ContainsResource( loc ) ? 0 : 1 ), - umt = UnitMovementType.Wheel, - checkForBlocked = true - }; - search.AddInitialCell( self.Location ); - - 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 Move( + () => + { + var search = new PathSearch + { + heuristic = loc => (Game.map.ContainsResource(loc) ? 0 : 1), + umt = UnitMovementType.Wheel, + checkForBlocked = true + }; + search.AddInitialCell(self.Location); + return Game.PathFinder.FindPath(search); + })); + mobile.QueueActivity(new Harvest()); mobile.InternalSetActivity( NextActivity ); } diff --git a/OpenRa.Game/Traits/Activities/Move.cs b/OpenRa.Game/Traits/Activities/Move.cs index d724d2f526..8cca93d5cb 100755 --- a/OpenRa.Game/Traits/Activities/Move.cs +++ b/OpenRa.Game/Traits/Activities/Move.cs @@ -34,10 +34,11 @@ namespace OpenRa.Game.Traits.Activities this.nearEnough = range; } - public Move( List path ) + public Move(Func> getPath) { - this.path = path; - this.destination = path[ 0 ]; + this.getPath = (_, _2) => getPath(); + this.destination = null; + this.nearEnough = 0; } static bool CanEnterCell( int2 c, Actor self ) @@ -114,17 +115,23 @@ namespace OpenRa.Game.Traits.Activities } Game.UnitInfluence.Remove( mobile ); - var newPath = Game.PathFinder.FindPathToPath( self.Location, path, mobile.GetMovementType() ) - .TakeWhile( a => a != self.Location ) - .ToList(); + var newPath = getPath(self, mobile).TakeWhile(a => a != self.Location).ToList(); + //var newPath = Game.PathFinder.FindPathToPath( self.Location, path, mobile.GetMovementType() ) + // .TakeWhile( a => a != self.Location ) + // .ToList(); Game.UnitInfluence.Add( mobile ); - if( newPath.Count == 0 ) + if (newPath.Count == 0) return null; + else + { + path = newPath; + return null; + } - while( path.Count != 0 && path[ path.Count - 1 ] != newPath[ 0 ] ) - path.RemoveAt( path.Count - 1 ); - for( int i = 1 ; i < newPath.Count ; i++ ) - path.Add( newPath[ i ] ); + //while( path.Count != 0 && path[ path.Count - 1 ] != newPath[ 0 ] ) + // path.RemoveAt( path.Count - 1 ); + //for( int i = 1 ; i < newPath.Count ; i++ ) + // path.Add( newPath[ i ] ); if( path.Count == 0 ) return null;