diff --git a/source/gui/scripting/ScriptFunctions.cpp b/source/gui/scripting/ScriptFunctions.cpp index 18f046f32c..21a9d064e4 100644 --- a/source/gui/scripting/ScriptFunctions.cpp +++ b/source/gui/scripting/ScriptFunctions.cpp @@ -47,11 +47,10 @@ #include "ps/ProfileViewer.h" #include "ps/Profile.h" #include "ps/Pyrogenesis.h" -#include "ps/Replay.h" #include "ps/UserReport.h" -#include "ps/World.h" #include "ps/scripting/JSInterface_ConfigDB.h" #include "ps/scripting/JSInterface_Console.h" +#include "ps/scripting/JSInterface_Game.h" #include "ps/scripting/JSInterface_Mod.h" #include "ps/scripting/JSInterface_SavedGame.h" #include "ps/scripting/JSInterface_VFS.h" @@ -65,8 +64,6 @@ #include "simulation2/components/ICmpSelectable.h" #include "simulation2/components/ICmpTemplateManager.h" #include "simulation2/helpers/Selection.h" -#include "simulation2/system/TurnManager.h" -#include "soundmanager/SoundManager.h" #include "soundmanager/scripting/JSInterface_Sound.h" #include "tools/atlas/GameInterface/GameLoop.h" @@ -146,64 +143,6 @@ std::vector PickSimilarPlayerEntities(ScriptInterface::CxPrivate* U return EntitySelection::PickSimilarEntities(*g_Game->GetSimulation2(), *g_Game->GetView()->GetCamera(), templateName, g_Game->GetViewedPlayerID(), includeOffScreen, matchRank, false, allowFoundations); } -bool IsVisualReplay(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) -{ - if (!g_Game) - return false; - - return g_Game->IsVisualReplay(); -} - -std::wstring GetCurrentReplayDirectory(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) -{ - if (!g_Game) - return std::wstring(); - - if (g_Game->IsVisualReplay()) - return g_Game->GetReplayPath().Parent().Filename().string(); - - return g_Game->GetReplayLogger().GetDirectory().Filename().string(); -} - -int GetPlayerID(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) -{ - if (g_Game) - return g_Game->GetPlayerID(); - return -1; -} - -void SetPlayerID(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int id) -{ - if (g_Game) - g_Game->SetPlayerID(id); -} - -void SetViewedPlayer(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int id) -{ - if (g_Game) - g_Game->SetViewedPlayerID(id); -} - -void StartGame(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue attribs, int playerID) -{ - ENSURE(!g_NetServer); - ENSURE(!g_NetClient); - - ENSURE(!g_Game); - g_Game = new CGame(); - - // Convert from GUI script context to sim script context - CSimulation2* sim = g_Game->GetSimulation2(); - JSContext* cxSim = sim->GetScriptInterface().GetContext(); - JSAutoRequest rqSim(cxSim); - - JS::RootedValue gameAttribs(cxSim, - sim->GetScriptInterface().CloneValueFromOtherContext(*(pCxPrivate->pScriptInterface), attribs)); - - g_Game->SetPlayerID(playerID); - g_Game->StartGame(&gameAttribs, ""); -} - JS::Value GetAIs(ScriptInterface::CxPrivate* pCxPrivate) { return ICmpAIManager::GetAIs(*(pCxPrivate->pScriptInterface)); @@ -231,7 +170,7 @@ bool AtlasIsAvailable(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) bool IsAtlasRunning(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) { - return (g_AtlasGameLoop && g_AtlasGameLoop->running); + return g_AtlasGameLoop && g_AtlasGameLoop->running; } JS::Value LoadMapSettings(ScriptInterface::CxPrivate* pCxPrivate, const VfsPath& pathname) @@ -299,16 +238,6 @@ void SubmitUserReport(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std: g_UserReporter.SubmitReport(type.c_str(), version, utf8_from_wstring(data)); } -void SetSimRate(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), float rate) -{ - g_Game->SetSimRate(rate); -} - -float GetSimRate(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) -{ - return g_Game->GetSimRate(); -} - // Deliberately cause the game to crash. // Currently implemented via access violation (read of address 0). // Useful for testing the crashlog/stack trace code. @@ -339,26 +268,6 @@ void DumpSimState(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) std::ofstream file (OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc); g_Game->GetSimulation2()->DumpDebugState(file); } - -void DumpTerrainMipmap(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) -{ - VfsPath filename(L"screenshots/terrainmipmap.png"); - g_Game->GetWorld()->GetTerrain()->GetHeightMipmap().DumpToDisk(filename); - OsPath realPath; - g_VFS->GetRealPath(filename, realPath); - LOGMESSAGERENDER("Terrain mipmap written to '%s'", realPath.string8()); -} - -void EnableTimeWarpRecording(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), unsigned int numTurns) -{ - g_Game->GetTurnManager()->EnableTimeWarpRecording(numTurns); -} - -void RewindTimeWarp(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) -{ - g_Game->GetTurnManager()->RewindTimeWarp(); -} - void SetBoundingBoxDebugOverlay(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), bool enabled) { ICmpSelectable::ms_EnableDebugOverlays = enabled; @@ -385,40 +294,6 @@ void ExitProgram(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) kill_mainloop(); } -// Is the game paused? -bool IsPaused(ScriptInterface::CxPrivate* pCxPrivate) -{ - if (!g_Game) - { - JS_ReportError(pCxPrivate->pScriptInterface->GetContext(), "Game is not started"); - return false; - } - - return g_Game->m_Paused; -} - -// Pause/unpause the game -void SetPaused(ScriptInterface::CxPrivate* pCxPrivate, bool pause, bool sendMessage) -{ - if (!g_Game) - { - JS_ReportError(pCxPrivate->pScriptInterface->GetContext(), "Game is not started"); - return; - } - g_Game->m_Paused = pause; -#if CONFIG2_AUDIO - if (g_SoundManager) - g_SoundManager->Pause(pause); -#endif - - if (g_NetClient && sendMessage) - g_NetClient->SendPausedMessage(pause); -} - -// Return the global frames-per-second value. -// params: -// returns: FPS [int] -// notes: // - This value is recalculated once a frame. We take special care to // filter it, so it is both accurate and free of jitter. int GetFps(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) @@ -522,6 +397,7 @@ void GuiScriptingInit(ScriptInterface& scriptInterface) JSI_Renderer::RegisterScriptFunctions(scriptInterface); JSI_Console::RegisterScriptFunctions(scriptInterface); JSI_ConfigDB::RegisterScriptFunctions(scriptInterface); + JSI_Game::RegisterScriptFunctions(scriptInterface); JSI_GUIManager::RegisterScriptFunctions(scriptInterface); JSI_Mod::RegisterScriptFunctions(scriptInterface); JSI_Network::RegisterScriptFunctions(scriptInterface); @@ -543,17 +419,10 @@ void GuiScriptingInit(ScriptInterface& scriptInterface) scriptInterface.RegisterFunction, &PickNonGaiaEntitiesOnScreen>("PickNonGaiaEntitiesOnScreen"); scriptInterface.RegisterFunction, std::string, bool, bool, bool, &PickSimilarPlayerEntities>("PickSimilarPlayerEntities"); - scriptInterface.RegisterFunction("StartGame"); scriptInterface.RegisterFunction("EndGame"); scriptInterface.RegisterFunction("GetAIs"); // Misc functions - scriptInterface.RegisterFunction("IsVisualReplay"); - scriptInterface.RegisterFunction("GetCurrentReplayDirectory"); - scriptInterface.RegisterFunction("GetPlayerID"); - scriptInterface.RegisterFunction("SetPlayerID"); - scriptInterface.RegisterFunction("SetViewedPlayer"); - scriptInterface.RegisterFunction("OpenURL"); scriptInterface.RegisterFunction("GetMatchID"); scriptInterface.RegisterFunction("RestartInAtlas"); @@ -565,8 +434,6 @@ void GuiScriptingInit(ScriptInterface& scriptInterface) scriptInterface.RegisterFunction("DisplayErrorDialog"); scriptInterface.RegisterFunction("GetProfilerState"); scriptInterface.RegisterFunction("Exit"); - scriptInterface.RegisterFunction("IsPaused"); - scriptInterface.RegisterFunction("SetPaused"); scriptInterface.RegisterFunction("GetFPS"); scriptInterface.RegisterFunction("GetBuildTimestamp"); scriptInterface.RegisterFunction("GetTextWidth"); @@ -579,15 +446,10 @@ void GuiScriptingInit(ScriptInterface& scriptInterface) // Development/debugging functions scriptInterface.RegisterFunction("GetMicroseconds"); - scriptInterface.RegisterFunction("SetSimRate"); - scriptInterface.RegisterFunction("GetSimRate"); scriptInterface.RegisterFunction("Crash"); scriptInterface.RegisterFunction("DebugWarn"); scriptInterface.RegisterFunction("ForceGC"); scriptInterface.RegisterFunction("DumpSimState"); - scriptInterface.RegisterFunction("DumpTerrainMipmap"); - scriptInterface.RegisterFunction("EnableTimeWarpRecording"); - scriptInterface.RegisterFunction("RewindTimeWarp"); scriptInterface.RegisterFunction("SetBoundingBoxDebugOverlay"); scriptInterface.RegisterFunction("GetSystemUsername"); } diff --git a/source/ps/scripting/JSInterface_Game.cpp b/source/ps/scripting/JSInterface_Game.cpp new file mode 100644 index 0000000000..4ae8d399d6 --- /dev/null +++ b/source/ps/scripting/JSInterface_Game.cpp @@ -0,0 +1,170 @@ +/* Copyright (C) 2017 Wildfire Games. + * This file is part of 0 A.D. + * + * 0 A.D. is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 0 A.D. is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with 0 A.D. If not, see . + */ + +#include "precompiled.h" + +#include "JSInterface_Game.h" + +#include "graphics/Terrain.h" +#include "network/NetClient.h" +#include "network/NetServer.h" +#include "ps/CLogger.h" +#include "ps/Game.h" +#include "ps/Replay.h" +#include "ps/World.h" +#include "simulation2/system/TurnManager.h" +#include "simulation2/Simulation2.h" +#include "soundmanager/SoundManager.h" + +void JSI_Game::StartGame(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue attribs, int playerID) +{ + ENSURE(!g_NetServer); + ENSURE(!g_NetClient); + ENSURE(!g_Game); + + g_Game = new CGame(); + + // Convert from GUI script context to sim script context + CSimulation2* sim = g_Game->GetSimulation2(); + JSContext* cxSim = sim->GetScriptInterface().GetContext(); + JSAutoRequest rqSim(cxSim); + + JS::RootedValue gameAttribs(cxSim, + sim->GetScriptInterface().CloneValueFromOtherContext(*(pCxPrivate->pScriptInterface), attribs)); + + g_Game->SetPlayerID(playerID); + g_Game->StartGame(&gameAttribs, ""); +} + +int JSI_Game::GetPlayerID(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) +{ + if (!g_Game) + return -1; + + return g_Game->GetPlayerID(); +} + +void JSI_Game::SetPlayerID(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int id) +{ + if (!g_Game) + return; + + g_Game->SetPlayerID(id); +} + +void JSI_Game::SetViewedPlayer(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int id) +{ + if (!g_Game) + return; + + g_Game->SetViewedPlayerID(id); +} + +float JSI_Game::GetSimRate(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) +{ + return g_Game->GetSimRate(); +} + +void JSI_Game::SetSimRate(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), float rate) +{ + g_Game->SetSimRate(rate); +} + +bool JSI_Game::IsPaused(ScriptInterface::CxPrivate* pCxPrivate) +{ + if (!g_Game) + { + JS_ReportError(pCxPrivate->pScriptInterface->GetContext(), "Game is not started"); + return false; + } + + return g_Game->m_Paused; +} + +void JSI_Game::SetPaused(ScriptInterface::CxPrivate* pCxPrivate, bool pause, bool sendMessage) +{ + if (!g_Game) + { + JS_ReportError(pCxPrivate->pScriptInterface->GetContext(), "Game is not started"); + return; + } + + g_Game->m_Paused = pause; + +#if CONFIG2_AUDIO + if (g_SoundManager) + g_SoundManager->Pause(pause); +#endif + + if (g_NetClient && sendMessage) + g_NetClient->SendPausedMessage(pause); +} + +bool JSI_Game::IsVisualReplay(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) +{ + if (!g_Game) + return false; + + return g_Game->IsVisualReplay(); +} + +std::wstring JSI_Game::GetCurrentReplayDirectory(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) +{ + if (!g_Game) + return std::wstring(); + + if (g_Game->IsVisualReplay()) + return g_Game->GetReplayPath().Parent().Filename().string(); + + return g_Game->GetReplayLogger().GetDirectory().Filename().string(); +} + +void JSI_Game::EnableTimeWarpRecording(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), unsigned int numTurns) +{ + g_Game->GetTurnManager()->EnableTimeWarpRecording(numTurns); +} + +void JSI_Game::RewindTimeWarp(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) +{ + g_Game->GetTurnManager()->RewindTimeWarp(); +} + +void JSI_Game::DumpTerrainMipmap(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) +{ + VfsPath filename(L"screenshots/terrainmipmap.png"); + g_Game->GetWorld()->GetTerrain()->GetHeightMipmap().DumpToDisk(filename); + OsPath realPath; + g_VFS->GetRealPath(filename, realPath); + LOGMESSAGERENDER("Terrain mipmap written to '%s'", realPath.string8()); +} + +void JSI_Game::RegisterScriptFunctions(const ScriptInterface& scriptInterface) +{ + scriptInterface.RegisterFunction("StartGame"); + scriptInterface.RegisterFunction("GetPlayerID"); + scriptInterface.RegisterFunction("SetPlayerID"); + scriptInterface.RegisterFunction("SetViewedPlayer"); + scriptInterface.RegisterFunction("GetSimRate"); + scriptInterface.RegisterFunction("SetSimRate"); + scriptInterface.RegisterFunction("IsPaused"); + scriptInterface.RegisterFunction("SetPaused"); + scriptInterface.RegisterFunction("IsVisualReplay"); + scriptInterface.RegisterFunction("GetCurrentReplayDirectory"); + scriptInterface.RegisterFunction("EnableTimeWarpRecording"); + scriptInterface.RegisterFunction("RewindTimeWarp"); + scriptInterface.RegisterFunction("DumpTerrainMipmap"); +} diff --git a/source/ps/scripting/JSInterface_Game.h b/source/ps/scripting/JSInterface_Game.h new file mode 100644 index 0000000000..cdba96268d --- /dev/null +++ b/source/ps/scripting/JSInterface_Game.h @@ -0,0 +1,42 @@ +/* Copyright (C) 2017 Wildfire Games. + * This file is part of 0 A.D. + * + * 0 A.D. is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 0 A.D. is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with 0 A.D. If not, see . + */ + +#ifndef INCLUDED_JSI_GAME +#define INCLUDED_JSI_GAME + +#include "scriptinterface/ScriptInterface.h" + +namespace JSI_Game +{ + void StartGame(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue attribs, int playerID); + int GetPlayerID(ScriptInterface::CxPrivate* pCxPrivate); + void SetPlayerID(ScriptInterface::CxPrivate* pCxPrivate, int id); + void SetViewedPlayer(ScriptInterface::CxPrivate* pCxPrivate, int id); + float GetSimRate(ScriptInterface::CxPrivate* pCxPrivate); + void SetSimRate(ScriptInterface::CxPrivate* pCxPrivate, float rate); + bool IsPaused(ScriptInterface::CxPrivate* pCxPrivate); + void SetPaused(ScriptInterface::CxPrivate* pCxPrivate, bool pause, bool sendMessage); + bool IsVisualReplay(ScriptInterface::CxPrivate* pCxPrivate); + std::wstring GetCurrentReplayDirectory(ScriptInterface::CxPrivate* pCxPrivate); + void RewindTimeWarp(ScriptInterface::CxPrivate* pCxPrivate); + void EnableTimeWarpRecording(ScriptInterface::CxPrivate* pCxPrivate, unsigned int numTurns); + void DumpTerrainMipmap(ScriptInterface::CxPrivate* pCxPrivate); + + void RegisterScriptFunctions(const ScriptInterface& ScriptInterface); +} + +#endif