Skip to content

Conversation

@cb341
Copy link
Owner

@cb341 cb341 commented Feb 1, 2026

World generation should be deterministic.
This should allow us to take some shortcuts further down the line.

Now chunks use a thread local RNG, instead of a global one.

There was the idea of adding a check that hashing yields unique seeds, however this was deemed to expensive and unnecessary, as I am delegating the hashing work to std.

There was the idea of creating my own implementation but I didn't figure out how properly hash coordinates such that the hash is unique within the testing bounds.

#[cfg(test)]
mod tests {
    use std::{collections::HashSet, hash::DefaultHasher};

    use super::*;

    #[test]
    fn test_rng_seed_uniqueness() {
        let mut seeds: HashSet<u64> = HashSet::new();
        let mut count = 0;
        for x in -100..100 {
            for y in -100..100 {
                for z in -100..100 {
                    let seed = HashablePos(IVec3::new(x, y, z)).seed();
                    assert!(!seeds.contains(&seed), "{}",count);
                    seeds.insert(seed);
                    count = count + 1;
                }
            }
        }
    }
}
    fn seed(&self) -> u64 {
        fn mul_31(number: u64) -> u64 {
            let (n,_b) = number.overflowing_mul(31);
            n
        }

        fn mul_67(number: u64) -> u64 {
            let (n,_b) = number.overflowing_mul(67);
            n
        }

        fn add(a: u64, b:u64) -> u64 {
            let (n,_b) = a.overflowing_add(b);
            n
        }

        let mut result: u64 = 17;
        result = add(mul_31(result) , self.x as u64);
        result = add(mul_67(result) , self.y as u64);
        result = add(mul_31(result) , self.z as u64);
        result = add(mul_31(result), 69);
        result
    }

@cb341 cb341 self-assigned this Feb 1, 2026
@cb341 cb341 marked this pull request as ready for review February 1, 2026 19:30
@cb341 cb341 changed the title Feature/deterministic world generation Deterministic World Generation Feb 1, 2026
@cb341 cb341 merged commit 3b292e4 into main Feb 1, 2026
3 checks passed
@cb341 cb341 deleted the feature/deterministic-world-generation branch February 1, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant