Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "JSInterface_Game.h" 21 : 22 : #include "graphics/HeightMipmap.h" 23 : #include "graphics/Terrain.h" 24 : #include "lib/config2.h" 25 : #include "lib/debug.h" 26 : #include "lib/file/vfs/vfs.h" 27 : #include "lib/os_path.h" 28 : #include "lib/path.h" 29 : #include "network/NetClient.h" 30 : #include "network/NetServer.h" 31 : #include "ps/CLogger.h" 32 : #include "ps/Filesystem.h" 33 : #include "ps/Game.h" 34 : #include "ps/GameSetup/GameSetup.h" 35 : #include "ps/Replay.h" 36 : #include "ps/World.h" 37 : #include "scriptinterface/FunctionWrapper.h" 38 : #include "scriptinterface/Request.h" 39 : #include "scriptinterface/StructuredClone.h" 40 : #include "simulation2/Simulation2.h" 41 : #include "simulation2/system/TurnManager.h" 42 : #include "soundmanager/ISoundManager.h" 43 : 44 : #include <js/RootingAPI.h> 45 : #include <js/TypeDecls.h> 46 : #include <js/Value.h> 47 : #include <stdexcept> 48 : #include <string> 49 : 50 : namespace Script { class Interface; } 51 : 52 : namespace JSI_Game 53 : { 54 0 : void StartGame(const Script::Interface& guiInterface, JS::HandleValue attribs, int playerID, bool storeReplay) 55 : { 56 0 : ENSURE(!g_NetServer); 57 0 : ENSURE(!g_NetClient); 58 0 : ENSURE(!g_Game); 59 : 60 0 : g_Game = new CGame(storeReplay); 61 : 62 : // Convert from GUI script context to sim script context/ 63 0 : CSimulation2* sim = g_Game->GetSimulation2(); 64 0 : Script::Request rqSim(sim->GetScriptInterface()); 65 : 66 0 : JS::RootedValue gameAttribs(rqSim.cx, Script::CloneValueFromOtherCompartment(sim->GetScriptInterface(), guiInterface, attribs)); 67 : 68 0 : g_Game->SetPlayerID(playerID); 69 0 : g_Game->StartGame(&gameAttribs, ""); 70 0 : } 71 : 72 0 : void Script_EndGame() 73 : { 74 0 : EndGame(); 75 0 : } 76 : 77 0 : int GetPlayerID() 78 : { 79 0 : if (!g_Game) 80 : return -1; 81 : 82 0 : return g_Game->GetPlayerID(); 83 : } 84 : 85 0 : void SetPlayerID(int id) 86 : { 87 0 : if (!g_Game) 88 : return; 89 : 90 0 : int currentID = g_Game->GetPlayerID(); 91 0 : if (currentID == id) 92 : return; 93 : 94 0 : if (g_Game->CheatsEnabled() || g_Game->IsVisualReplay()) 95 0 : g_Game->SetPlayerID(id); 96 : else 97 0 : throw std::logic_error{"Changing player ID with cheats disabled is prohibited"}; 98 : } 99 : 100 0 : void SetViewedPlayer(int id) 101 : { 102 0 : if (!g_Game || g_Game->GetViewedPlayerID() == id) 103 0 : return; 104 : 105 0 : int playerID = g_Game->GetPlayerID(); 106 : // Forbid active players to reveal the map by changing perspective, unless cheats are allowed. 107 0 : if (playerID == -1 || g_Game->CheatsEnabled() || g_Game->PlayerFinished(playerID) || g_Game->IsVisualReplay()) 108 0 : g_Game->SetViewedPlayerID(id); 109 : else 110 0 : throw std::logic_error{"Changing the perspective with cheats disabled is prohibited"}; 111 : } 112 : 113 0 : float GetSimRate() 114 : { 115 0 : return g_Game->GetSimRate(); 116 : } 117 : 118 0 : void SetSimRate(float rate) 119 : { 120 0 : g_Game->SetSimRate(rate); 121 0 : } 122 : 123 0 : int GetPendingTurns() 124 : { 125 0 : if (!g_Game || !g_Game->GetTurnManager()) 126 : return 0; 127 : 128 0 : return g_Game->GetTurnManager()->GetPendingTurns(); 129 : } 130 : 131 0 : bool IsPaused() 132 : { 133 0 : if (!g_Game) 134 0 : throw std::logic_error{"Game is not started"}; 135 : 136 0 : return g_Game->m_Paused; 137 : } 138 : 139 0 : void SetPaused(bool pause, bool sendMessage) 140 : { 141 0 : if (!g_Game) 142 0 : throw std::logic_error{"Game is not started"}; 143 : 144 0 : g_Game->m_Paused = pause; 145 : 146 : #if CONFIG2_AUDIO 147 0 : if (g_SoundManager) 148 : { 149 0 : g_SoundManager->PauseAmbient(pause); 150 0 : g_SoundManager->PauseAction(pause); 151 : } 152 : #endif 153 : 154 0 : if (g_NetClient && sendMessage) 155 0 : g_NetClient->SendPausedMessage(pause); 156 0 : } 157 : 158 0 : bool IsVisualReplay() 159 : { 160 0 : if (!g_Game) 161 : return false; 162 : 163 0 : return g_Game->IsVisualReplay(); 164 : } 165 : 166 0 : std::wstring GetCurrentReplayDirectory() 167 : { 168 0 : if (!g_Game) 169 0 : return std::wstring(); 170 : 171 0 : if (g_Game->IsVisualReplay()) 172 0 : return g_Game->GetReplayPath().Parent().Filename().string(); 173 : 174 0 : return g_Game->GetReplayLogger().GetDirectory().Filename().string(); 175 : } 176 : 177 0 : void EnableTimeWarpRecording(unsigned int numTurns) 178 : { 179 0 : g_Game->GetTurnManager()->EnableTimeWarpRecording(numTurns); 180 0 : } 181 : 182 0 : void RewindTimeWarp() 183 : { 184 0 : g_Game->GetTurnManager()->RewindTimeWarp(); 185 0 : } 186 : 187 0 : void DumpTerrainMipmap() 188 : { 189 0 : VfsPath filename(L"screenshots/terrainmipmap.png"); 190 0 : g_Game->GetWorld()->GetTerrain().GetHeightMipmap().DumpToDisk(filename); 191 0 : OsPath realPath; 192 0 : g_VFS->GetRealPath(filename, realPath); 193 0 : LOGMESSAGERENDER("Terrain mipmap written to '{}'", realPath.string8()); 194 0 : } 195 : 196 26 : void RegisterScriptFunctions(const Script::Request& rq) 197 : { 198 26 : Script::Function::Register<&StartGame>(rq, "StartGame"); 199 26 : Script::Function::Register<&Script_EndGame>(rq, "EndGame"); 200 26 : Script::Function::Register<&GetPlayerID>(rq, "GetPlayerID"); 201 26 : Script::Function::Register<&SetPlayerID>(rq, "SetPlayerID"); 202 26 : Script::Function::Register<&SetViewedPlayer>(rq, "SetViewedPlayer"); 203 26 : Script::Function::Register<&GetSimRate>(rq, "GetSimRate"); 204 26 : Script::Function::Register<&SetSimRate>(rq, "SetSimRate"); 205 26 : Script::Function::Register<&GetPendingTurns>(rq, "GetPendingTurns"); 206 26 : Script::Function::Register<&IsPaused>(rq, "IsPaused"); 207 26 : Script::Function::Register<&SetPaused>(rq, "SetPaused"); 208 26 : Script::Function::Register<&IsVisualReplay>(rq, "IsVisualReplay"); 209 26 : Script::Function::Register<&GetCurrentReplayDirectory>(rq, "GetCurrentReplayDirectory"); 210 26 : Script::Function::Register<&EnableTimeWarpRecording>(rq, "EnableTimeWarpRecording"); 211 26 : Script::Function::Register<&RewindTimeWarp>(rq, "RewindTimeWarp"); 212 26 : Script::Function::Register<&DumpTerrainMipmap>(rq, "DumpTerrainMipmap"); 213 26 : } 214 : }