diff --git a/OpenRA.Mods.Common/Activities/FindAndDeliverResources.cs b/OpenRA.Mods.Common/Activities/FindAndDeliverResources.cs index 6141e1199f..de57a68fca 100644 --- a/OpenRA.Mods.Common/Activities/FindAndDeliverResources.cs +++ b/OpenRA.Mods.Common/Activities/FindAndDeliverResources.cs @@ -191,7 +191,7 @@ namespace OpenRA.Mods.Common.Activities .WithCustomCost(loc => { if ((loc - searchFromLoc).LengthSquared > searchRadiusSquared) - return PathGraph.CostForInvalidCell; + return PathGraph.PathCostForInvalidPath; // Add a cost modifier to harvestable cells to prefer resources that are closer to the refinery. // This reduces the tendancy for harvesters to move in straight lines diff --git a/OpenRA.Mods.Common/Pathfinder/CellInfoLayerPool.cs b/OpenRA.Mods.Common/Pathfinder/CellInfoLayerPool.cs index 46ff003828..440a1ad3af 100644 --- a/OpenRA.Mods.Common/Pathfinder/CellInfoLayerPool.cs +++ b/OpenRA.Mods.Common/Pathfinder/CellInfoLayerPool.cs @@ -25,7 +25,7 @@ namespace OpenRA.Mods.Common.Pathfinder { defaultLayer = CellLayer.CreateInstance( - mpos => new CellInfo(int.MaxValue, int.MaxValue, mpos.ToCPos(map), CellStatus.Unvisited), + mpos => new CellInfo(PathGraph.PathCostForInvalidPath, PathGraph.PathCostForInvalidPath, mpos.ToCPos(map), CellStatus.Unvisited), new Size(map.MapSize.X, map.MapSize.Y), map.Grid.Type); } diff --git a/OpenRA.Mods.Common/Pathfinder/PathGraph.cs b/OpenRA.Mods.Common/Pathfinder/PathGraph.cs index 5d6f39265b..e5c5223ac1 100644 --- a/OpenRA.Mods.Common/Pathfinder/PathGraph.cs +++ b/OpenRA.Mods.Common/Pathfinder/PathGraph.cs @@ -75,7 +75,8 @@ namespace OpenRA.Mods.Common.Pathfinder sealed class PathGraph : IGraph { - public const int CostForInvalidCell = int.MaxValue; + public const int PathCostForInvalidPath = int.MaxValue; + public const short MovementCostForUnreachableCell = short.MaxValue; public Actor Actor { get; private set; } public World World { get; private set; } @@ -147,11 +148,11 @@ namespace OpenRA.Mods.Common.Pathfinder { var dir = directions[i]; var neighbor = position + dir; - var movementCost = GetCostToNode(neighbor, dir); + var pathCost = GetPathCostToNode(neighbor, dir); // PERF: Skip closed cells already, 15% of all cells - if (movementCost != CostForInvalidCell && info[neighbor].Status != CellStatus.Closed) - validNeighbors.Add(new GraphConnection(neighbor, movementCost)); + if (pathCost != PathCostForInvalidPath && info[neighbor].Status != CellStatus.Closed) + validNeighbors.Add(new GraphConnection(neighbor, pathCost)); } if (posLayer == 0) @@ -160,7 +161,7 @@ namespace OpenRA.Mods.Common.Pathfinder { var layerPosition = new CPos(position.X, position.Y, cli.Layer.Index); var entryCost = cli.Layer.EntryMovementCost(locomotor.Info, layerPosition); - if (entryCost != CostForInvalidCell) + if (entryCost != MovementCostForUnreachableCell) validNeighbors.Add(new GraphConnection(layerPosition, entryCost)); } } @@ -168,23 +169,23 @@ namespace OpenRA.Mods.Common.Pathfinder { var layerPosition = new CPos(position.X, position.Y, 0); var exitCost = customLayerInfo[posLayer].Layer.ExitMovementCost(locomotor.Info, layerPosition); - if (exitCost != CostForInvalidCell) + if (exitCost != MovementCostForUnreachableCell) validNeighbors.Add(new GraphConnection(layerPosition, exitCost)); } return validNeighbors; } - int GetCostToNode(CPos destNode, CVec direction) + int GetPathCostToNode(CPos destNode, CVec direction) { var movementCost = locomotor.MovementCostToEnterCell(Actor, destNode, checkConditions, IgnoreActor); - if (movementCost != short.MaxValue && !(CustomBlock != null && CustomBlock(destNode))) - return CalculateCellCost(destNode, direction, movementCost); + if (movementCost != MovementCostForUnreachableCell && !(CustomBlock != null && CustomBlock(destNode))) + return CalculateCellPathCost(destNode, direction, movementCost); - return CostForInvalidCell; + return PathCostForInvalidPath; } - int CalculateCellCost(CPos neighborCPos, CVec direction, int movementCost) + int CalculateCellPathCost(CPos neighborCPos, CVec direction, int movementCost) { var cellCost = movementCost; @@ -194,8 +195,8 @@ namespace OpenRA.Mods.Common.Pathfinder if (CustomCost != null) { var customCost = CustomCost(neighborCPos); - if (customCost == CostForInvalidCell) - return CostForInvalidCell; + if (customCost == PathCostForInvalidPath) + return PathCostForInvalidPath; cellCost += customCost; } @@ -206,7 +207,7 @@ namespace OpenRA.Mods.Common.Pathfinder var heightLayer = World.Map.Height; var from = neighborCPos - direction; if (Math.Abs(heightLayer[neighborCPos] - heightLayer[from]) > 1) - return CostForInvalidCell; + return PathCostForInvalidPath; } // Directional bonuses for smoother flow! diff --git a/OpenRA.Mods.Common/Pathfinder/PathSearch.cs b/OpenRA.Mods.Common/Pathfinder/PathSearch.cs index 896c2e59cb..59cb5792c6 100644 --- a/OpenRA.Mods.Common/Pathfinder/PathSearch.cs +++ b/OpenRA.Mods.Common/Pathfinder/PathSearch.cs @@ -108,7 +108,7 @@ namespace OpenRA.Mods.Common.Pathfinder var currentCell = Graph[currentMinNode]; Graph[currentMinNode] = new CellInfo(currentCell.CostSoFar, currentCell.EstimatedTotal, currentCell.PreviousPos, CellStatus.Closed); - if (Graph.CustomCost != null && Graph.CustomCost(currentMinNode) == PathGraph.CostForInvalidCell) + if (Graph.CustomCost != null && Graph.CustomCost(currentMinNode) == PathGraph.PathCostForInvalidPath) return currentMinNode; foreach (var connection in Graph.GetConnections(currentMinNode)) diff --git a/OpenRA.Mods.Common/Traits/Buildings/TransformsIntoMobile.cs b/OpenRA.Mods.Common/Traits/Buildings/TransformsIntoMobile.cs index 9c63ca73a2..beeee22809 100644 --- a/OpenRA.Mods.Common/Traits/Buildings/TransformsIntoMobile.cs +++ b/OpenRA.Mods.Common/Traits/Buildings/TransformsIntoMobile.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.Linq; using OpenRA.Mods.Common.Activities; +using OpenRA.Mods.Common.Pathfinder; using OpenRA.Primitives; using OpenRA.Traits; @@ -210,7 +211,7 @@ namespace OpenRA.Mods.Common.Traits bool CanEnterCell(Actor self, CPos cell) { - if (mobile.locomotor.MovementCostForCell(cell) == short.MaxValue) + if (mobile.locomotor.MovementCostForCell(cell) == PathGraph.MovementCostForUnreachableCell) return false; return mobile.locomotor.CanMoveFreelyInto(self, cell, BlockedByActor.All, null); diff --git a/OpenRA.Mods.Common/Traits/Harvester.cs b/OpenRA.Mods.Common/Traits/Harvester.cs index ae01dfe09d..02b017e793 100644 --- a/OpenRA.Mods.Common/Traits/Harvester.cs +++ b/OpenRA.Mods.Common/Traits/Harvester.cs @@ -206,7 +206,7 @@ namespace OpenRA.Mods.Common.Traits // Too many harvesters clogs up the refinery's delivery location: if (occupancy >= Info.MaxUnloadQueue) - return PathGraph.CostForInvalidCell; + return PathGraph.PathCostForInvalidPath; // Prefer refineries with less occupancy (multiplier is to offset distance cost): return occupancy * Info.UnloadQueueCostModifier; diff --git a/OpenRA.Mods.Common/Traits/Mobile.cs b/OpenRA.Mods.Common/Traits/Mobile.cs index ef2a2a8213..ceaa45bc31 100644 --- a/OpenRA.Mods.Common/Traits/Mobile.cs +++ b/OpenRA.Mods.Common/Traits/Mobile.cs @@ -119,7 +119,7 @@ namespace OpenRA.Mods.Common.Traits locomotor = world.WorldActor.TraitsImplementing() .SingleOrDefault(l => l.Info.Name == Locomotor); - if (locomotor.MovementCostForCell(cell) == short.MaxValue) + if (locomotor.MovementCostForCell(cell) == PathGraph.MovementCostForUnreachableCell) return false; return locomotor.CanMoveFreelyInto(self, cell, subCell, check, ignoreActor); @@ -520,7 +520,7 @@ namespace OpenRA.Mods.Common.Traits public bool CanExistInCell(CPos cell) { - return Locomotor.MovementCostForCell(cell) != short.MaxValue; + return Locomotor.MovementCostForCell(cell) != PathGraph.MovementCostForUnreachableCell; } public bool CanEnterCell(CPos cell, Actor ignoreActor = null, BlockedByActor check = BlockedByActor.All) @@ -1020,7 +1020,7 @@ namespace OpenRA.Mods.Common.Traits if (mobile.IsTraitPaused || !self.World.Map.Contains(location) || (!explored && !locomotorInfo.MoveIntoShroud) - || (explored && mobile.Locomotor.MovementCostForCell(location) == short.MaxValue)) + || (explored && mobile.Locomotor.MovementCostForCell(location) == PathGraph.MovementCostForUnreachableCell)) cursor = mobile.Info.BlockedCursor; else if (!explored || !mobile.Info.TerrainCursors.TryGetValue(self.World.Map.GetTerrainInfo(location).Type, out cursor)) cursor = mobile.Info.Cursor; diff --git a/OpenRA.Mods.Common/Traits/World/ElevatedBridgeLayer.cs b/OpenRA.Mods.Common/Traits/World/ElevatedBridgeLayer.cs index 0e1598b329..26cda2ab31 100644 --- a/OpenRA.Mods.Common/Traits/World/ElevatedBridgeLayer.cs +++ b/OpenRA.Mods.Common/Traits/World/ElevatedBridgeLayer.cs @@ -80,14 +80,14 @@ namespace OpenRA.Mods.Common.Traits return cellCenters[cell]; } - int ICustomMovementLayer.EntryMovementCost(LocomotorInfo li, CPos cell) + short ICustomMovementLayer.EntryMovementCost(LocomotorInfo li, CPos cell) { - return ends.Contains(cell) ? 0 : PathGraph.CostForInvalidCell; + return ends.Contains(cell) ? (short)0 : PathGraph.MovementCostForUnreachableCell; } - int ICustomMovementLayer.ExitMovementCost(LocomotorInfo li, CPos cell) + short ICustomMovementLayer.ExitMovementCost(LocomotorInfo li, CPos cell) { - return ends.Contains(cell) ? 0 : PathGraph.CostForInvalidCell; + return ends.Contains(cell) ? (short)0 : PathGraph.MovementCostForUnreachableCell; } byte ICustomMovementLayer.GetTerrainIndex(CPos cell) diff --git a/OpenRA.Mods.Common/Traits/World/JumpjetActorLayer.cs b/OpenRA.Mods.Common/Traits/World/JumpjetActorLayer.cs index 400d32c27e..590e496c34 100644 --- a/OpenRA.Mods.Common/Traits/World/JumpjetActorLayer.cs +++ b/OpenRA.Mods.Common/Traits/World/JumpjetActorLayer.cs @@ -87,16 +87,16 @@ namespace OpenRA.Mods.Common.Traits return map.Ramp[cell] == 0; } - int ICustomMovementLayer.EntryMovementCost(LocomotorInfo li, CPos cell) + short ICustomMovementLayer.EntryMovementCost(LocomotorInfo li, CPos cell) { var jli = (JumpjetLocomotorInfo)li; - return ValidTransitionCell(cell, jli) ? jli.JumpjetTransitionCost : PathGraph.CostForInvalidCell; + return ValidTransitionCell(cell, jli) ? jli.JumpjetTransitionCost : PathGraph.MovementCostForUnreachableCell; } - int ICustomMovementLayer.ExitMovementCost(LocomotorInfo li, CPos cell) + short ICustomMovementLayer.ExitMovementCost(LocomotorInfo li, CPos cell) { var jli = (JumpjetLocomotorInfo)li; - return ValidTransitionCell(cell, jli) ? jli.JumpjetTransitionCost : PathGraph.CostForInvalidCell; + return ValidTransitionCell(cell, jli) ? jli.JumpjetTransitionCost : PathGraph.MovementCostForUnreachableCell; } byte ICustomMovementLayer.GetTerrainIndex(CPos cell) diff --git a/OpenRA.Mods.Common/Traits/World/JumpjetLocomotor.cs b/OpenRA.Mods.Common/Traits/World/JumpjetLocomotor.cs index 5c21dd9516..dd0456bf11 100644 --- a/OpenRA.Mods.Common/Traits/World/JumpjetLocomotor.cs +++ b/OpenRA.Mods.Common/Traits/World/JumpjetLocomotor.cs @@ -17,7 +17,7 @@ namespace OpenRA.Mods.Common.Traits public class JumpjetLocomotorInfo : LocomotorInfo { [Desc("Pathfinding cost for taking off or landing.")] - public readonly int JumpjetTransitionCost = 0; + public readonly short JumpjetTransitionCost = 0; [Desc("The terrain types that this actor can transition on. Leave empty to allow any.")] public readonly HashSet JumpjetTransitionTerrainTypes = new HashSet(); diff --git a/OpenRA.Mods.Common/Traits/World/Locomotor.cs b/OpenRA.Mods.Common/Traits/World/Locomotor.cs index 8fb5844e48..cb5e4665e3 100644 --- a/OpenRA.Mods.Common/Traits/World/Locomotor.cs +++ b/OpenRA.Mods.Common/Traits/World/Locomotor.cs @@ -13,6 +13,7 @@ using System; using System.Collections.Generic; using System.Linq; using OpenRA.Graphics; +using OpenRA.Mods.Common.Pathfinder; using OpenRA.Primitives; using OpenRA.Support; using OpenRA.Traits; @@ -109,7 +110,7 @@ namespace OpenRA.Mods.Common.Traits public TerrainInfo() { - Cost = short.MaxValue; + Cost = PathGraph.MovementCostForUnreachableCell; Speed = 0; } @@ -168,13 +169,13 @@ namespace OpenRA.Mods.Common.Traits if (!info.TerrainSpeeds.TryGetValue(terrainInfo.TerrainTypes[i].Type, out terrainInfos[i])) terrainInfos[i] = LocomotorInfo.TerrainInfo.Impassable; - MovementClass = (uint)terrainInfos.Select(ti => ti.Cost < short.MaxValue).ToBits(); + MovementClass = (uint)terrainInfos.Select(ti => ti.Cost != PathGraph.MovementCostForUnreachableCell).ToBits(); } public short MovementCostForCell(CPos cell) { if (!world.Map.Contains(cell)) - return short.MaxValue; + return PathGraph.MovementCostForUnreachableCell; return cell.Layer == 0 ? cellsCost[cell] : customLayerCellsCost[cell.Layer][cell]; } @@ -190,13 +191,13 @@ namespace OpenRA.Mods.Common.Traits public short MovementCostToEnterCell(Actor actor, CPos destNode, BlockedByActor check, Actor ignoreActor) { if (!world.Map.Contains(destNode)) - return short.MaxValue; + return PathGraph.MovementCostForUnreachableCell; var cellCost = destNode.Layer == 0 ? cellsCost[destNode] : customLayerCellsCost[destNode.Layer][destNode]; - if (cellCost == short.MaxValue || + if (cellCost == PathGraph.MovementCostForUnreachableCell || !CanMoveFreelyInto(actor, destNode, check, ignoreActor)) - return short.MaxValue; + return PathGraph.MovementCostForUnreachableCell; return cellCost; } @@ -276,7 +277,7 @@ namespace OpenRA.Mods.Common.Traits public SubCell GetAvailableSubCell(Actor self, CPos cell, BlockedByActor check, SubCell preferredSubCell = SubCell.Any, Actor ignoreActor = null) { - if (MovementCostForCell(cell) == short.MaxValue) + if (MovementCostForCell(cell) == PathGraph.MovementCostForUnreachableCell) return SubCell.Invalid; if (check > BlockedByActor.None) @@ -381,7 +382,7 @@ namespace OpenRA.Mods.Common.Traits { var index = cml.GetTerrainIndex(cell); - var cost = short.MaxValue; + var cost = PathGraph.MovementCostForUnreachableCell; if (index != byte.MaxValue) cost = terrainInfos[index].Cost; @@ -416,7 +417,7 @@ namespace OpenRA.Mods.Common.Traits ? world.Map.GetTerrainIndex(cell) : world.GetCustomMovementLayers()[cell.Layer].GetTerrainIndex(cell); - var cost = short.MaxValue; + var cost = PathGraph.MovementCostForUnreachableCell; if (index != byte.MaxValue) cost = terrainInfos[index].Cost; diff --git a/OpenRA.Mods.Common/Traits/World/PathFinder.cs b/OpenRA.Mods.Common/Traits/World/PathFinder.cs index 65a890be09..2fb5b5b700 100644 --- a/OpenRA.Mods.Common/Traits/World/PathFinder.cs +++ b/OpenRA.Mods.Common/Traits/World/PathFinder.cs @@ -167,7 +167,7 @@ namespace OpenRA.Mods.Common.Traits // make some progress on the first search var p = fromSrc.Expand(); if (fromDest.Graph[p].Status == CellStatus.Closed && - fromDest.Graph[p].CostSoFar < int.MaxValue) + fromDest.Graph[p].CostSoFar != PathGraph.PathCostForInvalidPath) { path = MakeBidiPath(fromSrc, fromDest, p); break; @@ -176,7 +176,7 @@ namespace OpenRA.Mods.Common.Traits // make some progress on the second search var q = fromDest.Expand(); if (fromSrc.Graph[q].Status == CellStatus.Closed && - fromSrc.Graph[q].CostSoFar < int.MaxValue) + fromSrc.Graph[q].CostSoFar != PathGraph.PathCostForInvalidPath) { path = MakeBidiPath(fromSrc, fromDest, q); break; diff --git a/OpenRA.Mods.Common/Traits/World/SubterraneanActorLayer.cs b/OpenRA.Mods.Common/Traits/World/SubterraneanActorLayer.cs index 7e55ed7f77..67d2c76ecd 100644 --- a/OpenRA.Mods.Common/Traits/World/SubterraneanActorLayer.cs +++ b/OpenRA.Mods.Common/Traits/World/SubterraneanActorLayer.cs @@ -85,16 +85,16 @@ namespace OpenRA.Mods.Common.Traits return map.Ramp[cell] == 0; } - int ICustomMovementLayer.EntryMovementCost(LocomotorInfo li, CPos cell) + short ICustomMovementLayer.EntryMovementCost(LocomotorInfo li, CPos cell) { var sli = (SubterraneanLocomotorInfo)li; - return ValidTransitionCell(cell, sli) ? sli.SubterraneanTransitionCost : PathGraph.CostForInvalidCell; + return ValidTransitionCell(cell, sli) ? sli.SubterraneanTransitionCost : PathGraph.MovementCostForUnreachableCell; } - int ICustomMovementLayer.ExitMovementCost(LocomotorInfo li, CPos cell) + short ICustomMovementLayer.ExitMovementCost(LocomotorInfo li, CPos cell) { var sli = (SubterraneanLocomotorInfo)li; - return ValidTransitionCell(cell, sli) ? sli.SubterraneanTransitionCost : PathGraph.CostForInvalidCell; + return ValidTransitionCell(cell, sli) ? sli.SubterraneanTransitionCost : PathGraph.MovementCostForUnreachableCell; } byte ICustomMovementLayer.GetTerrainIndex(CPos cell) diff --git a/OpenRA.Mods.Common/Traits/World/SubterraneanLocomotor.cs b/OpenRA.Mods.Common/Traits/World/SubterraneanLocomotor.cs index 2388a43438..70c02c7274 100644 --- a/OpenRA.Mods.Common/Traits/World/SubterraneanLocomotor.cs +++ b/OpenRA.Mods.Common/Traits/World/SubterraneanLocomotor.cs @@ -17,7 +17,7 @@ namespace OpenRA.Mods.Common.Traits public class SubterraneanLocomotorInfo : LocomotorInfo { [Desc("Pathfinding cost for submerging or reemerging.")] - public readonly int SubterraneanTransitionCost = 0; + public readonly short SubterraneanTransitionCost = 0; [Desc("The terrain types that this actor can transition on. Leave empty to allow any.")] public readonly HashSet SubterraneanTransitionTerrainTypes = new HashSet(); @@ -25,7 +25,7 @@ namespace OpenRA.Mods.Common.Traits [Desc("Can this actor transition on slopes?")] public readonly bool SubterraneanTransitionOnRamps = false; - [Desc("Depth at which the subterranian condition is applied.")] + [Desc("Depth at which the subterranean condition is applied.")] public readonly WDist SubterraneanTransitionDepth = new WDist(-1024); public override bool DisableDomainPassabilityCheck => true; diff --git a/OpenRA.Mods.Common/Traits/World/TerrainTunnelLayer.cs b/OpenRA.Mods.Common/Traits/World/TerrainTunnelLayer.cs index 41699e60da..e406aae13c 100644 --- a/OpenRA.Mods.Common/Traits/World/TerrainTunnelLayer.cs +++ b/OpenRA.Mods.Common/Traits/World/TerrainTunnelLayer.cs @@ -79,14 +79,14 @@ namespace OpenRA.Mods.Common.Traits return cellCenters[cell]; } - int ICustomMovementLayer.EntryMovementCost(LocomotorInfo li, CPos cell) + short ICustomMovementLayer.EntryMovementCost(LocomotorInfo li, CPos cell) { - return portals.Contains(cell) ? 0 : PathGraph.CostForInvalidCell; + return portals.Contains(cell) ? (short)0 : PathGraph.MovementCostForUnreachableCell; } - int ICustomMovementLayer.ExitMovementCost(LocomotorInfo li, CPos cell) + short ICustomMovementLayer.ExitMovementCost(LocomotorInfo li, CPos cell) { - return portals.Contains(cell) ? 0 : PathGraph.CostForInvalidCell; + return portals.Contains(cell) ? (short)0 : PathGraph.MovementCostForUnreachableCell; } byte ICustomMovementLayer.GetTerrainIndex(CPos cell) diff --git a/OpenRA.Mods.Common/TraitsInterfaces.cs b/OpenRA.Mods.Common/TraitsInterfaces.cs index 919c6cb6a5..cce40590aa 100644 --- a/OpenRA.Mods.Common/TraitsInterfaces.cs +++ b/OpenRA.Mods.Common/TraitsInterfaces.cs @@ -404,8 +404,8 @@ namespace OpenRA.Mods.Common.Traits bool ReturnToGroundLayerOnIdle { get; } bool EnabledForLocomotor(LocomotorInfo li); - int EntryMovementCost(LocomotorInfo li, CPos cell); - int ExitMovementCost(LocomotorInfo li, CPos cell); + short EntryMovementCost(LocomotorInfo li, CPos cell); + short ExitMovementCost(LocomotorInfo li, CPos cell); byte GetTerrainIndex(CPos cell); WPos CenterOfCell(CPos cell);