--- a/rts/Lua/LuaHandleSynced.cpp +++ b/rts/Lua/LuaHandleSynced.cpp @@ -7,6 +7,7 @@ #ifdef __EMSCRIPTEN__ #include "Game/WebbarShieldHits.h" #include "Game/WebbarPveEvents.h" +#include "WebbarDistortionForwarders.h" #endif #include "LuaUtils.h" @@ -158,6 +159,11 @@ } lua_settop(L, 0); +#ifdef __EMSCRIPTEN__ + if (GetName()=="LuaRules" && std::getenv("WEBBAR_PLAYABLE") && std::getenv("WEBBAR_NATIVE_DISTORTION_VBO")) { + if (!LoadCode(L, WEBBAR_DISTORTION_FORWARDERS, "Original distortion event forwarder admission")) { KillLua(); return false; } + } +#endif if (!LoadCode(L, std::move(code), file)) { KillLua(); return false; --- /dev/null +++ b/rts/Lua/WebbarDistortionForwarders.h @@ -0,0 +1,21 @@ +/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ +#pragma once +// Host admission only; original gadget bodies and visibility checks are unchanged. +static constexpr const char* WEBBAR_DISTORTION_FORWARDERS=R"LUA( +if not Platform or not Platform.isHeadless then return end +local aliases={ + ['luarules/gadgets/webbar_native_distortion_explosions.lua']='LuaRules/Gadgets/gfx_explosion_lights.lua', + ['luarules/gadgets/webbar_native_distortion_unit_script.lua']='LuaRules/Gadgets/gfx_unit_script_lights.lua', +} +local list,load=VFS.DirList,VFS.LoadFile +local function normalized(path)return type(path)=='string' and string.lower(path:gsub('\\','/')) or '' end +VFS.DirList=function(path,pattern,...) + local files=list(path,pattern,...) + if normalized(path):gsub('/+$','')=='luarules/gadgets' and pattern=='*.lua' then + files[#files+1]='LuaRules/Gadgets/webbar_native_distortion_explosions.lua' + files[#files+1]='LuaRules/Gadgets/webbar_native_distortion_unit_script.lua' + end + return files +end +VFS.LoadFile=function(path,...)return load(aliases[normalized(path)] or path,...)end +)LUA"; --- a/rts/Game/Game.cpp +++ b/rts/Game/Game.cpp @@ -294,6 +294,7 @@ #if defined(__EMSCRIPTEN__) static void FreeWebbarNativeBeams(); static void PrepareWebbarNativeBeams(); +static void InitializeWebbarReplayView(); #endif CGame::~CGame() @@ -871,6 +872,10 @@ { ZoneScoped; assert(!(dryRun && onlyUnsynced)); + #if defined(__EMSCRIPTEN__) + // Original unsynced gadgets/widgets cache this identity during Initialize. + if (!dryRun && webbarReplay::Enabled()) InitializeWebbarReplayView(); + #endif // Lua components ENTER_SYNCED_CODE(); CLuaHandle::SetDevMode(gameSetup->luaDevMode); --- a/rts/Game/WebbarNativeBeams.h +++ b/rts/Game/WebbarNativeBeams.h @@ -205,6 +205,7 @@ __atomic_add_fetch(&webbarBeamDrawMailbox.counters[2],1u,__ATOMIC_RELAXED); if(request.flames) PublishFlames(sequence); } + bool HasRewound() const { return gs->frameNummyAllyTeam && full==gu->spectatingFullView && gs->frameNum>=lastFrame; } uint32_t epoch=0; private: @@ -335,7 +336,12 @@ webbarBeamDrawMailbox.version=WebbarDistortionEvents::Enabled()?3:WebbarFlamesEnabled()?2:1;webbarBeamDrawMailbox.bytes=webbarBeamDrawMailbox.version>=2?512:256; MAIN_THREAD_EM_ASM({ if(Module['webbarNativeBeamMailbox']) Module['webbarNativeBeamMailbox']($0,$1); },&webbarBeamDrawMailbox,webbarBeamDrawMailbox.bytes); } - if(webbarNativeBeams && !webbarNativeBeams->MatchesView()) FreeWebbarNativeBeams(); + if(webbarNativeBeams && !webbarNativeBeams->MatchesView()) { + // Beam caches are view-specific. Original distortion wind and world-effect + // lifetimes survive a spectator switch, as in native LuaUI. + if(webbarNativeBeams->HasRewound()) FreeWebbarNativeDistortion(); + delete webbarNativeBeams;webbarNativeBeams=nullptr; + } if(!webbarNativeBeams) { webbarNativeBeams=new WebbarNativeBeamRenderer; webbarNativeBeams->epoch=++epoch; --- a/rts/Game/WebbarNativeDistortion.h +++ b/rts/Game/WebbarNativeDistortion.h @@ -59,6 +59,7 @@ ~WebbarNativeDistortion()override{if(WebbarDistortionEvents::handle==this)WebbarDistortionEvents::handle=nullptr;KillLua();} bool WantsEvent(const std::string& name)override{return ready&&(name=="GameFrame"||name=="CollectGarbage"||CLuaHandle::WantsEvent(name));} void GameFrame(int frame)override{if(!ready)return;lua_getglobal(L,"WebbarDistortionSim");lua_pushnumber(L,frame);ready=RunCallIn(L,LuaHashString("WebbarDistortionSim"),1,0);if(!ready)Report();} + void SetEpoch(uint32_t next){epoch=next;} void PlayerChanged(int player)override{Perspective();CLuaHandle::PlayerChanged(player);} void Publish(const WebbarBeamDrawRequest& next,int32_t sequence){ if(!ready)return;request=next;const int width=int(next.distortionViewport[0]),height=int(next.distortionViewport[1]); @@ -119,5 +120,5 @@ }; static WebbarNativeDistortion* webbarNativeDistortion=nullptr; static void FreeWebbarNativeDistortion(){delete webbarNativeDistortion;webbarNativeDistortion=nullptr;} -static void PrepareWebbarNativeDistortion(uint32_t epoch){if(WebbarDistortionEvents::Enabled()&&!webbarNativeDistortion)webbarNativeDistortion=new WebbarNativeDistortion(epoch);} +static void PrepareWebbarNativeDistortion(uint32_t epoch){if(!WebbarDistortionEvents::Enabled())return;if(webbarNativeDistortion)webbarNativeDistortion->SetEpoch(epoch);else webbarNativeDistortion=new WebbarNativeDistortion(epoch);} static void PublishWebbarNativeDistortion(const WebbarBeamDrawRequest& request,int32_t sequence){if(webbarNativeDistortion)webbarNativeDistortion->Publish(request,sequence);} --- a/rts/Game/WebbarReplayBrowser.h +++ b/rts/Game/WebbarReplayBrowser.h @@ -6,19 +6,28 @@ static int webbarReplayTeam = webbarReplay::EnvironmentInt("WEBBAR_REPLAY_TEAM", 0); static bool webbarReplayFullView = webbarReplay::EnvironmentInt("WEBBAR_REPLAY_FULL_VIEW", 1) != 0; +static void InitializeWebbarReplayView() +{ + if (!teamHandler.IsValidTeam(webbarReplayTeam) || webbarReplayTeam == teamHandler.GaiaTeamID()) webbarReplayTeam = 0; + gu->myTeam=webbarReplayTeam; + gu->myAllyTeam=teamHandler.AllyTeam(webbarReplayTeam); + gu->spectating=true; + gu->spectatingFullView=webbarReplayFullView; + gu->spectatingFullSelect=true; + MAIN_THREAD_EM_ASM({ if (Module['webbarIdentity']) Module['webbarIdentity']($0, $1, $2, 1); }, gu->myPlayerNum, gu->myTeam, gu->myAllyTeam); +} + static void ApplyWebbarReplayView() { - if (!teamHandler.IsValidTeam(webbarReplayTeam) || webbarReplayTeam == teamHandler.GaiaTeamID()) webbarReplayTeam = 0; + if (!teamHandler.IsValidTeam(webbarReplayTeam) || webbarReplayTeam == teamHandler.GaiaTeamID()) webbarReplayTeam = 0; if (gu->myTeam != webbarReplayTeam || gu->spectatingFullView != webbarReplayFullView || !gu->spectating) { DiscardWebbarPresentationQueues(); webbarStateRefresh = true; webbarCloakEvents::publishedCommandSignatures.clear(); - gu->myTeam = webbarReplayTeam; - gu->myAllyTeam = teamHandler.AllyTeam(webbarReplayTeam); - gu->spectating = true; - gu->spectatingFullView = webbarReplayFullView; - gu->spectatingFullSelect = true; - MAIN_THREAD_EM_ASM({ if (Module['webbarIdentity']) Module['webbarIdentity']($0, $1, $2, 1); }, gu->myPlayerNum, gu->myTeam, gu->myAllyTeam); + InitializeWebbarReplayView(); + // The original explosion/unit-script forwarders and visual tracker update + // their cached visibility through the same unsynced event as /specteam. + eventHandler.PlayerChanged(gu->myPlayerNum); } }