From c321a8b6de0bc9bed80c56bd7d13f988580934e4 Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Tue, 10 Nov 2026 21:01:33 -0600 Subject: [PATCH] fix(preconditions): items sold to a vendor do not count as held The client's HasItem and DoesNotHaveItem checks count the item with ignoreBuyBack set (LWOPreconditionComponent), so what was sold to a vendor and sits in the buy back list is not held. The count here took every inventory, buy back included; the item cost of a quickbuild is picked with the same count. Co-Authored-By: Claude Fable 5.2 --- dGame/dComponents/InventoryComponent.cpp | 12 ++++++++++++ dGame/dComponents/InventoryComponent.h | 8 ++++++++ dGame/dUtilities/Preconditions.cpp | 5 +++-- tests/dGameTests/PreconditionCheckTests.cpp | 21 +++++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/dGame/dComponents/InventoryComponent.cpp b/dGame/dComponents/InventoryComponent.cpp index 6da299633..12c9ed842 100644 --- a/dGame/dComponents/InventoryComponent.cpp +++ b/dGame/dComponents/InventoryComponent.cpp @@ -203,6 +203,18 @@ uint32_t InventoryComponent::GetLotCount(const LOT lot) const { return count; } +uint32_t InventoryComponent::GetLotCountWithoutBuyBack(const LOT lot) const { + uint32_t count = 0; + + for (const auto& inventory : m_Inventories) { + if (inventory.second->GetType() == eInventoryType::VENDOR_BUYBACK) continue; + + count += inventory.second->GetLotCount(lot); + } + + return count; +} + uint32_t InventoryComponent::GetLotCountNonTransfer(LOT lot, bool includeVault) const { uint32_t count = 0; diff --git a/dGame/dComponents/InventoryComponent.h b/dGame/dComponents/InventoryComponent.h index 02cb71f3a..6ac068444 100644 --- a/dGame/dComponents/InventoryComponent.h +++ b/dGame/dComponents/InventoryComponent.h @@ -142,6 +142,14 @@ public: */ uint32_t GetLotCount(LOT lot) const; + /** + * Returns the amount of items this entity possesses of a certain LOT, leaving out what it sold to a vendor and + * can still buy back: the count the client's precondition check asks for (GetInvItemCount with ignoreBuyBack) + * @param lot the lot to search for + * @return the amount of items of the LOT outside the vendor buy back inventory + */ + uint32_t GetLotCountWithoutBuyBack(LOT lot) const; + /** * Returns the amount of items this entity possesses of a LOT, given that they're not in a temporary inventory * (vendor buyback, vault, etc). diff --git a/dGame/dUtilities/Preconditions.cpp b/dGame/dUtilities/Preconditions.cpp index bd1d68419..6ce7984a8 100644 --- a/dGame/dUtilities/Preconditions.cpp +++ b/dGame/dUtilities/Preconditions.cpp @@ -145,7 +145,8 @@ bool Precondition::CheckValue(Entity* player, const uint32_t value) const { return !inventoryComponent->IsEquipped(value); case PreconditionType::HasItem: case PreconditionType::DoesNotHaveItem: - return ItemCountPasses(type, inventoryComponent->GetLotCount(value), count); + // an unnamed client function counts with ignoreBuyBack set: an item sold to a vendor is no longer held + return ItemCountPasses(type, inventoryComponent->GetLotCountWithoutBuyBack(value), count); case PreconditionType::HasAchievement: if (missionComponent == nullptr) return false; return missionComponent->GetMissionState(value) >= eMissionState::COMPLETE; @@ -271,7 +272,7 @@ std::optional Precondition::GetItemCost(Entity* player) const { if (type != PreconditionType::HasItem || count == 0) return std::nullopt; auto* const inventoryComponent = player->GetComponent(); if (!inventoryComponent) return std::nullopt; - return PickItemCost(values, count, [inventoryComponent](const LOT lot) { return inventoryComponent->GetLotCount(lot); }); + return PickItemCost(values, count, [inventoryComponent](const LOT lot) { return inventoryComponent->GetLotCountWithoutBuyBack(lot); }); } std::vector PreconditionExpression::GetItemCosts(Entity* player) const { diff --git a/tests/dGameTests/PreconditionCheckTests.cpp b/tests/dGameTests/PreconditionCheckTests.cpp index d2bf697f7..fb77f2904 100644 --- a/tests/dGameTests/PreconditionCheckTests.cpp +++ b/tests/dGameTests/PreconditionCheckTests.cpp @@ -114,3 +114,24 @@ TEST_F(PreconditionCheckTests, RacingLicenceOfAPlayerWithoutTheMission) { EXPECT_FALSE(Preconditions::Check(player.get(), Row(PreconditionType::HasRacingLicence, "1", 1, 253))); EXPECT_TRUE(Preconditions::Check(player.get(), Row(PreconditionType::DoesNotHaveRacingLicence, "1", 1, 253))); } + +// Types 2 and 3 count the item everywhere but in the vendor buy back inventory (the client's count sets ignoreBuyBack) +TEST_F(PreconditionCheckTests, ItemsSoldToAVendorAreNotHeld) { + const auto hasFive = Row(PreconditionType::HasItem, "6194", 5, 255); + const auto hasNone = Row(PreconditionType::DoesNotHaveItem, "6194", 1, 255); + + Give(eInventoryType::VENDOR_BUYBACK, 5); + EXPECT_EQ(inventory->GetLotCount(BRICK), 5u); + EXPECT_EQ(inventory->GetLotCountWithoutBuyBack(BRICK), 0u); + EXPECT_FALSE(Preconditions::Check(player.get(), hasFive)); + EXPECT_TRUE(Preconditions::Check(player.get(), hasNone)); + // Nothing is taken as a cost from what the player no longer holds + EXPECT_FALSE(Preconditions::Get(hasFive).GetItemCost(player.get()).has_value()); + + Give(eInventoryType::ITEMS, 3); + Give(eInventoryType::VAULT_ITEMS, 2); + EXPECT_EQ(inventory->GetLotCountWithoutBuyBack(BRICK), 5u); + EXPECT_TRUE(Preconditions::Check(player.get(), hasFive)); + EXPECT_FALSE(Preconditions::Check(player.get(), hasNone)); + EXPECT_EQ(Preconditions::Get(hasFive).GetItemCost(player.get()), (ItemCost{ BRICK, 5 })); +}