Files
DarkflameServer/tests/dGameTests/PreconditionItemCostTests.cpp
Aaron Kimbrell 3f3d74a6a2 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>
2026-11-11 01:02:35 -06:00

114 lines
6.4 KiB
C++

#include "Preconditions.h"
#include <map>
#include <gtest/gtest.h>
// The items a quickbuild's HasItem precondition takes when the build starts
// (the FV Stone Warrior pedestal: precondition 99, 5 of LOT 6194).
namespace {
std::function<uint32_t(LOT)> Holding(const std::map<LOT, uint32_t>& inventory) {
return [inventory](const LOT lot) {
const auto it = inventory.find(lot);
return it == inventory.end() ? 0u : it->second;
};
}
}
TEST(PreconditionItemCostTests, TakesTheCountOfTheHeldLot) {
EXPECT_EQ(Precondition::PickItemCost({ 6194 }, 5, Holding({ { 6194, 7 } })), (ItemCost{ 6194, 5 }));
EXPECT_EQ(Precondition::PickItemCost({ 6194 }, 5, Holding({ { 6194, 5 } })), (ItemCost{ 6194, 5 }));
}
TEST(PreconditionItemCostTests, NotEnoughTakesNothing) {
EXPECT_FALSE(Precondition::PickItemCost({ 6194 }, 5, Holding({ { 6194, 4 } })));
EXPECT_FALSE(Precondition::PickItemCost({ 6194 }, 5, Holding({})));
}
// A precondition that lists several LOTs is met by any of them: the first one held is taken
TEST(PreconditionItemCostTests, TakesTheFirstHeldOfSeveralLots) {
EXPECT_EQ(Precondition::PickItemCost({ 100, 200, 300 }, 2, Holding({ { 100, 1 }, { 200, 2 }, { 300, 9 } })), (ItemCost{ 200, 2 }));
}
// Precondition 3 (DoesNotHaveItem, LOT 2633, count 1) is met while the player holds none of the item: the client
// negates the HasItem count check for it
TEST(PreconditionItemCostTests, DoesNotHaveItemIsTheOppositeOfHasItem) {
EXPECT_TRUE(Precondition::ItemCountPasses(PreconditionType::DoesNotHaveItem, 0, 1));
EXPECT_FALSE(Precondition::ItemCountPasses(PreconditionType::DoesNotHaveItem, 1, 1));
EXPECT_FALSE(Precondition::ItemCountPasses(PreconditionType::DoesNotHaveItem, 3, 1));
EXPECT_TRUE(Precondition::ItemCountPasses(PreconditionType::DoesNotHaveItem, 4, 5));
EXPECT_FALSE(Precondition::ItemCountPasses(PreconditionType::HasItem, 0, 1));
EXPECT_TRUE(Precondition::ItemCountPasses(PreconditionType::HasItem, 1, 1));
EXPECT_FALSE(Precondition::ItemCountPasses(PreconditionType::HasItem, 4, 5));
}
// The client's list checks stop at the first value that meets them; the "not" types negate that, so every value
// has to pass. Precondition 66 (DoesNotHaveFaction "1,33") fails for a player of faction 1.
TEST(PreconditionItemCostTests, NotTypesNeedEveryValue) {
for (const auto type : { PreconditionType::ItemNotEquipped, PreconditionType::DoesNotHaveItem, PreconditionType::DoesNotHaveFaction,
PreconditionType::DoesNotHaveRacingLicence, PreconditionType::DoesNotHaveFlag }) {
EXPECT_FALSE(Precondition::MetByAnyValue(type)) << static_cast<int>(type);
}
for (const auto type : { PreconditionType::ItemEquipped, PreconditionType::HasItem, PreconditionType::HasAchievement,
PreconditionType::MissionAvailable, PreconditionType::OnMission, PreconditionType::MissionComplete, PreconditionType::HasFlag,
PreconditionType::HasFaction, PreconditionType::HasRacingLicence, PreconditionType::MissionActive, PreconditionType::HasLevel }) {
EXPECT_TRUE(Precondition::MetByAnyValue(type)) << static_cast<int>(type);
}
}
// LWOPreconditionComponent::msgCheckAllPreconditions splits a list at ",;:" into groups that all have to pass, and a
// group at "|" into alternatives
TEST(PreconditionItemCostTests, ListIsGroupsOfAlternatives) {
using Groups = std::vector<std::vector<uint32_t>>;
EXPECT_EQ(PreconditionExpression::Parse(""), Groups{});
EXPECT_EQ(PreconditionExpression::Parse("99"), (Groups{ { 99 } }));
EXPECT_EQ(PreconditionExpression::Parse("62|26"), (Groups{ { 62, 26 } }));
EXPECT_EQ(PreconditionExpression::Parse("110|26|62"), (Groups{ { 110, 26, 62 } }));
EXPECT_EQ(PreconditionExpression::Parse("228;62|26"), (Groups{ { 228 }, { 62, 26 } }));
// An item's "248;290|340;338": the last one is asked whatever the middle group says
EXPECT_EQ(PreconditionExpression::Parse("248;290|340;338"), (Groups{ { 248 }, { 290, 340 }, { 338 } }));
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;
}