diff --git a/Source/inv.cpp b/Source/inv.cpp index 20d654522..9531a6536 100644 --- a/Source/inv.cpp +++ b/Source/inv.cpp @@ -1499,7 +1499,9 @@ bool AutoPlaceItemInInventorySlot(Player &player, int slotIndex, const Item &ite return true; } -bool GoldAutoPlace(Player &player) +bool GoldAutoPlaceInInventorySlot(Player &, int, Item &); + +bool GoldAutoPlace(Player &player, Item &goldStack) { bool done = false; @@ -1509,56 +1511,56 @@ bool GoldAutoPlace(Player &player) if (player.InvList[i]._ivalue >= MaxGold) continue; - player.InvList[i]._ivalue += player.HoldItem._ivalue; + player.InvList[i]._ivalue += goldStack._ivalue; if (player.InvList[i]._ivalue > MaxGold) { - player.HoldItem._ivalue = player.InvList[i]._ivalue - MaxGold; - SetPlrHandGoldCurs(player.HoldItem); + goldStack._ivalue = player.InvList[i]._ivalue - MaxGold; + SetPlrHandGoldCurs(goldStack); player.InvList[i]._ivalue = MaxGold; if (gbIsHellfire) - GenerateNewSeed(player.HoldItem); + GenerateNewSeed(goldStack); } else { - player.HoldItem._ivalue = 0; + goldStack._ivalue = 0; done = true; } SetPlrHandGoldCurs(player.InvList[i]); - player._pGold = CalculateGold(player); } for (int i = 39; i >= 30 && !done; i--) { - done = GoldAutoPlaceInInventorySlot(player, i); + done = GoldAutoPlaceInInventorySlot(player, i, goldStack); } for (int x = 9; x >= 0 && !done; x--) { for (int y = 2; y >= 0 && !done; y--) { - done = GoldAutoPlaceInInventorySlot(player, 10 * y + x); + done = GoldAutoPlaceInInventorySlot(player, 10 * y + x, goldStack); } } + player._pGold = CalculateGold(player); return done; } -bool GoldAutoPlaceInInventorySlot(Player &player, int slotIndex) +bool GoldAutoPlaceInInventorySlot(Player &player, int slotIndex, Item &goldStack) { if (player.InvGrid[slotIndex] != 0) { return false; } int ii = player._pNumInv; - player.InvList[ii] = player.HoldItem; + player.InvList[ii] = goldStack; player._pNumInv++; player.InvGrid[slotIndex] = player._pNumInv; GenerateNewSeed(player.InvList[ii]); - int gold = player.HoldItem._ivalue; + int gold = goldStack._ivalue; if (gold > MaxGold) { gold -= MaxGold; - player.HoldItem._ivalue = gold; - GenerateNewSeed(player.HoldItem); + goldStack._ivalue = gold; + GenerateNewSeed(goldStack); player.InvList[ii]._ivalue = MaxGold; return false; } - player.HoldItem._ivalue = 0; + goldStack._ivalue = 0; player._pGold = CalculateGold(player); NewCursor(CURSOR_HAND); @@ -1639,7 +1641,7 @@ void InvGetItem(int pnum, int ii) CheckBookLevel(player); CheckItemStats(player); bool cursorUpdated = false; - if (player.HoldItem._itype == ItemType::Gold && GoldAutoPlace(player)) + if (player.HoldItem._itype == ItemType::Gold && GoldAutoPlace(player, player.HoldItem)) cursorUpdated = true; CleanupItems(ii); pcursitem = -1; @@ -1673,7 +1675,7 @@ void AutoGetItem(int pnum, Item *item, int ii) CheckItemStats(player); SetICursor(player.HoldItem._iCurs + CURSOR_FIRSTITEM); if (player.HoldItem._itype == ItemType::Gold) { - done = GoldAutoPlace(player); + done = GoldAutoPlace(player, player.HoldItem); if (!done) { item->_ivalue = player.HoldItem._ivalue; SetPlrHandGoldCurs(*item); diff --git a/Source/inv.h b/Source/inv.h index 37d0a4b12..817fe555d 100644 --- a/Source/inv.h +++ b/Source/inv.h @@ -148,8 +148,7 @@ bool AutoPlaceItemInInventorySlot(Player &player, int slotIndex, const Item &ite * @return 'True' in case the item can be placed on the player's belt and 'False' otherwise. */ bool AutoPlaceItemInBelt(Player &player, const Item &item, bool persistItem = false); -bool GoldAutoPlace(Player &player); -bool GoldAutoPlaceInInventorySlot(Player &player, int slotIndex); +bool GoldAutoPlace(Player &player, Item &goldStack); void CheckInvSwap(Player &player, inv_body_loc bLoc, int idx, uint16_t wCI, int seed, bool bId, uint32_t dwBuff); void inv_update_rem_item(Player &player, inv_body_loc iv); void CheckInvItem(bool isShiftHeld = false, bool isCtrlHeld = false); diff --git a/Source/player.cpp b/Source/player.cpp index 51b5ebe56..8dc0627b5 100644 --- a/Source/player.cpp +++ b/Source/player.cpp @@ -3196,26 +3196,22 @@ StartPlayerKill(int pnum, int earflag) void StripTopGold(Player &player) { - Item tmpItem = player.HoldItem; - for (Item &item : InventoryPlayerItemsRange { player }) { if (item._itype == ItemType::Gold) { if (item._ivalue > MaxGold) { - int val = item._ivalue - MaxGold; + Item excessGold; + InitializeItem(excessGold, IDI_GOLD); + SetGoldSeed(player, excessGold); + excessGold._ivalue = item._ivalue - MaxGold; + SetPlrHandGoldCurs(excessGold); item._ivalue = MaxGold; - InitializeItem(player.HoldItem, IDI_GOLD); - SetGoldSeed(player, player.HoldItem); - player.HoldItem._ivalue = val; - SetPlrHandGoldCurs(player.HoldItem); - if (!GoldAutoPlace(player)) { - DeadItem(player, player.HoldItem, { 0, 0 }); - player.HoldItem._itype == ItemType::None; + if (!GoldAutoPlace(player, excessGold)) { + DeadItem(player, excessGold, { 0, 0 }); } } } } player._pGold = CalculateGold(player); - player.HoldItem = tmpItem; } void ApplyPlrDamage(int pnum, int dam, int minHP /*= 0*/, int frac /*= 0*/, int earflag /*= 0*/) diff --git a/test/inv_test.cpp b/test/inv_test.cpp index 2cd8ea107..25b888157 100644 --- a/test/inv_test.cpp +++ b/test/inv_test.cpp @@ -131,7 +131,7 @@ TEST(Inv, GoldAutoPlace) Players[MyPlayerId].HoldItem._itype = ItemType::Gold; Players[MyPlayerId].HoldItem._ivalue = GOLD_MAX_LIMIT - 100; - GoldAutoPlace(Players[MyPlayerId]); + GoldAutoPlace(Players[MyPlayerId], Players[MyPlayerId].HoldItem); // We expect the inventory: // | 5000 | 900 | ... EXPECT_EQ(Players[MyPlayerId].InvList[0]._ivalue, GOLD_MAX_LIMIT);