2020-11-13 05:18:22 -08:00
|
|
|
/* Copyright (C) 2020 Wildfire Games.
|
2017-09-11 18:38:46 -07:00
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#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"
|
2021-01-15 08:30:05 -08:00
|
|
|
#include "ps/GameSetup/GameSetup.h"
|
2018-04-27 09:48:44 -07:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2017-09-11 18:38:46 -07:00
|
|
|
#include "simulation2/system/TurnManager.h"
|
|
|
|
|
#include "simulation2/Simulation2.h"
|
|
|
|
|
#include "soundmanager/SoundManager.h"
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
bool JSI_Game::IsGameStarted(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))
|
2019-09-14 08:33:46 -07:00
|
|
|
{
|
|
|
|
|
return g_Game;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
void JSI_Game::StartGame(ScriptInterface::CmptPrivate* pCmptPrivate, JS::HandleValue attribs, int playerID)
|
2017-09-11 18:38:46 -07:00
|
|
|
{
|
|
|
|
|
ENSURE(!g_NetServer);
|
|
|
|
|
ENSURE(!g_NetClient);
|
|
|
|
|
ENSURE(!g_Game);
|
|
|
|
|
|
2019-08-25 04:02:55 -07:00
|
|
|
g_Game = new CGame(true);
|
2017-09-11 18:38:46 -07:00
|
|
|
|
|
|
|
|
// Convert from GUI script context to sim script context
|
|
|
|
|
CSimulation2* sim = g_Game->GetSimulation2();
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rqSim(sim->GetScriptInterface());
|
2017-09-11 18:38:46 -07:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue gameAttribs(rqSim.cx,
|
2020-12-06 06:03:02 -08:00
|
|
|
sim->GetScriptInterface().CloneValueFromOtherCompartment(*(pCmptPrivate->pScriptInterface), attribs));
|
2017-09-11 18:38:46 -07:00
|
|
|
|
|
|
|
|
g_Game->SetPlayerID(playerID);
|
|
|
|
|
g_Game->StartGame(&gameAttribs, "");
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
void JSI_Game::Script_EndGame(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))
|
2017-09-11 20:11:33 -07:00
|
|
|
{
|
|
|
|
|
EndGame();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
int JSI_Game::GetPlayerID(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))
|
2017-09-11 18:38:46 -07:00
|
|
|
{
|
|
|
|
|
if (!g_Game)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
return g_Game->GetPlayerID();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
void JSI_Game::SetPlayerID(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), int id)
|
2017-09-11 18:38:46 -07:00
|
|
|
{
|
|
|
|
|
if (!g_Game)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
g_Game->SetPlayerID(id);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
void JSI_Game::SetViewedPlayer(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), int id)
|
2017-09-11 18:38:46 -07:00
|
|
|
{
|
|
|
|
|
if (!g_Game)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
g_Game->SetViewedPlayerID(id);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
float JSI_Game::GetSimRate(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))
|
2017-09-11 18:38:46 -07:00
|
|
|
{
|
|
|
|
|
return g_Game->GetSimRate();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
void JSI_Game::SetSimRate(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), float rate)
|
2017-09-11 18:38:46 -07:00
|
|
|
{
|
|
|
|
|
g_Game->SetSimRate(rate);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
bool JSI_Game::IsPaused(ScriptInterface::CmptPrivate* pCmptPrivate)
|
2017-09-11 18:38:46 -07:00
|
|
|
{
|
|
|
|
|
if (!g_Game)
|
|
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(pCmptPrivate->pScriptInterface);
|
|
|
|
|
ScriptException::Raise(rq, "Game is not started");
|
2017-09-11 18:38:46 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return g_Game->m_Paused;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
void JSI_Game::SetPaused(ScriptInterface::CmptPrivate* pCmptPrivate, bool pause, bool sendMessage)
|
2017-09-11 18:38:46 -07:00
|
|
|
{
|
|
|
|
|
if (!g_Game)
|
|
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(pCmptPrivate->pScriptInterface);
|
|
|
|
|
ScriptException::Raise(rq, "Game is not started");
|
2017-09-11 18:38:46 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_Game->m_Paused = pause;
|
|
|
|
|
|
|
|
|
|
#if CONFIG2_AUDIO
|
|
|
|
|
if (g_SoundManager)
|
2021-01-21 01:34:20 -08:00
|
|
|
{
|
|
|
|
|
g_SoundManager->PauseAmbient(pause);
|
|
|
|
|
g_SoundManager->PauseAction(pause);
|
|
|
|
|
}
|
2017-09-11 18:38:46 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (g_NetClient && sendMessage)
|
|
|
|
|
g_NetClient->SendPausedMessage(pause);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
bool JSI_Game::IsVisualReplay(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))
|
2017-09-11 18:38:46 -07:00
|
|
|
{
|
|
|
|
|
if (!g_Game)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return g_Game->IsVisualReplay();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
std::wstring JSI_Game::GetCurrentReplayDirectory(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))
|
2017-09-11 18:38:46 -07:00
|
|
|
{
|
|
|
|
|
if (!g_Game)
|
|
|
|
|
return std::wstring();
|
|
|
|
|
|
|
|
|
|
if (g_Game->IsVisualReplay())
|
|
|
|
|
return g_Game->GetReplayPath().Parent().Filename().string();
|
|
|
|
|
|
|
|
|
|
return g_Game->GetReplayLogger().GetDirectory().Filename().string();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
void JSI_Game::EnableTimeWarpRecording(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate), unsigned int numTurns)
|
2017-09-11 18:38:46 -07:00
|
|
|
{
|
|
|
|
|
g_Game->GetTurnManager()->EnableTimeWarpRecording(numTurns);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
void JSI_Game::RewindTimeWarp(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))
|
2017-09-11 18:38:46 -07:00
|
|
|
{
|
|
|
|
|
g_Game->GetTurnManager()->RewindTimeWarp();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 08:44:15 -08:00
|
|
|
void JSI_Game::DumpTerrainMipmap(ScriptInterface::CmptPrivate* UNUSED(pCmptPrivate))
|
2017-09-11 18:38:46 -07:00
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
2019-09-14 08:33:46 -07:00
|
|
|
scriptInterface.RegisterFunction<bool, &IsGameStarted>("IsGameStarted");
|
2017-09-11 18:38:46 -07:00
|
|
|
scriptInterface.RegisterFunction<void, JS::HandleValue, int, &StartGame>("StartGame");
|
2017-09-11 20:11:33 -07:00
|
|
|
scriptInterface.RegisterFunction<void, &Script_EndGame>("EndGame");
|
2017-09-11 18:38:46 -07:00
|
|
|
scriptInterface.RegisterFunction<int, &GetPlayerID>("GetPlayerID");
|
|
|
|
|
scriptInterface.RegisterFunction<void, int, &SetPlayerID>("SetPlayerID");
|
|
|
|
|
scriptInterface.RegisterFunction<void, int, &SetViewedPlayer>("SetViewedPlayer");
|
|
|
|
|
scriptInterface.RegisterFunction<float, &GetSimRate>("GetSimRate");
|
|
|
|
|
scriptInterface.RegisterFunction<void, float, &SetSimRate>("SetSimRate");
|
|
|
|
|
scriptInterface.RegisterFunction<bool, &IsPaused>("IsPaused");
|
|
|
|
|
scriptInterface.RegisterFunction<void, bool, bool, &SetPaused>("SetPaused");
|
|
|
|
|
scriptInterface.RegisterFunction<bool, &IsVisualReplay>("IsVisualReplay");
|
|
|
|
|
scriptInterface.RegisterFunction<std::wstring, &GetCurrentReplayDirectory>("GetCurrentReplayDirectory");
|
|
|
|
|
scriptInterface.RegisterFunction<void, unsigned int, &EnableTimeWarpRecording>("EnableTimeWarpRecording");
|
|
|
|
|
scriptInterface.RegisterFunction<void, &RewindTimeWarp>("RewindTimeWarp");
|
|
|
|
|
scriptInterface.RegisterFunction<void, &DumpTerrainMipmap>("DumpTerrainMipmap");
|
|
|
|
|
}
|