From 2786e28577cb60782296b0636b7c4519aa3c75a9 Mon Sep 17 00:00:00 2001 From: Acrimon Date: Tue, 17 Sep 2019 16:56:23 +0200 Subject: [PATCH] Fixed rng stuff. --- world/src/util/hash_cache.rs | 2 +- world/src/util/random.rs | 28 +++------------------------- world/src/util/seed_expan.rs | 11 ++++++++++- 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/world/src/util/hash_cache.rs b/world/src/util/hash_cache.rs index 2abe5c8cd7..9e46ebe807 100644 --- a/world/src/util/hash_cache.rs +++ b/world/src/util/hash_cache.rs @@ -17,7 +17,7 @@ impl HashCache { pub fn with_capacity(capacity: usize) -> Self { Self { capacity, - map: HashMap::default(), + map: HashMap::with_capacity(1024), counter: 0, } } diff --git a/world/src/util/random.rs b/world/src/util/random.rs index da56a76ee2..3192138063 100644 --- a/world/src/util/random.rs +++ b/world/src/util/random.rs @@ -1,5 +1,6 @@ use super::Sampler; use vek::*; +use super::seed_expan; pub struct RandomField { seed: u32, @@ -17,22 +18,7 @@ impl Sampler<'static> for RandomField { fn get(&self, pos: Self::Index) -> Self::Sample { let pos = pos.map(|e| u32::from_le_bytes(e.to_le_bytes())); - - let mut a = self.seed; - a = (a ^ 61) ^ (a >> 16); - a = a.wrapping_add(a << 3); - a = a ^ pos.x; - a = a ^ (a >> 4); - a = a.wrapping_mul(0x27d4eb2d); - a = a ^ (a >> 15); - a = a ^ pos.y; - a = (a ^ 61) ^ (a >> 16); - a = a.wrapping_add(a << 3); - a = a ^ (a >> 4); - a = a ^ pos.z; - a = a.wrapping_mul(0x27d4eb2d); - a = a ^ (a >> 15); - a + seed_expan::diffuse_mult(&[self.seed, pos.x, pos.y, pos.z]) } } @@ -51,14 +37,6 @@ impl Sampler<'static> for RandomPerm { type Sample = u32; fn get(&self, perm: Self::Index) -> Self::Sample { - let a = self - .seed - .wrapping_mul(3471) - .wrapping_add(perm) - .wrapping_add(0x3BE7172B) - .wrapping_mul(perm) - .wrapping_add(0x172A3BE1); - let b = a.wrapping_mul(a); - b ^ (a >> 17) ^ b >> 15 + seed_expan::diffuse_mult(&[self.seed, perm]) } } diff --git a/world/src/util/seed_expan.rs b/world/src/util/seed_expan.rs index ceee6acd56..30338eca64 100644 --- a/world/src/util/seed_expan.rs +++ b/world/src/util/seed_expan.rs @@ -20,10 +20,19 @@ pub fn diffuse(mut x: u32) -> u32 { x = x.wrapping_add(0xd3a2646c) ^ (x << 9); x = x.wrapping_add(0xfd7046c5).wrapping_add(x << 3); x = (x ^ 0xb55a4f09) ^ (x >> 16); - x = x.wrapping_add((1 << 31) - 1) ^ (x << 13); + x = x.wrapping_add((1 << 31) - 13) ^ (x << 13); x } +/// Diffuse but takes multiple values as input. +pub fn diffuse_mult(v: &[u32]) -> u32 { + let mut state = (1 << 31) - 1; + for e in v { + state = diffuse(state ^ e); + } + state +} + /// Expand a 32 bit seed into a 32 byte RNG state. pub fn rng_state(mut x: u32) -> [u8; 32] { let mut r: [u32; 8] = [0; 8];