Beispiel #1
0
        private void loadZone()
        {
            using (BinaryReader reader = new BinaryReader(new MemoryStream(Data)))
            {
                XLength = reader.ReadInt16();
                YLength = reader.ReadInt16();

                JaggedGrid = JaggedArrayExtensions.CreateJaggedArray <GridPos>(XLength, YLength);
                for (short i = 0; i < YLength; ++i)
                {
                    for (short t = 0; t < XLength; ++t)
                    {
                        JaggedGrid[t][i] = new GridPos
                        {
                            Value = reader.ReadByte(),
                            X     = t,
                            Y     = i,
                        };
                    }
                }
            }
        }
Beispiel #2
0
        private void LoadZone()
        {
            // TODO: Optimize
            using (Stream stream = new MemoryStream(Data))
            {
                const int numBytesToRead = 1;
                const int numBytesRead   = 0;
                byte[]    bytes          = new byte[numBytesToRead];

                byte[] xlength = new byte[2];
                byte[] ylength = new byte[2];
                stream.Read(bytes, numBytesRead, numBytesToRead);
                xlength[0] = bytes[0];
                stream.Read(bytes, numBytesRead, numBytesToRead);
                xlength[1] = bytes[0];
                stream.Read(bytes, numBytesRead, numBytesToRead);
                ylength[0] = bytes[0];
                stream.Read(bytes, numBytesRead, numBytesToRead);
                ylength[1] = bytes[0];
                YLength    = BitConverter.ToInt16(ylength, 0);
                XLength    = BitConverter.ToInt16(xlength, 0);

                Grid = JaggedArrayExtensions.CreateJaggedArray <GridPos>(XLength, YLength);
                for (short i = 0; i < YLength; ++i)
                {
                    for (short t = 0; t < XLength; ++t)
                    {
                        stream.Read(bytes, numBytesRead, numBytesToRead);
                        Grid[t][i] = new GridPos
                        {
                            Value = bytes[0],
                            X     = t,
                            Y     = i
                        };
                    }
                }
            }
        }
Beispiel #3
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>());
        }
Beispiel #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);
        }