From 34b6132dc20ea31d0807d110f94e3cadecc9af78 Mon Sep 17 00:00:00 2001 From: Eric Robinson Date: Thu, 19 Sep 2024 20:42:10 -0400 Subject: [PATCH] Consolidate Sell functions --- Source/stores.cpp | 182 +++++++++++++++------------------------------- Source/stores.h | 11 +-- 2 files changed, 64 insertions(+), 129 deletions(-) diff --git a/Source/stores.cpp b/Source/stores.cpp index 63c544120b5..7f180fa4b36 100644 --- a/Source/stores.cpp +++ b/Source/stores.cpp @@ -35,7 +35,7 @@ const int doubleLineSpace = 2; const int tripleLineSpace = 3; constexpr int MainMenuDividerLine = 5; -constexpr int BuyMenuDividerLine = 3; +constexpr int BuySellMenuDividerLine = 3; constexpr int BuyLineSpace = 4; constexpr int WirtDialogueDrawLine = 12; @@ -597,6 +597,22 @@ void StoreSession::SetBuyMenuText() SetLineText(20, 1, _("I have this item for sale:"), flags, false); } +void StoreSession::SetWitchBookMinMag(Item &bookItem) +{ + if (bookItem._iMiscId != IMISC_BOOK) + return; + bookItem._iMinMag = GetSpellData(bookItem._iSpell).minInt; + uint8_t spellLevel = MyPlayer->_pSplLvl[static_cast(bookItem._iSpell)]; + while (spellLevel > 0) { + bookItem._iMinMag += 20 * bookItem._iMinMag / 100; + spellLevel--; + if (bookItem._iMinMag + 20 * bookItem._iMinMag / 100 > 255) { + bookItem._iMinMag = 255; + spellLevel = 0; + } + } +} + void StoreSession::UpdateItemStatFlags(TalkID talkId) { playerItemIdx = 0; @@ -656,7 +672,7 @@ void StoreSession::DrawTownerBuy(TalkID talkId) numTextLines = 20; } - SetLineAsDivider(BuyMenuDividerLine); + SetLineAsDivider(BuySellMenuDividerLine); DrawItemListForTowner(talkId, currentScrollPos); UpdateItemStatFlags(talkId); hasScrollbar = playerItemIdx > BuyLineSpace ? true : false; @@ -665,37 +681,7 @@ void StoreSession::DrawTownerBuy(TalkID talkId) AddItemListBackButton(talkId, true); } -bool StoreSession::CanSellToSmith(int i) -{ - Item *pI; - - if (i >= 0) { - pI = &MyPlayer->InvList[i]; - } else { - pI = &MyPlayer->SpdList[-(i + 1)]; - } - - if (pI->isEmpty()) - return false; - - if (pI->_iMiscId > IMISC_OILFIRST && pI->_iMiscId < IMISC_OILLAST) - return true; - - if (pI->_itype == ItemType::Misc) - return false; - if (pI->_itype == ItemType::Gold) - return false; - if (pI->_itype == ItemType::Staff && (!gbIsHellfire || IsValidSpell(pI->_iSpell))) - return false; - if (pI->_iClass == ICLASS_QUEST) - return false; - if (pI->IDidx == IDI_LAZSTAFF) - return false; - - return true; -} - -void StoreSession::DrawSmithSell() +void StoreSession::DrawTownerSell(TalkID talkId) { isTextFullSize = true; bool sellOk = false; @@ -708,7 +694,7 @@ void StoreSession::DrawSmithSell() for (int8_t i = 0; i < MyPlayer->_pNumInv; i++) { if (playerItemIdx >= 48) break; - if (CanSellToSmith(i)) { + if (talkId == TalkID::SmithSell ? CanSellToSmith(i) : CanSellToWitch(i)) { sellOk = true; playerItem[playerItemIdx] = MyPlayer->InvList[i]; @@ -725,7 +711,7 @@ void StoreSession::DrawSmithSell() for (int i = 0; i < MaxBeltItems; i++) { if (playerItemIdx >= 48) break; - if (CanSellToSmith(-(i + 1))) { + if (talkId == TalkID::SmithSell ? CanSellToSmith(-(i + 1)) : CanSellToWitch(-(i + 1))) { sellOk = true; playerItem[playerItemIdx] = MyPlayer->SpdList[i]; @@ -744,8 +730,8 @@ void StoreSession::DrawSmithSell() renderGold = true; SetLineText(20, 1, _("You have nothing I want."), UiFlags::ColorWhitegold, false); - SetLineAsDivider(3); - AddItemListBackButton(TalkID::SmithSell, /*selectable=*/true); + SetLineAsDivider(BuySellMenuDividerLine); + AddItemListBackButton(talkId, /*selectable=*/true); return; } @@ -755,9 +741,39 @@ void StoreSession::DrawSmithSell() renderGold = true; SetLineText(20, 1, _("Which item is for sale?"), UiFlags::ColorWhitegold, false); - SetLineAsDivider(3); - DrawItemListForTowner(TalkID::SmithSell, currentScrollPos, false); - AddItemListBackButton(TalkID::SmithSell); + SetLineAsDivider(BuySellMenuDividerLine); + DrawItemListForTowner(talkId, currentScrollPos, false); + AddItemListBackButton(talkId); +} + +bool StoreSession::CanSellToSmith(int i) +{ + Item *pI; + + if (i >= 0) { + pI = &MyPlayer->InvList[i]; + } else { + pI = &MyPlayer->SpdList[-(i + 1)]; + } + + if (pI->isEmpty()) + return false; + + if (pI->_iMiscId > IMISC_OILFIRST && pI->_iMiscId < IMISC_OILLAST) + return true; + + if (pI->_itype == ItemType::Misc) + return false; + if (pI->_itype == ItemType::Gold) + return false; + if (pI->_itype == ItemType::Staff && (!gbIsHellfire || IsValidSpell(pI->_iSpell))) + return false; + if (pI->_iClass == ICLASS_QUEST) + return false; + if (pI->IDidx == IDI_LAZSTAFF) + return false; + + return true; } bool StoreSession::CanSmithRepair(int i) const @@ -873,22 +889,6 @@ void StoreSession::RestoreMana() RedrawComponent(PanelDrawComponent::Mana); } -void StoreSession::SetWitchBookMinMag(Item &bookItem) -{ - if (bookItem._iMiscId != IMISC_BOOK) - return; - bookItem._iMinMag = GetSpellData(bookItem._iSpell).minInt; - uint8_t spellLevel = MyPlayer->_pSplLvl[static_cast(bookItem._iSpell)]; - while (spellLevel > 0) { - bookItem._iMinMag += 20 * bookItem._iMinMag / 100; - spellLevel--; - if (bookItem._iMinMag + 20 * bookItem._iMinMag / 100 > 255) { - bookItem._iMinMag = 255; - spellLevel = 0; - } - } -} - bool StoreSession::CanSellToWitch(int i) { Item *pI; @@ -915,72 +915,6 @@ bool StoreSession::CanSellToWitch(int i) return rv; } -void StoreSession::DrawWitchSell() -{ - isTextFullSize = true; - bool sellok = false; - playerItemIdx = 0; - - for (auto &item : playerItem) { - item.clear(); - } - - for (int i = 0; i < MyPlayer->_pNumInv; i++) { - if (playerItemIdx >= 48) - break; - if (CanSellToWitch(i)) { - sellok = true; - playerItem[playerItemIdx] = MyPlayer->InvList[i]; - - if (playerItem[playerItemIdx]._iMagical != ITEM_QUALITY_NORMAL && playerItem[playerItemIdx]._iIdentified) - playerItem[playerItemIdx]._ivalue = playerItem[playerItemIdx]._iIvalue; - - playerItem[playerItemIdx]._ivalue = std::max(playerItem[playerItemIdx]._ivalue / 4, 1); - playerItem[playerItemIdx]._iIvalue = playerItem[playerItemIdx]._ivalue; - playerItemIndexes[playerItemIdx] = i; - playerItemIdx++; - } - } - - for (int i = 0; i < MaxBeltItems; i++) { - if (playerItemIdx >= 48) - break; - if (!MyPlayer->SpdList[i].isEmpty() && CanSellToWitch(-(i + 1))) { - sellok = true; - playerItem[playerItemIdx] = MyPlayer->SpdList[i]; - - if (playerItem[playerItemIdx]._iMagical != ITEM_QUALITY_NORMAL && playerItem[playerItemIdx]._iIdentified) - playerItem[playerItemIdx]._ivalue = playerItem[playerItemIdx]._iIvalue; - - playerItem[playerItemIdx]._ivalue = std::max(playerItem[playerItemIdx]._ivalue / 4, 1); - playerItem[playerItemIdx]._iIvalue = playerItem[playerItemIdx]._ivalue; - playerItemIndexes[playerItemIdx] = -(i + 1); - playerItemIdx++; - } - } - - if (!sellok) { - hasScrollbar = false; - - renderGold = true; - SetLineText(20, 1, _("You have nothing I want."), UiFlags::ColorWhitegold, false); - - SetLineAsDivider(3); - AddItemListBackButton(TalkID::WitchSell, /*selectable=*/true); - return; - } - - hasScrollbar = true; - currentScrollPos = 0; - numTextLines = MyPlayer->_pNumInv; - - renderGold = true; - SetLineText(20, 1, _("Which item is for sale?"), UiFlags::ColorWhitegold, false); - SetLineAsDivider(3); - DrawItemListForTowner(TalkID::WitchSell, currentScrollPos, false); - AddItemListBackButton(TalkID::WitchSell); -} - bool StoreSession::CanWitchRecharge(int i) { const auto &item = MyPlayer->InvList[i]; @@ -2145,7 +2079,7 @@ void StoreSession::StartStore(TalkID s) break; } case TalkID::SmithSell: - DrawSmithSell(); + DrawTownerSell(s); break; case TalkID::SmithRepair: DrawSmithRepair(); @@ -2159,7 +2093,7 @@ void StoreSession::StartStore(TalkID s) DrawTownerBuy(s); break; case TalkID::WitchSell: - DrawWitchSell(); + DrawTownerSell(s); break; case TalkID::WitchRecharge: DrawWitchRecharge(); diff --git a/Source/stores.h b/Source/stores.h index 56e91871a99..fd9b8fe5f81 100644 --- a/Source/stores.h +++ b/Source/stores.h @@ -178,8 +178,13 @@ class StoreSession { void DrawItemList(TalkID talkId, Item *itemData, int storeLimit, int idx, bool selling = true); void DrawItemListForTowner(TalkID talkId, int idx, bool selling = true); void SetBuyMenuText(); + void SetWitchBookMinMag(Item &bookItem); void UpdateItemStatFlags(TalkID talkId); void DrawTownerBuy(TalkID talkId); + void DrawTownerSell(TalkID talkId); + + bool CanSellToSmith(int i); + bool CanSellToWitch(int i); /* Smith */ @@ -188,8 +193,6 @@ class StoreSession { // Smith Premium Buy // Smith Sell - bool CanSellToSmith(int i); - void DrawSmithSell(); // Smith Repair bool CanSmithRepair(int i) const; @@ -201,12 +204,10 @@ class StoreSession { void RestoreMana(); // Witch Buy - void SetWitchBookMinMag(Item &bookItem); // Witch Sell - bool CanSellToWitch(int i); + // Where is DrawWitchSellItem()? - void DrawWitchSell(); // Witch Recharge bool CanWitchRecharge(int i);