Skip to content

Commit

Permalink
Consolidate Sell functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kphoenix137 committed Sep 20, 2024
1 parent 1dd4624 commit 705e5f6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 129 deletions.
182 changes: 58 additions & 124 deletions Source/stores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<int8_t>(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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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];

Expand All @@ -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];

Expand All @@ -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;
}

Expand All @@ -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
Expand Down Expand Up @@ -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<int8_t>(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;
Expand All @@ -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];
Expand Down Expand Up @@ -2145,7 +2079,7 @@ void StoreSession::StartStore(TalkID s)
break;
}
case TalkID::SmithSell:
DrawSmithSell();
DrawTownerSell(s);
break;
case TalkID::SmithRepair:
DrawSmithRepair();
Expand All @@ -2159,7 +2093,7 @@ void StoreSession::StartStore(TalkID s)
DrawTownerBuy(s);
break;
case TalkID::WitchSell:
DrawWitchSell();
DrawTownerSell(s);
break;
case TalkID::WitchRecharge:
DrawWitchRecharge();
Expand Down
11 changes: 6 additions & 5 deletions Source/stores.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand All @@ -188,8 +193,6 @@ class StoreSession {
// Smith Premium Buy

// Smith Sell
bool CanSellToSmith(int i);
void DrawSmithSell();

// Smith Repair
bool CanSmithRepair(int i) const;
Expand All @@ -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);
Expand Down

0 comments on commit 705e5f6

Please sign in to comment.