Files
DarkflameServer/dGame/dUtilities/Preconditions.h
Aaron Kimbrell ad2aadc1fe 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>
2026-11-11 06:55:23 -06:00

136 lines
4.2 KiB
C++

#pragma once
#include <functional>
#include <optional>
#include <vector>
#include "Entity.h"
#include "eMissionState.h"
enum class PreconditionType
{
ItemEquipped,
ItemNotEquipped,
HasItem,
DoesNotHaveItem,
HasAchievement,
MissionAvailable,
OnMission,
MissionComplete,
PetDeployed,
HasFlag,
WithinShape,
InBuild,
TeamCheck,
IsPetTaming,
HasFaction,
DoesNotHaveFaction,
HasRacingLicence,
DoesNotHaveRacingLicence,
LegoClubMember,
NoInteraction,
NotFreeTrial,
MissionActive,
HasLevel,
DoesNotHaveFlag = 23
};
// 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 };
bool operator==(const ItemCost&) const = default;
};
class Precondition final
{
public:
explicit Precondition(uint32_t condition);
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.
std::optional<ItemCost> GetItemCost(Entity* player) const;
// The pick GetItemCost makes, given how many of a LOT the player has
static std::optional<ItemCost> PickItemCost(const std::vector<uint32_t>& lots, uint32_t count, const std::function<uint32_t(LOT)>& lotCount);
// Whether a precondition listing several values is met by any one of them (true) or needs every one (false)
static bool MetByAnyValue(PreconditionType type);
// The racing licence level the HasRacingLicence types compare their value with: 1 once the mission that makes
// the player a licensed racer has been completed, 0 before (LWORacingMissions::SendMessage)
static uint32_t RacingLicenceLevel(eMissionState licenceMission);
// That mission
static constexpr uint32_t RACING_LICENCE_MISSION = 637;
// Whether holding this many of a LOT meets a HasItem (enough) or a DoesNotHaveItem (fewer) precondition
static bool ItemCountPasses(PreconditionType type, uint32_t held, uint32_t count);
private:
bool CheckValue(Entity* player, uint32_t value) const;
PreconditionType type;
std::vector<uint32_t> values;
uint32_t count;
// Every context, for a row that does not say
uint64_t validContexts{ ~0ULL };
};
class PreconditionExpression final
{
public:
explicit PreconditionExpression(const std::string& conditions);
// 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;
// The groups of a precondition list, as the client splits it: groups at any of ",;:" (all have to pass), the
// alternatives of a group at "|" (one has to pass)
static std::vector<std::vector<uint32_t>> Parse(const std::string& conditions);
private:
std::vector<std::vector<uint32_t>> m_Groups;
bool empty = false;
};
class Preconditions final
{
public:
static bool Check(Entity* player, uint32_t condition, PreconditionContext context = PreconditionContext::None);
static const Precondition& Get(uint32_t condition);
static PreconditionExpression CreateExpression(const std::string& conditions);
~Preconditions();
private:
static std::map<uint32_t, Precondition*> cache;
};