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 bool AdjacentCellInDirectionIsCorridor(Point location, Direction.DirectionType direction, Dungeon dungeon) { if (HasAdjacentCellInDirection(location, direction)) { Point target = GetTargetLocation(location, direction); switch (direction) { case Direction.DirectionType.North: return dungeon[target].IsCorridor; case Direction.DirectionType.West: return dungeon[target].IsCorridor; case Direction.DirectionType.South: return dungeon[target].IsCorridor; case Direction.DirectionType.East: return dungeon[target].IsCorridor; default: throw new InvalidOperationException(); } } else return false; }
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; }
public Point CreateDoor(Point location, Direction.DirectionType direction) { return CreateSide(location, direction, Cell.Sidetype.Door); }
public Point CreateCorridor(Point currentLocation, Direction.DirectionType direction) { currentLocation = CreateSide(currentLocation, direction, Cell.Sidetype.Empty); return currentLocation; }
public bool AdjacentCellInDirectionVisited(Point p, Direction.DirectionType direction) { switch (direction) { case Direction.DirectionType.North: return this[p.X, p.Y - 1].IsVisited; case Direction.DirectionType.South: return this[p.X, p.Y + 1].IsVisited; case Direction.DirectionType.East: return this[p.X + 1, p.Y].IsVisited; case Direction.DirectionType.West: return this[p.X - 1, p.Y].IsVisited; default: throw new InvalidOperationException("Out of bounds"); } }
public bool CanMove(Direction.DirectionType direction, Point location) { switch (direction) { case Direction.DirectionType.North: if (this[location].NorthSide == Cell.Sidetype.Empty) { return true; } else return false; case Direction.DirectionType.South: if (this[location].SouthSide == Cell.Sidetype.Empty) { return true; } else return false; case Direction.DirectionType.West: if (this[location].WestSide == Cell.Sidetype.Empty) { return true; } else return false; case Direction.DirectionType.East: if (this[location].EastSide == Cell.Sidetype.Empty) { return true; } else return false; default: return false; } }
protected Point GetTargetLocation(Point currentLocation, Direction.DirectionType direction) { Point targetLocation = new Point(); targetLocation = currentLocation; switch (direction) { case Direction.DirectionType.North: targetLocation.Y--; break; case Direction.DirectionType.South: targetLocation.Y++; break; case Direction.DirectionType.East: targetLocation.X++; break; case Direction.DirectionType.West: targetLocation.X--; break; } return targetLocation; }
public bool HasAdjacentCellInDirection(Point p, Direction.DirectionType direction) { switch (direction) { case Direction.DirectionType.North: return p.Y > 0; case Direction.DirectionType.South: return p.Y < Height - 1; case Direction.DirectionType.East: return p.X < Width - 1; case Direction.DirectionType.West: return p.X > 0; default: return false; } }
public Point CreateSide(Point currentLocation, Direction.DirectionType direction, Cell.Sidetype sidetype) { Point targetLocation = new Point(); targetLocation = GetTargetLocation(currentLocation, direction); switch (direction) { case Direction.DirectionType.North: this[currentLocation].NorthSide = sidetype; this[targetLocation].SouthSide = sidetype; break; case Direction.DirectionType.South: this[currentLocation].SouthSide = sidetype; this[targetLocation].NorthSide = sidetype; break; case Direction.DirectionType.East: this[currentLocation].EastSide = sidetype; this[targetLocation].WestSide = sidetype; break; case Direction.DirectionType.West: this[currentLocation].WestSide = sidetype; this[targetLocation].EastSide = sidetype; break; } return targetLocation; }