Skip to content

Commit

Permalink
Progress update
Browse files Browse the repository at this point in the history
  • Loading branch information
kphoenix137 committed Sep 27, 2024
1 parent c9d30dc commit bff5579
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 47 deletions.
56 changes: 31 additions & 25 deletions Source/items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4404,8 +4404,6 @@ void SpawnSmith(int lvl)
newItem._iIdentified = true;
}

Blacksmith.basicItemCount = iCnt;

for (int i = iCnt; i < NumSmithBasicItems; i++)
Blacksmith.basicItems[i].clear();

Expand All @@ -4416,31 +4414,43 @@ void SpawnPremium(const Player &player)
{
int lvl = player.getCharacterLevel();
int maxItems = gbIsHellfire ? NumSmithItemsHf : NumSmithItems;
if (Blacksmith.itemCount < maxItems) {
for (int i = 0; i < maxItems; i++) {
if (Blacksmith.items[i].isEmpty()) {
int plvl = Blacksmith.itemLevel + (gbIsHellfire ? itemLevelAddHf[i] : itemLevelAdd[i]);
SpawnOnePremium(Blacksmith.items[i], plvl, player);
}

// Ensure the vector has the correct size
Blacksmith.items.resize(maxItems);

// Fill empty slots with new premium items
for (auto &item : Blacksmith.items) {
if (item.isEmpty()) {
int plvl = Blacksmith.itemLevel + (gbIsHellfire ? itemLevelAddHf[&item - &Blacksmith.items[0]] : itemLevelAdd[&item - &Blacksmith.items[0]]);
SpawnOnePremium(item, plvl, player);
}
Blacksmith.itemCount = maxItems;
}

// Increase the item level as the player's level increases
while (Blacksmith.itemLevel < lvl) {
Blacksmith.itemLevel++;
if (gbIsHellfire) {
// Discard first 3 items and shift next 10
std::move(&Blacksmith.items[3], &Blacksmith.items[12] + 1, &Blacksmith.items[0]);
SpawnOnePremium(Blacksmith.items[10], Blacksmith.itemLevel + itemLevelAddHf[10], player);
Blacksmith.items[11] = Blacksmith.items[13];
SpawnOnePremium(Blacksmith.items[12], Blacksmith.itemLevel + itemLevelAddHf[12], player);
Blacksmith.items[13] = Blacksmith.items[14];
SpawnOnePremium(Blacksmith.items[14], Blacksmith.itemLevel + itemLevelAddHf[14], player);
// Discard the first 3 items and shift the next 10
Blacksmith.items.erase(Blacksmith.items.begin(), Blacksmith.items.begin() + 3);
Blacksmith.items.resize(maxItems); // Ensure the vector has the correct size
for (int i = 10; i < 15; ++i) {
if (i == 11 || i == 13) {
Blacksmith.items[i] = Blacksmith.items[i + 2];
} else {
SpawnOnePremium(Blacksmith.items[i], Blacksmith.itemLevel + itemLevelAddHf[i], player);
}
}
} else {
// Discard first 2 items and shift next 3
std::move(&Blacksmith.items[2], &Blacksmith.items[4] + 1, &Blacksmith.items[0]);
SpawnOnePremium(Blacksmith.items[3], Blacksmith.itemLevel + itemLevelAdd[3], player);
Blacksmith.items[4] = Blacksmith.items[5];
SpawnOnePremium(Blacksmith.items[5], Blacksmith.itemLevel + itemLevelAdd[5], player);
// Discard the first 2 items and shift the next 3
Blacksmith.items.erase(Blacksmith.items.begin(), Blacksmith.items.begin() + 2);
Blacksmith.items.resize(maxItems); // Ensure the vector has the correct size
for (int i = 3; i < 6; ++i) {
if (i == 4) {
Blacksmith.items[i] = Blacksmith.items[i + 1];
} else {
SpawnOnePremium(Blacksmith.items[i], Blacksmith.itemLevel + itemLevelAdd[i], player);
}
}
}
}
}
Expand Down Expand Up @@ -4508,7 +4518,6 @@ void SpawnWitch(int lvl)
item._iCreateInfo = lvl | CF_WITCH;
item._iIdentified = true;
}
Witch.itemCount = itemCount;

SortVendor(Witch.items, PinnedItemCount);
}
Expand Down Expand Up @@ -4623,8 +4632,6 @@ void SpawnBoy(int lvl)
|| Boy.items[0]._iIvalue < ivalue)
&& count < 250));

Boy.itemCount = 1; // FIXME: Magic number

Boy.items[0]._iCreateInfo = lvl | CF_BOY;
Boy.items[0]._iIdentified = true;
Boy.itemLevel = lvl / 2;
Expand Down Expand Up @@ -4660,7 +4667,6 @@ void SpawnHealer(int lvl)
item._iCreateInfo = lvl | CF_HEALER;
item._iIdentified = true;
}
Healer.itemCount = itemCount;

SortVendor(Healer.items, PinnedItemCount);
}
Expand Down
4 changes: 2 additions & 2 deletions Source/loadsave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2523,7 +2523,7 @@ void LoadGame(bool firstflag)
memset(dLight, 0, sizeof(dLight));
}

Blacksmith.itemCount = file.NextBE<int32_t>();
file.Skip(4); // Blacksmith.itemCount
Blacksmith.itemLevel = file.NextBE<int32_t>();

for (int i = 0; i < giNumberOfSmithPremiumItems; i++)
Expand Down Expand Up @@ -2786,7 +2786,7 @@ void SaveGameData(SaveWriter &saveWriter)
}
}

