IEnumerator GenerateInProgress() { finished = false; if (useRandomSeed) { seed = DateTime.Now.ToString(); } Random.State oldState = Random.state; int seedHash = seed.GetHashCode(); Random.InitState(seedHash); yield return(new WaitForEndOfFrame()); for (int i = 0; i < randCol.Length; i++) { randCol[i] = Random.ColorHSV(0, 1, 0, 1, 0.5f, 1); } yield return(new WaitForEndOfFrame()); map = new LandMap(islandData.maxWidth, islandData.maxHeight); // Fill the map randomly with 0s and 1s based on percentage fill map.RandomFillMap(islandData.randomFillPercent); // Mold to the base shape if (islandData.baseShape) { map.makeBaseShape(islandData.baseShape); } // Smooth the map 5 times map.SmoothMap(5); yield return(new WaitForEndOfFrame()); meshGen = GetComponent <IslandMeshGenerator> (); vertDatabase.Clear(); vertDatabase.tileSize = islandData.tileSize; // Find separated regions to form an island List <MapRegion> regions = map.GetRegions(); yield return(new WaitForEndOfFrame()); // Create separate islands SeparateIslands(regions); yield return(new WaitForEndOfFrame()); clk.Start(); vertDatabase.SetCoordDB(); LoggerTool.Post("Indexing takes " + clk.Elapsed() + " seconds."); yield return(new WaitForEndOfFrame()); if (shouldElevate) { int highestPeak = 0; foreach (IsleInfo island in islands) { int peak = island.surfaceMeshDetail.localPeak; highestPeak = peak > highestPeak ? peak : highestPeak; } foreach (IsleInfo island in islands) { island.surfaceMeshDetail.NormalizeGradientMap(highestPeak); } vertDatabase.SetVerticesInlandPos(islands, islandData.mountainCurve); ElevationGenerator elevGen = GetComponent <ElevationGenerator> (); elevGen.elevateSurface(islands, islandData.altitude, islandData.mountainCurve, surfaceNoiseData, seedHash, 0, vertDatabase); // elevate hills on the surface elevGen.elevateSurface(islands, -islandData.stalactite, islandData.bellyCurve, undersideNoiseData, seedHash, 2, vertDatabase); // extend stakes at surface below } yield return(new WaitForEndOfFrame()); clk.Start(); int zoneNum = DoClustering(regions, map.spots, vertDatabase, clusterAnalysis, seedHash); LoggerTool.Post("Clustering takes " + clk.Elapsed() + " seconds."); yield return(new WaitForEndOfFrame()); // Find strategic locations in each region List <MapRegion> zones = map.GetZones(zoneNum); SpliceTerritory(zones); yield return(new WaitForEndOfFrame()); SetColliders(); yield return(new WaitForEndOfFrame()); PlacementGenerator placement = GetComponent <PlacementGenerator> (); if (placement && decorateTerrain) { placement.GenerateTrees(islands); placement.GenerateSectorsContent(sectors, vertDatabase); } else if (placement) { //placement.GeneratePlacements (islands); placement.GenerateSectorsContent(sectors, vertDatabase); } yield return(new WaitForEndOfFrame()); if (flatShading) { foreach (IsleInfo island in islands) { for (int surfaceIndex = 0; surfaceIndex < 3; surfaceIndex++) { MeshFilter mf = island.GetSurfaceMesh(surfaceIndex); float oldVertCount = mf.sharedMesh.vertexCount; mf.sharedMesh = FlatShade.DuplicateSharedVertex(mf.sharedMesh); float newVertCount = mf.sharedMesh.vertexCount; //Debug.Log (mf.transform.parent.name + "." + mf.transform.name + " new vertices are at " + (newVertCount / oldVertCount * 100) + "% with " + newVertCount + " verts."); } } } Random.state = oldState; finished = true; yield return(null); }
public GamestateRandomTestingUnit(int seed) { Random.InitState(seed); currentState = Random.state; }
private void Start() { randomState = Random.state; }
private void Awake() { _shapes = new List <Shape>(); _mainRandomState = Random.state; }
/// <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); }