git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1315 993157c7-ee19-0410-b2c4-bb4e9862e678

This commit is contained in:
(no author)
2007-07-21 03:00:34 +00:00
parent 434ec200c9
commit c4267e2241
9 changed files with 54 additions and 41 deletions

View File

@@ -30,9 +30,11 @@
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="int2.cs" />
<Compile Include="PriorityQueue.cs" /> <Compile Include="PriorityQueue.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tuple.cs" /> <Compile Include="Tuple.cs" />

View File

@@ -3,9 +3,9 @@ using System.Collections.Generic;
using System.Text; using System.Text;
using System.Drawing; using System.Drawing;
namespace OpenRa.Game namespace OpenRa
{ {
struct int2 public struct int2
{ {
public int X,Y; public int X,Y;
@@ -18,8 +18,6 @@ namespace OpenRa.Game
public static int2 operator *(int a, int2 b) { return new int2(a * b.X, a * b.Y); } public static int2 operator *(int a, int2 b) { return new int2(a * b.X, a * b.Y); }
public static int2 operator *(int2 b, int a) { return new int2(a * b.X, a * b.Y); } public static int2 operator *(int2 b, int a) { return new int2(a * b.X, a * b.Y); }
public float2 ToFloat2() { return new float2(X, Y); }
public static bool operator ==(int2 me, int2 other) { return (me.X == other.X && me.Y == other.Y); } public static bool operator ==(int2 me, int2 other) { return (me.X == other.X && me.Y == other.Y); }
public static bool operator !=(int2 me, int2 other) { return !(me == other); } public static bool operator !=(int2 me, int2 other) { return !(me == other); }

View File

@@ -30,7 +30,7 @@ namespace OpenRa.Game
public override float2 RenderLocation public override float2 RenderLocation
{ {
get { return 24.0f * location.ToFloat2(); } get { return 24.0f * (float2)location; }
} }
} }
} }

View File

@@ -22,28 +22,28 @@ namespace OpenRa.Game
{ {
TickFunc order = null; TickFunc order = null;
order = nextOrder = delegate order = nextOrder = delegate
{ {
// TODO: check that there's actually some ore in this cell :) // TODO: check that there's actually some ore in this cell :)
// face in one of the 8 directions // face in one of the 8 directions
int desiredFacing = ( facing + 1 ) & 28; int desiredFacing = ( facing + 1 ) & 28;
if( facing != desiredFacing ) if( facing != desiredFacing )
{ {
Turn( desiredFacing ); Turn( desiredFacing );
return; return;
} }
currentOrder = delegate { }; currentOrder = delegate { };
if( nextOrder == null ) if( nextOrder == null )
nextOrder = order; nextOrder = order;
string sequenceName = string.Format( "harvest{0}", facing / 4 ); string sequenceName = string.Format( "harvest{0}", facing / 4 );
animation.PlayThen( sequenceName, delegate animation.PlayThen( sequenceName, delegate
{ {
currentOrder = null; currentOrder = null;
animation.PlayFetchIndex( "idle", delegate { return facing; } ); animation.PlayFetchIndex( "idle", delegate { return facing; } );
} ); } );
}; };
} }
public class HarvestOrder : IOrder public class HarvestOrder : IOrder

View File

@@ -49,7 +49,6 @@
<Compile Include="Sequence.cs" /> <Compile Include="Sequence.cs" />
<Compile Include="ConstructionYard.cs" /> <Compile Include="ConstructionYard.cs" />
<Compile Include="float2.cs" /> <Compile Include="float2.cs" />
<Compile Include="int2.cs" />
<Compile Include="ISelectable.cs" /> <Compile Include="ISelectable.cs" />
<Compile Include="MoveOrder.cs" /> <Compile Include="MoveOrder.cs" />
<Compile Include="Region.cs" /> <Compile Include="Region.cs" />

View File

