Kelsidavis-WoWee/src/pipeline/wowee_conditions.cpp
Kelsi f290a0d4a9 feat(pipeline): add WPCD (Wowee Player Condition) format
Novel open replacement for Blizzard's PlayerCondition.dbc +
the AzerothCore-style condition_template SQL tables. The
37th open format added to the editor.

Defines reusable boolean conditions that other formats can
reference for gating: "player has quest X completed",
"player level >= N", "player class is mage", "player has
item Y in inventory", "WSEA event Z is active".

Conditions can be grouped and combined with AND/OR
aggregators on a per-group basis: a quest-giver gossip
option that says "show only to level 60 alliance mages
who completed quest 1234" composes 4 conditions sharing
the same groupId with AND aggregation. The runtime walks
each group, applies the group's aggregator, and returns
the boolean result to the caller.

Cross-references with previously-added formats — the
targetId field has a polymorphic interpretation by kind:
  WPCD.targetId (kind=QuestCompleted/Active) -> WQT.questId
  WPCD.targetId (kind=HasItem)               -> WIT.itemId
  WPCD.targetId (kind=HasSpell)              -> WSPL.spellId
  WPCD.targetId (kind=HasAchievement)        -> WACH.achievementId
  WPCD.targetId (kind=AreaId)                -> WMS.areaId
  WPCD.targetId (kind=EventActive)           -> WSEA.eventId
  WPCD.targetId (kind=HasTitle)              -> WTIT.titleId
  WPCD.targetId (kind=FactionRep)            -> WFAC.factionId
  WPCD.targetId (kind=Class/Race)            -> WCHC class/race id

Future format extensions can reference WPCD.conditionId in
their own gating fields — WTRG triggers gated by player
state, WGSP options visible only when conditions are met,
WMOU summon spells condition-gated by quest progress, etc.

Format:
  • magic "WPCD", version 1, little-endian
  • per condition: conditionId / groupId (0 = standalone) /
    name / description / kind / aggregator / negated /
    targetId / minValue / maxValue

Enums:
  • Kind (17): AlwaysTrue / AlwaysFalse / QuestCompleted /
                QuestActive / HasItem / HasSpell / MinLevel /
                MaxLevel / ClassMatch / RaceMatch /
                FactionRep / HasAchievement / TeamSize /
                GuildLevel / EventActive / AreaId / HasTitle
  • Aggregator (2): And / Or

API: WoweeConditionLoader::save / load / exists / findById.

