From 51098482152aa555a6bd2afe9ddcf9988c39ff5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Jim=C3=A9nez?= Date: Sat, 28 Mar 2015 07:36:39 +0100 Subject: [PATCH 1/2] Changes to improve understandability of code --- OpenRA.Game/Traits/World/ActorMap.cs | 23 ++++++++++++----------- OpenRA.Game/WPos.cs | 3 +++ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/OpenRA.Game/Traits/World/ActorMap.cs b/OpenRA.Game/Traits/World/ActorMap.cs index b90a987052..f6b8dc4a1d 100644 --- a/OpenRA.Game/Traits/World/ActorMap.cs +++ b/OpenRA.Game/Traits/World/ActorMap.cs @@ -212,35 +212,36 @@ namespace OpenRA.Traits yield return i.Actor; } - public bool HasFreeSubCell(CPos a, bool checkTransient = true) + public bool HasFreeSubCell(CPos cell, bool checkTransient = true) { - return FreeSubCell(a, SubCell.Any, checkTransient) != SubCell.Invalid; + return FreeSubCell(cell, SubCell.Any, checkTransient) != SubCell.Invalid; } - public SubCell FreeSubCell(CPos a, SubCell preferredSubCell = SubCell.Any, bool checkTransient = true) + public SubCell FreeSubCell(CPos cell, SubCell preferredSubCell = SubCell.Any, bool checkTransient = true) { - if (preferredSubCell > SubCell.Any && !AnyUnitsAt(a, preferredSubCell, checkTransient)) + if (preferredSubCell > SubCell.Any && !AnyUnitsAt(cell, preferredSubCell, checkTransient)) return preferredSubCell; - if (!AnyUnitsAt(a)) + if (!AnyUnitsAt(cell)) return map.DefaultSubCell; for (var i = (int)SubCell.First; i < map.SubCellOffsets.Length; i++) - if (i != (int)preferredSubCell && !AnyUnitsAt(a, (SubCell)i, checkTransient)) + if (i != (int)preferredSubCell && !AnyUnitsAt(cell, (SubCell)i, checkTransient)) return (SubCell)i; + return SubCell.Invalid; } - public SubCell FreeSubCell(CPos a, SubCell preferredSubCell, Func checkIfBlocker) + public SubCell FreeSubCell(CPos cell, SubCell preferredSubCell, Func checkIfBlocker) { - if (preferredSubCell > SubCell.Any && !AnyUnitsAt(a, preferredSubCell, checkIfBlocker)) + if (preferredSubCell > SubCell.Any && !AnyUnitsAt(cell, preferredSubCell, checkIfBlocker)) return preferredSubCell; - if (!AnyUnitsAt(a)) + if (!AnyUnitsAt(cell)) return map.DefaultSubCell; for (var i = (int)SubCell.First; i < map.SubCellOffsets.Length; i++) - if (i != (int)preferredSubCell && !AnyUnitsAt(a, (SubCell)i, checkIfBlocker)) + if (i != (int)preferredSubCell && !AnyUnitsAt(cell, (SubCell)i, checkIfBlocker)) return (SubCell)i; return SubCell.Invalid; } @@ -325,7 +326,7 @@ namespace OpenRA.Traits } } - void RemoveInfluenceInner(ref InfluenceNode influenceNode, Actor toRemove) + static void RemoveInfluenceInner(ref InfluenceNode influenceNode, Actor toRemove) { if (influenceNode == null) return; diff --git a/OpenRA.Game/WPos.cs b/OpenRA.Game/WPos.cs index 024e6ab4df..c1dfc4babc 100644 --- a/OpenRA.Game/WPos.cs +++ b/OpenRA.Game/WPos.cs @@ -35,6 +35,9 @@ namespace OpenRA public static bool operator ==(WPos me, WPos other) { return me.X == other.X && me.Y == other.Y && me.Z == other.Z; } public static bool operator !=(WPos me, WPos other) { return !(me == other); } + /// + /// Returns the linear interpolation between points 'a' and 'b' + /// public static WPos Lerp(WPos a, WPos b, int mul, int div) { return a + (b - a) * mul / div; } public static WPos LerpQuadratic(WPos a, WPos b, WAngle pitch, int mul, int div) From 711ec0c600702944c6638e9dcdb8a55e8a2f49b9 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Fri, 17 Jul 2015 23:58:45 +0200 Subject: [PATCH 2/2] Minor pathfinder changes --- .../Pathfinder/BasePathSearch.cs | 2 +- .../Pathfinder/CellInfoLayerManager.cs | 2 +- OpenRA.Mods.Common/Pathfinder/Constants.cs | 2 ++ OpenRA.Mods.Common/Pathfinder/PathGraph.cs | 35 +++++++++---------- OpenRA.Mods.Common/Traits/World/PathFinder.cs | 2 +- 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs b/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs index bbb597841a..5ca616f2b6 100644 --- a/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs +++ b/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs @@ -81,7 +81,7 @@ namespace OpenRA.Mods.Common.Pathfinder if (string.IsNullOrEmpty(id)) { var builder = new StringBuilder(); - builder.Append(this.Graph.Actor.ActorID); + builder.Append(Graph.Actor.ActorID); while (!startPoints.Empty) { var startpoint = startPoints.Pop(); diff --git a/OpenRA.Mods.Common/Pathfinder/CellInfoLayerManager.cs b/OpenRA.Mods.Common/Pathfinder/CellInfoLayerManager.cs index 5badfe959e..a78a783cf1 100644 --- a/OpenRA.Mods.Common/Pathfinder/CellInfoLayerManager.cs +++ b/OpenRA.Mods.Common/Pathfinder/CellInfoLayerManager.cs @@ -97,7 +97,7 @@ namespace OpenRA.Mods.Common.Pathfinder { defaultCellInfoLayer = CellLayer.CreateInstance( - mpos => new CellInfo(int.MaxValue, int.MaxValue, mpos.ToCPos(map as Map), CellStatus.Unvisited), + mpos => new CellInfo(int.MaxValue, int.MaxValue, mpos.ToCPos(map), CellStatus.Unvisited), new Size(map.MapSize.X, map.MapSize.Y), map.TileShape); } diff --git a/OpenRA.Mods.Common/Pathfinder/Constants.cs b/OpenRA.Mods.Common/Pathfinder/Constants.cs index 57036a4116..f3e0573afa 100644 --- a/OpenRA.Mods.Common/Pathfinder/Constants.cs +++ b/OpenRA.Mods.Common/Pathfinder/Constants.cs @@ -25,5 +25,7 @@ namespace OpenRA.Mods.Common.Pathfinder /// a unit took to move one cell diagonally) /// public const int DiagonalCellCost = 177; + + public const int InvalidNode = int.MaxValue; } } diff --git a/OpenRA.Mods.Common/Pathfinder/PathGraph.cs b/OpenRA.Mods.Common/Pathfinder/PathGraph.cs index bdd1177b9c..4336184890 100644 --- a/OpenRA.Mods.Common/Pathfinder/PathGraph.cs +++ b/OpenRA.Mods.Common/Pathfinder/PathGraph.cs @@ -23,7 +23,7 @@ namespace OpenRA.Mods.Common.Pathfinder /// /// Gets all the Connections for a given node in the graph /// - ICollection GetConnections(CPos position); + IEnumerable GetConnections(CPos position); /// /// Retrieves an object given a node in the graph @@ -73,8 +73,6 @@ namespace OpenRA.Mods.Common.Pathfinder readonly MobileInfo mobileInfo; CellLayer cellInfo; - public const int InvalidNode = int.MaxValue; - public PathGraph(CellLayer cellInfo, MobileInfo mobileInfo, Actor actor, World world, bool checkForBlocked) { this.cellInfo = cellInfo; @@ -102,7 +100,7 @@ namespace OpenRA.Mods.Common.Pathfinder new[] { new CVec(1, -1), new CVec(1, 0), new CVec(-1, 1), new CVec(0, 1), new CVec(1, 1) }, }; - public ICollection GetConnections(CPos position) + public IEnumerable GetConnections(CPos position) { var previousPos = cellInfo[position].PreviousPos; @@ -116,7 +114,7 @@ namespace OpenRA.Mods.Common.Pathfinder { var neighbor = position + directions[i]; var movementCost = GetCostToNode(neighbor, directions[i]); - if (movementCost != InvalidNode) + if (movementCost != Constants.InvalidNode) validNeighbors.AddLast(new GraphConnection(position, neighbor, movementCost)); } @@ -127,17 +125,17 @@ namespace OpenRA.Mods.Common.Pathfinder { int movementCost; if (mobileInfo.CanEnterCell( - World as World, - Actor as Actor, + World, + Actor, destNode, out movementCost, - IgnoredActor as Actor, + IgnoredActor, checkConditions) && !(CustomBlock != null && CustomBlock(destNode))) { return CalculateCellCost(destNode, direction, movementCost); } - return InvalidNode; + return Constants.InvalidNode; } int CalculateCellCost(CPos neighborCPos, CVec direction, int movementCost) @@ -148,7 +146,13 @@ namespace OpenRA.Mods.Common.Pathfinder cellCost = (cellCost * 34) / 24; if (CustomCost != null) - cellCost += CustomCost(neighborCPos); + { + var customCost = CustomCost(neighborCPos); + if (customCost == Constants.InvalidNode) + return Constants.InvalidNode; + + cellCost += customCost; + } // directional bonuses for smoother flow! if (LaneBias != 0) @@ -184,15 +188,8 @@ namespace OpenRA.Mods.Common.Pathfinder public CellInfo this[CPos pos] { - get - { - return cellInfo[pos]; - } - - set - { - cellInfo[pos] = value; - } + get { return cellInfo[pos]; } + set { cellInfo[pos] = value; } } } } diff --git a/OpenRA.Mods.Common/Traits/World/PathFinder.cs b/OpenRA.Mods.Common/Traits/World/PathFinder.cs index 25d96aea35..70e71fcfc6 100644 --- a/OpenRA.Mods.Common/Traits/World/PathFinder.cs +++ b/OpenRA.Mods.Common/Traits/World/PathFinder.cs @@ -93,7 +93,7 @@ namespace OpenRA.Mods.Common.Traits // This assumes that the SubCell does not change during the path traversal var tilesInRange = world.Map.FindTilesInCircle(targetCell, range.Length / 1024 + 1) .Where(t => (world.Map.CenterOfCell(t) - target).LengthSquared <= range.LengthSquared - && mi.CanEnterCell(self.World as World, self as Actor, t)); + && mi.CanEnterCell(self.World, self, t)); // See if there is any cell within range that does not involve a cross-domain request // Really, we only need to check the circle perimeter, but it's not clear that would be a performance win