fix(preconditions): a precondition only applies in the contexts its row names

LWOPreconditionComponent::CheckPrecondition is given the context
of the check and passes a precondition whose validContexts does not have that
context's bit: 0 buying from a vendor, 1 a vendor listing its items, 2
equipping an item that is bound to the player, and none (-1) for everything
else, which leaves out the rows that only name those three (a value under 5).
A row with no context at all fails. Every row was checked everywhere here.

Two things change:

- Preconditions 338 and 339 (mission 1926 / 1918 completed, validContexts 3)
  are for vendors only and no longer stop an item from being used or equipped.
- A bound item was equipped without any of its requirements. It is now checked
  in the bound item context, as the client checks it before asking: only the
  level requirements (validContexts 249) are left out.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-10 21:00:03 -06:00
parent 05072948c5
commit 3f3d74a6a2
6 changed files with 91 additions and 15 deletions

View File

@@ -1179,7 +1179,10 @@ void InventoryComponent::EquipItem(Item* item, const bool skipChecks) {
if (!building && (item->GetLot() == 6086 || type == eItemType::LOOT_MODEL || type == eItemType::VEHICLE)) return;
if (type != eItemType::LOOT_MODEL && type != eItemType::MODEL) {
if (!item->GetBound() && !item->GetPreconditionExpression()->Check(m_Parent)) {
// A bound item is checked in its own context (LWOItemComponent, IsItemEquippable): the requirements that
// are not for it, the level ones, no longer count once the item is the player's
const auto context = item->GetBound() ? PreconditionContext::BoundItem : PreconditionContext::None;
if (!item->GetPreconditionExpression()->Check(m_Parent, context)) {
return;
}
}

View File

@@ -20,7 +20,7 @@ std::map<uint32_t, Precondition*> Preconditions::cache = {};
Precondition::Precondition(const uint32_t condition) {
auto query = CDClientDatabase::CreatePreppedStmt(
"SELECT type, targetLOT, targetCount FROM Preconditions WHERE id = ?;");
"SELECT type, targetLOT, targetCount, validContexts FROM Preconditions WHERE id = ?;");
query.bind(1, static_cast<int>(condition));
auto result = query.execQuery();
@@ -50,11 +50,28 @@ Precondition::Precondition(const uint32_t condition) {
this->count = result.fieldIsNull("targetCount") ? 1 : result.getIntField("targetCount");
// The client reads a missing value as 0, which fails the precondition
this->validContexts = static_cast<uint64_t>(result.getInt64Field("validContexts", 0));
result.finalize();
}
bool Precondition::Check(Entity* player) const {
bool Precondition::AppliesIn(const uint64_t validContexts, const PreconditionContext context) {
const auto value = static_cast<int32_t>(context);
if (value < 0) return validContexts == ~0ULL || validContexts >= 5;
// The client tests a 32 bit flag against the 64 bit value, sign extended
const auto flag = static_cast<int32_t>(1U << (value & 31));
return (validContexts & static_cast<uint64_t>(static_cast<int64_t>(flag))) != 0;
}
bool Precondition::Check(Entity* player, const PreconditionContext context) const {
// LWOPreconditionComponent::CheckPrecondition: a row with no context at all fails ("Must be set
// greater than zero"), and a row that is not for this context passes without being looked at
if (validContexts == 0) return false;
if (!AppliesIn(validContexts, context)) return true;
if (values.empty()) {
return true; // There are very few of these
}
@@ -258,14 +275,14 @@ std::vector<ItemCost> PreconditionExpression::GetItemCosts(Entity* player) const
return costs;
}
bool PreconditionExpression::Check(Entity* player) const {
bool PreconditionExpression::Check(Entity* player, const PreconditionContext context) 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;
for (const auto& group : m_Groups) {
bool groupPasses = false;
for (const auto condition : group) {
if (Preconditions::Check(player, condition)) groupPasses = true;
if (Preconditions::Check(player, condition, context)) groupPasses = true;
}
if (groupPasses) continue;
@@ -291,8 +308,8 @@ const Precondition& Preconditions::Get(const uint32_t condition) {
return *precondition;
}
bool Preconditions::Check(Entity* player, const uint32_t condition) {
return Get(condition).Check(player);
bool Preconditions::Check(Entity* player, const uint32_t condition, const PreconditionContext context) {
return Get(condition).Check(player, context);
}

View File

@@ -35,6 +35,15 @@ enum class PreconditionType
};
// The situation a precondition is checked in. A Preconditions row's validContexts has one bit for each context it
// applies in (LWOPreconditionComponent::CheckPrecondition).
enum class PreconditionContext : int32_t {
None = -1, // using or equipping an item, and every check that is not about an item
VendorBuy = 0,
VendorList = 1,
BoundItem = 2 // equipping an item that is bound to the player
};
struct ItemCost {
LOT lot{ LOT_NULL };
uint32_t count{ 0 };
@@ -46,7 +55,12 @@ class Precondition final
public:
explicit Precondition(uint32_t condition);
bool Check(Entity* player) const;
bool Check(Entity* player, PreconditionContext context = PreconditionContext::None) const;
// Whether a row with these validContexts is checked in a context; where it is not, the precondition passes.
// With a context, its bit has to be set. Without one, rows that only name the two vendor contexts or the bound
// item one (a value under 5) are left out.
static bool AppliesIn(uint64_t validContexts, PreconditionContext context);
// The items a HasItem precondition takes as a cost (a quickbuild's): the first of its LOTs the player has enough
// of, and how many. Nothing for other types or when the player has none of them.
@@ -69,6 +83,9 @@ private:
std::vector<uint32_t> values;
uint32_t count;
// Every context, for a row that does not say
uint64_t validContexts{ ~0ULL };
};
@@ -77,7 +94,7 @@ class PreconditionExpression final
public:
explicit PreconditionExpression(const std::string& conditions);
bool Check(Entity* player) const;
bool Check(Entity* player, PreconditionContext context = PreconditionContext::None) const;
// The item costs of the HasItem preconditions in this expression that the player meets
std::vector<ItemCost> GetItemCosts(Entity* player) const;
@@ -95,7 +112,7 @@ private:
class Preconditions final
{
public:
static bool Check(Entity* player, uint32_t condition);
static bool Check(Entity* player, uint32_t condition, PreconditionContext context = PreconditionContext::None);
static const Precondition& Get(uint32_t condition);

View File

@@ -72,3 +72,42 @@ TEST(PreconditionItemCostTests, ListIsGroupsOfAlternatives) {
EXPECT_EQ(PreconditionExpression::Parse("1,2:3 ; 4"), (Groups{ { 1 }, { 2 }, { 3 }, { 4 } }));
EXPECT_EQ(PreconditionExpression::Parse(";5;"), (Groups{ { 5 } }));
}
// LWOPreconditionComponent::CheckPrecondition: with a context, a row is checked when its validContexts
// has the context's bit; where it is not checked, the precondition passes. The table holds 255, 253, 252, 249 and 3.
TEST(PreconditionItemCostTests, ValidContextsWithAContext) {
using enum PreconditionContext;
for (const auto context : { VendorBuy, VendorList, BoundItem }) EXPECT_TRUE(Precondition::AppliesIn(255, context));
// 253: not while a vendor lists its items
EXPECT_TRUE(Precondition::AppliesIn(253, VendorBuy));
EXPECT_FALSE(Precondition::AppliesIn(253, VendorList));
EXPECT_TRUE(Precondition::AppliesIn(253, BoundItem));
// 252: not at a vendor (the thinking hat and build mode requirements of bricks and models)
EXPECT_FALSE(Precondition::AppliesIn(252, VendorBuy));
EXPECT_FALSE(Precondition::AppliesIn(252, VendorList));
EXPECT_TRUE(Precondition::AppliesIn(252, BoundItem));
// 249: the level requirements, which a vendor's list and an item bound to the player leave out
EXPECT_TRUE(Precondition::AppliesIn(249, VendorBuy));
EXPECT_FALSE(Precondition::AppliesIn(249, VendorList));
EXPECT_FALSE(Precondition::AppliesIn(249, BoundItem));
// 3: at a vendor only
EXPECT_TRUE(Precondition::AppliesIn(3, VendorBuy));
EXPECT_TRUE(Precondition::AppliesIn(3, VendorList));
EXPECT_FALSE(Precondition::AppliesIn(3, BoundItem));
// Every bit set
EXPECT_TRUE(Precondition::AppliesIn(~0ULL, BoundItem));
EXPECT_TRUE(Precondition::AppliesIn(~0ULL, static_cast<PreconditionContext>(40)));
// The flag is 32 bits wide, taken from the context's low 5 bits and sign extended
EXPECT_TRUE(Precondition::AppliesIn(1ULL << 31, static_cast<PreconditionContext>(31)));
EXPECT_TRUE(Precondition::AppliesIn(1ULL << 40, static_cast<PreconditionContext>(31)));
EXPECT_FALSE(Precondition::AppliesIn(1ULL << 40, static_cast<PreconditionContext>(8)));
EXPECT_TRUE(Precondition::AppliesIn(1ULL << 8, static_cast<PreconditionContext>(40)));
}
// Without a context every row is checked, except those that only name the vendor or bound item contexts (under 5):
// preconditions 338 and 339 (a mission completed, validContexts 3) do not stop an item from being used
TEST(PreconditionItemCostTests, ValidContextsWithoutAContext) {
using enum PreconditionContext;
for (const uint64_t all : { 255ULL, 253ULL, 252ULL, 249ULL, 5ULL, 8ULL, ~0ULL }) EXPECT_TRUE(Precondition::AppliesIn(all, None)) << all;
for (const uint64_t few : { 1ULL, 2ULL, 3ULL, 4ULL }) EXPECT_FALSE(Precondition::AppliesIn(few, None)) << few;
}

View File

@@ -793,9 +793,9 @@ TEST_F(DestroyableTest, RequestResurrectOnlyWhenDead) {
// whatever its factions
TEST_F(DestroyableTest, EnemyPreconditionDecidesEnemies) {
if (!CDClientDatabase::isConnected) CDClientDatabase::Connect(":memory:");
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS Preconditions (id INTEGER, type INTEGER, targetLOT TEXT, targetCount INTEGER);");
CDClientDatabase::ExecuteDML("INSERT INTO Preconditions VALUES (90201, 2, '6194', 1);"); // has an item the asker lacks
CDClientDatabase::ExecuteDML("INSERT INTO Preconditions VALUES (90202, 2, '', 1);"); // always met
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS Preconditions (id INTEGER, type INTEGER, targetLOT TEXT, targetCount INTEGER, validContexts INTEGER);");
CDClientDatabase::ExecuteDML("INSERT INTO Preconditions VALUES (90201, 2, '6194', 1, 255);"); // has an item the asker lacks
CDClientDatabase::ExecuteDML("INSERT INTO Preconditions VALUES (90202, 2, '', 1, 255);"); // always met
Entity target(0x0102030405060709LL, info);
auto* targetDestroyable = target.AddComponent<DestroyableComponent>(-1);

View File

@@ -217,8 +217,8 @@ TEST_F(LootMetricsTests, QuickbuildTakesItsCostAsTheSource) {
constexpr LOT BRICK = 6194;
constexpr int32_t PRECONDITION = 90099; // stands in for 99: have 5 of LOT 6194
RegisterLot(BRICK);
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS Preconditions (id INTEGER, type INTEGER, targetLOT TEXT, targetCount INTEGER);");
CDClientDatabase::ExecuteDML("INSERT INTO Preconditions VALUES (" + std::to_string(PRECONDITION) + ", 2, '6194', 5);");
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS Preconditions (id INTEGER, type INTEGER, targetLOT TEXT, targetCount INTEGER, validContexts INTEGER);");
CDClientDatabase::ExecuteDML("INSERT INTO Preconditions VALUES (" + std::to_string(PRECONDITION) + ", 2, '6194', 5, 255);");
inventory->AddItem(BRICK, 7, eLootSourceType::NONE);
// What the precondition check looks at besides the inventory
entity->AddComponent<MissionComponent>(-1);