diff --git a/dGame/dComponents/InventoryComponent.cpp b/dGame/dComponents/InventoryComponent.cpp index 304524699..28ac0fb93 100644 --- a/dGame/dComponents/InventoryComponent.cpp +++ b/dGame/dComponents/InventoryComponent.cpp @@ -2231,7 +2231,10 @@ void InventoryComponent::OnRemoveItemFromInventory(const GameMessages::RemoveIte return; } - const auto iStackCount = std::min(item->GetCount(), msg.iStackCount); + // As LWOInventoryComponent_Common::msgRemoveItemFromInventory (0x00d203b0): no count means the whole stack, and a + // stack holding fewer than asked is left alone + const auto iStackCount = msg.iStackCount == 0 ? item->GetCount() : msg.iStackCount; + if (item->GetCount() < iStackCount) return; if (msg.bConfirmed && !CanDelete(*item)) { LOG("%llu tried to delete %llu (LOT %i), which its deletion restriction does not allow", m_Parent->GetObjectID(), item->GetId(), item->GetLot()); diff --git a/tests/dGameTests/dComponentsTests/ItemRemovalTests.cpp b/tests/dGameTests/dComponentsTests/ItemRemovalTests.cpp index aaa1e50c5..ad34fee5a 100644 --- a/tests/dGameTests/dComponentsTests/ItemRemovalTests.cpp +++ b/tests/dGameTests/dComponentsTests/ItemRemovalTests.cpp @@ -5,6 +5,7 @@ #include "CDItemComponentTable.h" #include "Entity.h" #include "InventoryComponent.h" +#include "InventoryMessages.h" #include "Item.h" #include "eReplicaComponentType.h" @@ -85,3 +86,31 @@ TEST_F(ItemRemovalTests, RetiredItemsAreFreedWithTheInventory) { // Freed by the inventory's destructor (checked by sanitizer builds) entity.reset(); } + +namespace { + GameMessages::RemoveItemFromInventory Delete(const Item& item, const uint32_t count) { + GameMessages::RemoveItemFromInventory msg; + msg.bConfirmed = true; + msg.eInvType = eInventoryType::ITEMS; + msg.eLootTypeSource = GeneralUtils::ToUnderlying(eLootSourceType::DELETION); + msg.iObjID = item.GetId(); + msg.iObjTemplate = item.GetLot(); + msg.iStackCount = count; + return msg; + } +} + +// LWOInventoryComponent_Common::msgRemoveItemFromInventory (0x00d203b0): a stack holding fewer than asked is left +// alone, and no count means the whole stack +TEST_F(ItemRemovalTests, DeletingMoreThanTheStackHoldsDoesNothing) { + auto* const item = Give(3); + inventoryComponent->OnRemoveItemFromInventory(Delete(*item, 4)); + EXPECT_EQ(item->GetCount(), 3u); + + inventoryComponent->OnRemoveItemFromInventory(Delete(*item, 2)); + EXPECT_EQ(item->GetCount(), 1u); + + auto* const other = Give(5); + inventoryComponent->OnRemoveItemFromInventory(Delete(*other, 0)); + EXPECT_EQ(other->GetCount(), 0u); +}