public static Node[][] LoadBrushFireJagged(GridPos user, GridPos[][] Grid, short MaxDistance = 22) { int GridX = Grid.Length; int GridY = Grid[0].Length; Node[][] grid = JaggedArrayExtensions.CreateJaggedArray <Node>(GridX, GridY); Node node = new Node(); if (grid[user.X][user.Y] == null) { grid[user.X][user.Y] = new Node(Grid[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(Grid[node.X][node.Y]); } grid[node.X][node.Y].Closed = true; // get neighbors of the current node List <Node> neighbors = GetNeighborsJagged(grid, node, Grid); 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; } 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); }
public static Node[,] LoadBrushFire(GridPos user, GridPos[,] mapGrid, short MaxDistance = 22) { Node[,] grid = new Node[mapGrid.GetLength(0), mapGrid.GetLength(1)]; 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]; var 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 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) { continue; } 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; } 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); }
public static List <Node> FindPathJagged(GridPos start, GridPos end, GridPos[][] Grid) { int GridX = Grid.Length; int GridY = Grid[0].Length; if (GridX <= start.X || GridY <= start.Y || start.X < 0 || start.Y < 0) { return(new List <Node>()); } Node node = new Node(); Node[][] grid = JaggedArrayExtensions.CreateJaggedArray <Node>(GridX, GridY); if (grid[start.X][start.Y] == null) { grid[start.X][start.Y] = new Node(Grid[start.X][start.Y]); } Node startingNode = grid[start.X][start.Y]; MinHeap path = new MinHeap(); // push the start node into the open list path.Push(startingNode); startingNode.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(Grid[node.X][node.Y]); } 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 = GetNeighborsJagged(grid, node, Grid); 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 <Node>()); }
public static List <Node> FindPath(GridPos start, GridPos end, GridPos[,] Grid) { if (Grid.GetLength(0) < start.X || Grid.GetLength(1) < start.Y || start.X < 0 || start.Y < 0) { return(new List <Node>()); } Node[,] grid = new Node[Grid.GetLength(0), Grid.GetLength(1)]; if (grid[start.X, start.Y] == null) { grid[start.X, start.Y] = new Node(Grid[start.X, start.Y]); } Node Start = grid[start.X, start.Y]; var 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 node = path.Pop(); if (grid[node.X, node.Y] == null) { grid[node.X, node.Y] = new Node(Grid[node.X, node.Y]); } 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, Grid); 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) { continue; } 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 <Node>()); }