Exemplo n.º 1
0
        private static string TryGenerateMap(IMapGenerationParams mapParams)
        {
            var rooms     = RoomGenerator.GenerateRooms(mapParams);
            var corridors = CorridorGenerator.ConnectRooms(mapParams, rooms);

            return(PrintBoard(mapParams, rooms, corridors));
        }
Exemplo n.º 2
0
        private static bool TryToExpand(
            IMapGenerationParams mapParams, List <Room> rooms, Room room
            )
        {
            var expanded = false;

            if (mapParams.RoomExpandCheck() && CanExpandLeft(mapParams, rooms, room))
            {
                room.TilePos = room.TilePos.Substract(x: 1);
                room.Size    = room.Size.Add(width: 1);
                expanded     = true;
            }
            if (mapParams.RoomExpandCheck() && CanExpandRight(mapParams, rooms, room))
            {
                room.Size = room.Size.Add(width: 1);
                expanded  = true;
            }
            if (mapParams.RoomExpandCheck() && CanExpandUp(mapParams, rooms, room))
            {
                room.TilePos = room.TilePos.Substract(y: 1);
                room.Size    = room.Size.Add(height: 1);
                expanded     = true;
            }
            if (mapParams.RoomExpandCheck() && CanExpandDown(mapParams, rooms, room))
            {
                room.Size = room.Size.Add(height: 1);
                expanded  = true;
            }

            return(expanded);
        }
Exemplo n.º 3
0
 public static string GenerateMap(IMapGenerationParams mapParams)
 {
     try
     {
         return(TryGenerateMap(mapParams));
     }
     catch (InvalidMapException)
     {
         return(string.Empty);
     }
 }
Exemplo n.º 4
0
        private static bool CanExpandUp(
            IMapGenerationParams mapParams, List <Room> rooms, Room room
            )
        {
            var targetY = room.TilePos.Y - 1;

            if (targetY <= 0)
            {
                return(false);
            }

            return(CanExpandVertically(mapParams, rooms, room, targetY));
        }
Exemplo n.º 5
0
        private static bool CanExpandDown(
            IMapGenerationParams mapParams, List <Room> rooms, Room room
            )
        {
            var targetY = room.TilePos.Y + room.Size.Height;

            if (targetY >= mapParams.Height - 1)
            {
                return(false);
            }

            return(CanExpandVertically(mapParams, rooms, room, targetY));
        }
Exemplo n.º 6
0
        private static bool CanExpandRight(
            IMapGenerationParams mapParams, List <Room> rooms, Room room
            )
        {
            var targetX = room.TilePos.X + room.Size.Width;

            if (targetX >= mapParams.Width - 1)
            {
                return(false);
            }

            return(CanExpandHorizontally(mapParams, rooms, room, targetX));
        }
Exemplo n.º 7
0
        private static bool CanExpandLeft(
            IMapGenerationParams mapParams, List <Room> rooms, Room room
            )
        {
            var targetX = room.TilePos.X - 1;

            if (targetX <= 0)
            {
                return(false);
            }

            return(CanExpandHorizontally(mapParams, rooms, room, targetX));
        }
Exemplo n.º 8
0
        private static bool CanExpandVertically(
            IMapGenerationParams mapParams, List <Room> rooms, Room room, int targetY
            )
        {
            var fromX = room.TilePos.X;
            var toX   = room.TilePos.X + room.Size.Width;

            return(rooms
                   .Where(r => r != room)
                   .All(r => !r.Intersects(
                            new Point(fromX, targetY),
                            new Size(toX - fromX, 1),
                            mapParams.MinRoomSeparation
                            )));
        }
Exemplo n.º 9
0
        private static bool CanExpandHorizontally(
            IMapGenerationParams mapParams, List <Room> rooms, Room room, int targetX
            )
        {
            var fromY = room.TilePos.Y;
            var toY   = room.TilePos.Y + room.Size.Height;

            return(rooms
                   .Where(r => r != room)
                   .All(r => !r.Intersects(
                            new Point(targetX, fromY),
                            new Size(1, toY - fromY),
                            mapParams.MinRoomSeparation
                            )));
        }
