public static BiomeData GetBiomeAt(Vector3 worldPosition) { Vector2 vec = new Vector2(worldPosition.X, worldPosition.Z) / PlayState.WorldScale; Overworld.Biome biome = Overworld.Map[(int)MathFunctions.Clamp(vec.X, 0, Overworld.Map.GetLength(0) - 1), (int)MathFunctions.Clamp(vec.Y, 0, Overworld.Map.GetLength(1) - 1)].Biome; return(BiomeLibrary.Biomes[biome]); }
public BiomeData(Overworld.Biome biome) { Biome = biome; Vegetation = new List <VegetationData>(); Motes = new List <DetailMoteData>(); Fauna = new List <FaunaData>(); ClumpGrass = false; ClumpSize = 30.0f; ClumpTreshold = 0.75f; MapColor = Color.White; }
public static Biome GetBiome(float temp, float rainfall, float height) { Overworld.Biome closest = Biome.Waste; float closestDist = float.MaxValue; foreach (var pair in BiomeLibrary.Biomes) { float dist = Math.Abs(pair.Value.Temp - temp) + Math.Abs(pair.Value.Rain - rainfall) + Math.Abs(pair.Value.Height - height); if (dist < closestDist) { closest = pair.Key; closestDist = dist; } } return(closest); }
/* * public void GenerateOres(VoxelChunk chunk, ComponentManager components, ContentManager content, * GraphicsDevice graphics) * { * Vector3 origin = chunk.Origin; * int chunkSizeX = chunk.SizeX; * int chunkSizeY = chunk.SizeY; * int chunkSizeZ = chunk.SizeZ; * Voxel v = chunk.MakeVoxel(0, 0, 0); * for (int x = 0; x < chunkSizeX; x++) * { * for (int z = 0; z < chunkSizeZ; z++) * { * int h = chunk.GetFilledVoxelGridHeightAt(x, chunkSizeY - 1, z); * for (int y = 1; y < chunkSizeY; y++) * { * foreach ( * KeyValuePair<string, VoxelLibrary.ResourceSpawnRate> spawns in VoxelLibrary.ResourceSpawns) * { * float s = spawns.Value.VeinSize; * float p = spawns.Value.VeinSpawnThreshold; * v.GridPosition = new Vector3(x, y, z); * if (v.IsEmpty || y >= h - 1 || !(y - h/2 < spawns.Value.MaximumHeight) || * !(y - h/2 > spawns.Value.MinimumHeight) || * !(PlayState.Random.NextDouble() <= spawns.Value.Probability) || v.Type.Name != "Stone") * { * continue; * } * * float caviness = (float) NoiseGenerator.Noise((float) (x + origin.X)*s, * (float) (z + origin.Z)*s, * (float) (y + origin.Y + h)*s); * * if (caviness > p) * { * v.Type = VoxelLibrary.GetVoxelType(spawns.Key); * } * continue; * } * } * } * } * } */ public void GenerateFauna(VoxelChunk chunk, ComponentManager components, ContentManager content, GraphicsDevice graphics, FactionLibrary factions) { int waterHeight = (int)(SeaLevel * chunk.SizeY); Voxel v = chunk.MakeVoxel(0, 0, 0); for (int x = 0; x < chunk.SizeX; x++) { for (int z = 0; z < chunk.SizeZ; z++) { Vector2 vec = new Vector2(x + chunk.Origin.X, z + chunk.Origin.Z) / PlayState.WorldScale; Overworld.Biome biome = Overworld.Map[(int)MathFunctions.Clamp(vec.X, 0, Overworld.Map.GetLength(0) - 1), (int)MathFunctions.Clamp(vec.Y, 0, Overworld.Map.GetLength(1) - 1)].Biome; BiomeData biomeData = BiomeLibrary.Biomes[biome]; int y = chunk.GetFilledVoxelGridHeightAt(x, chunk.SizeY - 1, z); if (!chunk.IsCellValid(x, (int)(y - chunk.Origin.Y), z)) { continue; } v.GridPosition = new Vector3(x, y, z); if (chunk.Data.Water[v.Index].WaterLevel != 0 || y <= waterHeight) { continue; } foreach (FaunaData animal in biomeData.Fauna) { if (y <= 0 || !(PlayState.Random.NextDouble() < animal.SpawnProbability)) { continue; } EntityFactory.CreateEntity <Body>(animal.Name, chunk.Origin + new Vector3(x, y, z) + Vector3.Up * 1.0f); break; } } } }
public VoxelChunk GenerateChunk(Vector3 origin, int chunkSizeX, int chunkSizeY, int chunkSizeZ, ComponentManager components, ContentManager content, GraphicsDevice graphics) { float waterHeight = SeaLevel; VoxelChunk c = new VoxelChunk(Manager, origin, 1, Manager.ChunkData.GetChunkID(origin + new Vector3(0.5f, 0.5f, 0.5f)), chunkSizeX, chunkSizeY, chunkSizeZ) { ShouldRebuild = true, ShouldRecalculateLighting = true }; Voxel voxel = c.MakeVoxel(0, 0, 0); for (int x = 0; x < chunkSizeX; x++) { for (int z = 0; z < chunkSizeZ; z++) { Vector2 v = new Vector2(x + origin.X, z + origin.Z) / PlayState.WorldScale; Overworld.Biome biome = Overworld.Map[(int)MathFunctions.Clamp(v.X, 0, Overworld.Map.GetLength(0) - 1), (int)MathFunctions.Clamp(v.Y, 0, Overworld.Map.GetLength(1) - 1)].Biome; BiomeData biomeData = BiomeLibrary.Biomes[biome]; Vector2 pos = new Vector2(x + origin.X, z + origin.Z) / PlayState.WorldScale; float hNorm = Overworld.GetValue(Overworld.Map, pos, Overworld.ScalarFieldType.Height); float h = MathFunctions.Clamp(hNorm * chunkSizeY, 0.0f, chunkSizeY - 2); int stoneHeight = (int)Math.Max(h - 2, 1); for (int y = 0; y < chunkSizeY; y++) { voxel.GridPosition = new Vector3(x, y, z); if (y == 0) { voxel.Type = VoxelLibrary.GetVoxelType("Bedrock"); voxel.Health = 255; continue; } if (y <= stoneHeight && stoneHeight > 1) { voxel.Type = VoxelLibrary.GetVoxelType(biomeData.SubsurfVoxel); voxel.Health = VoxelLibrary.GetVoxelType(biomeData.SubsurfVoxel).StartingHealth; } else if ((y == (int)h || y == stoneHeight) && hNorm > waterHeight) { if (biomeData.ClumpGrass && NoiseGenerator.Noise(pos.X / biomeData.ClumpSize, 0, pos.Y / biomeData.ClumpSize) > biomeData.ClumpTreshold) { voxel.Type = VoxelLibrary.GetVoxelType(biomeData.GrassVoxel); voxel.Health = VoxelLibrary.GetVoxelType(biomeData.GrassVoxel).StartingHealth; } else if (!biomeData.ClumpGrass) { voxel.Type = VoxelLibrary.GetVoxelType(biomeData.GrassVoxel); voxel.Health = VoxelLibrary.GetVoxelType(biomeData.GrassVoxel).StartingHealth; } else { voxel.Type = VoxelLibrary.GetVoxelType(biomeData.SoilVoxel); voxel.Health = VoxelLibrary.GetVoxelType(biomeData.SoilVoxel).StartingHealth; } } else if (y > h && y > 0) { voxel.Type = VoxelLibrary.GetVoxelType("empty"); } else if (hNorm < waterHeight) { voxel.Type = VoxelLibrary.GetVoxelType(biomeData.ShoreVoxel); voxel.Health = VoxelLibrary.GetVoxelType(biomeData.ShoreVoxel).StartingHealth; } else { voxel.Type = VoxelLibrary.GetVoxelType(biomeData.SoilVoxel); voxel.Health = VoxelLibrary.GetVoxelType(biomeData.SoilVoxel).StartingHealth; } } } } GenerateCaves(c); GenerateWater(c); GenerateLava(c); c.ShouldRebuildWater = true; return(c); }
public void GenerateVegetation(VoxelChunk chunk, ComponentManager components, ContentManager content, GraphicsDevice graphics) { int waterHeight = (int)(SeaLevel * chunk.SizeY); bool updated = false; Voxel v = chunk.MakeVoxel(0, 0, 0); Voxel vUnder = chunk.MakeVoxel(0, 0, 0); for (int x = 0; x < chunk.SizeX; x++) { for (int z = 0; z < chunk.SizeZ; z++) { Vector2 vec = new Vector2(x + chunk.Origin.X, z + chunk.Origin.Z) / PlayState.WorldScale; Overworld.Biome biome = Overworld.Map[(int)MathFunctions.Clamp(vec.X, 0, Overworld.Map.GetLength(0) - 1), (int)MathFunctions.Clamp(vec.Y, 0, Overworld.Map.GetLength(1) - 1)].Biome; BiomeData biomeData = BiomeLibrary.Biomes[biome]; int y = chunk.GetFilledVoxelGridHeightAt(x, chunk.SizeY - 1, z); if (!chunk.IsCellValid(x, (int)(y - chunk.Origin.Y), z)) { continue; } v.GridPosition = new Vector3(x, y, z); if (!v.IsEmpty || chunk.Data.Water[v.Index].WaterLevel != 0 || y <= waterHeight) { continue; } foreach (VegetationData veg in biomeData.Vegetation) { if (y <= 0) { continue; } if (!MathFunctions.RandEvent(veg.SpawnProbability)) { continue; } if (NoiseGenerator.Noise(vec.X / veg.ClumpSize, veg.NoiseOffset, vec.Y / veg.ClumpSize) < veg.ClumpThreshold) { continue; } int yh = chunk.GetFilledVoxelGridHeightAt(x, y, z); if (yh > 0) { vUnder.GridPosition = new Vector3(x, yh - 1, z); if (!vUnder.IsEmpty && vUnder.TypeName == biomeData.GrassVoxel) { vUnder.Type = VoxelLibrary.GetVoxelType(biomeData.SoilVoxel); updated = true; float offset = veg.VerticalOffset; if (vUnder.RampType != RampType.None) { offset -= 0.25f; } float treeSize = MathFunctions.Rand() * veg.SizeVariance + veg.MeanSize; EntityFactory.CreateEntity <Body>(veg.Name, chunk.Origin + new Vector3(x, y, z) + new Vector3(0, treeSize * offset, 0), Blackboard.Create("Scale", treeSize)); } } break; } } } if (updated) { chunk.ShouldRebuild = true; } }
public VoxelChunk GenerateChunk(Vector3 origin, int chunkSizeX, int chunkSizeY, int chunkSizeZ, ComponentManager components, ContentManager content, GraphicsDevice graphics) { float waterHeight = SeaLevel + 1.0f / chunkSizeY; VoxelChunk c = new VoxelChunk(Manager, origin, 1, Manager.ChunkData.GetChunkID(origin + new Vector3(0.5f, 0.5f, 0.5f)), chunkSizeX, chunkSizeY, chunkSizeZ) { ShouldRebuild = true, ShouldRecalculateLighting = true }; Voxel voxel = c.MakeVoxel(0, 0, 0); for (int x = 0; x < chunkSizeX; x++) { for (int z = 0; z < chunkSizeZ; z++) { Vector2 v = new Vector2(x + origin.X, z + origin.Z) / WorldScale; Overworld.Biome biome = Overworld.Map[(int)MathFunctions.Clamp(v.X, 0, Overworld.Map.GetLength(0) - 1), (int)MathFunctions.Clamp(v.Y, 0, Overworld.Map.GetLength(1) - 1)].Biome; BiomeData biomeData = BiomeLibrary.Biomes[biome]; Vector2 pos = new Vector2(x + origin.X, z + origin.Z) / WorldScale; float hNorm = Overworld.LinearInterpolate(pos, Overworld.Map, Overworld.ScalarFieldType.Height); float h = MathFunctions.Clamp(hNorm * chunkSizeY, 0.0f, chunkSizeY - 2); int stoneHeight = (int)Math.Max(h - biomeData.SoilLayer.Depth, 1); int currentGrassLayer = 0; int currentSoilLayer = 0; int currentSubsurfaceLayer = 0; int depthWithinSubsurface = 0; for (int y = chunkSizeY - 1; y >= 0; y--) { voxel.GridPosition = new Vector3(x, y, z); if (y == 0) { voxel.Type = VoxelLibrary.GetVoxelType("Bedrock"); voxel.Health = 8; continue; } if (y <= stoneHeight && stoneHeight > 1) { voxel.Type = VoxelLibrary.GetVoxelType(biomeData.SubsurfaceLayers[currentSubsurfaceLayer].VoxelType); voxel.Health = VoxelLibrary.GetVoxelType(biomeData.SubsurfaceLayers[currentSubsurfaceLayer].VoxelType).StartingHealth; depthWithinSubsurface++; if (depthWithinSubsurface > biomeData.SubsurfaceLayers[currentSubsurfaceLayer].Depth) { depthWithinSubsurface = 0; currentSubsurfaceLayer++; if (currentSubsurfaceLayer > biomeData.SubsurfaceLayers.Count - 1) { currentSubsurfaceLayer = biomeData.SubsurfaceLayers.Count - 1; } } } else if ((y == (int)h || y == stoneHeight) && hNorm > waterHeight) { if (biomeData.ClumpGrass && NoiseGenerator.Noise(pos.X / biomeData.ClumpSize, 0, pos.Y / biomeData.ClumpSize) > biomeData.ClumpTreshold) { voxel.Type = VoxelLibrary.GetVoxelType(biomeData.GrassLayer.VoxelType); voxel.Health = VoxelLibrary.GetVoxelType(biomeData.GrassLayer.VoxelType).StartingHealth; } else if (!biomeData.ClumpGrass) { voxel.Type = VoxelLibrary.GetVoxelType(biomeData.GrassLayer.VoxelType); voxel.Health = VoxelLibrary.GetVoxelType(biomeData.GrassLayer.VoxelType).StartingHealth; } else { voxel.Type = VoxelLibrary.GetVoxelType(biomeData.SoilLayer.VoxelType); voxel.Health = VoxelLibrary.GetVoxelType(biomeData.SoilLayer.VoxelType).StartingHealth; } } else if (y > h && y > 0) { voxel.Type = VoxelLibrary.GetVoxelType("empty"); } else if (hNorm <= waterHeight) { voxel.Type = VoxelLibrary.GetVoxelType(biomeData.ShoreVoxel); voxel.Health = VoxelLibrary.GetVoxelType(biomeData.ShoreVoxel).StartingHealth; } else { voxel.Type = VoxelLibrary.GetVoxelType(biomeData.SoilLayer.VoxelType); voxel.Health = VoxelLibrary.GetVoxelType(biomeData.SoilLayer.VoxelType).StartingHealth; } } } } GenerateWater(c); GenerateLava(c); GenerateCaves(c, components.World); //GenerateAquifers(c); //GenerateLavaTubes(c); c.ShouldRecalculateLighting = true; c.ShouldRebuildWater = true; return(c); }