From f0692c491b49cf4375160ae8c74b9764cfa10cf2 Mon Sep 17 00:00:00 2001 From: juliancoffee Date: Sun, 23 May 2021 17:24:22 +0300 Subject: [PATCH] Implement random choosing of item from loadout --- common/src/comp/inventory/loadout_builder.rs | 31 ++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/common/src/comp/inventory/loadout_builder.rs b/common/src/comp/inventory/loadout_builder.rs index 9936066170..c5d6c33eb8 100644 --- a/common/src/comp/inventory/loadout_builder.rs +++ b/common/src/comp/inventory/loadout_builder.rs @@ -13,7 +13,7 @@ use crate::{ trade::{Good, SiteInformation}, }; use hashbrown::HashMap; -use rand::Rng; +use rand::{seq::SliceRandom, Rng}; use serde::{Deserialize, Serialize}; use strum_macros::EnumIter; use tracing::warn; @@ -1037,7 +1037,34 @@ impl LoadoutBuilder { for (key, specifier) in spec { let item = match specifier { ItemSpec::Item(specifier) => Item::new_from_asset_expect(&specifier), - ItemSpec::Choice(_) => Item::empty(), + ItemSpec::Choice(items) => { + let mut rng = rand::thread_rng(); + match items + .choose_weighted(&mut rng, |item| item.0) + .unwrap_or_else(|| { + panic!( + "failed to choose item from loadout asset ({})", + asset_specifier + ) + }) { + (_, Some(ItemSpec::Item(item_specifier))) => { + Item::new_from_asset_expect(&item_specifier) + }, + (_, Some(ItemSpec::Choice(_))) => { + let err = format!( + "Using choice of choices in ({}): {}. Unimplemented.", + asset_specifier, key, + ); + if cfg!(tests) { + panic!("{}", err); + } else { + warn!("{}", err); + } + continue; + }, + (_, None) => continue, + } + }, }; match key.as_str() { "active_mainhand" => {