public void AlgorithmRunInfo_ExactReproduction() { DungeonGenerator g1 = TestHelpers.GetTestDungeonGenerator(); Dungeon d1 = g1.Generate(); DungeonGenerator g2 = new DungeonGenerator() { WorkingDungeon = d1, Options = new DungeonGenerator.DungeonGeneratorOptions() { DoReset = true, Height = d1.Tiles.Height, Width = d1.Tiles.Width, // Here's what we're actually testing: that reconstructing the run gives exact results AlgRuns = d1.Runs.ReconstructRuns(), } }; Dungeon d2 = g2.Generate(); #if RENDER_OUTPUT DungeonSerializer saver = new DungeonSerializer(); DungeonTileRenderer renderer = new DungeonTileRenderer(); try { saver.Save(d1, "d1_test.dgd", FileMode.Create); using (Image dungeonImage = renderer.Render(d1)) { dungeonImage.Save("d1_test.bmp", ImageFormat.Bmp); } saver.Save(d2, "d2_test.dgd", FileMode.Create); using (Image dungeonImage = renderer.Render(d2)) { dungeonImage.Save("d2_test.bmp", ImageFormat.Bmp); } } catch (Exception) { } #endif for (int y = 0; y < d1.Tiles.Height; ++y) { for (int x = 0; x < d1.Tiles.Width; ++x) { Assert.AreEqual(d1.Tiles[y, x].Physics, d2.Tiles[y, x].Physics); } } }
public void AllAlgorithmsDefaultParams() { foreach (var algProto in AlgorithmPluginEnumerator.GetAllLoadedAlgorithms()) { IList <AlgorithmRun> runs = new List <AlgorithmRun>() { new AlgorithmRun() { Alg = algProto.Clone() as IAlgorithm, Context = new AlgorithmContextBase() { Mask = _dungeonMask_default, R = _r } }, }; Dungeon d = null; try { var generator = CreateDefaultTestGenerator(runs); d = generator.Generate(); } catch (NotImplementedException) { // If the algorithm is not implemented, that doesn't mean the // renderer is broken, so skip this Algorithm. continue; } Assert.IsNotNull(d); DungeonTileRenderer renderer = new DungeonTileRenderer(); using (Image renderedDungeon = renderer.Render(d)) { Assert.IsNotNull(renderedDungeon); string tempFile = System.IO.Path.GetTempFileName(); renderedDungeon.Save(tempFile, ImageFormat.Bmp); Assert.IsTrue(System.IO.File.Exists(tempFile)); System.IO.File.Delete(tempFile); } } }
public void RunWithDefaultParams() { IEnumerable <IAlgorithm> allAlgs = AlgorithmPluginEnumerator.GetAllLoadedAlgorithms(); foreach (var algProto in allAlgs) { IList <AlgorithmRun> runs = new List <AlgorithmRun>() { new AlgorithmRun() { Alg = algProto.Clone() as IAlgorithm, Context = new AlgorithmContextBase() { Mask = _dungeonMask_default, R = _r } }, }; var generator = CreateDefaultTestGenerator(runs); try { Dungeon d = generator.Generate(); #if RENDER_OUTPUT DungeonTileRenderer renderer = new DungeonTileRenderer(); using (Image renderedDungeon = renderer.Render(d)) { renderedDungeon.Save(String.Format("dungeon_{0}.bmp", algProto.Name), ImageFormat.Bmp); } #endif } catch (NotImplementedException) { Console.WriteLine("Skipping {0} as it is not implemented", algProto.Name); continue; } } }
public IActionResult Get(int id, [FromQuery] int width = 25, [FromQuery] int height = 25) { Random r = new Random(id); var allAlgs = _algorithms; var alg = allAlgs.ElementAt(r.Next(allAlgs.Count())); var runs = new List <AlgorithmRun> { new AlgorithmRun() { Alg = alg.Clone() as IAlgorithm, Context = new AlgorithmContextBase() { R = new AlgorithmRandom(id) } } }; var renderer = new DungeonTileRenderer(); renderer.ShadeGroups = false; var generator = new DungeonGenerator(); var stepImages = new List <Image>(); var collection = new MagickImageCollection(); var counter = 0; Action <IAlgorithmContext> RenderAction = new Action <IAlgorithmContext>((context) => { if (counter++ % 5 != 0) { return; } var d = context.D; var img = renderer.Render(d); AddImageToCollection(img, collection, 5); }); generator.Options = new DungeonGenerator.DungeonGeneratorOptions() { DoReset = true, EgressConnections = null, Width = width, Height = height, AlgRuns = runs, Callbacks = { RenderAction } }; try { var dungeon = generator.Generate(); var lastImage = renderer.Render(dungeon); AddImageToCollection(lastImage, collection, 500); var gif_ms = new MemoryStream(); collection.First().AnimationIterations = 0; var magickSettings = new QuantizeSettings(); magickSettings.Colors = 256; collection.Quantize(magickSettings); collection.Optimize(); collection.Write(gif_ms, MagickFormat.Gif); gif_ms.Seek(0, SeekOrigin.Begin); var ms = new MemoryStream(); lastImage.Save(ms, ImageFormat.Png); ms.Seek(0, SeekOrigin.Begin); var jsonObj = new { alt = "A DunGenImage", algorithm = alg.Name, imageBytes = ms.GetBuffer(), gifBytes = gif_ms.GetBuffer() }; return(Ok(jsonObj)); } catch (NotImplementedException) { return(NotFound(String.Format("{0} is not implemented.", alg.Name))); } catch (Exception e) { var message = new StringBuilder(); message.AppendLine("Something went wrong. Exception details:"); while (e != null) { message.AppendFormat("\"{0}\"\n", e.Message); message.AppendLine(e.StackTrace); message.AppendLine(e.InnerException != null ? "Inner Exception:" : ""); e = e.InnerException; } return(NotFound(message.ToString())); } }