Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
Properly seed organism stat randomization
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-i-cecile committed Jun 27, 2023
1 parent b54b4e7 commit a566753
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
8 changes: 4 additions & 4 deletions emergence_lib/src/crafting/inventories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions emergence_lib/src/items/slot.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
13 changes: 6 additions & 7 deletions emergence_lib/src/world_gen/unit_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<RecipeManifest>,
mut rng: ResMut<GlobalRng>,
) {
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);
}
}
}

0 comments on commit a566753

Please sign in to comment.