From d65a168b9cd07b993725745c3759d1bbab6d0937 Mon Sep 17 00:00:00 2001 From: Ezra Barrow Date: Fri, 13 May 2022 18:10:52 -0400 Subject: [PATCH 1/2] Modify seeding to be reproducible and portable --- routes-native/Cargo.toml | 3 ++- routes-native/src/stateserver/world/gen.rs | 17 +++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/routes-native/Cargo.toml b/routes-native/Cargo.toml index 0f85a09..b299a2e 100644 --- a/routes-native/Cargo.toml +++ b/routes-native/Cargo.toml @@ -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" diff --git a/routes-native/src/stateserver/world/gen.rs b/routes-native/src/stateserver/world/gen.rs index 4c086a7..db0dd28 100644 --- a/routes-native/src/stateserver/world/gen.rs +++ b/routes-native/src/stateserver/world/gen.rs @@ -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::() 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() From 397168dfc20609051a6551afd5acc362c774bf44 Mon Sep 17 00:00:00 2001 From: Ezra Barrow Date: Fri, 13 May 2022 18:12:15 -0400 Subject: [PATCH 2/2] cargo fmt'd ur use statement x3 --- routes-native/src/stateserver/world/gen.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/routes-native/src/stateserver/world/gen.rs b/routes-native/src/stateserver/world/gen.rs index db0dd28..ad1e0a6 100644 --- a/routes-native/src/stateserver/world/gen.rs +++ b/routes-native/src/stateserver/world/gen.rs @@ -1,4 +1,7 @@ -use noise::{Seedable, utils::{PlaneMapBuilder, NoiseMapBuilder, NoiseMap}, SuperSimplex}; +use noise::{ + utils::{NoiseMap, NoiseMapBuilder, PlaneMapBuilder}, + Seedable, SuperSimplex, +}; use rand::{Rng, SeedableRng}; pub type GameRng = rand_xoshiro::Xoshiro256PlusPlus;