diff --git a/crates/horizon-core/src/random.rs b/crates/horizon-core/src/random.rs index 309d48124..46397b5c4 100644 --- a/crates/horizon-core/src/random.rs +++ b/crates/horizon-core/src/random.rs @@ -29,6 +29,9 @@ pub trait RandomSource: Sized { fn next_boolean(&mut self) -> bool; fn next_float(&mut self) -> f32; fn next_double(&mut self) -> f64; + /// MarsagliaPolarGaussian over this source; the cached second value is + /// part of the source state (reset by `set_seed` where vanilla resets it). + fn next_gaussian(&mut self) -> f64; fn fork_positional(&mut self) -> Self::Positional; fn fork(&mut self) -> Self where @@ -61,6 +64,7 @@ pub trait RandomSource: Sized { #[derive(Clone, Debug)] pub struct LegacyRandom { state: u64, + gaussian: Option, } impl LegacyRandom { @@ -70,6 +74,7 @@ impl LegacyRandom { pub fn new(seed: i64) -> Self { Self { state: (seed as u64 ^ Self::MULTIPLIER) & Self::MASK, + gaussian: None, } } @@ -125,6 +130,12 @@ impl RandomSource for LegacyRandom { let value = (i64::from(self.bits(26)) << 27) + i64::from(self.bits(27)); value as f64 * (1.0 / 9_007_199_254_740_992.0) } + fn next_gaussian(&mut self) -> f64 { + let mut cache = self.gaussian.take(); + let value = marsaglia_polar(&mut cache, || self.next_double()); + self.gaussian = cache; + value + } fn fork(&mut self) -> Self { Self::new(self.next_long()) @@ -135,6 +146,7 @@ impl RandomSource for LegacyRandom { pub struct XoroshiroRandom { lo: u64, hi: u64, + gaussian: Option, } impl XoroshiroRandom { @@ -154,9 +166,14 @@ impl XoroshiroRandom { Self { lo: Self::GOLDEN_RATIO, hi: Self::SILVER_RATIO, + gaussian: None, } } else { - Self { lo, hi } + Self { + lo, + hi, + gaussian: None, + } } } } @@ -207,6 +224,12 @@ impl RandomSource for XoroshiroRandom { fn next_double(&mut self) -> f64 { ((self.next_long() as u64) >> 11) as f64 * (1.0 / 9_007_199_254_740_992.0) } + fn next_gaussian(&mut self) -> f64 { + let mut cache = self.gaussian.take(); + let value = marsaglia_polar(&mut cache, || self.next_double()); + self.gaussian = cache; + value + } fn fork(&mut self) -> Self { let lo = self.next_long() as u64; let hi = self.next_long() as u64; @@ -214,6 +237,24 @@ impl RandomSource for XoroshiroRandom { } } +/// MarsagliaPolarGaussian.nextGaussian with an external second-value cache. +pub fn marsaglia_polar(cache: &mut Option, mut next_double: impl FnMut() -> f64) -> f64 { + if let Some(value) = cache.take() { + return value; + } + loop { + let x = 2.0 * next_double() - 1.0; + let y = 2.0 * next_double() - 1.0; + let radius = x * x + y * y; + if radius >= 1.0 || radius == 0.0 { + continue; + } + let multiplier = (-2.0 * radius.ln() / radius).sqrt(); + *cache = Some(y * multiplier); + return x * multiplier; + } +} + pub const fn mix_stafford13(mut value: u64) -> u64 { value = (value ^ (value >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9); value = (value ^ (value >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb); diff --git a/crates/horizon-core/src/random/worldgen.rs b/crates/horizon-core/src/random/worldgen.rs index 70c88b0e2..aa16ab249 100644 --- a/crates/horizon-core/src/random/worldgen.rs +++ b/crates/horizon-core/src/random/worldgen.rs @@ -28,6 +28,8 @@ pub struct WorldgenRandom { source: R, count: i32, wrapped: bool, + /// The adapter's own Gaussian cache; native setSeed does not reset it. + gaussian: Option, } impl WorldgenRandom { pub fn new(source: R) -> Self { @@ -35,6 +37,7 @@ impl WorldgenRandom { source, count: 0, wrapped: true, + gaussian: None, } } fn plain(source: R) -> Self { @@ -42,6 +45,7 @@ impl WorldgenRandom { source, count: 0, wrapped: false, + gaussian: None, } } pub fn count(&self) -> i32 { @@ -150,6 +154,15 @@ impl RandomSource for WorldgenRandom { let bits = ((self.next_bits(26) as i64) << 27) + self.next_bits(27) as i64; bits as f64 * (1.0 / 9007199254740992.0) } + fn next_gaussian(&mut self) -> f64 { + if !self.wrapped { + return self.source.next_gaussian(); + } + let mut cache = self.gaussian.take(); + let value = super::marsaglia_polar(&mut cache, || self.next_double()); + self.gaussian = cache; + value + } /// Native forks return the underlying source, not another bit adapter. /// The returned value keeps that behavior through a transparent variant. fn fork(&mut self) -> Self { diff --git a/crates/horizon-world/src/worldgen/value_provider.rs b/crates/horizon-world/src/worldgen/value_provider.rs index 27bc8d4ae..442b1cdb5 100644 --- a/crates/horizon-world/src/worldgen/value_provider.rs +++ b/crates/horizon-world/src/worldgen/value_provider.rs @@ -10,6 +10,12 @@ pub enum FloatProvider { Constant(f32), Uniform { min: f32, max: f32 }, Trapezoid { min: f32, max: f32, plateau: f32 }, + ClampedNormal { + mean: f32, + deviation: f32, + min: f32, + max: f32, + }, } impl FloatProvider { pub fn from_json(json: &str) -> Result { @@ -38,13 +44,27 @@ impl FloatProvider { } Ok(Self::Trapezoid { min, max, plateau }) } + "minecraft:clamped_normal" => { + let (min, max) = (field("min")?, field("max")?); + if max < min { + return Err(DecodeError::Field("clamped normal float bounds")); + } + Ok(Self::ClampedNormal { + mean: field("mean")?, + deviation: field("deviation")?, + min, + max, + }) + } _ => Err(DecodeError::Field("float provider type")), } } pub fn bounds(&self) -> [f32; 2] { match *self { Self::Constant(value) => [value; 2], - Self::Uniform { min, max } | Self::Trapezoid { min, max, .. } => [min, max], + Self::Uniform { min, max } + | Self::Trapezoid { min, max, .. } + | Self::ClampedNormal { min, max, .. } => [min, max], } } pub fn sample(&self, random: &mut impl RandomSource) -> f32 { @@ -57,6 +77,15 @@ impl FloatProvider { let middle = range - slope; (min + random.next_float() * middle) + random.next_float() * slope } + Self::ClampedNormal { + mean, + deviation, + min, + max, + } => { + let value = mean + random.next_gaussian() as f32 * deviation; + if value < min { min } else { value.min(max) } + } } } }