// Methods public AStar(PathFinder.InspectAStarBlock CallBack, gMap argument) { this.Argument = argument; this.InspectBlockCallback = CallBack; this.AStack = new List<AStarRec>(); this.Path = new List<Point>(); }
/// <summary>Draws a map as a bitmap</summary> public static Bitmap DrawMap(gMap Map) { Bitmap Bmp = new Bitmap(Map.Width, Map.Height); Graphics gfx = Graphics.FromImage(Bmp); for (int x = 0; x < Map.Width; x++) { for (int y = 0; y < Map.Height; y++) { if (Map[x, y]) gfx.FillRectangle(new SolidBrush(Color.White), x, y, 1, 1); else gfx.FillRectangle(new SolidBrush(Color.Black), x, y, 1, 1); } } return Bmp; }
/// <summary>Find a path from a point on a map to another point</summary> /// <returns>Point[] of path</returns> public static Point[] GetPathFast(gMap Map, int X, int Y, int EndX, int EndY) { int NewWidth = (int)Math.Pow(2d, Math.Ceiling(Math.Log(Map.Width) / Math.Log(2d))); int NewHeight = (int)Math.Pow(2d, Math.Ceiling(Math.Log(Map.Height) / Math.Log(2d))); byte[,] Grid = new byte[NewWidth, NewHeight]; Grid.Initialize(); for (int x = 0; x < Map.Width; x++) { for (int y = 0; y < Map.Height; y++) { Grid[x, y] = Convert.ToByte(Map[x, y]); } } PathFinderFast PathFinder = new PathFinderFast(Grid); PathFinder.Formula = HeuristicFormula.Manhattan; PathFinder.Diagonals = true; PathFinder.HeavyDiagonals = false; PathFinder.HeuristicEstimate = 2; PathFinder.PunishChangeDirection = false; PathFinder.TieBreaker = false; PathFinder.SearchLimit = 50000; PathFinder.DebugProgress = false; PathFinder.ReopenCloseNodes = true; PathFinder.DebugFoundPath = true; List<PathFinderNode> path = PathFinder.FindPath(new Point(X, Y), new Point(EndX, EndY)); Point[] pointArray = new Point[path.Count]; for (int i = 0; i < pointArray.Length; i++) { pointArray[(pointArray.Length - i) - 1] = new Point(path[i].X, path[i].Y); } return pointArray; }
/// <summary>Find a path from a point on a map to another point</summary> /// <returns>Point[] of path</returns> public static Point[] GetPath(gMap Map, int X, int Y, int EndX, int EndY) { try { if (!Map[EndX, EndY]) { throw new ArgumentException(string.Format("The end coordinates are invalid ({0}, {1})", EndX, EndY), "EndX, EndY"); } if (!Map[X, Y]) { throw new ArgumentException(string.Format("The start coordinates are invalid ({0}, {1})", X, Y), "X, Y"); } AStar star = new AStar(delegate(int x, int y, int fromx, int fromy, gMap arg) { int num = 1; if (!arg[x, y]) { num = -1; } return num; }, Map); star.FindPath(new Point(X, Y), new Point(EndX, EndY), new Size(Map.Width, Map.Height), true); Point[] pointArray = new Point[star.PathRoute.Count]; for (int i = 0; i < pointArray.Length; i++) { pointArray[(pointArray.Length - i) - 1] = star.PathRoute[i]; } return pointArray; } catch (Exception exp) { throw exp; } }