]> Dogcows Code - chaz/carfire/blobdiff - CarFire/CarFire/CarFire/PathFinder.cs
Fixed path finder thrown exception when finding a path to the cell you are already at.
[chaz/carfire] / CarFire / CarFire / CarFire / PathFinder.cs
index 8b627ace0bed4f8500132ee33da9b6540615940b..c37e51863d7ad4b4abe9b18c707de8a8126fcaf8 100644 (file)
@@ -1,4 +1,8 @@
-using System;\r
+\r
+// Uncomment this to disable diagonal movemet.\r
+//#define ALLOW_DIAGONAL_MOVEMENT\r
+\r
+using System;\r
 using System.Collections.Generic;\r
 using System.Linq;\r
 using System.Text;\r
@@ -112,7 +116,7 @@ namespace CarFire
             mFringe = new BinaryHeap<Cell>();\r
             mCells = new Cell[mGridWidth, mGridHeight];\r
 \r
-            Cell startCell = new Cell(start, 0, GetManhattanDistance(start, finish));\r
+            Cell startCell = new Cell(start, 0, heuristic(start, finish));\r
             mFringe.Add(startCell);\r
             mCells[start.X, start.Y] = startCell;\r
             while (mFringe.Count > 0)\r
@@ -125,7 +129,7 @@ namespace CarFire
                     List<Point> list = new List<Point>();\r
 \r
                     cell = cell.Parent;\r
-                    while (cell.Point != start)\r
+                    while (cell != null && cell.Point != start)\r
                     {\r
                         list.Add(cell.Point);\r
                         cell = cell.Parent;\r
@@ -136,14 +140,16 @@ namespace CarFire
                 }\r
 \r
                 List<Point> neighbors = new List<Point>(8);\r
+                neighbors.Add(new Point(cell.Point.X, cell.Point.Y - 1));\r
+                neighbors.Add(new Point(cell.Point.X - 1, cell.Point.Y));\r
+                neighbors.Add(new Point(cell.Point.X + 1, cell.Point.Y));\r
+                neighbors.Add(new Point(cell.Point.X, cell.Point.Y + 1));\r
+#if ALLOW_DIAGONAL_MOVEMENT\r
                 neighbors.Add(new Point(cell.Point.X - 1, cell.Point.Y - 1));\r
-                neighbors.Add(new Point(cell.Point.X + 0, cell.Point.Y - 1));\r
                 neighbors.Add(new Point(cell.Point.X + 1, cell.Point.Y - 1));\r
-                neighbors.Add(new Point(cell.Point.X - 1, cell.Point.Y + 0));\r
-                neighbors.Add(new Point(cell.Point.X + 1, cell.Point.Y + 0));\r
                 neighbors.Add(new Point(cell.Point.X - 1, cell.Point.Y + 1));\r
-                neighbors.Add(new Point(cell.Point.X + 0, cell.Point.Y + 1));\r
                 neighbors.Add(new Point(cell.Point.X + 1, cell.Point.Y + 1));\r
+#endif\r
                 foreach (Point point in neighbors)\r
                 {\r
                     Cell inQueue = mCells[point.X, point.Y];\r
@@ -151,11 +157,11 @@ namespace CarFire
                     if (0 <= point.X && point.X < mGridWidth && 0 <= point.Y && point.Y < mGridHeight &&\r
                         mGrid[point.X, point.Y])\r
                     {\r
-                        int cost = cell.G + GetCost(cell.Point, point);\r
+                        int cost = cell.G + costFunction(cell.Point, point);\r
 \r
                         if (inQueue == null)\r
                         {\r
-                            Cell neighbor = new Cell(point, cost, GetManhattanDistance(point, finish), cell);\r
+                            Cell neighbor = new Cell(point, cost, heuristic(point, finish), cell);\r
                             mFringe.Add(neighbor);\r
                             mCells[point.X, point.Y] = neighbor;\r
                         }\r
This page took 0.022621 seconds and 4 git commands to generate.