示例#1
0
        public static IGeneratorRunner GetGeneratorRunnerFactory(
            GeneratorInput <IMapDescription <int> > input)
        {
            var layoutDrawer = new SVGLayoutDrawer <int>();

            var dungeonGeneratorInput = (DungeonGeneratorInput <int>)input;
            var layoutGenerator       = new DungeonGenerator <int>(input.MapDescription, dungeonGeneratorInput.Configuration);

            layoutGenerator.InjectRandomGenerator(new Random(0));

            return(new LambdaGeneratorRunner(() =>
            {
                var simulatedAnnealingArgsContainer = new List <SimulatedAnnealingEventArgs>();
                void SimulatedAnnealingEventHandler(object sender, SimulatedAnnealingEventArgs eventArgs)
                {
                    simulatedAnnealingArgsContainer.Add(eventArgs);
                }

                layoutGenerator.OnSimulatedAnnealingEvent += SimulatedAnnealingEventHandler;
                var layout = layoutGenerator.GenerateLayout();
                layoutGenerator.OnSimulatedAnnealingEvent -= SimulatedAnnealingEventHandler;

                var additionalData = new AdditionalRunData()
                {
                    SimulatedAnnealingEventArgs = simulatedAnnealingArgsContainer,
                    GeneratedLayoutSvg = layoutDrawer.DrawLayout(layout, 800, forceSquare: true),
                    // GeneratedLayout = layout,
                };

                var generatorRun = new GeneratorRun <AdditionalRunData>(layout != null, layoutGenerator.TimeTotal, layoutGenerator.IterationsCount, additionalData);

                return generatorRun;
            }));
        }
