2 using System.Collections.Generic;
5 using System.Diagnostics;
6 using Microsoft.Xna.Framework;
11 /// A class to navigate from here to there through a grid of
12 /// open and closed cells.
14 public class PathFinder
19 /// The heuristic function should return some value representing
20 /// the distance between two points. A common approach is to
21 /// return the manhattan distance.
23 /// <param name="a">A point.</param>
24 /// <param name="b">The endpoint.</param>
25 /// <returns>The heuristic.</returns>
26 public delegate int Heuristic(Point a, Point b);
29 /// The cost function should take two points representing two
30 /// adjacent cells and return a cost measure of how expensive it
31 /// is to move from one cell to the other.
33 /// <param name="a">A point.</param>
34 /// <param name="b">Another point.</param>
35 /// <returns>The cost.</returns>
36 public delegate int CostFunction(Point a, Point b);
41 #region Public Methods
44 /// Construct a path finder with a grid. The grid is a matrix
45 /// of boolean values, true meaning the cell is walkable and false
46 /// meaning the cell is closed.
48 /// <param name="grid">The grid to find paths on.</param>
49 public PathFinder(bool[,] grid)
51 Debug.Assert(grid != null);
54 mGridWidth = mGrid.GetUpperBound(0) + 1;
55 mGridHeight = mGrid.GetUpperBound(1) + 1;
60 /// The A* algorithm for finding the best path through a grid of cells.
61 /// The manhattan distance heuristic and a simple distance-based cost
62 /// function will be used.
64 /// <param name="start">The cell to start at.</param>
65 /// <param name="finish">The desired destination.</param>
66 /// <returns>A list of points representing the path through the grid,
67 /// ends points not included, or null if no path could be found.</return>
68 public List<Point> GetPath(Point start, Point finish)
70 return GetPath(start, finish, GetManhattanDistance, GetCost);
74 /// The A* algorithm for finding the best path through a grid of cells.
75 /// A simple distance-based cost function will be used.
77 /// <param name="start">The cell to start at.</param>
78 /// <param name="finish">The desired destination.</param>
79 /// <param name="heuristic">The heuristic function.</param>
80 /// <returns>A list of points representing the path through the grid,
81 /// ends points not included, or null if no path could be found.</return>
82 public List<Point> GetPath(Point start, Point finish, Heuristic heuristic)
84 return GetPath(start, finish, heuristic, GetCost);
88 /// The A* algorithm for finding the best path through a grid of cells.
89 /// The manhattan distance heuristic will be used.
91 /// <param name="start">The cell to start at.</param>
92 /// <param name="finish">The desired destination.</param>
93 /// <param name="costFunction">The cost function</param>
94 /// <returns>A list of points representing the path through the grid,
95 /// ends points not included, or null if no path could be found.</return>
96 public List<Point> GetPath(Point start, Point finish, CostFunction costFunction)
98 return GetPath(start, finish, GetManhattanDistance, costFunction);
102 /// The A* algorithm for finding the best path through a grid of cells.
104 /// <param name="start">The cell to start at.</param>
105 /// <param name="finish">The desired destination.</param>
106 /// <param name="heuristic">The heuristic function.</param>
107 /// <param name="costFunction">The cost function.</param>
108 /// <returns>A list of points representing the path through the grid,
109 /// ends points not included, or null if no path could be found.</return>
110 public List<Point> GetPath(Point start, Point finish, Heuristic heuristic, CostFunction costFunction)
112 mFringe = new BinaryHeap<Cell>();
113 mCells = new Cell[mGridWidth, mGridHeight];
115 Cell startCell = new Cell(start, 0, GetManhattanDistance(start, finish));
116 mFringe.Add(startCell);
117 mCells[start.X, start.Y] = startCell;
118 while (mFringe.Count > 0)
120 Cell cell = mFringe.GetNext();
123 if (cell.Point == finish)
125 List<Point> list = new List<Point>();
128 while (cell.Point != start)
130 list.Add(cell.Point);
138 List<Point> neighbors = new List<Point>(8);
139 neighbors.Add(new Point(cell.Point.X - 1, cell.Point.Y - 1));
140 neighbors.Add(new Point(cell.Point.X + 0, cell.Point.Y - 1));
141 neighbors.Add(new Point(cell.Point.X + 1, cell.Point.Y - 1));
142 neighbors.Add(new Point(cell.Point.X - 1, cell.Point.Y + 0));
143 neighbors.Add(new Point(cell.Point.X + 1, cell.Point.Y + 0));
144 neighbors.Add(new Point(cell.Point.X - 1, cell.Point.Y + 1));
145 neighbors.Add(new Point(cell.Point.X + 0, cell.Point.Y + 1));
146 neighbors.Add(new Point(cell.Point.X + 1, cell.Point.Y + 1));
147 foreach (Point point in neighbors)
149 Cell inQueue = mCells[point.X, point.Y];
151 if (0 <= point.X && point.X < mGridWidth && 0 <= point.Y && point.Y < mGridHeight &&
152 mGrid[point.X, point.Y])
154 int cost = cell.G + GetCost(cell.Point, point);
158 Cell neighbor = new Cell(point, cost, GetManhattanDistance(point, finish), cell);
159 mFringe.Add(neighbor);
160 mCells[point.X, point.Y] = neighbor;
162 else if (inQueue.IsOpen && cost < inQueue.G)
165 inQueue.Parent = cell;
166 mFringe.Promote(inQueue);
177 /// Get the manhattan distance between two points. This is a simple but
178 /// effective and commonly-used heuristic.
180 /// <param name="a">A point.</param>
181 /// <param name="b">Another point.</param>
182 /// <returns>The manhattan distance.</returns>
183 public static int GetManhattanDistance(Point a, Point b)
193 /// Get the cost to travel from one point to another. This is a simple
194 /// cost function based purely on distance. On a square grid, diagonal
195 /// cells are further away than adjacent cells; therefore, adjacent moves
198 /// <param name="a">A point.</param>
199 /// <param name="b">Another point.</param>
200 /// <returns>The cost.</returns>
201 public static int GetCost(Point a, Point b)
203 if (a.X != b.X && a.Y != b.Y) return 14;
210 #region Private Types
212 class Cell : IComparable<Cell>
221 set { mG = value; mF = mG + mH; }
227 set { mH = value; mF = mG + mH; }
230 public int F { get { return mF; } }
233 public Cell(Point point, int g, int h)
242 public Cell(Point point, int g, int h, Cell parent)
252 public int CompareTo(Cell other)
266 #region Private Variables
272 IPriorityQueue<Cell> mFringe;