veloren/world/src/util/random.rs

38 lines
760 B
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 fn new(seed: u32) -> Self {
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
}
}