From 1886126f6210b8b8f31196d9f779d124242063fc Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Tue, 14 May 2024 00:52:45 +0200 Subject: [PATCH] optimize worldgen --- kubi-shared/src/worldgen/steps/_01_terrain.rs | 20 ++++++++++++++++++- kubi-shared/src/worldgen/steps/_02_water.rs | 13 +++++++++++- kubi-shared/src/worldgen/steps/_03_caves.rs | 9 ++++++++- kubi-shared/src/worldgen/steps/_04_layers.rs | 2 ++ .../src/worldgen/steps/_05_decorate.rs | 2 ++ kubi-shared/src/worldgen/steps/_06_trees.rs | 2 ++ 6 files changed, 45 insertions(+), 3 deletions(-) diff --git a/kubi-shared/src/worldgen/steps/_01_terrain.rs b/kubi-shared/src/worldgen/steps/_01_terrain.rs index 76d9b1c..ffccfa2 100644 --- a/kubi-shared/src/worldgen/steps/_01_terrain.rs +++ b/kubi-shared/src/worldgen/steps/_01_terrain.rs @@ -3,6 +3,9 @@ use glam::ivec3; use crate::{block::Block, chunk::CHUNK_SIZE}; use super::super::{SeedThingy, WorldGenStep, WorldGenerator}; +pub const MAX_TERAIN_HEIGHT: i32 = 32; +pub const MIN_TERRAIN_HEIGHT: i32 = -MAX_TERAIN_HEIGHT; + pub struct TerrainStep { noise: FastNoiseLite, } @@ -17,11 +20,26 @@ impl WorldGenStep for TerrainStep { } fn generate(&mut self, gen: &mut WorldGenerator) { + let is_oob_upper = gen.offset().y > MAX_TERAIN_HEIGHT; + if is_oob_upper { return } + + let is_oob_lower = (gen.offset().y + CHUNK_SIZE as i32) < MIN_TERRAIN_HEIGHT; + if is_oob_lower { + for x in 0..CHUNK_SIZE as i32 { + for y in 0..CHUNK_SIZE as i32 { + for z in 0..CHUNK_SIZE as i32 { + gen.place(ivec3(x, y, z), Block::Stone); + } + } + } + return + } + let mut height_map = vec![vec![0; CHUNK_SIZE]; CHUNK_SIZE]; for x in 0..CHUNK_SIZE as i32 { for z in 0..CHUNK_SIZE as i32 { let global_xz = gen.global_position(ivec3(x, 0, z)); - let height = (self.noise.get_noise_2d(global_xz.x as f64, global_xz.z as f64) * 32.0) as i32; + let height = (self.noise.get_noise_2d(global_xz.x as f64, global_xz.z as f64) * MAX_TERAIN_HEIGHT as f32) as i32; height_map[x as usize][z as usize] = height; for y in 0..gen.local_height(height) { gen.place(ivec3(x, y, z), Block::Stone); diff --git a/kubi-shared/src/worldgen/steps/_02_water.rs b/kubi-shared/src/worldgen/steps/_02_water.rs index bfa4a70..a290ec1 100644 --- a/kubi-shared/src/worldgen/steps/_02_water.rs +++ b/kubi-shared/src/worldgen/steps/_02_water.rs @@ -1,6 +1,9 @@ use glam::ivec3; use crate::{block::Block, chunk::CHUNK_SIZE, worldgen::SeedThingy}; -use super::super::{WorldGenerator, WorldGenStep}; +use super::{ + super::{WorldGenStep, WorldGenerator}, + _01_terrain::MIN_TERRAIN_HEIGHT, +}; pub const WATER_LEVEL: i32 = 0; @@ -9,6 +12,14 @@ pub struct WaterStep; impl WorldGenStep for WaterStep { fn initialize(_: &WorldGenerator, _: &mut SeedThingy) -> Self { Self } fn generate(&mut self, gen: &mut WorldGenerator) { + // If chunk's lower bound is above water level, we can skip this step + if gen.offset().y > WATER_LEVEL { + return + } + // If upper bound is below terrain, skip this step + if (gen.offset().y + CHUNK_SIZE as i32) < MIN_TERRAIN_HEIGHT { + return + } for x in 0..CHUNK_SIZE as i32 { for z in 0..CHUNK_SIZE as i32 { for y in 0..gen.local_height(WATER_LEVEL) { diff --git a/kubi-shared/src/worldgen/steps/_03_caves.rs b/kubi-shared/src/worldgen/steps/_03_caves.rs index 387b884..b2f6a3d 100644 --- a/kubi-shared/src/worldgen/steps/_03_caves.rs +++ b/kubi-shared/src/worldgen/steps/_03_caves.rs @@ -1,7 +1,10 @@ use fastnoise_lite::{FastNoiseLite, FractalType}; use glam::ivec3; use crate::{block::Block, chunk::CHUNK_SIZE}; -use super::super::{SeedThingy, WorldGenStep, WorldGenerator}; +use super::{ + super::{SeedThingy, WorldGenStep, WorldGenerator}, + _01_terrain::MAX_TERAIN_HEIGHT, +}; pub struct CaveStep { a: FastNoiseLite, @@ -22,6 +25,10 @@ impl WorldGenStep for CaveStep { } fn generate(&mut self, gen: &mut WorldGenerator) { + // If chunk's lower bound is above max terrain height, + // ...we can skip this step as caves cannot exist here + if gen.offset().y > MAX_TERAIN_HEIGHT { return } + for x in 0..CHUNK_SIZE as i32 { for y in 0..CHUNK_SIZE as i32 { for z in 0..CHUNK_SIZE as i32 { diff --git a/kubi-shared/src/worldgen/steps/_04_layers.rs b/kubi-shared/src/worldgen/steps/_04_layers.rs index 52204b9..cf58a24 100644 --- a/kubi-shared/src/worldgen/steps/_04_layers.rs +++ b/kubi-shared/src/worldgen/steps/_04_layers.rs @@ -11,6 +11,8 @@ impl WorldGenStep for LayersStep { fn initialize(_: &WorldGenerator, _: &mut SeedThingy) -> Self { Self } fn generate(&mut self, gen: &mut WorldGenerator) { + if gen.data.master_height_map.is_none() { return } + for x in 0..CHUNK_SIZE as i32 { for z in 0..CHUNK_SIZE as i32 { let terrain_height = gen.data.master_height_map.as_ref().unwrap()[x as usize][z as usize]; diff --git a/kubi-shared/src/worldgen/steps/_05_decorate.rs b/kubi-shared/src/worldgen/steps/_05_decorate.rs index 7efbc28..da312cc 100644 --- a/kubi-shared/src/worldgen/steps/_05_decorate.rs +++ b/kubi-shared/src/worldgen/steps/_05_decorate.rs @@ -11,6 +11,8 @@ impl WorldGenStep for DecorateStep { fn initialize(_: &WorldGenerator, _: &mut SeedThingy) -> Self { Self } fn generate(&mut self, gen: &mut WorldGenerator) { + if gen.data.master_height_map.is_none() { return } + for x in 0..CHUNK_SIZE as i32 { for z in 0..CHUNK_SIZE as i32 { let global_xz = gen.global_position(ivec3(x, 0, z)); diff --git a/kubi-shared/src/worldgen/steps/_06_trees.rs b/kubi-shared/src/worldgen/steps/_06_trees.rs index 1d268c1..d72d31f 100644 --- a/kubi-shared/src/worldgen/steps/_06_trees.rs +++ b/kubi-shared/src/worldgen/steps/_06_trees.rs @@ -21,6 +21,8 @@ impl WorldGenStep for TreesStep { } fn generate(&mut self, gen: &mut WorldGenerator) { + if gen.data.master_height_map.is_none() { return } + for x in 0..CHUNK_SIZE as i32 { for z in 0..CHUNK_SIZE as i32 { let terrain_height = gen.data.master_height_map.as_ref().unwrap()[x as usize][z as usize];