kubi/kubi-shared/src/worldgen.rs

50 lines
1.5 KiB
Rust
Raw Normal View History

2023-01-26 18:04:01 -06:00
use glam::{IVec3, ivec3};
use bracket_noise::prelude::*;
2023-01-29 20:23:39 -06:00
use crate::{
chunk::{BlockData, CHUNK_SIZE},
2023-01-29 20:23:39 -06:00
blocks::Block
};
2023-01-26 18:04:01 -06:00
pub fn generate_world(chunk_position: IVec3, seed: u64) -> BlockData {
let offset = chunk_position * CHUNK_SIZE as i32;
2023-01-28 16:41:34 -06:00
let mut cave_noise = FastNoise::seeded(seed);
cave_noise.set_fractal_type(FractalType::FBM);
cave_noise.set_frequency(0.1);
2023-01-26 18:04:01 -06:00
2023-01-28 16:41:34 -06:00
let mut dirt_noise = FastNoise::seeded(seed.rotate_left(1));
dirt_noise.set_fractal_type(FractalType::FBM);
dirt_noise.set_frequency(0.1);
2023-01-26 18:04:01 -06:00
2023-01-28 16:41:34 -06:00
let mut blocks = Box::new([[[Block::Air; CHUNK_SIZE]; CHUNK_SIZE]; CHUNK_SIZE]);
2023-01-26 20:24:21 -06:00
2023-01-28 16:43:19 -06:00
if chunk_position.y >= 0 {
if chunk_position.y == 0 {
2023-01-28 16:41:34 -06:00
for x in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
blocks[x][0][z] = Block::Dirt;
blocks[x][1][z] = Block::Grass;
}
}
}
} else {
for x in 0..CHUNK_SIZE {
for y in 0..CHUNK_SIZE {
for z in 0..CHUNK_SIZE {
let position = ivec3(x as i32, y as i32, z as i32) + offset;
let v_cave_noise = cave_noise.get_noise3d(position.x as f32, position.y as f32, position.z as f32) * (-position.y as f32 - 10.0).clamp(0., 1.);
let v_dirt_noise = dirt_noise.get_noise3d(position.x as f32, position.y as f32, position.z as f32) * (-position.y as f32).clamp(0., 1.);
if v_cave_noise > 0.5 {
blocks[x][y][z] = Block::Stone;
} else if v_dirt_noise > 0.5 {
blocks[x][y][z] = Block::Dirt;
}
2023-01-27 15:32:23 -06:00
}
}
}
}
2023-01-26 18:04:01 -06:00
2023-01-28 16:41:34 -06:00
blocks
}