more refs to Game.world (PathSearch)

This commit is contained in:
Bob
2010-01-21 13:51:09 +13:00
parent 232ed72bbf
commit 02ce7990c0
5 changed files with 24 additions and 24 deletions

View File

@@ -31,8 +31,8 @@ namespace OpenRa
using (new PerfSample("find_unit_path")) using (new PerfSample("find_unit_path"))
{ {
var pb = FindBidiPath( var pb = FindBidiPath(
PathSearch.FromPoint(target, from, umt, false).WithCustomBlocker(AvoidUnitsNear(from, 4)), PathSearch.FromPoint(world, target, from, umt, false).WithCustomBlocker(AvoidUnitsNear(from, 4)),
PathSearch.FromPoint(from, target, umt, false).WithCustomBlocker(AvoidUnitsNear(from, 4))); PathSearch.FromPoint(world, from, target, umt, false).WithCustomBlocker(AvoidUnitsNear(from, 4)));
CheckSanePath2(pb, from, target); CheckSanePath2(pb, from, target);
return pb; return pb;
@@ -46,7 +46,7 @@ namespace OpenRa
var tilesInRange = world.FindTilesInCircle(target, range) var tilesInRange = world.FindTilesInCircle(target, range)
.Where( t => world.IsCellBuildable( t, umt ) ); .Where( t => world.IsCellBuildable( t, umt ) );
var path = FindPath( PathSearch.FromPoints( tilesInRange, src, umt, false ).WithCustomBlocker(AvoidUnitsNear(src, 4))); var path = FindPath( PathSearch.FromPoints( world, tilesInRange, src, umt, false ).WithCustomBlocker(AvoidUnitsNear(src, 4)));
path.Reverse(); path.Reverse();
return path; return path;
} }
@@ -66,7 +66,7 @@ namespace OpenRa
{ {
while (!search.queue.Empty) while (!search.queue.Empty)
{ {
var p = search.Expand( passableCost ); var p = search.Expand( world, passableCost );
PerfHistory.Increment("nodes_expanded", .01); PerfHistory.Increment("nodes_expanded", .01);
if (search.heuristic(p) == 0) if (search.heuristic(p) == 0)
@@ -103,13 +103,13 @@ namespace OpenRa
while (!fromSrc.queue.Empty && !fromDest.queue.Empty) while (!fromSrc.queue.Empty && !fromDest.queue.Empty)
{ {
/* make some progress on the first search */ /* make some progress on the first search */
var p = fromSrc.Expand( passableCost ); var p = fromSrc.Expand( world, passableCost );
if (fromDest.cellInfo[p.X, p.Y].Seen && fromDest.cellInfo[p.X, p.Y].MinCost < float.PositiveInfinity) if (fromDest.cellInfo[p.X, p.Y].Seen && fromDest.cellInfo[p.X, p.Y].MinCost < float.PositiveInfinity)
return MakeBidiPath(fromSrc, fromDest, p); return MakeBidiPath(fromSrc, fromDest, p);
/* make some progress on the second search */ /* make some progress on the second search */
var q = fromDest.Expand( passableCost ); var q = fromDest.Expand( world, passableCost );
if (fromSrc.cellInfo[q.X, q.Y].Seen && fromSrc.cellInfo[q.X, q.Y].MinCost < float.PositiveInfinity) if (fromSrc.cellInfo[q.X, q.Y].Seen && fromSrc.cellInfo[q.X, q.Y].MinCost < float.PositiveInfinity)
return MakeBidiPath(fromSrc, fromDest, q); return MakeBidiPath(fromSrc, fromDest, q);

View File

@@ -34,12 +34,12 @@ namespace OpenRa
return this; return this;
} }
public int2 Expand( float[][ , ] passableCost ) public int2 Expand( World world, float[][ , ] passableCost )
{ {
var p = queue.Pop(); var p = queue.Pop();
cellInfo[ p.Location.X, p.Location.Y ].Seen = true; cellInfo[ p.Location.X, p.Location.Y ].Seen = true;
var custom2 = Game.world.customTerrain[p.Location.X, p.Location.Y]; var custom2 = world.customTerrain[p.Location.X, p.Location.Y];
var thisCost = (custom2 != null) var thisCost = (custom2 != null)
? custom2.GetCost(p.Location, umt) ? custom2.GetCost(p.Location, umt)
: passableCost[(int)umt][p.Location.X, p.Location.Y]; : passableCost[(int)umt][p.Location.X, p.Location.Y];
@@ -51,24 +51,24 @@ namespace OpenRa
{ {
int2 newHere = p.Location + d; int2 newHere = p.Location + d;
if (!Game.world.Map.IsInMap(newHere.X, newHere.Y)) continue; if (!world.Map.IsInMap(newHere.X, newHere.Y)) continue;
if( cellInfo[ newHere.X, newHere.Y ].Seen ) if( cellInfo[ newHere.X, newHere.Y ].Seen )
continue; continue;
var custom = Game.world.customTerrain[newHere.X, newHere.Y]; var custom = world.customTerrain[newHere.X, newHere.Y];
var costHere = (custom != null) ? custom.GetCost(newHere, umt) : passableCost[(int)umt][newHere.X, newHere.Y]; var costHere = (custom != null) ? custom.GetCost(newHere, umt) : passableCost[(int)umt][newHere.X, newHere.Y];
if (costHere == float.PositiveInfinity) if (costHere == float.PositiveInfinity)
continue; continue;
if (!Game.world.BuildingInfluence.CanMoveHere(newHere) && if (!world.BuildingInfluence.CanMoveHere(newHere) &&
Game.world.BuildingInfluence.GetBuildingAt(newHere) != ignoreBuilding) world.BuildingInfluence.GetBuildingAt(newHere) != ignoreBuilding)
continue; continue;
if (Game.world.Map.IsOverlaySolid(newHere)) if (world.Map.IsOverlaySolid(newHere))
continue; continue;
// Replicate real-ra behavior of not being able to enter a cell if there is a mixture of crushable and uncrushable units // Replicate real-ra behavior of not being able to enter a cell if there is a mixture of crushable and uncrushable units
if (checkForBlocked && (Game.world.UnitInfluence.GetUnitsAt(newHere).Any(a => !Game.world.IsActorPathableToCrush(a, umt)))) if (checkForBlocked && (world.UnitInfluence.GetUnitsAt(newHere).Any(a => !world.IsActorPathableToCrush(a, umt))))
continue; continue;
if (customBlock != null && customBlock(newHere)) if (customBlock != null && customBlock(newHere))
@@ -93,27 +93,27 @@ namespace OpenRa
return p.Location; return p.Location;
} }
public void AddInitialCell( int2 location ) public void AddInitialCell( World world, int2 location )
{ {
if (!Game.world.Map.IsInMap(location.X, location.Y)) if (!world.Map.IsInMap(location.X, location.Y))
return; return;
cellInfo[ location.X, location.Y ] = new CellInfo( 0, location, false ); cellInfo[ location.X, location.Y ] = new CellInfo( 0, location, false );
queue.Add( new PathDistance( heuristic( location ), location ) ); queue.Add( new PathDistance( heuristic( location ), location ) );
} }
public static PathSearch FromPoint( int2 from, int2 target, UnitMovementType umt, bool checkForBlocked ) public static PathSearch FromPoint( World world, int2 from, int2 target, UnitMovementType umt, bool checkForBlocked )
{ {
var search = new PathSearch { var search = new PathSearch {
heuristic = DefaultEstimator( target ), heuristic = DefaultEstimator( target ),
umt = umt, umt = umt,
checkForBlocked = checkForBlocked }; checkForBlocked = checkForBlocked };
search.AddInitialCell( from ); search.AddInitialCell( world, from );
return search; return search;
} }
public static PathSearch FromPoints(IEnumerable<int2> froms, int2 target, UnitMovementType umt, bool checkForBlocked) public static PathSearch FromPoints(World world, IEnumerable<int2> froms, int2 target, UnitMovementType umt, bool checkForBlocked)
{ {
var search = new PathSearch var search = new PathSearch
{ {
@@ -123,7 +123,7 @@ namespace OpenRa
}; };
foreach (var sl in froms) foreach (var sl in froms)
search.AddInitialCell(sl); search.AddInitialCell(world, sl);
return search; return search;
} }

View File

@@ -45,10 +45,10 @@ namespace OpenRa.Traits.Activities
var refineries = self.World.Actors.Where( x => x.traits.Contains<AcceptsOre>() var refineries = self.World.Actors.Where( x => x.traits.Contains<AcceptsOre>()
&& x.Owner == self.Owner ).ToList(); && x.Owner == self.Owner ).ToList();
if( refinery != null ) if( refinery != null )
search.AddInitialCell( refinery.Location + refineryDeliverOffset ); search.AddInitialCell( self.World, refinery.Location + refineryDeliverOffset );
else else
foreach( var r in refineries ) foreach( var r in refineries )
search.AddInitialCell( r.Location + refineryDeliverOffset ); search.AddInitialCell( self.World, r.Location + refineryDeliverOffset );
var path = self.World.PathFinder.FindPath( search ); var path = self.World.PathFinder.FindPath( search );
path.Reverse(); path.Reverse();

View File

@@ -62,7 +62,7 @@ namespace OpenRa.Traits.Activities
umt = UnitMovementType.Wheel, umt = UnitMovementType.Wheel,
checkForBlocked = true checkForBlocked = true
}; };
search.AddInitialCell(self.Location); search.AddInitialCell(self.World, self.Location);
return self.World.PathFinder.FindPath(search); return self.World.PathFinder.FindPath(search);
})); }));
self.QueueActivity(new Harvest()); self.QueueActivity(new Harvest());

View File

@@ -31,7 +31,7 @@ namespace OpenRa.Traits.Activities
{ {
this.getPath = (self, mobile) => this.getPath = (self, mobile) =>
self.World.PathFinder.FindPath( self.World.PathFinder.FindPath(
PathSearch.FromPoint( self.Location, destination, mobile.GetMovementType(), false ) PathSearch.FromPoint( self.World, self.Location, destination, mobile.GetMovementType(), false )
.WithCustomBlocker( self.World.PathFinder.AvoidUnitsNear( self.Location, 4 )).WithIgnoredBuilding( ignoreBuilding )); .WithCustomBlocker( self.World.PathFinder.AvoidUnitsNear( self.Location, 4 )).WithIgnoredBuilding( ignoreBuilding ));
this.destination = destination; this.destination = destination;