Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buyback functionality for Griswold and Adria #1069

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
236 changes: 228 additions & 8 deletions Source/stores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ ItemStruct smithitem[SMITH_ITEMS];
int stextdown;
char stextscrlubtn;
char stextflag;
ItemStruct smithbuybackitems[SMITH_ITEMS];
ItemStruct witchbuybackitems[SMITH_ITEMS];
AJenbo marked this conversation as resolved.
Show resolved Hide resolved
ItemStruct *itemlist;

/** Maps from towner IDs to NPC names. */
const char *const talkname[9] = {
Expand Down Expand Up @@ -75,6 +78,11 @@ void InitStores()
for (i = 0; i < SMITH_PREMIUM_ITEMS; i++)
premiumitem[i]._itype = ITYPE_NONE;

for (i = 0; i < SMITH_ITEMS; i++) {
smithbuybackitems[i]._itype = ITYPE_NONE;
AJenbo marked this conversation as resolved.
Show resolved Hide resolved
witchbuybackitems[i]._itype = ITYPE_NONE;
}

boyitem._itype = ITYPE_NONE;
boylevel = 0;
}
Expand Down Expand Up @@ -437,8 +445,9 @@ void S_StartSmith()
AddSText(0, 12, TRUE, "Buy basic items", COL_WHITE, TRUE);
AddSText(0, 14, TRUE, "Buy premium items", COL_WHITE, TRUE);
AddSText(0, 16, TRUE, "Sell items", COL_WHITE, TRUE);
AddSText(0, 18, TRUE, "Repair items", COL_WHITE, TRUE);
AddSText(0, 20, TRUE, "Leave the shop", COL_WHITE, TRUE);
AddSText(0, 18, TRUE, "Buy back items", COL_WHITE, TRUE);
AddSText(0, 20, TRUE, "Repair items", COL_WHITE, TRUE);
AddSText(0, 22, TRUE, "Leave the shop", COL_WHITE, TRUE);
AddSLine(5);
storenumh = 20;
}
Expand Down Expand Up @@ -820,8 +829,9 @@ void S_StartWitch()
AddSText(0, 12, TRUE, "Talk to Adria", COL_BLUE, TRUE);
AddSText(0, 14, TRUE, "Buy items", COL_WHITE, TRUE);
AddSText(0, 16, TRUE, "Sell items", COL_WHITE, TRUE);
AddSText(0, 18, TRUE, "Recharge staves", COL_WHITE, TRUE);
AddSText(0, 20, TRUE, "Leave the shack", COL_WHITE, TRUE);
AddSText(0, 18, TRUE, "Buy back items", COL_WHITE, TRUE);
AddSText(0, 20, TRUE, "Recharge staves", COL_WHITE, TRUE);
AddSText(0, 22, TRUE, "Leave the shack", COL_WHITE, TRUE);
AddSLine(5);
storenumh = 20;
}
Expand Down Expand Up @@ -1138,6 +1148,10 @@ void S_StartConfirm()
case STORE_SREPAIR:
strcpy(tempstr, "Are you sure you want to repair this item?");
break;
case STORE_WBUYBACK:
case STORE_SBUYBACK:
strcpy(tempstr, "Are you sure you want to buy back this item?");
break;
}
AddSText(0, 15, TRUE, tempstr, COL_WHITE, FALSE);
AddSText(0, 18, TRUE, "Yes", COL_WHITE, TRUE);
Expand Down Expand Up @@ -1569,6 +1583,14 @@ void StartStore(char s)
case STORE_BARMAID:
S_StartBarMaid();
break;
case STORE_SBUYBACK:
itemlist = smithbuybackitems;
S_StartBuyBack();
break;
case STORE_WBUYBACK:
itemlist = witchbuybackitems;
S_StartBuyBack();
break;
}

