use bevy::math::{IVec3, Vec3};
use crate::chunk::CHUNK_SIZE;
use crate::chunk::Chunk;
use crate::noise::{fbm, hash3};
use crate::voxel::DefaultVoxel;
use super::ChunkGenerator;
#[derive(Clone, Debug)]
pub struct MineralFrequencies {
pub rare_mineral: f32,
pub silicon_crystal: f32,
pub iron_ore: f32,
pub rock: f32,
}
impl Default for MineralFrequencies {
fn default() -> Self {
Self {
rare_mineral: 0.05,
silicon_crystal: 0.15,
iron_ore: 0.25,
rock: 0.55,
}
}
}
#[derive(Clone, Debug)]
pub struct AsteroidConfig {
pub seed: u32,
pub asteroid_count: usize,
pub min_radius: f32,
pub max_radius: f32,
pub noise_octaves: u32,
pub noise_frequency: f32,
pub noise_amplitude: f32,
pub mineral_frequencies: MineralFrequencies,
}
impl Default for AsteroidConfig {
fn default() -> Self {
Self {
seed: 12345,
asteroid_count: 8,
min_radius: 20.0,
max_radius: 60.0,
noise_octaves: 4,
noise_frequency: 2.5,
noise_amplitude: 0.34,
mineral_frequencies: MineralFrequencies::default(),
}
}
}
#[derive(Clone, Debug)]
struct Asteroid {
center: Vec3,
radius: f32,
seed: u32,
flatten_y: f32,
}
pub struct AsteroidGenerator {
config: AsteroidConfig,
asteroids: Vec<Asteroid>,
}
impl AsteroidGenerator {
pub fn new(config: AsteroidConfig) -> Self {
let asteroids = Self::build_asteroids(&config);
Self { config, asteroids }
}
pub fn config(&self) -> &AsteroidConfig {
&self.config
}
pub fn asteroid_count(&self) -> usize {
self.asteroids.len()
}
fn build_asteroids(config: &AsteroidConfig) -> Vec<Asteroid> {
let mut asteroids = Vec::with_capacity(config.asteroid_count);
for i in 0..config.asteroid_count {
let per_seed = config.seed.wrapping_add(i as u32 * 97 + 1);
let (center, radius, flatten_y) = if i == 0 {
let r = (config.min_radius + config.max_radius) * 0.5;
(Vec3::ZERO, r, 1.0)
} else {
let hx = hash3(i as i32, 0, 0, per_seed);
let hy = hash3(i as i32, 1, 0, per_seed);
let hz = hash3(i as i32, 2, 0, per_seed);
let hr = hash3(i as i32, 3, 0, per_seed);
let spread_xz = config.max_radius * 10.0;
let spread_y = config.max_radius * 5.0;
let x = (hx * 2.0 - 1.0) * spread_xz;
let y = (hy * 2.0 - 1.0) * spread_y;
let z = (hz * 2.0 - 1.0) * spread_xz;
let r = config.min_radius + hr * (config.max_radius - config.min_radius);
(Vec3::new(x, y, z), r, 1.0_f32)
};
asteroids.push(Asteroid {
center,
radius,
seed: per_seed,
flatten_y,
});
}
asteroids
}
#[inline]
fn determine_mineral(world_pos: Vec3, asteroid_seed: u32) -> DefaultVoxel {
let mineral_octaves = 3;
let mineral_freq = 5.0;
let lacunarity = 2.0;
let gain = 0.5;
let rare_noise = fbm(
world_pos.x,
world_pos.y,
world_pos.z,
asteroid_seed.wrapping_add(100),
mineral_octaves,
mineral_freq,
lacunarity,
gain,
);
let crystal_noise = fbm(
world_pos.x,
world_pos.y,
world_pos.z,
asteroid_seed.wrapping_add(200),
mineral_octaves,
mineral_freq,
lacunarity,
gain,
);
let iron_noise = fbm(
world_pos.x,
world_pos.y,
world_pos.z,
asteroid_seed.wrapping_add(300),
mineral_octaves,
mineral_freq,
lacunarity,
gain,
);
if rare_noise > 0.55 {
DefaultVoxel::RareMineral
} else if crystal_noise > 0.30 {
DefaultVoxel::SiliconCrystal
} else if iron_noise > 0.20 {
DefaultVoxel::IronOre
} else {
DefaultVoxel::Rock
}
}
}
impl ChunkGenerator<DefaultVoxel> for AsteroidGenerator {
fn generate(&self, coord: IVec3, chunk: &mut Chunk<DefaultVoxel>) {
let chunk_world = coord.as_vec3() * CHUNK_SIZE as f32;
for lx in 0..CHUNK_SIZE {
for ly in 0..CHUNK_SIZE {
for lz in 0..CHUNK_SIZE {
let world_pos =
chunk_world + Vec3::new(lx as f32 + 0.5, ly as f32 + 0.5, lz as f32 + 0.5);
for asteroid in &self.asteroids {
let dx = world_pos.x - asteroid.center.x;
let dy = (world_pos.y - asteroid.center.y) / asteroid.flatten_y;
let dz = world_pos.z - asteroid.center.z;
let dist = (dx * dx + dy * dy + dz * dz).sqrt();
let max_extent =
asteroid.radius + asteroid.radius * self.config.noise_amplitude;
if dist > max_extent + 1.0 {
continue;
}
let inner_solid_radius =
asteroid.radius * (1.0 - self.config.noise_amplitude);
if dist < inner_solid_radius {
let voxel = Self::determine_mineral(world_pos, asteroid.seed);
chunk.set(lx, ly, lz, voxel);
break;
}
let norm = world_pos / (asteroid.radius * 2.0);
let surface_noise = fbm(
norm.x,
norm.y,
norm.z,
asteroid.seed,
self.config.noise_octaves,
self.config.noise_frequency,
2.0, 0.5, ) * asteroid.radius
* self.config.noise_amplitude;
let density = asteroid.radius - dist + surface_noise;
if density > 0.0 {
let voxel = Self::determine_mineral(world_pos, asteroid.seed);
chunk.set(lx, ly, lz, voxel);
break;
}
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deterministic_generation() {
let config = AsteroidConfig::default();
let generator = AsteroidGenerator::new(config.clone());
let coord = IVec3::new(0, 0, 0);
let mut chunk_a = Chunk::<DefaultVoxel>::new();
generator.generate(coord, &mut chunk_a);
let mut chunk_b = Chunk::<DefaultVoxel>::new();
generator.generate(coord, &mut chunk_b);
for x in 0..CHUNK_SIZE {
for y in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
assert_eq!(
chunk_a.get(x, y, z),
chunk_b.get(x, y, z),
"Voxel mismatch at ({x}, {y}, {z}): generation must be deterministic"
);
}
}
}
}
#[test]
fn empty_space_produces_air() {
let config = AsteroidConfig {
asteroid_count: 1,
min_radius: 10.0,
max_radius: 10.0,
..AsteroidConfig::default()
};
let generator = AsteroidGenerator::new(config);
let coord = IVec3::new(1000, 1000, 1000);
let mut chunk = Chunk::<DefaultVoxel>::new();
generator.generate(coord, &mut chunk);
assert!(
chunk.is_empty(),
"Chunk far from any asteroid should be entirely air"
);
}
#[test]
fn asteroid_interior_has_solid_voxels() {
let config = AsteroidConfig {
asteroid_count: 1,
min_radius: 30.0,
max_radius: 30.0,
..AsteroidConfig::default()
};
let generator = AsteroidGenerator::new(config);
let coord = IVec3::new(0, 0, 0);
let mut chunk = Chunk::<DefaultVoxel>::new();
generator.generate(coord, &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.is_solid_at(x, y, z))
.count();
assert!(
solid_count > 0,
"Chunk at asteroid center should contain solid voxels, but found none"
);
assert!(
solid_count > CHUNK_SIZE * CHUNK_SIZE,
"Expected substantial solid voxels at asteroid center, got only {solid_count}"
);
}
#[test]
fn mineral_distribution_has_variety() {
let config = AsteroidConfig {
asteroid_count: 1,
min_radius: 40.0,
max_radius: 40.0,
..AsteroidConfig::default()
};
let generator = AsteroidGenerator::new(config);
let mut has_rock = false;
let mut has_iron = false;
let mut has_silicon = false;
let mut has_rare = false;
for cx in -1..=1 {
for cy in -1..=1 {
for cz in -1..=1 {
let coord = IVec3::new(cx, cy, cz);
let mut chunk = Chunk::<DefaultVoxel>::new();
generator.generate(coord, &mut chunk);
for x in 0..CHUNK_SIZE {
for y in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
match chunk.get(x, y, z) {
DefaultVoxel::Rock => has_rock = true,
DefaultVoxel::IronOre => has_iron = true,
DefaultVoxel::SiliconCrystal => has_silicon = true,
DefaultVoxel::RareMineral => has_rare = true,
DefaultVoxel::Air => {}
}
}
}
}
}
}
}
assert!(has_rock, "Should contain Rock voxels");
assert!(has_iron, "Should contain IronOre voxels");
assert!(has_silicon, "Should contain SiliconCrystal voxels");
assert!(has_rare, "Should contain RareMineral voxels");
}
}