From 2f6315b816e944c6afeafb59d2b9b75c84372b50 Mon Sep 17 00:00:00 2001 From: Bob Date: Wed, 22 Sep 2010 07:40:21 +1200 Subject: [PATCH] make the pathfinder use integers --- OpenRA.Game/PathFinder.cs | 8 +++--- OpenRA.Game/PathSearch.cs | 27 ++++++++++---------- OpenRA.Game/Traits/Mobile.cs | 19 ++++++-------- OpenRA.Game/Traits/TraitsInterfaces.cs | 1 - OpenRA.Mods.RA/Aircraft.cs | 2 -- mods/ra/rules/defaults.yaml | 34 +++++++++++++------------- 6 files changed, 42 insertions(+), 49 deletions(-) diff --git a/OpenRA.Game/PathFinder.cs b/OpenRA.Game/PathFinder.cs index a5dd764e24..b7ca1758a4 100644 --- a/OpenRA.Game/PathFinder.cs +++ b/OpenRA.Game/PathFinder.cs @@ -207,11 +207,11 @@ namespace OpenRA public struct CellInfo { - public float MinCost; + public int MinCost; public int2 Path; public bool Seen; - public CellInfo( float minCost, int2 path, bool seen ) + public CellInfo( int minCost, int2 path, bool seen ) { MinCost = minCost; Path = path; @@ -221,10 +221,10 @@ namespace OpenRA public struct PathDistance : IComparable { - public float EstTotal; + public int EstTotal; public int2 Location; - public PathDistance(float estTotal, int2 location) + public PathDistance(int estTotal, int2 location) { EstTotal = estTotal; Location = location; diff --git a/OpenRA.Game/PathSearch.cs b/OpenRA.Game/PathSearch.cs index 28bc0e22f9..9a2ae8410a 100755 --- a/OpenRA.Game/PathSearch.cs +++ b/OpenRA.Game/PathSearch.cs @@ -20,7 +20,7 @@ namespace OpenRA World world; public CellInfo[ , ] cellInfo; public PriorityQueue queue; - public Func heuristic; + public Func heuristic; Func customBlock; public bool checkForBlocked; public Actor ignoreBuilding; @@ -58,7 +58,7 @@ namespace OpenRA return this; } - public PathSearch WithHeuristic(Func h) + public PathSearch WithHeuristic(Func h) { heuristic = h; return this; @@ -66,7 +66,7 @@ namespace OpenRA public PathSearch WithoutLaneBias() { - LaneBias = 0f; + LaneBias = 0; return this; } @@ -76,7 +76,7 @@ namespace OpenRA return this; } - float LaneBias = .5f; + int LaneBias = 1; public int2 Expand( World world ) { @@ -91,7 +91,7 @@ namespace OpenRA var thisCost = Mobile.MovementCostForCell(mobileInfo, world, p.Location); - if (thisCost == float.PositiveInfinity) + if (thisCost == int.MaxValue) return p.Location; foreach( int2 d in directions ) @@ -102,9 +102,9 @@ namespace OpenRA if( cellInfo[ newHere.X, newHere.Y ].Seen ) continue; - var costHere = (float)Mobile.MovementCostForCell(mobileInfo, world, newHere); + var costHere = Mobile.MovementCostForCell(mobileInfo, world, newHere); - if (costHere == float.PositiveInfinity) + if (costHere == int.MaxValue) continue; if (!Mobile.CanEnterCell(mobileInfo, world, uim, bim, newHere, ignoreBuilding, checkForBlocked)) @@ -114,10 +114,11 @@ namespace OpenRA continue; var est = heuristic( newHere ); - if( est == float.PositiveInfinity ) + if( est == int.MaxValue ) continue; - float cellCost = (float)(((d.X * d.Y != 0) ? 1.414213563f : 1.0f) * costHere); + int cellCost = costHere; + if( d.X * d.Y != 0 ) cellCost = ( cellCost * 34 ) / 24; // directional bonuses for smoother flow! var ux = (newHere.X + (inReverse ? 1 : 0) & 1); @@ -128,7 +129,7 @@ namespace OpenRA if (uy == 0 && d.X < 0) cellCost += LaneBias; else if (uy == 1 && d.X > 0) cellCost += LaneBias; - float newCost = (float)(cellInfo[ p.Location.X, p.Location.Y ].MinCost + cellCost); + int newCost = cellInfo[ p.Location.X, p.Location.Y ].MinCost + cellCost; if( newCost >= cellInfo[ newHere.X, newHere.Y ].MinCost ) continue; @@ -199,18 +200,18 @@ namespace OpenRA var cellInfo = new CellInfo[ world.Map.MapSize.X, world.Map.MapSize.Y ]; for( int x = 0 ; x < world.Map.MapSize.X ; x++ ) for( int y = 0 ; y < world.Map.MapSize.Y ; y++ ) - cellInfo[ x, y ] = new CellInfo( float.PositiveInfinity, new int2( x, y ), false ); + cellInfo[ x, y ] = new CellInfo( int.MaxValue, new int2( x, y ), false ); return cellInfo; } - public static Func DefaultEstimator( int2 destination ) + public static Func DefaultEstimator( int2 destination ) { return here => { int2 d = ( here - destination ).Abs(); int diag = Math.Min( d.X, d.Y ); int straight = Math.Abs( d.X - d.Y ); - return 1.5f * diag + straight; + return (3400 * diag / 24) + (100 * straight); }; } } diff --git a/OpenRA.Game/Traits/Mobile.cs b/OpenRA.Game/Traits/Mobile.cs index ba4d6f18f6..3df6876ed4 100644 --- a/OpenRA.Game/Traits/Mobile.cs +++ b/OpenRA.Game/Traits/Mobile.cs @@ -39,7 +39,7 @@ namespace OpenRA.Traits foreach (var t in y.NodesDict["TerrainSpeeds"].Nodes) { var speed = (float)FieldLoader.GetValue("speed", typeof(float),t.Value.Value); - var cost = t.Value.NodesDict.ContainsKey("PathingCost") ? (float)FieldLoader.GetValue("cost", typeof(float), t.Value.NodesDict["PathingCost"].Value) : 1f/speed; + var cost = t.Value.NodesDict.ContainsKey("PathingCost") ? (int)FieldLoader.GetValue("cost", typeof(int), t.Value.NodesDict["PathingCost"].Value) : (int)(10000/speed); ret.Add(t.Key, new TerrainInfo{Speed = speed, Cost = cost}); } @@ -48,7 +48,7 @@ namespace OpenRA.Traits public class TerrainInfo { - public float Cost = float.PositiveInfinity; + public int Cost = int.MaxValue; public float Speed = 0; } } @@ -250,7 +250,7 @@ namespace OpenRA.Traits public static bool CanEnterCell( MobileInfo mobileInfo, World world, UnitInfluence uim, BuildingInfluence bim, int2 cell, Actor ignoreActor, bool checkTransientActors ) { - if (MovementCostForCell(mobileInfo, world, cell) == float.PositiveInfinity) + if (MovementCostForCell(mobileInfo, world, cell) == int.MaxValue) return false; // Check for buildings @@ -295,19 +295,14 @@ namespace OpenRA.Traits } } - public float MovementCostForCell( Actor self, int2 cell ) - { - return MovementCostForCell( Info, self.World, cell ); - } - - public static float MovementCostForCell(MobileInfo info, World world, int2 cell) + public static int MovementCostForCell(MobileInfo info, World world, int2 cell) { if (!world.Map.IsInMap(cell.X,cell.Y)) - return float.PositiveInfinity; + return int.MaxValue; var type = world.GetTerrainType(cell); if (!info.TerrainSpeeds.ContainsKey(type)) - return float.PositiveInfinity; + return int.MaxValue; return info.TerrainSpeeds[type].Cost; } @@ -323,7 +318,7 @@ namespace OpenRA.Traits .TraitsImplementing() .Select(t => t.GetSpeedModifier()) .Product(); - return Info.Speed * Info.TerrainSpeeds[type].Speed * modifier; + return Info.Speed * Info.TerrainSpeeds[type].Speed * modifier / 100f; } public IEnumerable GetCurrentPath(Actor self) diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index 23bf38302e..675909c98a 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -112,7 +112,6 @@ namespace OpenRA.Traits public interface IMove : ITeleportable { - float MovementCostForCell(Actor self, int2 cell); float MovementSpeedForCell(Actor self, int2 cell); IEnumerable GetCurrentPath(Actor self); int Altitude { get; set; } diff --git a/OpenRA.Mods.RA/Aircraft.cs b/OpenRA.Mods.RA/Aircraft.cs index 12fd648570..2de181476f 100755 --- a/OpenRA.Mods.RA/Aircraft.cs +++ b/OpenRA.Mods.RA/Aircraft.cs @@ -80,8 +80,6 @@ namespace OpenRA.Mods.RA public bool CanEnterCell(int2 location) { return true; } - public float MovementCostForCell(Actor self, int2 cell) { return 1f; } - public float MovementSpeedForCell(Actor self, int2 cell) { var modifier = self diff --git a/mods/ra/rules/defaults.yaml b/mods/ra/rules/defaults.yaml index f1ea2e9e34..7f8c0ce8fe 100644 --- a/mods/ra/rules/defaults.yaml +++ b/mods/ra/rules/defaults.yaml @@ -3,11 +3,11 @@ Mobile: Crushes: atmine, crate TerrainSpeeds: - Clear: 60% - Rough: 40% - Road: 100% - Ore: 90% - Beach: 40% + Clear: 60 + Rough: 40 + Road: 100 + Ore: 90 + Beach: 40 ROT: 5 Selectable: Voice: VehicleVoice @@ -30,11 +30,11 @@ Mobile: Crushes: wall, atmine, crate TerrainSpeeds: - Clear: 80% - Rough: 70% - Road: 100% - Ore: 70% - Beach: 70% + Clear: 80 + Rough: 70 + Road: 100 + Ore: 70 + Beach: 70 ROT: 5 Selectable: Voice: VehicleVoice @@ -62,11 +62,11 @@ Mobile: Crushes: apmine, crate TerrainSpeeds: - Clear: 90% - Rough: 80% - Road: 100% - Ore: 100% - Beach: 80% + Clear: 90 + Rough: 80 + Road: 100 + Ore: 100 + Beach: 80 Selectable: Voice: GenericVoice Targetable: @@ -88,7 +88,7 @@ Mobile: Crushes: crate TerrainSpeeds: - Water: 100% + Water: 100 Selectable: Voice: ShipVoice Targetable: @@ -208,4 +208,4 @@ Footprint: ____ ____ Dimensions: 4,2 Health: - HP: 1000 \ No newline at end of file + HP: 1000