use bevy::math::IVec3;
use crate::chunk::{CHUNK_SIZE, Chunk};
use crate::noise::{fbm, hash3};
use crate::voxel::DefaultTerrainVoxel;
use super::ChunkGenerator;
#[derive(Debug, Clone)]
pub struct TerrainConfig {
pub seed: u32,
pub height_scale: f32,
pub frequency: f32,
pub sand_level: f32,
pub water_level: f32,
pub trees_enabled: bool,
pub biomes_enabled: bool,
}
impl Default for TerrainConfig {
fn default() -> Self {
Self {
seed: 42,
height_scale: 48.0,
frequency: 0.008,
sand_level: 5.0,
water_level: 4.0,
trees_enabled: true,
biomes_enabled: true,
}
}
}
pub struct TerrainGenerator {
config: TerrainConfig,
}
impl TerrainGenerator {
pub fn new(config: TerrainConfig) -> Self {
Self { config }
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum Biome {
Desert,
Plains,
Forest,
Snow,
}
impl Biome {
fn height_multiplier(self) -> f32 {
match self {
Biome::Desert => 0.5,
Biome::Plains => 0.8,
Biome::Forest => 1.0,
Biome::Snow => 1.4,
}
}
fn surface_voxels(self) -> (DefaultTerrainVoxel, DefaultTerrainVoxel) {
match self {
Biome::Desert => (DefaultTerrainVoxel::Sand, DefaultTerrainVoxel::Sand),
Biome::Plains => (DefaultTerrainVoxel::Grass, DefaultTerrainVoxel::Dirt),
Biome::Forest => (DefaultTerrainVoxel::Grass, DefaultTerrainVoxel::Dirt),
Biome::Snow => (DefaultTerrainVoxel::Snow, DefaultTerrainVoxel::Gravel),
}
}
fn tree_density(self) -> Option<u32> {
match self {
Biome::Desert => None,
Biome::Plains => Some(30), Biome::Forest => Some(8), Biome::Snow => None,
}
}
}
fn sample_biome(wx: f32, wz: f32, seed: u32) -> Biome {
let n = fbm(wx, 0.0, wz, seed.wrapping_add(1000), 2, 0.001, 2.0, 0.5);
if n < -0.33 {
Biome::Desert
} else if n < 0.2 {
Biome::Plains
} else if n < 0.6 {
Biome::Forest
} else {
Biome::Snow
}
}
fn place_tree(chunk: &mut Chunk<DefaultTerrainVoxel>, lx: usize, lz: usize, trunk_base_ly: usize) {
const TRUNK_HEIGHT: usize = 4;
const CROWN_RADIUS: i32 = 2;
const CROWN_HEIGHT: usize = 4;
for h in 0..TRUNK_HEIGHT {
let ly = trunk_base_ly + h;
if lx < CHUNK_SIZE && lz < CHUNK_SIZE && ly < CHUNK_SIZE {
chunk.set(lx, ly, lz, DefaultTerrainVoxel::Wood);
}
}
let crown_center_ly = trunk_base_ly + TRUNK_HEIGHT;
for dy in 0..CROWN_HEIGHT {
for dx in -CROWN_RADIUS..=CROWN_RADIUS {
for dz in -CROWN_RADIUS..=CROWN_RADIUS {
let dist = dx.abs().max(dz.abs()).max(dy as i32 - 1);
if dist > CROWN_RADIUS {
continue;
}
let cx = lx as i32 + dx;
let cy = crown_center_ly as i32 + dy as i32;
let cz = lz as i32 + dz;
if cx < 0 || cy < 0 || cz < 0 {
continue;
}
let (cx, cy, cz) = (cx as usize, cy as usize, cz as usize);
if cx < CHUNK_SIZE
&& cy < CHUNK_SIZE
&& cz < CHUNK_SIZE
&& chunk.get(cx, cy, cz) == DefaultTerrainVoxel::Air
{
chunk.set(cx, cy, cz, DefaultTerrainVoxel::Leaves);
}
}
}
}
}
impl ChunkGenerator<DefaultTerrainVoxel> for TerrainGenerator {
fn generate(&self, coord: IVec3, chunk: &mut Chunk<DefaultTerrainVoxel>) {
let cfg = &self.config;
let chunk_world_y = coord.y * CHUNK_SIZE as i32;
let mut surface_info: Vec<Option<(usize, bool)>> = vec![None; CHUNK_SIZE * CHUNK_SIZE];
for lx in 0..CHUNK_SIZE {
for lz in 0..CHUNK_SIZE {
let wx = (coord.x * CHUNK_SIZE as i32 + lx as i32) as f32;
let wz = (coord.z * CHUNK_SIZE as i32 + lz as i32) as f32;
let biome = if cfg.biomes_enabled {
sample_biome(wx, wz, cfg.seed)
} else {
Biome::Plains
};
let effective_height = cfg.height_scale * biome.height_multiplier();
let noise = fbm(wx, 0.0, wz, cfg.seed, 4, cfg.frequency, 2.0, 0.5);
let surface_y = (noise * 0.5 + 0.5) * effective_height;
let (top_voxel, sub_voxel) = if surface_y <= cfg.sand_level {
(DefaultTerrainVoxel::Sand, DefaultTerrainVoxel::Sand)
} else {
biome.surface_voxels()
};
let can_have_trees = cfg.trees_enabled
&& top_voxel == DefaultTerrainVoxel::Grass
&& biome.tree_density().is_some();
for ly in 0..CHUNK_SIZE {
let wy = (chunk_world_y + ly as i32) as f32;
let voxel = if wy >= surface_y {
if wy < cfg.water_level {
DefaultTerrainVoxel::Water
} else {
DefaultTerrainVoxel::Air
}
} else if wy >= surface_y - 1.0 {
top_voxel
} else if wy >= surface_y - 5.0 {
sub_voxel
} else {
DefaultTerrainVoxel::Stone
};
chunk.set(lx, ly, lz, voxel);
}
if can_have_trees {
let surface_world_ly = surface_y.ceil() as i32 - chunk_world_y;
if surface_world_ly >= 0 && surface_world_ly < CHUNK_SIZE as i32 {
surface_info[lz * CHUNK_SIZE + lx] =
Some((surface_world_ly as usize, true));
}
}
}
}
if cfg.trees_enabled {
for lx in 0..CHUNK_SIZE {
for lz in 0..CHUNK_SIZE {
if let Some((trunk_base_ly, _)) = surface_info[lz * CHUNK_SIZE + lx] {
let wx_i = coord.x * CHUNK_SIZE as i32 + lx as i32;
let wz_i = coord.z * CHUNK_SIZE as i32 + lz as i32;
let wx = wx_i as f32;
let wz = wz_i as f32;
let biome = if cfg.biomes_enabled {
sample_biome(wx, wz, cfg.seed)
} else {
Biome::Plains
};
if let Some(density) = biome.tree_density() {
let h =
((hash3(wx_i, 0, wz_i, cfg.seed) * 0.5 + 0.5) * 1_000_000.0) as u32;
if h % density == 0 {
place_tree(chunk, lx, lz, trunk_base_ly);
}
}
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::voxel::VoxelType;
fn make_gen() -> TerrainGenerator {
TerrainGenerator::new(TerrainConfig::default())
}
#[test]
fn test_terrain_surface_chunk_has_grass() {
let tgen = make_gen();
let mut chunk = Chunk::new();
tgen.generate(IVec3::new(0, 1, 0), &mut chunk);
let solid_count = (0..CHUNK_SIZE)
.flat_map(|x| {
(0..CHUNK_SIZE).flat_map(move |y| (0..CHUNK_SIZE).map(move |z| (x, y, z)))
})
.filter(|&(x, y, z)| chunk.get(x, y, z).is_solid())
.count();
assert!(solid_count > 0, "surface chunk must have solid voxels");
}
#[test]
fn test_terrain_deep_chunk_is_all_stone() {
let tgen = make_gen();
let mut chunk = Chunk::new();
tgen.generate(IVec3::new(0, -10, 0), &mut chunk);
for x in 0..CHUNK_SIZE {
for y in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
assert_eq!(
chunk.get(x, y, z),
DefaultTerrainVoxel::Stone,
"deep chunk voxel ({x},{y},{z}) should be Stone"
);
}
}
}
}
#[test]
fn test_terrain_high_chunk_is_all_air() {
let tgen = make_gen();
let mut chunk = Chunk::new();
tgen.generate(IVec3::new(0, 10, 0), &mut chunk);
for x in 0..CHUNK_SIZE {
for y in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
assert_eq!(
chunk.get(x, y, z),
DefaultTerrainVoxel::Air,
"high chunk voxel ({x},{y},{z}) should be Air"
);
}
}
}
}
#[test]
fn test_terrain_deterministic() {
let tgen = make_gen();
let mut a = Chunk::new();
let mut b = Chunk::new();
tgen.generate(IVec3::new(3, 1, -2), &mut a);
tgen.generate(IVec3::new(3, 1, -2), &mut b);
for x in 0..CHUNK_SIZE {
for y in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
assert_eq!(
a.get(x, y, z),
b.get(x, y, z),
"same coord must produce identical voxels"
);
}
}
}
}
}