Skip to content

Commit

Permalink
remember items bought from shops
Browse files Browse the repository at this point in the history
  • Loading branch information
qndel committed Oct 20, 2021
1 parent d5999e6 commit 508f7b0
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 105 deletions.
1 change: 1 addition & 0 deletions Source/diablo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,7 @@ void DisableInputWndProc(uint32_t uMsg, int32_t /*wParam*/, int32_t lParam)

void LoadGameLevel(bool firstflag, lvl_entry lvldir)
{
ResetBoughtItems();
music_stop();
if (pcurs > CURSOR_HAND && pcurs < CURSOR_FIRSTITEM) {
NewCursor(CURSOR_HAND);
Expand Down
60 changes: 60 additions & 0 deletions Source/loadsave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,43 @@ void LoadHeroItems(Player &player)
gbIsHellfireSaveGame = gbIsHellfire;
}

void LoadBoughtItems()
{
ResetBoughtItems();

if (currlevel != 0)
return;
if (gbIsMultiplayer)
return;

LoadHelper file("boughtitems");
if (!file.IsValid())
return;

boughtWirtItem = file.NextLE<uint32_t>();
if (boughtWirtItem)
BoyBuyItem(true);
int smithSize = file.NextLE<uint32_t>();
int witchSize = file.NextLE<uint32_t>();
int healerSize = file.NextLE<uint32_t>();

for (int i = 0; i < smithSize; i++) {
auto index = file.NextLE<uint32_t>();
boughtSmithItems.push_back(index);
SmithBuyItem(true, index);
}
for (int i = 0; i < witchSize; i++) {
auto index = file.NextLE<uint32_t>();
boughtWitchItems.push_back(index);
WitchBuyItem(true, index);
}
for (int i = 0; i < healerSize; i++) {
auto index = file.NextLE<uint32_t>();
boughtHealerItems.push_back(index);
HealerBuyItem(true, index);
}
}

void RemoveEmptyInventory(Player &player)
{
for (int i = NUM_INV_GRID_ELEM; i > 0; i--) {
Expand Down Expand Up @@ -1853,6 +1890,8 @@ void LoadGame(bool firstflag)
}

gbIsHellfireSaveGame = gbIsHellfire;

LoadBoughtItems();
}

void SaveHeroItems(Player &player)
Expand All @@ -1870,6 +1909,25 @@ void SaveHeroItems(Player &player)
SaveItem(file, item);
}

void SaveBoughtItems()
{
size_t boughtItemsDataSize = sizeof(uint32_t) * (4 + boughtSmithItems.size() + boughtWitchItems.size() + boughtHealerItems.size());
SaveHelper file("boughtitems", boughtItemsDataSize);

file.WriteLE<uint32_t>(boughtWirtItem);
file.WriteLE<uint32_t>(boughtSmithItems.size());
file.WriteLE<uint32_t>(boughtWitchItems.size());
file.WriteLE<uint32_t>(boughtHealerItems.size());

for (auto index : boughtSmithItems)
file.WriteLE<uint32_t>(index);
for (auto index : boughtWitchItems)
file.WriteLE<uint32_t>(index);
for (auto index : boughtHealerItems)
file.WriteLE<uint32_t>(index);

}

void SaveGameData()
{
SaveHelper file("game", 320 * 1024);
Expand Down Expand Up @@ -2021,6 +2079,8 @@ void SaveGameData()

file.WriteLE<uint8_t>(AutomapActive ? 1 : 0);
file.WriteBE<int32_t>(AutoMapScale);

SaveBoughtItems();
}

void SaveGame()
Expand Down
230 changes: 125 additions & 105 deletions Source/stores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ Item witchitem[WITCH_ITEMS];
int boylevel;
Item boyitem;

std::vector<uint32_t> boughtSmithItems;
std::vector<uint32_t> boughtWitchItems;
std::vector<uint32_t> boughtHealerItems;
bool boughtWirtItem = false;

namespace {

/** The current towner being interacted with */
Expand Down Expand Up @@ -1298,30 +1303,6 @@ void SmithEnter()
}
}

/**
* @brief Purchases an item from the smith.
*/
void SmithBuyItem()
{
auto &myPlayer = Players[MyPlayerId];
auto &item = myPlayer.HoldItem;

TakePlrsMoney(item._iIvalue);
if (item._iMagical == ITEM_QUALITY_NORMAL)
item._iIdentified = false;
StoreAutoPlace();
int idx = stextvhold + ((stextlhold - stextup) / 4);
if (idx == SMITH_ITEMS - 1) {
smithitem[SMITH_ITEMS - 1]._itype = ItemType::None;
} else {
for (; !smithitem[idx + 1].isEmpty(); idx++) {
smithitem[idx] = smithitem[idx + 1];
}
smithitem[idx]._itype = ItemType::None;
}
CalcPlrInv(myPlayer, true);
}

void SmitBuyEnter()
{
if (stextsel == 22) {
Expand Down Expand Up @@ -1625,35 +1606,6 @@ void WitchEnter()
}
}

/**
* @brief Purchases an item from the witch.
*/
void WitchBuyItem()
{
auto &myPlayer = Players[MyPlayerId];

int idx = stextvhold + ((stextlhold - stextup) / 4);

if (idx < 3)
myPlayer.HoldItem._iSeed = AdvanceRndSeed();

TakePlrsMoney(myPlayer.HoldItem._iIvalue);
StoreAutoPlace();

if (idx >= 3) {
if (idx == WITCH_ITEMS - 1) {
witchitem[WITCH_ITEMS - 1]._itype = ItemType::None;
} else {
for (; !witchitem[idx + 1].isEmpty(); idx++) {
witchitem[idx] = witchitem[idx + 1];
}
witchitem[idx]._itype = ItemType::None;
}
}

CalcPlrInv(myPlayer, true);
}

