veloren/world/src/util/random.rs
2019-08-31 11:44:52 +01:00

63 lines
1.2 KiB
Rust

use super::Sampler;
use vek::*;
pub struct RandomField {
seed: u32,
}
impl RandomField {
pub const fn new(seed: u32) -> Self {
Self { seed }
}
}
impl Sampler<'static> 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
}
}
pub struct RandomPerm {
seed: u32,
}
impl RandomPerm {
pub const fn new(seed: u32) -> Self {
Self { seed }
}
}
impl Sampler<'static> 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
}
}