public void SaveAndLoad()
        {
            DungeonGenerator generator = TestHelpers.GetTestDungeonGenerator();

            Dungeon d_original = generator.Generate();

            DungeonSerializer serializer  = new DungeonSerializer();
            string            dungeonFile = System.IO.Path.GetTempFileName();

            serializer.Save(d_original, dungeonFile, FileMode.Create);
            Assert.IsTrue(File.Exists(dungeonFile));

            Dungeon d_loaded = serializer.Load(dungeonFile);

            Assert.IsNotNull(d_loaded);

            //
            // The basic qualities of the dungeon should match.
            //

            // The constituent tiles should be set appropriately
            Assert.AreEqual(d_original, d_original.Tiles.Parent);
            Assert.AreEqual(d_loaded, d_loaded.Tiles.Parent);

            // If the dungeons aren't the same dimensions, we definitely failed.
            Assert.AreEqual(d_original.Tiles.Width, d_loaded.Tiles.Width);
            Assert.AreEqual(d_original.Tiles.Height, d_loaded.Tiles.Height);

            // Physics must be identical, or else we didn't load the same dungeon
            foreach (Tile t in d_original.Tiles.Tiles_Set)
            {
                Assert.IsTrue(t.Physics == d_loaded.Tiles[t.Location.Y, t.Location.X].Physics);
            }

            //
            // We also save the "runs" associated with a file, to allow re-creating
            // it. Those should be saved/loaded
            //

            Assert.AreEqual(d_original.Runs.Count, d_loaded.Runs.Count);

            for (int i = 0; i < d_original.Runs.Count; ++i)
            {
                Assert.AreEqual(d_original.Runs[i].RandomSeed, d_loaded.Runs[i].RandomSeed);
                Assert.AreEqual(d_original.Runs[i].Info, d_loaded.Runs[i].Info);
                for (int m = 0; m < d_original.Runs[i].Mask.Count; ++m)
                {
                    Assert.IsTrue(Enumerable.SequenceEqual(d_original.Runs[i].Mask[m], d_loaded.Runs[i].Mask[m]));
                }
            }

            File.Delete(dungeonFile);
        }
Example #2
0
        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);
                }
            }
        }