Beispiel #1
0
    void GenerateBlocks()
    {
        //Choose random side
        //Check for another block in this location
        //if it is - create block in another location
        //else create block
        //repeat

        ValueChance[] valueChances = new ValueChance[4];

        //Creating first block
        CreateBlock(new Vector2Int(Random.Range(1, blockTiles.GetLength(0) - 2), (Random.Range(1, blockTiles.GetLength(1) - 2))));


        for (int i = 0; i < blocksCount; i++)
        {
            RestoreValueChance(valueChances);

            currentSide = (Side)Randomizer.GetRandomValue(valueChances);
            int k = 0;
            //out of the map check
            while ((currentBlock.x + SideToVector(currentSide).x) >= blockTiles.GetLength(0) - 1 ||
                   (currentBlock.y + SideToVector(currentSide).y) >= blockTiles.GetLength(1) - 1 ||
                   (currentBlock.x + SideToVector(currentSide).x) <= 0 ||
                   (currentBlock.y + SideToVector(currentSide).y) <= 0 ||
                   blockTiles[currentBlock.x + SideToVector(currentSide).x, currentBlock.y + SideToVector(currentSide).y] == true ||

                   blockTiles[currentBlock.x + SideToVector(currentSide).x + SideToVector(RightSide(currentSide)).x,
                              currentBlock.y + SideToVector(currentSide).y + SideToVector(RightSide(currentSide)).y] == true &&
                   blockTiles[currentBlock.x + SideToVector(RightSide(currentSide)).x,
                              currentBlock.y + SideToVector(RightSide(currentSide)).y] == true ||

                   blockTiles[currentBlock.x + SideToVector(currentSide).x + SideToVector(LeftSide(currentSide)).x,
                              currentBlock.y + SideToVector(currentSide).y + SideToVector(LeftSide(currentSide)).y] == true &&
                   blockTiles[currentBlock.x + SideToVector(LeftSide(currentSide)).x,
                              currentBlock.y + SideToVector(LeftSide(currentSide)).y] == true)
            {
                for (int j = 0; j < valueChances.Length; j++)
                {
                    if (valueChances[j].value == (int)currentSide)
                    {
                        valueChances[j].chance = 0;
                    }
                }
                currentSide = (Side)Randomizer.GetRandomValue(valueChances);
                k++;
                if (k >= valueChances.Length)
                {
                    int num = Random.Range(0, blockList.Count - 1);
                    currentBlock = blockList[num];
                    blockList.Remove(blockList[num]);
                    RestoreValueChance(valueChances);
                }
            }

            {
                CreateBlock(currentBlock + SideToVector(currentSide));
            }
        }
    }
Beispiel #2
0
    void GenerateFurniture()
    {
        //Place required rooms first
        int requiredCount = 0;

        foreach (RoomType roomType in sector.requiredRooms)
        {
            _rooms[requiredCount].roomType = roomType;
        }

        //Randomly choose from the optional ones
        ValueChance[] valueChance = new ValueChance[sector.optionalRooms.Count];
        for (int i = 0; i < sector.optionalRooms.Count; i++)
        {
            valueChance[i].value  = i;
            valueChance[i].chance = sector.optionalRooms[i].roomChance;
        }

        foreach (Room room in _rooms)
        {
            int         tileCount      = (room.endPos.x - room.startPos.x) * (room.endPos.y - room.startPos.y);
            float       percentage     = Random.Range(room.roomType.minFill, room.roomType.maxFill);
            List <Tile> doorTiles      = new List <Tile>();
            List <Tile> tiles          = new List <Tile>();
            List <Tile> furnitureTiles = new List <Tile>();
            for (int i = room.startPos.x; i <= room.endPos.x; i++)
            {
                for (int j = room.startPos.y; j <= room.endPos.y; j++)
                {
                    bool tileIsDoor = false;
                    foreach (Connection connection in room.connections)
                    {
                        if (connection.from == _tiles[i, j])
                        {
                            tileIsDoor = true;
                        }
                    }
                    if (tileIsDoor)
                    {
                        doorTiles.Add(_tiles[i, j]);
                    }

                    else
                    {
                        tiles.Add(_tiles[i, j]);
                    }
                }
            }
            if (room.roomType == null)
            {
                room.roomType = sector.optionalRooms[Randomizer.GetRandomValue(valueChance)];
            }

            for (int i = 0; i < room.roomType.requiredFurnitures.Count; i++)
            {
                Furniture   currentFurniture = room.roomType.requiredFurnitures[i];
                List <Tile> tilesToCheck     = tiles;
                int         k = Random.Range(currentFurniture.minCount, currentFurniture.maxCount);
                while (tiles.Count > 0 && (percentage <= (furnitureTiles.Count / tileCount)) && k > 0)
                //while there is more created furniture than empty tiles (in percentage), or there's no more empty tiles
                {
                    //Form furniture conditions, and if there's no tiles that it can be placed on, we will place other furniture
                    bool bad = true;
                    while (tilesToCheck.Count > 0 && bad)
                    {
                        //Take random tile
                        Tile currentTile = tilesToCheck[Random.Range(0, tiles.Count - 1)];
                        tilesToCheck.Remove(currentTile);
                        //if furniture blocks way
                        if (currentFurniture.blocksOut)
                        {
                            //check path to doors
                            foreach (Connection connection in room.connections)
                            {
                                List <Tile> path = new List <Tile>();
                                IsBlockingPath(currentTile, path, connection);
                            }
                        }
                        //Check should it be near the wall or not
                        if (currentFurniture.nearTheWall)
                        {
                            isNearWall(currentTile.coordinates, room);
                        }
                        //Check should it be in line with other tiles of this type or not
                        if (currentFurniture.lineUp)
                        {
                            IsLineUp(new Vector2Int(currentTile.coordinates.x, currentTile.coordinates.y), currentFurniture);
                        }
                    }
                    // k--;
                }
                //do the same for optional furniture
            }
        }
    }
