diff --git a/dGame/dComponents/Vendor/AchievementVendorComponent.cpp b/dGame/dComponents/Vendor/AchievementVendorComponent.cpp index ea2e42612..a92d3da91 100644 --- a/dGame/dComponents/Vendor/AchievementVendorComponent.cpp +++ b/dGame/dComponents/Vendor/AchievementVendorComponent.cpp @@ -65,7 +65,7 @@ void AchievementVendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) { } auto* inventoryComponent = buyer->GetComponent(); - if (!inventoryComponent) { + if (!inventoryComponent || !MeetsBuyRequirements(*buyer, itemComp.reqPrecondition)) { SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL); return; } diff --git a/dGame/dComponents/Vendor/BaseVendorComponent.cpp b/dGame/dComponents/Vendor/BaseVendorComponent.cpp index 0b50fe081..5cd26e6a0 100644 --- a/dGame/dComponents/Vendor/BaseVendorComponent.cpp +++ b/dGame/dComponents/Vendor/BaseVendorComponent.cpp @@ -19,6 +19,7 @@ #include "Item.h" #include "EconomyLedger.h" #include "VendorPricing.h" +#include "Preconditions.h" #include #include @@ -186,6 +187,11 @@ void BaseVendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) { int itemCompID = compRegistryTable->GetByIDAndType(lot, eReplicaComponentType::ITEM); CDItemComponent itemComp = itemComponentTable->GetItemComponentByID(itemCompID); + if (!MeetsBuyRequirements(*buyer, itemComp.reqPrecondition)) { + SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL); + return; + } + // Extra currency that needs to be deducted in case of crafting auto craftingCurrencies = CDItemComponentTable::ParseCraftingCurrencies(itemComp); for (const auto& [crafintCurrencyLOT, crafintCurrencyCount]: craftingCurrencies) { @@ -233,6 +239,11 @@ void BaseVendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) { SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_SUCCESS); } +bool BaseVendorComponent::MeetsBuyRequirements(Entity& buyer, const std::string& reqPrecondition) { + if (reqPrecondition.empty()) return true; + return PreconditionExpression(reqPrecondition).Check(&buyer, PreconditionContext::VendorBuy, false); +} + bool BaseVendorComponent::SetupItem(LOT item) { auto* itemComponentTable = CDClientManager::GetTable(); diff --git a/dGame/dComponents/Vendor/BaseVendorComponent.h b/dGame/dComponents/Vendor/BaseVendorComponent.h index 371fef081..aaa5f8cb1 100644 --- a/dGame/dComponents/Vendor/BaseVendorComponent.h +++ b/dGame/dComponents/Vendor/BaseVendorComponent.h @@ -58,6 +58,11 @@ public: void Buy(Entity* buyer, LOT lot, uint32_t count); + // Whether buyer meets the item's own requirements (reqPrecondition) for buying it. The vendor window checks them + // in the vendor buy context and greys the item out without a tooltip (an unnamed function in LWOVendorComponent, + // LWOItemComponent::StaticCheckUseRequirements). + static bool MeetsBuyRequirements(Entity& buyer, const std::string& reqPrecondition); + // SellToVendor / BuybackFromVendor from player (sent from sysAddr), for the item with object ID itemObjID. void SellToVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count); void BuybackFromVendor(Entity& player, const SystemAddress& sysAddr, LWOOBJID itemObjID, int32_t count); diff --git a/dGame/dUtilities/Preconditions.cpp b/dGame/dUtilities/Preconditions.cpp index 6ce7984a8..a11cd3734 100644 --- a/dGame/dUtilities/Preconditions.cpp +++ b/dGame/dUtilities/Preconditions.cpp @@ -286,7 +286,7 @@ std::vector PreconditionExpression::GetItemCosts(Entity* player) const return costs; } -bool PreconditionExpression::Check(Entity* player, const PreconditionContext context) const { +bool PreconditionExpression::Check(Entity* player, const PreconditionContext context, const bool showTooltip) const { // LWOPreconditionComponent::msgCheckAllPreconditions: every group has to pass, a group passes with // any of its alternatives, and only the alternatives of a failed group are reported bool pass = true; @@ -298,6 +298,7 @@ bool PreconditionExpression::Check(Entity* player, const PreconditionContext con if (groupPasses) continue; pass = false; + if (!showTooltip) continue; for (const auto condition : group) { GameMessages::NotifyClientFailedPrecondition failedPrecondition; failedPrecondition.target = player->GetObjectID(); diff --git a/dGame/dUtilities/Preconditions.h b/dGame/dUtilities/Preconditions.h index ac4551282..cafc7af02 100644 --- a/dGame/dUtilities/Preconditions.h +++ b/dGame/dUtilities/Preconditions.h @@ -102,7 +102,9 @@ class PreconditionExpression final public: explicit PreconditionExpression(const std::string& conditions); - bool Check(Entity* player, PreconditionContext context = PreconditionContext::None) const; + // Whether every group passes. With showTooltip, the player is told which preconditions of a failed group they do + // not meet (NotifyClientFailedPrecondition); the client's CheckUseRequirements has the same flag (bShowTooltip). + bool Check(Entity* player, PreconditionContext context = PreconditionContext::None, bool showTooltip = true) const; // The item costs of the HasItem preconditions in this expression that the player meets std::vector GetItemCosts(Entity* player) const; diff --git a/tests/dGameTests/PreconditionCheckTests.cpp b/tests/dGameTests/PreconditionCheckTests.cpp index fb77f2904..76a68e235 100644 --- a/tests/dGameTests/PreconditionCheckTests.cpp +++ b/tests/dGameTests/PreconditionCheckTests.cpp @@ -4,6 +4,7 @@ #include "CDClientDatabase.h" #include "CDComponentsRegistryTable.h" #include "CDItemComponentTable.h" +#include "BaseVendorComponent.h" #include "Character.h" #include "DestroyableComponent.h" #include "Entity.h" @@ -135,3 +136,20 @@ TEST_F(PreconditionCheckTests, ItemsSoldToAVendorAreNotHeld) { EXPECT_FALSE(Preconditions::Check(player.get(), hasNone)); EXPECT_EQ(Preconditions::Get(hasFive).GetItemCost(player.get()), (ItemCost{ BRICK, 5 })); } + +// Buying from a vendor checks the item's requirements in the vendor buy context: rows without that bit (the thinking +// hat one, 252) do not stop a purchase; the level ones (249) and the everywhere ones do +TEST_F(PreconditionCheckTests, VendorBuyChecksTheItemsRequirementsInItsContext) { + const auto notAtVendors = Row(PreconditionType::HasItem, "6194", 1, 252); + const auto level = Row(PreconditionType::HasItem, "6194", 1, 249); + const auto everywhere = Row(PreconditionType::HasItem, "6194", 1, 255); + + EXPECT_TRUE(BaseVendorComponent::MeetsBuyRequirements(*player, "")); + EXPECT_TRUE(BaseVendorComponent::MeetsBuyRequirements(*player, std::to_string(notAtVendors))); + EXPECT_FALSE(BaseVendorComponent::MeetsBuyRequirements(*player, std::to_string(level))); + EXPECT_FALSE(BaseVendorComponent::MeetsBuyRequirements(*player, std::to_string(notAtVendors) + ";" + std::to_string(everywhere))); + + Give(eInventoryType::ITEMS, 1); + EXPECT_TRUE(BaseVendorComponent::MeetsBuyRequirements(*player, std::to_string(level))); + EXPECT_TRUE(BaseVendorComponent::MeetsBuyRequirements(*player, std::to_string(notAtVendors) + ";" + std::to_string(everywhere))); +}