Esempio n. 1
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);
        }