protected virtual DungeonPrefab CreateRandomRoom(Size minRoomSize, Size maxRoomSize) { var room = new DungeonPrefab(Rng.Next(minRoomSize.Width, maxRoomSize.Width), Rng.Next(minRoomSize.Height, maxRoomSize.Height), () => new Floor()); // Surround room with walls foreach (var location in room.Locations) { if ((location.X >= room.Bounds.X) && (location.X < room.Bounds.Right) && (location.Y == room.Bounds.Y)) { room[location] = new Wall(); } if ((location.X >= room.Bounds.X) && (location.X < room.Bounds.Right) && (location.Y == room.Bounds.Bottom - 1)) { room[location] = new Wall(); } if ((location.Y >= room.Bounds.Y) && (location.Y < room.Bounds.Bottom) && (location.X == room.Bounds.X)) { room[location] = new Wall(); } if ((location.Y >= room.Bounds.Y) && (location.Y < room.Bounds.Bottom) && (location.X == room.Bounds.Right - 1)) { room[location] = new Wall(); } } return(room); }
protected void PlaceRoom(Point location, DungeonPrefab room, TerrainMap terrainMap) { // Create a copy of the room var roomToPlace = (DungeonPrefab)room.Clone(); // Offset the room origin to the new location roomToPlace.SetLocation(location); // Loop for each cell in the room foreach (var roomLocation in room.Locations) { // Translate the room cell location to its location in the dungeon var dungeonLocation = new Point(location.X + roomLocation.X, location.Y + roomLocation.Y); if (room[roomLocation] != null) { // Copy the tiles from the room to the dungeon terrainMap[dungeonLocation] = room[roomLocation]; } } // Add the lightsources to the dungeon foreach (var lightSource in room.LightSources) { var lightSourceToPlace = (LightSource)lightSource.Clone(); lightSourceToPlace.Location.Coordinate = new Point(lightSourceToPlace.Location.Coordinate.X + location.X, lightSourceToPlace.Location.Coordinate.Y + location.Y); terrainMap.LightSources.Add(lightSourceToPlace); } terrainMap.Prefabs.Add(roomToPlace); }
public Point?GetClosestUnexploredLocation() { int closestDistance = int.MaxValue; Point?closestLocation = null; // We want to explore rooms before we explore corridors DungeonPrefab currentRoom = GameInstance.Terrain.GetPrefabAtLocation(Location.Coordinate); // If we are in a room then get the closest unseen location in the room foreach ( Point unseenLocation in currentRoom == null ? GetWalkableUnseenLocations() : GetWalkableUnseenLocationsWithinPrefab(currentRoom)) { int currentDistance = Location.DistanceTo(unseenLocation); if (currentDistance >= closestDistance) { continue; } closestDistance = currentDistance; closestLocation = unseenLocation; } return(closestLocation); }
protected virtual int CalculateRoomPlacementScore(Point location, DungeonPrefab room, TerrainMap terrainMap) { if (!ContainingBoundsIsWithinDungeonScore(terrainMap, location, room.Bounds.Size)) { return(int.MinValue); } if (RoomOverlapsOtherRooms(terrainMap, location, room.Bounds.Size)) { return(int.MinValue); } return(GetSurroundedByFloorScore(terrainMap, location, room.Bounds.Size)); }
protected override int CalculateRoomPlacementScore(Point location, DungeonPrefab room, TerrainMap terrainMap) { if (room.Connectors.Count <= 0) { return(base.CalculateRoomPlacementScore(location, room, terrainMap)); } if (!ContainingBoundsIsWithinDungeonScore(terrainMap, location, room.Bounds.Size)) { return(int.MinValue); } if (RoomOverlapsOtherRooms(terrainMap, location, room.Bounds.Size)) { return(int.MinValue); } var connectorPlacementScore = 0; foreach (var connector in room.Connectors) { // Translate the connector location to its location in the dungeon var dungeonLocation = new Point(location.X + connector.X, location.Y + connector.Y); connectorPlacementScore += GetConnectorPlacementScore(new Point(connector.X, connector.Y), dungeonLocation, room, terrainMap, Direction.North); connectorPlacementScore += GetConnectorPlacementScore(new Point(connector.X, connector.Y), dungeonLocation, room, terrainMap, Direction.South); connectorPlacementScore += GetConnectorPlacementScore(new Point(connector.X, connector.Y), dungeonLocation, room, terrainMap, Direction.West); connectorPlacementScore += GetConnectorPlacementScore(new Point(connector.X, connector.Y), dungeonLocation, room, terrainMap, Direction.East); } return((connectorPlacementScore == 0) ? int.MinValue : connectorPlacementScore); }
public void GenerateDoors(TerrainMap terrainMap) { AddDoorsToDeadEnds(terrainMap); // Evaulate each prefab and see if doors need to be placed foreach (DungeonPrefab dungeonPrefab in terrainMap.Prefabs) { DungeonPrefab dungeonPrefab1 = dungeonPrefab; if (dungeonPrefab.Locations.Where(x => dungeonPrefab1[x] is Door).Count() == 0) { // Need to place some doors var noOfDoorsToPlace = Rng.Next(MinDoorsPerRoom, MaxDoorsPerRoom); var doorsPlaced = 0; doorsPlaced += PlaceDoorsOnWall(terrainMap, dungeonPrefab.Bounds, doorsPlaced, noOfDoorsToPlace, Direction.North); doorsPlaced += PlaceDoorsOnWall(terrainMap, dungeonPrefab.Bounds, doorsPlaced, noOfDoorsToPlace, Direction.South); doorsPlaced += PlaceDoorsOnWall(terrainMap, dungeonPrefab.Bounds, doorsPlaced, noOfDoorsToPlace, Direction.East); doorsPlaced += PlaceDoorsOnWall(terrainMap, dungeonPrefab.Bounds, doorsPlaced, noOfDoorsToPlace, Direction.West); } } RemoveInvalidDoors(terrainMap); }
private int GetConnectorPlacementScore(Point connectorLocation, Point dungeonLocation, DungeonPrefab room, TerrainMap terrainMap, Direction direction) { // Check if the room has an adjacent location in the given direction from the connector location // Check that the dungeon has a walkable location in the given direction if ((!room.HasAdjacentLocation(connectorLocation, direction)) && (terrainMap.Bounds.Contains(direction.ApplyTransform(dungeonLocation))) ) { var targetLocation = terrainMap.GetTargetLocation(dungeonLocation, direction).Value; if (terrainMap[targetLocation] is Floor) { return(1); } var door = terrainMap[targetLocation] as Door; if ((door != null) && ((door.State == DoorStates.Open) || (door.State == DoorStates.Broken))) { return(1); } return(0); } return(0); }