void WitchBuyEnter()
{
if (stextsel == 22) {
Expand Down Expand Up @@ -1778,58 +1730,6 @@ void BoyEnter()
StartStore(STORE_GOSSIP);
}

void BoyBuyItem()
{
auto &myPlayer = Players[MyPlayerId];
TakePlrsMoney(myPlayer.HoldItem._iIvalue);
StoreAutoPlace();
boyitem._itype = ItemType::None;
stextshold = STORE_BOY;
CalcPlrInv(myPlayer, true);
stextlhold = 12;
}

/**
* @brief Purchases an item from the healer.
*/
void HealerBuyItem()
{
auto &myPlayer = Players[MyPlayerId];
auto &item = myPlayer.HoldItem;

int idx = stextvhold + ((stextlhold - stextup) / 4);
if (!gbIsMultiplayer) {
if (idx < 2)
item._iSeed = AdvanceRndSeed();
} else {
if (idx < 3)
item._iSeed = AdvanceRndSeed();
}

TakePlrsMoney(item._iIvalue);
if (item._iMagical == ITEM_QUALITY_NORMAL)
item._iIdentified = false;
StoreAutoPlace();

if (!gbIsMultiplayer) {
if (idx < 2)
return;
} else {
if (idx < 3)
return;
}
idx = stextvhold + ((stextlhold - stextup) / 4);
if (idx == 19) {
healitem[19]._itype = ItemType::None;
} else {
for (; !healitem[idx + 1].isEmpty(); idx++) {
healitem[idx] = healitem[idx + 1];
}
healitem[idx]._itype = ItemType::None;
}
CalcPlrInv(myPlayer, true);
}

void BoyBuyEnter()
{
if (stextsel != 10) {
Expand Down Expand Up @@ -2785,4 +2685,124 @@ void ReleaseStoreBtn()
stextscrldbtn = -1;
}

void SmithBuyItem(bool fakeBuy, int newIdx)
{
auto &myPlayer = Players[MyPlayerId];
auto &item = myPlayer.HoldItem;

int idx = newIdx;
if (!fakeBuy) {
TakePlrsMoney(item._iIvalue);
if (item._iMagical == ITEM_QUALITY_NORMAL)
item._iIdentified = false;
StoreAutoPlace();
idx = stextvhold + ((stextlhold - stextup) / 4);
boughtSmithItems.push_back(idx);
}
if (idx == SMITH_ITEMS - 1) {
smithitem[SMITH_ITEMS - 1]._itype = ItemType::None;
} else {
for (; !smithitem[idx + 1].isEmpty(); idx++) {
smithitem[idx] = smithitem[idx + 1];
}
smithitem[idx]._itype = ItemType::None;
}
CalcPlrInv(myPlayer, true);
}

void WitchBuyItem(bool fakeBuy, int newIdx)
{
auto &myPlayer = Players[MyPlayerId];

int idx = newIdx;
if (!fakeBuy) {
idx = stextvhold + ((stextlhold - stextup) / 4);

if (idx < 3)
myPlayer.HoldItem._iSeed = AdvanceRndSeed();

TakePlrsMoney(myPlayer.HoldItem._iIvalue);
StoreAutoPlace();
boughtWitchItems.push_back(idx);
}

if (idx >= 3) {
if (idx == WITCH_ITEMS - 1) {
witchitem[WITCH_ITEMS - 1]._itype = ItemType::None;
} else {
for (; !witchitem[idx + 1].isEmpty(); idx++) {
witchitem[idx] = witchitem[idx + 1];
}
witchitem[idx]._itype = ItemType::None;
}
}

CalcPlrInv(myPlayer, true);
}

void HealerBuyItem(bool fakeBuy, int newIdx)
{
auto &myPlayer = Players[MyPlayerId];
auto &item = myPlayer.HoldItem;

int idx = newIdx;
if (!fakeBuy) {
idx = stextvhold + ((stextlhold - stextup) / 4);
if (!gbIsMultiplayer) {
if (idx < 2)
item._iSeed = AdvanceRndSeed();
} else {
if (idx < 3)
item._iSeed = AdvanceRndSeed();
}

TakePlrsMoney(item._iIvalue);
if (item._iMagical == ITEM_QUALITY_NORMAL)
item._iIdentified = false;
StoreAutoPlace();

if (!gbIsMultiplayer) {
if (idx < 2)
return;
} else {
if (idx < 3)
return;
}

boughtHealerItems.push_back(idx);
}

if (idx == 19) {
healitem[19]._itype = ItemType::None;
} else {
for (; !healitem[idx + 1].isEmpty(); idx++) {
healitem[idx] = healitem[idx + 1];
}
healitem[idx]._itype = ItemType::None;
}
CalcPlrInv(myPlayer, true);
}

void BoyBuyItem(bool fakeBuy)
{
auto &myPlayer = Players[MyPlayerId];
if (!fakeBuy) {
TakePlrsMoney(myPlayer.HoldItem._iIvalue);
StoreAutoPlace();
boughtWirtItem = true;
}
boyitem._itype = ItemType::None;
stextshold = STORE_BOY;
CalcPlrInv(myPlayer, true);
stextlhold = 12;
}

void ResetBoughtItems()
{
boughtSmithItems.clear();
boughtWitchItems.clear();
boughtHealerItems.clear();
boughtWirtItem = false;
}

} // namespace devilution
Loading

0 comments on commit 508f7b0

Please sign in to comment.