コード例 #1
0
        public void Snapshot_Argument0_InitialEqualsSnapshot()
        {
            Random.State initialState = Random.state;
            Random.State snapshot     = RandomState.Snapshot(0);

            Assert.AreEqual(initialState, snapshot);
        }
コード例 #2
0
        public void Snapshot_Argument0_InitialDoesNotEqualCurrent()
        {
            Random.InitState(-1);
            Random.State initialState = Random.state;
            RandomState.Snapshot(0);

            Assert.AreNotEqual(initialState, Random.state);
        }
コード例 #3
0
/// <summary>
/// Constructor.
/// </summary>
/// <param name="hashGridSize">
///     The size of the hash grid. The larger this value, the
///     less reptition of random values across a given range
///     of coordinates. To eliminate repitition this value should
///     be equal to the maximum x or y coordinate, which ever is
///     larger.
/// </param>
/// <param name="hashGridScale">
///     Density of the the unique values per unit square. A value
///     of 1 produces a unique value for every unique coordinate,
///     while a value of .25f produces a unique value every 4 unique
///     coordinates square.
/// </param>
/// <param name="seed">
///     The random seed value used to create the values in the
///     hash grid.
/// </param>
/// <param name="randomValuesPerCooridnate">
///     The number of random values stored per coordinate.
/// </param>
    public RandomHashGrid(
        int hashGridSize,
        float hashGridScale,
        int seed,
        int randomValuesPerCooridnate
        )
    {
        _hashGridSize  = hashGridSize;
        _hashGridScale = hashGridScale;

        _grid = new RandomHash[hashGridSize * hashGridSize];

// Snapshot the current random state and initialize new random state with seed.
        Random.State snapshot = RandomState.Snapshot(seed);

// Populate grid with RandomHash structs.
        for (int i = 0; i < _grid.Length; i++)
        {
            _grid[i] = new RandomHash(randomValuesPerCooridnate);
        }

// Restore random state from snapshot.
        Random.state = snapshot;
    }