Three preset emitters showcase typical usage:
  • makeStarter — 4 standalone conditions covering the most
                   common kinds (quest-done / has-item /
                   min-level / class)
  • makeGated   — 5 conditions in 2 groups demonstrating
                   AND-aggregation (alliance + mage + lvl 60)
                   and OR-aggregation (did quest 1 OR quest 100)
  • makeEvent   — 3 event-gated conditions cross-referencing
                   WSEA event IDs (Hallow's End / Brewfest /
                   Winter's Veil)

CLI added (5 flags, 656 documented total now):
  --gen-conditions / --gen-conditions-gated / --gen-conditions-event
  --info-wpcd / --validate-wpcd

Validator catches: conditionId=0 + duplicates, kind /
aggregator out of range, kinds requiring targetId having
target=0 (skips AlwaysTrue/False, MinLevel/MaxLevel,
TeamSize, GuildLevel which use min/max instead), TeamSize
with min > max.
2026-05-09 18:18:27 -07:00

277 lines
8.6 KiB
C++

#include "pipeline/wowee_conditions.hpp"
#include <cstdio>
#include <cstring>
#include <fstream>
namespace wowee {
namespace pipeline {
namespace {
constexpr char kMagic[4] = {'W', 'P', 'C', 'D'};
constexpr uint32_t kVersion = 1;
template <typename T>
void writePOD(std::ofstream& os, const T& v) {
os.write(reinterpret_cast<const char*>(&v), sizeof(T));
}
template <typename T>
bool readPOD(std::ifstream& is, T& v) {
is.read(reinterpret_cast<char*>(&v), sizeof(T));
return is.gcount() == static_cast<std::streamsize>(sizeof(T));
}
void writeStr(std::ofstream& os, const std::string& s) {
uint32_t n = static_cast<uint32_t>(s.size());
writePOD(os, n);
if (n > 0) os.write(s.data(), n);
}
bool readStr(std::ifstream& is, std::string& s) {
uint32_t n = 0;
if (!readPOD(is, n)) return false;
if (n > (1u << 20)) return false;
s.resize(n);
if (n > 0) {
is.read(s.data(), n);
if (is.gcount() != static_cast<std::streamsize>(n)) {
s.clear();
return false;
}
}
return true;
}
std::string normalizePath(std::string base) {
if (base.size() < 5 || base.substr(base.size() - 5) != ".wpcd") {
base += ".wpcd";
}
return base;
}
} // namespace
const WoweeCondition::Entry*
WoweeCondition::findById(uint32_t conditionId) const {
for (const auto& e : entries) if (e.conditionId == conditionId) return &e;
return nullptr;
}
const char* WoweeCondition::kindName(uint8_t k) {
switch (k) {
case AlwaysTrue: return "true";
case AlwaysFalse: return "false";
case QuestCompleted: return "quest-done";
case QuestActive: return "quest-active";
case HasItem: return "has-item";
case HasSpell: return "has-spell";
case MinLevel: return "min-level";
case MaxLevel: return "max-level";
case ClassMatch: return "class";
case RaceMatch: return "race";
case FactionRep: return "rep";
case HasAchievement: return "achievement";
case TeamSize: return "team-size";
case GuildLevel: return "guild-level";
case EventActive: return "event";
case AreaId: return "area";
case HasTitle: return "title";
default: return "unknown";
}
}
const char* WoweeCondition::aggregatorName(uint8_t a) {
switch (a) {
case And: return "and";
case Or: return "or";
default: return "unknown";
}
}
bool WoweeConditionLoader::save(const WoweeCondition& cat,
const std::string& basePath) {
std::ofstream os(normalizePath(basePath), std::ios::binary);
if (!os) return false;
os.write(kMagic, 4);
writePOD(os, kVersion);
writeStr(os, cat.name);
uint32_t entryCount = static_cast<uint32_t>(cat.entries.size());
writePOD(os, entryCount);
for (const auto& e : cat.entries) {
writePOD(os, e.conditionId);
writePOD(os, e.groupId);
writeStr(os, e.name);
writeStr(os, e.description);
writePOD(os, e.kind);
writePOD(os, e.aggregator);
writePOD(os, e.negated);
uint8_t pad = 0;
writePOD(os, pad);
writePOD(os, e.targetId);
writePOD(os, e.minValue);
writePOD(os, e.maxValue);
}
return os.good();
}
WoweeCondition WoweeConditionLoader::load(const std::string& basePath) {
WoweeCondition out;
std::ifstream is(normalizePath(basePath), std::ios::binary);
if (!is) return out;
char magic[4];
is.read(magic, 4);
if (std::memcmp(magic, kMagic, 4) != 0) return out;
uint32_t version = 0;
if (!readPOD(is, version) || version != kVersion) return out;
if (!readStr(is, out.name)) return out;
uint32_t entryCount = 0;
if (!readPOD(is, entryCount)) return out;
if (entryCount > (1u << 20)) return out;
out.entries.resize(entryCount);
for (auto& e : out.entries) {
if (!readPOD(is, e.conditionId) ||
!readPOD(is, e.groupId)) {
out.entries.clear(); return out;
}
if (!readStr(is, e.name) || !readStr(is, e.description)) {
out.entries.clear(); return out;
}
if (!readPOD(is, e.kind) ||
!readPOD(is, e.aggregator) ||
!readPOD(is, e.negated)) {
out.entries.clear(); return out;
}
uint8_t pad = 0;
if (!readPOD(is, pad)) {
out.entries.clear(); return out;
}
if (!readPOD(is, e.targetId) ||
!readPOD(is, e.minValue) ||
!readPOD(is, e.maxValue)) {
out.entries.clear(); return out;
}
}
return out;
}
bool WoweeConditionLoader::exists(const std::string& basePath) {
std::ifstream is(normalizePath(basePath), std::ios::binary);
return is.good();
}
WoweeCondition WoweeConditionLoader::makeStarter(const std::string& catalogName) {
WoweeCondition c;
c.name = catalogName;
{
WoweeCondition::Entry e;
e.conditionId = 1; e.name = "Bandit Trouble done";
e.description = "Player has completed the Bandit Trouble quest.";
e.kind = WoweeCondition::QuestCompleted;
e.targetId = 1; // matches WQT.makeStarter quest 1
c.entries.push_back(e);
}
{
WoweeCondition::Entry e;
e.conditionId = 2; e.name = "Has Healing Potion";
e.description = "Player has at least 1 healing potion in bags.";
e.kind = WoweeCondition::HasItem;
e.targetId = 3; // WIT.makeStarter healing potion
e.minValue = 1;
c.entries.push_back(e);
}
{
WoweeCondition::Entry e;
e.conditionId = 3; e.name = "Level 60+";
e.description = "Player is level 60 or higher.";
e.kind = WoweeCondition::MinLevel;
e.minValue = 60;
c.entries.push_back(e);
}
{
WoweeCondition::Entry e;
e.conditionId = 4; e.name = "Is Mage";
e.description = "Player class is Mage.";
e.kind = WoweeCondition::ClassMatch;
e.targetId = 8; // WCHC mage class id
c.entries.push_back(e);
}
return c;
}
WoweeCondition WoweeConditionLoader::makeGated(const std::string& catalogName) {
WoweeCondition c;
c.name = catalogName;
// Group 100: Alliance AND Mage AND level >= 60.
{
WoweeCondition::Entry e;
e.conditionId = 100; e.groupId = 100;
e.name = "Alliance race";
e.kind = WoweeCondition::RaceMatch;
e.aggregator = WoweeCondition::And;
// raceMask bits for Alliance races (Human/Dwarf/NightElf/Gnome).
e.targetId = (1u << 1) | (1u << 3) | (1u << 4) | (1u << 7);
c.entries.push_back(e);
}
{
WoweeCondition::Entry e;
e.conditionId = 101; e.groupId = 100;
e.name = "Mage class";
e.kind = WoweeCondition::ClassMatch;
e.aggregator = WoweeCondition::And;
e.targetId = 8;
c.entries.push_back(e);
}
{
WoweeCondition::Entry e;
e.conditionId = 102; e.groupId = 100;
e.name = "Level 60+";
e.kind = WoweeCondition::MinLevel;
e.aggregator = WoweeCondition::And;
e.minValue = 60;
c.entries.push_back(e);
}
// Group 200: completed quest 1 OR completed quest 2.
{
WoweeCondition::Entry e;
e.conditionId = 200; e.groupId = 200;
e.name = "Did quest 1";
e.kind = WoweeCondition::QuestCompleted;
e.aggregator = WoweeCondition::Or;
e.targetId = 1;
c.entries.push_back(e);
}
{
WoweeCondition::Entry e;
e.conditionId = 201; e.groupId = 200;
e.name = "Did quest 100";
e.kind = WoweeCondition::QuestCompleted;
e.aggregator = WoweeCondition::Or;
e.targetId = 100;
c.entries.push_back(e);
}
return c;
}
WoweeCondition WoweeConditionLoader::makeEvent(const std::string& catalogName) {
WoweeCondition c;
c.name = catalogName;
auto add = [&](uint32_t id, const char* name, uint32_t eventId,
const char* desc) {
WoweeCondition::Entry e;
e.conditionId = id; e.name = name; e.description = desc;
e.kind = WoweeCondition::EventActive;
e.targetId = eventId;
c.entries.push_back(e);
};
// eventIds 100/101/103 match WSEA.makeYearly (Hallow's End,
// Brewfest, Winter's Veil).
add(300, "Hallow's End active", 100, "Hallow's End event is currently running.");
add(301, "Brewfest active", 101, "Brewfest event is currently running.");
add(302, "Winter's Veil active", 103, "Winter's Veil event is currently running.");
return c;
}
} // namespace pipeline
} // namespace wowee