Files
DarkflameServer/dGame/dUtilities/Preconditions.cpp
Aaron Kimbrell b2a1e9e0b8 refactor: remaining game messages as structs, switch removed
Every game message still written or read by hand is now a NetGameMsg with
Serialize and Deserialize, in per-domain files:
- MovementMessages: teleport, platforms (resync and its request), orient to
  angle, node rotation lock, gravity scale, jetpack mode, control scheme,
  respawn checkpoint, rails (set/start, ready, cancel, arrived), mount
  inventory ID, dismount complete, possession ack, ghost reference override
  and position, camera cycling (eCameraTargetCyclingMode moves here).
- ZoneMessages: player loaded (the old PLAYER_LOADED case), ready for
  updates, player ready, restore to post load stats, server done loading,
  invalid zone transfer list, zone summary display/dismissed, level
  processing complete, object world state, and the localized announcement
  WorldMigration wrote by hand.
- PlayerMessages: chat mode, GM level, LEGO score, currency, reputation,
  GM invis, pickup currency, zone and player statistics, chat commands, bug
  reports, verify ack.
- ObjectMessages: fire event client/server side, notify client
  (zone) object, notify object, script network vars, failed preconditions,
  terminate interaction, set name, request use, request server object info.
- QuickBuildMessages: notify state, enable, cancel.
- ActivityMessages gains match response/update/request, leaderboard request
  and data, shooting gallery score/rotation/fire, activity state change.
- MissionMessages gains MissionDialogueCancelled (a no-op, as before).
The wire structs left in GameMessages.h move to their domains (tooltip and
emote to Effects, loot and item use to Inventory, model build to Building,
behavior sound to Property, skill sets to Skill) and gain the missing
direction. The dismount logic moves to PossessorComponent::OnDismountComplete.

Every inbound message is registered in the GameMessageHandler map; the
switch is gone, and GameMessages.cpp only holds the GameMsg/NetGameMsg base
code. Call sites build the structs (NotifyClientObject, TerminateInteraction,
Teleport, PlatformResync, FireEventClientSide, NotifyObject and
NotifyClientZoneObject get convenience constructors like PlayFXEffect).
Dead senders are dropped: SendSetShootingGalleryParams (no callers, field
order was a guess), SendTeamPickupItem (the struct already existed),
SendRequestActivitySummaryLeaderboardData (the struct covers it).

Verified with RemainingMessagesTests: every old Send* function is frozen
verbatim in Legacy/RemainingMessagesLegacy.h and compared byte for byte
(same bits, destination and broadcast flag) over grids of inputs; every old
Handle* read sequence is frozen as a Read* oracle and compared with the
struct's Deserialize; round trips, truncation and a golden packet.
PlayerLoaded (0x00dc36f0), SetGMLevel (0x00dd6230), MissionDialogueCancelled
(0x00d9cc10) and LocalizedAnnouncementServerToSingleClient (0x00f23c50)
were checked against the client. Behaviour notes: an inbound message that
fails to deserialize is dropped, so ParseChatMessage over MAX_MESSAGE_LENGTH
is dropped instead of truncated, and PLAYER_LOADED / READY_FOR_UPDATES /
MISSION_DIALOGUE_CANCELLED now read their (unused) client fields.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 22:30:56 -05:00

