public IChunk GenerateChunk(IWorld world, Coordinates2D coordinates) { const int featurePointDistance = 400; // TODO: Create a terrain generator initializer function that gets passed the seed etc int seed = world.Seed; var worley = new CellNoise(seed); HighNoise.Seed = seed; LowNoise.Seed = seed; CaveNoise.Seed = seed; var chunk = new Chunk(coordinates); for (int x = 0; x < 16; x++) { for (int z = 0; z < 16; z++) { var blockX = MathHelper.ChunkToBlockX(x, coordinates.X); var blockZ = MathHelper.ChunkToBlockZ(z, coordinates.Z); const double lowClampRange = 5; double lowClampMid = LowClamp.MaxValue - ((LowClamp.MaxValue + LowClamp.MinValue) / 2); double lowClampValue = LowClamp.Value2D(blockX, blockZ); if (lowClampValue > lowClampMid - lowClampRange && lowClampValue < lowClampMid + lowClampRange) { InvertNoise NewPrimary = new InvertNoise(HighClamp); FinalNoise.PrimaryNoise = NewPrimary; } else { //reset it after modifying the values FinalNoise = new ModifyNoise(HighClamp, LowClamp, NoiseModifier.Add); } FinalNoise = new ModifyNoise(FinalNoise, BottomClamp, NoiseModifier.Subtract); var cellValue = worley.Value2D(blockX, blockZ); var location = new Coordinates2D(blockX, blockZ); if (world.BiomeDiagram.BiomeCells.Count < 1 || cellValue.Equals(1) && world.BiomeDiagram.ClosestCellPoint(location) >= featurePointDistance) { byte id = (SingleBiome) ? GenerationBiome : world.BiomeDiagram.GenerateBiome(seed, Biomes, location); var cell = new BiomeCell(id, location); world.BiomeDiagram.AddCell(cell); } var biomeId = GetBiome(world, location); var biome = Biomes.GetBiome(biomeId); chunk.Biomes[x * Chunk.Width + z] = biomeId; var height = GetHeight(blockX, blockZ); var surfaceHeight = height - biome.SurfaceDepth; chunk.HeightMap[x * Chunk.Width + z] = height; // TODO: Do not overwrite blocks if they are already set from adjacent chunks for (int y = 0; y <= height; y++) { double cave = 0; if (!EnableCaves) cave = double.MaxValue; else cave = CaveNoise.Value3D((blockX + x) / 2, y / 2, (blockZ + z) / 2); double threshold = 0.05; if (y < 4) threshold = double.MaxValue; else { if (y > height - 8) threshold = 8; } if (cave < threshold) { if (y == 0) chunk.SetBlockID(new Coordinates3D(x, y, z), BedrockBlock.BlockID); else { if (y.Equals(height) || y < height && y > surfaceHeight) chunk.SetBlockID(new Coordinates3D(x, y, z), biome.SurfaceBlock); else { if (y > surfaceHeight - biome.FillerDepth) chunk.SetBlockID(new Coordinates3D(x, y, z), biome.FillerBlock); else chunk.SetBlockID(new Coordinates3D(x, y, z), StoneBlock.BlockID); } } } } } } foreach (var decorator in ChunkDecorators) decorator.Decorate(world, chunk, Biomes); chunk.TerrainPopulated = true; return chunk; }
public void AddCell(BiomeCell cell) { BiomeCells.Add(cell); }