public static void RemoveDeadEnds(Dungeon dungeon, int deadEndRemovalModifier) { foreach (Point deadEndLocation in dungeon.FindDeadEnds) { if (ShouldRemoveDeadend(deadEndRemovalModifier)) { Point currentLocation = deadEndLocation; do { Direction directionPicker = new Direction((Direction.DirectionType)dungeon.CalculateDeadEndCorridorDirection(currentLocation), 100); Direction.DirectionType direction = directionPicker.GetNextDirection(); while (!dungeon.HasAdjacentCellInDirection(currentLocation, direction)) { if (directionPicker.HasNextDirection) direction = directionPicker.GetNextDirection(); else throw new InvalidOperationException("This should not happen"); } currentLocation = dungeon.CreateCorridor(currentLocation, direction); } while (dungeon[currentLocation].IsDeadEnd); } } }
public Dungeon Generate(int width, int height, int changeDirectionModifier, int sparesnessModifier) { Dungeon dungeon = new Dungeon(width, height); Point location = dungeon.PickRandomCellMarkVisited(); Direction.DirectionType previousDirection = Direction.DirectionType.North; while (dungeon.visitedCells < dungeon.Height * dungeon.Width) { Direction DirectionPicker = new Direction(previousDirection, changeDirectionModifier); Direction.DirectionType direction = DirectionPicker.GetNextDirection(); while (!dungeon.HasAdjacentCellInDirection(location, direction) || dungeon.AdjacentCellInDirectionVisited(location, direction)) { if (DirectionPicker.HasNextDirection) { direction = DirectionPicker.GetNextDirection(); } else { location = dungeon.PickRandomVisitedCell(location); DirectionPicker = new Direction(previousDirection, changeDirectionModifier); direction = DirectionPicker.GetNextDirection(); } } location = dungeon.CreateCorridor(location, direction); dungeon.MarkCellsAsVisited(location); previousDirection = direction; } SparsifyMaze(dungeon, sparesnessModifier); RemoveDeadEnds(dungeon, 70); roomGenerator.GenerateRooms(dungeon); PlaceDoors(dungeon); return dungeon; }