loop over slots_mut instead of using an index

This commit is contained in:
Christof Petig 2021-03-24 00:15:55 +01:00
parent a83a74d216
commit acba98366a

View File

@ -530,28 +530,30 @@ impl LoadoutBuilder {
Merchant => { Merchant => {
let mut backpack = let mut backpack =
Item::new_from_asset_expect("common.items.armor.misc.back.backpack"); Item::new_from_asset_expect("common.items.armor.misc.back.backpack");
let mut coins = Item::new_from_asset_expect("common.items.utility.coins"); let mut coins = economy
coins .map(|e| e.unconsumed_stock.get(&Good::Coin))
.set_amount( .flatten()
(economy .copied()
.map(|e| e.unconsumed_stock.get(&Good::Coin)) .unwrap_or_default()
.flatten() .round() as u32;
.copied()
.unwrap_or_default()
.round() as u32)
.max(1),
)
.expect("coins should be stackable");
backpack.slots_mut()[0] = Some(coins);
let armor = economy let armor = economy
.map(|e| e.unconsumed_stock.get(&Good::Armor)) .map(|e| e.unconsumed_stock.get(&Good::Armor))
.flatten() .flatten()
.copied() .copied()
.unwrap_or_default() .unwrap_or_default()
/ 10.0; / 10.0;
for i in 1..18 { for s in backpack.slots_mut() {
if let Some(item_id) = TradePricing::random_item(Good::Armor, armor) { if coins > 0 {
backpack.slots_mut()[i] = Some(Item::new_from_asset_expect(&item_id)); 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( let mut bag1 = Item::new_from_asset_expect(
@ -563,9 +565,9 @@ impl LoadoutBuilder {
.copied() .copied()
.unwrap_or_default() .unwrap_or_default()
/ 10.0; / 10.0;
for i in 0..16 { for i in bag1.slots_mut() {
if let Some(item_id) = TradePricing::random_item(Good::Tools, weapon) { 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( let mut bag2 = Item::new_from_asset_expect(
@ -577,11 +579,11 @@ impl LoadoutBuilder {
.copied() .copied()
.unwrap_or_default() .unwrap_or_default()
/ 10.0; / 10.0;
for i in 0..16 { for i in bag2.slots_mut() {
if let Some(item_id) = if let Some(item_id) =
TradePricing::random_item(Good::Ingredients, ingredients) 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( let mut bag3 = Item::new_from_asset_expect(
@ -593,9 +595,9 @@ impl LoadoutBuilder {
.copied() .copied()
.unwrap_or_default() .unwrap_or_default()
/ 10.0; / 10.0;
for i in 0..16 { for i in bag3.slots_mut() {
if let Some(item_id) = TradePricing::random_item(Good::Food, food) { 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( let mut bag4 = Item::new_from_asset_expect(
@ -607,9 +609,9 @@ impl LoadoutBuilder {
.copied() .copied()
.unwrap_or_default() .unwrap_or_default()
/ 10.0; / 10.0;
for i in 0..16 { for i in bag4.slots_mut() {
if let Some(item_id) = TradePricing::random_item(Good::Potions, potions) { 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() LoadoutBuilder::new()