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

@@ -22,7 +22,7 @@ namespace OpenRa.Game
static TreeCache treeCache; static TreeCache treeCache;
public static TerrainRenderer terrain; public static TerrainRenderer terrain;
public static Viewport viewport; public static Viewport viewport;
public static PathFinder pathFinder; public static PathFinder PathFinder;
public static Network network; public static Network network;
public static WorldRenderer worldRenderer; public static WorldRenderer worldRenderer;
public static Controller controller; public static Controller controller;
@@ -65,7 +65,7 @@ namespace OpenRa.Game
LoadMapBuildings(mapFile); LoadMapBuildings(mapFile);
LoadMapUnits(mapFile); LoadMapUnits(mapFile);
pathFinder = new PathFinder(map, terrain.tileSet); PathFinder = new PathFinder(map, terrain.tileSet);
network = new Network(); network = new Network();

View File

@@ -42,6 +42,32 @@ namespace OpenRa.Game
return path; 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 ) List<int2> FindUnitPath( int2 unitLocation, Func<int2,double> estimator, UnitMovementType umt )
{ {
var startLocation = unitLocation + map.Offset; var startLocation = unitLocation + map.Offset;
@@ -50,13 +76,8 @@ namespace OpenRa.Game
List<int2> FindUnitPath(IEnumerable<int2> startLocations, Func<int2, double> estimator, UnitMovementType umt) List<int2> FindUnitPath(IEnumerable<int2> startLocations, Func<int2, double> estimator, UnitMovementType umt)
{ {
var cellInfo = new CellInfo[128, 128];
var offset = map.Offset; var offset = map.Offset;
var cellInfo = InitCellInfo();
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 queue = new PriorityQueue<PathDistance>(); var queue = new PriorityQueue<PathDistance>();
foreach (var sl in startLocations) foreach (var sl in startLocations)
@@ -65,6 +86,13 @@ namespace OpenRa.Game
cellInfo[sl.X, sl.Y].MinCost = 0; 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 ) while( !queue.Empty )
{ {
PathDistance p = queue.Pop(); PathDistance p = queue.Pop();
@@ -84,6 +112,8 @@ namespace OpenRa.Game
continue; continue;
if (Game.BuildingInfluence.GetBuildingAt(newHere - offset) != null) if (Game.BuildingInfluence.GetBuildingAt(newHere - offset) != null)
continue; 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 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; double newCost = cellInfo[ here.X, here.Y ].MinCost + cellCost;
@@ -102,6 +132,15 @@ namespace OpenRa.Game
return new List<int2>(); 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> MakePath( CellInfo[ , ] cellInfo, int2 destination, int2 offset )
{ {
List<int2> ret = new List<int2>(); List<int2> ret = new List<int2>();

View File

@@ -128,7 +128,7 @@ namespace OpenRa.Game.Traits
public MoveTo( int2 destination ) public MoveTo( int2 destination )
{ {
this.getPath = (self, mobile) => Game.pathFinder.FindUnitPath( this.getPath = (self, mobile) => Game.PathFinder.FindUnitPath(
self.Location, destination, self.Location, destination,
mobile.GetMovementType()); mobile.GetMovementType());
this.destination = destination; this.destination = destination;
@@ -136,7 +136,7 @@ namespace OpenRa.Game.Traits
public MoveTo(Actor target, int range) public MoveTo(Actor target, int range)
{ {
this.getPath = (self, mobile) => Game.pathFinder.FindUnitPathToRange( this.getPath = (self, mobile) => Game.PathFinder.FindUnitPathToRange(
self.Location, target.Location, self.Location, target.Location,
mobile.GetMovementType(), range); mobile.GetMovementType(), range);
this.destination = null; this.destination = null;
@@ -145,7 +145,8 @@ namespace OpenRa.Game.Traits
static bool CanEnterCell(int2 c, Actor self) static bool CanEnterCell(int2 c, Actor self)
{ {
var u = Game.UnitInfluence.GetUnitAt(c); var u = Game.UnitInfluence.GetUnitAt(c);
return u == null || u == self; var b = Game.BuildingInfluence.GetBuildingAt(c);
return (u == null || u == self) && b == null;
} }
public void Tick( Actor self, Mobile mobile ) public void Tick( Actor self, Mobile mobile )
@@ -169,21 +170,23 @@ namespace OpenRa.Game.Traits
destination = mobile.toCell; destination = mobile.toCell;
return; return;
} }
else
destination = path[0]; destination = path[0];
var nextCell = path[ path.Count - 1 ]; var nextCell = PopPath( self, mobile );
int2 dir = nextCell - mobile.fromCell; if( nextCell == null )
return;
int2 dir = nextCell.Value - mobile.fromCell;
var firstFacing = Util.GetFacing( dir, mobile.facing ); var firstFacing = Util.GetFacing( dir, mobile.facing );
if( firstFacing != mobile.facing ) if( firstFacing != mobile.facing )
{
mobile.currentActivity = new Turn( firstFacing ) { NextActivity = this }; mobile.currentActivity = new Turn( firstFacing ) { NextActivity = this };
path.Add( nextCell.Value );
}
else else
{ {
if (!CanEnterCell(nextCell, self)) return; /* todo: repath, sometimes */ mobile.toCell = nextCell.Value;
mobile.toCell = nextCell;
path.RemoveAt( path.Count - 1 );
move = new MoveFirstHalf( move = new MoveFirstHalf(
CenterOfCell( mobile.fromCell ), CenterOfCell( mobile.fromCell ),
BetweenCells( mobile.fromCell, mobile.toCell ), BetweenCells( mobile.fromCell, mobile.toCell ),
@@ -196,6 +199,25 @@ namespace OpenRa.Game.Traits
mobile.currentActivity.Tick( self, mobile ); mobile.currentActivity.Tick( self, mobile );
} }
int2? PopPath( Actor self, Mobile mobile )
{
if( path.Count == 0 ) return null;
var nextCell = path[ path.Count - 1 ];
if( !CanEnterCell( nextCell, self ) )
{
Game.UnitInfluence.Remove( mobile );
path = Game.PathFinder.FindPathToPath( self.Location, path, mobile.GetMovementType() )
.TakeWhile( a => a != self.Location )
.ToList();
Game.UnitInfluence.Add( mobile );
if( path.Count == 0 )
return null;
nextCell = path[ path.Count - 1 ];
}
path.RemoveAt( path.Count - 1 );
return nextCell;
}
static float2 CenterOfCell( int2 loc ) static float2 CenterOfCell( int2 loc )
{ {
return new float2( 12, 12 ) + Game.CellSize * (float2)loc; return new float2( 12, 12 ) + Game.CellSize * (float2)loc;
@@ -261,27 +283,24 @@ namespace OpenRa.Game.Traits
protected override MovePart OnComplete( Actor self, Mobile mobile, MoveTo parent ) protected override MovePart OnComplete( Actor self, Mobile mobile, MoveTo parent )
{ {
if( parent.path.Count > 0 ) var nextCell = parent.PopPath( self, mobile );
if( nextCell != null )
{ {
var nextCell = parent.path[ parent.path.Count - 1 ];
if( ( nextCell - mobile.toCell ) != ( mobile.toCell - mobile.fromCell ) ) if( ( nextCell - mobile.toCell ) != ( mobile.toCell - mobile.fromCell ) )
{ {
if( CanEnterCell( nextCell, self ) )
{
parent.path.RemoveAt( parent.path.Count - 1 );
var ret = new MoveFirstHalf( var ret = new MoveFirstHalf(
BetweenCells( mobile.fromCell, mobile.toCell ), BetweenCells( mobile.fromCell, mobile.toCell ),
BetweenCells( mobile.toCell, nextCell ), BetweenCells( mobile.toCell, nextCell.Value ),
mobile.facing, mobile.facing,
Util.GetNearestFacing( mobile.facing, Util.GetFacing( nextCell - mobile.toCell, mobile.facing ) ), Util.GetNearestFacing( mobile.facing, Util.GetFacing( nextCell.Value - mobile.toCell, mobile.facing ) ),
moveFraction - moveFractionTotal ); moveFraction - moveFractionTotal );
mobile.fromCell = mobile.toCell; mobile.fromCell = mobile.toCell;
mobile.toCell = nextCell; mobile.toCell = nextCell.Value;
Game.UnitInfluence.Update( mobile ); Game.UnitInfluence.Update( mobile );
return ret; return ret;
} }
} else
parent.path.Add( nextCell.Value );
} }
var ret2 = new MoveSecondHalf( var ret2 = new MoveSecondHalf(
BetweenCells( mobile.fromCell, mobile.toCell ), BetweenCells( mobile.fromCell, mobile.toCell ),

View File

@@ -25,7 +25,13 @@ namespace OpenRa.Game
Update(u); Update(u);
} }
public Actor GetUnitAt(int2 a) { return influence[a.X, a.Y]; } public Actor GetUnitAt( int2 a )
{
var actor = influence[ a.X, a.Y ];
if( actor != null && !actor.traits.Get<Mobile>().OccupiedCells().Contains( a ) )
throw new InvalidOperationException( "UIM: Unit is not in influenced square" );
return actor;
}
public void Add(Mobile a) public void Add(Mobile a)
{ {