mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Fixed rng stuff.
This commit is contained in:
parent
5487c8b7bc
commit
2786e28577
@ -17,7 +17,7 @@ impl<K: Hash + Eq + Clone, V> HashCache<K, V> {
|
||||
pub fn with_capacity(capacity: usize) -> Self {
|
||||
Self {
|
||||
capacity,
|
||||
map: HashMap::default(),
|
||||
map: HashMap::with_capacity(1024),
|
||||
counter: 0,
|
||||
}
|
||||
}
|
||||
|
@ -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])
|
||||
}
|
||||
}
|
||||
|
@ -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];
|
||||
|
Loading…
Reference in New Issue
Block a user