veloren/world/src/util/random.rs

63 lines
1.2 KiB
Rust
Raw Normal View History

2019-06-15 10:36:26 +00:00
use super::Sampler;
use vek::*;
pub struct RandomField {
seed: u32,
}
impl RandomField {
pub const fn new(seed: u32) -> Self {
2019-06-15 10:36:26 +00:00
Self { seed }
}
}
impl Sampler for RandomField {
type Index = Vec3<i32>;
type Sample = u32;
fn get(&self, pos: Self::Index) -> Self::Sample {
let pos = pos.map(|e| (e * 13 + (1 << 31)) as u32);
let mut a = self.seed;
a = (a ^ 61) ^ (a >> 16);
a = a + (a << 3);
a = a ^ pos.x;
a = a ^ (a >> 4);
a = a * 0x27d4eb2d;
a = a ^ (a >> 15);
a = a ^ pos.y;
a = (a ^ 61) ^ (a >> 16);
a = a + (a << 3);
a = a ^ (a >> 4);
a = a ^ pos.z;
a = a * 0x27d4eb2d;
a = a ^ (a >> 15);
a
2019-06-15 10:36:26 +00:00
}
}
pub struct RandomPerm {
seed: u32,
}
impl RandomPerm {
pub const fn new(seed: u32) -> Self {
Self { seed }
}
}
impl Sampler for RandomPerm {
type Index = u32;
type Sample = u32;
fn get(&self, perm: Self::Index) -> Self::Sample {
let mut a = perm;
a = (a ^ 61) ^ (a >> 16);
a = a + (a << 3);
a = a ^ (a >> 4);
a = a * 0x27d4eb2d;
a = a ^ (a >> 15);
a
}
}