#region Copyright & License Information /* * Copyright 2007-2014 The OpenRA Developers (see AUTHORS) * This file is part of OpenRA, which is free software. It is made * available to you under the terms of the GNU General Public License * as published by the Free Software Foundation. For more information, * see COPYING. */ #endregion using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using OpenRA; using OpenRA.Primitives; using OpenRA.Support; using OpenRA.Traits; namespace OpenRA.Mods.Common.Traits { [Desc("Calculates routes for mobile units based on the A* search algorithm.", " Attach this to the world actor.")] public class PathFinderInfo : ITraitInfo { public object Create(ActorInitializer init) { return new PathFinder(init.world); } } public class PathFinder { const int MaxPathAge = 50; /* x 40ms ticks */ static readonly List EmptyPath = new List(0); readonly World world; public PathFinder(World world) { this.world = world; } class CachedPath { public CPos From; public CPos To; public List Result; public int Tick; public Actor Actor; } List cachedPaths = new List(); public List FindUnitPath(CPos from, CPos target, Actor self) { using (new PerfSample("Pathfinder")) { var cached = cachedPaths.FirstOrDefault(p => p.From == from && p.To == target && p.Actor == self); if (cached != null) { Log.Write("debug", "Actor {0} asked for a path from {1} tick(s) ago", self.ActorID, world.WorldTick - cached.Tick); if (world.WorldTick - cached.Tick > MaxPathAge) cachedPaths.Remove(cached); return new List(cached.Result); } var mi = self.Info.Traits.Get(); // If a water-land transition is required, bail early var domainIndex = self.World.WorldActor.TraitOrDefault(); if (domainIndex != null) { var passable = mi.GetMovementClass(world.TileSet); if (!domainIndex.IsPassable(from, target, (uint)passable)) return EmptyPath; } var fromPoint = PathSearch.FromPoint(world, mi, self, target, from, true) .WithIgnoredActor(self); var fromPointReverse = PathSearch.FromPoint(world, mi, self, from, target, true) .WithIgnoredActor(self) .Reverse(); var pb = FindBidiPath( fromPoint, fromPointReverse); CheckSanePath2(pb, from, target); cachedPaths.RemoveAll(p => world.WorldTick - p.Tick > MaxPathAge); cachedPaths.Add(new CachedPath { From = from, To = target, Actor = self, Result = pb, Tick = world.WorldTick }); return new List(pb); } } public List FindUnitPathToRange(CPos src, SubCell srcSub, WPos target, WRange range, Actor self) { using (new PerfSample("Pathfinder")) { var mi = self.Info.Traits.Get(); var targetCell = self.World.Map.CellContaining(target); var rangeSquared = range.Range * range.Range; // Correct for SubCell offset target -= self.World.Map.OffsetOfSubCell(srcSub); // Select only the tiles that are within range from the requested SubCell // This assumes that the SubCell does not change during the path traversal var tilesInRange = world.Map.FindTilesInCircle(targetCell, range.Range / 1024 + 1) .Where(t => (world.Map.CenterOfCell(t) - target).LengthSquared <= rangeSquared && 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 var domainIndex = self.World.WorldActor.TraitOrDefault(); if (domainIndex != null) { var passable = mi.GetMovementClass(world.TileSet); tilesInRange = new List(tilesInRange.Where(t => domainIndex.IsPassable(src, t, (uint)passable))); if (!tilesInRange.Any()) return EmptyPath; } var path = FindBidiPath( PathSearch.FromPoints(world, mi, self, tilesInRange, src, true), PathSearch.FromPoint(world, mi, self, src, targetCell, true).Reverse()); return path; } } public List FindPath(PathSearch search) { using (new PerfSample("Pathfinder")) { using (search) { List path = null; while (!search.Queue.Empty) { var p = search.Expand(world); if (search.Heuristic(p) == 0) { path = MakePath(search.CellInfo, p); break; } } var dbg = world.WorldActor.TraitOrDefault(); if (dbg != null) dbg.AddLayer(search.Considered.Select(p => new Pair(p, search.CellInfo[p].MinCost)), search.MaxCost, search.Owner); if (path != null) return path; } // no path exists return EmptyPath; } } static List MakePath(CellLayer cellInfo, CPos destination) { var ret = new List(); var pathNode = destination; while (cellInfo[pathNode].Path != pathNode) { ret.Add(pathNode); pathNode = cellInfo[pathNode].Path; } ret.Add(pathNode); CheckSanePath(ret); return ret; } // Searches from both ends toward each other public List FindBidiPath(PathSearch fromSrc, PathSearch fromDest) { using (new PerfSample("Pathfinder")) { using (fromSrc) using (fromDest) { List path = null; while (!fromSrc.Queue.Empty && !fromDest.Queue.Empty) { /* make some progress on the first search */ var p = fromSrc.Expand(world); if (fromDest.CellInfo[p].Seen && fromDest.CellInfo[p].MinCost < float.PositiveInfinity) { path = MakeBidiPath(fromSrc, fromDest, p); break; } /* make some progress on the second search */ var q = fromDest.Expand(world); if (fromSrc.CellInfo[q].Seen && fromSrc.CellInfo[q].MinCost < float.PositiveInfinity) { path = MakeBidiPath(fromSrc, fromDest, q); break; } } var dbg = world.WorldActor.TraitOrDefault(); if (dbg != null) { dbg.AddLayer(fromSrc.Considered.Select(p => new Pair(p, fromSrc.CellInfo[p].MinCost)), fromSrc.MaxCost, fromSrc.Owner); dbg.AddLayer(fromDest.Considered.Select(p => new Pair(p, fromDest.CellInfo[p].MinCost)), fromDest.MaxCost, fromDest.Owner); } if (path != null) return path; } return EmptyPath; } } static List MakeBidiPath(PathSearch a, PathSearch b, CPos p) { var ca = a.CellInfo; var cb = b.CellInfo; var ret = new List(); var q = p; while (ca[q].Path != q) { ret.Add(q); q = ca[q].Path; } ret.Add(q); ret.Reverse(); q = p; while (cb[q].Path != q) { q = cb[q].Path; ret.Add(q); } CheckSanePath(ret); return ret; } [Conditional("SANITY_CHECKS")] static void CheckSanePath(List path) { if (path.Count == 0) return; var prev = path[0]; for (var i = 0; i < path.Count; i++) { var d = path[i] - prev; if (Math.Abs(d.X) > 1 || Math.Abs(d.Y) > 1) throw new InvalidOperationException("(PathFinder) path sanity check failed"); prev = path[i]; } } [Conditional("SANITY_CHECKS")] static void CheckSanePath2(List path, CPos src, CPos dest) { if (path.Count == 0) return; if (path[0] != dest) throw new InvalidOperationException("(PathFinder) sanity check failed: doesn't go to dest"); if (path[path.Count - 1] != src) throw new InvalidOperationException("(PathFinder) sanity check failed: doesn't come from src"); } } public struct CellInfo { public int MinCost; public CPos Path; public bool Seen; public CellInfo(int minCost, CPos path, bool seen) { MinCost = minCost; Path = path; Seen = seen; } } public struct PathDistance : IComparable { public readonly int EstTotal; public readonly CPos Location; public PathDistance(int estTotal, CPos location) { EstTotal = estTotal; Location = location; } public int CompareTo(PathDistance other) { return Math.Sign(EstTotal - other.EstTotal); } } }