/// <summary>Gets a distances map for the specified target.</summary> /// <param name="target"></param> public Distance[,] GetDistances(Tile target) { #if DEBUG if (target == null) { throw new ArgumentNullException("target"); } #endif lock (locker) { Distance[,] distances; if (!m_Distances.TryGetValue(target, out distances)) { distances = Distances.Create(this); var queue = new Queue <Tile>(); queue.Enqueue(target); distances.Set(target, Distance.Zero); SetDistances(distances, queue, Distance.One); m_Distances[target] = distances; } return(distances); } }
public Distance[,] GetDistances(IEnumerable <Tile> targets, IEnumerable <Tile> enermies) { var distances = Distances.Create(this.Height, this.Width); var dis = Distance.Zero; var qOppo = new Queue <Tile>(); foreach (var tile in enermies.SelectMany(e => e.Targets).Where(t => t.IsPassable)) { qOppo.Enqueue(tile); distances.Set(tile, Distance.Blocked); } var queue = new Queue <Tile>(); foreach (var tile in targets) { queue.Enqueue(tile); distances.Set(tile, dis); } dis++; while (queue.Count > 0) { int sOppo = qOppo.Count; for (int i = 0; i < sOppo; i++) { var t = qOppo.Dequeue(); foreach (var n in t.Neighbors) { if (n.IsPassable && distances.Get(n) == Distance.Unknown) { qOppo.Enqueue(n); distances.Set(n, Distance.Blocked); } } } int size = queue.Count; for (int i = 0; i < size; i++) { var t = queue.Dequeue(); foreach (var n in t.Neighbors) { if (n.IsPassable && distances.Get(n) == Distance.Unknown) { queue.Enqueue(n); distances.Set(n, dis); } } } dis++; } return(distances); }
/// <summary>Gets a distances map for the specified targets.</summary> /// <param name="target"></param> public Distance[,] GetDistances(params Tile[] targets) { #if DEBUG if (targets == null || targets.Length == 0) { throw new ArgumentNullException("targets"); } #endif if (targets.Length == 1) { return(GetDistances(targets[0])); } var distances = Distances.Create(this); var queue = new Queue <Tile>(); foreach (var tile in targets) { queue.Enqueue(tile); distances.Set(tile, Distance.Zero); } SetDistances(distances, queue, Distance.One); return(distances); }