private static void ScaleMap(Overworld.MapData[,] map, int width, int height, Overworld.ScalarFieldType fieldType) { float min = 99999; float max = -99999; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { float v = map[x, y].GetValue(fieldType); if (v < min) { min = v; } if (v > max) { max = v; } } } for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { float v = map[x, y].GetValue(fieldType); map[x, y].SetValue(fieldType, ((v - min) / (max - min)) + 0.001f); } } }
public OverworldData(Overworld.MapData[,] map, string name) { int sizeX = map.GetLength(0); int sizeY = map.GetLength(1); Biomes = new int[sizeX, sizeY]; Erosion = new float[sizeX, sizeY]; Faults = new float[sizeX, sizeY]; Rainfall = new float[sizeX, sizeY]; Temperature = new float[sizeX, sizeY]; Water = new int[sizeX, sizeY]; Weathering = new float[sizeX, sizeY]; Height = new float[sizeX, sizeY]; Name = name; for (int x = 0; x < sizeX; x++) { for (int y = 0; y < sizeY; y++) { Overworld.MapData data = map[x, y]; Biomes[x, y] = (int)data.Biome; Erosion[x, y] = (data.Erosion); Faults[x, y] = (data.Faults); Height[x, y] = (data.Height); Rainfall[x, y] = (data.Rainfall); Temperature[x, y] = (data.Temperature); Water[x, y] = (int)(data.Water); Weathering[x, y] = (data.Weathering); } } Screenshot = CreateTexture(PlayState.Game.GraphicsDevice, sizeX, sizeY); }
public OverworldData(GraphicsDevice device, Overworld.MapData[,] map, string name, float seaLevel) { int sizeX = map.GetLength(0); int sizeY = map.GetLength(1); Name = name; SeaLevel = seaLevel; Data = map; Screenshot = CreateTexture(device, sizeX, sizeY, seaLevel); FactionList = new List <FactionDescriptor>(); byte id = 0; foreach (Faction f in Overworld.NativeFactions) { FactionList.Add(new FactionDescriptor() { Name = f.Name, PrimaryColory = f.PrimaryColor, SecondaryColor = f.SecondaryColor, Id = id, Race = f.Race.Name, CenterX = f.Center.X, CenterY = f.Center.Y, GoodWill = f.GoodWill }); id++; } }
public void LoadFromTexture(Texture2D Texture) { Data = new Overworld.MapData[Texture.Width, Texture.Height]; var colorData = new Color[Texture.Width * Texture.Height]; GameState.Game.GraphicsDevice.BlendState = BlendState.NonPremultiplied; Texture.GetData(colorData); Overworld.DecodeSaveTexture(Data, Texture.Width, Texture.Height, colorData); }
private void Erode(int width, int height, float seaLevel, Overworld.MapData[,] heightMap, int numRains, int rainLength, int numRainSamples, float[,] buffer) { float remaining = 1.0f - Progress.Value - 0.2f; float orig = Progress.Value; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { buffer[x, y] = heightMap[x, y].Height; } } for (int i = 0; i < numRains; i++) { LoadingMessage = "Erosion " + i + "/" + numRains; Progress.Value = orig + remaining * ((float)i / (float)numRains); Vector2 currentPos = new Vector2(0, 0); Vector2 bestPos = currentPos; float bestHeight = 0.0f; for (int k = 0; k < numRainSamples; k++) { int randX = PlayState.Random.Next(1, width - 1); int randY = PlayState.Random.Next(1, height - 1); currentPos = new Vector2(randX, randY); float h = Overworld.GetHeight(buffer, currentPos); if (h > bestHeight) { bestHeight = h; bestPos = currentPos; } } currentPos = bestPos; const float erosionRate = 0.99f; Vector2 velocity = Vector2.Zero; for (int j = 0; j < rainLength; j++) { Vector2 g = Overworld.GetMinNeighbor(buffer, currentPos); float h = Overworld.GetHeight(buffer, currentPos); if (h < seaLevel || g.LengthSquared() < 1e-12) { break; } Overworld.MinBlend(Overworld.Map, currentPos, erosionRate * Overworld.GetValue(Overworld.Map, currentPos, Overworld.ScalarFieldType.Erosion), Overworld.ScalarFieldType.Erosion); velocity = 0.1f * g + 0.7f * velocity + 0.2f * MathFunctions.RandVector2Circle(); currentPos += velocity; } } }
public Texture2D CreateTexture(GraphicsDevice device, int width, int height) { Texture2D toReturn = null; Overworld.MapData[,] mapData = CreateMap(); toReturn = new Texture2D(device, width, height); System.Threading.Mutex imageMutex = new System.Threading.Mutex(); Color[] worldData = new Color[width * height]; Overworld.TextureFromHeightMap("Height", mapData, Overworld.ScalarFieldType.Height, width, height, imageMutex, worldData, toReturn, PlayState.SeaLevel); return(toReturn); }
public OverworldData(GraphicsDevice device, Overworld.MapData[,] map, string name, float seaLevel) { int sizeX = map.GetLength(0); int sizeY = map.GetLength(1); Biomes = new int[sizeX, sizeY]; Erosion = new float[sizeX, sizeY]; Faults = new float[sizeX, sizeY]; Rainfall = new float[sizeX, sizeY]; Temperature = new float[sizeX, sizeY]; Water = new int[sizeX, sizeY]; Weathering = new float[sizeX, sizeY]; Height = new float[sizeX, sizeY]; Factions = new byte[sizeX, sizeY]; Name = name; SeaLevel = seaLevel; for (int x = 0; x < sizeX; x++) { for (int y = 0; y < sizeY; y++) { Overworld.MapData data = map[x, y]; Biomes[x, y] = (int)data.Biome; Erosion[x, y] = (data.Erosion); Faults[x, y] = (data.Faults); Height[x, y] = (data.Height); Rainfall[x, y] = (data.Rainfall); Temperature[x, y] = (data.Temperature); Water[x, y] = (int)(data.Water); Weathering[x, y] = (data.Weathering); Factions[x, y] = data.Faction; } } Screenshot = CreateTexture(device, sizeX, sizeY, seaLevel); FactionList = new List <FactionDescriptor>(); byte id = 0; foreach (Faction f in Overworld.NativeFactions) { FactionList.Add(new FactionDescriptor() { Name = f.Name, PrimaryColory = f.PrimaryColor, SecondaryColor = f.SecondaryColor, Id = id, Race = f.Race.Name, CenterX = f.Center.X, CenterY = f.Center.Y, }); id++; } }
public NewOverworldFile(GraphicsDevice device, Overworld.MapData[,] map, string name, float seaLevel) { var worldFilePath = name + System.IO.Path.DirectorySeparatorChar + "world.png"; var metaFilePath = name + System.IO.Path.DirectorySeparatorChar + "meta.txt"; if (File.Exists(worldFilePath) && File.Exists(metaFilePath)) { // Do nothing since overworlds should be saved precisely once. return; } Data = new OverworldData(device, map, name, seaLevel); Width = map.GetLength(0); Height = map.GetLength(1); }
public void SeedCivs(Overworld.MapData[,] map, int numCivs, List <Faction> civs) { for (int i = 0; i < numCivs; i++) { Point?randomPoint = GetRandomLandPoint(map); if (randomPoint == null) { continue; } else { map[randomPoint.Value.X, randomPoint.Value.Y].Faction = (byte)(i + 1); } } }
private static void ScaleMap(Overworld.MapData[,] map, int width, int height, Overworld.ScalarFieldType fieldType) { float min = 99999; float max = -99999; float average = 0; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { float v = map[x, y].GetValue(fieldType); average += v; if (v < min) { min = v; } if (v > max) { max = v; } } } average /= (width * height); average = ((average - min) / (max - min)); bool tooLow = average < 0.5f; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { float v = map[x, y].GetValue(fieldType); float newVal = ((v - min) / (max - min)) + 0.001f; if (tooLow) { newVal = 1.0f - newVal; } map[x, y].SetValue(fieldType, newVal); } } }
public Point?GetRandomLandPoint(Overworld.MapData[,] map) { const int maxIters = 1000; int i = 0; int width = map.GetLength(0); int height = map.GetLength(1); while (i < maxIters) { int x = MathFunctions.Random.Next(0, width); int y = MathFunctions.Random.Next(0, height); if (map[x, y].Height > Settings.SeaLevel) { return(new Point(x, y)); } i++; } return(null); }
public OverworldFile(Overworld.MapData[,] map, string name) { Data = new OverworldData(map, name); }
public OverworldFile(GraphicsDevice device, Overworld.MapData[,] map, string name, float seaLevel) { Data = new OverworldData(device, map, name, seaLevel); }
public void GrowCivs(Overworld.MapData[,] map, int iters, List <Faction> civs) { int width = map.GetLength(0); int height = map.GetLength(1); byte[] neighbors = new byte[] { 0, 0, 0, 0 }; float[] neighborheights = new float[] { 0, 0, 0, 0 }; Point[] deltas = new Point[] { new Point(1, 0), new Point(0, 1), new Point(-1, 0), new Point(1, -1) }; for (int i = 0; i < iters; i++) { for (int x = 1; x < width - 1; x++) { for (int y = 1; y < height - 1; y++) { bool isUnclaimed = map[x, y].Faction == 0; bool isWater = map[x, y].Height < Settings.SeaLevel; if (!isUnclaimed && !isWater) { neighbors[0] = map[x + 1, y].Faction; neighbors[1] = map[x, y + 1].Faction; neighbors[2] = map[x - 1, y].Faction; neighbors[3] = map[x, y - 1].Faction; neighborheights[0] = map[x + 1, y].Height; neighborheights[1] = map[x, y + 1].Height; neighborheights[2] = map[x - 1, y].Height; neighborheights[3] = map[x, y - 1].Height; int minNeighbor = -1; float minHeight = float.MaxValue; for (int k = 0; k < 4; k++) { if (neighbors[k] == 0 && neighborheights[k] < minHeight && neighborheights[k] > Settings.SeaLevel) { minHeight = neighborheights[k]; minNeighbor = k; } } if (minNeighbor >= 0 && MathFunctions.RandEvent(0.25f / (neighborheights[minNeighbor] + 1e-2f))) { var faction = map[x, y].Faction; map[x + deltas[minNeighbor].X, y + deltas[minNeighbor].Y].Faction = faction; var biome = map[x + deltas[minNeighbor].X, y + deltas[minNeighbor].Y].Biome; var biomeName = BiomeLibrary.Biomes[biome].Name; var myFaction = civs[faction - 1]; if (myFaction.Race.Biomes.ContainsKey(biomeName)) { map[x + deltas[minNeighbor].X, y + deltas[minNeighbor].Y].Biome = BiomeLibrary.GetBiome(myFaction.Race.Biomes[biomeName]).Biome; } } } } } } foreach (var civ in NativeCivilizations) { civ.Center = new Point(0, 0); } for (int x = 1; x < width - 1; x++) { for (int y = 1; y < height - 1; y++) { byte f = map[x, y].Faction; if (f > 0) { civs[f - 1].Center = new Point(x + civs[f - 1].Center.X, y + civs[f - 1].Center.Y); civs[f - 1].TerritorySize++; } } } foreach (Faction f in civs) { if (f.TerritorySize > 0) { f.Center = new Point(f.Center.X / f.TerritorySize, f.Center.Y / f.TerritorySize); } } }