|
8 | 8 | */ |
9 | 9 | #endregion |
10 | 10 |
|
| 11 | +using System; |
11 | 12 | using System.Collections.Generic; |
12 | 13 | using System.Linq; |
13 | 14 | using OpenRA.Mods.Common; |
| 15 | +using OpenRA.Mods.Common.Pathfinder; |
14 | 16 | using OpenRA.Mods.Common.Traits; |
15 | 17 | using OpenRA.Traits; |
16 | 18 |
|
@@ -114,5 +116,213 @@ public static WPos PositionClosestTo_Old(this IEnumerable<WPos> positions, WPos |
114 | 116 | { |
115 | 117 | return positions.MinByOrDefault(p => (p - pos).LengthSquared); |
116 | 118 | } |
| 119 | + |
| 120 | + // Finds multiple distinct routes between source and target for a given locomotor. |
| 121 | + public static List<List<CPos>> FindDistinctRoutes( |
| 122 | + World world, |
| 123 | + Locomotor locomotor, |
| 124 | + CPos source, |
| 125 | + CPos target, |
| 126 | + int maxRoutes = 3, |
| 127 | + BlockedByActor check = BlockedByActor.None) |
| 128 | + { |
| 129 | + var routes = new List<List<CPos>>(); |
| 130 | + |
| 131 | + // Get the PathFinder trait from the world actor |
| 132 | + var pathFinder = world.WorldActor.TraitOrDefault<PathFinder>(); |
| 133 | + if (pathFinder == null) |
| 134 | + return routes; |
| 135 | + |
| 136 | + // Get the abstract graph data from the hierarchical pathfinder |
| 137 | + var (abstractGraph, abstractDomains) = pathFinder.GetOverlayDataForLocomotor(locomotor, check); |
| 138 | + if (abstractGraph == null || abstractDomains == null) |
| 139 | + return routes; |
| 140 | + |
| 141 | + // Map source and target to their abstract nodes |
| 142 | + var sourceAbstract = FindAbstractNodeForCell(source, abstractGraph, abstractDomains); |
| 143 | + var targetAbstract = FindAbstractNodeForCell(target, abstractGraph, abstractDomains); |
| 144 | + |
| 145 | + if (sourceAbstract == null || targetAbstract == null) |
| 146 | + return routes; |
| 147 | + |
| 148 | + // Check if source and target are in the same domain (connected) |
| 149 | + if (!abstractDomains.TryGetValue(sourceAbstract.Value, out var sourceDomain) || |
| 150 | + !abstractDomains.TryGetValue(targetAbstract.Value, out var targetDomain) || |
| 151 | + sourceDomain != targetDomain) |
| 152 | + return routes; |
| 153 | + |
| 154 | + // Track nodes that have been used in previous routes |
| 155 | + var excludedNodes = new HashSet<CPos>(); |
| 156 | + |
| 157 | + for (var i = 0; i < maxRoutes; i++) |
| 158 | + { |
| 159 | + var route = FindAbstractPath(sourceAbstract.Value, targetAbstract.Value, abstractGraph, excludedNodes); |
| 160 | + if (route == null || route.Count == 0) |
| 161 | + break; // No more valid routes available |
| 162 | + |
| 163 | + routes.Add(route); |
| 164 | + |
| 165 | + // Exclude all nodes in this route (except source and target) for future routes |
| 166 | + foreach (var node in route) |
| 167 | + { |
| 168 | + if (node != sourceAbstract.Value |
| 169 | + && node != targetAbstract.Value |
| 170 | + && node != route.ElementAtOrDefault(1) |
| 171 | + && node != route.ElementAtOrDefault(2) |
| 172 | + && node != route.ElementAtOrDefault(route.Count - 2)) |
| 173 | + excludedNodes.Add(node); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return routes; |
| 178 | + } |
| 179 | + |
| 180 | + // Finds the abstract node that corresponds to a given cell position. |
| 181 | + static CPos? FindAbstractNodeForCell( |
| 182 | + CPos cell, |
| 183 | + IReadOnlyDictionary<CPos, List<GraphConnection>> abstractGraph, |
| 184 | + IReadOnlyDictionary<CPos, uint> abstractDomains) |
| 185 | + { |
| 186 | + // If the cell itself is an abstract node, return it |
| 187 | + if (abstractDomains.ContainsKey(cell)) |
| 188 | + return cell; |
| 189 | + |
| 190 | + // Otherwise, find the nearest abstract node |
| 191 | + CPos? nearestNode = null; |
| 192 | + var minDistSq = int.MaxValue; |
| 193 | + |
| 194 | + foreach (var abstractNode in abstractDomains.Keys) |
| 195 | + { |
| 196 | + var distSq = (abstractNode - cell).LengthSquared; |
| 197 | + if (distSq < minDistSq) |
| 198 | + { |
| 199 | + minDistSq = distSq; |
| 200 | + nearestNode = abstractNode; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + return nearestNode; |
| 205 | + } |
| 206 | + |
| 207 | + // Performs A* pathfinding on the abstract graph to find a route from source to target, |
| 208 | + // avoiding any nodes in the excluded set. |
| 209 | + static List<CPos> FindAbstractPath( |
| 210 | + CPos source, |
| 211 | + CPos target, |
| 212 | + IReadOnlyDictionary<CPos, List<GraphConnection>> abstractGraph, |
| 213 | + HashSet<CPos> excludedNodes) |
| 214 | + { |
| 215 | + var openSet = new Dictionary<CPos, PathNode>(); |
| 216 | + var closedSet = new HashSet<CPos>(); |
| 217 | + |
| 218 | + // Initialize the starting node |
| 219 | + var startNode = new PathNode |
| 220 | + { |
| 221 | + Position = source, |
| 222 | + CostFromStart = 0, |
| 223 | + EstimatedTotalCost = Heuristic(source, target), |
| 224 | + Parent = null |
| 225 | + }; |
| 226 | + |
| 227 | + openSet[source] = startNode; |
| 228 | + |
| 229 | + while (openSet.Count > 0) |
| 230 | + { |
| 231 | + // Find the node in openSet with the lowest estimated total cost |
| 232 | + var current = openSet.Values.MinBy(n => n.EstimatedTotalCost); |
| 233 | + if (current == null) |
| 234 | + break; |
| 235 | + |
| 236 | + // Check if we've reached the target |
| 237 | + if (current.Position == target) |
| 238 | + return ReconstructPath(current); |
| 239 | + |
| 240 | + openSet.Remove(current.Position); |
| 241 | + closedSet.Add(current.Position); |
| 242 | + |
| 243 | + // Explore neighbors |
| 244 | + if (!abstractGraph.TryGetValue(current.Position, out var connections)) |
| 245 | + continue; |
| 246 | + |
| 247 | + foreach (var connection in connections) |
| 248 | + { |
| 249 | + var neighbor = connection.Destination; |
| 250 | + |
| 251 | + // Skip if already evaluated or excluded (unless it's the target) |
| 252 | + if (closedSet.Contains(neighbor) || (excludedNodes.Contains(neighbor) && neighbor != target)) |
| 253 | + continue; |
| 254 | + |
| 255 | + var newCost = current.CostFromStart + connection.Cost; |
| 256 | + |
| 257 | + // If this is a new node or we found a better path to it |
| 258 | + if (!openSet.TryGetValue(neighbor, out var neighborNode)) |
| 259 | + { |
| 260 | + neighborNode = new PathNode |
| 261 | + { |
| 262 | + Position = neighbor, |
| 263 | + CostFromStart = newCost, |
| 264 | + EstimatedTotalCost = newCost + Heuristic(neighbor, target), |
| 265 | + Parent = current |
| 266 | + }; |
| 267 | + openSet[neighbor] = neighborNode; |
| 268 | + } |
| 269 | + else if (newCost < neighborNode.CostFromStart) |
| 270 | + { |
| 271 | + neighborNode.CostFromStart = newCost; |
| 272 | + neighborNode.EstimatedTotalCost = newCost + Heuristic(neighbor, target); |
| 273 | + neighborNode.Parent = current; |
| 274 | + } |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + // No path found |
| 279 | + return null; |
| 280 | + } |
| 281 | + |
| 282 | + // Reconstructs the path by following parent pointers from target back to source. |
| 283 | + static List<CPos> ReconstructPath(PathNode targetNode) |
| 284 | + { |
| 285 | + var path = new List<CPos>(); |
| 286 | + var current = targetNode; |
| 287 | + |
| 288 | + while (current != null) |
| 289 | + { |
| 290 | + path.Add(current.Position); |
| 291 | + current = current.Parent; |
| 292 | + } |
| 293 | + |
| 294 | + path.Reverse(); |
| 295 | + return path; |
| 296 | + } |
| 297 | + |
| 298 | + // Simple heuristic function for A* pathfinding (Manhattan distance). |
| 299 | + static int Heuristic(CPos from, CPos to) |
| 300 | + { |
| 301 | + var delta = from - to; |
| 302 | + return Math.Abs(delta.X) + Math.Abs(delta.Y); |
| 303 | + } |
| 304 | + |
| 305 | + // Helper class to represent a node in the A* pathfinding algorithm. |
| 306 | + class PathNode |
| 307 | + { |
| 308 | + public CPos Position; |
| 309 | + public int CostFromStart; |
| 310 | + public int EstimatedTotalCost; |
| 311 | + public PathNode Parent; |
| 312 | + } |
| 313 | + |
| 314 | + public static bool PathExistsForLocomotor( |
| 315 | + World world, |
| 316 | + Locomotor locomotor, |
| 317 | + CPos source, |
| 318 | + CPos target) |
| 319 | + { |
| 320 | + var pathFinder = world.WorldActor.TraitOrDefault<IPathFinder>(); |
| 321 | + |
| 322 | + if (pathFinder == null) |
| 323 | + return false; |
| 324 | + |
| 325 | + return pathFinder.PathExistsForLocomotor(locomotor, source, target); |
| 326 | + } |
117 | 327 | } |
118 | 328 | } |
0 commit comments