file.WriteBE<int32_t>(Blacksmith.itemCount);
file.Skip(4); // Blacksmith.itemCount
file.WriteBE<int32_t>(Blacksmith.itemLevel);

for (int i = 0; i < giNumberOfSmithPremiumItems; i++)
Expand Down
70 changes: 52 additions & 18 deletions Source/stores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,12 @@ int GetItemCount(TalkID talkId)
if (towner != nullptr) {
switch (talkId) {
case TalkID::SmithBuy:
return towner->basicItemCount;
return towner->basicItems.size();

Check warning on line 277 in Source/stores.cpp

View workflow job for this annotation

GitHub Actions / build

'return': conversion from 'size_t' to 'int', possible loss of data
case TalkID::SmithPremiumBuy:
case TalkID::HealerBuy:
case TalkID::WitchBuy:
case TalkID::BoyBuy:
return towner->itemCount;
return towner->items.size();

Check warning on line 282 in Source/stores.cpp

View workflow job for this annotation

GitHub Actions / build

'return': conversion from 'size_t' to 'int', possible loss of data
}
}

Expand Down Expand Up @@ -992,6 +992,13 @@ void SetupTownerBuyScreen(TalkID talkId)
numTextLines = std::max(itemCount - BuyLineSpace, 0);
SetBuyScreenHeader(talkId);
AddItemListBackButton(talkId, true);
SDL_Log("SetupTownerBuyScreen: itemCount=%d", itemCount);
SDL_Log("item 6 name: %s", Blacksmith.items[0]._iIName);
SDL_Log("item 6 name: %s", Blacksmith.items[1]._iIName);
SDL_Log("item 6 name: %s", Blacksmith.items[2]._iIName);
SDL_Log("item 6 name: %s", Blacksmith.items[3]._iIName);
SDL_Log("item 6 name: %s", Blacksmith.items[4]._iIName);
SDL_Log("item 6 name: %s", Blacksmith.items[5]._iIName);
}

void SetupTownerSellScreen(TalkID talkId)
Expand Down Expand Up @@ -1220,22 +1227,34 @@ void SmithBuyEnter()
*/
void SmithBuyPItem(Item &item)
{
// Deduct the item cost from the player's gold
TakePlrsMoney(item._iIvalue);

// If the item is not magical, mark it as unidentified
if (item._iMagical == ITEM_QUALITY_NORMAL)
item._iIdentified = false;

// Give the item to the player
GiveItemToPlayer(item, true);

// Calculate the index of the item in the vector
int idx = oldScrollPos + ((oldTextLine - previousScrollPos) / 4);
int xx = 0;
for (int i = 0; idx >= 0; i++) {
if (!Blacksmith.items[i].isEmpty()) {

// Iterate through the vector to find the correct item to remove
auto it = Blacksmith.items.begin();
while (idx >= 0 && it != Blacksmith.items.end()) {
if (!it->isEmpty()) {
if (idx == 0) {
// Remove the item from the vector
it = Blacksmith.items.erase(it);
break;
}
idx--;
xx = i;
}
++it;
}

Blacksmith.items[xx].clear();
Blacksmith.itemCount--;
// Spawn new premium items
SpawnPremium(*MyPlayer);
}

Expand All @@ -1251,26 +1270,42 @@ void SmithPremiumBuyEnter()
oldTextLine = currentTextLine;
oldScrollPos = scrollPos;

int xx = scrollPos + ((currentTextLine - previousScrollPos) / 4);
int idx = 0;
for (int i = 0; xx >= 0; i++) {
if (!Blacksmith.items[i].isEmpty()) {
xx--;
idx = i;
// Calculate the index based on scroll position and current text line
int idx = scrollPos + ((currentTextLine - previousScrollPos) / 4);

// Find the corresponding item in the Blacksmith.items vector
auto it = Blacksmith.items.begin();
int currentIdx = 0;
while (it != Blacksmith.items.end()) {
if (!it->isEmpty()) {
if (currentIdx == idx) {
break; // Found the correct item
}
currentIdx++;
}
++it;
}

// Check if we've gone past the end of the vector
if (it == Blacksmith.items.end() || currentIdx != idx) {
// If idx is out of range, return or handle error
StartStore(TalkID::NoMoney); // Or appropriate error handling
return;
}

if (!CanPlayerAfford(Blacksmith.items[idx]._iIvalue)) {
// Now `it` points to the correct item, check if the player can afford it
if (!CanPlayerAfford(it->_iIvalue)) {
StartStore(TalkID::NoMoney);
return;
}

if (!GiveItemToPlayer(Blacksmith.items[idx], false)) {
// Try to give the item to the player
if (!GiveItemToPlayer(*it, false)) {
StartStore(TalkID::NoRoom);
return;
}

tempItem = Blacksmith.items[idx];
tempItem = *it;
StartStore(TalkID::Confirm);
}

Expand Down Expand Up @@ -1981,7 +2016,6 @@ void InitStores()
activeStore = TalkID::None;
isTextFullSize = false;
hasItemList = false;
Blacksmith.itemCount = 0;
Blacksmith.itemLevel = 1;

// Resize the vectors to their intended sizes
Expand Down
2 changes: 0 additions & 2 deletions Source/stores.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ class TownerStore {
// Store();
public:
std::vector<Item> basicItems; // Used for the blacksmith store that only displays non-magical items
int basicItemCount;
std::vector<Item> items;
int itemCount;
uint8_t itemLevel;

//_talker_id townerId;
Expand Down

0 comments on commit bff5579

Please sign in to comment.