optimize worldgen

This commit is contained in:
griffi-gh 2024-05-14 00:52:45 +02:00
parent e73b336670
commit 1886126f62
6 changed files with 45 additions and 3 deletions

View file

@ -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);

View file

@ -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) {

View file

@ -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 {

View file

@ -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];

View file

@ -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));

View file

@ -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];