Skip to content

Commit b47d97f

Browse files
Rebase of #7876 - Shift item order when selling to keep consistent window order (#7886)
* Shift item order when selling instead of using last item to fill the slot Player has items i1, i2, i3, picked up in that order. Previous Behavior: 1. Sell window at vendor shows i1, i2, i3. 2. Player sells i1 3. i3 is last in list and replaces i1 4. Sell window now shows i3, i2. New Behavior: 1. Sell window at vendor shows i1, i2, i3. 2. Player sells i1 3. i2/i3 are shifted in index position down to fill the gap left by previous index. 4. Sell window now shows i2, i3. * Remove whitespace
1 parent b72327b commit b47d97f

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

Source/inv.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ void AddItemToInvGrid(Player &player, int invGridIndex, int invListIndex, Size i
148148
if (x == 0 && y == itemSize.height - 1)
149149
player.InvGrid[rowGridIndex + x] = invListIndex;
150150
else
151-
player.InvGrid[rowGridIndex + x] = -invListIndex;
151+
player.InvGrid[rowGridIndex + x] = -invListIndex; // use negative index to denote it's occupied but it's not the top-left cell.
152152
}
153153
}
154154

Source/player.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -1558,16 +1558,18 @@ void Player::RemoveInvItem(int iv, bool calcScrolls)
15581558

15591559
_pNumInv--;
15601560

1561-
// If the item at the end of inventory array isn't the one we removed, we need to swap its position in the array with the removed item
1561+
// If the item at the end of inventory array isn't the one removed, shift all following items back one index to retain inventory order.
15621562
if (_pNumInv > 0 && _pNumInv != iv) {
1563-
InvList[iv] = InvList[_pNumInv].pop();
1563+
for (size_t newIndex = iv; newIndex < _pNumInv; newIndex++) {
1564+
InvList[newIndex] = InvList[newIndex + 1].pop();
1565+
}
15641566

15651567
for (int8_t &itemIndex : InvGrid) {
1566-
if (itemIndex == _pNumInv + 1) {
1567-
itemIndex = iv + 1;
1568+
if (itemIndex > iv + 1) { // if item was shifted, decrease the index so it's paired with the correct item.
1569+
itemIndex--;
15681570
}
1569-
if (itemIndex == -(_pNumInv + 1)) {
1570-
itemIndex = -(iv + 1);
1571+
if (itemIndex < -(iv + 1)) {
1572+
itemIndex++; // since occupied cells are negative, increment the index to keep it same as as top-left cell for item, only negative.
15711573
}
15721574
}
15731575
}

test/inv_test.cpp

+48-6
Original file line numberDiff line numberDiff line change
@@ -180,28 +180,70 @@ TEST_F(InvTest, RemoveInvItem)
180180
EXPECT_EQ(MyPlayer->_pNumInv, 0);
181181
}
182182

183-
// Test removing an item from inventory with other items in it.
184-
TEST_F(InvTest, RemoveInvItem_other_item)
183+
// Test removing an item from middle of inventory list.
184+
TEST_F(InvTest, RemoveInvItem_shiftsListFromMiddle)
185185
{
186186
SNetInitializeProvider(SELCONN_LOOPBACK, nullptr);
187187

188188
clear_inventory();
189-
// Put a two-slot misc item and a ring into the inventory:
190-
// | (item) | (item) | (ring) | ...
191-
MyPlayer->_pNumInv = 2;
189+
// Put a two-slot misc item and a ring into the inventory, followed by another two-slot misc item:
190+
// | (item) | (item) | (ring) | (item) | (item) | ...
191+
MyPlayer->_pNumInv = 3;
192192
MyPlayer->InvGrid[0] = 1;
193193
MyPlayer->InvGrid[1] = -1;
194194
MyPlayer->InvList[0]._itype = ItemType::Misc;
195195

196196
MyPlayer->InvGrid[2] = 2;
197197
MyPlayer->InvList[1]._itype = ItemType::Ring;
198198

199+
MyPlayer->InvGrid[3] = 3;
200+
MyPlayer->InvGrid[4] = -3;
201+
MyPlayer->InvList[2]._itype = ItemType::Misc;
202+
203+
MyPlayer->RemoveInvItem(1);
204+
EXPECT_EQ(MyPlayer->InvGrid[0], 1);
205+
EXPECT_EQ(MyPlayer->InvGrid[1], -1);
206+
EXPECT_EQ(MyPlayer->InvGrid[2], 0);
207+
EXPECT_EQ(MyPlayer->InvGrid[3], 2);
208+
EXPECT_EQ(MyPlayer->InvGrid[4], -2);
209+
210+
EXPECT_EQ(MyPlayer->InvList[0]._itype, ItemType::Misc);
211+
EXPECT_EQ(MyPlayer->InvList[1]._itype, ItemType::Misc);
212+
213+
EXPECT_EQ(MyPlayer->_pNumInv, 2);
214+
}
215+
216+
// Test removing an item from front of inventory list.
217+
TEST_F(InvTest, RemoveInvItem_shiftsListFromFront)
218+
{
219+
SNetInitializeProvider(SELCONN_LOOPBACK, nullptr);
220+
221+
clear_inventory();
222+
// Put a two-slot misc item and a ring into the inventory, followed by another two-slot misc item:
223+
// | (item) | (item) | (ring) | (item) | (item) | ...
224+
MyPlayer->_pNumInv = 3;
225+
MyPlayer->InvGrid[0] = 1;
226+
MyPlayer->InvGrid[1] = -1;
227+
MyPlayer->InvList[0]._itype = ItemType::Misc;
228+
229+
MyPlayer->InvGrid[2] = 2;
230+
MyPlayer->InvList[1]._itype = ItemType::Ring;
231+
232+
MyPlayer->InvGrid[3] = 3;
233+
MyPlayer->InvGrid[4] = -3;
234+
MyPlayer->InvList[2]._itype = ItemType::Misc;
235+
199236
MyPlayer->RemoveInvItem(0);
200237
EXPECT_EQ(MyPlayer->InvGrid[0], 0);
201238
EXPECT_EQ(MyPlayer->InvGrid[1], 0);
202239
EXPECT_EQ(MyPlayer->InvGrid[2], 1);
240+
EXPECT_EQ(MyPlayer->InvGrid[3], 2);
241+
EXPECT_EQ(MyPlayer->InvGrid[4], -2);
242+
203243
EXPECT_EQ(MyPlayer->InvList[0]._itype, ItemType::Ring);
204-
EXPECT_EQ(MyPlayer->_pNumInv, 1);
244+
EXPECT_EQ(MyPlayer->InvList[1]._itype, ItemType::Misc);
245+
246+
EXPECT_EQ(MyPlayer->_pNumInv, 2);
205247
}
206248

207249
// Test removing an item from the belt

0 commit comments

Comments
 (0)