--- a/rts/Game/WebbarWorld.h +++ b/rts/Game/WebbarWorld.h @@ -1,5 +1,6 @@ /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ #pragma once +#include "WebbarInfoTexture.h" // Low-frequency, read-only UI data. Hidden enemies never enter the unit, pose, // feature or queue streams. Radar contacts contain positions, not unit identity. @@ -17,8 +18,11 @@ std::vector contacts; for (const CUnit* unit: unitHandler.GetActiveUnits()) if (!WebbarVisible(unit) && losHandler->InRadar(unit, gu->myAllyTeam)) contacts.push_back(unit->GetErrorPos(gu->myAllyTeam)); - const uint32_t visionHeader[] = {0x31564257, uint32_t(gs->frameNum), grid, uint32_t(contacts.size()), uint32_t(mapDims.mapx * SQUARE_SIZE), uint32_t(mapDims.mapy * SQUARE_SIZE), 0, 0}; - std::vector vision(sizeof(visionHeader) + grid * grid + contacts.size() * sizeof(float3)); + const bool infoGlobal = gu->spectatingFullView || losHandler->GetGlobalLOS(gu->myAllyTeam); + const auto infoPlane = [&](const auto& type) { return WebbarInfoTexture::Binary(type.size.x, type.size.y, type.losMaps[gu->myAllyTeam].GetLosMap(), infoGlobal); }; + const auto nativeInfo = WebbarInfoTexture::Packet(infoPlane(losHandler->los), infoPlane(losHandler->airLos), infoPlane(losHandler->radar), losHandler->jammer.losMaps[modInfo.separateJammers ? gu->myAllyTeam : 0].GetLosMap(), infoGlobal); + const uint32_t visionHeader[] = {0x31564257, uint32_t(gs->frameNum), grid, uint32_t(contacts.size()), uint32_t(mapDims.mapx * SQUARE_SIZE), uint32_t(mapDims.mapy * SQUARE_SIZE), nativeInfo.empty() ? 0 : WebbarInfoTexture::MAGIC, uint32_t(nativeInfo.size())}; + std::vector vision(sizeof(visionHeader) + grid * grid + contacts.size() * sizeof(float3) + nativeInfo.size()); std::memcpy(vision.data(), visionHeader, sizeof(visionHeader)); for (uint32_t z = 0; z < grid; ++z) for (uint32_t x = 0; x < grid; ++x) { const float px = (x + 0.5f) * visionHeader[4] / grid, pz = (z + 0.5f) * visionHeader[5] / grid; @@ -26,6 +30,7 @@ vision[sizeof(visionHeader) + z * grid + x] = gu->spectatingFullView ? 3 : ((losHandler->InLos(pos, gu->myAllyTeam) ? 1 : 0) | (losHandler->InRadar(pos, gu->myAllyTeam) ? 2 : 0)); } std::memcpy(vision.data() + sizeof(visionHeader) + grid * grid, contacts.data(), contacts.size() * sizeof(float3)); + if (!nativeInfo.empty()) std::memcpy(vision.data() + sizeof(visionHeader) + grid * grid + contacts.size() * sizeof(float3), nativeInfo.data(), nativeInfo.size()); MAIN_THREAD_EM_ASM({ if (Module['webbarVision']) Module['webbarVision'](HEAPU8.slice($0, $0 + $1).buffer); }, vision.data(), vision.size()); struct FeatureRecord { uint32_t id, def; float metal, energy, reclaim; float matrix[16]; }; --- /dev/null +++ b/rts/Game/WebbarInfoTexture.h @@ -0,0 +1,62 @@ +/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ +#pragma once +#include +#include +#include +#include +#include + +// Read-only equivalents of Modern/{Los,AirLos,Radar}.cpp's information inputs. +// The RG radar texture masks jamming by filtered ground LOS BEFORE publication; +// neither unseen jammer coverage nor enemy unit identities enter this extension. +namespace WebbarInfoTexture { +constexpr uint32_t MAGIC = 0x31495657; // WVI1, optional WBV1 trailing payload +struct Plane { uint32_t width, height; std::vector pixels; }; +template Plane Binary(uint32_t width, uint32_t height, const T& refs, bool global) { + Plane result{width, height, {}}; + if (!width || !height || width > 8192 || height > 8192 || size_t(width)*height != refs.size()) return result; + result.pixels.resize(refs.size()); + for (size_t i = 0; i < refs.size(); ++i) result.pixels[i] = (global || refs[i] > 0) ? 255 : 0; + return result; +} +// CLosTexture uses linear filtering within the nearest mip. Radar's fullscreen +// target has the same map aspect and a power-of-two sensor-mip ratio. +inline Plane RadarLosMip(Plane los, uint32_t radarWidth, uint32_t radarHeight) { + const double ratio = std::max(double(los.width)/radarWidth, double(los.height)/radarHeight); + const int lod = std::max(0, int(std::floor(std::log2(ratio) + 0.5))); + for (int level = 0; level < lod && (los.width > 1 || los.height > 1); ++level) { + Plane next{std::max(1u, los.width/2), std::max(1u, los.height/2), {}}; + next.pixels.resize(size_t(next.width)*next.height); + for (uint32_t y = 0; y < next.height; ++y) for (uint32_t x = 0; x < next.width; ++x) { + unsigned sum = 0; + for (unsigned dy = 0; dy < 2; ++dy) for (unsigned dx = 0; dx < 2; ++dx) + sum += los.pixels[std::min(los.height-1, y*2+dy)*los.width + std::min(los.width-1, x*2+dx)]; + next.pixels[y*next.width+x] = (sum+2)/4; + } + los = std::move(next); + } + return los; +} +inline uint8_t Sample(const Plane& los, uint32_t x, uint32_t y, uint32_t width, uint32_t height) { + const double px = (x+0.5)*los.width/width-0.5, py = (y+0.5)*los.height/height-0.5; + const int ix = int(std::floor(px)), iy = int(std::floor(py)); + const double fx = px-ix, fy = py-iy; + const auto at = [&](int a, int b) { return los.pixels[std::clamp(b,0,int(los.height)-1)*los.width+std::clamp(a,0,int(los.width)-1)]; }; + return uint8_t(std::lround((at(ix,iy)*(1-fx)+at(ix+1,iy)*fx)*(1-fy)+(at(ix,iy+1)*(1-fx)+at(ix+1,iy+1)*fx)*fy)); +} +template std::vector Packet(const Plane& ground, const Plane& air, const Plane& radar, const T& jammer, bool global) { + const size_t count = radar.pixels.size(), length = 32+ground.pixels.size()+air.pixels.size()+count*2; + if (ground.pixels.empty() || air.pixels.empty() || count == 0 || jammer.size() != count || length > 32*1024*1024) return {}; + const uint32_t header[] = {1, ground.width, ground.height, air.width, air.height, radar.width, radar.height, uint32_t(global)}; + std::vector result(length); std::memcpy(result.data(), header, sizeof(header)); + size_t offset = sizeof(header); + for (const auto* plane: {&ground,&air}) { std::memcpy(result.data()+offset, plane->pixels.data(), plane->pixels.size()); offset += plane->pixels.size(); } + const Plane los = RadarLosMip(ground, radar.width, radar.height); + for (uint32_t y = 0; y < radar.height; ++y) for (uint32_t x = 0; x < radar.width; ++x) { + const size_t i = y*radar.width+x; + result[offset+i*2] = radar.pixels[i]; + result[offset+i*2+1] = (!global && jammer[i] > 0) ? Sample(los,x,y,radar.width,radar.height) : 0; + } + return result; +} +}