void CreateDownTriangle(BiomeCell baseBiome) { BiomeCell biomeE = biomeCells.Single(b => b.coordinates.Equals(baseBiome.coordinates.East())); BiomeCell biomeSE = biomeCells.Single(b => b.coordinates.Equals(baseBiome.coordinates.SouthEast())); BiomeCell[] relevantCells = new BiomeCell[] { baseBiome, biomeE, biomeSE }; tileManager.CreateTileCell(relevantCells, false); }
void CreateUpTriangle(BiomeCell baseBiome) { BiomeCell biomeNE = biomeCells.Single(b => b.coordinates.Equals(baseBiome.coordinates.NorthEast())); BiomeCell biomeE = biomeCells.Single(b => b.coordinates.Equals(baseBiome.coordinates.East())); BiomeCell[] relevantCells = new BiomeCell[] { baseBiome, biomeNE, biomeE }; tileManager.CreateTileCell(relevantCells, true); }
internal void AlterBiome(BiomeCell biomeCell) { if (biomeCell.type == selectedBiome.type) { return; } biomeCell.SetBiome(selectedBiome); CreateTrianglesForBiome(biomeCell); }
/* * The closest biome cell to the specified location(uses the Chebyshev distance function). */ public BiomeCell ClosestCell(Coordinates2D location) { BiomeCell cell = null; var distance = double.MaxValue; foreach (BiomeCell C in BiomeCells) { var _distance = Distance(location, C.CellPoint); if (_distance < distance) { distance = _distance; cell = C; } } return(cell); }
void CreateCell(int q, int r) { var position = new Vector3 { x = (q + r * 0.5f - r / 2) * (HexMetrics.innerRadius * 2f), y = 0f, z = r * (HexMetrics.outerRadius * 1.5f), }; BiomeCell cell = Instantiate(biomePrefab, transform); cell.grid = this; cell.SetCoordinates(q, r); cell.transform.localPosition = position; biomeCells.Add(cell); }
void CreateTrianglesForBiome(BiomeCell biomeCell) { var coordinates = biomeCell.coordinates; var neighborNE = biomeCells.SingleOrDefault(bc => bc.coordinates.Equals(coordinates.NorthEast())); var neighborE = biomeCells.SingleOrDefault(bc => bc.coordinates.Equals(coordinates.East())); var neighborSE = biomeCells.SingleOrDefault(bc => bc.coordinates.Equals(coordinates.SouthEast())); var neighborSW = biomeCells.SingleOrDefault(bc => bc.coordinates.Equals(coordinates.SouthWest())); var neighborW = biomeCells.SingleOrDefault(bc => bc.coordinates.Equals(coordinates.West())); var neighborNW = biomeCells.SingleOrDefault(bc => bc.coordinates.Equals(coordinates.NorthWest())); bool HasBeenSet(BiomeCell neighbor) => neighbor != null && neighbor.type.HasValue; if (HasBeenSet(neighborNE) && HasBeenSet(neighborE)) { CreateUpTriangle(biomeCell); } if (HasBeenSet(neighborSE) && HasBeenSet(neighborE)) { CreateDownTriangle(biomeCell); } if (HasBeenSet(neighborNE) && HasBeenSet(neighborNW)) { CreateDownTriangle(neighborNW); } if (HasBeenSet(neighborNW) && HasBeenSet(neighborW)) { CreateUpTriangle(neighborW); } if (HasBeenSet(neighborSW) && HasBeenSet(neighborW)) { CreateDownTriangle(neighborW); } if (HasBeenSet(neighborSE) && HasBeenSet(neighborSW)) { CreateUpTriangle(neighborSW); } }
void ChooseBiome(BiomeCell cell) { var neighborCoordinates = cell.coordinates.AllNeighbors(); var neighborTypes = biomeCells.Where(biomeCell => neighborCoordinates.Contains(biomeCell.coordinates) && biomeCell.type.HasValue) .Select(neighborCell => neighborCell.type.Value) .ToList(); var possibilities = new Dictionary <Biome, float>(); biomes.Values.ForEach(biome => possibilities.Add(biome, biome.GetWeight(neighborTypes))); var sum = 0.0; var total = possibilities.Values.Sum(); var rand = UnityEngine.Random.value * total; var chosenBiome = possibilities.First(entry => { sum += entry.Value; return(rand < sum); }).Key; cell.SetBiome(chosenBiome); }
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; chunk.UpdateHeightMap(); return(chunk); }
public void AddCell(BiomeCell cell) { BiomeCells.Add(cell); }