diff --git a/rts/Game/WebbarBrowser.h b/rts/Game/WebbarBrowser.h --- a/rts/Game/WebbarBrowser.h +++ b/rts/Game/WebbarBrowser.h @@ -104,6 +104,7 @@ #include "WebbarPoses.h" #include "WebbarQueueActions.h" #include "WebbarQuotaOwnership.h" +#include "WebbarLegion.h" static void PublishWebbarPlayState() { @@ -115,7 +116,7 @@ // EB4 (M5) bits: 22 pose mailbox (only used when the page asks for it) | 23 removals by cause. // 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, 1107296255 /* bit 30: complete queue actions, original factory modifiers and owned factory provenance | bit 24: read-only replay playback, controls and perspectives | bit 23: unit removals by cause (WLC1) | bit 22: pose mailbox (WEBBAR_POSE_MAILBOX, WebbarPoses.h) | 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 */); }); + MAIN_THREAD_EM_ASM({ if (Module['webbarProtocol']) Module['webbarProtocol'](9, 1375731711 /* bit 30: complete queue actions, original factory modifiers and owned factory provenance | bit 28: Legion carrier spawn switch and owned reactive armor (WRA1) | bit 24: read-only replay playback, controls and perspectives | bit 23: unit removals by cause (WLC1) | bit 22: pose mailbox (WEBBAR_POSE_MAILBOX, WebbarPoses.h) | 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); @@ -141,12 +142,14 @@ // publication in their current ownership lifetime) are sent; the browser merges by unit. std::vector words(1, 0); const auto pointer = [](const std::string& value) { return uint32_t(reinterpret_cast(value.c_str())); }; + webbarCarrierModes.clear(); for (const CUnit* unit: unitHandler.GetActiveUnits()) { if (unit->team != gu->myTeam) continue; const auto& commands = unit->commandAI->GetPossibleCommands(); WebbarHash signature; signature.Word(uint32_t(commands.size())); for (const auto* command: commands) { + if (command->id == 31200 && !unit->isDead) webbarCarrierModes.emplace(unit->id, ReadWebbarCarrierMode(*command)); signature.Word(uint32_t(command->id)); signature.Word(uint32_t(command->type)); signature.Word(uint32_t(command->disabled) | (uint32_t(command->hidden) << 1)); signature.String(command->action); signature.String(command->name); signature.String(command->tooltip); signature.Word(uint32_t(command->params.size())); @@ -212,6 +215,7 @@ PublishWebbarUnitDamage(); PublishWebbarPveEvents(); PublishWebbarQuotaOwnership(); } + PublishWebbarReactiveArmor(); if (gs->frameNum % 300 == 0) PublishWebbarEngineMemory(); if (webbarStateRefresh || (gs->frameNum > 0 && gs->frameNum % (TeamStatistics::statsPeriod * GAME_SPEED) == 0)) PublishWebbarTeamStats(false, nullptr); { SCOPED_TIMER("Webbar::Publish::State::WorkAudio"); webbarWorkAudio::Publish(); } @@ -253,6 +257,7 @@ }, lastPaused, gs->frameNum); } if (!gs->paused) webbarPausedCommandsChanged = false; + else if (!catchingUp && WebbarCarrierModesChanged()) webbarPausedCommandsChanged = true; if (gs->paused && !catchingUp && webbarPausedCommandsChanged) { webbarPausedCommandsChanged = false; webbarStateRefresh = true; @@ -340,8 +345,9 @@ } else { command.PushPos(float3(order.x, CGround::GetHeightReal(order.x, order.z), order.z)); } - } else if (order.kind == CMD_FIRE_STATE || order.kind == CMD_MOVE_STATE || order.kind == CMD_REPEAT || order.kind == CMD_ONOFF || order.kind == 37382 /* BAR CMD_WANT_CLOAK */ || order.kind == CMD_TRAJECTORY || order.kind == 37384 /* BAR SMART_TOGGLE */ || order.kind == CMD_AUTOREPAIRLEVEL || order.kind == CMD_IDLEMODE) { + } else if (order.kind == CMD_FIRE_STATE || order.kind == CMD_MOVE_STATE || order.kind == CMD_REPEAT || order.kind == CMD_ONOFF || order.kind == 37382 /* BAR CMD_WANT_CLOAK */ || order.kind == CMD_TRAJECTORY || order.kind == 37384 /* BAR SMART_TOGGLE */ || order.kind == CMD_AUTOREPAIRLEVEL || order.kind == CMD_IDLEMODE || order.kind == 31200 /* BAR CARRIER_SPAWN_ONOFF */) { requireCapability(order.kind); + if (order.kind == 31200 && (order.target > 1 || order.options != 0)) status = 6; for (int id: ids) { const auto* desc = description(id, order.kind); if (desc->type != CMDTYPE_ICON_MODE || desc->params.size() < 2 || order.target >= desc->params.size() - 1) status = 6; diff --git a/rts/Game/WebbarLegion.h b/rts/Game/WebbarLegion.h new file mode 100644 --- /dev/null +++ b/rts/Game/WebbarLegion.h @@ -0,0 +1,68 @@ +/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ +#pragma once + +// BAR consumes the spawn toggle in AllowCommand, before UnitCommand observers +// run. Watch only carriers seen in the last native descriptor publication, so a +// paused toggle can trigger the existing full refresh without advancing sync. +struct WebbarCarrierMode { + std::string value; + int type = -1; + bool disabled = false, hidden = false; + bool operator==(const WebbarCarrierMode&) const = default; +}; +static std::unordered_map webbarCarrierModes; +static WebbarCarrierMode ReadWebbarCarrierMode(const SCommandDescription& command) +{ + return {command.params.empty() ? std::string() : command.params[0], command.type, command.disabled, command.hidden}; +} +static bool WebbarCarrierModesChanged() +{ + for (const auto& [id, previous]: webbarCarrierModes) { + const CUnit* unit = unitHandler.GetUnit(id); + WebbarCarrierMode current; + if (unit != nullptr && !unit->isDead && unit->team == gu->myTeam) { + for (const auto* command: unit->commandAI->GetPossibleCommands()) + if (command->id == 31200) { current = ReadWebbarCarrierMode(*command); break; } + } + if (current != previous) return true; + } + return false; +} + +// WRA1: original reactiveArmorHealth, never inferred from ordinary health. +// Header {magic, frame, count, dropped, team, flags(reset=1, complete=4)}; +// record {unit, ownershipLifetime, kind(0=false, 1=float), f32 value}. +// Each publication replaces the whole map at the exact snapshot frame. Full +// replay view does not widen ownership; an ordinary spectator receives empty. +static void PublishWebbarReactiveArmor() +{ + struct Record { uint32_t unit, lifetime, kind; float value; }; + static_assert(sizeof(Record) == 16); + static int lastTeam = -1; + static bool lastSpectating = false, lastFullView = false; + const bool reset = webbarStateRefresh || lastTeam != gu->myTeam || lastSpectating != gu->spectating || lastFullView != gu->spectatingFullView; + lastTeam = gu->myTeam; lastSpectating = gu->spectating; lastFullView = gu->spectatingFullView; + const auto& entries = GetWebbarQuotaOwnership().entries; + std::vector records; + uint32_t dropped = 0; + if (!gu->spectating || webbarReplay::Enabled()) for (const CUnit* unit: unitHandler.GetActiveUnits()) { + if (unit->isDead || unit->team != gu->myTeam) continue; + const auto param = unit->modParams.find("reactiveArmorHealth"); + if (param == unit->modParams.end()) continue; + const float* value = std::get_if(¶m->second.value); + const bool* broken = std::get_if(¶m->second.value); + if (value != nullptr) { + if (!std::isfinite(*value) || *value < 0.0f) continue; + } else if (broken == nullptr || *broken) continue; + const auto known = entries.find(unit->id); + if (records.size() >= 4096 || known == entries.end() || known->second.team != gu->myTeam) { ++dropped; continue; } + records.push_back({uint32_t(unit->id), known->second.lifetime, value != nullptr ? 1u : 0u, value != nullptr ? *value : 0.0f}); + } + // A bounded/incomplete sample must not leave a convincing partial armor map. + if (dropped != 0) { dropped += uint32_t(records.size()); records.clear(); } + const uint32_t header[] = {0x31415257, uint32_t(gs->frameNum), uint32_t(records.size()), dropped, uint32_t(gu->myTeam), uint32_t(reset) | (uint32_t(dropped == 0) << 2)}; + 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['webbarReactiveArmor']) Module['webbarReactiveArmor'](HEAPU8.slice($0, $0 + $1).buffer); }, packet.data(), packet.size()); +}