Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Initializing ...");

            CellGenerator cellGenerator = new CellGenerator();

            Console.WriteLine("Generating dungeons with 150 rooms ...");

            /**
             * =========================================================================
             *  STEP 1
             * =========================================================================
             */
            List <Cell> cells = cellGenerator.GenerateCellList(150, 50, 250, 750, 50, 1, 0.2);

            // Draw first step
            Console.WriteLine("Drawing first step to image ...");

            // Generate image with background
            Image    image = new Bitmap(CANVAS_SIZE_X, CANVAS_SIZE_Y);
            Graphics graph = Graphics.FromImage(image);

            graph.Clear(Color.White);

            // Ready pen and draw the cells
            Pen pen = new Pen(Brushes.Black);

            //foreach (Cell cell in cells) {
            foreach (Cell cell in cells)
            {
                DrawCube(ref graph, ref pen, cell, image.Width, image.Height);
            }

            Console.WriteLine("Image drawn. Saving ...");

            if (File.Exists("step1.png"))
            {
                File.Delete("step1.png");
                Console.WriteLine("Previous save file has been deleted.");
            }

            image.Save("step1.png", System.Drawing.Imaging.ImageFormat.Png);

            Console.WriteLine("Image has been saved as \"step1.png\"");

            /**
             * =========================================================================
             *  STEP 2
             * =========================================================================
             */
            //List<Cell> rearrangedCells = CellProcessor.FlockingSeparation(cells, cells.Count * 2);
            List <Cell> rearrangedCells;

            while (true)
            {
                try {
                    rearrangedCells = CellProcessor.FlockingSeparation(cells, cells.Count * 2);
                    break;
                } catch (OutOfIterationException exception) {
                    Console.WriteLine("WARNING: Separation iteration has been exhausted. " +
                                      "Iteration limit is " + exception.IterationNumber + ". Retrying with new dataset ...\n");
                    //cells = cellGenerator.GenerateCellList(150, 50, 250, 750);
                    cells = cellGenerator.GenerateCellList(150, 50, 250, 750, 1, 0.2);
                }
            }

            // Draw second step
            Console.WriteLine("Drawing second step to image ...");

            // Generate image with background
            image = new Bitmap(CANVAS_SIZE_X, CANVAS_SIZE_Y);
            graph = Graphics.FromImage(image);
            graph.Clear(Color.White);

            //foreach (Cell cell in cells) {
            foreach (Cell cell in rearrangedCells)
            {
                DrawCube(ref graph, ref pen, cell, image.Width, image.Height);
            }

            Console.WriteLine("Image drawn. Saving ...");

            if (File.Exists("step2.png"))
            {
                File.Delete("step2.png");
                Console.WriteLine("Previous save file has been deleted.");
            }

            image.Save("step2.png", System.Drawing.Imaging.ImageFormat.Png);

            Console.WriteLine("Image has been saved as \"step2.png\"");

            /**
             * =========================================================================
             *  STEP 3
             * =========================================================================
             */
            List <Cell> selectedCells = CellProcessor.TrimCells(rearrangedCells, 25);

            // Draw second step
            Console.WriteLine("Drawing third step to image ...");

            // Generate image with background
            image = new Bitmap(CANVAS_SIZE_X, CANVAS_SIZE_Y);
            graph = Graphics.FromImage(image);
            graph.Clear(Color.White);

            //foreach (Cell cell in cells) {
            foreach (Cell cell in selectedCells)
            {
                DrawCube(ref graph, ref pen, cell, image.Width, image.Height);
            }

            Console.WriteLine("Image drawn. Saving ...");

            if (File.Exists("step3.png"))
            {
                File.Delete("step3.png");
                Console.WriteLine("Previous save file has been deleted.");
            }

            image.Save("step3.png", System.Drawing.Imaging.ImageFormat.Png);

            Console.WriteLine("Image has been saved as \"step3.png\"");

            /**
             * =========================================================================
             *  STEP 4.1
             * =========================================================================
             */
            TunnelGenerator tunnelGenerator = new TunnelGenerator(selectedCells);

            tunnelGenerator.DelaunayTriangulation();
            List <Cell> triangulatedCells = tunnelGenerator.ExportCells();

            // Draw step
            Console.WriteLine("Drawing fourth step phase one to image ...");

            // Generate image with background
            image = new Bitmap(CANVAS_SIZE_X, CANVAS_SIZE_Y);
            graph = Graphics.FromImage(image);
            graph.Clear(Color.White);

            // Draw the boxes.
            foreach (Cell cell in selectedCells)
            {
                DrawCube(ref graph, ref pen, cell, image.Width, image.Height);
            }

            // Change pen color to difference the lines.
            pen = new Pen(Brushes.Red);

            foreach (Cell cell in triangulatedCells)
            {
                var foundCells = triangulatedCells.Where(o => cell != o && cell.ConnectedCell.Contains(o.GetHashCode()));
                foreach (Cell foundCell in foundCells)
                {
                    DrawLineFromCells(ref graph, ref pen, cell, foundCell, CANVAS_SIZE_X, CANVAS_SIZE_Y);
                }
            }

            Console.WriteLine("Image drawn. Saving ...");

            if (File.Exists("step4.1.png"))
            {
                File.Delete("step4.1.png");
                Console.WriteLine("Previous save file has been deleted.");
            }

            image.Save("step4.1.png", System.Drawing.Imaging.ImageFormat.Png);

            Console.WriteLine("Image has been saved as \"step4.1.png\"");

            /**
             * =========================================================================
             *  STEP 4.2
             * =========================================================================
             */
            tunnelGenerator.RemoveOverlapping();
            triangulatedCells = tunnelGenerator.ExportCells();

            // Draw step
            Console.WriteLine("Drawing fourth step phase two to image ...");

            // Generate image with background
            image = new Bitmap(CANVAS_SIZE_X, CANVAS_SIZE_Y);
            graph = Graphics.FromImage(image);
            graph.Clear(Color.White);

            pen = new Pen(Brushes.Black);

            // Draw the boxes.
            foreach (Cell cell in selectedCells)
            {
                DrawCube(ref graph, ref pen, cell, image.Width, image.Height);
            }

            // Change pen color to difference the lines.
            pen = new Pen(Brushes.Red);

            foreach (Cell cell in triangulatedCells)
            {
                var foundCells = triangulatedCells.Where(o => cell != o && cell.ConnectedCell.Contains(o.GetHashCode()));
                foreach (Cell foundCell in foundCells)
                {
                    DrawLineFromCells(ref graph, ref pen, cell, foundCell, CANVAS_SIZE_X, CANVAS_SIZE_Y);
                }
            }

            Console.WriteLine("Image drawn. Saving ...");

            if (File.Exists("step4.2.png"))
            {
                File.Delete("step4.2.png");
                Console.WriteLine("Previous save file has been deleted.");
            }

            image.Save("step4.2.png", System.Drawing.Imaging.ImageFormat.Png);

            Console.WriteLine("Image has been saved as \"step4.2.png\"");

            /**
             * =========================================================================
             *  STEP 4.3
             * =========================================================================
             */

            tunnelGenerator.TrimCellConnections();
            tunnelGenerator.TrimCellConnections();
            triangulatedCells = tunnelGenerator.ExportCells();

            // Draw step
            Console.WriteLine("Drawing fourth step phase three to image ...");

            // Generate image with background
            image = new Bitmap(CANVAS_SIZE_X, CANVAS_SIZE_Y);
            graph = Graphics.FromImage(image);
            graph.Clear(Color.White);

            pen = new Pen(Brushes.Black);

            // Draw the boxes.
            foreach (Cell cell in selectedCells)
            {
                DrawCube(ref graph, ref pen, cell, image.Width, image.Height);
            }

            // Change pen color to difference the lines.
            pen = new Pen(Brushes.Red);

            foreach (Cell cell in triangulatedCells)
            {
                var foundCells = triangulatedCells.Where(o => cell != o && cell.ConnectedCell.Contains(o.GetHashCode()));
                foreach (Cell foundCell in foundCells)
                {
                    DrawLineFromCells(ref graph, ref pen, cell, foundCell, CANVAS_SIZE_X, CANVAS_SIZE_Y);
                }
            }

            Console.WriteLine("Image drawn. Saving ...");

            if (File.Exists("step4.3.png"))
            {
                File.Delete("step4.3.png");
                Console.WriteLine("Previous save file has been deleted.");
            }

            image.Save("step4.3.png", System.Drawing.Imaging.ImageFormat.Png);

            Console.WriteLine("Image has been saved as \"step4.3.png\"");

            /**
             * =========================================================================
             *  STEP 4.5
             * =========================================================================
             */
            //tunnelGenerator.RemoveOverlapping();
            //triangulatedCells = tunnelGenerator.ExportCells();
            List <Tunnel> tunnels = tunnelGenerator.GenerateTunnel();


            // Draw step
            Console.WriteLine("Drawing fourth step phase five to image ...");

            // Generate image with background
            image = new Bitmap(CANVAS_SIZE_X, CANVAS_SIZE_Y);
            graph = Graphics.FromImage(image);
            graph.Clear(Color.White);

            pen = new Pen(Brushes.Black);

            // Draw the boxes.
            foreach (Cell cell in selectedCells)
            {
                DrawCube(ref graph, ref pen, cell, image.Width, image.Height);
            }

            // Change pen color to difference the lines.
            pen = new Pen(Brushes.Blue);

            // TODO: It sometimes break for some reason.
            foreach (Tunnel tunnel in tunnels)
            {
                var a = triangulatedCells.Single(cell => cell.GetHashCode() == tunnel.CellHashA).LocationCenter;
                var b = triangulatedCells.Single(cell => cell.GetHashCode() == tunnel.CellHashB).LocationCenter;
                DrawLineFromPoints(ref graph, ref pen, a, tunnel.AnglePoint.First(), CANVAS_SIZE_X, CANVAS_SIZE_Y);
                DrawLineFromPoints(ref graph, ref pen, b, tunnel.AnglePoint.Last(), CANVAS_SIZE_X, CANVAS_SIZE_Y);
            }

            Console.WriteLine("Image drawn. Saving ...");

            if (File.Exists("step4.5.png"))
            {
                File.Delete("step4.5.png");
                Console.WriteLine("Previous save file has been deleted.");
            }

            image.Save("step4.5.png", System.Drawing.Imaging.ImageFormat.Png);

            Console.WriteLine("Image has been saved as \"step4.5.png\"");

            /**
             * =========================================================================
             *  STEP 5
             * =========================================================================
             */
            //var gridProcessor = new GridConverterAsync(triangulatedCells, tunnels, 200, 200);
            var gridProcessor = new GridConverter(triangulatedCells, tunnels, 150, 150);

            //var tunnelLayerTask = gridProcessor.CreateTunnelLayer();
            //var cellWallLayerTask = gridProcessor.CreateCellWallLayer();
            //var cellLayerTask = gridProcessor.CreateCellLayer();

            //tunnelLayerTask.Wait();
            //cellWallLayerTask.Wait();
            //cellLayerTask.Wait();

            //var tunnelLayer = tunnelLayerTask.Result;
            //var cellWallLayer = cellWallLayerTask.Result;
            //var cellLayer = cellLayerTask.Result;

            //var tunnelLayer = gridProcessor.CreateTunnelLayer();
            var tunnelLayer   = gridProcessor.CreateTunnelLayerSimple();
            var cellWallLayer = gridProcessor.CreateCellWallLayer();
            //var cellWallLayer = gridProcessor.CreateCellWallLayerSimple();
            var cellLayer       = gridProcessor.CreateCellLayer();
            var connectionLayer = GridConverter.IntersectGrid(tunnelLayer, cellWallLayer,
                                                              procedural_dungeon_generator.Common.BlockType.RoomConnector);

            //var gridResult = GridConverterAsync.MergeGrid(tunnelLayer,
            //    cellLayer);
            //gridResult = GridConverterAsync.MergeGrid(gridResult, cellWallLayer);
            //gridResult = GridConverterAsync.GenerateConnections(gridResult);
            var gridResult = GridConverter.MergeGrid(tunnelLayer,
                                                     cellLayer);

            gridResult = GridConverter.MergeGrid(gridResult, cellWallLayer);
            //gridResult = GridConverter.GenerateConnections(gridResult);
            gridResult = GridConverter.MergeGrid(gridResult, connectionLayer);
            gridResult = GridConverter.VerifyConnections(gridResult);


            // Draw step
            Console.WriteLine("Drawing fifth step to image ...");

            // Generate image with background
            image = new Bitmap(CANVAS_SIZE_X, CANVAS_SIZE_Y);
            graph = Graphics.FromImage(image);
            graph.Clear(Color.White);

            pen = new Pen(Brushes.Black);

            // Get block size.
            int blockWidth  = CANVAS_SIZE_X / gridResult.Width;
            int blockHeight = CANVAS_SIZE_Y / gridResult.Height;

            // Draw the boxes.
            for (int width = 0; width < gridResult.Width; width++)
            {
                for (int height = 0; height < gridResult.Height; height++)
                {
                    // Draw the colored boxes first.
                    switch (gridResult[width, height].Type)
                    {
                    case procedural_dungeon_generator.Common.BlockType.Room:
                        FillCube(ref graph, Brushes.DarkGreen,
                                 new Cell(blockWidth - 2, blockHeight - 2,
                                          width * blockWidth + 1, height * blockHeight + 1));
                        break;

                    case procedural_dungeon_generator.Common.BlockType.Tunnel:
                        FillCube(ref graph, Brushes.DarkGray,
                                 new Cell(blockWidth - 2, blockHeight - 2,
                                          width * blockWidth + 1, height * blockHeight + 1));
                        break;

                    case procedural_dungeon_generator.Common.BlockType.RoomWall:
                        FillCube(ref graph, Brushes.Green,
                                 new Cell(blockWidth - 2, blockHeight - 2,
                                          width * blockWidth + 1, height * blockHeight + 1));
                        break;

                    case procedural_dungeon_generator.Common.BlockType.RoomConnector:
                        FillCube(ref graph, Brushes.GreenYellow,
                                 new Cell(blockWidth - 2, blockHeight - 2,
                                          width * blockWidth + 1, height * blockHeight + 1));
                        break;

                    default: break;
                    }

                    // Then draw the box.
                    DrawCubeNoText(ref graph, ref pen, new Cell(blockWidth, blockHeight,
                                                                width * blockWidth, height * blockHeight));
                }
            }

            Console.WriteLine("Image drawn. Saving ...");

            if (File.Exists("step5.png"))
            {
                File.Delete("step5.png");
                Console.WriteLine("Previous save file has been deleted.");
            }

            image.Save("step5.png", System.Drawing.Imaging.ImageFormat.Png);

            Console.WriteLine("Image has been saved as \"step5.png\"");

            Console.WriteLine("\n\nDebug Log:\n");
            triangulatedCells.ForEach(o => Console.WriteLine($"{o}"));
        }