예제 #1
0
        public CaveBoard asBoard()
        {
            checkConstraints();

            Board board = _dunGen.asBoard();
            ShapeCellularAutomaton     roomAlgo    = new ShapeCellularAutomaton(_seed, _cellularFillChance, _cellularSmoothingStep);
            CustomSeededPickerStrategy shapePicker = new CustomSeededPickerStrategy(_seed);

            CaveBoard      result    = new CaveBoard(board.rows(), board.cols());
            List <IXShape> onlyRooms = new List <IXShape>();

            _logger.info("Rooms: " + board.rooms().Length);
            foreach (Room each in board.rooms())
            {
                Cell       leftVert = each.topLeftVertex();
                int        rows     = each.height();
                int        cols     = each.width();
                APolyShape currentRoom; //Select a shape for the Room
                if (shapePicker.drawBetween(0, 100) < 50)
                {
                    currentRoom = new RectShape(leftVert, new OIGrid(rows, cols));
                }
                else
                {
                    currentRoom = new ElliShape(leftVert, new OIGrid(rows, cols));
                }
                _logger.info("Shape type: " + currentRoom.GetType());
                roomAlgo.applyOn(currentRoom);

                _logger.info("Shape regions before clean: " + currentRoom.regionsNumber());
                if (!currentRoom.hasRegions())
                {
                    _logger.warning("No Region found. Room will be skipped!!!");
                    continue;
                }
                currentRoom.deleteRegionsButTheBiggest();
                _logger.info("Shape regions after clean: " + currentRoom.regionsNumber());


                onlyRooms.Add(currentRoom);
                if (onlyRooms.Count > 1)
                {
                    IXShape  previousRoom    = onlyRooms[onlyRooms.Count - 2];
                    int      corrIndex       = onlyRooms.Count - 2;
                    Corridor corr            = board.corridors()[corrIndex];
                    int      corridorSection = corr.isVertical() ? corr.width() : corr.height();
                    result.addCorridor(CaveCorridorFactory.createCorrShape(previousRoom, currentRoom, corridorSection));
                }
                result.addRoom(currentRoom);
            }
            return(result);
        }
예제 #2
0
        public virtual Board asBoard()
        {
            checkConstraints();
            if (!isBoardCleared())
            {
                return(_board);
            }
            //_board = new Board(_mapRows, _mapColumns);
            _board = new Board(_mapRows - _mapMargin * 2, _mapColumns - _mapMargin * 2);

            //IPickerStrategy seedStrategy = new RandomSeededPickerStrategy(_seed);
            CustomSeededPickerStrategy seedStrategy = new CustomSeededPickerStrategy(_seed);

            seedStrategy.setLogger(_logger);

            IntInRangePicker    roomNumberPicker = new IntInRangePicker(_roomsNumberMin, _roomsNumberMax, seedStrategy);
            IntInRangePicker    roomSizePicker   = new IntInRangePicker(_roomSizeMin, _roomSizeMax, seedStrategy);
            IntInRangePicker    corrLengthPicker = new IntInRangePicker(_corridorLengthMin, _corridorLengthMax, seedStrategy);
            IntInRangePicker    corrWidthPicker  = new IntInRangePicker(_corridorWidthMin, _corridorWidthMax, seedStrategy);
            CardinalPointPicker cardPointPicker  = new CardinalPointPicker(seedStrategy);
            CellInRangePicker   cellRangePicker  = new CellInRangePicker(seedStrategy);

            int roomNumber = roomNumberPicker.draw();

            if (roomNumber < 1)
            {
                _logger.warning("Room number should be at least 1. Instead is: " + roomNumber);
                return(_board);
            }

            Grid grid             = new Grid(roomSizePicker.draw(), roomSizePicker.draw());
            Cell topLeftVertexMin = new Cell(0, 0);
            Cell topLeftVertexMax = new Cell(_board.rows() - 1, _board.cols() - 1).minusSize(grid.rows(), grid.columns());
            Cell topLeftCell      = cellRangePicker.drawBetween(topLeftVertexMin, topLeftVertexMax);
            Room lastRoom         = new Room(topLeftCell, grid);

            if (!_board.fitsIn(lastRoom))
            {
                _logger.error("First room not fit in. This should never happen");
                return(_board);
            }
            _logger.info("OK: " + lastRoom);
            _board.addRoom(lastRoom);

            CardinalPoint lastDirection       = 0;
            int           roomCreationAttempt = 1;

            for (int i = 1; i < roomNumber; i++)
            {
                //If room and corridor has not been dropped, pick random direction
                if (roomCreationAttempt == 1)
                {
                    lastDirection = cardPointPicker.draw();
                }
                else     //else force to next direction
                {
                    lastDirection = cardPointPicker.nextClockwise(lastDirection);
                }

                //Try to create a Corridor on a direction if there is enough space
                int      cardinalPointAttempt = 1;
                Corridor lastCorridor         = null;
                do
                {
                    lastCorridor = generateCorridor(lastDirection, lastRoom, corrLengthPicker, corrWidthPicker, cellRangePicker);
                    if (!_board.fitsIn(lastCorridor))
                    {
                        _logger.info("NO FITS: " + lastCorridor + " " + lastDirection);
                        lastCorridor = null;
                        cardinalPointAttempt++;
                        lastDirection = cardPointPicker.nextClockwise(lastDirection);
                    }
                } while (cardinalPointAttempt <= 4 && lastCorridor == null);

                //If no corridor has been created, then terminate the algorithm
                if (lastCorridor == null)
                {
                    _logger.warning("PROCEDURAL GENERATION INTERRUPTED: No more chance for a Corridor to fit in");
                    break;
                }
                _logger.info("OK: " + lastCorridor + " " + lastDirection);
                _board.addCorridor(lastCorridor);

                //Try to create a room. If there is enough space retry (until 4 times) restarting from a new corridor
                Room newRoom = generateRoom(lastDirection, lastCorridor, roomSizePicker, cellRangePicker);
                if (!_board.fitsIn(newRoom))
                {
                    _board.removeLast(); //remove last item added to the board (a corridor in this case)
                    if (roomCreationAttempt <= 4)
                    {
                        _logger.info("NO FITS: " + newRoom + " Retry: " + roomCreationAttempt);
                        roomCreationAttempt++;
                        i--;
                        continue;
                    }
                    else
                    {
                        _logger.warning("PROCEDURAL GENERATION INTERRUPTED: No more chance for a Room to fit in");
                        break;
                    }
                }
                else
                {
                    _logger.info("OK: " + newRoom + " Retry: " + roomCreationAttempt);
                    lastRoom            = newRoom;
                    roomCreationAttempt = 1;
                    _board.addRoom(lastRoom);
                }
            }

            if (_mapMargin > 0)
            {
                _board = _board.resize(_mapMargin);
            }
            if (_mapCropEnabled)
            {
                _board = _board.crop(_mapMargin);
            }
            return(_board);
        }