//private System.Random intInRangePicker;

    public ShapeCellularAutomaton(int seed, int randomFillPercent, int smoothSteps)
    {
        this.seed = seed;
        this.randomFillPercent = randomFillPercent;
        this.smoothSteps       = smoothSteps;

        //this.intInRangePicker = new System.Random(seed.GetHashCode());

        this.intInRangePicker = new CustomSeededPickerStrategy(seed);
    }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
    private void addTrees(GameObject boardHolder, OIGrid grid)
    {
        GameObject treesContainer = new GameObject("Trees");

        treesContainer.transform.parent = boardHolder.transform;

        int             highPolyTreeThreshold = _behavior._highPolyTreeChance;
        IPickerStrategy seededStrategy        = new CustomSeededPickerStrategy(_behavior._seed);

        ChildInGameObjectPicker lowPolyTreePicker    = new ChildInGameObjectPicker(_behavior._lowPolyTreePrefabs, seededStrategy);
        ChildInGameObjectPicker highPolyTreePicker   = new ChildInGameObjectPicker(_behavior._highPolyTreePrefabs, seededStrategy);
        IntInRangePicker        treeTypeChancePicker = new IntInRangePicker(0, 100, seededStrategy);
        FloatInRangePicker      treeScalePicker      = new FloatInRangePicker(0.2f, 0.5f, seededStrategy);

        float floorSpan = TILE_SIZE;

        grid.forEach2((rowZ, colX, value) => {
            if (value != TREE)
            {
                return;
            }

            float treeScale = treeScalePicker.draw();
            int treeChance  = treeTypeChancePicker.draw();
            GameObject selectedTree;
            if (treeChance < highPolyTreeThreshold)
            {
                selectedTree = highPolyTreePicker.draw();
            }
            else
            {
                selectedTree = lowPolyTreePicker.draw();
            }
            float xPos                   = colX * floorSpan + 2;
            float zPos                   = -rowZ * floorSpan - 2;
            float yRot                   = xPos * zPos * 1000 % 360;
            GameObject created           = istantiate(selectedTree, xPos, zPos, yRot, treesContainer, rowZ, colX, "Tree");
            created.transform.localScale = new Vector3(treeScale, treeScale, treeScale);
        });
    }
Ejemplo n.º 4
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);
        }