/// <summary> /// Instantiates a new instance of the RootGenConfig scriptable object with the default parameters, and attempts to /// populate its fields with JSON data. /// </summary> /// <param name="path">The path of the JSON file containing the desired data.</param> /// <returns> /// A new instance of the RootGenConfig scriptable object, populated with the desired JSON data if the path exists or /// the default parameters for RootGenConfig if it does not. /// </returns> public static RootGenConfig FromJson(string configName) { RootGenConfig result = ScriptableObject.CreateInstance <RootGenConfig>(); string path = Path.Combine(Application.persistentDataPath, configName + ".json"); JsonUtility.FromJsonOverwrite(path, result); return(result); }
// DESTRUCTORS ~~~~~~~~~~ // DELEGATES ~~~~~~~~~~ // EVENTS ~~~~~~~~~~ // ENUMS ~~~~~~~~~~ // INTERFACES ~~~~~~~~~~ // PROPERTIES ~~~~~~~~~~ // INDEXERS ~~~~~~~~~~ // METHODS ~~~~~~~~~ /// <summary> /// Generate a hex map using the provided configuration. /// </summary> /// <param name="config">A RootGenConfig scriptable object.</param> /// <returns> /// A HexGrid generated according to the config settings. /// </returns> public HexMap GenerateMap( RootGenConfig config, bool editMode = true ) { Destroy.DestroyAll <HexMap>(); HexMap result = _mapGenerator.GenerateMap( config ); HexGridCamera.AttachCamera(result, config); return(result); }
/// <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); }
public static void AttachCamera(HexMap grid, RootGenConfig config) { AttachCamera(grid, config.hexSize); }