# WebBAR engine batch EB1 (parity plan M2, docs/engine-integration.md "Engine integration 5"): every solo match # takes its result from native GameOver (BAR game_end) instead of WebbarSkirmish's armcom/corcom judge, which gave # false results for Legion commanders and PvE teams; the practice controller only gives orders without BARb and # Lua AIs. New unsynced, read-only exports: whitelisted PvE GameRulesParams (webbarRules), RaptorEvent/ScavEvent # (webbarPveEvents, observed in CUnsyncedLuaHandle::RecvFromSynced like AddShieldHitDataHandler), a display-only # PvE unit catalog (webbarDisplayCatalog), own/allied experience and self-destruct (WXP1) and own/allied unit # damage with the victim ID (WDM1). Bridge 9, capabilities 4194303 (bits 17-21). --- a/rts/Game/WebbarBrowser.h +++ b/rts/Game/WebbarBrowser.h @@ -77,6 +77,7 @@ #include "WebbarResurrection.h" #include "WebbarNano.h" +#include "WebbarRules.h" #include "WebbarSkirmish.h" #include "WebbarReports.h" #include "WebbarPlacement.h" @@ -84,26 +85,16 @@ #include "WebbarTerrain.h" #include "WebbarTerrainFixture.h" -class WebbarNetworkResult final: public CEventClient -{ -public: - WebbarNetworkResult(): CEventClient("Webbar network result", -99999, false) { eventHandler.AddClient(this); } - bool WantsEvent(const std::string& name) override { return name == "GameOver"; } - void GameOver(const std::vector& winners) override { - // Spectators get a neutral result; the per-team won flags carry the winners. - const int outcome = (winners.empty() || gu->spectating) ? 3 : (std::find(winners.begin(), winners.end(), gu->myAllyTeam) != winners.end() ? 1 : 2); - PublishWebbarTeamStats(true, &winners); - MAIN_THREAD_EM_ASM({ if (Module['webbarMatch']) Module['webbarMatch']($0, $1, 0, 0, $0); }, gs->frameNum, outcome); - } -}; - static void PublishWebbarPlayState() { static bool catalogSent = false; if (!catalogSent) { catalogSent = true; - if (std::getenv("WEBBAR_NETWORK") != nullptr) { static WebbarNetworkResult result; } else { static WebbarStatsResult stats; } - MAIN_THREAD_EM_ASM({ if (Module['webbarProtocol']) Module['webbarProtocol'](9, 131071 /* bit 16: match recovery stream export + resume | restore | shields | cloak | resurrection | native work effects | area resurrection | manual launch | capture | smart trajectory | energy conversion | geothermal | selectability | build restrictions | queue edit | game speed | engine memory */); }); + GetWebbarMatchResult(); + GetWebbarUnitDamage(); + // EB1 bits: 17 solo result from BAR game_end | 18 PvE GameRulesParams | 19 RaptorEvent/ScavEvent | + // 20 display-only PvE catalog | 21 own/allied experience, self-destruct and damage reports. + MAIN_THREAD_EM_ASM({ if (Module['webbarProtocol']) Module['webbarProtocol'](9, 4194303 /* bits 17-21: EB1 (above) | bit 16: match recovery stream export + resume | restore | shields | cloak | resurrection | native work effects | area resurrection | manual launch | capture | smart trajectory | energy conversion | geothermal | selectability | build restrictions | queue edit | game speed | engine memory */); }); PublishWebbarEngineMemory(); MAIN_THREAD_EM_ASM({ if (Module['webbarIdentity']) Module['webbarIdentity']($0, $1, $2, $3); @@ -117,6 +108,8 @@ MAIN_THREAD_EM_ASM({ if (Module['webbarBuildOption']) Module['webbarBuildOption']($0, $1); }, def.id, target->id); } } + PublishWebbarDisplayCatalog(); + PublishWebbarRules(true); } const bool cloakCommandsChanged = PublishWebbarCloak(); if (cloakCommandsChanged || gs->frameNum % 30 == 0) { @@ -193,6 +186,9 @@ }, packet.data(), packet.size()); PublishWebbarShields(); if (gs->frameNum % 15 == 0) PublishWebbarUnitModifiers(); + if (gs->frameNum % 30 == 0) { PublishWebbarUnitExperience(); PublishWebbarRules(); } + PublishWebbarUnitDamage(); + PublishWebbarPveEvents(); if (gs->frameNum % 300 == 0) PublishWebbarEngineMemory(); if (gs->frameNum > 0 && gs->frameNum % (TeamStatistics::statsPeriod * GAME_SPEED) == 0) PublishWebbarTeamStats(false, nullptr); webbarWorkAudio::Publish(); @@ -208,6 +204,7 @@ if (!enabled || !game->playing || gs->frameNum < 0) return; GetWebbarEffects(); + GetWebbarMatchResult(); PollWebbarTerrainFixture(); if (std::getenv("WEBBAR_NETWORK") == nullptr) GetWebbarSkirmish(); static bool testSpeedSet = false; @@ -575,6 +572,8 @@ webbarCeg::pending.clear(); webbarCeg::dropped = 0; webbarCloakEvents::pending.clear(); webbarCloakEvents::dropped = 0; WebbarShieldHits::pending.clear(); WebbarShieldHits::dropped = 0; + WebbarPveEvents::pending.clear(); WebbarPveEvents::dropped = 0; + GetWebbarUnitDamage().records.clear(); GetWebbarUnitDamage().dropped = 0; WebbarEffects& effects = GetWebbarEffects(); effects.records.clear(); effects.webbarDamage.clear(); effects.musicDamage = 0.0f; effects.dropped = 0; effects.cegDropped = 0; } --- a/rts/Game/WebbarReports.h +++ b/rts/Game/WebbarReports.h @@ -41,25 +41,89 @@ MAIN_THREAD_EM_ASM({ if (Module['webbarTeamStats']) Module['webbarTeamStats'](HEAPU8.slice($0, $0 + $1).buffer); }, packet.data(), packet.size()); } -// Solo WebbarSkirmish outcome: 1 local ally team won, 2 the other ally teams, 3 none. -static void PublishWebbarSoloFinalStats(int outcome) +// EB1 own/allied unit reports for HUD widgets (rank icons, commander-hurt, self-destruct). +// Visibility follows the Lua API: GetUnitExperience/GetUnitSelfDTime are ally-only reads. +class WebbarUnitDamage final: public CEventClient { - if (outcome == 0 || webbarFinalStatsSent) return; - std::vector winners; - for (int a = 0; a < teamHandler.ActiveAllyTeams(); ++a) - if ((outcome == 1 && a == gu->myAllyTeam) || (outcome == 2 && a != gu->myAllyTeam && a != teamHandler.AllyTeam(teamHandler.GaiaTeamID()))) winners.push_back((unsigned char)a); - PublishWebbarTeamStats(true, &winners); -} +public: + // u32 victim, victimDef; f32 damage, paralyze; u32 attacker, attackerDef, weaponDef (0xffffffff when + // unknown or the attacker is not visible), frame. + struct Record { uint32_t victim, victimDef; float damage, paralyze; uint32_t attacker, attackerDef, weaponDef, frame; }; + static_assert(sizeof(Record) == 32); + WebbarUnitDamage(): CEventClient("Webbar unit damage", -99997, false) { eventHandler.AddClient(this); } + bool WantsEvent(const std::string& name) override { return name == "UnitDamaged"; } + int GetReadAllyTeam() const override { return AllAccessTeam; } + void UnitDamaged(const CUnit* unit, const CUnit* attacker, float damage, int weaponDefID, int, bool paralyzer) override { + if (gs->frameNum < 0 || damage <= 0.0f || unit == nullptr || !teamHandler.Ally(gu->myAllyTeam, unit->allyteam)) return; + if (records.size() >= 1024) { ++dropped; return; } + const bool seen = attacker != nullptr && WebbarVisible(attacker); + records.push_back({uint32_t(unit->id), uint32_t(unit->unitDef->id), paralyzer ? 0.0f : damage, paralyzer ? damage : 0.0f, + seen ? uint32_t(attacker->id) : 0xffffffffu, seen ? uint32_t(attacker->unitDef->id) : 0xffffffffu, + (seen && weaponDefID >= 0) ? uint32_t(weaponDefID) : 0xffffffffu, uint32_t(gs->frameNum)}); + } + std::vector records; + uint32_t dropped = 0; +}; +static WebbarUnitDamage& GetWebbarUnitDamage() { static WebbarUnitDamage damage; return damage; } -// Native GameOver (BAR game_end winners) in solo and network matches. -class WebbarStatsResult final: public CEventClient +// EB1: every match (solo and network) takes its result from native GameOver, i.e. BAR's game_end +// winners (deathmode, PvE teams, every commander definition). 1 local ally team won, 2 lost, +// 3 neutral (spectator or no winner). The periodic solo webbarMatch repeats it (WebbarWorld.h). +class WebbarMatchResult final: public CEventClient { public: - WebbarStatsResult(): CEventClient("Webbar team statistics", -99998, false) { eventHandler.AddClient(this); } + // Identity is taken when the match starts: a player whose team died is a spectator by GameOver, + // but still lost (2). Only players who spectate from the start get the neutral result. + WebbarMatchResult(): CEventClient("Webbar match result", -99999, false), allyTeam(gu->myAllyTeam), spectator(gu->spectating) { eventHandler.AddClient(this); } bool WantsEvent(const std::string& name) override { return name == "GameOver"; } - void GameOver(const std::vector& winners) override { PublishWebbarTeamStats(true, &winners); } + void GameOver(const std::vector& winners) override { + // Spectators get a neutral result; the per-team won flags carry the winners. + outcome = (winners.empty() || spectator) ? 3 : (std::find(winners.begin(), winners.end(), (unsigned char)allyTeam) != winners.end() ? 1 : 2); + frame = gs->frameNum; + std::string list; + for (const unsigned char ally: winners) list += (list.empty() ? "" : ",") + std::to_string(int(ally)); + LOG("[WebbarMatch] gameover frame=%d outcome=%d allyTeam=%d winners=%s", frame, outcome, allyTeam, list.c_str()); + PublishWebbarTeamStats(true, &winners); + MAIN_THREAD_EM_ASM({ if (Module['webbarMatch']) Module['webbarMatch']($0, $1, 0, 0, $0); }, gs->frameNum, outcome); + } + const int allyTeam; + const bool spectator; + int outcome = 0, frame = 0; }; +static WebbarMatchResult& GetWebbarMatchResult() { static WebbarMatchResult result; return result; } +// WDM1 (every published frame with records): header {magic, frame, count, dropped} + Record[count]. +static void PublishWebbarUnitDamage() +{ + auto& d = GetWebbarUnitDamage(); + if (d.records.empty() && d.dropped == 0) return; + const uint32_t header[] = {0x314d4457, uint32_t(gs->frameNum), uint32_t(d.records.size()), d.dropped}; + std::vector packet(sizeof(header) + d.records.size() * sizeof(WebbarUnitDamage::Record)); + std::memcpy(packet.data(), header, sizeof(header)); + if (!d.records.empty()) std::memcpy(packet.data() + sizeof(header), d.records.data(), d.records.size() * sizeof(WebbarUnitDamage::Record)); + MAIN_THREAD_EM_ASM({ if (Module['webbarUnitDamage']) Module['webbarUnitDamage'](HEAPU8.slice($0, $0 + $1).buffer); }, packet.data(), packet.size()); + d.records.clear(); d.dropped = 0; +} + +// WXP1 every 30 frames: own and allied units with experience or a running self-destruct countdown. +// Record {u32 id; f32 experience; i32 selfDCountdown (GetUnitSelfDTime units, 0 = none); u32 flags (1 own, 2 allied)}. +static void PublishWebbarUnitExperience() +{ + struct Record { uint32_t id; float experience; int32_t selfD; uint32_t flags; }; + static_assert(sizeof(Record) == 16); + std::vector records; + for (const CUnit* unit: unitHandler.GetActiveUnits()) { + if (unit->isDead || !teamHandler.Ally(gu->myAllyTeam, unit->allyteam) || (unit->experience <= 0.0f && unit->selfDCountdown <= 0)) continue; + if (records.size() >= 8192) break; + records.push_back({uint32_t(unit->id), unit->experience, int32_t(unit->selfDCountdown), unit->team == gu->myTeam ? 1u : 2u}); + } + const uint32_t header[] = {0x31505857, uint32_t(gs->frameNum), uint32_t(records.size()), 0}; + std::vector packet(sizeof(header) + records.size() * sizeof(Record)); + std::memcpy(packet.data(), header, sizeof(header)); + if (!records.empty()) std::memcpy(packet.data() + sizeof(header), records.data(), records.size() * sizeof(Record)); + MAIN_THREAD_EM_ASM({ if (Module['webbarUnitExperience']) Module['webbarUnitExperience'](HEAPU8.slice($0, $0 + $1).buffer); }, packet.data(), packet.size()); +} + // WRX1: owned units only. u32 id; f32 experience, maxHealth, armoredMultiple (1 when open); // u32 weaponCount (<= 8); then per weapon f32 reload seconds (XP-adjusted), f32 range. static void PublishWebbarUnitModifiers() --- a/rts/Game/WebbarSkirmish.h +++ b/rts/Game/WebbarSkirmish.h @@ -4,6 +4,9 @@ // A small, deterministic practice opponent. Enabled only by WEBBAR_PLAYABLE. // Every action uses native commands and normal resources, build times and LOS. // Its only prior knowledge is the two starting base locations in this scenario. +// EB1: it no longer judges the match. The result comes from BAR's game_end through native +// GameOver (WebbarMatchResult). It gives practice orders only when no BARb (WEBBAR_AI) and no +// Lua AI (Raptors, Scavengers) team exists. class WebbarSkirmish final: public CEventClient { public: @@ -26,22 +29,20 @@ } if (frame >= std::atoi(testFrames)) gu->globalQuit = true; } - if (outcome != 0) return; + if (practice < 0) { + practice = std::getenv("WEBBAR_AI") == nullptr ? 1 : 0; + for (const auto& [id, data]: skirmishAIHandler.GetAllSkirmishAIs()) + if (data != nullptr && data->isLuaAI) practice = 0; + } + if (practice == 0) return; CUnit* commander = nullptr; std::vector factories, tanks; - bool playerAlive = false; for (CUnit* unit: unitHandler.GetActiveUnits()) { - if (unit->isDead) continue; - if (unit->team == 0 && (unit->unitDef->name == "armcom" || unit->unitDef->name == "corcom")) playerAlive = true; - if (unit->team != 1) continue; + if (unit->isDead || unit->team != 1) continue; if (unit->unitDef->name == commanderName) commander = unit; if (unit->unitDef->IsFactoryUnit()) factories.push_back(unit); if (unit->unitDef->name == tankName && !unit->beingBuilt) tanks.push_back(unit); } - if (!playerAlive) { outcome = 2; finishedFrame = frame; return; } - if (commander == nullptr && factories.empty()) { outcome = 1; finishedFrame = frame; return; } - // BARb owns all opposing orders when selected; retain only result detection. - if (std::getenv("WEBBAR_AI") != nullptr) return; // Expand power and claim a second metal deposit. A blocked site is skipped; // no resources or units are created by this opponent controller. @@ -89,7 +90,7 @@ const char* towerName = armada ? "armllt" : "corllt"; const float playerX = EnvFloat("WEBBAR_PLAYER_X", 6500), playerZ = EnvFloat("WEBBAR_PLAYER_Z", 3330); const float enemyX = EnvFloat("WEBBAR_ENEMY_X", 7910), enemyZ = EnvFloat("WEBBAR_ENEMY_Z", 3110); - int outcome = 0, finishedFrame = 0, wave = 0, nextWave = 2700; + int wave = 0, nextWave = 2700, practice = -1; size_t site = 0; }; static WebbarSkirmish& GetWebbarSkirmish() { static WebbarSkirmish skirmish; return skirmish; } --- a/rts/Game/WebbarWorld.h +++ b/rts/Game/WebbarWorld.h @@ -86,8 +86,9 @@ std::memcpy(queuePacket.data() + sizeof(queueHeader), queues.data(), queues.size() * sizeof(QueueRecord)); MAIN_THREAD_EM_ASM({ if (Module['webbarQueues']) Module['webbarQueues'](HEAPU8.slice($0, $0 + $1).buffer); }, queuePacket.data(), queuePacket.size()); if (std::getenv("WEBBAR_NETWORK") == nullptr) { + // Practice waves from WebbarSkirmish; the outcome only from native GameOver (WebbarMatchResult). const auto& match = GetWebbarSkirmish(); - PublishWebbarSoloFinalStats(match.outcome); - MAIN_THREAD_EM_ASM({ if (Module['webbarMatch']) Module['webbarMatch']($0, $1, $2, $3, $4); }, gs->frameNum, match.outcome, match.wave, match.nextWave, match.finishedFrame); + const auto& result = GetWebbarMatchResult(); + MAIN_THREAD_EM_ASM({ if (Module['webbarMatch']) Module['webbarMatch']($0, $1, $2, $3, $4); }, gs->frameNum, result.outcome, match.wave, match.nextWave, result.frame); } } --- a/rts/Lua/LuaHandleSynced.cpp +++ b/rts/Lua/LuaHandleSynced.cpp @@ -6,6 +6,7 @@ #include "LuaInclude.h" #ifdef __EMSCRIPTEN__ #include "Game/WebbarShieldHits.h" +#include "Game/WebbarPveEvents.h" #endif #include "LuaUtils.h" @@ -198,6 +199,17 @@ if (frame >= 0 && frame <= gs->frameNum && frame == std::floor(frame) && unit >= 0 && unit < 65536 && unit == std::floor(unit)) { WebbarShieldHits::Emit(int(frame), int(unit), lua_tonumber(srcState, 4), lua_tonumber(srcState, 5), lua_tonumber(srcState, 6), lua_tonumber(srcState, 7), lua_toboolean(srcState, 8)); } + } + } + // BAR PvE status messages (raptor/scav spawner gadgets): type string, optional number, optional tech. + if (args >= 2 && lua_type(srcState, 1) == LUA_TSTRING && lua_type(srcState, 2) == LUA_TSTRING) { + const char* event = lua_tostring(srcState, 1); + const int source = std::strcmp(event, "RaptorEvent") == 0 ? 0 : std::strcmp(event, "ScavEvent") == 0 ? 1 : -1; + if (source >= 0) { + const double num = (args >= 3 && lua_type(srcState, 3) == LUA_TNUMBER) ? lua_tonumber(srcState, 3) : NAN; + // Only string values are read: lua_tostring would convert a number in place on the synced stack. + const char* tech = (args >= 4 && lua_type(srcState, 4) == LUA_TSTRING) ? lua_tostring(srcState, 4) : nullptr; + WebbarPveEvents::Emit(source, lua_tostring(srcState, 2), num, tech); } } #endif --- /dev/null +++ b/rts/Game/WebbarPveEvents.h @@ -0,0 +1,31 @@ +/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ +#pragma once + +#include +#include +#include +#include +#include +#include "Sim/Misc/GlobalSynced.h" + +// Read-only observation of BAR's existing unsynced PvE messages +// (SendToUnsynced("RaptorEvent"|"ScavEvent", type, num, tech) in raptor_spawner_defense.lua +// and scav_spawner_defense.lua). The browser has no LuaUI, so the message is copied before +// the Lua early-out. It never executes Lua or changes simulation state. +namespace WebbarPveEvents { +struct Record { int frame; int source; std::string type; double num; std::string tech; }; +inline std::vector pending; +inline uint32_t dropped = 0; + +// source: 0 RaptorEvent, 1 ScavEvent. num is NaN when absent or not a finite number. +inline void Emit(int source, const char* type, double num, const char* tech) +{ + static const bool playable = std::getenv("WEBBAR_PLAYABLE") != nullptr; + if (!playable || type == nullptr) return; + if (pending.size() >= 256) { ++dropped; return; } + std::string name(type), extra(tech != nullptr ? tech : ""); + if (name.size() > 48) name.resize(48); + if (extra.size() > 48) extra.resize(48); + pending.push_back({gs->frameNum, source, std::move(name), std::isfinite(num) ? num : NAN, std::move(extra)}); +} +} --- /dev/null +++ b/rts/Game/WebbarRules.h @@ -0,0 +1,140 @@ +/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ +#pragma once + +// EB1 PvE and widget data bridge. Unsynced, read-only observation: +// - whitelisted GameRulesParams (always readable by every player in Lua) every 30 frames, on change; +// - BAR RaptorEvent/ScavEvent messages (WebbarPveEvents.h); +// - a display-only catalog of PvE unit definitions (never buildable: WebbarSupports is unchanged). +#include +#include "Game/WebbarPveEvents.h" +#include "Lua/LuaHandleSynced.h" +#include "ExternalAI/SkirmishAIHandler.h" + +static void WebbarJsonString(std::string& out, const std::string& text) +{ + out += '"'; + for (const unsigned char c: text) { + if (c == '"' || c == '\\') { out += '\\'; out += char(c); } + else if (c < 0x20) { char escaped[8]; std::snprintf(escaped, sizeof(escaped), "\\u%04x", c); out += escaped; } + else out += char(c); + } + out += '"'; +} +static void WebbarJsonNumber(std::string& out, double value) +{ + if (!std::isfinite(value)) { out += "null"; return; } + char number[32]; std::snprintf(number, sizeof(number), "%.9g", value); out += number; +} + +// The HUD-relevant PvE rules: raptor*/Raptor*, scav*/Scav* (includes ScavBossAnger*), pveBossInfo, BossFightStarted. +static bool WebbarRulesWhitelisted(const std::string& name) +{ + const auto starts = [&](const char* prefix) { return name.rfind(prefix, 0) == 0; }; + return starts("raptor") || starts("Raptor") || starts("scav") || starts("Scav") || name == "pveBossInfo" || name == "BossFightStarted"; +} + +// {"name": value, ...} with every whitelisted param, sent when any of them changed. +static void PublishWebbarRules(bool force = false) +{ + static uint64_t lastHash = 0; + static bool sentAny = false; + std::vector> params; + for (const auto& [name, param]: CSplitLuaHandle::GetGameParams()) + if (WebbarRulesWhitelisted(name)) params.emplace_back(&name, ¶m); + if (params.empty() && !sentAny) return; + std::sort(params.begin(), params.end(), [](const auto& a, const auto& b) { return *a.first < *b.first; }); + std::string json = "{"; + for (const auto& [name, param]: params) { + if (json.size() > 1) json += ','; + WebbarJsonString(json, *name); json += ':'; + std::visit([&json](auto&& value) { + using T = std::decay_t; + if constexpr (std::is_same_v) json += value ? "true" : "false"; + else if constexpr (std::is_same_v) WebbarJsonNumber(json, value); + else WebbarJsonString(json, value); + }, param->value); + } + json += '}'; + WebbarHash hash; hash.String(json); + if (!force && sentAny && hash.value == lastHash) return; + lastHash = hash.value; sentAny = true; + MAIN_THREAD_EM_ASM({ if (Module['webbarRules']) Module['webbarRules']($0, UTF8ToString($1)); }, gs->frameNum, json.c_str()); +} + +// [[frame, source(0 raptor, 1 scav), type, num|null, tech|null], ...] plus the dropped count. +static void PublishWebbarPveEvents() +{ + if (WebbarPveEvents::pending.empty() && WebbarPveEvents::dropped == 0) return; + std::string json = "["; + for (const auto& e: WebbarPveEvents::pending) { + if (json.size() > 1) json += ','; + json += '['; WebbarJsonNumber(json, e.frame); json += ','; WebbarJsonNumber(json, e.source); json += ','; + WebbarJsonString(json, e.type); json += ','; WebbarJsonNumber(json, e.num); json += ','; + if (e.tech.empty()) json += "null"; else WebbarJsonString(json, e.tech); + json += ']'; + } + json += ']'; + MAIN_THREAD_EM_ASM({ if (Module['webbarPveEvents']) Module['webbarPveEvents']($0, UTF8ToString($1), $2); }, gs->frameNum, json.c_str(), WebbarPveEvents::dropped); + WebbarPveEvents::pending.clear(); WebbarPveEvents::dropped = 0; +} + +// Which PvE Lua AIs are in this match (bit 0 Raptors, bit 1 Scavengers), from the start script's AI slots. +static int WebbarPveTeams() +{ + int mask = 0; + for (const auto& [id, data]: skirmishAIHandler.GetAllSkirmishAIs()) { + if (data == nullptr || !data->isLuaAI) continue; + if (data->shortName.find("Raptors") != std::string::npos) mask |= 1; + if (data->shortName.find("Scavengers") != std::string::npos) mask |= 2; + } + return mask; +} + +// Display-only definitions for PvE units (names, labels, health, cost) so the page can label and +// draw raptors and scavengers. Sent once, only when a PvE team exists. Build gating is unchanged. +static void PublishWebbarDisplayCatalog() +{ + const int pve = WebbarPveTeams(); + if (pve == 0) return; + std::string json = "["; + uint32_t count = 0; + for (const UnitDef& def: unitDefHandler->GetUnitDefsVec()) { + if (def.id <= 0 || WebbarSupports(&def)) continue; + const std::string& name = def.name; + const bool raptor = (pve & 1) && name.rfind("raptor", 0) == 0; + const bool scav = (pve & 2) && (name.rfind("scav", 0) == 0 || (name.size() > 5 && name.compare(name.size() - 5, 5, "_scav") == 0)); + if (!raptor && !scav) continue; + if (json.size() > 1) json += ','; + json += "{\"id\":"; WebbarJsonNumber(json, def.id); + json += ",\"name\":"; WebbarJsonString(json, name); + json += ",\"title\":"; WebbarJsonString(json, def.humanName); + json += ",\"tooltip\":"; WebbarJsonString(json, def.tooltip); + json += ",\"source\":"; json += raptor ? "\"raptors\"" : "\"scavengers\""; + json += ",\"health\":"; WebbarJsonNumber(json, def.health); + json += ",\"metal\":"; WebbarJsonNumber(json, def.cost.metal); + json += ",\"energy\":"; WebbarJsonNumber(json, def.cost.energy); + json += ",\"buildTime\":"; WebbarJsonNumber(json, def.buildTime); + json += ",\"speed\":"; WebbarJsonNumber(json, def.speed); + json += ",\"width\":"; WebbarJsonNumber(json, def.xsize * SQUARE_SIZE); + json += ",\"depth\":"; WebbarJsonNumber(json, def.zsize * SQUARE_SIZE); + json += ",\"model\":"; WebbarJsonString(json, def.modelName); + json += ",\"mobile\":"; json += def.IsImmobileUnit() ? "false" : "true"; + json += ",\"air\":"; json += def.canfly ? "true" : "false"; + json += ",\"armed\":"; json += def.CanDamage() ? "true" : "false"; + json += ",\"builder\":"; json += def.IsBuilderUnit() ? "true" : "false"; + json += ",\"factory\":"; json += def.IsFactoryUnit() ? "true" : "false"; + json += ",\"customParams\":{"; + bool first = true; + for (const char* key: {"iscommander", "isscavenger", "fromunit", "i18nfromunit", "techlevel", "subfolder", "unitgroup", "raptorsquadbehavior"}) { + const auto found = def.customParams.find(key); + if (found == def.customParams.end()) continue; + if (!first) json += ','; + first = false; + WebbarJsonString(json, key); json += ':'; WebbarJsonString(json, found->second); + } + json += "}}"; + ++count; + } + json += ']'; + MAIN_THREAD_EM_ASM({ if (Module['webbarDisplayCatalog']) Module['webbarDisplayCatalog'](UTF8ToString($0), $1, $2); }, json.c_str(), count, pve); +}