fix(vendor): check the item's requirements when it is bought

The vendor window checks an item's reqPrecondition in the vendor buy context
(an unnamed function in LWOVendorComponent, through
LWOItemComponent::StaticCheckUseRequirements) and greys out what
the player does not meet, without a tooltip. The server sold the item
whatever its requirements; a buy that fails them now gets the vendor's
normal PURCHASE_FAIL, for both the vendor and the achievement vendor.

Rows without the vendor buy bit (the thinking hat one, validContexts 252)
still do not stop a purchase; the level ones (249) do.

PreconditionExpression::Check takes the client's bShowTooltip flag, so this
check sends no NotifyClientFailedPrecondition.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-11 06:55:23 -06:00
parent 96a7b64ed1
commit ad2aadc1fe
6 changed files with 40 additions and 3 deletions

View File

@@ -65,7 +65,7 @@ void AchievementVendorComponent::Buy(Entity* buyer, LOT lot, uint32_t count) {
}
auto* inventoryComponent = buyer->GetComponent<InventoryComponent>();
if (!inventoryComponent) {
if (!inventoryComponent || !MeetsBuyRequirements(*buyer, itemComp.reqPrecondition)) {
SendTransactionResult(buyer->GetObjectID(), buyer->GetSystemAddress(), eVendorTransactionResult::PURCHASE_FAIL);
return;
}

View File

@@ -19,6 +19,7 @@
#include "Item.h"
#include "EconomyLedger.h"
#include "VendorPricing.h"
#include "Preconditions.h"
#include <algorithm>
#include <ranges>
@@ -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<CDItemComponentTable>();

View File

@@ -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);

View File

@@ -286,7 +286,7 @@ std::vector<ItemCost> 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();

View File

@@ -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<ItemCost> GetItemCosts(Entity* player) const;

View File

@@ -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)));
}