diff --git a/src/world/doors.rs b/src/world/doors.rs index f2e4274..4575cc5 100644 --- a/src/world/doors.rs +++ b/src/world/doors.rs @@ -1,5 +1,3 @@ -use bevy::prelude::*; - use crate::{config::TERRAIN_COLS, world::rng::SplitMix64, world::rng::derive}; const SALT_DOOR: u64 = 1; diff --git a/src/world/generator.rs b/src/world/generator.rs index 960f4fd..e69de29 100644 --- a/src/world/generator.rs +++ b/src/world/generator.rs @@ -1,335 +0,0 @@ -use crate::{ - config::TERRAIN_COLS, - world::{ - doors::{Door, door}, - grid::GridRect, - rng::{SplitMix64, derive}, - validator::validate, - }, -}; - -const SALT_WALLS: u64 = 2; -const SALT_VARIANT: u64 = 3; - -const MIN_WALLS: i32 = 3; -const MAX_WALLS: i32 = 5; -const MAX_ATTEMPTS: i32 = 16; -const FIRST_WALL_ROW: i32 = 26; -const MIN_BOTTOM_ROW: i32 = 4; -const HOLE_MIN_WIDTH: i32 = 2; -const HOLE_MAX_WIDTH: i32 = 10; -const SECOND_HOLE_GAP: i32 = 2; -const THICK_HEIGHT: i32 = 2; - -const GAP_VARIANTS: u64 = 4; - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum GapVariant { - Classic, - Double, - Thick, - Staircase, -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct ChunkData { - pub rects: Vec, -} - -pub fn generate_chunk(seed: u64, depth: i64) -> ChunkData { - match try_generate(seed, depth) { - Some(chunk) => chunk, - None => fallback_chunk(seed, depth), - } -} - -pub fn try_generate(seed: u64, depth: i64) -> Option { - let bottom_door = door(seed, depth)?; - let top_door = door(seed, depth - 1)?; - let v = gap_variant(seed, depth); - - let mut rng = SplitMix64::new(derive(seed, depth, SALT_WALLS)); - - for _ in 0..MAX_ATTEMPTS { - let n_walls = MIN_WALLS + (rng.next_u64() % ((MAX_WALLS - MIN_WALLS + 1) as u64)) as i32; - let rects = build_gapwalls(v, top_door, bottom_door, n_walls, &mut rng); - if validate(&rects, bottom_door, top_door) { - return Some(ChunkData { rects }); - } - } - None -} - -fn gap_variant(seed: u64, depth: i64) -> GapVariant { - match derive(seed, depth, SALT_VARIANT) % GAP_VARIANTS { - 1 => GapVariant::Double, - 2 => GapVariant::Thick, - 3 => GapVariant::Staircase, - _ => GapVariant::Classic, - } -} - -fn build_gapwalls( - v: GapVariant, - top: Door, - bottom: Door, - n_walls: i32, - rng: &mut SplitMix64, -) -> Vec { - match v { - GapVariant::Classic => classic(top, bottom, n_walls, rng), - GapVariant::Double => double(top, bottom, n_walls, rng), - GapVariant::Thick => thick(top, bottom, n_walls, rng), - GapVariant::Staircase => staircase(top, bottom, n_walls), - } -} - -fn classic(top: Door, bottom: Door, n_walls: i32, rng: &mut SplitMix64) -> Vec { - let (t, b) = centers(top, bottom); - let mut rects = Vec::new(); - - for i in 0..n_walls { - let mut c = lerp(t, b, i, n_walls); - if i < n_walls - 1 { - c += jitter(rng); - } - let h = hole(c, width(bottom, n_walls, i)); - emit(&mut rects, wall_row(i, n_walls), 1, &[h]); - } - rects -} - -fn double(top: Door, bottom: Door, n_walls: i32, rng: &mut SplitMix64) -> Vec { - let (t, b) = centers(top, bottom); - let mut rects = Vec::new(); - - for i in 0..n_walls { - let mut c = lerp(t, b, i, n_walls); - if i < n_walls - 1 { - c += jitter(rng); - } - let main = hole(c, width(bottom, n_walls, i)); - - // Narrow risky hole, SECOND_HOLE_GAP columns of wall away - let side = (rng.next_u64() % 2) as i32 * 2 - 1; // -1 or +1 - let (sec_min, sec_max) = if side < 0 { - (main.0 - SECOND_HOLE_GAP - 1, main.0 - SECOND_HOLE_GAP) - } else { - (main.1 + SECOND_HOLE_GAP, main.1 + SECOND_HOLE_GAP + 1) - }; - - let mut holes = vec![main]; - if side < 0 { - if sec_min >= 0 { - holes.insert(0, (sec_min, sec_max)); - } - } else if sec_max < TERRAIN_COLS { - holes.push((sec_min, sec_max)); - } - - emit(&mut rects, wall_row(i, n_walls), 1, &holes); - } - rects -} - -fn thick(top: Door, bottom: Door, n_walls: i32, rng: &mut SplitMix64) -> Vec { - let (t, b) = centers(top, bottom); - let mut rects = Vec::new(); - - for i in 0..n_walls { - let mut c = lerp(t, b, i, n_walls); - if i < n_walls - 1 { - c += jitter(rng); - } - let h = hole(c, width(bottom, n_walls, i)); - emit(&mut rects, wall_row(i, n_walls), THICK_HEIGHT, &[h]); - } - rects -} - -fn staircase(top: Door, bottom: Door, n_walls: i32) -> Vec { - let (t, b) = centers(top, bottom); - - let step = ((b - t) / (n_walls - 1) as f32).round(); - let mut rects = Vec::new(); - - for i in 0..n_walls { - let c = t + step * i as f32; - let h = hole(c, width(bottom, n_walls, i)); - emit(&mut rects, wall_row(i, n_walls), 1, &[h]); - } - rects -} - -fn centers(top: Door, bottom: Door) -> (f32, f32) { - ( - (top.min_col + top.max_col) as f32 * 0.5, - (bottom.min_col + bottom.max_col) as f32 * 0.5, - ) -} - -fn lerp(t: f32, b: f32, i: i32, n: i32) -> f32 { - t + (b - t) * (i as f32 / (n - 1) as f32) -} - -fn jitter(rng: &mut SplitMix64) -> f32 { - (rng.next_u64() % 3) as f32 - 1.0 // -1..=1 column -} - -fn width(bottom: Door, n: i32, i: i32) -> i32 { - (bottom.width() + n - 1 - i).clamp(HOLE_MIN_WIDTH, HOLE_MAX_WIDTH) -} - -fn hole(center: f32, w: i32) -> (i32, i32) { - let min = ((center - w as f32 * 0.5).round() as i32).clamp(0, TERRAIN_COLS - w); - (min, min + w - 1) -} - -fn wall_spacing(n: i32) -> i32 { - (FIRST_WALL_ROW - MIN_BOTTOM_ROW) / (n - 1) -} - -fn wall_row(i: i32, n: i32) -> i32 { - FIRST_WALL_ROW - i * wall_spacing(n) -} - -fn emit(rects: &mut Vec, row: i32, height: i32, holes: &[(i32, i32)]) { - let mut cursor = 0; - - for &(h_min, h_max) in holes { - if h_max < 0 || h_min >= TERRAIN_COLS { - continue; - } - let h_min = h_min.max(0); - let h_max = h_max.min(TERRAIN_COLS - 1); - - if h_min > cursor { - rects.push(GridRect { - col: cursor, - row, - width: h_min - cursor, - height, - }); - } - cursor = cursor.max(h_max + 1); - } - - if cursor < TERRAIN_COLS { - rects.push(GridRect { - col: cursor, - row, - width: TERRAIN_COLS - cursor, - height, - }); - } -} - -fn fallback_chunk(seed: u64, depth: i64) -> ChunkData { - let Some(bottom_door) = door(seed, depth) else { - return ChunkData { rects: Vec::new() }; - }; - - let mut rects = Vec::new(); - emit( - &mut rects, - 8, - 1, - &[(bottom_door.min_col, bottom_door.max_col)], - ); - ChunkData { rects } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::config::CHUNK_ROWS; - - #[test] - fn fallback_rate_is_below_one_percent() { - const SEEDS: u64 = 10_000; - const CHUNKS_PER_SEED: i64 = 200; - - let mut fallbacks: u64 = 0; - let mut total: u64 = 0; - - for seed in 0..SEEDS { - for depth in 2..=2 + CHUNKS_PER_SEED { - if try_generate(seed, depth).is_none() { - fallbacks += 1; - } - total += 1; - } - } - - let rate = fallbacks as f64 / total as f64; - assert!( - rate < 0.01, - "fallback rate {:.2}% too high ({} fallbacks over {} chunks)", - rate * 100.0, - fallbacks, - total - ); - } - - #[test] - fn every_generated_chunk_is_playable() { - const SEEDS: u64 = 10_000; - const CHUNKS_PER_SEED: i64 = 200; - - for seed in 0..SEEDS { - for depth in 2..=2 + CHUNKS_PER_SEED { - let chunk = generate_chunk(seed, depth); - let bottom = door(seed, depth).unwrap(); - let top = door(seed, depth - 1).unwrap(); - assert!( - validate(&chunk.rects, bottom, top), - "seed {} depth {} is not playable", - seed, - depth - ); - } - } - } - - #[test] - fn generation_is_deterministic() { - for seed in 0..100 { - for depth in 2..=100 { - let a = generate_chunk(seed, depth); - let b = generate_chunk(seed, depth); - assert_eq!(a, b, "seed {} depth {} is not deterministic", seed, depth); - } - } - } - - #[test] - fn every_rect_stays_inside_the_chunk() { - for seed in 0..200 { - for depth in 2..=120 { - for r in &generate_chunk(seed, depth).rects { - assert!(r.width > 0 && r.height > 0, "empty rect {:?}", r); - assert!(r.col >= 0 && r.col + r.width <= TERRAIN_COLS, "col {:?}", r); - assert!(r.row >= 0 && r.row + r.height <= CHUNK_ROWS, "row {:?}", r); - } - } - } - } - - #[test] - fn every_variant_appears() { - let (mut classic, mut double, mut thick, mut stair) = (0, 0, 0, 0); - - for seed in 0..200u64 { - for depth in 2..=120i64 { - match gap_variant(seed, depth) { - GapVariant::Classic => classic += 1, - GapVariant::Double => double += 1, - GapVariant::Thick => thick += 1, - GapVariant::Staircase => stair += 1, - } - } - } - - assert!(classic > 0 && double > 0 && thick > 0 && stair > 0); - } -} diff --git a/src/world/obstacle.rs b/src/world/obstacle.rs index 1813e95..d8f8d10 100644 --- a/src/world/obstacle.rs +++ b/src/world/obstacle.rs @@ -10,7 +10,7 @@ use crate::{ }; // const OBSTACLE_COLOR: Color = Color::srgb(0.1, 0.7, 0.3); -const DEBUG_OBSTACLE_COLOR: Color = Color::srgba(0.0, 1.0, 0.0, 0.7); +const DEBUG_OBSTACLE_COLOR: Color = Color::srgba(0.0, 1.0, 0.0, 1.0); #[derive(Component)] pub struct Obstacle; diff --git a/src/world/validator.rs b/src/world/validator.rs index fe6a186..299c214 100644 --- a/src/world/validator.rs +++ b/src/world/validator.rs @@ -20,7 +20,6 @@ fn door_px(door: Door) -> (f32, f32) { fn free_intervals(rects: &[GridRect], row: i32) -> Vec<(f32, f32)> { let row_y0 = row as f32 * CELL_SIZE; let row_y1 = row_y0 + CELL_SIZE; - let mut blocked: Vec<(f32, f32)> = Vec::new(); for r in rects { let y0 = r.row as f32 * CELL_SIZE - INFLATE_Y;