From acba98366a615353a01fae875c41180fc5452674 Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Wed, 24 Mar 2021 00:15:55 +0100 Subject: [PATCH] loop over slots_mut instead of using an index --- common/src/comp/inventory/loadout_builder.rs | 50 ++++++++++---------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/common/src/comp/inventory/loadout_builder.rs b/common/src/comp/inventory/loadout_builder.rs index e4040e079b..9b91368886 100644 --- a/common/src/comp/inventory/loadout_builder.rs +++ b/common/src/comp/inventory/loadout_builder.rs @@ -530,28 +530,30 @@ impl LoadoutBuilder { Merchant => { let mut backpack = Item::new_from_asset_expect("common.items.armor.misc.back.backpack"); - let mut coins = Item::new_from_asset_expect("common.items.utility.coins"); - coins - .set_amount( - (economy - .map(|e| e.unconsumed_stock.get(&Good::Coin)) - .flatten() - .copied() - .unwrap_or_default() - .round() as u32) - .max(1), - ) - .expect("coins should be stackable"); - backpack.slots_mut()[0] = Some(coins); + let mut coins = economy + .map(|e| e.unconsumed_stock.get(&Good::Coin)) + .flatten() + .copied() + .unwrap_or_default() + .round() as u32; let armor = economy .map(|e| e.unconsumed_stock.get(&Good::Armor)) .flatten() .copied() .unwrap_or_default() / 10.0; - for i in 1..18 { - if let Some(item_id) = TradePricing::random_item(Good::Armor, armor) { - backpack.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); + for s in backpack.slots_mut() { + if coins > 0 { + let mut coin_item = + Item::new_from_asset_expect("common.items.utility.coins"); + coin_item + .set_amount(coins) + .expect("coins should be stackable"); + *s = Some(coin_item); + coins = 0; + } else if let Some(item_id) = TradePricing::random_item(Good::Armor, armor) + { + *s = Some(Item::new_from_asset_expect(&item_id)); } } let mut bag1 = Item::new_from_asset_expect( @@ -563,9 +565,9 @@ impl LoadoutBuilder { .copied() .unwrap_or_default() / 10.0; - for i in 0..16 { + for i in bag1.slots_mut() { if let Some(item_id) = TradePricing::random_item(Good::Tools, weapon) { - bag1.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); + *i = Some(Item::new_from_asset_expect(&item_id)); } } let mut bag2 = Item::new_from_asset_expect( @@ -577,11 +579,11 @@ impl LoadoutBuilder { .copied() .unwrap_or_default() / 10.0; - for i in 0..16 { + for i in bag2.slots_mut() { if let Some(item_id) = TradePricing::random_item(Good::Ingredients, ingredients) { - bag2.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); + *i = Some(Item::new_from_asset_expect(&item_id)); } } let mut bag3 = Item::new_from_asset_expect( @@ -593,9 +595,9 @@ impl LoadoutBuilder { .copied() .unwrap_or_default() / 10.0; - for i in 0..16 { + for i in bag3.slots_mut() { if let Some(item_id) = TradePricing::random_item(Good::Food, food) { - bag3.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); + *i = Some(Item::new_from_asset_expect(&item_id)); } } let mut bag4 = Item::new_from_asset_expect( @@ -607,9 +609,9 @@ impl LoadoutBuilder { .copied() .unwrap_or_default() / 10.0; - for i in 0..16 { + for i in bag4.slots_mut() { if let Some(item_id) = TradePricing::random_item(Good::Potions, potions) { - bag4.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); + *i = Some(Item::new_from_asset_expect(&item_id)); } } LoadoutBuilder::new()