diff --git a/OpenRa.DataStructures/OpenRa.DataStructures.csproj b/OpenRa.DataStructures/OpenRa.DataStructures.csproj index f0fddf98c3..11f3dd9b72 100644 --- a/OpenRa.DataStructures/OpenRa.DataStructures.csproj +++ b/OpenRa.DataStructures/OpenRa.DataStructures.csproj @@ -30,9 +30,11 @@ + + diff --git a/OpenRa.Game/int2.cs b/OpenRa.DataStructures/int2.cs similarity index 90% rename from OpenRa.Game/int2.cs rename to OpenRa.DataStructures/int2.cs index b4aa703623..ca013a248a 100644 --- a/OpenRa.Game/int2.cs +++ b/OpenRa.DataStructures/int2.cs @@ -3,9 +3,9 @@ using System.Collections.Generic; using System.Text; using System.Drawing; -namespace OpenRa.Game +namespace OpenRa { - struct int2 + public struct int2 { 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 *(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 == other); } diff --git a/OpenRa.Game/Building.cs b/OpenRa.Game/Building.cs index adc81adefd..ae6c21e6a1 100644 --- a/OpenRa.Game/Building.cs +++ b/OpenRa.Game/Building.cs @@ -30,7 +30,7 @@ namespace OpenRa.Game public override float2 RenderLocation { - get { return 24.0f * location.ToFloat2(); } + get { return 24.0f * (float2)location; } } } } diff --git a/OpenRa.Game/Harvester.cs b/OpenRa.Game/Harvester.cs index 88c6c372bd..772feda071 100644 --- a/OpenRa.Game/Harvester.cs +++ b/OpenRa.Game/Harvester.cs @@ -22,28 +22,28 @@ namespace OpenRa.Game { TickFunc order = null; 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 - int desiredFacing = ( facing + 1 ) & 28; - if( facing != desiredFacing ) - { - Turn( desiredFacing ); - return; - } + // face in one of the 8 directions + int desiredFacing = ( facing + 1 ) & 28; + if( facing != desiredFacing ) + { + Turn( desiredFacing ); + return; + } - currentOrder = delegate { }; - if( nextOrder == null ) - nextOrder = order; + currentOrder = delegate { }; + if( nextOrder == null ) + nextOrder = order; - string sequenceName = string.Format( "harvest{0}", facing / 4 ); - animation.PlayThen( sequenceName, delegate - { - currentOrder = null; - animation.PlayFetchIndex( "idle", delegate { return facing; } ); - } ); - }; + string sequenceName = string.Format( "harvest{0}", facing / 4 ); + animation.PlayThen( sequenceName, delegate + { + currentOrder = null; + animation.PlayFetchIndex( "idle", delegate { return facing; } ); + } ); + }; } public class HarvestOrder : IOrder diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index 31aed69f6c..9a96039198 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -49,7 +49,6 @@ - diff --git a/OpenRa.Game/PathFinder.cs b/OpenRa.Game/PathFinder.cs index b79cb84139..bf6f2a70cb 100644 --- a/OpenRa.Game/PathFinder.cs +++ b/OpenRa.Game/PathFinder.cs @@ -26,11 +26,12 @@ namespace OpenRa.Game : double.PositiveInfinity; } - public List 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 FindUnitPath( World world, Unit unit, DestinationFunc estimator ) { int2 offset = new int2( map.XOffset, map.YOffset ); - - destination += offset; int2 startLocation = unit.Location + offset; CellInfo[ , ] cellInfo = new CellInfo[ 128, 128 ]; @@ -39,9 +40,14 @@ namespace OpenRa.Game for( int y = 0 ; y < 128 ; y++ ) cellInfo[ x, y ] = new CellInfo( double.PositiveInfinity, new int2( x, y ), false ); + return FindUnitPath( startLocation, estimator, offset, cellInfo ); + } + + List FindUnitPath( int2 startLocation, DestinationFunc estimator, int2 offset, CellInfo[,] cellInfo ) + { PriorityQueue queue = new PriorityQueue(); - queue.Add( new PathDistance( Estimate( startLocation, destination ), startLocation ) ); + queue.Add( new PathDistance( estimator( startLocation - offset ), startLocation ) ); cellInfo[ startLocation.X, startLocation.Y ].MinCost = 0; int seenCount = 0; @@ -53,10 +59,10 @@ namespace OpenRa.Game int2 here = p.Location; cellInfo[ here.X, here.Y ].Seen = true; - if( p.Location == destination ) + if( estimator( here - offset ) == 0.0 ) { Log.Write( "{0}, {1}", seenCount, impassableCount ); - return MakePath( cellInfo, destination, offset ); + return MakePath( cellInfo, here, offset ); } foreach( int2 d in directions ) @@ -83,7 +89,7 @@ namespace OpenRa.Game cellInfo[ newHere.X, newHere.Y ].Path = here; 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 ), }; - double Estimate( int2 here, int2 destination ) + public static DestinationFunc DefaultEstimator( int2 destination ) { - int2 d = ( here - destination ).Abs(); - int diag = Math.Min( d.X, d.Y ); - int straight = Math.Abs( d.X - d.Y ); - return 1.5 * diag + straight; + return delegate( int2 here ) + { + int2 d = ( here - destination ).Abs(); + int diag = Math.Min( d.X, d.Y ); + int straight = Math.Abs( d.X - d.Y ); + return 1.5 * diag + straight; + }; } } diff --git a/OpenRa.Game/Tree.cs b/OpenRa.Game/Tree.cs index 6a4fef113c..4a1261da22 100644 --- a/OpenRa.Game/Tree.cs +++ b/OpenRa.Game/Tree.cs @@ -19,6 +19,6 @@ namespace OpenRa.Game Sprite[] 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; } } } } diff --git a/OpenRa.Game/Unit.cs b/OpenRa.Game/Unit.cs index ce661cfd6f..7e76187ee7 100644 --- a/OpenRa.Game/Unit.cs +++ b/OpenRa.Game/Unit.cs @@ -76,7 +76,7 @@ namespace OpenRa.Game if( nextOrder != null ) destination = toCell; - int desiredFacing = GetFacing( ( toCell - fromCell ).ToFloat2() ); + int desiredFacing = GetFacing( toCell - fromCell ); if( facing != desiredFacing ) Turn( desiredFacing ); else @@ -92,7 +92,7 @@ namespace OpenRa.Game currentOrder = null; else { - List res = PathFinder.Instance.FindUnitPath( world, this, destination ); + List res = PathFinder.Instance.FindUnitPath( world, this, PathFinder.DefaultEstimator( destination ) ); if( res.Count != 0 ) { toCell = res[ res.Count - 1 ]; @@ -130,7 +130,7 @@ namespace OpenRa.Game { 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(); ; } } diff --git a/OpenRa.Game/float2.cs b/OpenRa.Game/float2.cs index d08839a298..1efa986738 100644 --- a/OpenRa.Game/float2.cs +++ b/OpenRa.Game/float2.cs @@ -20,6 +20,11 @@ namespace OpenRa.Game 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); }