Skip to content

Commit

Permalink
Make functions work with new vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
kphoenix137 committed Sep 27, 2024
1 parent bff5579 commit 1c94d2d
Showing 1 changed file with 53 additions and 48 deletions.
101 changes: 53 additions & 48 deletions Source/items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1891,7 +1891,7 @@ _item_indexes RndSmithItem(const Player &player, int lvl)
return RndVendorItem<SmithItemOk, true>(player, 0, lvl);
}

// KPHOENIX: Move to stores.cpp
// FIXME: Move to stores.cpp
void SortVendor(std::vector<Item> &itemList, size_t startIndex = 0)
{
// Boundary check
Expand Down Expand Up @@ -4389,6 +4389,9 @@ void SpawnSmith(int lvl)

int iCnt = RandomIntBetween(10, maxItems);

// Resize the vector to accommodate the required number of items
Blacksmith.basicItems.resize(iCnt);

for (int i = 0; i < iCnt; i++) {
Item &newItem = Blacksmith.basicItems[i];

Expand All @@ -4404,8 +4407,10 @@ void SpawnSmith(int lvl)
newItem._iIdentified = true;
}

for (int i = iCnt; i < NumSmithBasicItems; i++)
Blacksmith.basicItems[i].clear();
// Clear any excess items beyond the number we need
if (iCnt < Blacksmith.basicItems.size()) {
Blacksmith.basicItems.erase(Blacksmith.basicItems.begin() + iCnt, Blacksmith.basicItems.end());
}

SortVendor(Blacksmith.basicItems, PinnedItemCount);
}
Expand Down Expand Up @@ -4467,56 +4472,56 @@ void SpawnWitch(int lvl)
const int itemCount = RandomIntBetween(10, gbIsHellfire ? NumWitchItemsHf : NumWitchItems);
const int maxValue = gbIsHellfire ? 200000 : 140000;

for (int i = 0; i < itemCount; i++) {
Item &item = Witch.items[i];
item = {};
// Reserve space if necessary to optimize performance when filling the vector
Witch.items.reserve(itemCount);

if (i < PinnedItemCount) {
item._iSeed = AdvanceRndSeed();
GetItemAttrs(item, PinnedItemTypes[i], 1);
item._iCreateInfo = lvl;
item._iStatFlag = true;
continue;
}
for (int i = 0; i < itemCount; ++i) {
Item newItem = {};

if (gbIsHellfire) {
if (i < PinnedItemCount + MaxPinnedBookCount && bookCount < pinnedBookCount) {
_item_indexes bookType = PinnedBookTypes[i - PinnedItemCount];
if (lvl >= AllItemsList[bookType].iMinMLvl) {
item._iSeed = AdvanceRndSeed();
SetRndSeed(item._iSeed);
DiscardRandomValues(1);
GetItemAttrs(item, bookType, lvl);
item._iCreateInfo = lvl | CF_WITCH;
item._iIdentified = true;
bookCount++;
continue;
}
if (i < PinnedItemCount) {
newItem._iSeed = AdvanceRndSeed();
GetItemAttrs(newItem, PinnedItemTypes[i], 1);
newItem._iCreateInfo = lvl;
newItem._iStatFlag = true;
} else if (gbIsHellfire && i < PinnedItemCount + MaxPinnedBookCount && bookCount < pinnedBookCount) {
_item_indexes bookType = PinnedBookTypes[i - PinnedItemCount];
if (lvl >= AllItemsList[bookType].iMinMLvl) {
newItem._iSeed = AdvanceRndSeed();
SetRndSeed(newItem._iSeed);
DiscardRandomValues(1);
GetItemAttrs(newItem, bookType, lvl);
newItem._iCreateInfo = lvl | CF_WITCH;
newItem._iIdentified = true;
bookCount++;
} else {
continue;
}
} else {
do {
newItem = {};
newItem._iSeed = AdvanceRndSeed();
SetRndSeed(newItem._iSeed);
_item_indexes itemData = RndWitchItem(*MyPlayer, lvl);
GetItemAttrs(newItem, itemData, lvl);
int maxlvl = -1;
if (GenerateRnd(100) <= 5)
maxlvl = 2 * lvl;
if (maxlvl == -1 && newItem._iMiscId == IMISC_STAFF)
maxlvl = 2 * lvl;
if (maxlvl != -1)
GetItemBonus(*MyPlayer, newItem, maxlvl / 2, maxlvl, true, true);
} while (newItem._iIvalue > maxValue);

newItem._iCreateInfo = lvl | CF_WITCH;
newItem._iIdentified = true;
}

if (i >= itemCount) {
item.clear();
continue;
}
Witch.items.push_back(std::move(newItem));
}

do {
item = {};
item._iSeed = AdvanceRndSeed();
SetRndSeed(item._iSeed);
_item_indexes itemData = RndWitchItem(*MyPlayer, lvl);
GetItemAttrs(item, itemData, lvl);
int maxlvl = -1;
if (GenerateRnd(100) <= 5)
maxlvl = 2 * lvl;
if (maxlvl == -1 && item._iMiscId == IMISC_STAFF)
maxlvl = 2 * lvl;
if (maxlvl != -1)
GetItemBonus(*MyPlayer, item, maxlvl / 2, maxlvl, true, true);
} while (item._iIvalue > maxValue);

item._iCreateInfo = lvl | CF_WITCH;
item._iIdentified = true;
// Remove any excess items beyond itemCount if vector contains more
if (Witch.items.size() > itemCount) {
Witch.items.erase(Witch.items.begin() + itemCount, Witch.items.end());
}

SortVendor(Witch.items, PinnedItemCount);
Expand All @@ -4538,7 +4543,7 @@ void SpawnBoy(int lvl)
dexterity += dexterity / 5;
magic += magic / 5;

if (Boy.itemLevel >= (lvl / 2) && !Boy.items[0].isEmpty()) // FIXME: Make this work properly with vectors
if (Boy.itemLevel >= (lvl / 2) && !Boy.items[0].isEmpty())
return;
do {
keepgoing = false;
Expand Down

0 comments on commit 1c94d2d

Please sign in to comment.