public Node(GridPos node) { Value = node.Value; X = node.X; Y = node.Y; }
public static List <GridPos> findPath(GridPos start, GridPos end, GridPos[,] Grid) { Node[,] grid = new Node[Grid.GetLength(0), Grid.GetLength(1)]; for (short y = 0; y < grid.GetLength(1); y++) { for (short x = 0; x < grid.GetLength(0); x++) { grid[x, y] = new Node() { Value = Grid[x, y].Value, X = x, Y = y }; } } Node node = new Node(); Node Start = grid[start.X, start.Y]; MinHeap path = new MinHeap(); // push the start node into the open list path.Push(Start); Start.Opened = true; // while the open list is not empty while (path.Count > 0) { // pop the position of node which has the minimum `f` value. node = path.Pop(); Grid[node.X, node.Y].Closed = true; //if reached the end position, construct the path and return it if (node.X == end.X && node.Y == end.Y) { return(Backtrace(node)); } // get neigbours of the current node List <Node> neighbors = GetNeighbors(grid, node); for (int i = 0, l = neighbors.Count(); i < l; ++i) { Node neighbor = neighbors[i]; if (neighbor.Closed) { continue; } // check if the neighbor has not been inspected yet, or can be reached with // smaller cost from the current node if (!neighbor.Opened) { if (neighbor.F == 0) { neighbor.F = Heuristic.Octile(Math.Abs(neighbor.X - end.X), Math.Abs(neighbor.Y - end.Y)); } neighbor.Parent = node; if (!neighbor.Opened) { path.Push(neighbor); neighbor.Opened = true; } else { neighbor.Parent = node; } } } } return(new List <GridPos>()); }
public static Node[,] LoadBrushFire(GridPos user, GridPos[,] mapGrid, short MaxDistance = 22) { Node[,] grid = new Node[mapGrid.GetLength(0), mapGrid.GetLength(1)]; Node node = new Node(); if (grid[user.X, user.Y] == null) { grid[user.X, user.Y] = new Node(mapGrid[user.X, user.Y]); } Node Start = grid[user.X, user.Y]; MinHeap path = new MinHeap(); // push the start node into the open list path.Push(Start); Start.Opened = true; // while the open list is not empty while (path.Count > 0) { // pop the position of node which has the minimum `f` value. node = path.Pop(); if (grid[node.X, node.Y] == null) { grid[node.X, node.Y] = new Node(mapGrid[node.X, node.Y]); } grid[node.X, node.Y].Closed = true; // get neighbors of the current node List <Node> neighbors = GetNeighbors(grid, node, mapGrid); for (int i = 0, l = neighbors.Count; i < l; ++i) { Node neighbor = neighbors[i]; if (neighbor.Closed) { continue; } // check if the neighbor has not been inspected yet, or can be reached with // smaller cost from the current node if (!neighbor.Opened) { if (neighbor.F == 0) { double distance = Heuristic.Octile(Math.Abs(neighbor.X - node.X), Math.Abs(neighbor.Y - node.Y)) + node.F; if (distance > MaxDistance) { neighbor.Value = 1; continue; } else { neighbor.F = distance; } grid[neighbor.X, neighbor.Y].F = neighbor.F; } neighbor.Parent = node; if (!neighbor.Opened) { path.Push(neighbor); neighbor.Opened = true; } else { neighbor.Parent = node; } } } } return(grid); }