public Pathfinder(int width, int height) { //use our own tiles independent of that game state Map = new MapTile[height, width]; for (int col = 0; col < width; col++) for (int row = 0; row < height; row++) Map[row, col] = new MapTile(col, row); }
//Key: u = unknown, d = dirt, w = water/wall public Map(int width, int height) { w = width; h = height; map = new MapTile[width, height]; //Whole map is unknown at creation for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { map[x, y] = new MapTile(); } } }
/// <summary> /// when the pathfinding has terminated this builds the path, returns the Route object and readies the map for the next pathfinding call /// </summary> /// <param name="end"></param> /// <param name="toReset"></param> /// <returns></returns> private Route BuildRoute(MapTile end, HashSet<MapTile> toReset) { //pointers are end -> begin so the path comes in reverse Location[] Path = new Location[end.CostKnown+1];//tiles in a path eqaul to the cost + 1 (hops between tile +1), use this to avoid having to reverse the path in O(n) MapTile current = end; while(current != null) { Path[current.CostKnown] = current.GetLocation; current = current.Parent; } Location first = Path[0]; foreach (MapTile t in toReset)//ready the map for next pathfinding call t.Reset(); return new Route(first, end.GetLocation, Path); }
public void Reset() { h = 10000; g = 10000; f = 10000; InOpen = false; Pi = null; }
/// <summary> /// relaxes the edge between two nodes, this needs to happen for all neighbours of source. /// </summary> /// <param name="source"></param> /// <param name="neighbour"></param> /// <param name="destination"></param> /// <returns></returns> private bool Relax(MapTile source, MapTile neighbour, MapTile destination) { float heuristic = Globals.state.GetDistance(neighbour.GetLocation, destination.GetLocation);//manhattan distance as heuristic bool betterRoute = neighbour.CostEstimate > source.CostKnown + 1 + heuristic; bool passable = Globals.state.GetIsPassable(neighbour.GetLocation); if (betterRoute && passable) { neighbour.CostKnown = source.CostKnown + 1; neighbour.CostEstimate = neighbour.CostKnown + heuristic; neighbour.Heuristic = heuristic; neighbour.Parent = source; return true; } return false; }