Exemplo n.º 10
0
        private static void AddNewRoom(IMapGenerationParams mapParams, List <Room> rooms)
        {
            var newRoom = new Room()
            {
                TilePos = new Point(
                    x: Luck.Next(1, mapParams.Width - 1),  // external wall border required
                    y: Luck.Next(1, mapParams.Height - 1)  // external wall border required
                    ),
                Size = new Size(1, 1)
            };

            var isNotAdjacentToOtherRooms = rooms
                                            .All(room => !room.Intersects(newRoom, mapParams.MinRoomSeparation));

            if (isNotAdjacentToOtherRooms)
            {
                rooms.Add(newRoom);
            }
        }
Exemplo n.º 11
0
        private static string PrintBoard(
            IMapGenerationParams mapParams, List <Room> rooms, List <Corridor> corridors
            )
        {
            var board = "";

            for (var y = 0; y < mapParams.Height; y++)
            {
                var boardRow = "";
                for (var x = 0; x < mapParams.Width; x++)
                {
                    var tile = new Point(x, y);
                    boardRow += rooms.Any(room => room.HasTile(tile))
                        ? "T"
                        : corridors.Any(c => c.Tiles.Contains(tile)) ? "t" : ".";
                }

                board += boardRow + Environment.NewLine;
            }

            return(board);
        }
Exemplo n.º 12
0
        public static List <Room> GenerateRooms(IMapGenerationParams mapParams)
        {
            var rooms = new List <Room>();
            var area  = mapParams.Width * mapParams.Height;

            while (rooms.Count < mapParams.InitialRooms)
            {
                AddNewRoom(mapParams, rooms);
            }

            var whileLoops = 0;

            while (true)
            {
                whileLoops++;
                if (whileLoops > INITIAL_ROOMS_MAX_LOOPS)
                {
                    throw new InvalidMapException("Creating initial rooms");
                }

                foreach (var room in rooms)
                {
                    var expanded = TryToExpand(mapParams, rooms, room);
                    if (expanded)
                    {
                        var roomsWithMinSize = rooms
                                               .Where(room => room.HasMinSize(mapParams.MinRoomSize))
                                               .ToList();

                        var roomArea = (float)roomsWithMinSize.Sum(room => room.Area);
                        var density  = roomArea / area;
                        if (density >= mapParams.MinDensity)
                        {
                            return(roomsWithMinSize);
                        }
                    }
                }
            }
        }
Exemplo n.º 13
0
        private static bool CreateCorridor(
            IMapGenerationParams mapParams, List <Room> rooms, List <Corridor> corridors
            )
        {
            var corridorWidth = mapParams.GetRandomCorridorWidth();
            var wideIndexList = Enumerable.Range(0, corridorWidth).ToList();

            var(startRoom, startTiles, direction) =
                GetStartTiles(rooms, corridors, corridorWidth, wideIndexList);

            if (startRoom == null)
            {
                return(false);
            }

            var newCorridor = new Corridor();

            newCorridor.Rooms.Add(startRoom);

            var endRooms = ExpandCorridor(
                mapParams, corridors, newCorridor, corridorWidth, wideIndexList, startTiles, direction, rooms
                );

            if (endRooms == null || !endRooms.Any())
            {
                return(false);
            }

            startRoom.Corridors.Add(newCorridor);

            newCorridor.Rooms.AddRange(endRooms);
            endRooms.ForEach(endRoom => endRoom.Corridors.Add(newCorridor));

            corridors.Add(newCorridor);

            return(true);
        }
Exemplo n.º 14
0
        public static List <Corridor> ConnectRooms(
            IMapGenerationParams mapParams, List <Room> rooms
            )
        {
            var corridors  = new List <Corridor>();
            var whileLoops = 0;

            while (true)
            {
                whileLoops++;
                if (whileLoops > CONNECT_ROOMS_MAX_LOOPS)
                {
                    throw new InvalidMapException("Connecting rooms");
                }

                var added = CreateCorridor(mapParams, rooms, corridors);
                if (added && AreAllConnected(rooms))
                {
                    break;
                }
            }

            return(corridors);
        }