Beispiel #3
0
    void GenerateRooms()
    {
//        foreach(Tile t in connectorTiles.ToArray())
        while (connectorTiles.Count > 0)
        {
            if (limitRoomsCount)
            {
                if (roomsLimit <= roomsCount)
                {
                    break;
                }
            }

            ValueChance[] connectorValueChance = new ValueChance[2];
            connectorValueChance[0].value  = (int)ConnectorType.Corridor;
            connectorValueChance[0].chance = connectorChanceCorridor;
            connectorValueChance[1].value  = (int)ConnectorType.Room;
            connectorValueChance[1].chance = connectorChanceRoom;

            Tile t;

            ConnectorType connectorType = (ConnectorType)Randomizer.GetRandomValue(connectorValueChance);
            int           c             = 0;
            do
            {
                t = connectorTiles[Random.Range(0, connectorTiles.Count)];
                c++;
            }while (c < 10 && t.connectorType != connectorType);

            Vector2Int connectPosition = Vector2Int.zero;
            Side       heightSide      = 0;
            bool       deadConnector   = true;
            for (int i = 0; i < sideCount; i++)
            {
                connectPosition = t.coordinates + SideToVector((Side)i);
                if ((connectPosition.x < _tiles.GetLength(0) && connectPosition.y < _tiles.GetLength(1)) && (connectPosition.x > 0 && connectPosition.y > 0))
                {
                    if (_tiles[connectPosition.x, connectPosition.y] == null)
                    {
                        deadConnector = false;
                        heightSide    = (Side)i;
                        break;
                    }
                }
            }
            if (!deadConnector)
            {
                Side adjacentSide = 0;
                bool roomCreated  = false;
                int  k            = 0;

                while (!roomCreated)
                {
                    if (k > 1)
                    {
                        break;
                    }
                    if (k == 0)
                    {
                        for (int i = 0; i < 4; i++)
                        {
                            if ((Side)i != heightSide && (Side)i != AdjacentSide(heightSide))
                            {
                                adjacentSide = (Side)i;
                            }
                        }
                    }
                    else
                    {
                        adjacentSide = AdjacentSide(adjacentSide);
                    }
                    k++;

                    List <Vector2Int> obstacles   = new List <Vector2Int>();
                    List <Vector2Int> roomEndList = new List <Vector2Int>();
                    for (int i = 0; i < roomMaxSize; i++)
                    {
                        for (int j = 0; j < roomMaxSize; j++)
                        {
                            int x, y = 0;
                            //check all tiles in one random direction
                            //change height and adjacent due to direction
                            //Check each tile, can it be room edge or not
                            //(Check for enough length and no obstacles)
                            if (heightSide == Side.up || heightSide == Side.down)
                            {
                                x = connectPosition.x + i * SideToVector(adjacentSide).x;
                                y = connectPosition.y + j * SideToVector(heightSide).y;
                            }
                            else
                            {
                                x = connectPosition.x + i * SideToVector(heightSide).x;
                                y = connectPosition.y + j * SideToVector(adjacentSide).y;
                            }
                            if ((x < _tiles.GetLength(0) && y < _tiles.GetLength(1)) && (x > 0 && y > 0))
                            {
                                if (_tiles[x, y] != null)
                                {
                                    if (i <= roomMinSize || j <= roomMinSize)
                                    {
                                        deadConnector = true;
                                    }
                                    else
                                    {
                                        obstacles.Add(_tiles[x, y].coordinates);
                                    }
                                }
                                else
                                {
                                    if (i >= roomMinSize && j >= roomMinSize)
                                    {
                                        roomEndList.Add(new Vector2Int(x, y));
                                    }
                                }
                            }
                        }
                        if (deadConnector)
                        {
                            break;
                        }
                    }
                    //Check how big room can be
                    //If space is enough - create random size room
                    //choose EndPos randomly

                    if (roomEndList.Count > 0 && !deadConnector)
                    {
                        int        opa = 0;
                        Vector2Int start = Vector2Int.zero, end = Vector2Int.zero;
                        Vector2Int roomEnd;
                        bool       deadEnd;
                        do
                        {
                            opa++;
                            roomEnd = roomEndList[Random.Range(0, roomEndList.Count - 1)];
                            deadEnd = false;

                            //startpos should allways has less x and y and if it's not swap х or y or both
                            if (connectPosition.x > roomEnd.x)
                            {
                                start.x = roomEnd.x;
                                end.x   = connectPosition.x;
                            }
                            else
                            {
                                start.x = connectPosition.x;
                                end.x   = roomEnd.x;
                            }

                            if (connectPosition.y > roomEnd.y)
                            {
                                start.y = roomEnd.y;
                                end.y   = connectPosition.y;
                            }
                            else
                            {
                                start.y = connectPosition.y;
                                end.y   = roomEnd.y;
                            }
                            foreach (Vector2Int obstacle in obstacles)
                            {
                                if (obstacle.x >= start.x && obstacle.y >= start.y && obstacle.x <= end.x && obstacle.x <= obstacle.y)
                                {
                                    deadEnd = true;
                                    break;
                                }
                            }
                            if (opa > 30)
                            {
                                Debug.Log("Shit");
                                deadEnd = false;
                            }
                        }while (deadEnd);

                        _rooms.Add(CreateRoom(start, end));
                    }
                }
            }
            connectorTiles.Remove(t);
            //delete tile from connectors
        }
        //repeat until connectors isn't empty
    }