public override void RandomInitialization() { Gene = new MapBlueprint(); Gene.Encoding = new byte[Width, Height][]; Rooms = new Dictionary <byte, HashSet <Coords> >(); RoomsOrdered = new SortedSet <byte>(new RoomSizeComparer(Rooms)); Random rnd = new Random(); byte nextRoomId = 1; for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { Gene.Encoding[x, y] = new byte[3]; } } for (int x = 0; x < Width - 1; x++) { for (int y = 0; y < Height - 1; y++) { if (rnd.NextDouble() < WallProbability) { continue; } if (Gene.IsOnMap(x - 1, y) && Gene.Encoding[x - 1, y][0] > 0) { Gene.Encoding[x, y][0] = Gene.Encoding[x - 1, y][0]; AddTileToRoom(Gene.Encoding[x, y][0], new Coords(x, y)); } else if (Gene.IsOnMap(x + 1, y) && Gene.Encoding[x + 1, y][0] > 0) { Gene.Encoding[x, y][0] = Gene.Encoding[x + 1, y][0]; AddTileToRoom(Gene.Encoding[x, y][0], new Coords(x, y)); } else if (Gene.IsOnMap(x, y - 1) && Gene.Encoding[x, y - 1][0] > 0) { Gene.Encoding[x, y][0] = Gene.Encoding[x, y - 1][0]; AddTileToRoom(Gene.Encoding[x, y][0], new Coords(x, y)); } else if (Gene.IsOnMap(x, y + 1) && Gene.Encoding[x, y + 1][0] > 0) { Gene.Encoding[x, y][0] = Gene.Encoding[x, y + 1][0]; AddTileToRoom(Gene.Encoding[x, y][0], new Coords(x, y)); } else { Gene.Encoding[x, y][0] = nextRoomId; Rooms.Add(nextRoomId, new HashSet <Coords>()); Rooms[nextRoomId].Add(new Coords(x, y)); RoomsOrdered.Add(nextRoomId++); } } } }
public void Save(MapBlueprint map) { string text = JsonConvert.SerializeObject(map); text = CompressionHelper.Compress(text); File.WriteAllText(map.MapPath, text); }
static void FloodFillNeighbor(Coords start, Coords neighbor, MapBlueprint map, Stack <Coords> open, HashSet <byte> neighbors, HashSet <Coords> closed) { if (map.IsOnMap(neighbor.X, neighbor.Y) && !closed.Contains(neighbor)) { if (map.Encoding[start.X, start.Y][0] == map.Encoding[neighbor.X, neighbor.Y][0]) { open.Push(neighbor); } else if (map.Encoding[start.X, start.Y][1] > 0 && map.Encoding[start.X, start.Y][1] == map.Encoding[neighbor.X, neighbor.Y][1]) { neighbors.Add(map.Encoding[neighbor.X, neighbor.Y][0]); } } }
static void AddDoors(MapBlueprint map) { byte nextDoorId = 1; HashSet <Connection> existingConnections = new HashSet <Connection>(); for (int x = 0; x < map.Encoding.GetLength(0) - 1; x++) { for (int y = 0; y < map.Encoding.GetLength(1) - 1; y++) { if (!TryAddingDoor(x, y, x + 1, y, map, existingConnections, ref nextDoorId)) { TryAddingDoor(x, y, x, y + 1, map, existingConnections, ref nextDoorId); } } } }
public void LoadMap(MapBlueprint map) { if (!IsConnected) { return; } lock (_scheduledMessages) { _scheduledMessages.Clear(); _scheduledMessages.Enqueue(new MessageWithType { message = JsonConvert.SerializeObject(map), message_type = nameof(MapBlueprint) }); } }
static bool TryAddingDoor(int x1, int y1, int x2, int y2, MapBlueprint map, HashSet <Connection> existingConnections, ref byte nextDoorId) { Connection connection = new Connection(map.Encoding[x1, y1][0], map.Encoding[x2, y2][0]); bool added = false; if (map.Encoding[x1, y1][0] > 0 && map.Encoding[x1, y1][1] == 0 && map.Encoding[x2, y2][0] > 0 && map.Encoding[x2, y2][1] == 0 && map.Encoding[x1, y1][0] != map.Encoding[x2, y2][0] && !existingConnections.Contains(connection)) { map.Encoding[x1, y1][1] = nextDoorId; map.Encoding[x2, y2][1] = nextDoorId++; existingConnections.Add(connection); added = true; } return(added); }
public void Host(string key, MapBlueprint map, Position selectedPosition) { if (IsConnected) { return; } var matches = regex.Matches(key); _apiKey = matches[0].Groups[1].Value; _chatId = matches[0].Groups[2].Value; _sendingBlocked = false; IsHost = true; GetUpdatesMessage() .ContinueWith(_ => { LoadMap(map); ClickOnPosition(selectedPosition); _timer.Start(); }); }
void CreateMap() { MapBlueprint map = MapBlueprint.FromFile(@"..\..\..\..\..\map.bin"); GameObject[,] tiles = new GameObject[map.Encoding.GetLength(0), map.Encoding.GetLength(1)]; Dictionary <byte, Room> rooms = new Dictionary <byte, Room>(); for (int x = 0; x < map.Encoding.GetLength(0); x++) { for (int y = 0; y < map.Encoding.GetLength(1); y++) { byte roomID = map.Encoding[x, y][0]; byte doorID = map.Encoding[x, y][1]; if (!rooms.ContainsKey(roomID)) { rooms.Add(roomID, new Room(roomID)); } Room room = rooms[roomID]; MapNode node = new MapNode(x, y, doorID, room); tiles[x, y] = new GameObjectBuilder() .AddComponent(new Transform(new Vector2(x * Map.Size, y * Map.Size))) .AddComponent(new SpriteRenderer(Wall, .9f)) .AddComponent(node) .AddComponent(new Dimentions(0, 0, Map.Size, Map.Size)) .AddComponent(new Clickable(() => GlobalScripts.GetComponent <Player>().SelectNode(node))) //.AddComponent(new Hoverable(() => GlobalScripts.GetComponent<Player>().RequestInfoboxOverride("Room ID: " + node.Room.ID, node))) .Register(this); } } Map = new Map(tiles, rooms.Values); Map.SetupRooms(map.StartRoomId); Map.SetNeighbors(); Map.SetTileGraphics(Floor, FloorDark, Wall); GlobalScripts.GetComponent <MapPanner>().Map = Map; }
static HashSet <byte> FindNeighboringRooms(MapBlueprint map, Coords start) { // flood fill Stack <Coords> open = new Stack <Coords>(); HashSet <Coords> closed = new HashSet <Coords>(); HashSet <byte> neighbors = new HashSet <byte>(); open.Push(start); while (open.Count > 0) { Coords tile = open.Pop(); FloodFillNeighbor(tile, new Coords(tile.X - 1, tile.Y), map, open, neighbors, closed); FloodFillNeighbor(tile, new Coords(tile.X + 1, tile.Y), map, open, neighbors, closed); FloodFillNeighbor(tile, new Coords(tile.X, tile.Y - 1), map, open, neighbors, closed); FloodFillNeighbor(tile, new Coords(tile.X, tile.Y + 1), map, open, neighbors, closed); closed.Add(tile); } return(neighbors); }
public void Delete(MapBlueprint selectedMap) { File.Delete(selectedMap.MapPath); }
static void GenerateSmart(string filepath) { int w = 30, h = 20, rooms = 20; const int numheroes = 5; MapBlueprint map = new MapBlueprint(); map.StartRoomId = 1; map.Encoding = new byte[w, h][]; for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { map.Encoding[x, y] = new byte[3]; } } Rectangle[] initialRooms = new Rectangle[rooms]; Random rnd = new Random(); /*int maxW = (int)(w * 2f / rooms); * int maxH = (int)(h * 2f / rooms);*/ int maxW = 7, maxH = 7; int index = 0; /*do * initialRooms[index] = new Rectangle(rnd.Next(w - maxW), rnd.Next(h - maxH), rnd.Next(1, maxW), rnd.Next(1, maxH)); * while (++attempts < 1000 && initialRooms[0].Width * initialRooms[0].Height >= numheroes && * (Overlaps(index, initialRooms) || ++index < rooms));*/ do // FIXME: slow! { initialRooms = new Rectangle[rooms]; index = 0; int attempts = 0; do { initialRooms[index] = new Rectangle(rnd.Next(w - maxW), rnd.Next(h - maxH), rnd.Next(1, maxW), rnd.Next(1, maxH)); }while (++attempts < 1000 && (Overlaps(index, initialRooms) || ++index < rooms)); if (attempts == 1000) { Rectangle[] tmp = new Rectangle[index]; Array.Copy(initialRooms, tmp, index); initialRooms = tmp; } }while (initialRooms[0].Width * initialRooms[0].Height < numheroes); for (int i = 0; i < initialRooms.Length; i++) { Rectangle r = Rectangle.Zero; int width = 1; do { r = new Rectangle(initialRooms[i].X + initialRooms[i].Width, initialRooms[i].Y, width++, initialRooms[i].Height); if (r.X + r.Width - 1 > w) { r.Width = 0; break; } }while (!Overlaps(r, initialRooms)); initialRooms[i].Width += r.Width - 1; int height = 1; do { r = new Rectangle(initialRooms[i].X, initialRooms[i].Y + initialRooms[i].Height, initialRooms[i].Width, height++); if (r.Y + r.Height - 1 > h) { r.Height = 0; break; } }while (!Overlaps(r, initialRooms)); initialRooms[i].Height += r.Height - 1; } for (int i = 0; i < initialRooms.Length; i++) { for (int x = initialRooms[i].X; x < initialRooms[i].X + initialRooms[i].Width; x++) { for (int y = initialRooms[i].Y; y < initialRooms[i].Y + initialRooms[i].Height; y++) { if (map.Encoding[x, y][0] > 0) { Rectangle a = initialRooms[i]; Rectangle b = initialRooms[map.Encoding[x, y][0] - 1]; bool inter = a.Overlaps(b); throw new InvalidOperationException("Overlap!"); } map.Encoding[x, y][0] = (byte)(i + 1); } } } byte doors = 1; HashSet <int> doorsExistBetweenRooms = new HashSet <int>(); for (int x = 0; x < w - 1; x++) { for (int y = 0; y < h - 1; y++) { int hash = (851 + map.Encoding[x, y][0]) * 37 + map.Encoding[x + 1, y][0]; bool doorsAdded = false; if (map.Encoding[x, y][0] > 0 && map.Encoding[x + 1, y][0] > 0 && map.Encoding[x, y][0] != map.Encoding[x + 1, y][0] && !doorsExistBetweenRooms.Contains(hash)) { map.Encoding[x + 1, y][1] = doors; map.Encoding[x, y][1] = doors++; doorsExistBetweenRooms.Add(hash); doorsAdded = true; } if (!doorsAdded) { hash = (851 + map.Encoding[x, y][0]) * 37 + map.Encoding[x, y + 1][0]; if (map.Encoding[x, y][0] > 0 && map.Encoding[x, y + 1][0] > 0 && map.Encoding[x, y][0] != map.Encoding[x, y + 1][0] && !doorsExistBetweenRooms.Contains(hash)) { map.Encoding[x, y][1] = doors; map.Encoding[x, y + 1][1] = doors++; doorsExistBetweenRooms.Add(hash); } } } } map.SaveMapToFile(filepath + "map.bin"); map.SaveImageToFile(filepath + "map.png", 1536, 1024); }