From a34b859777b2c7e2844abb597f3aeda5c2cf2cdb Mon Sep 17 00:00:00 2001 From: Alexander Medvedev Date: Thu, 10 Sep 2026 21:57:07 +0200 Subject: [PATCH] fix: carver --- crates/pumpkin-data/src/generated/carver.rs | 4 +- .../src/generation/carver/canyon.rs | 219 +++++----------- .../src/generation/carver/cave.rs | 242 ++++++----------- .../src/generation/carver/mask.rs | 177 +++++++++++-- .../src/generation/carver/mod.rs | 244 ++++++++++++++---- .../noise/router/density_function/test.rs | 4 +- .../src/generation/proto_chunk.rs | 5 - tools/pumpkin-codegen/src/carver.rs | 4 +- 8 files changed, 501 insertions(+), 398 deletions(-) diff --git a/crates/pumpkin-data/src/generated/carver.rs b/crates/pumpkin-data/src/generated/carver.rs index 57ab7dea9..c096c9998 100644 --- a/crates/pumpkin-data/src/generated/carver.rs +++ b/crates/pumpkin-data/src/generated/carver.rs @@ -65,7 +65,7 @@ pub struct CanyonShapeConfig { pub horizontal_radius_factor: FloatProvider, pub vertical_radius_default_factor: f32, pub vertical_radius_center_factor: f32, - pub y_scale: f32, + pub y_scale: FloatProvider, } #[derive(Clone, Debug)] pub struct CanyonCarverConfig { @@ -107,7 +107,7 @@ pub const CANYON: CarverConfig = CarverConfig { )), vertical_radius_default_factor: 1f32, vertical_radius_center_factor: 0f32, - y_scale: 3f32, + y_scale: FloatProvider::Constant(3f32), }, }), }; diff --git a/crates/pumpkin-world/src/generation/carver/canyon.rs b/crates/pumpkin-world/src/generation/carver/canyon.rs index 2133a4105..17b2b5fd7 100644 --- a/crates/pumpkin-world/src/generation/carver/canyon.rs +++ b/crates/pumpkin-world/src/generation/carver/canyon.rs @@ -1,5 +1,5 @@ use super::cave::get_height; -use super::{CarveRun, Carver, overworld_carve_state, place_carved_block}; +use super::{Carver, CarverOutput}; use pumpkin_data::carver::{CarverAdditionalConfig, CarverConfig}; use pumpkin_util::math::vector2::Vector2; use pumpkin_util::random::{RandomGenerator, RandomImpl}; @@ -11,36 +11,39 @@ impl Carver for CanyonCarver { fn carve( &self, config: &CarverConfig, - run: &mut CarveRun, + output: &mut dyn CarverOutput, random: &mut RandomGenerator, - _chunk_pos: &Vector2, + chunk_pos: &Vector2, carver_chunk_pos: &Vector2, + min_gen_y: i8, + gen_depth: u16, legacy_random_source: bool, ) { let CarverAdditionalConfig::Canyon(ref canyon_config) = config.additional else { return; }; - let min_y = run.chunk.generation_bottom_y() as i32; - let height = run.chunk.generation_height(); - let max_distance = (4 * 2 - 1) * 16; let x = (carver_chunk_pos.x << 4) + random.next_bounded_i32(16); - let y = get_height(&config.y, random, min_y as i8, height); + let y = get_height(&config.y, random, min_gen_y, gen_depth); let z = (carver_chunk_pos.y << 4) + random.next_bounded_i32(16); let horizontal_rotation = random.next_f32() * PI * 2.0; let vertical_rotation = canyon_config.vertical_rotation.get(random); - let y_scale = canyon_config.shape.y_scale as f64; + let y_scale = canyon_config.shape.y_scale.get(random) as f64; let thickness = canyon_config.shape.thickness.get(random); let distance = (max_distance as f32 * canyon_config.shape.distance_factor.get(random)) as i32; + let tunnel_seed = random.next_i64(); Self::do_carve( config, - run, - random.next_i64(), + *chunk_pos, + output, + min_gen_y, + gen_depth, + tunnel_seed, x as f64, y as f64, z as f64, @@ -59,7 +62,10 @@ impl CanyonCarver { #[allow(clippy::too_many_arguments)] fn do_carve( config: &CarverConfig, - run: &mut CarveRun, + chunk_pos: Vector2, + output: &mut dyn CarverOutput, + min_gen_y: i8, + gen_depth: u16, tunnel_seed: i64, mut x: f64, mut y: f64, @@ -74,7 +80,7 @@ impl CanyonCarver { ) { let mut random = super::new_carver_random(tunnel_seed as u64, legacy_random_source); let width_factor_per_height = - Self::init_width_factors(run.chunk.generation_height() as usize, config, &mut random); + Self::init_width_factors(gen_depth as usize, config, &mut random); let mut y_rota = 0.0f32; let mut x_rota = 0.0f32; @@ -105,36 +111,36 @@ impl CanyonCarver { y += xs as f64; z += f64::from(pumpkin_util::math::sin(horizontal_rotation) * xc); - vertical_rotation *= 0.7; - vertical_rotation += x_rota * 0.05; + vertical_rotation = (vertical_rotation * 0.7) + (x_rota * 0.05); horizontal_rotation += y_rota * 0.05; - x_rota *= 0.8; - y_rota *= 0.5; - x_rota += (random.next_f32() - random.next_f32()) * random.next_f32() * 2.0; - y_rota += (random.next_f32() - random.next_f32()) * random.next_f32() * 4.0; + x_rota = (x_rota * 0.8) + + ((random.next_f32() - random.next_f32()) * random.next_f32() * 2.0); + y_rota = (y_rota * 0.5) + + ((random.next_f32() - random.next_f32()) * random.next_f32() * 4.0); if random.next_bounded_i32(4) != 0 { - if !Self::can_reach( - run.chunk.x, - run.chunk.z, - x, - z, - current_step, - distance, - thickness, - ) { + if !super::can_reach(&chunk_pos, x, z, current_step, distance, thickness) { return; } - Self::carve_ellipsoid( - run, - config, + super::carve_ellipsoid( + &chunk_pos, x, y, z, horizontal_radius, vertical_radius, - &width_factor_per_height, + output, + |xd, yd, zd, world_y| { + Self::should_skip( + &width_factor_per_height, + xd, + yd, + zd, + world_y, + min_gen_y as i32, + ) + }, ); } } @@ -176,101 +182,6 @@ impl CanyonCarver { factor as f64 * vertical_radius * random.next_inbetween_f32(0.75, 1.0) as f64 } - #[allow(clippy::too_many_arguments)] - fn can_reach( - chunk_x: i32, - chunk_z: i32, - x: f64, - z: f64, - step: i32, - distance: i32, - thickness: f32, - ) -> bool { - let chunk_middle_x = (chunk_x << 4) + 8; - let chunk_middle_z = (chunk_z << 4) + 8; - let dx = x - chunk_middle_x as f64; - let dz = z - chunk_middle_z as f64; - let remaining = (distance - step) as f64; - let rr = (thickness + 2.0 + 16.0) as f64; - dx * dx + dz * dz - remaining * remaining <= rr * rr - } - - #[allow(clippy::too_many_arguments)] - fn carve_ellipsoid( - run: &mut CarveRun, - config: &CarverConfig, - x: f64, - y: f64, - z: f64, - horizontal_radius: f64, - vertical_radius: f64, - width_factor_per_height: &[f32], - ) { - let center_x = (run.chunk.x << 4) as f64 + 8.0; - let center_z = (run.chunk.z << 4) as f64 + 8.0; - let max_delta = 16.0 + horizontal_radius * 2.0; - - if (x - center_x).abs() > max_delta || (z - center_z).abs() > max_delta { - return; - } - - let chunk_min_x = run.chunk.x << 4; - let chunk_min_z = run.chunk.z << 4; - - let x_index_min = ((x - horizontal_radius).floor() as i32 - chunk_min_x - 1).max(0); - let x_index_max = ((x + horizontal_radius).floor() as i32 - chunk_min_x).min(15); - - let min_y = ((y - vertical_radius).floor() as i32 - 1) - .max(run.chunk.generation_bottom_y() as i32 + 1); - let protected_blocks_on_top = 7; - let max_y = ((y + vertical_radius).floor() as i32 + 1).min( - run.chunk.generation_bottom_y() as i32 + run.chunk.generation_height() as i32 - - 1 - - protected_blocks_on_top, - ); - - let z_index_min = ((z - horizontal_radius).floor() as i32 - chunk_min_z - 1).max(0); - let z_index_max = ((z + horizontal_radius).floor() as i32 - chunk_min_z).min(15); - - for x_index in x_index_min..=x_index_max { - let world_x = chunk_min_x + x_index; - let xd = (world_x as f64 + 0.5 - x) / horizontal_radius; - - for z_index in z_index_min..=z_index_max { - let world_z = chunk_min_z + z_index; - let zd = (world_z as f64 + 0.5 - z) / horizontal_radius; - - if xd * xd + zd * zd < 1.0 { - let mut has_grass = false; - - for world_y in (min_y + 1..=max_y).rev() { - let yd = (world_y as f64 - 0.5 - y) / vertical_radius; - - if !Self::should_skip( - width_factor_per_height, - xd, - yd, - zd, - world_y, - run.chunk.generation_bottom_y() as i32, - ) && !run.chunk.carving_mask.get(world_x, world_y, world_z) - { - run.chunk.carving_mask.set(world_x, world_y, world_z); - Self::carve_block( - run, - config, - world_x, - world_y, - world_z, - &mut has_grass, - ); - } - } - } - } - } - } - fn should_skip( width_factor_per_height: &[f32], xd: f64, @@ -283,41 +194,37 @@ impl CanyonCarver { if y_index == 0 { return true; } - (xd * xd + zd * zd) * width_factor_per_height[y_index - 1] as f64 + yd * yd / 6.0 >= 1.0 + (xd * xd + zd * zd) * width_factor_per_height[y_index - 1] as f64 + (yd * yd) / 6.0 >= 1.0 } +} - fn carve_block( - run: &mut CarveRun, - _config: &CarverConfig, - x: i32, - y: i32, - z: i32, - has_grass: &mut bool, - ) -> bool { - let local_y = y - run.chunk.bottom_y() as i32; - let state_id = run.chunk.get_block_state_raw(x & 15, local_y, z & 15); - let block = pumpkin_data::Block::from_state_id(state_id); +#[cfg(test)] +mod tests { + use super::*; + use crate::generation::carver::mask::CarvingMask; + use pumpkin_data::carver::CANYON; + use pumpkin_util::random::RandomGenerator; - if block.id == pumpkin_data::Block::GRASS_BLOCK.id - || block.id == pumpkin_data::Block::MYCELIUM.id - { - *has_grass = true; - } - - let Some((state, should_schedule_fluid_update)) = overworld_carve_state(run, x, y, z) - else { - return false; - }; + #[test] + fn canyon_carve() { + let chunk_pos = Vector2::new(0, 0); + let carver_chunk_pos = Vector2::new(0, 0); + let mut mask = CarvingMask::new(-64, 320); + let mut random = RandomGenerator::Legacy( + pumpkin_util::random::legacy_rand::LegacyRand::from_seed(12345), + ); - place_carved_block( - run, - pumpkin_util::math::vector3::Vector3::new(x, y, z), - state, - should_schedule_fluid_update, - *has_grass, - true, + CanyonCarver.carve( + &CANYON, + &mut mask, + &mut random, + &chunk_pos, + &carver_chunk_pos, + -64, + 384, + false, ); - true + assert!(!mask.is_empty()); } } diff --git a/crates/pumpkin-world/src/generation/carver/cave.rs b/crates/pumpkin-world/src/generation/carver/cave.rs index 36f13502b..744677b7c 100644 --- a/crates/pumpkin-world/src/generation/carver/cave.rs +++ b/crates/pumpkin-world/src/generation/carver/cave.rs @@ -1,4 +1,5 @@ -use super::{CarveRun, Carver, overworld_carve_state, place_carved_block}; +use super::CarverOutput; +use super::{Carver, overworld_carve_state, place_carved_block}; use pumpkin_data::carver::{CarverAdditionalConfig, CarverConfig, HeightProvider}; use pumpkin_util::math::vector2::Vector2; use pumpkin_util::math::vector3::Vector3; @@ -11,51 +12,52 @@ impl Carver for CaveCarver { fn carve( &self, config: &CarverConfig, - run: &mut CarveRun, + output: &mut dyn CarverOutput, random: &mut RandomGenerator, - _chunk_pos: &Vector2, + chunk_pos: &Vector2, carver_chunk_pos: &Vector2, + min_gen_y: i8, + gen_depth: u16, legacy_random_source: bool, ) { let CarverAdditionalConfig::Cave(ref cave_config) = config.additional else { return; }; - let min_y = run.chunk.generation_bottom_y() as i32; - let height = run.chunk.generation_height(); - let max_distance = (4 * 2 - 1) << 4; - let cave_count = cave_config.count.get(random); for _ in 0..cave_count { let x = (carver_chunk_pos.x << 4) + random.next_bounded_i32(16); - let y = get_height(&config.y, random, min_y as i8, height) as f64; + let y = get_height(&config.y, random, min_gen_y, gen_depth) as f64; let z = (carver_chunk_pos.y << 4) + random.next_bounded_i32(16); let horizontal_radius_multiplier = cave_config.horizontal_radius_multiplier.get(random) as f64; let vertical_radius_multiplier = cave_config.vertical_radius_multiplier.get(random) as f64; + let start_vertical_radius_multiplier = + cave_config.start_vertical_radius_multiplier.get(random) as f64; let floor_level = cave_config.floor_level.get(random) as f64; - let mut tunnels = 1; - if random.next_bounded_i32(4) == 0 { + let tunnels = if random.next_bounded_i32(4) == 0 { let room_vertical_radius_multiplier = cave_config.room_vertical_radius_multiplier.get(random) as f64; let thickness = 1.0 + random.next_f32() * 6.0; Self::create_room( - run, + *chunk_pos, + output, x as f64, y, z as f64, thickness, room_vertical_radius_multiplier, - config, floor_level, ); - tunnels += random.next_bounded_i32(4); - } + 1 + random.next_bounded_i32(4) + } else { + 1 + }; for _ in 0..tunnels { let horizontal_rotation = random.next_f32() * PI * 2.0; @@ -65,13 +67,12 @@ impl Carver for CaveCarver { thickness *= random.next_f32() * random.next_f32() * 3.0 + 1.0; } let distance = max_distance - random.next_bounded_i32(max_distance / 4); - let start_vertical_radius_multiplier = - cave_config.start_vertical_radius_multiplier.get(random) as f64; + let tunnel_seed = random.next_i64(); Self::create_tunnel( - config, - run, - random.next_i64(), + *chunk_pos, + output, + tunnel_seed, x as f64, y, z as f64, @@ -94,34 +95,34 @@ impl Carver for CaveCarver { impl CaveCarver { #[allow(clippy::too_many_arguments)] fn create_room( - run: &mut CarveRun, + chunk_pos: Vector2, + output: &mut dyn CarverOutput, x: f64, y: f64, z: f64, thickness: f32, y_scale: f64, - config: &CarverConfig, floor_level: f64, ) { let horizontal_radius = 1.5 + f64::from(pumpkin_util::math::sin(std::f32::consts::FRAC_PI_2) * thickness); let vertical_radius = horizontal_radius * y_scale; - Self::carve_ellipsoid( - run, - config, + super::carve_ellipsoid( + &chunk_pos, x + 1.0, y, z, horizontal_radius, vertical_radius, - floor_level, + output, + |xd, yd, zd, _world_y| Self::should_skip(xd, yd, zd, floor_level), ); } #[allow(clippy::too_many_arguments)] fn create_tunnel( - config: &CarverConfig, - run: &mut CarveRun, + chunk_pos: Vector2, + output: &mut dyn CarverOutput, tunnel_seed: i64, mut x: f64, mut y: f64, @@ -153,18 +154,18 @@ impl CaveCarver { y += f64::from(pumpkin_util::math::sin(vertical_rotation)); z += f64::from(pumpkin_util::math::sin(horizontal_rotation) * cos_x); - vertical_rotation *= if is_steep { 0.92 } else { 0.7 }; - vertical_rotation += x_rota * 0.1; + vertical_rotation = + (vertical_rotation * if is_steep { 0.92 } else { 0.7 }) + (x_rota * 0.1); horizontal_rotation += y_rota * 0.1; - x_rota *= 0.9; - y_rota *= 0.75; - x_rota += (random.next_f32() - random.next_f32()) * random.next_f32() * 2.0; - y_rota += (random.next_f32() - random.next_f32()) * random.next_f32() * 4.0; + x_rota = (x_rota * 0.9) + + ((random.next_f32() - random.next_f32()) * random.next_f32() * 2.0); + y_rota = (y_rota * 0.75) + + ((random.next_f32() - random.next_f32()) * random.next_f32() * 4.0); if current_step == split_point && thickness > 1.0 { Self::create_tunnel( - config, - run, + chunk_pos, + output, random.next_i64(), x, y, @@ -181,8 +182,8 @@ impl CaveCarver { legacy_random_source, ); Self::create_tunnel( - config, - run, + chunk_pos, + output, random.next_i64(), x, y, @@ -202,145 +203,41 @@ impl CaveCarver { } if random.next_bounded_i32(4) != 0 { - if !Self::can_reach( - run.chunk.x, - run.chunk.z, - x, - z, - current_step, - dist, - thickness, - ) { + if !super::can_reach(&chunk_pos, x, z, current_step, dist, thickness) { return; } - Self::carve_ellipsoid( - run, - config, + super::carve_ellipsoid( + &chunk_pos, x, y, z, horizontal_radius * horizontal_radius_multiplier, vertical_radius * vertical_radius_multiplier, - floor_level, + output, + |xd, yd, zd, _world_y| Self::should_skip(xd, yd, zd, floor_level), ); } } } - #[must_use] - fn can_reach( - chunk_x: i32, - chunk_z: i32, - x: f64, - z: f64, - step: i32, - distance: i32, - thickness: f32, - ) -> bool { - let chunk_middle_x = (chunk_x << 4) + 8; - let chunk_middle_z = (chunk_z << 4) + 8; - let dx = x - chunk_middle_x as f64; - let dz = z - chunk_middle_z as f64; - let remaining = (distance - step) as f64; - let rr = (thickness + 2.0 + 16.0) as f64; - dx * dx + dz * dz - remaining * remaining <= rr * rr - } - - #[allow(clippy::too_many_arguments)] - fn carve_ellipsoid( - run: &mut CarveRun, - config: &CarverConfig, - x: f64, - y: f64, - z: f64, - horizontal_radius: f64, - vertical_radius: f64, - floor_level: f64, - ) { - let chunk_min_x = run.chunk.x << 4; - let chunk_min_z = run.chunk.z << 4; - - if !Self::in_chunk_range( - chunk_min_x, - chunk_min_z, - x, - z, - horizontal_radius, - horizontal_radius, - ) { - return; - } - - let min_y = 1.max((y - vertical_radius).floor() as i32 - 1); - let max_y = (run.chunk.generation_height() as i32 - 1) - .min((y + vertical_radius).floor() as i32 + 1); - - let x_index_min = ((x - horizontal_radius).floor() as i32 - chunk_min_x - 1).max(0); - let x_index_max = ((x + horizontal_radius).floor() as i32 - chunk_min_x).min(15); - - let is_overworld = run.ctx.carver_aquifer.is_some(); - let protected_blocks_on_top = i32::from(!is_overworld); - let max_y = max_y.min( - (run.chunk.generation_bottom_y() as i32 + run.chunk.generation_height() as i32) - - 1 - - protected_blocks_on_top, - ); - - let z_index_min = ((z - horizontal_radius).floor() as i32 - chunk_min_z - 1).max(0); - let z_index_max = ((z + horizontal_radius).floor() as i32 - chunk_min_z).min(15); - - for x_index in x_index_min..=x_index_max { - let world_x = chunk_min_x + x_index; - let xd = (world_x as f64 + 0.5 - x) / horizontal_radius; - - for z_index in z_index_min..=z_index_max { - let world_z = chunk_min_z + z_index; - let zd = (world_z as f64 + 0.5 - z) / horizontal_radius; - - if xd * xd + zd * zd < 1.0 { - let mut has_grass = false; - - for world_y in (min_y + 1..=max_y).rev() { - let yd = (world_y as f64 - 0.5 - y) / vertical_radius; - - if !Self::should_skip(xd, yd, zd, floor_level) - && !run.chunk.carving_mask.get(world_x, world_y, world_z) - { - run.chunk.carving_mask.set(world_x, world_y, world_z); - Self::carve_block( - run, - config, - world_x, - world_y, - world_z, - &mut has_grass, - ); - } - } - } - } - } - } - fn should_skip(xd: f64, yd: f64, zd: f64, floor_level: f64) -> bool { - if yd <= floor_level { - true - } else { - xd * xd + yd * yd + zd * zd >= 1.0 - } + yd <= floor_level || xd * xd + yd * yd + zd * zd >= 1.0 } - fn carve_block( - run: &mut CarveRun, + pub fn carve_block( + run: &mut super::CarveRun, _config: &CarverConfig, x: i32, y: i32, z: i32, has_grass: &mut bool, ) -> bool { - let state = run.chunk.get_block_state(&Vector3::new(x, y, z)); - let block = state.to_block(); + let current_state = run.chunk.get_block_state(&Vector3::new(x, y, z)); + if current_state == pumpkin_data::Block::BEDROCK.default_state.id { + return false; + } + let block = pumpkin_data::Block::from_state_id(current_state); if block.id == pumpkin_data::Block::GRASS_BLOCK.id || block.id == pumpkin_data::Block::MYCELIUM.id @@ -366,20 +263,6 @@ impl CaveCarver { true } - - fn in_chunk_range( - chunk_min_x: i32, - chunk_min_z: i32, - x: f64, - z: f64, - horizontal_radius_x: f64, - horizontal_radius_z: f64, - ) -> bool { - x >= (chunk_min_x as f64 - 16.0 - horizontal_radius_x * 2.0) - && z >= (chunk_min_z as f64 - 16.0 - horizontal_radius_z * 2.0) - && x <= (chunk_min_x as f64 + 16.0 + 16.0 + horizontal_radius_x * 2.0) - && z <= (chunk_min_z as f64 + 16.0 + 16.0 + horizontal_radius_z * 2.0) - } } pub fn get_height(p: &HeightProvider, random: &mut RandomGenerator, min_y: i8, height: u16) -> i32 { @@ -432,6 +315,29 @@ mod tests { type Run<'a, 'b> = super::super::CarveRun<'a, 'b>; + #[test] + fn cave_carve() { + let chunk_pos = Vector2::new(0, 0); + let carver_chunk_pos = Vector2::new(0, 0); + let mut mask = crate::generation::carver::mask::CarvingMask::new(-64, 320); + let mut random = RandomGenerator::Legacy( + pumpkin_util::random::legacy_rand::LegacyRand::from_seed(12345), + ); + + CaveCarver.carve( + &CAVE, + &mut mask, + &mut random, + &chunk_pos, + &carver_chunk_pos, + -64, + 384, + false, + ); + + assert!(!mask.is_empty()); + } + #[test] fn carves_at_world_y() { super::super::with_carve_run(Dimension::OVERWORLD, |run| { diff --git a/crates/pumpkin-world/src/generation/carver/mask.rs b/crates/pumpkin-world/src/generation/carver/mask.rs index f9ccd2773..1c3db7831 100644 --- a/crates/pumpkin-world/src/generation/carver/mask.rs +++ b/crates/pumpkin-world/src/generation/carver/mask.rs @@ -1,40 +1,181 @@ +use super::CarverOutput; + pub struct CarvingMask { min_y: i32, + max_y: i32, + height: usize, mask: Vec, } impl CarvingMask { #[must_use] - pub fn new(height: i32, min_y: i32) -> Self { - // Each u64 stores 64 bits. Total bits needed: 16 * 16 * height. - let total_bits = 16 * 16 * height; - let num_u64s = (total_bits as usize).div_ceil(64); + pub fn new(min_y: i32, max_y: i32) -> Self { + let height = ((max_y - min_y) + 1).max(0) as usize; + let total_bits = 256 * height; + let num_u64s = total_bits.div_ceil(64); Self { min_y, + max_y, + height, mask: vec![0; num_u64s], } } - const fn get_bit_index(&self, x: i32, y: i32, z: i32) -> usize { - // x: 0..16, z: 0..16, y: min_y..min_y+height - let rel_x = x & 15; - let rel_z = z & 15; - let rel_y = y - self.min_y; - (rel_x | (rel_z << 4) | (rel_y << 8)) as usize + #[must_use] + pub const fn min_y(&self) -> i32 { + self.min_y } - pub fn set(&mut self, x: i32, y: i32, z: i32) { - let bit_index = self.get_bit_index(x, y, z); - let u64_idx = bit_index / 64; - let bit_in_u64 = bit_index % 64; + #[must_use] + pub const fn max_y(&self) -> i32 { + self.max_y + } + + #[must_use] + pub const fn height(&self) -> usize { + self.height + } + + pub fn set(&mut self, x: usize, y: i32, z: usize) { + if y < self.min_y || y > self.max_y || x >= 16 || z >= 16 { + return; + } + let index = (y - self.min_y) as usize + ((z + (x << 4)) * self.height); + let u64_idx = index / 64; + let bit_in_u64 = index % 64; self.mask[u64_idx] |= 1 << bit_in_u64; } #[must_use] - pub fn get(&self, x: i32, y: i32, z: i32) -> bool { - let bit_index = self.get_bit_index(x, y, z); - let u64_idx = bit_index / 64; - let bit_in_u64 = bit_index % 64; + pub fn get(&self, x: usize, y: i32, z: usize) -> bool { + if y < self.min_y || y > self.max_y || x >= 16 || z >= 16 { + return false; + } + let index = (y - self.min_y) as usize + ((z + (x << 4)) * self.height); + let u64_idx = index / 64; + let bit_in_u64 = index % 64; (self.mask[u64_idx] >> bit_in_u64) & 1 != 0 } + + #[must_use] + pub fn is_empty(&self) -> bool { + self.mask.iter().all(|&w| w == 0) + } + + #[must_use] + pub fn next_set_bit(&self, from: usize) -> Option { + let total_bits = 256 * self.height; + if from >= total_bits { + return None; + } + let mut word_idx = from / 64; + let bit_idx = from % 64; + let mut word = self.mask[word_idx] & (!0u64 << bit_idx); + loop { + if word != 0 { + let bit = word.trailing_zeros() as usize; + let idx = word_idx * 64 + bit; + return (idx < total_bits).then_some(idx); + } + word_idx += 1; + if word_idx >= self.mask.len() { + return None; + } + word = self.mask[word_idx]; + } + } + + #[must_use] + pub fn next_clear_bit(&self, from: usize) -> usize { + let total_bits = 256 * self.height; + if from >= total_bits { + return total_bits; + } + let mut word_idx = from / 64; + let bit_idx = from % 64; + let mut word = (!self.mask[word_idx]) & (!0u64 << bit_idx); + loop { + if word != 0 { + let bit = word.trailing_zeros() as usize; + let idx = word_idx * 64 + bit; + return idx.min(total_bits); + } + word_idx += 1; + if word_idx >= self.mask.len() { + return total_bits; + } + word = !self.mask[word_idx]; + } + } + + pub fn visit(&self, mut visitor: F) + where + F: FnMut(usize, usize, i32, i32), + { + let total_bits = 256 * self.height; + let mut next_set = self.next_set_bit(0); + while let Some(start_index) = next_set { + let end_index = self.next_clear_bit(start_index) - 1; + self.visit_segment(&mut visitor, start_index, end_index); + if end_index + 1 >= total_bits { + break; + } + next_set = self.next_set_bit(end_index + 1); + } + } + + fn visit_segment(&self, visitor: &mut F, start_index: usize, end_index: usize) + where + F: FnMut(usize, usize, i32, i32), + { + let start_column = start_index / self.height; + let end_column = end_index / self.height; + for column in start_column..=end_column { + let column_x = (column >> 4) & 15; + let column_z = column & 15; + let column_base_index = column * self.height; + let bottom_y = + (start_index.saturating_sub(column_base_index) as i32).max(0) + self.min_y; + let top_y = + ((end_index - column_base_index) as i32).min(self.height as i32 - 1) + self.min_y; + visitor(column_x, column_z, bottom_y, top_y); + } + } +} + +impl CarverOutput for CarvingMask { + fn carve(&mut self, x: usize, y: i32, z: usize) { + self.set(x, y, z); + } + + fn min_y(&self) -> i32 { + self.min_y + } + + fn max_y(&self) -> i32 { + self.max_y + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn carving_mask_visit() { + let mut mask = CarvingMask::new(-64, 320); + assert!(mask.is_empty()); + + mask.set(0, 10, 0); + mask.set(0, 11, 0); + mask.set(0, 12, 0); + mask.set(1, 20, 2); + + let mut visits = Vec::new(); + mask.visit(|x, z, bottom_y, top_y| { + visits.push((x, z, bottom_y, top_y)); + }); + + assert_eq!(visits, vec![(0, 0, 10, 12), (1, 2, 20, 20)]); + } } diff --git a/crates/pumpkin-world/src/generation/carver/mod.rs b/crates/pumpkin-world/src/generation/carver/mod.rs index 16f22fa5b..ef43bab31 100644 --- a/crates/pumpkin-world/src/generation/carver/mod.rs +++ b/crates/pumpkin-world/src/generation/carver/mod.rs @@ -109,14 +109,93 @@ impl CarvingContext<'_> { } } +pub trait CarverOutput { + fn carve(&mut self, x: usize, y: i32, z: usize); + fn min_y(&self) -> i32; + fn max_y(&self) -> i32; +} + +#[must_use] +pub fn can_reach( + chunk_pos: &Vector2, + x: f64, + z: f64, + current_step: i32, + total_steps: i32, + thickness: f32, +) -> bool { + let x_mid = (chunk_pos.x << 4) as f64 + 8.0; + let z_mid = (chunk_pos.y << 4) as f64 + 8.0; + let xd = x - x_mid; + let zd = z - z_mid; + let remaining = (total_steps - current_step) as f64; + let rr = (thickness + 2.0 + 16.0) as f64; + (xd * xd + zd * zd) - (remaining * remaining) <= rr * rr +} + +#[allow(clippy::too_many_arguments)] +pub fn carve_ellipsoid( + chunk_pos: &Vector2, + x: f64, + y: f64, + z: f64, + horizontal_radius: f64, + vertical_radius: f64, + output: &mut O, + skip_checker: S, +) where + O: CarverOutput + ?Sized, + S: Fn(f64, f64, f64, i32) -> bool, +{ + let center_x = (chunk_pos.x << 4) as f64 + 8.0; + let center_z = (chunk_pos.y << 4) as f64 + 8.0; + let max_delta = 16.0 + horizontal_radius * 2.0; + + if (x - center_x).abs() > max_delta || (z - center_z).abs() > max_delta { + return; + } + + let chunk_min_x = chunk_pos.x << 4; + let chunk_min_z = chunk_pos.y << 4; + + let min_x_index = (((x - horizontal_radius).floor() as i32 - chunk_min_x) - 1).max(0) as usize; + let max_x_index = ((x + horizontal_radius).floor() as i32 - chunk_min_x).clamp(0, 15) as usize; + let min_y = ((y - vertical_radius).floor() as i32 - 1).max(output.min_y()); + let max_y = ((y + vertical_radius).floor() as i32 + 1).min(output.max_y()); + let min_z_index = (((z - horizontal_radius).floor() as i32 - chunk_min_z) - 1).max(0) as usize; + let max_z_index = ((z + horizontal_radius).floor() as i32 - chunk_min_z).clamp(0, 15) as usize; + + for x_index in min_x_index..=max_x_index { + let world_x = chunk_min_x + x_index as i32; + let xd = (world_x as f64 + 0.5 - x) / horizontal_radius; + + for z_index in min_z_index..=max_z_index { + let world_z = chunk_min_z + z_index as i32; + let zd = (world_z as f64 + 0.5 - z) / horizontal_radius; + + if xd * xd + zd * zd < 1.0 { + for world_y in (min_y + 1..=max_y).rev() { + let yd = (world_y as f64 - 0.5 - y) / vertical_radius; + if !skip_checker(xd, yd, zd, world_y) { + output.carve(x_index, world_y, z_index); + } + } + } + } + } +} + pub trait Carver { + #[allow(clippy::too_many_arguments)] fn carve( &self, config: &CarverConfig, - run: &mut CarveRun, + output: &mut dyn CarverOutput, random: &mut RandomGenerator, chunk_pos: &Vector2, carver_chunk_pos: &Vector2, + min_gen_y: i8, + gen_depth: u16, legacy_random_source: bool, ); } @@ -133,47 +212,12 @@ pub fn carve(chunk: &mut ProtoChunk, generator: &VanillaGenerator) { let mut multi_noise_sampler = MultiNoiseSampler::generate(&generator.base_router.multi_noise); let generation_shape = &generator.settings.shape; - let surface_config = SurfaceHeightSamplerBuilderOptions::new( - generation_shape.min_y as i32, - generation_shape.max_y() as i32, - generation_shape.vertical_cell_block_count() as usize, - ); - let surface_height_sampler = SurfaceHeightEstimateSampler::generate( - &generator.base_router.surface_estimator, - &surface_config, - ); - let carver_aquifer = generator.settings.aquifers_enabled.then(|| { - CarverAquiferSampler::new( - chunk_x, - chunk_z, - &generator.base_router, - &generator.random_config, - generator.settings, - ) - }); - - let mut context = CarvingContext { - min_y: generator.dimension.min_y as i8, - height: generator.dimension.logical_height as u16, - random_config: &generator.random_config, - surface_noise: &generator.terrain_cache.surface_noise, - secondary_noise: &generator.terrain_cache.secondary_noise, - terrain_builder: &generator.terrain_cache.terrain_builder, - sea_level: generator.settings.sea_level, - default_block: generator.settings.default_block, - default_fluid: generator.settings.default_fluid, - surface_rule: generator.surface_rule, - surface_height_sampler, - carver_aquifer, - }; - - let center_biome = chunk.get_biome(0, 0, 0); - - let mut run = CarveRun { - ctx: &mut context, - chunk, - ids: CarverBlockIds::new(), - }; + let min_gen_y = generation_shape.min_y; + let gen_depth = generation_shape.height; + let protected_blocks_on_top = 7; + let min_y = min_gen_y as i32 + 1; + let max_y = (min_gen_y as i32 + gen_depth as i32 - 1) - protected_blocks_on_top; + let mut mask = mask::CarvingMask::new(min_y, max_y); let cave_carver = cave::CaveCarver; let canyon_carver = canyon::CanyonCarver; @@ -185,7 +229,7 @@ pub fn carve(chunk: &mut ProtoChunk, generator: &VanillaGenerator) { let carver_chunk_pos = Vector2::new(carver_x, carver_z); let carver_biome = if dx == 0 && dz == 0 { - center_biome + chunk.get_biome(0, 0, 0) } else { supplier.biome( biome_coords::from_block(section_coords::section_to_block(carver_x)), @@ -209,20 +253,24 @@ pub fn carve(chunk: &mut ProtoChunk, generator: &VanillaGenerator) { CarverAdditionalConfig::Cave(_) => { cave_carver.carve( config, - &mut run, + &mut mask, &mut carver_random, &chunk_pos, &carver_chunk_pos, + min_gen_y, + gen_depth, generator.settings.legacy_random_source, ); } CarverAdditionalConfig::Canyon(_) => { canyon_carver.carve( config, - &mut run, + &mut mask, &mut carver_random, &chunk_pos, &carver_chunk_pos, + min_gen_y, + gen_depth, generator.settings.legacy_random_source, ); } @@ -231,6 +279,88 @@ pub fn carve(chunk: &mut ProtoChunk, generator: &VanillaGenerator) { } } } + + if !mask.is_empty() { + let surface_config = SurfaceHeightSamplerBuilderOptions::new( + generation_shape.min_y as i32, + generation_shape.max_y() as i32, + generation_shape.vertical_cell_block_count() as usize, + ); + let surface_height_sampler = SurfaceHeightEstimateSampler::generate( + &generator.base_router.surface_estimator, + &surface_config, + ); + let carver_aquifer = generator.settings.aquifers_enabled.then(|| { + CarverAquiferSampler::new( + chunk_x, + chunk_z, + &generator.base_router, + &generator.random_config, + generator.settings, + ) + }); + + let mut context = CarvingContext { + min_y: generator.dimension.min_y as i8, + height: generator.dimension.logical_height as u16, + random_config: &generator.random_config, + surface_noise: &generator.terrain_cache.surface_noise, + secondary_noise: &generator.terrain_cache.secondary_noise, + terrain_builder: &generator.terrain_cache.terrain_builder, + sea_level: generator.settings.sea_level, + default_block: generator.settings.default_block, + default_fluid: generator.settings.default_fluid, + surface_rule: generator.surface_rule, + surface_height_sampler, + carver_aquifer, + }; + + let mut run = CarveRun { + ctx: &mut context, + chunk, + ids: CarverBlockIds::new(), + }; + + apply_carving_mask(&mut run, &mask); + } +} + +fn apply_carving_mask(run: &mut CarveRun, mask: &mask::CarvingMask) { + let chunk_x = run.chunk.x; + let chunk_z = run.chunk.z; + let overworld = run.ctx.carver_aquifer.is_some(); + let bedrock_id = pumpkin_data::Block::BEDROCK.default_state.id; + let grass_block_id = pumpkin_data::Block::GRASS_BLOCK.default_state.id; + let mycelium_id = pumpkin_data::Block::MYCELIUM.default_state.id; + + mask.visit(|x, z, bottom_y, top_y| { + let mut has_grass = false; + let world_x = (chunk_x << 4) + x as i32; + let world_z = (chunk_z << 4) + z as i32; + for world_y in (bottom_y..=top_y).rev() { + let current_state_id = run + .chunk + .get_block_state(&Vector3::new(world_x, world_y, world_z)); + if current_state_id == bedrock_id { + continue; + } + if current_state_id == grass_block_id || current_state_id == mycelium_id { + has_grass = true; + } + if let Some((state, should_schedule_fluid_update)) = + overworld_carve_state(run, world_x, world_y, world_z) + { + place_carved_block( + run, + Vector3::new(world_x, world_y, world_z), + state, + should_schedule_fluid_update, + has_grass, + overworld, + ); + } + } + }); } fn should_carve(config: &CarverConfig, random: &mut RandomGenerator) -> bool { @@ -573,4 +703,28 @@ mod tests { assert!(Biome::THE_END.carvers.is_empty()); } + + #[test] + fn apply_carving_mask_skips_bedrock() { + with_carve_run(Dimension::OVERWORLD, |run| { + let bedrock = Block::BEDROCK.default_state; + let stone = Block::STONE.default_state; + run.chunk.set_block_state(5, 10, 5, bedrock); + run.chunk.set_block_state(5, 11, 5, stone); + + let mut mask = mask::CarvingMask::new(-64, 320); + mask.set(5, 10, 5); + mask.set(5, 11, 5); + + apply_carving_mask(run, &mask); + + // Bedrock must not be carved + assert_eq!( + run.chunk.get_block_state(&Vector3::new(5, 10, 5)), + bedrock.id + ); + // Stone must be carved + assert_ne!(run.chunk.get_block_state(&Vector3::new(5, 11, 5)), stone.id); + }); + } } diff --git a/crates/pumpkin-world/src/generation/noise/router/density_function/test.rs b/crates/pumpkin-world/src/generation/noise/router/density_function/test.rs index 17dc1314a..540143a90 100644 --- a/crates/pumpkin-world/src/generation/noise/router/density_function/test.rs +++ b/crates/pumpkin-world/src/generation/noise/router/density_function/test.rs @@ -841,7 +841,7 @@ fn normal_surface_noisified() { // } #[test] -fn test_round_function_parity() { +fn round_function_parity() { use super::math::round_to_integer; use pumpkin_data::noise_router::RoundingOperation; @@ -871,7 +871,7 @@ fn test_round_function_parity() { } #[test] -fn test_gradient_function_parity() { +fn gradient_function_parity() { use super::StaticIndependentChunkNoiseFunctionComponentImpl; use super::misc::Gradient; use pumpkin_data::noise_router::{Axis, GradientData, Tiling}; diff --git a/crates/pumpkin-world/src/generation/proto_chunk.rs b/crates/pumpkin-world/src/generation/proto_chunk.rs index 6b8ea270f..2639f0348 100644 --- a/crates/pumpkin-world/src/generation/proto_chunk.rs +++ b/crates/pumpkin-world/src/generation/proto_chunk.rs @@ -142,7 +142,6 @@ pub struct ProtoChunk { generation_bottom_y: i8, pub stage: StagedChunkEnum, pub light: ChunkLight, - pub carving_mask: crate::generation::carver::mask::CarvingMask, pub blending_data: Option, pub pending_block_entities: Vec, pending_structure_entities: Vec, @@ -252,10 +251,6 @@ impl ProtoChunk { .map(|_| LightContainer::new_empty(0)) .collect(), }, - carving_mask: crate::generation::carver::mask::CarvingMask::new( - height as i32, - bottom_y as i32, - ), blending_data: None, pending_block_entities: Vec::new(), pending_structure_entities: Vec::new(), diff --git a/tools/pumpkin-codegen/src/carver.rs b/tools/pumpkin-codegen/src/carver.rs index d407fb540..78e862269 100644 --- a/tools/pumpkin-codegen/src/carver.rs +++ b/tools/pumpkin-codegen/src/carver.rs @@ -77,7 +77,7 @@ pub fn build() -> TokenStream { let vertical_radius_center_factor = shape["vertical_radius_center_factor"] .as_f64() .unwrap_or(0.0) as f32; - let y_scale = shape["y_scale"].as_f64().unwrap_or(0.0) as f32; + let y_scale = value_to_float_provider(&shape["y_scale"]); quote! { CarverAdditionalConfig::Canyon(CanyonCarverConfig { @@ -180,7 +180,7 @@ pub fn build() -> TokenStream { pub horizontal_radius_factor: FloatProvider, pub vertical_radius_default_factor: f32, pub vertical_radius_center_factor: f32, - pub y_scale: f32, + pub y_scale: FloatProvider, } #[derive(Clone, Debug)]