for (i = 0; i < STORE_LINES; i++) {
Expand Down Expand Up @@ -1663,6 +1685,14 @@ void STextESC()
stextsel = 16;
break;
case STORE_SREPAIR:
StartStore(STORE_SMITH);
stextsel = 20;
break;
case STORE_WBUYBACK:
StartStore(STORE_WITCH);
stextsel = 18;
break;
case STORE_SBUYBACK:
StartStore(STORE_SMITH);
stextsel = 18;
break;
Expand Down Expand Up @@ -1826,9 +1856,12 @@ void S_SmithEnter()
StartStore(STORE_SSELL);
break;
case 18:
StartStore(STORE_SREPAIR);
StartStore(STORE_SBUYBACK);
break;
case 20:
StartStore(STORE_SREPAIR);
break;
case 22:
stextflag = STORE_NONE;
break;
}
Expand Down Expand Up @@ -2097,10 +2130,25 @@ void StoreSellItem()
int i, idx, cost;

idx = stextvhold + ((stextlhold - stextup) >> 2);
if (storehidx[idx] >= 0)

switch (stextshold) {
case STORE_SSELL:
itemlist = smithbuybackitems;
break;
case STORE_WSELL:
itemlist = witchbuybackitems;
}

for (int i = SMITH_ITEMS - 2; i > 0; i--)
itemlist[i] = itemlist[i - 1];

if (storehidx[idx] >= 0) {
itemlist[0] = plr[myplr].InvList[storehidx[idx]];
RemoveInvItem(myplr, storehidx[idx]);
else
} else {
itemlist[0] = plr[myplr].SpdList[-(storehidx[idx] + 1)];
RemoveSpdBarItem(myplr, -(storehidx[idx] + 1));
}
cost = storehold[idx]._iIvalue;
storenumh--;
if (idx != storenumh) {
Expand Down Expand Up @@ -2219,9 +2267,12 @@ void S_WitchEnter()
StartStore(STORE_WSELL);
return;
case 18:
StartStore(STORE_WRECHARGE);
StartStore(STORE_WBUYBACK);
return;
case 20:
StartStore(STORE_WRECHARGE);
return;
case 22:
stextflag = STORE_NONE;
break;
}
Expand Down Expand Up @@ -2521,6 +2572,14 @@ void S_ConfirmEnter()
case STORE_SPBUY:
SmithBuyPItem();
break;
case STORE_SBUYBACK:
itemlist = smithbuybackitems;
BuyBackItem();
break;
case STORE_WBUYBACK:
itemlist = witchbuybackitems;
BuyBackItem();
break;
}
}

Expand Down Expand Up @@ -2810,6 +2869,12 @@ void STextEnter()
case STORE_BARMAID:
S_BarmaidEnter();
break;
case STORE_SBUYBACK:
S_SBuyBackEnter();
break;
case STORE_WBUYBACK:
S_WBuyBackEnter();
break;
}
}
}
Expand Down Expand Up @@ -2872,4 +2937,159 @@ void ReleaseStoreBtn()
stextscrldbtn = -1;
}

void S_StartBuyBack()
{
int i;

storenumh = 0;
for (i = 0; itemlist[i]._itype != ITYPE_NONE; i++) {
storenumh++;
}

if (storenumh == 0) {
stextscrl = FALSE;
sprintf(tempstr, "You have not sold me anything. Your gold : %i", plr[myplr]._pGold);
AddSText(0, 1, TRUE, tempstr, COL_GOLD, FALSE);
AddSLine(3);
AddSLine(21);
AddSText(0, 22, TRUE, "Back", COL_WHITE, TRUE);
OffsetSTextY(22, 6);
} else {
stextsize = TRUE;
stextscrl = TRUE;
stextsval = 0;
sprintf(tempstr, "You can buy back these items : Your gold : %i", plr[myplr]._pGold);
AddSText(0, 1, TRUE, tempstr, COL_GOLD, FALSE);
AddSLine(3);
AddSLine(21);
S_ScrollBuyBack(stextsval);
AddSText(0, 22, TRUE, "Back", COL_WHITE, FALSE);
OffsetSTextY(22, 6);

stextsmax = storenumh - 4;
if (stextsmax < 0)
stextsmax = 0;
}
return;
}