342 lines
9.4 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 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");
result.finalize();
}
bool Precondition::Check(Entity* player, bool evaluateCosts) const {
if (values.empty()) {
return true; // There are very few of these
}
bool any;
// Guesses
switch (type) {
case PreconditionType::HasItem:
case PreconditionType::ItemEquipped:
case PreconditionType::HasAchievement:
case PreconditionType::MissionAvailable:
case PreconditionType::OnMission:
case PreconditionType::MissionComplete:
case PreconditionType::PetDeployed:
case PreconditionType::HasFlag:
case PreconditionType::WithinShape:
case PreconditionType::InBuild:
case PreconditionType::TeamCheck:
case PreconditionType::IsPetTaming:
case PreconditionType::HasFaction:
case PreconditionType::DoesNotHaveFaction:
case PreconditionType::HasRacingLicence:
case PreconditionType::DoesNotHaveRacingLicence:
case PreconditionType::LegoClubMember:
case PreconditionType::NoInteraction:
case PreconditionType::NotFreeTrial:
case PreconditionType::MissionActive:
case PreconditionType::DoesNotHaveFlag:
any = true;
break;
case PreconditionType::DoesNotHaveItem:
case PreconditionType::ItemNotEquipped:
case PreconditionType::HasLevel:
any = false;
break;
default:
any = true;
break;
}
auto passedAny = false;
for (const auto value : values) {
const auto passed = CheckValue(player, value, evaluateCosts);
if (passed && any) {
return true;
}
if (!passed && !any) {
return false;
}
if (passed) {
passedAny = true;
}
}
return passedAny;
}
bool Precondition::CheckValue(Entity* player, const uint32_t value, bool evaluateCosts) 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:
if (evaluateCosts) // As far as I know this is only used for quickbuilds, and removal shouldn't actually be handled here.
{
return inventoryComponent->RemoveItem(value, count, eInventoryType::ALL);
}
return inventoryComponent->GetLotCount(value) >= count;
case PreconditionType::DoesNotHaveItem:
return inventoryComponent->IsEquipped(value) && count > 0;
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 false; // TODO: requires a racing licence level on the player; DLU does not track this
case PreconditionType::DoesNotHaveRacingLicence:
return false; // TODO: requires a racing licence level on the player; DLU does not track this
case PreconditionType::LegoClubMember:
return true; // Live LU opened LEGO CLUB to All players at some point, so always return true
case PreconditionType::NoInteraction:
return false; // TODO: requires tracking the player's currently active interaction object; DLU does not track this
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.
}
}
PreconditionExpression::PreconditionExpression(const std::string& conditions) {
if (conditions.empty()) {
empty = true;
return;
}
std::stringstream a;
std::stringstream b;
auto bor = false;
auto done = false;
for (auto i = 0u; i < conditions.size(); ++i) {
if (done) {
break;
}
const auto character = conditions[i];
switch (character) {
case '|':
bor = true;
b << conditions.substr(i + 1);
done = true;
break;
case ' ':
case ')':
break;
case ',':
case '&':
case ';':
case '(':
case ':':
b << conditions.substr(i + 1);
done = true;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
a << character;
break;
default:
break;
}
}
this->m_or = bor;
const auto aString = a.str();
if (!aString.empty()) {
this->condition = std::stoul(a.str());
} else {
this->condition = 0;
}
const auto bString = b.str();
if (!bString.empty()) {
this->next = new PreconditionExpression(bString);
} else {
this->next = nullptr;
}
}
bool PreconditionExpression::Check(Entity* player, bool evaluateCosts) const {
if (empty) {
return true;
}
const auto a = Preconditions::Check(player, condition, evaluateCosts);
if (!a) {
GameMessages::NotifyClientFailedPrecondition failedPrecondition;
failedPrecondition.target = player->GetObjectID();
failedPrecondition.failedReason = u"";
failedPrecondition.preconditionID = condition;
failedPrecondition.Send(player->GetSystemAddress());
}
const auto b = next == nullptr ? true : next->Check(player, evaluateCosts);
return m_or ? a || b : a && b;
}
PreconditionExpression::~PreconditionExpression() {
delete next;
}
bool Preconditions::Check(Entity* player, const uint32_t condition, bool evaluateCosts) {
Precondition* precondition;
const auto& index = cache.find(condition);
if (index != cache.end()) {
precondition = index->second;
} else {
precondition = new Precondition(condition);
cache.insert_or_assign(condition, precondition);
}
return precondition->Check(player, evaluateCosts);
}
PreconditionExpression Preconditions::CreateExpression(const std::string& conditions) {
return PreconditionExpression(conditions);
}
Preconditions::~Preconditions() {
for (const auto& condition : cache) {
delete condition.second;
}
cache.clear();
}