Example #1
0
        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>());
        }
Example #2
0
        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);
        }
Example #3
0
 public Node(GridPos node)
 {
     Value = node.Value;
     X     = node.X;
     Y     = node.Y;
 }