LCOV - code coverage report
Current view: top level - source/ps/scripting - JSInterface_Game.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 19 94 20.2 %
Date: 2023-01-19 00:18:29 Functions: 3 19 15.8 %

          Line data    Source code
       1             : /* Copyright (C) 2022 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/Terrain.h"
      23             : #include "network/NetClient.h"
      24             : #include "network/NetServer.h"
      25             : #include "ps/CLogger.h"
      26             : #include "ps/Filesystem.h"
      27             : #include "ps/Game.h"
      28             : #include "ps/GameSetup/GameSetup.h"
      29             : #include "ps/Replay.h"
      30             : #include "ps/World.h"
      31             : #include "scriptinterface/FunctionWrapper.h"
      32             : #include "scriptinterface/StructuredClone.h"
      33             : #include "simulation2/system/TurnManager.h"
      34             : #include "simulation2/Simulation2.h"
      35             : #include "soundmanager/SoundManager.h"
      36             : 
      37             : namespace JSI_Game
      38             : {
      39           0 : bool IsGameStarted()
      40             : {
      41           0 :     return g_Game;
      42             : }
      43             : 
      44           0 : void StartGame(const ScriptInterface& guiInterface, JS::HandleValue attribs, int playerID, bool storeReplay)
      45             : {
      46           0 :     ENSURE(!g_NetServer);
      47           0 :     ENSURE(!g_NetClient);
      48           0 :     ENSURE(!g_Game);
      49             : 
      50           0 :     g_Game = new CGame(storeReplay);
      51             : 
      52             :     // Convert from GUI script context to sim script context/
      53           0 :     CSimulation2* sim = g_Game->GetSimulation2();
      54           0 :     ScriptRequest rqSim(sim->GetScriptInterface());
      55             : 
      56           0 :     JS::RootedValue gameAttribs(rqSim.cx, Script::CloneValueFromOtherCompartment(sim->GetScriptInterface(), guiInterface, attribs));
      57             : 
      58           0 :     g_Game->SetPlayerID(playerID);
      59           0 :     g_Game->StartGame(&gameAttribs, "");
      60           0 : }
      61             : 
      62           0 : void Script_EndGame()
      63             : {
      64           0 :     EndGame();
      65           0 : }
      66             : 
      67           0 : int GetPlayerID()
      68             : {
      69           0 :     if (!g_Game)
      70           0 :         return -1;
      71             : 
      72           0 :     return g_Game->GetPlayerID();
      73             : }
      74             : 
      75           0 : void SetPlayerID(int id)
      76             : {
      77           0 :     if (!g_Game)
      78           0 :         return;
      79             : 
      80           0 :     g_Game->SetPlayerID(id);
      81             : }
      82             : 
      83           0 : void SetViewedPlayer(int id)
      84             : {
      85           0 :     if (!g_Game)
      86           0 :         return;
      87             : 
      88           0 :     g_Game->SetViewedPlayerID(id);
      89             : }
      90             : 
      91           0 : float GetSimRate()
      92             : {
      93           0 :     return g_Game->GetSimRate();
      94             : }
      95             : 
      96           0 : void SetSimRate(float rate)
      97             : {
      98           0 :     g_Game->SetSimRate(rate);
      99           0 : }
     100             : 
     101           0 : int GetPendingTurns()
     102             : {
     103           0 :     if (!g_Game || !g_Game->GetTurnManager())
     104           0 :         return 0;
     105             : 
     106           0 :     return g_Game->GetTurnManager()->GetPendingTurns();
     107             : }
     108             : 
     109           0 : bool IsPaused(const ScriptRequest& rq)
     110             : {
     111           0 :     if (!g_Game)
     112             :     {
     113           0 :         ScriptException::Raise(rq, "Game is not started");
     114           0 :         return false;
     115             :     }
     116             : 
     117           0 :     return g_Game->m_Paused;
     118             : }
     119             : 
     120           0 : void SetPaused(const ScriptRequest& rq, bool pause, bool sendMessage)
     121             : {
     122           0 :     if (!g_Game)
     123             :     {
     124           0 :         ScriptException::Raise(rq, "Game is not started");
     125           0 :         return;
     126             :     }
     127             : 
     128           0 :     g_Game->m_Paused = pause;
     129             : 
     130             : #if CONFIG2_AUDIO
     131           0 :     if (g_SoundManager)
     132             :     {
     133           0 :         g_SoundManager->PauseAmbient(pause);
     134           0 :         g_SoundManager->PauseAction(pause);
     135             :     }
     136             : #endif
     137             : 
     138           0 :     if (g_NetClient && sendMessage)
     139           0 :         g_NetClient->SendPausedMessage(pause);
     140             : }
     141             : 
     142           0 : bool IsVisualReplay()
     143             : {
     144           0 :     if (!g_Game)
     145           0 :         return false;
     146             : 
     147           0 :     return g_Game->IsVisualReplay();
     148             : }
     149             : 
     150           0 : std::wstring GetCurrentReplayDirectory()
     151             : {
     152           0 :     if (!g_Game)
     153           0 :         return std::wstring();
     154             : 
     155           0 :     if (g_Game->IsVisualReplay())
     156           0 :         return g_Game->GetReplayPath().Parent().Filename().string();
     157             : 
     158           0 :     return g_Game->GetReplayLogger().GetDirectory().Filename().string();
     159             : }
     160             : 
     161           0 : void EnableTimeWarpRecording(unsigned int numTurns)
     162             : {
     163           0 :     g_Game->GetTurnManager()->EnableTimeWarpRecording(numTurns);
     164           0 : }
     165             : 
     166           0 : void RewindTimeWarp()
     167             : {
     168           0 :     g_Game->GetTurnManager()->RewindTimeWarp();
     169           0 : }
     170             : 
     171           0 : void DumpTerrainMipmap()
     172             : {
     173           0 :     VfsPath filename(L"screenshots/terrainmipmap.png");
     174           0 :     g_Game->GetWorld()->GetTerrain()->GetHeightMipmap().DumpToDisk(filename);
     175           0 :     OsPath realPath;
     176           0 :     g_VFS->GetRealPath(filename, realPath);
     177           0 :     LOGMESSAGERENDER("Terrain mipmap written to '%s'", realPath.string8());
     178           0 : }
     179             : 
     180          12 : void RegisterScriptFunctions(const ScriptRequest& rq)
     181             : {
     182          12 :     ScriptFunction::Register<&IsGameStarted>(rq, "IsGameStarted");
     183          12 :     ScriptFunction::Register<&StartGame>(rq, "StartGame");
     184          12 :     ScriptFunction::Register<&Script_EndGame>(rq, "EndGame");
     185          12 :     ScriptFunction::Register<&GetPlayerID>(rq, "GetPlayerID");
     186          12 :     ScriptFunction::Register<&SetPlayerID>(rq, "SetPlayerID");
     187          12 :     ScriptFunction::Register<&SetViewedPlayer>(rq, "SetViewedPlayer");
     188          12 :     ScriptFunction::Register<&GetSimRate>(rq, "GetSimRate");
     189          12 :     ScriptFunction::Register<&SetSimRate>(rq, "SetSimRate");
     190          12 :     ScriptFunction::Register<&GetPendingTurns>(rq, "GetPendingTurns");
     191          12 :     ScriptFunction::Register<&IsPaused>(rq, "IsPaused");
     192          12 :     ScriptFunction::Register<&SetPaused>(rq, "SetPaused");
     193          12 :     ScriptFunction::Register<&IsVisualReplay>(rq, "IsVisualReplay");
     194          12 :     ScriptFunction::Register<&GetCurrentReplayDirectory>(rq, "GetCurrentReplayDirectory");
     195          12 :     ScriptFunction::Register<&EnableTimeWarpRecording>(rq, "EnableTimeWarpRecording");
     196          12 :     ScriptFunction::Register<&RewindTimeWarp>(rq, "RewindTimeWarp");
     197          12 :     ScriptFunction::Register<&DumpTerrainMipmap>(rq, "DumpTerrainMipmap");
     198          12 : }
     199           3 : }

Generated by: LCOV version 1.13