mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-30 01:41:40 +00:00
The client's HasItem and DoesNotHaveItem checks count the item with ignoreBuyBack set (LWOPreconditionComponent), so what was sold to a vendor and sits in the buy back list is not held. The count here took every inventory, buy back included; the item cost of a quickbuild is picked with the same count. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
339 lines
12 KiB
C++
339 lines
12 KiB
C++
#include "Preconditions.h"
|
|
|
|
#include "Game.h"
|
|
#include "Logger.h"
|
|
|
|
#include <sstream>
|
|
|
|
#include "InventoryComponent.h"
|
|
#include "MissionComponent.h"
|
|
#include "Character.h"
|
|
#include "CharacterComponent.h"
|
|
#include "LevelProgressionComponent.h"
|
|
#include "DestroyableComponent.h"
|
|
#include "GameMessages.h"
|
|
#include "ObjectMessages.h"
|
|
#include "eMissionState.h"
|
|
#include "PetComponent.h"
|
|
|
|
std::map<uint32_t, Precondition*> Preconditions::cache = {};
|
|
|
|
Precondition::Precondition(const uint32_t condition) {
|
|
auto query = CDClientDatabase::CreatePreppedStmt(
|
|
"SELECT type, targetLOT, targetCount, validContexts FROM Preconditions WHERE id = ?;");
|
|
query.bind(1, static_cast<int>(condition));
|
|
|
|
auto result = query.execQuery();
|
|
|
|
if (result.eof()) {
|
|
this->type = PreconditionType::ItemEquipped;
|
|
this->count = 1;
|
|
this->values.clear();
|
|
this->values.push_back(0);
|
|
|
|
LOG("Failed to find precondition of id (%i)!", condition);
|
|
|
|
return;
|
|
}
|
|
|
|
this->type = static_cast<PreconditionType>(result.fieldIsNull("type") ? 0 : result.getIntField("type"));
|
|
|
|
if (!result.fieldIsNull("targetLOT")) {
|
|
std::istringstream stream(result.getStringField("targetLOT"));
|
|
std::string token;
|
|
|
|
while (std::getline(stream, token, ',')) {
|
|
const auto validToken = GeneralUtils::TryParse<uint32_t>(token);
|
|
if (validToken) this->values.push_back(validToken.value());
|
|
}
|
|
}
|
|
|
|
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::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
|
|
}
|
|
|
|
const bool any = MetByAnyValue(type);
|
|
|
|
auto passedAny = false;
|
|
|
|
for (const auto value : values) {
|
|
const auto passed = CheckValue(player, value);
|
|
|
|
if (passed && any) {
|
|
return true;
|
|
}
|
|
|
|
if (!passed && !any) {
|
|
return false;
|
|
}
|
|
|
|
if (passed) {
|
|
passedAny = true;
|
|
}
|
|
}
|
|
|
|
return passedAny;
|
|
}
|
|
|
|
|
|
bool Precondition::MetByAnyValue(const PreconditionType type) {
|
|
// LWOPreconditionComponent::CheckPrecondition: every list check passes on the first value that
|
|
// meets it, and the "not" types are the opposite of that: none of the values may meet the positive check
|
|
switch (type) {
|
|
case PreconditionType::ItemNotEquipped:
|
|
case PreconditionType::DoesNotHaveItem:
|
|
case PreconditionType::DoesNotHaveFaction:
|
|
case PreconditionType::DoesNotHaveRacingLicence:
|
|
case PreconditionType::DoesNotHaveFlag:
|
|
return false;
|
|
default:
|
|
return true;
|
|
}
|
|
}
|
|
|
|
uint32_t Precondition::RacingLicenceLevel(const eMissionState licenceMission) {
|
|
// The state's COMPLETE bit (UNKNOWN, -1, is no state)
|
|
const auto state = static_cast<int32_t>(licenceMission);
|
|
const bool completed = state > 0 && (state & static_cast<int32_t>(eMissionState::COMPLETE)) != 0;
|
|
return completed ? 1 : 0;
|
|
}
|
|
|
|
bool Precondition::ItemCountPasses(const PreconditionType type, const uint32_t held, const uint32_t count) {
|
|
// LWOPreconditionComponent::CheckPrecondition: type 3 is the opposite of type 2's count check
|
|
return type == PreconditionType::DoesNotHaveItem ? held < count : held >= count;
|
|
}
|
|
|
|
bool Precondition::CheckValue(Entity* player, const uint32_t value) const {
|
|
auto* character = player->GetCharacter();
|
|
auto [missionComponent, inventoryComponent, destroyableComponent, levelComponent] =
|
|
player->GetComponentsMut<const MissionComponent, /* not const */ InventoryComponent, const DestroyableComponent, const LevelProgressionComponent>();
|
|
|
|
if (!missionComponent || !inventoryComponent || !destroyableComponent || !levelComponent || !character) {
|
|
return false;
|
|
}
|
|
|
|
Mission* mission;
|
|
|
|
switch (type) {
|
|
case PreconditionType::ItemEquipped:
|
|
return inventoryComponent->IsEquipped(value);
|
|
case PreconditionType::ItemNotEquipped:
|
|
return !inventoryComponent->IsEquipped(value);
|
|
case PreconditionType::HasItem:
|
|
case PreconditionType::DoesNotHaveItem:
|
|
// an unnamed client function counts with ignoreBuyBack set: an item sold to a vendor is no longer held
|
|
return ItemCountPasses(type, inventoryComponent->GetLotCountWithoutBuyBack(value), count);
|
|
case PreconditionType::HasAchievement:
|
|
if (missionComponent == nullptr) return false;
|
|
return missionComponent->GetMissionState(value) >= eMissionState::COMPLETE;
|
|
case PreconditionType::MissionAvailable:
|
|
if (missionComponent == nullptr) return false;
|
|
return missionComponent->GetMissionState(value) == eMissionState::AVAILABLE || missionComponent->GetMissionState(value) == eMissionState::COMPLETE_AVAILABLE;
|
|
case PreconditionType::OnMission:
|
|
if (missionComponent == nullptr) return false;
|
|
return missionComponent->GetMissionState(value) == eMissionState::ACTIVE ||
|
|
missionComponent->GetMissionState(value) == eMissionState::COMPLETE_ACTIVE ||
|
|
missionComponent->GetMissionState(value) == eMissionState::READY_TO_COMPLETE ||
|
|
missionComponent->GetMissionState(value) == eMissionState::COMPLETE_READY_TO_COMPLETE;
|
|
case PreconditionType::MissionComplete:
|
|
if (missionComponent == nullptr) return false;
|
|
return missionComponent->GetMissionState(value) >= eMissionState::COMPLETE;
|
|
case PreconditionType::PetDeployed:
|
|
return PetComponent::GetActivePet(player->GetObjectID()) != nullptr;
|
|
case PreconditionType::HasFlag:
|
|
return character->GetPlayerFlag(value);
|
|
case PreconditionType::WithinShape:
|
|
return true; // Client checks this one
|
|
case PreconditionType::InBuild:
|
|
return character->GetBuildMode();
|
|
case PreconditionType::TeamCheck:
|
|
return false; // TODO: requires knowing the player's minigame team assignment (red/blue etc.); DLU does not track this per-player
|
|
case PreconditionType::IsPetTaming:
|
|
return PetComponent::GetTamingPet(player->GetObjectID()) != nullptr;
|
|
case PreconditionType::HasFaction:
|
|
for (const auto faction : destroyableComponent->GetFactionIDs()) {
|
|
if (faction == static_cast<int>(value)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
case PreconditionType::DoesNotHaveFaction:
|
|
for (const auto faction : destroyableComponent->GetFactionIDs()) {
|
|
if (faction == static_cast<int>(value)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
case PreconditionType::HasRacingLicence:
|
|
return RacingLicenceLevel(missionComponent->GetMissionState(RACING_LICENCE_MISSION)) >= value;
|
|
case PreconditionType::DoesNotHaveRacingLicence:
|
|
return RacingLicenceLevel(missionComponent->GetMissionState(RACING_LICENCE_MISSION)) < value;
|
|
case PreconditionType::LegoClubMember:
|
|
return true; // Live LU opened LEGO CLUB to All players at some point, so always return true
|
|
case PreconditionType::NoInteraction:
|
|
// Passes while the player is not interacting with anything (GetPlayerInteraction answers no object). The
|
|
// client keeps that itself and does not tell the server when most interactions end; taming a pet is the
|
|
// one the server follows from start to end.
|
|
return PetComponent::GetTamingPet(player->GetObjectID()) == nullptr;
|
|
case PreconditionType::HasLevel:
|
|
return levelComponent->GetLevel() >= value;
|
|
case PreconditionType::NotFreeTrial:
|
|
return true; // DLU does not support free trial accounts; all players pass this check
|
|
case PreconditionType::MissionActive: {
|
|
if (missionComponent == nullptr) return false;
|
|
const auto state = missionComponent->GetMissionState(value);
|
|
return state == eMissionState::ACTIVE || state == eMissionState::COMPLETE_ACTIVE;
|
|
}
|
|
case PreconditionType::DoesNotHaveFlag:
|
|
return !character->GetPlayerFlag(value);
|
|
default:
|
|
return true; // There are a couple more unknown preconditions. Always return true in this case.
|
|
}
|
|
}
|
|
|
|
std::vector<std::vector<uint32_t>> PreconditionExpression::Parse(const std::string& conditions) {
|
|
std::vector<std::vector<uint32_t>> groups;
|
|
std::vector<uint32_t> group;
|
|
std::string digits;
|
|
const auto endAlternative = [&group, &digits]() {
|
|
const auto id = GeneralUtils::TryParse<uint32_t>(digits);
|
|
if (id) group.push_back(id.value());
|
|
digits.clear();
|
|
};
|
|
const auto endGroup = [&groups, &group, &endAlternative]() {
|
|
endAlternative();
|
|
if (!group.empty()) groups.push_back(std::move(group));
|
|
group.clear();
|
|
};
|
|
|
|
for (const auto character : conditions) {
|
|
if (character >= '0' && character <= '9') {
|
|
digits.push_back(character);
|
|
continue;
|
|
}
|
|
switch (character) {
|
|
case '|':
|
|
endAlternative();
|
|
break;
|
|
case ',':
|
|
case '&':
|
|
case ';':
|
|
case '(':
|
|
case ':':
|
|
endGroup();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
endGroup();
|
|
return groups;
|
|
}
|
|
|
|
PreconditionExpression::PreconditionExpression(const std::string& conditions) : m_Groups(Parse(conditions)) {
|
|
empty = m_Groups.empty();
|
|
}
|
|
|
|
|
|
std::optional<ItemCost> Precondition::PickItemCost(const std::vector<uint32_t>& lots, const uint32_t count, const std::function<uint32_t(LOT)>& lotCount) {
|
|
for (const auto lot : lots) {
|
|
if (lotCount(static_cast<LOT>(lot)) >= count) return ItemCost{ static_cast<LOT>(lot), count };
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::optional<ItemCost> Precondition::GetItemCost(Entity* player) const {
|
|
if (type != PreconditionType::HasItem || count == 0) return std::nullopt;
|
|
auto* const inventoryComponent = player->GetComponent<InventoryComponent>();
|
|
if (!inventoryComponent) return std::nullopt;
|
|
return PickItemCost(values, count, [inventoryComponent](const LOT lot) { return inventoryComponent->GetLotCountWithoutBuyBack(lot); });
|
|
}
|
|
|
|
std::vector<ItemCost> PreconditionExpression::GetItemCosts(Entity* player) const {
|
|
std::vector<ItemCost> costs;
|
|
for (const auto& group : m_Groups) {
|
|
for (const auto condition : group) {
|
|
const auto cost = Preconditions::Get(condition).GetItemCost(player);
|
|
if (cost) costs.push_back(*cost);
|
|
}
|
|
}
|
|
return costs;
|
|
}
|
|
|
|
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, context)) groupPasses = true;
|
|
}
|
|
if (groupPasses) continue;
|
|
|
|
pass = false;
|
|
for (const auto condition : group) {
|
|
GameMessages::NotifyClientFailedPrecondition failedPrecondition;
|
|
failedPrecondition.target = player->GetObjectID();
|
|
failedPrecondition.failedReason = u"";
|
|
failedPrecondition.preconditionID = condition;
|
|
failedPrecondition.Send(player->GetSystemAddress());
|
|
}
|
|
}
|
|
return pass;
|
|
}
|
|
|
|
|
|
const Precondition& Preconditions::Get(const uint32_t condition) {
|
|
const auto& index = cache.find(condition);
|
|
if (index != cache.end()) return *index->second;
|
|
|
|
auto* const precondition = new Precondition(condition);
|
|
cache.insert_or_assign(condition, precondition);
|
|
return *precondition;
|
|
}
|
|
|
|
bool Preconditions::Check(Entity* player, const uint32_t condition, const PreconditionContext context) {
|
|
return Get(condition).Check(player, context);
|
|
}
|
|
|
|
|
|
PreconditionExpression Preconditions::CreateExpression(const std::string& conditions) {
|
|
return PreconditionExpression(conditions);
|
|
}
|
|
|
|
|
|
Preconditions::~Preconditions() {
|
|
for (const auto& condition : cache) {
|
|
delete condition.second;
|
|
}
|
|
|
|
cache.clear();
|
|
}
|