From 1e6277595d1d063250ea048345af5555d3dad4f2 Mon Sep 17 00:00:00 2001 From: qndel Date: Mon, 10 Jul 2023 20:26:23 +0200 Subject: [PATCH] deduplicate NPC sell/buy code --- Source/stores.cpp | 119 ++++++++++++---------------------------------- 1 file changed, 31 insertions(+), 88 deletions(-) diff --git a/Source/stores.cpp b/Source/stores.cpp index d4e87fd7a..b8b88e7b7 100644 --- a/Source/stores.cpp +++ b/Source/stores.cpp @@ -348,6 +348,32 @@ bool StoreAutoPlace(Item &item, bool persistItem) return AutoPlaceItemInInventory(player, item, persistItem); } +void ScrollVendorStore(Item *itemData, int storeLimit, int idx, int selling = true) +{ + ClearSText(5, 21); + stextup = 5; + + for (int l = 5; l < 20 && idx < storeLimit; l += 4) { + const Item &item = itemData[idx]; + if (!item.isEmpty()) { + UiFlags itemColor = item.getTextColorWithStatCheck(); + AddSText(20, l, item.getName(), itemColor, true, item._iCurs, true); + AddSTextVal(l, item._iIdentified ? item._iIvalue : item._ivalue); + PrintStoreItem(item, l + 1, itemColor, true); + stextdown = l; + } else { + l -= 4; + } + idx++; + } + if (selling) { + if (stextsel != -1 && !stext[stextsel].isSelectable() && stextsel != BackButtonLine()) + stextsel = stextdown; + } else { + stextsmax = std::max(storeLimit - 4, 0); + } +} + void StartSmith() { stextsize = false; @@ -367,22 +393,7 @@ void StartSmith() void ScrollSmithBuy(int idx) { - ClearSText(5, 21); - stextup = 5; - - for (int l = 5; l < 20; l += 4) { - if (!smithitem[idx].isEmpty()) { - UiFlags itemColor = smithitem[idx].getTextColorWithStatCheck(); - AddSText(20, l, smithitem[idx].getName(), itemColor, true, smithitem[idx]._iCurs, true); - AddSTextVal(l, smithitem[idx]._iIvalue); - PrintStoreItem(smithitem[idx], l + 1, itemColor, true); - stextdown = l; - idx++; - } - } - - if (stextsel != -1 && !stext[stextsel].isSelectable() && stextsel != BackButtonLine()) - stextsel = stextdown; + ScrollVendorStore(smithitem, std::size(smithitem), idx); } uint32_t TotalPlayerGold() @@ -422,29 +433,13 @@ void StartSmithBuy() void ScrollSmithPremiumBuy(int boughtitems) { - ClearSText(5, 21); - stextup = 5; - int idx = 0; for (; boughtitems != 0; idx++) { if (!premiumitems[idx].isEmpty()) boughtitems--; } - for (int l = 5; l < 20 && idx < SMITH_PREMIUM_ITEMS; l += 4) { - if (!premiumitems[idx].isEmpty()) { - UiFlags itemColor = premiumitems[idx].getTextColorWithStatCheck(); - AddSText(20, l, premiumitems[idx].getName(), itemColor, true, premiumitems[idx]._iCurs, true); - AddSTextVal(l, premiumitems[idx]._iIvalue); - PrintStoreItem(premiumitems[idx], l + 1, itemColor, true); - stextdown = l; - } else { - l -= 4; - } - idx++; - } - if (stextsel != -1 && !stext[stextsel].isSelectable() && stextsel != BackButtonLine()) - stextsel = stextdown; + ScrollVendorStore(premiumitems, std::size(premiumitems), idx); } bool StartSmithPremiumBuy() @@ -511,30 +506,7 @@ bool SmithSellOk(int i) void ScrollSmithSell(int idx) { - ClearSText(5, 21); - stextup = 5; - - for (int l = 5; l < 20; l += 4) { - if (idx >= storenumh) - break; - if (!storehold[idx].isEmpty()) { - UiFlags itemColor = storehold[idx].getTextColorWithStatCheck(); - - if (storehold[idx]._iMagical != ITEM_QUALITY_NORMAL && storehold[idx]._iIdentified) { - AddSText(20, l, storehold[idx].getName(), itemColor, true, storehold[idx]._iCurs, true); - AddSTextVal(l, storehold[idx]._iIvalue); - } else { - AddSText(20, l, storehold[idx].getName(), itemColor, true, storehold[idx]._iCurs, true); - AddSTextVal(l, storehold[idx]._ivalue); - } - - PrintStoreItem(storehold[idx], l + 1, itemColor, true); - stextdown = l; - } - idx++; - } - - stextsmax = std::max(storenumh - 4, 0); + ScrollVendorStore(storehold, storenumh, idx, false); } void StartSmithSell() @@ -714,22 +686,7 @@ void StartWitch() void ScrollWitchBuy(int idx) { - ClearSText(5, 21); - stextup = 5; - - for (int l = 5; l < 20; l += 4) { - if (!witchitem[idx].isEmpty()) { - UiFlags itemColor = witchitem[idx].getTextColorWithStatCheck(); - AddSText(20, l, witchitem[idx].getName(), itemColor, true, witchitem[idx]._iCurs, true); - AddSTextVal(l, witchitem[idx]._iIvalue); - PrintStoreItem(witchitem[idx], l + 1, itemColor, true); - stextdown = l; - idx++; - } - } - - if (stextsel != -1 && !stext[stextsel].isSelectable() && stextsel != BackButtonLine()) - stextsel = stextdown; + ScrollVendorStore(witchitem, std::size(witchitem), idx); } void WitchBookLevel(Item &bookItem) @@ -1078,21 +1035,7 @@ void StartHealer() void ScrollHealerBuy(int idx) { - ClearSText(5, 21); - stextup = 5; - for (int l = 5; l < 20; l += 4) { - if (!healitem[idx].isEmpty()) { - UiFlags itemColor = healitem[idx].getTextColorWithStatCheck(); - AddSText(20, l, healitem[idx].getName(), itemColor, true, healitem[idx]._iCurs, true); - AddSTextVal(l, healitem[idx]._iIvalue); - PrintStoreItem(healitem[idx], l + 1, itemColor, true); - stextdown = l; - idx++; - } - } - - if (stextsel != -1 && !stext[stextsel].isSelectable() && stextsel != BackButtonLine()) - stextsel = stextdown; + ScrollVendorStore(healitem, std::size(healitem), idx); } void StartHealerBuy()