Merge pull request 'modify seeding 2 b portable and reproducible' (#1) from barrow/codename-routes:main into main

Reviewed-on: https://git.kittycat.homes/zoe/codename-routes/pulls/1
This commit is contained in:
zoe 2022-05-14 00:22:36 +02:00
commit 24a9b6a07e
2 changed files with 17 additions and 8 deletions

View file

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

View file

@ -1,13 +1,21 @@
use rand::{prelude::StdRng, Rng}; use noise::{
use noise::{Seedable, utils::{PlaneMapBuilder, NoiseMapBuilder, NoiseMap}, SuperSimplex}; utils::{NoiseMap, NoiseMapBuilder, PlaneMapBuilder},
use rand_seeder::Seeder; Seedable, SuperSimplex,
};
use rand::{Rng, SeedableRng};
pub fn get_rng(seed: String) -> StdRng{ pub type GameRng = rand_xoshiro::Xoshiro256PlusPlus;
Seeder::from(seed).make_rng()
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{ pub fn get_noise(rng: &mut GameRng, size: (usize, usize)) -> NoiseMap {
let noise = SuperSimplex::new().set_seed(rng.gen_range(0..u32::MAX)); // 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) PlaneMapBuilder::new(&noise)
.set_size(size.0, size.1) .set_size(size.0, size.1)
.build() .build()