From 8f606b77c02e6d0b64118b56fa3f459ad1dbbad5 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sat, 4 May 2024 13:50:58 +0200 Subject: [PATCH] use single seeder instance --- kubi-shared/src/worldgen.rs | 5 +++-- kubi-shared/src/worldgen/steps/_01_terrain.rs | 3 +-- kubi-shared/src/worldgen/steps/_02_water.rs | 4 ++-- kubi-shared/src/worldgen/steps/_03_caves.rs | 4 +--- kubi-shared/src/worldgen/steps/_04_layers.rs | 7 ++++--- kubi-shared/src/worldgen/steps/_05_decorate.rs | 6 +++--- kubi-shared/src/worldgen/steps/_06_trees.rs | 5 ++--- 7 files changed, 16 insertions(+), 18 deletions(-) diff --git a/kubi-shared/src/worldgen.rs b/kubi-shared/src/worldgen.rs index f5ebba6..98d1565 100644 --- a/kubi-shared/src/worldgen.rs +++ b/kubi-shared/src/worldgen.rs @@ -48,7 +48,7 @@ impl SeedThingy { } } trait WorldGenStep { - fn initialize(generator: &WorldGenerator) -> Self; + fn initialize(generator: &WorldGenerator, seeder: &mut SeedThingy) -> Self; fn generate(&mut self, generator: &mut WorldGenerator); } @@ -67,11 +67,12 @@ macro_rules! run_steps { if _chkabt() { return false } + let mut _seeder = $crate::worldgen::SeedThingy::new($gen.seed); $({ let _ensure_ref: &mut $crate::worldgen::WorldGenerator = $gen; struct _Ensure0(T); type _Ensure1 = _Ensure0<$step>; - let mut step: _Ensure1 = _Ensure0(<$step>::initialize(&*_ensure_ref)); + let mut step: _Ensure1 = _Ensure0(<$step>::initialize(&*_ensure_ref, &mut _seeder)); if _chkabt() { return false } step.0.generate(_ensure_ref); if _chkabt() { return false } diff --git a/kubi-shared/src/worldgen/steps/_01_terrain.rs b/kubi-shared/src/worldgen/steps/_01_terrain.rs index 0a0bc36..76d9b1c 100644 --- a/kubi-shared/src/worldgen/steps/_01_terrain.rs +++ b/kubi-shared/src/worldgen/steps/_01_terrain.rs @@ -8,8 +8,7 @@ pub struct TerrainStep { } impl WorldGenStep for TerrainStep { - fn initialize(generator: &WorldGenerator) -> Self { - let mut seeder = SeedThingy::new(generator.seed); + fn initialize(_: &WorldGenerator, seeder: &mut SeedThingy) -> Self { let mut noise = FastNoiseLite::with_seed(seeder.next_seed()); noise.set_fractal_type(Some(FractalType::FBm)); noise.set_fractal_octaves(Some(4)); diff --git a/kubi-shared/src/worldgen/steps/_02_water.rs b/kubi-shared/src/worldgen/steps/_02_water.rs index 80c6a3a..bfa4a70 100644 --- a/kubi-shared/src/worldgen/steps/_02_water.rs +++ b/kubi-shared/src/worldgen/steps/_02_water.rs @@ -1,5 +1,5 @@ use glam::ivec3; -use crate::{block::Block, chunk::CHUNK_SIZE}; +use crate::{block::Block, chunk::CHUNK_SIZE, worldgen::SeedThingy}; use super::super::{WorldGenerator, WorldGenStep}; pub const WATER_LEVEL: i32 = 0; @@ -7,7 +7,7 @@ pub const WATER_LEVEL: i32 = 0; pub struct WaterStep; impl WorldGenStep for WaterStep { - fn initialize(_: &WorldGenerator) -> Self { Self } + fn initialize(_: &WorldGenerator, _: &mut SeedThingy) -> Self { Self } fn generate(&mut self, gen: &mut WorldGenerator) { for x in 0..CHUNK_SIZE as i32 { for z in 0..CHUNK_SIZE as i32 { diff --git a/kubi-shared/src/worldgen/steps/_03_caves.rs b/kubi-shared/src/worldgen/steps/_03_caves.rs index ed3540f..387b884 100644 --- a/kubi-shared/src/worldgen/steps/_03_caves.rs +++ b/kubi-shared/src/worldgen/steps/_03_caves.rs @@ -9,9 +9,7 @@ pub struct CaveStep { } impl WorldGenStep for CaveStep { - fn initialize(gen: &WorldGenerator) -> Self { - let mut seeder = SeedThingy::new(gen.seed); - + fn initialize(_: &WorldGenerator, seeder: &mut SeedThingy) -> Self { let mut a = FastNoiseLite::with_seed(seeder.next_seed()); a.set_fractal_type(Some(FractalType::FBm)); a.set_fractal_octaves(Some(2)); diff --git a/kubi-shared/src/worldgen/steps/_04_layers.rs b/kubi-shared/src/worldgen/steps/_04_layers.rs index a80354f..f6d873d 100644 --- a/kubi-shared/src/worldgen/steps/_04_layers.rs +++ b/kubi-shared/src/worldgen/steps/_04_layers.rs @@ -1,5 +1,6 @@ +use fastnoise_lite::FastNoiseLite; use glam::ivec3; -use crate::{block::Block, chunk::CHUNK_SIZE}; +use crate::{block::Block, chunk::CHUNK_SIZE, worldgen::SeedThingy}; use super::{ _02_water::WATER_LEVEL, super::{WorldGenStep, WorldGenerator} @@ -8,7 +9,7 @@ use super::{ pub struct LayersStep; impl WorldGenStep for LayersStep { - fn initialize(_: &WorldGenerator) -> Self { Self } + fn initialize(_: &WorldGenerator, _: &mut SeedThingy) -> Self { Self } fn generate(&mut self, gen: &mut WorldGenerator) { for x in 0..CHUNK_SIZE as i32 { @@ -17,7 +18,7 @@ impl WorldGenStep for LayersStep { // Dirt layer height, naturally gets thinner as height gets deeper let mut dirt_layer_height = (((terrain_height as f32 + 15.) / 20.).clamp(0., 1.) * 8.).round() as i32; - dirt_layer_height -= (gen.seeded_hash((x, z, 1)) & 1) as i32; //+ (gen.seeded_hash((x, z, 0xbau8)) & 1) as i32; + dirt_layer_height -= (gen.seeded_hash((x, z, 0x040)) & 1) as i32; //+ (gen.seeded_hash((x, z, 0x041)) & 1) as i32; // Place dirt layer for y in gen.local_height(terrain_height - dirt_layer_height)..gen.local_height(terrain_height) { diff --git a/kubi-shared/src/worldgen/steps/_05_decorate.rs b/kubi-shared/src/worldgen/steps/_05_decorate.rs index 20e9b32..7efbc28 100644 --- a/kubi-shared/src/worldgen/steps/_05_decorate.rs +++ b/kubi-shared/src/worldgen/steps/_05_decorate.rs @@ -1,5 +1,5 @@ use glam::ivec3; -use crate::{block::Block, chunk::CHUNK_SIZE}; +use crate::{block::Block, chunk::CHUNK_SIZE, worldgen::SeedThingy}; use super::{ _02_water::WATER_LEVEL, super::{WorldGenStep, WorldGenerator}, @@ -8,7 +8,7 @@ use super::{ pub struct DecorateStep; impl WorldGenStep for DecorateStep { - fn initialize(_: &WorldGenerator) -> Self { Self } + fn initialize(_: &WorldGenerator, _: &mut SeedThingy) -> Self { Self } fn generate(&mut self, gen: &mut WorldGenerator) { for x in 0..CHUNK_SIZE as i32 { @@ -20,7 +20,7 @@ impl WorldGenStep for DecorateStep { //Place tall grass if terrain_height >= WATER_LEVEL { if let Some(local_y) = gen.local_y_position(terrain_height) { - if (gen.seeded_hash((global_xz.x, global_xz.z)) & 0xf) == 0xf { + if (gen.seeded_hash((global_xz.x, global_xz.z, 0x050)) & 0xf) == 0xf { gen.place_if_empty(ivec3(x, local_y, z), Block::TallGrass); } } diff --git a/kubi-shared/src/worldgen/steps/_06_trees.rs b/kubi-shared/src/worldgen/steps/_06_trees.rs index 4b0a969..1d268c1 100644 --- a/kubi-shared/src/worldgen/steps/_06_trees.rs +++ b/kubi-shared/src/worldgen/steps/_06_trees.rs @@ -13,8 +13,7 @@ pub struct TreesStep { } impl WorldGenStep for TreesStep { - fn initialize(gen: &WorldGenerator) -> Self { - let mut seeder = SeedThingy::new(gen.seed.rotate_left(5)); + fn initialize(_: &WorldGenerator, seeder: &mut SeedThingy) -> Self { let mut density_noise = FastNoiseLite::with_seed(seeder.next_seed()); density_noise.set_noise_type(Some(NoiseType::OpenSimplex2)); density_noise.set_frequency(Some(0.008)); @@ -30,7 +29,7 @@ impl WorldGenStep for TreesStep { let global_xz = gen.global_position(ivec3(x, 0, z)); let mut density = self.density_noise.get_noise_2d(global_xz.x as f64, global_xz.z as f64) * 0.5 + 0.5; density = density.powi(3); - if gen.seeded_hash((global_xz.x, global_xz.z, 0xfef)) & 0xff >= (density * 7.).round() as u64 { + if gen.seeded_hash((global_xz.x, global_xz.z, 0x060)) & 0xff >= (density * 7.).round() as u64 { continue }