Kelsidavis-WoWee/tools/editor/cli_spell_schools_catalog.cpp

402 lines
15 KiB
C++
Raw Normal View History

feat(pipeline): add WSCH (Wowee Spell School) catalog 56th open format — replaces SpellSchools.dbc plus the Resistances.dbc resistance-cap tables. Defines damage schools spells use: Physical, Holy, Fire, Nature, Frost, Shadow, Arcane, plus combined / hybrid schools that count as multiple types simultaneously (Spellfire, Spellshadow, Spellfrost — relevant for resistance-bypass mechanics). 7 canonical schools with single-bit IDs (1, 2, 4, 8, 16, 32, 64) so combinedSchoolMask values line up directly with the spell engine's school-bit enum. Hybrid schools use high-bit IDs (0x80000001+) and their combinedSchoolMask references the canonical bits they qualify as. Each school carries visual identity (color tint, icon), gameplay rules (canBeImmune / canBeAbsorbed / canBeReflected / canCrit), resistance cap at max level, and cast / impact sound IDs. Cross-references with prior formats — castSoundId and impactSoundId point at WSND.soundId; combinedSchoolMask is a bitmask of OTHER WSCH.schoolId values within the same catalog. CLI: --gen-sch (3 base — Physical / Fire / Holy showing non-resistable Holy + non-reflectable Physical), --gen-sch- magical (6 canonical magical schools with proper colors + 365 max-level resistance caps), --gen-sch-combined (3 hybrids — Spellfire / Spellshadow / Spellfrost with multi-bit combinedSchoolMask), --info-wsch, --validate-wsch with --json variants. Validator catches id+name required, reflected-without-absorbed warning (reflected damage should be absorbable), self-referential combinedSchoolMask (school qualifying as itself), and combined-mask references to bits not defined in the same catalog (resolved at runtime across catalogs). Format graph: 55 → 56 binary formats. CLI flag count: 798 → 804.
2026-05-09 20:19:34 -07:00
#include "cli_spell_schools_catalog.hpp"
#include "cli_arg_parse.hpp"
#include "cli_box_emitter.hpp"
#include "pipeline/wowee_spell_schools.hpp"
#include <nlohmann/json.hpp>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <string>
#include <vector>
namespace wowee {
namespace editor {
namespace cli {
namespace {
std::string stripWschExt(std::string base) {
stripExt(base, ".wsch");
return base;
}
bool saveOrError(const wowee::pipeline::WoweeSpellSchool& c,
const std::string& base, const char* cmd) {
if (!wowee::pipeline::WoweeSpellSchoolLoader::save(c, base)) {
std::fprintf(stderr, "%s: failed to save %s.wsch\n",
cmd, base.c_str());
return false;
}
return true;
}
void printGenSummary(const wowee::pipeline::WoweeSpellSchool& c,
const std::string& base) {
std::printf("Wrote %s.wsch\n", base.c_str());
std::printf(" catalog : %s\n", c.name.c_str());
std::printf(" schools : %zu\n", c.entries.size());
}
int handleGenStarter(int& i, int argc, char** argv) {
std::string base = argv[++i];
std::string name = "StarterSchools";
if (parseOptArg(i, argc, argv)) name = argv[++i];
base = stripWschExt(base);
auto c = wowee::pipeline::WoweeSpellSchoolLoader::makeStarter(name);
if (!saveOrError(c, base, "gen-sch")) return 1;
printGenSummary(c, base);
return 0;
}
int handleGenMagical(int& i, int argc, char** argv) {
std::string base = argv[++i];
std::string name = "MagicalSchools";
if (parseOptArg(i, argc, argv)) name = argv[++i];
base = stripWschExt(base);
auto c = wowee::pipeline::WoweeSpellSchoolLoader::makeMagical(name);
if (!saveOrError(c, base, "gen-sch-magical")) return 1;
printGenSummary(c, base);
return 0;
}
int handleGenCombined(int& i, int argc, char** argv) {
std::string base = argv[++i];
std::string name = "CombinedSchools";
if (parseOptArg(i, argc, argv)) name = argv[++i];
base = stripWschExt(base);
auto c = wowee::pipeline::WoweeSpellSchoolLoader::makeCombined(name);
if (!saveOrError(c, base, "gen-sch-combined")) return 1;
printGenSummary(c, base);
return 0;
}
int handleInfo(int& i, int argc, char** argv) {
std::string base = argv[++i];
bool jsonOut = consumeJsonFlag(i, argc, argv);
base = stripWschExt(base);
if (!wowee::pipeline::WoweeSpellSchoolLoader::exists(base)) {
std::fprintf(stderr, "WSCH not found: %s.wsch\n", base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeSpellSchoolLoader::load(base);
if (jsonOut) {
nlohmann::json j;
j["wsch"] = base + ".wsch";
j["name"] = c.name;
j["count"] = c.entries.size();
nlohmann::json arr = nlohmann::json::array();
for (const auto& e : c.entries) {
arr.push_back({
{"schoolId", e.schoolId},
{"name", e.name},
{"description", e.description},
{"iconPath", e.iconPath},
{"canBeImmune", e.canBeImmune},
{"canBeAbsorbed", e.canBeAbsorbed},
{"canBeReflected", e.canBeReflected},
{"canCrit", e.canCrit},
{"colorRGBA", e.colorRGBA},
{"baseResistanceCap", e.baseResistanceCap},
{"castSoundId", e.castSoundId},
{"impactSoundId", e.impactSoundId},
{"combinedSchoolMask", e.combinedSchoolMask},
});
}
j["entries"] = arr;
std::printf("%s\n", j.dump(2).c_str());
return 0;
}
std::printf("WSCH: %s.wsch\n", base.c_str());
std::printf(" catalog : %s\n", c.name.c_str());
std::printf(" schools : %zu\n", c.entries.size());
if (c.entries.empty()) return 0;
std::printf(" schoolId immune absorb reflect crit resCap combined-mask name\n");
for (const auto& e : c.entries) {
std::printf(" 0x%08x %u %u %u %u %5u 0x%08x %s\n",
e.schoolId, e.canBeImmune, e.canBeAbsorbed,
e.canBeReflected, e.canCrit,
e.baseResistanceCap, e.combinedSchoolMask,
e.name.c_str());
}
return 0;
}
int handleExportJson(int& i, int argc, char** argv) {
// Mirrors the JSON pairs added for every other novel
// open format. WSCH has no enum fields with name forms
// — just raw numeric school bits and flag bytes — so
// the JSON mapping is a direct dump.
std::string base = argv[++i];
std::string outPath;
if (parseOptArg(i, argc, argv)) outPath = argv[++i];
base = stripWschExt(base);
if (outPath.empty()) outPath = base + ".wsch.json";
if (!wowee::pipeline::WoweeSpellSchoolLoader::exists(base)) {
std::fprintf(stderr,
"export-wsch-json: WSCH not found: %s.wsch\n", base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeSpellSchoolLoader::load(base);
nlohmann::json j;
j["name"] = c.name;
nlohmann::json arr = nlohmann::json::array();
for (const auto& e : c.entries) {
arr.push_back({
{"schoolId", e.schoolId},
{"name", e.name},
{"description", e.description},
{"iconPath", e.iconPath},
{"canBeImmune", e.canBeImmune},
{"canBeAbsorbed", e.canBeAbsorbed},
{"canBeReflected", e.canBeReflected},
{"canCrit", e.canCrit},
{"colorRGBA", e.colorRGBA},
{"baseResistanceCap", e.baseResistanceCap},
{"castSoundId", e.castSoundId},
{"impactSoundId", e.impactSoundId},
{"combinedSchoolMask", e.combinedSchoolMask},
});
}
j["entries"] = arr;
std::ofstream out(outPath);
if (!out) {
std::fprintf(stderr,
"export-wsch-json: cannot write %s\n", outPath.c_str());
return 1;
}
out << j.dump(2) << "\n";
out.close();
std::printf("Wrote %s\n", outPath.c_str());
std::printf(" source : %s.wsch\n", base.c_str());
std::printf(" schools : %zu\n", c.entries.size());
return 0;
}
int handleImportJson(int& i, int argc, char** argv) {
std::string jsonPath = argv[++i];
std::string outBase;
if (parseOptArg(i, argc, argv)) outBase = argv[++i];
if (outBase.empty()) {
outBase = jsonPath;
std::string suffix = ".wsch.json";
if (outBase.size() > suffix.size() &&
outBase.substr(outBase.size() - suffix.size()) == suffix) {
outBase = outBase.substr(0, outBase.size() - suffix.size());
} else if (outBase.size() > 5 &&
outBase.substr(outBase.size() - 5) == ".json") {
outBase = outBase.substr(0, outBase.size() - 5);
}
}
outBase = stripWschExt(outBase);
std::ifstream in(jsonPath);
if (!in) {
std::fprintf(stderr,
"import-wsch-json: cannot read %s\n", jsonPath.c_str());
return 1;
}
nlohmann::json j;
try { in >> j; }
catch (const std::exception& e) {
std::fprintf(stderr,
"import-wsch-json: bad JSON in %s: %s\n",
jsonPath.c_str(), e.what());
return 1;
}
wowee::pipeline::WoweeSpellSchool c;
c.name = j.value("name", std::string{});
if (j.contains("entries") && j["entries"].is_array()) {
for (const auto& je : j["entries"]) {
wowee::pipeline::WoweeSpellSchool::Entry e;
e.schoolId = je.value("schoolId", 0u);
e.name = je.value("name", std::string{});
e.description = je.value("description", std::string{});
e.iconPath = je.value("iconPath", std::string{});
// Defaults match the WoW canonical behavior: most
// schools allow absorbs and crits, only Holy is
// non-immune by default. Hand-edits should set
// these explicitly when they differ.
e.canBeImmune = static_cast<uint8_t>(
je.value("canBeImmune", 1));
e.canBeAbsorbed = static_cast<uint8_t>(
je.value("canBeAbsorbed", 1));
e.canBeReflected = static_cast<uint8_t>(
je.value("canBeReflected", 0));
e.canCrit = static_cast<uint8_t>(
je.value("canCrit", 1));
e.colorRGBA = je.value("colorRGBA", 0xFFFFFFFFu);
e.baseResistanceCap = je.value("baseResistanceCap", 0u);
e.castSoundId = je.value("castSoundId", 0u);
e.impactSoundId = je.value("impactSoundId", 0u);
e.combinedSchoolMask = je.value("combinedSchoolMask", 0u);
c.entries.push_back(e);
}
}
if (!wowee::pipeline::WoweeSpellSchoolLoader::save(c, outBase)) {
std::fprintf(stderr,
"import-wsch-json: failed to save %s.wsch\n", outBase.c_str());
return 1;
}
std::printf("Wrote %s.wsch\n", outBase.c_str());
std::printf(" source : %s\n", jsonPath.c_str());
std::printf(" schools : %zu\n", c.entries.size());
return 0;
}
feat(pipeline): add WSCH (Wowee Spell School) catalog 56th open format — replaces SpellSchools.dbc plus the Resistances.dbc resistance-cap tables. Defines damage schools spells use: Physical, Holy, Fire, Nature, Frost, Shadow, Arcane, plus combined / hybrid schools that count as multiple types simultaneously (Spellfire, Spellshadow, Spellfrost — relevant for resistance-bypass mechanics). 7 canonical schools with single-bit IDs (1, 2, 4, 8, 16, 32, 64) so combinedSchoolMask values line up directly with the spell engine's school-bit enum. Hybrid schools use high-bit IDs (0x80000001+) and their combinedSchoolMask references the canonical bits they qualify as. Each school carries visual identity (color tint, icon), gameplay rules (canBeImmune / canBeAbsorbed / canBeReflected / canCrit), resistance cap at max level, and cast / impact sound IDs. Cross-references with prior formats — castSoundId and impactSoundId point at WSND.soundId; combinedSchoolMask is a bitmask of OTHER WSCH.schoolId values within the same catalog. CLI: --gen-sch (3 base — Physical / Fire / Holy showing non-resistable Holy + non-reflectable Physical), --gen-sch- magical (6 canonical magical schools with proper colors + 365 max-level resistance caps), --gen-sch-combined (3 hybrids — Spellfire / Spellshadow / Spellfrost with multi-bit combinedSchoolMask), --info-wsch, --validate-wsch with --json variants. Validator catches id+name required, reflected-without-absorbed warning (reflected damage should be absorbable), self-referential combinedSchoolMask (school qualifying as itself), and combined-mask references to bits not defined in the same catalog (resolved at runtime across catalogs). Format graph: 55 → 56 binary formats. CLI flag count: 798 → 804.
2026-05-09 20:19:34 -07:00
int handleValidate(int& i, int argc, char** argv) {
std::string base = argv[++i];
bool jsonOut = consumeJsonFlag(i, argc, argv);
base = stripWschExt(base);
if (!wowee::pipeline::WoweeSpellSchoolLoader::exists(base)) {
std::fprintf(stderr,
"validate-wsch: WSCH not found: %s.wsch\n", base.c_str());
return 1;
}
auto c = wowee::pipeline::WoweeSpellSchoolLoader::load(base);
std::vector<std::string> errors;
std::vector<std::string> warnings;
if (c.entries.empty()) {
warnings.push_back("catalog has zero entries");
}
std::vector<uint32_t> idsSeen;
// Build the set of canonical (single-bit) school IDs so we
// can check that combinedSchoolMask only references real
// schools defined in this catalog.
std::vector<uint32_t> canonicalIds;
for (const auto& e : c.entries) {
// A canonical school is one whose schoolId is a single
// power of 2 (only one bit set). Hybrid schools have
// the high bit set or multiple bits.
if (e.schoolId != 0 &&
(e.schoolId & (e.schoolId - 1)) == 0 &&
e.schoolId < (1u << 16)) {
canonicalIds.push_back(e.schoolId);
}
}
auto schoolBitDefined = [&](uint32_t bit) {
for (uint32_t s : canonicalIds) if (s == bit) return true;
return false;
};
for (size_t k = 0; k < c.entries.size(); ++k) {
const auto& e = c.entries[k];
std::string ctx = "entry " + std::to_string(k) +
" (id=0x" +
[&]() {
char buf[16];
std::snprintf(buf, sizeof(buf),
"%08x", e.schoolId);
return std::string(buf);
}();
if (!e.name.empty()) ctx += " " + e.name;
ctx += ")";
if (e.schoolId == 0)
errors.push_back(ctx + ": schoolId is 0");
if (e.name.empty())
errors.push_back(ctx + ": name is empty");
// canBeReflected without canBeAbsorbed is unusual —
// reflected damage typically also goes through absorb.
if (e.canBeReflected && !e.canBeAbsorbed) {
warnings.push_back(ctx +
": canBeReflected=1 but canBeAbsorbed=0 "
"(reflected damage usually absorbable too)");
}
// combinedSchoolMask must only reference canonical
// school bits that exist in this catalog.
if (e.combinedSchoolMask != 0) {
for (int b = 0; b < 16; ++b) {
uint32_t bit = 1u << b;
if ((e.combinedSchoolMask & bit) &&
!schoolBitDefined(bit)) {
warnings.push_back(ctx +
": combinedSchoolMask references bit 0x" +
[&]() {
char buf[16];
std::snprintf(buf, sizeof(buf),
"%x", bit);
return std::string(buf);
}() +
" which isn't defined in this catalog");
}
}
// A hybrid school's combinedSchoolMask should not
// include itself (would be self-referential and
// confuses the resistance lookup).
if (e.combinedSchoolMask & e.schoolId) {
errors.push_back(ctx +
": combinedSchoolMask includes own schoolId "
"(self-referential)");
}
}
for (uint32_t prev : idsSeen) {
if (prev == e.schoolId) {
errors.push_back(ctx + ": duplicate schoolId");
break;
}
}
idsSeen.push_back(e.schoolId);
}
bool ok = errors.empty();
if (jsonOut) {
nlohmann::json j;
j["wsch"] = base + ".wsch";
j["ok"] = ok;
j["errors"] = errors;
j["warnings"] = warnings;
std::printf("%s\n", j.dump(2).c_str());
return ok ? 0 : 1;
}
std::printf("validate-wsch: %s.wsch\n", base.c_str());
if (ok && warnings.empty()) {
std::printf(" OK — %zu schools, all schoolIds unique, "
"all combined masks resolve\n", c.entries.size());
return 0;
}
if (!warnings.empty()) {
std::printf(" warnings (%zu):\n", warnings.size());
for (const auto& w : warnings)
std::printf(" - %s\n", w.c_str());
}
if (!errors.empty()) {
std::printf(" ERRORS (%zu):\n", errors.size());
for (const auto& e : errors)
std::printf(" - %s\n", e.c_str());
}
return ok ? 0 : 1;
}
} // namespace
bool handleSpellSchoolsCatalog(int& i, int argc, char** argv,
int& outRc) {
if (std::strcmp(argv[i], "--gen-sch") == 0 && i + 1 < argc) {
outRc = handleGenStarter(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--gen-sch-magical") == 0 &&
i + 1 < argc) {
outRc = handleGenMagical(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--gen-sch-combined") == 0 &&
i + 1 < argc) {
outRc = handleGenCombined(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--info-wsch") == 0 && i + 1 < argc) {
outRc = handleInfo(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--validate-wsch") == 0 && i + 1 < argc) {
outRc = handleValidate(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--export-wsch-json") == 0 && i + 1 < argc) {
outRc = handleExportJson(i, argc, argv); return true;
}
if (std::strcmp(argv[i], "--import-wsch-json") == 0 && i + 1 < argc) {
outRc = handleImportJson(i, argc, argv); return true;
}
feat(pipeline): add WSCH (Wowee Spell School) catalog 56th open format — replaces SpellSchools.dbc plus the Resistances.dbc resistance-cap tables. Defines damage schools spells use: Physical, Holy, Fire, Nature, Frost, Shadow, Arcane, plus combined / hybrid schools that count as multiple types simultaneously (Spellfire, Spellshadow, Spellfrost — relevant for resistance-bypass mechanics). 7 canonical schools with single-bit IDs (1, 2, 4, 8, 16, 32, 64) so combinedSchoolMask values line up directly with the spell engine's school-bit enum. Hybrid schools use high-bit IDs (0x80000001+) and their combinedSchoolMask references the canonical bits they qualify as. Each school carries visual identity (color tint, icon), gameplay rules (canBeImmune / canBeAbsorbed / canBeReflected / canCrit), resistance cap at max level, and cast / impact sound IDs. Cross-references with prior formats — castSoundId and impactSoundId point at WSND.soundId; combinedSchoolMask is a bitmask of OTHER WSCH.schoolId values within the same catalog. CLI: --gen-sch (3 base — Physical / Fire / Holy showing non-resistable Holy + non-reflectable Physical), --gen-sch- magical (6 canonical magical schools with proper colors + 365 max-level resistance caps), --gen-sch-combined (3 hybrids — Spellfire / Spellshadow / Spellfrost with multi-bit combinedSchoolMask), --info-wsch, --validate-wsch with --json variants. Validator catches id+name required, reflected-without-absorbed warning (reflected damage should be absorbable), self-referential combinedSchoolMask (school qualifying as itself), and combined-mask references to bits not defined in the same catalog (resolved at runtime across catalogs). Format graph: 55 → 56 binary formats. CLI flag count: 798 → 804.
2026-05-09 20:19:34 -07:00
return false;
}
} // namespace cli
} // namespace editor
} // namespace wowee