示例#2
0
        public static void CompareWithReference()
        {
            var inputs = new List <DungeonGeneratorInput <int> >();

            inputs.AddRange(Program.GetMapDescriptionsSet(new Vector2Int(1, 1), false, null, true));
            //inputs.AddRange(Program.GetMapDescriptionsSet(new IntVector2(1, 1), true, new List<int>() { 2 }, true));
            //inputs.AddRange(Program.GetMapDescriptionsSet(new IntVector2(1, 1), true, new List<int>() { 2 }, false));
            //inputs.AddRange(GetMapDescriptionsSet(new IntVector2(1, 1), true, new List<int>() { 2, 4, 6, 8 }, false));
            //inputs.AddRange(GetMapDescriptionsSet(new IntVector2(1, 1), true, new List<int>() { 2, 4, 6 }, false));
            //inputs.AddRange(GetMapDescriptionsSet(new IntVector2(1, 1), true, new List<int>() { 2, 4 }, false));
            //inputs.AddRange(GetMapDescriptionsSet(new IntVector2(1, 1), true, new List<int>() { 2 }, false));

            if (true)
            {
                inputs.Sort((x1, x2) => string.Compare(x1.Name, x2.Name, StringComparison.Ordinal));
            }

            var layoutDrawer = new SVGLayoutDrawer <int>();

            var benchmarkRunner   = new BenchmarkRunner <IMapDescription <int> >();
            var benchmarkScenario = new BenchmarkScenario <IMapDescription <int> >("CorridorConfigurationSpaces", input =>
            {
                var dungeonGeneratorInput = (DungeonGeneratorInput <int>)input;
                var layoutGenerator       = new DungeonGenerator <int>(input.MapDescription, dungeonGeneratorInput.Configuration);
                layoutGenerator.InjectRandomGenerator(new Random(0));

                //return new LambdaGeneratorRunner(() =>
                //{
                //    var layouts = layoutGenerator.GenerateLayout();

                //    return new GeneratorRun(layouts != null, layoutGenerator.TimeTotal, layoutGenerator.IterationsCount);
                //});
                return(new LambdaGeneratorRunner(() =>
                {
                    var simulatedAnnealingArgsContainer = new List <SimulatedAnnealingEventArgs>();
                    void SimulatedAnnealingEventHandler(object sender, SimulatedAnnealingEventArgs eventArgs)
                    {
                        simulatedAnnealingArgsContainer.Add(eventArgs);
                    }

                    layoutGenerator.OnSimulatedAnnealingEvent += SimulatedAnnealingEventHandler;
                    var layout = layoutGenerator.GenerateLayout();
                    layoutGenerator.OnSimulatedAnnealingEvent -= SimulatedAnnealingEventHandler;

                    var additionalData = new AdditionalRunData <int>()
                    {
                        SimulatedAnnealingEventArgs = simulatedAnnealingArgsContainer,
                        GeneratedLayoutSvg = layoutDrawer.DrawLayout(layout, 800, forceSquare: true),
                        // GeneratedLayout = layout,
                    };

                    var generatorRun = new GeneratorRun <AdditionalRunData <int> >(layout != null, layoutGenerator.TimeTotal, layoutGenerator.IterationsCount, additionalData);

                    return generatorRun;
                }));
            });
        public IGeneratorRunner GetGeneratorRunner(LevelDescriptionGrid2D <TNode> levelDescription)
        {
            if (benchmarkInitialization)
            {
                return(GetGeneratorRunnerWithInit(levelDescription));
            }

            var configuration = this.configuration.SmartClone();

            configuration.RoomsCanTouch = levelDescription.MinimumRoomDistance == 0;

            var mapDescription = levelDescription.GetMapDescription();

            var chainDecompositionOld = new BreadthFirstChainDecompositionOld <TNode>();
            var chainDecomposition    = new TwoStageChainDecomposition <TNode>(mapDescription, chainDecompositionOld);

            configuration.Chains = chainDecomposition.GetChains(levelDescription.GetGraph());
            configuration.SimulatedAnnealingConfiguration = new SimulatedAnnealingConfigurationProvider(new SimulatedAnnealingConfiguration()
            {
                MaxIterationsWithoutSuccess = 10000,
            });

            var layoutDrawer = new SVGLayoutDrawer <TNode>();

            var layoutGenerator = new DungeonGenerator <TNode>(mapDescription, configuration);

            layoutGenerator.InjectRandomGenerator(new Random(0));

            return(new LambdaGeneratorRunner(() =>
            {
                var simulatedAnnealingArgsContainer = new List <SimulatedAnnealingEventArgs>();

                void SimulatedAnnealingEventHandler(object sender, SimulatedAnnealingEventArgs eventArgs)
                {
                    simulatedAnnealingArgsContainer.Add(eventArgs);
                }

                layoutGenerator.OnSimulatedAnnealingEvent += SimulatedAnnealingEventHandler;
                var layout = layoutGenerator.GenerateLayout();
                layoutGenerator.OnSimulatedAnnealingEvent -= SimulatedAnnealingEventHandler;

                var additionalData = new AdditionalRunData <TNode>()
                {
                    SimulatedAnnealingEventArgs = simulatedAnnealingArgsContainer,
                    GeneratedLayoutSvg =
                        layout != null ? layoutDrawer.DrawLayout(layout, 800, forceSquare: true) : null,
                    GeneratedLayout = layout,
                };

                var generatorRun = new GeneratorRun <AdditionalRunData <TNode> >(layout != null, layoutGenerator.TimeTotal,
                                                                                 layoutGenerator.IterationsCount, additionalData);

                return generatorRun;
            }));
        }
示例#4
0
        private IGeneratorRunner GetGeneratorRunnerWithInit(LevelDescriptionGrid2D <TNode> levelDescription)
        {
            var configuration = this.configuration.SmartClone();

            configuration.RoomsCanTouch = levelDescription.MinimumRoomDistance == 0;

            configuration.RepeatModeOverride = levelDescription.RoomTemplateRepeatModeOverride;
            if (levelDescription.RoomTemplateRepeatModeDefault.HasValue)
            {
                throw new ArgumentException("Default repeat mode not supported");
            }

            var layoutDrawer  = new SVGLayoutDrawer <TNode>();
            var seedGenerator = new Random();

            var mapDescription = levelDescription.GetMapDescription();

            return(new LambdaGeneratorRunner(() =>
            {
                var stopwatch = new Stopwatch();
                stopwatch.Start();

                var layoutGenerator = new DungeonGenerator <TNode>(mapDescription, configuration);
                layoutGenerator.InjectRandomGenerator(new Random(seedGenerator.Next()));

                var simulatedAnnealingArgsContainer = new List <SimulatedAnnealingEventArgs>();

                void SimulatedAnnealingEventHandler(object sender, SimulatedAnnealingEventArgs eventArgs)
                {
                    simulatedAnnealingArgsContainer.Add(eventArgs);
                }

                layoutGenerator.OnSimulatedAnnealingEvent += SimulatedAnnealingEventHandler;
                var layout = layoutGenerator.GenerateLayout();
                layoutGenerator.OnSimulatedAnnealingEvent -= SimulatedAnnealingEventHandler;

                stopwatch.Stop();

                var additionalData = new AdditionalRunData <TNode>()
                {
                    SimulatedAnnealingEventArgs = simulatedAnnealingArgsContainer,
                    GeneratedLayoutSvg =
                        layout != null ? layoutDrawer.DrawLayout(layout, 800, forceSquare: true) : null,
                    GeneratedLayout = layout,
                };

                var generatorRun = new GeneratorRun <AdditionalRunData <TNode> >(layout != null, stopwatch.ElapsedMilliseconds,
                                                                                 layoutGenerator.IterationsCount, additionalData);

                return generatorRun;
            }));
        }
示例#5
0
        private IGeneratorRunner GetGeneratorRunnerWithInit(LevelDescriptionGrid2D <TNode> levelDescription)
        {
            var configuration = this.configuration.SmartClone();

            configuration.RoomsCanTouch = levelDescription.MinimumRoomDistance == 0;

            var layoutDrawer  = new SVGLayoutDrawer <TNode>();
            var seedGenerator = new Random();

            return(new LambdaGeneratorRunner(() =>
            {
                var stopwatch = new Stopwatch();
                stopwatch.Start();

                var layoutGenerator = new GraphBasedGeneratorGrid2D <TNode>(levelDescription, configuration);
                layoutGenerator.InjectRandomGenerator(new Random(seedGenerator.Next()));

                var simulatedAnnealingArgsContainer = new List <SimulatedAnnealingEventArgs>();

                void SimulatedAnnealingEventHandler(object sender, SimulatedAnnealingEventArgs eventArgs)
                {
                    simulatedAnnealingArgsContainer.Add(eventArgs);
                }

                layoutGenerator.OnSimulatedAnnealingEvent += SimulatedAnnealingEventHandler;
                var layout = layoutGenerator.GenerateLayout();
                layoutGenerator.OnSimulatedAnnealingEvent -= SimulatedAnnealingEventHandler;

                stopwatch.Stop();

                var additionalData = new AdditionalRunData <TNode>()
                {
                    SimulatedAnnealingEventArgs = simulatedAnnealingArgsContainer,
                    //GeneratedLayoutSvg =
                    //    layout != null ? layoutDrawer.DrawLayout(layout, 800, forceSquare: true) : null,
                    // GeneratedLayout = layout,
                };

                var generatorRun = new GeneratorRun <AdditionalRunData <TNode> >(layout != null, stopwatch.ElapsedMilliseconds,
                                                                                 layoutGenerator.IterationsCount, additionalData);

                return generatorRun;
            }));
        }
示例#6
0
        public void Run()
        {
            // Create map description
            var mapDescription = new CorridorsExample().GetMapDescription();

            // Instantiate dungeon generator
            var generator = new DungeonGenerator <int>(mapDescription);

            generator.InjectRandomGenerator(new Random(0));

            // Generate level
            var layout = generator.GenerateLayout();

            // Export to svg and save to file
            var svgDrawer = new SVGLayoutDrawer <int>();
            var svg       = svgDrawer.DrawLayout(layout, 800, forceSquare: true);

            File.WriteAllText("generated_level.svg", svg);

            // Export to json and save to file
            var json = JsonConvert.SerializeObject(layout, Formatting.Indented);

            File.WriteAllText("generated_level.json", json);
        }
示例#7
0
        public void Run()
        {
            var rectangularRoomTemplates = MapDescriptionUtils.GetRectangularRoomTemplates(new Vector2Int(1, 1));
            var basicRoomDescription     = new BasicRoomDescription(rectangularRoomTemplates);

            var inputs = new List <DungeonGeneratorInput <int> >();

            inputs.AddRange(Program.GetMapDescriptionsSet(new Vector2Int(1, 1), false, null, true));
            inputs.AddRange(Program.GetMapDescriptionsSet(new Vector2Int(1, 1), false, null, true, basicRoomDescription: basicRoomDescription, suffix: "rect shapes"));
            inputs.AddRange(Program.GetMapDescriptionsSet(new Vector2Int(1, 1), true, new List <int>()
            {
                2
            }, false));

            inputs.Add(LoadInput("gungeon_1_1"));
            inputs.Add(LoadInput("gungeon_1_1", true));
            inputs.Add(LoadInput("gungeon_1_2"));
            inputs.Add(LoadInput("gungeon_1_2", true));
            inputs.Add(LoadInput("gungeon_2_1"));
            inputs.Add(LoadInput("gungeon_2_1", true));
            inputs.Add(LoadInput("gungeon_2_2"));
            inputs.Add(LoadInput("gungeon_2_2", true));

            foreach (var input in inputs)
            {
                var mapDescription = input.MapDescription;
                var graph          = mapDescription.GetStageOneGraph();
                Console.WriteLine($"{input.Name} {graph.VerticesCount} {graph.Edges.Count()}");
            }

            if (true)
            {
                inputs.Sort((x1, x2) => string.Compare(x1.Name, x2.Name, StringComparison.Ordinal));
            }

            inputs = inputs.Where(x => !x.Name.StartsWith("Example 4")).ToList();

            var layoutDrawer = new SVGLayoutDrawer <int>();

            var benchmarkRunner   = new BenchmarkRunner <IMapDescription <int> >();
            var benchmarkScenario = new BenchmarkScenario <IMapDescription <int> >("CorridorConfigurationSpaces", input =>
            {
                var dungeonGeneratorInput = (DungeonGeneratorInput <int>)input;
                var layoutGenerator       = new DungeonGenerator <int>(input.MapDescription, dungeonGeneratorInput.Configuration);
                layoutGenerator.InjectRandomGenerator(new Random(0));

                return(new LambdaGeneratorRunner(() =>
                {
                    var simulatedAnnealingArgsContainer = new List <SimulatedAnnealingEventArgs>();
                    void SimulatedAnnealingEventHandler(object sender, SimulatedAnnealingEventArgs eventArgs)
                    {
                        simulatedAnnealingArgsContainer.Add(eventArgs);
                    }

                    layoutGenerator.OnSimulatedAnnealingEvent += SimulatedAnnealingEventHandler;
                    var layout = layoutGenerator.GenerateLayout();
                    layoutGenerator.OnSimulatedAnnealingEvent -= SimulatedAnnealingEventHandler;

                    var additionalData = new AdditionalRunData()
                    {
                        SimulatedAnnealingEventArgs = simulatedAnnealingArgsContainer,
                        GeneratedLayoutSvg = layoutDrawer.DrawLayout(layout, 800, forceSquare: true),
                        GeneratedLayout = layout,
                    };

                    var generatorRun = new GeneratorRun <AdditionalRunData>(layout != null, layoutGenerator.TimeTotal, layoutGenerator.IterationsCount, additionalData);

                    return generatorRun;
                }));
            });
示例#8
0
        public void Run()
        {
            var inputs = new List <GeneratorInput <IMapDescription <int> > >();


            var settings = new JsonSerializerSettings()
            {
                PreserveReferencesHandling = PreserveReferencesHandling.All,
                TypeNameHandling           = TypeNameHandling.All,
            };

            inputs.Add(new GeneratorInput <IMapDescription <int> >(
                           "DeadCells Ramparts",
                           JsonConvert.DeserializeObject <MapDescription <int> >(
                               File.ReadAllText("Resources/MapDescriptions/deadCells_ramparts.json"), settings)));
            inputs.Add(new GeneratorInput <IMapDescription <int> >(
                           "DeadCells Underground",
                           JsonConvert.DeserializeObject <MapDescription <int> >(
                               File.ReadAllText("Resources/MapDescriptions/deadCells_underground.json"), settings)));

            var layoutDrawer = new SVGLayoutDrawer <int>();

            var benchmarkRunner   = new BenchmarkRunner <IMapDescription <int> >();
            var benchmarkScenario = new BenchmarkScenario <IMapDescription <int> >("Platformers", input =>
            {
                var configuration = new DungeonGeneratorConfiguration <int>()
                {
                    RepeatModeOverride              = RoomTemplateRepeatMode.NoImmediate,
                    ThrowIfRepeatModeNotSatisfied   = true,
                    SimulatedAnnealingConfiguration = new SimulatedAnnealingConfigurationProvider(new SimulatedAnnealingConfiguration()
                    {
                        HandleTreesGreedily = true,
                    })
                };
                // var layoutGenerator = new PlatformersGenerator<int>(input.MapDescription, configuration);
                var layoutGenerator = new PlatformersGenerator <int>(input.MapDescription, configuration);
                layoutGenerator.InjectRandomGenerator(new Random(0));

                //return new LambdaGeneratorRunner(() =>
                //{
                //    var layouts = layoutGenerator.GenerateLayout();

                //    return new GeneratorRun(layouts != null, layoutGenerator.TimeTotal, layoutGenerator.IterationsCount);
                //});
                return(new LambdaGeneratorRunner(() =>
                {
                    var simulatedAnnealingArgsContainer = new List <SimulatedAnnealingEventArgs>();
                    void SimulatedAnnealingEventHandler(object sender, SimulatedAnnealingEventArgs eventArgs)
                    {
                        simulatedAnnealingArgsContainer.Add(eventArgs);
                    }

                    layoutGenerator.OnSimulatedAnnealingEvent += SimulatedAnnealingEventHandler;
                    var layout = layoutGenerator.GenerateLayout();
                    layoutGenerator.OnSimulatedAnnealingEvent -= SimulatedAnnealingEventHandler;

                    var additionalData = new AdditionalRunData()
                    {
                        SimulatedAnnealingEventArgs = simulatedAnnealingArgsContainer,
                        GeneratedLayoutSvg = layoutDrawer.DrawLayout(layout, 800, forceSquare: true),
                        // GeneratedLayout = layout,
                    };

                    var generatorRun = new GeneratorRun <AdditionalRunData>(layout != null, layoutGenerator.TimeTotal, layoutGenerator.IterationsCount, additionalData);

                    return generatorRun;
                }));
            });
示例#9
0
        public void Run()
        {
            var count  = 20;
            var name   = "edges_0_3";
            var graphs = GetGraphs(count, name);

            var inputs = new List <DungeonGeneratorInput <int> >();
            var chainDecompositionOld = new BreadthFirstChainDecompositionOld <int>();

            for (var i = 0; i < graphs.Count; i++)
            {
                var graph = graphs[i];

                for (int j = 0; j < 1; j++)
                {
                    var withCorridors      = j == 1;
                    var mapDescription     = GetMapDescription(graph, withCorridors);
                    var chainDecomposition = new TwoStageChainDecomposition <int>(mapDescription, chainDecompositionOld);

                    inputs.Add(new DungeonGeneratorInput <int>($"RandomGraph {i} {(withCorridors ? "wc" : "")}", mapDescription, new DungeonGeneratorConfiguration <int>()
                    {
                        EarlyStopIfIterationsExceeded = 20000,
                        // Chains = chainDecomposition.GetChains(mapDescription.GetGraph()),
                        SimulatedAnnealingConfiguration = new SimulatedAnnealingConfigurationProvider(new SimulatedAnnealingConfiguration()
                        {
                            MaxIterationsWithoutSuccess = 150
                        })
                    }, null));
                }
            }

            var benchmarkRunner   = new BenchmarkRunner <IMapDescription <int> >();
            var benchmarkScenario = new BenchmarkScenario <IMapDescription <int> >("RandomGraphs", input =>
            {
                var layoutDrawer          = new SVGLayoutDrawer <int>();
                var dungeonGeneratorInput = (DungeonGeneratorInput <int>)input;
                var layoutGenerator       = new DungeonGenerator <int>(input.MapDescription, dungeonGeneratorInput.Configuration);
                layoutGenerator.InjectRandomGenerator(new Random(0));

                return(new LambdaGeneratorRunner(() =>
                {
                    var simulatedAnnealingArgsContainer = new List <SimulatedAnnealingEventArgs>();
                    void SimulatedAnnealingEventHandler(object sender, SimulatedAnnealingEventArgs eventArgs)
                    {
                        simulatedAnnealingArgsContainer.Add(eventArgs);
                    }

                    layoutGenerator.OnSimulatedAnnealingEvent += SimulatedAnnealingEventHandler;
                    var layout = layoutGenerator.GenerateLayout();
                    layoutGenerator.OnSimulatedAnnealingEvent -= SimulatedAnnealingEventHandler;

                    var additionalData = new AdditionalRunData()
                    {
                        SimulatedAnnealingEventArgs = simulatedAnnealingArgsContainer,
                        GeneratedLayoutSvg = layout != null ? layoutDrawer.DrawLayout(layout, 800, forceSquare: true) : null,
                        GeneratedLayout = layout,
                    };

                    var generatorRun = new GeneratorRun <AdditionalRunData>(layout != null, layoutGenerator.TimeTotal, layoutGenerator.IterationsCount, additionalData);

                    return generatorRun;
                }));
            });