public static void AddValue(MapData[,] map, Vector2 pos, ScalarFieldType value, float amount) { int x = Math.Max(Math.Min((int)pos.X, map.GetLength(0) - 1), 0); int y = Math.Max(Math.Min((int)pos.Y, map.GetLength(1) - 1), 0); map[x, y].SetValue(value, map[x, y].GetValue(value) + amount); }
public void SetValue(ScalarFieldType type, float value) { switch (type) { case ScalarFieldType.Erosion: Erosion = value; break; case ScalarFieldType.Faults: Faults = value; break; case ScalarFieldType.Height: Height = value; break; case ScalarFieldType.Rainfall: Rainfall = value; break; case ScalarFieldType.Temperature: Temperature = value; break; case ScalarFieldType.Weathering: Weathering = value; break; case ScalarFieldType.Factions: Faction = (byte)(value * 255.0f); break; } }
public static float GetValue(MapData[,] map, Vector2 pos, ScalarFieldType value) { int x = Math.Max(Math.Min((int)pos.X, map.GetLength(0) - 1), 0); int y = Math.Max(Math.Min((int)pos.Y, map.GetLength(1) - 1), 0); return(map[x, y].GetValue(value)); }
public static float LinearInterpolate(Vector2 position, MapData[,] map, ScalarFieldType fieldType) { float x = position.X; float y = position.Y; float x1 = (int)MathFunctions.Clamp((float)Math.Ceiling(x), 0, map.GetLength(0) - 2); float y1 = (int)MathFunctions.Clamp((float)Math.Ceiling(y), 0, map.GetLength(1) - 2); float x2 = (int)MathFunctions.Clamp((float)Math.Floor(x), 0, map.GetLength(0) - 2); float y2 = (int)MathFunctions.Clamp((float)Math.Floor(y), 0, map.GetLength(1) - 2); if (Math.Abs(x1 - x2) < 0.5f) { x1 = x1 + 1; } if (Math.Abs(y1 - y2) < 0.5f) { y1 = y1 + 1; } float q11 = map[(int)x1, (int)y1].GetValue(fieldType); float q12 = map[(int)x1, (int)y2].GetValue(fieldType); float q21 = map[(int)x2, (int)y1].GetValue(fieldType); float q22 = map[(int)x2, (int)y2].GetValue(fieldType); return(MathFunctions.LinearCombination(x, y, x1, y1, x2, y2, q11, q12, q21, q22)); }
public float GetValue(ScalarFieldType type) { switch (type) { case ScalarFieldType.Erosion: return(Erosion); case ScalarFieldType.Faults: return(Faults); case ScalarFieldType.Height: return(Height); case ScalarFieldType.Rainfall: return(Rainfall); case ScalarFieldType.Temperature: return(Temperature); case ScalarFieldType.Weathering: return(Weathering); case ScalarFieldType.Factions: return(Faction); } return(-1.0f); }
public static void MultValue(MapData[,] heightMap, Vector2 pos, ScalarFieldType value, float height) { int x = Math.Max(Math.Min((int)pos.X, heightMap.GetLength(0) - 1), 0); int y = Math.Max(Math.Min((int)pos.Y, heightMap.GetLength(1) - 1), 0); float c = heightMap[x, y].GetValue(value); heightMap[x, y].SetValue(value, c * height); }
public static void Blur(MapData[,] array, int width, int height, ScalarFieldType type) { float[,] b = new float[width, height]; float[,] kernel = CalculateGaussianKernel(10, 0.75f); const int kernelSizeX = 10; const int kernelSizeY = 10; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { b[x, y] = array[x, y].GetValue(type); } } for (int x = kernelSizeX; x < width - kernelSizeX; x++) { for (int y = kernelSizeY; y < height - kernelSizeY; y++) { b[x, y] = 0.0f; for (int dx = 0; dx < kernelSizeX; dx++) { for (int dy = 0; dy < kernelSizeY; dy++) { int nx = x + dx - kernelSizeX / 2; int ny = y + dy - kernelSizeY / 2; float a = array[nx, ny].GetValue(type); float h = kernel[dx, dy] * a; b[x, y] += h; } } } } for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { array[x, y].SetValue(type, (float)(b[x, y])); } } }
public static void Blur(MapData[,] array, int width, int height, ScalarFieldType type) { float[,] b = new float[width, height]; float[,] kernel = CalculateGaussianKernel(10, 0.75f); const int kernelSizeX = 10; const int kernelSizeY = 10; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { b[x, y] = array[x, y].GetValue(type); } } for(int x = kernelSizeX; x < width - kernelSizeX; x++) { for(int y =kernelSizeY; y < height - kernelSizeY; y++) { b[x, y] = 0.0f; for(int dx = 0; dx < kernelSizeX; dx++) { for(int dy = 0; dy < kernelSizeY; dy++) { int nx = x + dx - kernelSizeX / 2; int ny = y + dy - kernelSizeY / 2; float a = array[nx, ny].GetValue(type); float h = kernel[dx, dy] * a; b[x, y] += h; } } } } for(int x = 0; x < width; x++) { for(int y = 0; y < height; y++) { array[x, y].SetValue(type, (float)(b[x, y])); } } }
public float GetValue(ScalarFieldType type) { switch(type) { case ScalarFieldType.Erosion: return Erosion; case ScalarFieldType.Faults: return Faults; case ScalarFieldType.Height: return Height; case ScalarFieldType.Rainfall: return Rainfall; case ScalarFieldType.Temperature: return Temperature; case ScalarFieldType.Weathering: return Weathering; case ScalarFieldType.Factions: return Faction; } return -1.0f; }
public static void TextureFromHeightMap(string displayMode, MapData[,] map, ScalarFieldType type, int width, int height, Mutex imageMutex, Color[] worldData, Texture2D worldMap, float sealevel) { if(JetGradient == null) { List<ColorStop> stops = new List<ColorStop>(); ColorStop first = new ColorStop { m_color = new Color(0, 255, 255), m_position = 0.0f }; ColorStop second = new ColorStop { m_color = new Color(0, 0, 255), m_position = 0.2f }; ColorStop third = new ColorStop { m_color = new Color(255, 255, 0), m_position = 0.4f }; ColorStop fourth = new ColorStop { m_color = new Color(255, 0, 0), m_position = 0.8f }; ColorStop fifth = new ColorStop { m_color = new Color(255, 255, 255), m_position = 1.0f }; stops.Add(first); stops.Add(second); stops.Add(third); stops.Add(fourth); stops.Add(fifth); JetGradient = new ColorGradient(stops); } int stepX = map.GetLength(0) / width; int stepY = map.GetLength(1) / height; string index = ""; for(int tx = 0; tx < width; tx++) { for(int ty = 0; ty < height; ty++) { int x = tx * stepX; int y = ty * stepY; float h1 = map[x, y].GetValue(type); Biome biome = Map[x, y].Biome; if(h1 < 0.1f) { index = "Sea"; } else if(h1 >= 0.1f && h1 <= sealevel) { index = "Water"; } else if(displayMode == "Biomes") { index = "Biome"; } else if(displayMode == "Height") { if(h1 >= 0.2f && h1 < 0.21f) { index = "Shore"; } else if(h1 >= 0.21f && h1 < 0.4f) { index = "Lowlands"; } else if(h1 >= 0.4f && h1 < 0.6f) { index = "Highlands"; } else if(h1 >= 0.6f && h1 < 0.9f) { index = "Mountains"; } else { index = "Peaks"; } } if(displayMode == "Gray") { Color toDraw = JetGradient.GetColor(h1); worldData[y * width + x] = toDraw; } else if (displayMode == "Factions") { float h2 = map[x, y].Height; byte factionColor = map[x, y].Faction; Color ci = Color.DarkBlue; if (factionColor > 0) { bool inside = x > 0 && x < width - 1 && y > 0 && y < height - 1; ci = NativeFactions[factionColor - 1].PrimaryColor; if(inside && (map[x + 1, y].Faction != factionColor || map[x - 1, y].Faction != factionColor || map[x, y - 1].Faction != factionColor || map[x, y + 1].Faction != factionColor || map[x + 1, y + 1].Faction != factionColor || map[x - 1, y - 1].Faction != factionColor || map[x + 1, y - 1].Faction != factionColor || map[x - 1, y + 1].Faction != factionColor)) { ci = NativeFactions[factionColor - 1].SecondaryColor; } } else if (h2 > sealevel) { ci = Color.Gray; } Color toDraw = new Color((float)(ci.R) * (h2 + 0.5f) / 255.0f, (float)(ci.G * (h2 + 0.5f)) / 255.0f, (float)(ci.B * (h2 + 0.5f)) / 255.0f); worldData[ty * width + tx] = toDraw; } else { Color ci = displayMode == "Biomes" && index != "Water" && index != "Sea" ? BiomeLibrary.Biomes[biome].MapColor : HeightColors[index]; Color toDraw = new Color((float) (ci.R) * (h1 + 0.5f) / 255.0f, (float) (ci.G * (h1 + 0.5f)) / 255.0f, (float) (ci.B * (h1 + 0.5f)) / 255.0f); worldData[ty * width + tx] = toDraw; } } } if(imageMutex != null) { imageMutex.WaitOne(); } GameState.Game.GraphicsDevice.Textures[0] = null; worldMap.SetData(worldData); if(imageMutex != null) { imageMutex.ReleaseMutex(); } }
public static void MultValue(MapData[,] heightMap, Vector2 pos, ScalarFieldType value, float height) { int x = Math.Max(Math.Min((int) pos.X, heightMap.GetLength(0) - 1), 0); int y = Math.Max(Math.Min((int) pos.Y, heightMap.GetLength(1) - 1), 0); float c = heightMap[x, y].GetValue(value); heightMap[x, y].SetValue(value, c * height); }
public static float LinearInterpolate(Vector2 position, MapData[,] map, ScalarFieldType fieldType) { float x = position.X; float y = position.Y; float x1 = (int) MathFunctions.Clamp((float) Math.Ceiling(x), 0, map.GetLength(0) - 2); float y1 = (int) MathFunctions.Clamp((float) Math.Ceiling(y), 0, map.GetLength(1) - 2); float x2 = (int) MathFunctions.Clamp((float) Math.Floor(x), 0, map.GetLength(0) - 2); float y2 = (int) MathFunctions.Clamp((float) Math.Floor(y), 0, map.GetLength(1) - 2); if(Math.Abs(x1 - x2) < 0.5f) { x1 = x1 + 1; } if(Math.Abs(y1 - y2) < 0.5f) { y1 = y1 + 1; } float q11 = map[(int) x1, (int) y1].GetValue(fieldType); float q12 = map[(int) x1, (int) y2].GetValue(fieldType); float q21 = map[(int) x2, (int) y1].GetValue(fieldType); float q22 = map[(int) x2, (int) y2].GetValue(fieldType); return MathFunctions.LinearCombination(x, y, x1, y1, x2, y2, q11, q12, q21, q22); }
public static float GetValue(MapData[,] map, Vector2 pos, ScalarFieldType value) { int x = Math.Max(Math.Min((int) pos.X, map.GetLength(0) - 1), 0); int y = Math.Max(Math.Min((int) pos.Y, map.GetLength(1) - 1), 0); return map[x, y].GetValue(value); }
public static void Distort(int width, int height, float distortAmount, float distortScale, ScalarFieldType fieldType) { float[,] buffer = new float[width, height]; for(int x = 0; x < width; x++) { for(int y = 0; y < height; y++) { buffer[x, y] = GetValue(Map, new Vector2(x, y) + new Vector2((XDistort.Noise(x * distortScale, y * distortScale, 0) * 2.0f - 1.0f) * distortAmount, (YDistort.Noise(x * distortScale, y * distortScale, 0) * 2.0f - 1.0f) * distortAmount), fieldType); } } for(int x = 0; x < width; x++) { for(int y = 0; y < height; y++) { Map[x, y].SetValue(fieldType, buffer[x, y]); } } }
public void SetValue(ScalarFieldType type, float value) { switch(type) { case ScalarFieldType.Erosion: Erosion = value; break; case ScalarFieldType.Faults: Faults = value; break; case ScalarFieldType.Height: Height = value; break; case ScalarFieldType.Rainfall: Rainfall = value; break; case ScalarFieldType.Temperature: Temperature = value; break; case ScalarFieldType.Weathering: Weathering = value; break; case ScalarFieldType.Factions: Faction = (byte) (value*255.0f); break; } }
public static void Distort(int width, int height, float distortAmount, float distortScale, ScalarFieldType fieldType) { float[,] buffer = new float[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { buffer[x, y] = GetValue(Map, new Vector2(x, y) + new Vector2((XDistort.Noise(x * distortScale, y * distortScale, 0) * 2.0f - 1.0f) * distortAmount, (YDistort.Noise(x * distortScale, y * distortScale, 0) * 2.0f - 1.0f) * distortAmount), fieldType); } } for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Map[x, y].SetValue(fieldType, buffer[x, y]); } } }
public static float GetValueAt(Vector3 worldPos, ScalarFieldType fieldType, float scale, Vector2 origin) { Vector2 v = WorldToOverworld(worldPos, scale, origin); return(GetValue(Overworld.Map, v, fieldType)); }
public static void TextureFromHeightMap(string displayMode, MapData[,] map, ScalarFieldType type, int width, int height, Mutex imageMutex, Color[] worldData, Texture2D worldMap, float sealevel) { if (JetGradient == null) { List <ColorStop> stops = new List <ColorStop>(); ColorStop first = new ColorStop { m_color = new Color(0, 255, 255), m_position = 0.0f }; ColorStop second = new ColorStop { m_color = new Color(0, 0, 255), m_position = 0.2f }; ColorStop third = new ColorStop { m_color = new Color(255, 255, 0), m_position = 0.4f }; ColorStop fourth = new ColorStop { m_color = new Color(255, 0, 0), m_position = 0.8f }; ColorStop fifth = new ColorStop { m_color = new Color(255, 255, 255), m_position = 1.0f }; stops.Add(first); stops.Add(second); stops.Add(third); stops.Add(fourth); stops.Add(fifth); JetGradient = new ColorGradient(stops); } int stepX = map.GetLength(0) / width; int stepY = map.GetLength(1) / height; string index = ""; for (int tx = 0; tx < width; tx++) { for (int ty = 0; ty < height; ty++) { int x = tx * stepX; int y = ty * stepY; float h1 = map[x, y].GetValue(type); var biome = Map[x, y].Biome; if (h1 < 0.1f) { index = "Sea"; } else if (h1 >= 0.1f && h1 <= sealevel) { index = "Water"; } else if (displayMode == "Biomes") { index = "Biome"; } else if (displayMode == "Height") { if (h1 >= 0.2f && h1 < 0.21f) { index = "Shore"; } else if (h1 >= 0.21f && h1 < 0.4f) { index = "Lowlands"; } else if (h1 >= 0.4f && h1 < 0.6f) { index = "Highlands"; } else if (h1 >= 0.6f && h1 < 0.9f) { index = "Mountains"; } else { index = "Peaks"; } } if (displayMode == "Gray") { Color toDraw = JetGradient.GetColor(h1); worldData[y * width + x] = toDraw; } else if (displayMode == "Factions") { float h2 = map[x, y].Height; byte factionColor = map[x, y].Faction; Color ci = Color.DarkBlue; if (factionColor > 0 && factionColor <= NativeFactions.Count) { bool inside = x > 0 && x < width - 1 && y > 0 && y < height - 1; ci = NativeFactions[factionColor - 1].PrimaryColor; if (inside && (map[x + 1, y].Faction != factionColor || map[x - 1, y].Faction != factionColor || map[x, y - 1].Faction != factionColor || map[x, y + 1].Faction != factionColor || map[x + 1, y + 1].Faction != factionColor || map[x - 1, y - 1].Faction != factionColor || map[x + 1, y - 1].Faction != factionColor || map[x - 1, y + 1].Faction != factionColor)) { ci = NativeFactions[factionColor - 1].SecondaryColor; } } else if (h2 > sealevel) { ci = Color.Gray; } Color toDraw = new Color((float)(ci.R) * (h2 + 0.5f) / 255.0f, (float)(ci.G * (h2 + 0.5f)) / 255.0f, (float)(ci.B * (h2 + 0.5f)) / 255.0f); worldData[ty * width + tx] = toDraw; } else { Color ci = Color.Black; if (displayMode == "Biomes" && index != "Water" && index != "Sea") { if ((int)biome < BiomeLibrary.Biomes.Count) { ci = BiomeLibrary.Biomes[biome].MapColor; } } else { ci = HeightColors[index]; } Color toDraw = new Color((float)(ci.R) * (h1 + 0.5f) / 255.0f, (float)(ci.G * (h1 + 0.5f)) / 255.0f, (float)(ci.B * (h1 + 0.5f)) / 255.0f); worldData[ty * width + tx] = toDraw; } } } if (imageMutex != null) { imageMutex.WaitOne(); } GameState.Game.GraphicsDevice.Textures[0] = null; worldMap.SetData(worldData); if (imageMutex != null) { imageMutex.ReleaseMutex(); } }
public static void AddValue(MapData[,] map, Vector2 pos, ScalarFieldType value, float amount) { int x = Math.Max(Math.Min((int) pos.X, map.GetLength(0) - 1), 0); int y = Math.Max(Math.Min((int) pos.Y, map.GetLength(1) - 1), 0); map[x, y].SetValue(value, map[x, y].GetValue(value) + amount); }