Example #1
0
        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 neigbours 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);
        }
Example #2
0
        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[][] nodeGrid = JaggedArrayExtensions.CreateJaggedArray <Node>(gridX, gridY);
            if (nodeGrid[start.X][start.Y] == null)
            {
                nodeGrid[start.X][start.Y] = new Node(grid[start.X][start.Y]);
            }
            Node    startingNode = nodeGrid[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 node = path.Pop();
                if (nodeGrid[node.X][node.Y] == null)
                {
                    nodeGrid[node.X][node.Y] = new Node(grid[node.X][node.Y]);
                }
                nodeGrid[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(nodeGrid, 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>());
        }
Example #3
0
        public static List <Node> FindPath(GridPos start, GridPos end, GridPos[,] Grid)
        {
            Node node = new 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];
            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;

                //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)
                    {
                        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 #4
0
        public static Node[][] LoadBrushFireJagged(GridPos user, GridPos[][] grid, short maxDistance = 22)
        {
            int gridX = grid.Length;
            int gridY = grid[0].Length;

            Node[][] nodeGrid = JaggedArrayExtensions.CreateJaggedArray <Node>(gridX, gridY);

            if (nodeGrid[user.X][user.Y] == null)
            {
                nodeGrid[user.X][user.Y] = new Node(grid[user.X][user.Y]);
            }
            Node    start = nodeGrid[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 node = path.Pop();
                if (nodeGrid[node.X][node.Y] == null)
                {
                    nodeGrid[node.X][node.Y] = new Node(grid[node.X][node.Y]);
                }

                nodeGrid[node.X][node.Y].Closed = true;

                // get neighbors of the current node
                List <Node> neighbors = GetNeighborsJagged(nodeGrid, 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;
                            nodeGrid[neighbor.X][neighbor.Y].F = neighbor.F;
                        }

                        neighbor.Parent = node;

                        if (!neighbor.Opened)
                        {
                            path.Push(neighbor);
                            neighbor.Opened = true;
                        }
                        else
                        {
                            neighbor.Parent = node;
                        }
                    }
                }
            }
            return(nodeGrid);
        }