/// <summary> /// Converts a generated world into a legends generator world. /// </summary> /// <param name="generated">The generated world.</param> /// <returns>The legends generator world.</returns> private World ConvertToWorld(GeneratedWorld generated) { World world = new World() { #pragma warning disable SA1101 // Prefix local calls with this. New C#9 feature. Grid = new WorldGrid(generated.Width, generated.Height), #pragma warning restore SA1101 // Prefix local calls with this }; for (int x = 0; x < generated.Width; x++) { for (int y = 0; y < generated.Height; y++) { var genSquare = generated.Grid[x, y]; WorldSquare square = this.thingFactory.CreateWorldSquare(this.Random, x, y, this.GetDefinitionName(genSquare)); // Overwrite the attributes provided by the world generation process. square.BaseAttributes[nameof(genSquare.Elevation)] = genSquare.Elevation; square.BaseAttributes[nameof(genSquare.Drainage)] = genSquare.Drainage; square.BaseAttributes[nameof(genSquare.Rainfall)] = genSquare.Rainfall; square.BaseAttributes[nameof(genSquare.Temperature)] = genSquare.Temperature; square.BaseAttributes[nameof(genSquare.Evil)] = genSquare.Evil; square.BaseAttributes[nameof(genSquare.Savagery)] = genSquare.Savagery; square.BaseAttributes[nameof(genSquare.Materials)] = genSquare.Materials; world.Grid.GetSquare(x, y).SquareDefinition = square; } } return(world); }
/// <summary> /// Generates the land. /// </summary> /// <param name="width">The width of the generated world.</param> /// <param name="height">The height of the generated world.</param> /// <param name="rdm">The random number generator.</param> /// <returns>The generated world.</returns> public static GeneratedWorld Generate(int width, int height, Random rdm) { GeneratedWorld world = new GeneratedWorld(width, height); Noise.Seed = rdm.Next(); float[,] elevationMap = Noise.Calc2D(width, height, 0.05f); Noise.Seed = rdm.Next(); float[,] rainfallMap = Noise.Calc2D(width, height, 0.05f); Noise.Seed = rdm.Next(); float[,] drainageMap = Noise.Calc2D(width, height, 0.05f); Noise.Seed = rdm.Next(); float[,] tempertureMap = Noise.Calc2D(width, height, 0.05f); Noise.Seed = rdm.Next(); float[,] evilMap = Noise.Calc2D(width, height, 0.05f); Noise.Seed = rdm.Next(); float[,] savageMap = Noise.Calc2D(width, height, 0.05f); Noise.Seed = rdm.Next(); float[,] materialsMap = Noise.Calc2D(width, height, 0.05f); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int elevation = (int)(ElevationMask(elevationMap[x, y], x, y, width, height) / 255f * 400f); int rainfall = (int)(rainfallMap[x, y] / 255f * 100f); int drainage = (int)(drainageMap[x, y] / 255f * 100f); int temperture = (int)TempertureMask(tempertureMap[x, y], y, height); int evil = (int)(evilMap[x, y] / 255f * 100f); int savagery = (int)(savageMap[x, y] / 255f * 100f); int materials = (int)(materialsMap[x, y] / 255f * 100f); world.Grid[x, y] = new GeneratedSquare() { Elevation = elevation, Rainfall = rainfall, Drainage = drainage, Temperature = temperture, Evil = evil, Savagery = savagery, Materials = materials, Water = elevation < 100, }; } } // Convert to ocean on both sides of the world. ConvertToOcean(world, 0, height / 2); ConvertToOcean(world, width, height / 2); return(world); }
/// <summary> /// Converts a square, and adjacent lake squares, into an ocean. /// </summary> /// <param name="world">The world.</param> /// <param name="x">The X coord.</param> /// <param name="y">The Y coord.</param> private static void ConvertToOcean(GeneratedWorld world, int x, int y) { if (x < 0 || x >= world.Width || y < 0 || y >= world.Height) { return; } GeneratedSquare square = world.Grid[x, y]; if (!square.Water || square.SaltWater) { return; } square.SaltWater = true; ConvertToOcean(world, x - 1, y); ConvertToOcean(world, x + 1, y); ConvertToOcean(world, x, y - 1); ConvertToOcean(world, x, y + 1); }
/// <summary> /// Generates the world. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <returns>The legends generator world.</returns> public World GenerateWorld(int width, int height) { GeneratedWorld world = LandGenerator.Generate(width, height, this.Random); return(this.ConvertToWorld(world)); }