From a56675345b2bc0a012174af61326aa1969c9b06c Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Tue, 27 Jun 2023 18:14:17 -0400 Subject: [PATCH] Properly seed organism stat randomization --- emergence_lib/src/crafting/inventories.rs | 8 ++++---- emergence_lib/src/items/slot.rs | 4 ++-- emergence_lib/src/world_gen/unit_generation.rs | 13 ++++++------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/emergence_lib/src/crafting/inventories.rs b/emergence_lib/src/crafting/inventories.rs index 812323375..5059f8034 100644 --- a/emergence_lib/src/crafting/inventories.rs +++ b/emergence_lib/src/crafting/inventories.rs @@ -19,7 +19,7 @@ use crate::{ use std::{fmt::Display, time::Duration}; use bevy::prelude::*; -use rand::{distributions::Uniform, prelude::Distribution, rngs::ThreadRng}; +use rand::{distributions::Uniform, prelude::Distribution, rngs::ThreadRng, Rng}; use serde::{Deserialize, Serialize}; /// The current state in the crafting progress. @@ -49,7 +49,7 @@ impl CraftingState { /// Generates a random crafting state. /// /// This will always be `InProgress` with a random progress value. - pub(crate) fn randomize(&mut self, rng: &mut ThreadRng, recipe_data: &RecipeData) { + pub(crate) fn randomize(&mut self, rng: &mut impl Rng, recipe_data: &RecipeData) { let distribution = Uniform::new(Duration::ZERO, recipe_data.craft_time); let progress = distribution.sample(rng); *self = CraftingState::InProgress { @@ -242,7 +242,7 @@ impl InputInventory { /// Randomizes the contents of this inventory so that each slot is somewhere between empty and full. /// /// Note that this only works for [`InputInventory::Exact`]. - pub(crate) fn randomize(&mut self, rng: &mut ThreadRng) { + pub(crate) fn randomize(&mut self, rng: &mut impl Rng) { if let InputInventory::Exact { inventory } = self { for item_slot in inventory.iter_mut() { item_slot.randomize(rng); @@ -296,7 +296,7 @@ impl OutputInventory { }; /// Randomizes the contents of this inventory so that each slot is somewhere between empty and full. - pub(crate) fn randomize(&mut self, rng: &mut ThreadRng) { + pub(crate) fn randomize(&mut self, rng: &mut impl Rng) { for item_slot in self.iter_mut() { item_slot.randomize(rng); } diff --git a/emergence_lib/src/items/slot.rs b/emergence_lib/src/items/slot.rs index 652dfedab..e9f6edd29 100644 --- a/emergence_lib/src/items/slot.rs +++ b/emergence_lib/src/items/slot.rs @@ -1,6 +1,6 @@ //! A container for a single item type, with a capacity. -use rand::{distributions::Uniform, prelude::Distribution, rngs::ThreadRng}; +use rand::{distributions::Uniform, prelude::Distribution, Rng}; use serde::{Deserialize, Serialize}; use crate::asset_management::manifest::Id; @@ -184,7 +184,7 @@ impl ItemSlot { /// Randomizes the quantity of items in this slot, return `self`. /// /// The new value will be chosen uniformly between 0 and `max_item_count`. - pub fn randomize(&mut self, rng: &mut ThreadRng) { + pub fn randomize(&mut self, rng: &mut impl Rng) { let distribution = Uniform::new(0, self.max_item_count); self.count = distribution.sample(rng); } diff --git a/emergence_lib/src/world_gen/unit_generation.rs b/emergence_lib/src/world_gen/unit_generation.rs index cae2539c8..ca18a839c 100644 --- a/emergence_lib/src/world_gen/unit_generation.rs +++ b/emergence_lib/src/world_gen/unit_generation.rs @@ -12,7 +12,7 @@ use crate::units::unit_manifest::UnitManifest; use crate::units::UnitBundle; use bevy::prelude::*; -use rand::{thread_rng, Rng}; +use rand::Rng; use super::GenerationConfig; @@ -63,25 +63,24 @@ pub(super) fn randomize_starting_organisms( mut output_inventory_query: Query<&mut OutputInventory>, mut crafting_state_query: Query<(&mut CraftingState, &ActiveRecipe)>, recipe_manifest: Res, + mut rng: ResMut, ) { - let rng = &mut thread_rng(); - for mut energy_pool in energy_pool_query.iter_mut() { - energy_pool.randomize(rng) + energy_pool.randomize(rng.get_mut()) } for mut input_inventory in input_inventory_query.iter_mut() { - input_inventory.randomize(rng) + input_inventory.randomize(rng.get_mut()) } for mut output_inventory in output_inventory_query.iter_mut() { - output_inventory.randomize(rng) + output_inventory.randomize(rng.get_mut()) } for (mut crafting_state, active_recipe) in crafting_state_query.iter_mut() { if let Some(recipe_id) = active_recipe.recipe_id() { let recipe_data = recipe_manifest.get(*recipe_id); - crafting_state.randomize(rng, recipe_data); + crafting_state.randomize(rng.get_mut(), recipe_data); } } }