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 => {
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()