private static void createCache(string key, DungeonTemplate template)
 {
     logger.Info($"Generating cache for dungeon: {key}");
     cachedMaps.Add(key, new List<string>());
     for (var i = 0; i < 3; i++) //Keep at least 3 maps in cache
         cachedMaps[key].Add(generateNext((uint)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds, template));
 }
 private static string nextMap(uint seed, string key, DungeonTemplate template)
 {
     var map = cachedMaps[key][0];
     cachedMaps[key].RemoveAt(0);
     logger.Info($"Generating new map for dungeon: {key}");
     Task.Factory.StartNew(() => cachedMaps[key].Add(generateNext(seed, template)));
     return map;
 }
        internal DungeonGraph(DungeonTemplate template, Room[] rooms)
        {
            Template = template;

            int dx = int.MaxValue, dy = int.MaxValue;
            int mx = int.MinValue, my = int.MinValue;

            for (int i = 0; i < rooms.Length; i++)
            {
                var bounds = rooms[i].Bounds;

                if (bounds.X < dx)
                    dx = bounds.X;
                if (bounds.Y < dy)
                    dy = bounds.Y;

                if (bounds.MaxX > mx)
                    mx = bounds.MaxX;
                if (bounds.MaxY > my)
                    my = bounds.MaxY;
            }

            const int Pad = 4;

            Width = mx - dx + Pad * 2;
            Height = my - dy + Pad * 2;

            for (int i = 0; i < rooms.Length; i++)
            {
                var room = rooms[i];
                var pos = room.Pos;
                room.Pos = new Point(pos.X - dx + Pad, pos.Y - dy + Pad);

                foreach (var edge in room.Edges)
                {
                    if (edge.RoomA != room)
                        continue;
                    if (edge.Linkage.Direction == Direction.South || edge.Linkage.Direction == Direction.North)
                        edge.Linkage = new Link(edge.Linkage.Direction, edge.Linkage.Offset - dx + Pad);
                    else if (edge.Linkage.Direction == Direction.East || edge.Linkage.Direction == Direction.West)
                        edge.Linkage = new Link(edge.Linkage.Direction, edge.Linkage.Offset - dy + Pad);
                }
            }
            Rooms = rooms;
        }
 private static string generateNext(uint seed, DungeonTemplate template)
 {
     var gen = new DungeonGen((int)seed, template);
     gen.GenerateAsync();
     return gen.ExportToJson();
 }
        public DungeonGen(int seed, DungeonTemplate template)
        {
            this.seed = seed;

            gen = new Generator(seed, template);
        }
 public Generator(int seed, DungeonTemplate template)
 {
     rand = new Random(seed);
     this.template = template;
     Step = GenerationStep.Initialize;
 }