@@ -26,11 +26,12 @@ namespace OpenRa.Game
: double.PositiveInfinity; : double.PositiveInfinity;
} }
public List<int2> FindUnitPath( World world, Unit unit, int2 destination ) // returns estimate to destination, 0.0 is cell is dest
public delegate double DestinationFunc( int2 cell );
public List<int2> FindUnitPath( World world, Unit unit, DestinationFunc estimator )
{ {
int2 offset = new int2( map.XOffset, map.YOffset ); int2 offset = new int2( map.XOffset, map.YOffset );
destination += offset;
int2 startLocation = unit.Location + offset; int2 startLocation = unit.Location + offset;
CellInfo[ , ] cellInfo = new CellInfo[ 128, 128 ]; CellInfo[ , ] cellInfo = new CellInfo[ 128, 128 ];
@@ -39,9 +40,14 @@ namespace OpenRa.Game
for( int y = 0 ; y < 128 ; y++ ) for( int y = 0 ; y < 128 ; y++ )
cellInfo[ x, y ] = new CellInfo( double.PositiveInfinity, new int2( x, y ), false ); cellInfo[ x, y ] = new CellInfo( double.PositiveInfinity, new int2( x, y ), false );
return FindUnitPath( startLocation, estimator, offset, cellInfo );
}
List<int2> FindUnitPath( int2 startLocation, DestinationFunc estimator, int2 offset, CellInfo[,] cellInfo )
{
PriorityQueue<PathDistance> queue = new PriorityQueue<PathDistance>(); PriorityQueue<PathDistance> queue = new PriorityQueue<PathDistance>();
queue.Add( new PathDistance( Estimate( startLocation, destination ), startLocation ) ); queue.Add( new PathDistance( estimator( startLocation - offset ), startLocation ) );
cellInfo[ startLocation.X, startLocation.Y ].MinCost = 0; cellInfo[ startLocation.X, startLocation.Y ].MinCost = 0;
int seenCount = 0; int seenCount = 0;
@@ -53,10 +59,10 @@ namespace OpenRa.Game
int2 here = p.Location; int2 here = p.Location;
cellInfo[ here.X, here.Y ].Seen = true; cellInfo[ here.X, here.Y ].Seen = true;
if( p.Location == destination ) if( estimator( here - offset ) == 0.0 )
{ {
Log.Write( "{0}, {1}", seenCount, impassableCount ); Log.Write( "{0}, {1}", seenCount, impassableCount );
return MakePath( cellInfo, destination, offset ); return MakePath( cellInfo, here, offset );
} }
foreach( int2 d in directions ) foreach( int2 d in directions )
@@ -83,7 +89,7 @@ namespace OpenRa.Game
cellInfo[ newHere.X, newHere.Y ].Path = here; cellInfo[ newHere.X, newHere.Y ].Path = here;
cellInfo[ newHere.X, newHere.Y ].MinCost = newCost; cellInfo[ newHere.X, newHere.Y ].MinCost = newCost;
queue.Add( new PathDistance( newCost + Estimate( newHere, destination ), newHere ) ); queue.Add( new PathDistance( newCost + estimator( newHere - offset ), newHere ) );
} }
} }
@@ -118,12 +124,15 @@ namespace OpenRa.Game
new int2( 1, 1 ), new int2( 1, 1 ),
}; };
double Estimate( int2 here, int2 destination ) public static DestinationFunc DefaultEstimator( int2 destination )
{ {
int2 d = ( here - destination ).Abs(); return delegate( int2 here )
int diag = Math.Min( d.X, d.Y ); {
int straight = Math.Abs( d.X - d.Y ); int2 d = ( here - destination ).Abs();
return 1.5 * diag + straight; int diag = Math.Min( d.X, d.Y );
int straight = Math.Abs( d.X - d.Y );
return 1.5 * diag + straight;
};
} }
} }

View File

@@ -19,6 +19,6 @@ namespace OpenRa.Game
Sprite[] currentImages; Sprite[] currentImages;
public override Sprite[] CurrentImages { get { return currentImages; } } public override Sprite[] CurrentImages { get { return currentImages; } }
public override float2 RenderLocation { get { return 24 * location.ToFloat2(); } } public override float2 RenderLocation { get { return 24 * location; } }
} }
} }

View File

@@ -76,7 +76,7 @@ namespace OpenRa.Game
if( nextOrder != null ) if( nextOrder != null )
destination = toCell; destination = toCell;
int desiredFacing = GetFacing( ( toCell - fromCell ).ToFloat2() ); int desiredFacing = GetFacing( toCell - fromCell );
if( facing != desiredFacing ) if( facing != desiredFacing )
Turn( desiredFacing ); Turn( desiredFacing );
else else
@@ -92,7 +92,7 @@ namespace OpenRa.Game
currentOrder = null; currentOrder = null;
else else
{ {
List<int2> res = PathFinder.Instance.FindUnitPath( world, this, destination ); List<int2> res = PathFinder.Instance.FindUnitPath( world, this, PathFinder.DefaultEstimator( destination ) );
if( res.Count != 0 ) if( res.Count != 0 )
{ {
toCell = res[ res.Count - 1 ]; toCell = res[ res.Count - 1 ];
@@ -130,7 +130,7 @@ namespace OpenRa.Game
{ {
float fraction = (moveFraction > 0) ? (float)moveFraction / moveFractionTotal : 0f; float fraction = (moveFraction > 0) ? (float)moveFraction / moveFractionTotal : 0f;
float2 location = 24 * float2.Lerp( fromCell.ToFloat2(), toCell.ToFloat2(), fraction ); float2 location = 24 * float2.Lerp( fromCell, toCell, fraction );
return ( location - renderOffset ).Round(); ; return ( location - renderOffset ).Round(); ;
} }
} }

View File

@@ -20,6 +20,11 @@ namespace OpenRa.Game
public PointF ToPointF() { return new PointF(X, Y); } public PointF ToPointF() { return new PointF(X, Y); }
public static implicit operator float2( int2 src )
{
return new float2( src.X, src.Y );
}
public static float2 operator +(float2 a, float2 b) { return new float2(a.X + b.X, a.Y + b.Y); } public static float2 operator +(float2 a, float2 b) { return new float2(a.X + b.X, a.Y + b.Y); }
public static float2 operator -(float2 a, float2 b) { return new float2(a.X - b.X, a.Y - b.Y); } public static float2 operator -(float2 a, float2 b) { return new float2(a.X - b.X, a.Y - b.Y); }