Modify seeding to be reproducible and portable

This commit is contained in:
Ezra Barrow 2022-05-13 18:10:52 -04:00
parent 929e296fb7
commit d65a168b9c
No known key found for this signature in database
GPG key ID: 5EF8BA3CE9180419
2 changed files with 13 additions and 7 deletions

View file

@ -13,8 +13,9 @@ gdnative = {version = "0.10", features = ["async"]}
strum = { version = "0.24", features = ["derive"] }
strum_macros = "0.24"
derive_builder = "0.11.2"
rand_seeder = "0.2.3"
rand = "0.8.5"
rand_xoshiro = "0.6"
blake3 = "1" # 1.3.1 at time of addition
toml = "0.5.9"
noise = "0.7.0"

View file

@ -1,13 +1,18 @@
use rand::{prelude::StdRng, Rng};
use noise::{Seedable, utils::{PlaneMapBuilder, NoiseMapBuilder, NoiseMap}, SuperSimplex};
use rand_seeder::Seeder;
use rand::{Rng, SeedableRng};
pub fn get_rng(seed: String) -> StdRng{
Seeder::from(seed).make_rng()
pub type GameRng = rand_xoshiro::Xoshiro256PlusPlus;
pub fn get_rng(seed: impl AsRef<[u8]> /* implemented by String */) -> GameRng {
// blake3::Hash is a [u8; 32] under the hood and implements From and Into to convert to and from it
let hash: [u8; 32] = blake3::hash(seed.as_ref()).into();
// Xoshiro256++ seeds are [u8; 32] :3
GameRng::from_seed(hash)
}
pub fn get_noise(rng: &mut StdRng, size: (usize, usize)) -> NoiseMap{
let noise = SuperSimplex::new().set_seed(rng.gen_range(0..u32::MAX));
pub fn get_noise(rng: &mut GameRng, size: (usize, usize)) -> NoiseMap {
// rng.gen::<u32>() generates a random u32 which is already between 0 and u32::MAX
let noise = SuperSimplex::new().set_seed(rng.gen());
PlaneMapBuilder::new(&noise)
.set_size(size.0, size.1)
.build()