private void RemoveDeadEnds() { List <RoomsAndMazesCell> deadEnds = new List <RoomsAndMazesCell>(); IterateThroughCave(new CaveCellsIterator((int x, int y) => { RoomsAndMazesCell cell = grid[x][y]; if (cell.type == RoomsAndMazesCellType.Ground) { if (IsDeadEnd(cell.x, cell.y)) { deadEnds.Add(cell); } } })); while (deadEnds.Count > 0) { RoomsAndMazesCell cell = deadEnds[Random.Range(0, deadEnds.Count)]; cell.type = RoomsAndMazesCellType.Wall; deadEnds.Remove(cell); IterateThroughNeumannNeighbours(new CaveCellsIterator((int nX, int nY) => { if ((grid[nX][nY].type == RoomsAndMazesCellType.Ground || grid[nX][nY].type == RoomsAndMazesCellType.Door) && IsDeadEnd(nX, nY)) { deadEnds.Add(grid[nX][nY]); } }), cell.x, cell.y); } }
/// <summary> /// Change the 'color' of the grid according to the type of the first cell (bucket) /// </summary> /// <param name="x">Start cell X position</param> /// <param name="y">Start cell Y position</param> /// <param name="color">the 'color' to paint the cave with with</param> /// <returns>Returns the boundries cells</returns> private RoomsAndMazesCell[] PaintDungeon(int x, int y, RoomsAndMazesCellType color) { RoomsAndMazesCellType firstType = grid[x][y].type; List <RoomsAndMazesCell> cellsToPaint = new List <RoomsAndMazesCell>(); cellsToPaint.Add(grid[x][y]); List <RoomsAndMazesCell> boundries = new List <RoomsAndMazesCell>(); while (cellsToPaint.Count > 0) { RoomsAndMazesCell currentCell = cellsToPaint[0]; cellsToPaint.Remove(currentCell); if (currentCell.type == firstType) { currentCell.type = color; IterateThroughNeumannNeighbours(new CaveCellsIterator((int nX, int nY) => { if (grid[nX][nY].type == firstType) { cellsToPaint.Add(grid[nX][nY]); } else if (grid[nX][nY].type != color && !boundries.Contains(grid[nX][nY])) { boundries.Add(grid[nX][nY]); } }), currentCell.x, currentCell.y); } } return(boundries.ToArray()); }
private void ConnectDungeon() { RoomsAndMazesCell currentCell = lastRoomCell; RoomsAndMazesCell[] boudries = PaintDungeon(currentCell.x, currentCell.y, RoomsAndMazesCellType.Ground); List <RoomsAndMazesCell> connections = new List <RoomsAndMazesCell>(); for (int i = 0; i < boudries.Length; i++) { if (IsConnection(boudries[i].x, boudries[i].y)) { boudries[i].type = RoomsAndMazesCellType.Connection; connections.Add(boudries[i]); } } while (connections.Count > 0) { RoomsAndMazesCell currentConnection = connections[Random.Range(0, connections.Count)]; connections.Remove(currentConnection); if (IsConnection(currentConnection.x, currentConnection.y)) { RoomsAndMazesCell cellToPaint = null; IterateThroughNeumannNeighbours(new CaveCellsIterator((int x, int y) => { if (grid[x][y].type == RoomsAndMazesCellType.SemiGround) { cellToPaint = grid[x][y]; } }), currentConnection.x, currentConnection.y); if (cellToPaint != null) { currentConnection.type = RoomsAndMazesCellType.Door; boudries = PaintDungeon(cellToPaint.x, cellToPaint.y, RoomsAndMazesCellType.Ground); for (int i = 0; i < boudries.Length; i++) { if (IsConnection(boudries[i].x, boudries[i].y)) { boudries[i].type = RoomsAndMazesCellType.Connection; connections.Add(boudries[i]); } else if (grid[boudries[i].x][boudries[i].y].type == RoomsAndMazesCellType.Connection) { boudries[i].type = RoomsAndMazesCellType.Wall; connections.Remove(grid[boudries[i].x][boudries[i].y]); } } } } } }
private void CleanDungeon() { IterateThroughCave(new CaveCellsIterator((int x, int y) => { RoomsAndMazesCell cell = grid[x][y]; if (cell.type != RoomsAndMazesCellType.Ground && cell.type != RoomsAndMazesCellType.Wall && cell.type != RoomsAndMazesCellType.Door) { cell.type = RoomsAndMazesCellType.Wall; } })); }
private void CreateRooms() { int currentRoomsAmount = 0; int currentAttempt = 0; while (currentAttempt < createRoomsAttempts && currentRoomsAmount < maxRoomsAmmout) { int roomWidth = Random.Range(minRoomSize, maxRoomSize); int roomHeight = Random.Range(minRoomSize, maxRoomSize); int startX = Random.Range(0, caveWidth - roomWidth); int startY = Random.Range(0, caveHeight - roomHeight); bool canDungeonBeMade = true; for (int x = startX + 1; x < startX + roomWidth - 1; x++) { for (int y = startY + 1; y < startY + roomHeight - 1; y++) { if (grid[x][y].type != RoomsAndMazesCellType.Null) { canDungeonBeMade = false; x = caveWidth; y = caveHeight; } } } if (canDungeonBeMade) { for (int x = startX; x < startX + roomWidth; x++) { for (int y = startY; y < startY + roomHeight; y++) { if (x == startX || x == startX + roomWidth - 1 || y == startY || y == startY + roomHeight - 1) { grid[x][y].type = RoomsAndMazesCellType.Wall; } else { grid[x][y].type = RoomsAndMazesCellType.SemiGround; lastRoomCell = grid[x][y]; } } } currentRoomsAmount++; } currentAttempt++; } }
private void CreateMaze() { // check all the dungeon and get all null cells and add it to the list 'nullCells' List <RoomsAndMazesCell> nullCells = new List <RoomsAndMazesCell>(); IterateThroughCave(new CaveCellsIterator((int x, int y) => { if (grid[x][y].type == RoomsAndMazesCellType.Null) { nullCells.Add(grid[x][y]); } })); // while the is an null cell inside 'nullCells' while (nullCells.Count > 0) { // create a list representing all cells that have a possibility to enter the maze List <RoomsAndMazesCell> mazePossibleCells = new List <RoomsAndMazesCell>(); // add a random null cell in the maze RoomsAndMazesCell currentCell = nullCells[Random.Range(0, nullCells.Count)]; mazePossibleCells.Add(currentCell); nullCells.Remove(currentCell); while (mazePossibleCells.Count > 0) { currentCell = mazePossibleCells[Random.Range(0, mazePossibleCells.Count)]; mazePossibleCells.Remove(currentCell); grid[currentCell.x][currentCell.y].type = RoomsAndMazesCellType.SemiGround; IterateThroughNeumannNeighbours(new CaveCellsIterator((int x, int y) => { if (grid[x][y].type == RoomsAndMazesCellType.Null) { grid[x][y].type = RoomsAndMazesCellType.SemiWall; mazePossibleCells.Add(grid[x][y]); nullCells.Remove(grid[x][y]); } else if (grid[x][y].type == RoomsAndMazesCellType.SemiWall) { grid[x][y].type = RoomsAndMazesCellType.Wall; mazePossibleCells.Remove(grid[x][y]); } }), currentCell.x, currentCell.y); } } }
protected override void InitCave() { base.InitCave(); grid = new RoomsAndMazesCell[caveWidth][]; for (int x = 0; x < caveWidth; x++) { grid[x] = new RoomsAndMazesCell[caveHeight]; for (int y = 0; y < caveHeight; y++) { grid[x][y] = new RoomsAndMazesCell(x, y); if (initWithSurroundingWalls && IsBorderCell(x, y)) { grid[x][y].type = RoomsAndMazesCellType.Wall; } } } }