Browse Source

Don't assume holdItem when auto-placing gold

pull/4125/head
ephphatha 4 years ago committed by Anders Jenbo
parent
commit
f5da32a21c
  1. 36
      Source/inv.cpp
  2. 3
      Source/inv.h
  3. 18
      Source/player.cpp
  4. 2
      test/inv_test.cpp

36
Source/inv.cpp

@ -1499,7 +1499,9 @@ bool AutoPlaceItemInInventorySlot(Player &player, int slotIndex, const Item &ite
return true; return true;
} }
bool GoldAutoPlace(Player &player) bool GoldAutoPlaceInInventorySlot(Player &, int, Item &);
bool GoldAutoPlace(Player &player, Item &goldStack)
{ {
bool done = false; bool done = false;
@ -1509,56 +1511,56 @@ bool GoldAutoPlace(Player &player)
if (player.InvList[i]._ivalue >= MaxGold) if (player.InvList[i]._ivalue >= MaxGold)
continue; continue;
player.InvList[i]._ivalue += player.HoldItem._ivalue; player.InvList[i]._ivalue += goldStack._ivalue;
if (player.InvList[i]._ivalue > MaxGold) { if (player.InvList[i]._ivalue > MaxGold) {
player.HoldItem._ivalue = player.InvList[i]._ivalue - MaxGold; goldStack._ivalue = player.InvList[i]._ivalue - MaxGold;
SetPlrHandGoldCurs(player.HoldItem); SetPlrHandGoldCurs(goldStack);
player.InvList[i]._ivalue = MaxGold; player.InvList[i]._ivalue = MaxGold;
if (gbIsHellfire) if (gbIsHellfire)
GenerateNewSeed(player.HoldItem); GenerateNewSeed(goldStack);
} else { } else {
player.HoldItem._ivalue = 0; goldStack._ivalue = 0;
done = true; done = true;
} }
SetPlrHandGoldCurs(player.InvList[i]); SetPlrHandGoldCurs(player.InvList[i]);
player._pGold = CalculateGold(player);
} }
for (int i = 39; i >= 30 && !done; i--) { 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 x = 9; x >= 0 && !done; x--) {
for (int y = 2; y >= 0 && !done; y--) { 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; return done;
} }
bool GoldAutoPlaceInInventorySlot(Player &player, int slotIndex) bool GoldAutoPlaceInInventorySlot(Player &player, int slotIndex, Item &goldStack)
{ {
if (player.InvGrid[slotIndex] != 0) { if (player.InvGrid[slotIndex] != 0) {
return false; return false;
} }
int ii = player._pNumInv; int ii = player._pNumInv;
player.InvList[ii] = player.HoldItem; player.InvList[ii] = goldStack;
player._pNumInv++; player._pNumInv++;
player.InvGrid[slotIndex] = player._pNumInv; player.InvGrid[slotIndex] = player._pNumInv;
GenerateNewSeed(player.InvList[ii]); GenerateNewSeed(player.InvList[ii]);
int gold = player.HoldItem._ivalue; int gold = goldStack._ivalue;
if (gold > MaxGold) { if (gold > MaxGold) {
gold -= MaxGold; gold -= MaxGold;
player.HoldItem._ivalue = gold; goldStack._ivalue = gold;
GenerateNewSeed(player.HoldItem); GenerateNewSeed(goldStack);
player.InvList[ii]._ivalue = MaxGold; player.InvList[ii]._ivalue = MaxGold;
return false; return false;
} }
player.HoldItem._ivalue = 0; goldStack._ivalue = 0;
player._pGold = CalculateGold(player); player._pGold = CalculateGold(player);
NewCursor(CURSOR_HAND); NewCursor(CURSOR_HAND);
@ -1639,7 +1641,7 @@ void InvGetItem(int pnum, int ii)
CheckBookLevel(player); CheckBookLevel(player);
CheckItemStats(player); CheckItemStats(player);
bool cursorUpdated = false; bool cursorUpdated = false;
if (player.HoldItem._itype == ItemType::Gold && GoldAutoPlace(player)) if (player.HoldItem._itype == ItemType::Gold && GoldAutoPlace(player, player.HoldItem))
cursorUpdated = true; cursorUpdated = true;
CleanupItems(ii); CleanupItems(ii);
pcursitem = -1; pcursitem = -1;
@ -1673,7 +1675,7 @@ void AutoGetItem(int pnum, Item *item, int ii)
CheckItemStats(player); CheckItemStats(player);
SetICursor(player.HoldItem._iCurs + CURSOR_FIRSTITEM); SetICursor(player.HoldItem._iCurs + CURSOR_FIRSTITEM);
if (player.HoldItem._itype == ItemType::Gold) { if (player.HoldItem._itype == ItemType::Gold) {
done = GoldAutoPlace(player); done = GoldAutoPlace(player, player.HoldItem);
if (!done) { if (!done) {
item->_ivalue = player.HoldItem._ivalue; item->_ivalue = player.HoldItem._ivalue;
SetPlrHandGoldCurs(*item); SetPlrHandGoldCurs(*item);

3
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. * @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 AutoPlaceItemInBelt(Player &player, const Item &item, bool persistItem = false);
bool GoldAutoPlace(Player &player); bool GoldAutoPlace(Player &player, Item &goldStack);
bool GoldAutoPlaceInInventorySlot(Player &player, int slotIndex);
void CheckInvSwap(Player &player, inv_body_loc bLoc, int idx, uint16_t wCI, int seed, bool bId, uint32_t dwBuff); 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 inv_update_rem_item(Player &player, inv_body_loc iv);
void CheckInvItem(bool isShiftHeld = false, bool isCtrlHeld = false); void CheckInvItem(bool isShiftHeld = false, bool isCtrlHeld = false);

18
Source/player.cpp

@ -3196,26 +3196,22 @@ StartPlayerKill(int pnum, int earflag)
void StripTopGold(Player &player) void StripTopGold(Player &player)
{ {
Item tmpItem = player.HoldItem;
for (Item &item : InventoryPlayerItemsRange { player }) { for (Item &item : InventoryPlayerItemsRange { player }) {
if (item._itype == ItemType::Gold) { if (item._itype == ItemType::Gold) {
if (item._ivalue > MaxGold) { 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; item._ivalue = MaxGold;
InitializeItem(player.HoldItem, IDI_GOLD); if (!GoldAutoPlace(player, excessGold)) {
SetGoldSeed(player, player.HoldItem); DeadItem(player, excessGold, { 0, 0 });
player.HoldItem._ivalue = val;
SetPlrHandGoldCurs(player.HoldItem);
if (!GoldAutoPlace(player)) {
DeadItem(player, player.HoldItem, { 0, 0 });
player.HoldItem._itype == ItemType::None;
} }
} }
} }
} }
player._pGold = CalculateGold(player); player._pGold = CalculateGold(player);
player.HoldItem = tmpItem;
} }
void ApplyPlrDamage(int pnum, int dam, int minHP /*= 0*/, int frac /*= 0*/, int earflag /*= 0*/) void ApplyPlrDamage(int pnum, int dam, int minHP /*= 0*/, int frac /*= 0*/, int earflag /*= 0*/)

2
test/inv_test.cpp

@ -131,7 +131,7 @@ TEST(Inv, GoldAutoPlace)
Players[MyPlayerId].HoldItem._itype = ItemType::Gold; Players[MyPlayerId].HoldItem._itype = ItemType::Gold;
Players[MyPlayerId].HoldItem._ivalue = GOLD_MAX_LIMIT - 100; Players[MyPlayerId].HoldItem._ivalue = GOLD_MAX_LIMIT - 100;
GoldAutoPlace(Players[MyPlayerId]); GoldAutoPlace(Players[MyPlayerId], Players[MyPlayerId].HoldItem);
// We expect the inventory: // We expect the inventory:
// | 5000 | 900 | ... // | 5000 | 900 | ...
EXPECT_EQ(Players[MyPlayerId].InvList[0]._ivalue, GOLD_MAX_LIMIT); EXPECT_EQ(Players[MyPlayerId].InvList[0]._ivalue, GOLD_MAX_LIMIT);

Loading…
Cancel
Save