// Preconditions checked against a player, as LWOPreconditionComponent::CheckPrecondition checks them #include "GameDependencies.h" #include "CDClientDatabase.h" #include "CDComponentsRegistryTable.h" #include "CDItemComponentTable.h" #include "BaseVendorComponent.h" #include "Character.h" #include "DestroyableComponent.h" #include "Entity.h" #include "InventoryComponent.h" #include "Item.h" #include "LevelProgressionComponent.h" #include "MissionComponent.h" #include "Preconditions.h" #include "eReplicaComponentType.h" #include #include #include class PreconditionCheckTests : public GameDependenciesTest { protected: static constexpr LOT BRICK = 6194; std::unique_ptr player; std::unique_ptr character; InventoryComponent* inventory{}; LWOOBJID nextId = 0x1000000000008000LL; // Preconditions are cached by id for the life of the process: every test takes ids of its own static inline uint32_t nextPrecondition = 91000; void SetUp() override { SetUpDependencies(); CDClientDatabase::Connect(":memory:"); CDClientDatabase::ExecuteDML("CREATE TABLE Preconditions (id INTEGER, type INTEGER, targetLOT TEXT, targetCount INTEGER, validContexts INTEGER);"); CDClientDatabase::ExecuteDML("CREATE TABLE ItemSets (setID INTEGER, itemIDs TEXT);"); CDClientDatabase::ExecuteDML("CREATE TABLE Missions (id INTEGER, isMission INTEGER);"); CDClientDatabase::ExecuteDML("CREATE TABLE PlayerFlags (id INTEGER, SessionOnly INTEGER, OnlySetByServer INTEGER, SessionZoneOnly INTEGER);"); const auto componentID = static_cast(96000 + BRICK); auto& registry = CDClientManager::GetEntriesMutable(); registry.insert_or_assign(static_cast(BRICK), componentID); registry.insert_or_assign(static_cast(eReplicaComponentType::ITEM) << 32 | static_cast(BRICK), componentID); CDItemComponent component{}; component.id = componentID; component.stackSize = 100; CDClientManager::GetEntriesMutable().insert_or_assign(componentID, component); info.lot = 1; registry.insert_or_assign(static_cast(info.lot), 0); player = std::make_unique(0x1000000000000002LL, info); inventory = player->AddComponent(-1); // before the character, so it doesn't load a save player->AddComponent(-1); player->AddComponent(-1); player->AddComponent(-1); character = std::make_unique(1, nullptr); player->SetCharacter(character.get()); } void TearDown() override { player->SetCharacter(nullptr); player.reset(); character.reset(); TearDownDependencies(); } uint32_t Row(const PreconditionType type, const std::string& values, const uint32_t count, const uint64_t validContexts) { const auto id = nextPrecondition++; CDClientDatabase::ExecuteDML("INSERT INTO Preconditions VALUES (" + std::to_string(id) + ", " + std::to_string(static_cast(type)) + ", '" + values + "', " + std::to_string(count) + ", " + std::to_string(validContexts) + ");"); return id; } Item* Give(const eInventoryType type, const uint32_t count) { auto* const bag = inventory->GetInventory(type); return new Item(nextId++, BRICK, bag, static_cast(bag->GetItems().size()), count, false, {}, LWOOBJID_EMPTY, LWOOBJID_EMPTY, eLootSourceType::NONE); } }; // A row is only looked at in the contexts it names; elsewhere the precondition passes. With no context at all it fails. TEST_F(PreconditionCheckTests, ARowOutsideItsContextsPasses) { // The player holds none of the item, so each of these fails wherever it is looked at const auto everywhere = Row(PreconditionType::HasItem, "6194", 1, 255); const auto vendorsOnly = Row(PreconditionType::HasItem, "6194", 1, 3); const auto notWhenBound = Row(PreconditionType::HasItem, "6194", 1, 249); const auto nowhere = Row(PreconditionType::HasItem, "6194", 1, 0); EXPECT_FALSE(Preconditions::Check(player.get(), everywhere)); EXPECT_FALSE(Preconditions::Check(player.get(), everywhere, PreconditionContext::BoundItem)); EXPECT_TRUE(Preconditions::Check(player.get(), vendorsOnly)); EXPECT_TRUE(Preconditions::Check(player.get(), vendorsOnly, PreconditionContext::BoundItem)); EXPECT_FALSE(Preconditions::Check(player.get(), vendorsOnly, PreconditionContext::VendorBuy)); EXPECT_FALSE(Preconditions::Check(player.get(), notWhenBound)); EXPECT_TRUE(Preconditions::Check(player.get(), notWhenBound, PreconditionContext::BoundItem)); EXPECT_FALSE(Preconditions::Check(player.get(), nowhere)); EXPECT_FALSE(Preconditions::Check(player.get(), nowhere, PreconditionContext::VendorBuy)); // A list takes the context to each of its rows const auto list = std::to_string(notWhenBound) + ";" + std::to_string(vendorsOnly); EXPECT_FALSE(PreconditionExpression(list).Check(player.get())); EXPECT_TRUE(PreconditionExpression(list).Check(player.get(), PreconditionContext::BoundItem)); } // Type 19 passes while the player interacts with nothing TEST_F(PreconditionCheckTests, NoInteractionPassesWhenNothingIsGoingOn) { EXPECT_TRUE(Preconditions::Check(player.get(), Row(PreconditionType::NoInteraction, "1", 1, 253))); } // Types 16 and 17: a player who has not done the licence mission has licence level 0 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 })); } // 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))); } // A type the client does not know fails TEST_F(PreconditionCheckTests, AnUnknownTypeFails) { EXPECT_FALSE(Preconditions::Check(player.get(), Row(static_cast(99), "1", 1, 255))); }