private void createRandomModis(int numbertocreate)
    {
        currentModifikationlist.Clear();

        if (numbertocreate > modifikationlist.Count)
        {
            numbertocreate = modifikationlist.Count;
        }

        //List<modification> moditochoose = new List<modification>();
        //moditochoose = modifikationlist;
        randomHelper rndhelper = new randomHelper();

        //rndhelper.setupValues();

        for (int i = 0; i < numbertocreate; i++)
        {
            int rand = rndhelper.getRandom();
            rndhelper.sortoutRandomValue(rand);

            //Random.Range(0, _modifikationdicta.Count);
            currentModifikationlist.Add(_modifikationdicta[rand]);
            //_modifikationdicta.Remove(rand);
            //moditochoose.RemoveAt(rand);
        }
    }
Пример #2
0
    /*(random generation)path functions*/
    private bool buildPath(int positionX, int positionY, int pathLength, int oppositeDirection, mapData mapData)
    {
        randomHelper directionRandomness = new randomHelper();

        /*Initialise all possible direction for the next path tile.*/
        /*Sort out the opposite direction of the previous pathtile to reduce unnecessary checks.*/
        directionRandomness.setupValues(new List <int>()
        {
            0, 1, 2, 3
        });
        directionRandomness.sortoutRandomValue(oppositeDirection);

        /*While there is still a possible direction to check.*/
        while (!directionRandomness.isEmpty())
        {
            /*Get a random direction.*/
            int direction = directionRandomness.getRandom();

            /*Set the next tile position to the previous one.*/
            int nextPositionX = positionX;
            int nextPositionY = positionY;

            /*Calculate the opposite direction and the next tile position with the set random direction
             *           - no diagonal moves possible.*/
            switch (direction)
            {
            case 0:
                oppositeDirection = 1;
                ++nextPositionY;
                break;

            case 1:
                oppositeDirection = 0;
                --nextPositionY;
                break;

            case 2:
                oppositeDirection = 3;
                ++nextPositionX;
                break;

            case 3:
                oppositeDirection = 2;
                --nextPositionX;
                break;

            default:
                return(false);
            }

            /*Is the next tile not a possible path...*/
            if (!isPossiblePath(nextPositionX, nextPositionY, direction, mapData))
            {
                /*The random direction will be sorted out of the possible directions.*/
                directionRandomness.sortoutRandomValue(direction);

                /*New try with a new direction.*/
                continue;
            }

            /*Is the current path length equal to the target pathlength...*/
            if (pathLength == _targetPathLength)
            {
                /*Create a spawn.*/
                mapData.setTileOnLayer(nextPositionX, nextPositionY, "path", 2);

                /*Set spawn position.*/
                _spawnX = nextPositionX;
                _spawnY = nextPositionY;

                /*Set the pathlength.*/
                //_actualPathLength = _targetPathLength;

                /*Reset the target pathlength back to the maximum path length.*/
                _targetPathLength = _maxPathLength;

                _pathList.Add(new path(direction, nextPositionX, nextPositionY));

                return(true);
            }

            /*If it is a possible path create a path tile on that position.*/
            mapData.setTileOnLayer(nextPositionX, nextPositionY, "path", 2);

            /*If the path generation finds a valid path it returns true.*/
            if (buildPath(nextPositionX, nextPositionY, pathLength + 1, oppositeDirection, mapData))
            {
                _pathList.Add(new path(direction, nextPositionX, nextPositionY));

                return(true);
            }
            else
            {
                /*If somewhere the path generation got stucked - the path generation will go back to
                 * the previous tile and tries out another direction till he found a valid way. The direction
                 * that does not work will be sorted out and the path tile that has been created will be destroyed
                 * and a wall will be placed there to symbolize the path generation there is no way in that direction.*/
                directionRandomness.sortoutRandomValue(direction);
                mapData.setTileOnLayer(nextPositionX, nextPositionY, "path", 1);
            }
        }

        /*If all possible direction do not include a possible path the target path length will be reduced
         * - to avoid that the path generation gets stucked but it will never go lower than the minimum pathlength
         * to generate maps that are worth playing.*/
        if (_targetPathLength > _minPathLength)
        {
            _targetPathLength--;
        }

        return(false);
    }