コード例 #4
0
    /// <summary>
    /// Generate a HexMap using the standard RootGen algorithm.
    /// </summary>
    /// <param name="config">
    /// The configuration data for the map to be generated.
    /// </param>
    /// <returns>
    ///A randomly generated HexMap object.
    /// </returns>
    public HexMap GenerateMap(
        RootGenConfig config,
        bool editMode = true
        )
    {
        string diagnostics = "Generate Map Performance Diagnostics\n\n";

        HexMap result = HexMap.CreateHexMapGameObject();
        int    seed;

        if (config.useFixedSeed)
        {
            seed = config.seed;
        }
        else
        {
            config.seed  = Random.Range(0, int.MaxValue);
            config.seed ^= (int)System.DateTime.Now.Ticks;
            config.seed ^= (int)Time.time;
            config.seed &= int.MaxValue;
            seed         = config.seed;
        }

        // Snapshot the initial random state before consuming the random
        // sequence.
        Random.State snapshot = RandomState.Snapshot(seed);

        result.Initialize(
            new Rect(0, 0, config.width, config.height),
            seed,
            config.hexSize,
            config.wrapping,
            editMode
            );

        foreach (Hex hex in result.Hexes)
        {
            hex.WaterLevel = config.waterLevel;
        }

        Stopwatch stopwatch = new Stopwatch();

        stopwatch.Start();

        HexMapTectonics hexMapTectonics = new HexMapTectonics(
            result,
            config.regionBorder,
            config.mapBorderX,
            config.mapBorderZ,
            config.numRegions
            );

        int landBudget = Mathf.RoundToInt(
            result.SizeSquared *
            config.landPercentage *
            0.01f
            );

        TectonicParameters tectonicParameters =
            new TectonicParameters(
                config.hexSize,
                config.highRiseProbability,
                config.jitterProbability,
                config.sinkProbability,
                config.elevationMax,
                config.elevationMin,
                config.waterLevel,
                landBudget,
                config.maximumRegionDensity,
                config.minimumRegionDensity
                );

        string logString = "Tectonic Statistics\n";

        for (int i = 0; i < MAX_TECTONIC_STEPS; i++)
        {
            tectonicParameters.LandBudget = hexMapTectonics.Step(
                tectonicParameters
                );

            //result.Draw(config.hexSize);

            logString +=
                "Step " + i + ", Land Hexes: " +
                result.LandHexes.Count + " / Land Budget: " +
                tectonicParameters.LandBudget + " Total: " +
                (result.LandHexes.Count + tectonicParameters.LandBudget) + "\n";

            if (tectonicParameters.LandBudget == 0)
            {
                break;
            }
        }

        RootLog.Log(logString, Severity.Information, "Diagnostics");

        // If land budget is greater than 0, all land hexes specified to
        // be allocated were not allocated successfully. Log a warning,
        // decrement the remaining land budget from the result, and return
        // the result as the number of land hexes allocated.
        if (tectonicParameters.LandBudget > 0)
        {
            RootLog.Log(
                "Failed to use up " + tectonicParameters.LandBudget + " land budget.",
                Severity.Warning,
                "MapGenerator"
                );
        }

        stopwatch.Stop();
        diagnostics += "Generate Tectonics: " + stopwatch.Elapsed + "\n";

        stopwatch.Start();

        HexMapErosion erosion       = new HexMapErosion(result);
        int           erodibleHexes = erosion.ErodibleHexes.Count;

        // Calculate the target number of uneroded hexes.
        int targetUnerodedHexes =
            (int)(
                erodibleHexes *
                (100 - config.erosionPercentage) *
                0.01f
                );

        while (erosion.ErodibleHexes.Count > targetUnerodedHexes)
        {
            erosion.Step(
                config.hexSize
                );
        }

        stopwatch.Stop();
        diagnostics += "Generate Erosion: " + stopwatch.Elapsed + "\n";


        stopwatch.Start();

        HexMapClimate hexMapClimate = new HexMapClimate(
            result,
            config.startingMoisture
            );


        ClimateParameters climateParameters = new ClimateParameters(
            config.hemisphere,
            config.windDirection,
            config.evaporationFactor,
            config.highTemperature,
            config.lowTemperature,
            config.precipitationFactor,
            config.runoffFactor,
            config.seepageFactor,
            config.temperatureJitter,
            config.windStrength,
            config.hexSize,
            config.elevationMax,
            config.waterLevel
            );

        for (int i = 0; i < config.initialClimateSteps; i++)
        {
            hexMapClimate.Step(climateParameters);
        }

        List <ClimateData> climates = hexMapClimate.List;

        stopwatch.Stop();
        diagnostics += "Generate Climate: " + stopwatch.Elapsed + "\n";

        stopwatch.Start();

        HexMapRivers hexMapRivers = new HexMapRivers(
            result,
            climates,
            config.waterLevel,
            config.elevationMax
            );

        for (int i = 0; i < config.numInitialRivers; i++)
        {
            hexMapRivers.StartRiver();
        }

        for (int i = 0; i < config.numInitialRiverSteps; i++)
        {
            hexMapRivers.Step(
                config.waterLevel,
                config.elevationMax,
                config.extraLakeProbability,
                config.hexSize,
                climates
                );
        }

        result.RiverDigraph = hexMapRivers.RiverDigraph;

        stopwatch.Stop();
        diagnostics += "Generate Rivers: " + stopwatch.Elapsed + "\n";

        stopwatch.Start();

        hexMapClimate.RefreshTerrainTypes(
            climateParameters,
            result.RiverDigraph
            );

        stopwatch.Stop();
        diagnostics += "Assign Terrain Types: " + stopwatch.Elapsed + "\n";

        RootLog.Log(
            diagnostics,
            Severity.Information,
            "Diagonstics"
            );

        // Restore the snapshot of the random state taken before consuming
        // the random sequence.
        Random.state = snapshot;
        return(result);
    }