rng + doors

This commit is contained in:
2026-09-19 17:00:01 +02:00
parent f911cd3300
commit fb45de8223
3 changed files with 85 additions and 0 deletions
+2
View File
@@ -7,8 +7,10 @@ use crate::{
}; };
pub mod background; pub mod background;
pub mod doors;
pub mod grid; pub mod grid;
pub mod obstacle; pub mod obstacle;
pub mod rng;
#[derive(Component)] #[derive(Component)]
pub struct Chunk { pub struct Chunk {
+51
View File
@@ -0,0 +1,51 @@
use bevy::prelude::*;
use crate::{config::TERRAIN_COLS, world::rng::SplitMix64, world::rng::derive};
const SALT_DOOR: u64 = 1;
const DOOR_HALF_WIDTH: i32 = 6;
const DOOR_MIN_WIDTH: i32 = 2;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Door {
pub min_col: i32,
pub max_col: i32,
}
impl Door {
pub fn width(&self) -> i32 {
self.max_col - self.min_col + 1
}
}
pub fn door(seed: u64, k: i64) -> Option<Door> {
if k < 0 {
return None;
}
// Chunk 0
if k == 0 {
let center = TERRAIN_COLS / 2;
return Some(Door {
min_col: center - DOOR_HALF_WIDTH,
max_col: center + DOOR_HALF_WIDTH - 1,
});
}
let mut rng = SplitMix64::new(derive(seed, k, SALT_DOOR));
// Random width
let width = DOOR_MIN_WIDTH
+ (rng.next_u64() % (2 * DOOR_HALF_WIDTH as u64 - DOOR_MIN_WIDTH as u64 + 1)) as i32;
// DOor stay in terrain
let center =
DOOR_HALF_WIDTH + (rng.next_u64() % (TERRAIN_COLS - 2 * DOOR_HALF_WIDTH) as u64) as i32;
let min_col = center - width / 2;
Some(Door {
min_col,
max_col: min_col + width - 1,
})
}
+32
View File
@@ -0,0 +1,32 @@
// Pseudo random générator SplitMix64
pub const GENERATOR_VERSION: u32 = 1;
const MIX_GAMMA: u64 = 0x9E37_79B9_7F4A_7C15;
const MIX_1: u64 = 0xBF58_476D_1CE4_E5B9;
const MIX_2: u64 = 0x94D0_49BB_1331_11EB;
fn splitmix64_mix(mut z: u64) -> u64 {
z = (z ^ (z >> 30)).wrapping_mul(MIX_1);
z = (z ^ (z >> 27)).wrapping_mul(MIX_2);
z ^ (z >> 31)
}
pub struct SplitMix64 {
state: u64,
}
impl SplitMix64 {
pub fn new(seed: u64) -> Self {
Self { state: seed }
}
pub fn next_u64(&mut self) -> u64 {
self.state = self.state.wrapping_add(MIX_GAMMA);
splitmix64_mix(self.state)
}
}
pub fn derive(seed: u64, index: i64, salt: u64) -> u64 {
splitmix64_mix(seed ^ (index as u64).wrapping_mul(MIX_GAMMA) ^ salt.rotate_left(32))
}