void S_ScrollBuyBack(int idx)
{
int l, ls;
char iclr;

ls = idx;
ClearSText(5, 21);
stextup = 5;

for (l = 5; l < 20; l += 4) {
if (itemlist[ls]._itype != ITYPE_NONE) {
iclr = COL_WHITE;
if (itemlist[ls]._iMagical) {
iclr = COL_BLUE;
}

if (!itemlist[ls]._iStatFlag) {
iclr = COL_RED;
}

if (itemlist[ls]._iMagical) {
AddSText(20, l, FALSE, itemlist[ls]._iIName, iclr, TRUE);
} else {
AddSText(20, l, FALSE, itemlist[ls]._iName, iclr, TRUE);
}

AddSTextVal(l, itemlist[ls]._iIvalue);
PrintStoreItem(&itemlist[ls], l + 1, iclr);
stextdown = l;
ls++;
}
}
}

void S_SBuyBackEnter()
{
int idx, i;
BOOL done;

if (stextsel == 22) {
StartStore(STORE_SMITH);
stextsel = 12;
} else {
stextlhold = stextsel;
stextvhold = stextsval;
stextshold = STORE_SBUYBACK;
idx = stextsval + ((stextsel - stextup) >> 2);
if (plr[myplr]._pGold < smithbuybackitems[idx]._iIvalue) {
StartStore(STORE_NOMONEY);
} else {
plr[myplr].HoldItem = smithbuybackitems[idx];
SetCursor_(plr[myplr].HoldItem._iCurs + CURSOR_FIRSTITEM);
done = FALSE;

for (i = 0; i < NUM_INV_GRID_ELEM && !done; i++) {
done = AutoPlace(myplr, i, cursW / 28, cursH / 28, FALSE);
}
if (done)
StartStore(STORE_CONFIRM);
else
StartStore(STORE_NOROOM);
SetCursor_(CURSOR_HAND);
}
}
}

void S_WBuyBackEnter()
{
int idx, i;
BOOL done;

if (stextsel == 22) {
StartStore(STORE_WITCH);
stextsel = 12;
} else {
stextlhold = stextsel;
stextvhold = stextsval;
stextshold = STORE_WBUYBACK;
idx = stextsval + ((stextsel - stextup) >> 2);
if (plr[myplr]._pGold < witchbuybackitems[idx]._iIvalue) {
StartStore(STORE_NOMONEY);
} else {
plr[myplr].HoldItem = witchbuybackitems[idx];
SetCursor_(plr[myplr].HoldItem._iCurs + CURSOR_FIRSTITEM);
done = FALSE;

for (i = 0; i < NUM_INV_GRID_ELEM && !done; i++) {
done = AutoPlace(myplr, i, cursW / 28, cursH / 28, FALSE);
}
if (done)
StartStore(STORE_CONFIRM);
else
StartStore(STORE_NOROOM);
SetCursor_(CURSOR_HAND);
}
}
}

void BuyBackItem()
{
int idx;

TakePlrsMoney(plr[myplr].HoldItem._iIvalue);
if (plr[myplr].HoldItem._iMagical == ITEM_QUALITY_NORMAL)
plr[myplr].HoldItem._iIdentified = FALSE;
else
plr[myplr].HoldItem._iIdentified = TRUE;
StoreAutoPlace();
idx = stextvhold + ((stextlhold - stextup) >> 2);
if (idx == SMITH_ITEMS - 1) {
itemlist[SMITH_ITEMS - 1]._itype = ITYPE_NONE;
} else {
for (; itemlist[idx + 1]._itype != ITYPE_NONE; idx++) {
itemlist[idx] = itemlist[idx + 1];
}
itemlist[idx]._itype = ITYPE_NONE;
}
CalcPlrInv(myplr, TRUE);
}
DEVILUTION_END_NAMESPACE
5 changes: 5 additions & 0 deletions Source/stores.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ void STextEnter();
void CheckStoreBtn();
void ReleaseStoreBtn();

void S_StartBuyBack();
void S_ScrollBuyBack(int idx);
void BuyBackItem();
void S_SBuyBackEnter();
void S_WBuyBackEnter();
/* rdata */

#ifdef __cplusplus
Expand Down
4 changes: 3 additions & 1 deletion enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -3099,7 +3099,9 @@ typedef enum talk_id {
STORE_IDSHOW = 0x14,
STORE_TAVERN = 0x15,
STORE_DRUNK = 0x16,
STORE_BARMAID = 0x17,
STORE_BARMAID = 0x17,
STORE_SBUYBACK = 0x18,
STORE_WBUYBACK = 0x19,
} talk_id;

typedef enum _unique_items {
Expand Down