Changeset View
Changeset View
Standalone View
Standalone View
source/ps/scripting/JSInterface_Game.cpp
Show All 21 Lines | |||||
#include "graphics/Terrain.h" | #include "graphics/Terrain.h" | ||||
#include "network/NetClient.h" | #include "network/NetClient.h" | ||||
#include "network/NetServer.h" | #include "network/NetServer.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/Game.h" | #include "ps/Game.h" | ||||
#include "ps/Replay.h" | #include "ps/Replay.h" | ||||
#include "ps/World.h" | #include "ps/World.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "scriptinterface/FunctionWrapper.h" | |||||
#include "simulation2/system/TurnManager.h" | #include "simulation2/system/TurnManager.h" | ||||
#include "simulation2/Simulation2.h" | #include "simulation2/Simulation2.h" | ||||
#include "soundmanager/SoundManager.h" | #include "soundmanager/SoundManager.h" | ||||
extern void EndGame(); | extern void EndGame(); | ||||
bool JSI_Game::IsGameStarted(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | bool JSI_Game::IsGameStarted() | ||||
{ | { | ||||
return g_Game; | return g_Game; | ||||
} | } | ||||
void JSI_Game::StartGame(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue attribs, int playerID) | void JSI_Game::StartGame(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue attribs, int playerID) | ||||
{ | { | ||||
ENSURE(!g_NetServer); | ENSURE(!g_NetServer); | ||||
ENSURE(!g_NetClient); | ENSURE(!g_NetClient); | ||||
ENSURE(!g_Game); | ENSURE(!g_Game); | ||||
g_Game = new CGame(true); | g_Game = new CGame(true); | ||||
// Convert from GUI script context to sim script context | // Convert from GUI script context to sim script context | ||||
CSimulation2* sim = g_Game->GetSimulation2(); | CSimulation2* sim = g_Game->GetSimulation2(); | ||||
JSContext* cxSim = sim->GetScriptInterface().GetContext(); | JSContext* cxSim = sim->GetScriptInterface().GetContext(); | ||||
JSAutoRequest rqSim(cxSim); | JSAutoRequest rqSim(cxSim); | ||||
JS::RootedValue gameAttribs(cxSim, | JS::RootedValue gameAttribs(cxSim, | ||||
sim->GetScriptInterface().CloneValueFromOtherContext(*(pCxPrivate->pScriptInterface), attribs)); | sim->GetScriptInterface().CloneValueFromOtherContext(*(pCxPrivate->pScriptInterface), attribs)); | ||||
g_Game->SetPlayerID(playerID); | g_Game->SetPlayerID(playerID); | ||||
g_Game->StartGame(&gameAttribs, ""); | g_Game->StartGame(&gameAttribs, ""); | ||||
} | } | ||||
void JSI_Game::Script_EndGame(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | void JSI_Game::Script_EndGame() | ||||
{ | { | ||||
EndGame(); | EndGame(); | ||||
} | } | ||||
int JSI_Game::GetPlayerID(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | int JSI_Game::GetPlayerID() | ||||
{ | { | ||||
if (!g_Game) | if (!g_Game) | ||||
return -1; | return -1; | ||||
return g_Game->GetPlayerID(); | return g_Game->GetPlayerID(); | ||||
} | } | ||||
void JSI_Game::SetPlayerID(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int id) | void JSI_Game::SetPlayerID(int id) | ||||
{ | { | ||||
if (!g_Game) | if (!g_Game) | ||||
return; | return; | ||||
g_Game->SetPlayerID(id); | g_Game->SetPlayerID(id); | ||||
} | } | ||||
void JSI_Game::SetViewedPlayer(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int id) | void JSI_Game::SetViewedPlayer(int id) | ||||
{ | { | ||||
if (!g_Game) | if (!g_Game) | ||||
return; | return; | ||||
g_Game->SetViewedPlayerID(id); | g_Game->SetViewedPlayerID(id); | ||||
} | } | ||||
float JSI_Game::GetSimRate(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | float JSI_Game::GetSimRate() | ||||
{ | { | ||||
return g_Game->GetSimRate(); | return g_Game->GetSimRate(); | ||||
} | } | ||||
void JSI_Game::SetSimRate(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), float rate) | void JSI_Game::SetSimRate(float rate) | ||||
{ | { | ||||
g_Game->SetSimRate(rate); | g_Game->SetSimRate(rate); | ||||
} | } | ||||
bool JSI_Game::IsPaused(ScriptInterface::CxPrivate* pCxPrivate) | bool JSI_Game::IsPaused(ScriptInterface::CxPrivate* pCxPrivate) | ||||
{ | { | ||||
if (!g_Game) | if (!g_Game) | ||||
{ | { | ||||
Show All 22 Lines | #if CONFIG2_AUDIO | ||||
if (g_SoundManager) | if (g_SoundManager) | ||||
g_SoundManager->Pause(pause); | g_SoundManager->Pause(pause); | ||||
#endif | #endif | ||||
if (g_NetClient && sendMessage) | if (g_NetClient && sendMessage) | ||||
g_NetClient->SendPausedMessage(pause); | g_NetClient->SendPausedMessage(pause); | ||||
} | } | ||||
bool JSI_Game::IsVisualReplay(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | bool JSI_Game::IsVisualReplay() | ||||
{ | { | ||||
if (!g_Game) | if (!g_Game) | ||||
return false; | return false; | ||||
return g_Game->IsVisualReplay(); | return g_Game->IsVisualReplay(); | ||||
} | } | ||||
std::wstring JSI_Game::GetCurrentReplayDirectory(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | std::wstring JSI_Game::GetCurrentReplayDirectory() | ||||
{ | { | ||||
if (!g_Game) | if (!g_Game) | ||||
return std::wstring(); | return std::wstring(); | ||||
if (g_Game->IsVisualReplay()) | if (g_Game->IsVisualReplay()) | ||||
return g_Game->GetReplayPath().Parent().Filename().string(); | return g_Game->GetReplayPath().Parent().Filename().string(); | ||||
return g_Game->GetReplayLogger().GetDirectory().Filename().string(); | return g_Game->GetReplayLogger().GetDirectory().Filename().string(); | ||||
} | } | ||||
void JSI_Game::EnableTimeWarpRecording(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), unsigned int numTurns) | void JSI_Game::EnableTimeWarpRecording(unsigned int numTurns) | ||||
{ | { | ||||
g_Game->GetTurnManager()->EnableTimeWarpRecording(numTurns); | g_Game->GetTurnManager()->EnableTimeWarpRecording(numTurns); | ||||
} | } | ||||
void JSI_Game::RewindTimeWarp(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | void JSI_Game::RewindTimeWarp() | ||||
{ | { | ||||
g_Game->GetTurnManager()->RewindTimeWarp(); | g_Game->GetTurnManager()->RewindTimeWarp(); | ||||
} | } | ||||
void JSI_Game::DumpTerrainMipmap(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | void JSI_Game::DumpTerrainMipmap() | ||||
{ | { | ||||
VfsPath filename(L"screenshots/terrainmipmap.png"); | VfsPath filename(L"screenshots/terrainmipmap.png"); | ||||
g_Game->GetWorld()->GetTerrain()->GetHeightMipmap().DumpToDisk(filename); | g_Game->GetWorld()->GetTerrain()->GetHeightMipmap().DumpToDisk(filename); | ||||
OsPath realPath; | OsPath realPath; | ||||
g_VFS->GetRealPath(filename, realPath); | g_VFS->GetRealPath(filename, realPath); | ||||
LOGMESSAGERENDER("Terrain mipmap written to '%s'", realPath.string8()); | LOGMESSAGERENDER("Terrain mipmap written to '%s'", realPath.string8()); | ||||
} | } | ||||
void JSI_Game::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | void JSI_Game::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
scriptInterface.RegisterFunction<bool, &IsGameStarted>("IsGameStarted"); | #define Register(name) ScriptWrapper::WrapAndRegister<decltype(&name), &name>(scriptInterface, #name) | ||||
scriptInterface.RegisterFunction<void, JS::HandleValue, int, &StartGame>("StartGame"); | Register(IsGameStarted); | ||||
scriptInterface.RegisterFunction<void, &Script_EndGame>("EndGame"); | Register(StartGame); | ||||
scriptInterface.RegisterFunction<int, &GetPlayerID>("GetPlayerID"); | Register(EndGame); | ||||
scriptInterface.RegisterFunction<void, int, &SetPlayerID>("SetPlayerID"); | Register(GetPlayerID); | ||||
scriptInterface.RegisterFunction<void, int, &SetViewedPlayer>("SetViewedPlayer"); | Register(SetPlayerID); | ||||
scriptInterface.RegisterFunction<float, &GetSimRate>("GetSimRate"); | Register(SetViewedPlayer); | ||||
scriptInterface.RegisterFunction<void, float, &SetSimRate>("SetSimRate"); | Register(GetSimRate); | ||||
scriptInterface.RegisterFunction<bool, &IsPaused>("IsPaused"); | Register(SetSimRate); | ||||
scriptInterface.RegisterFunction<void, bool, bool, &SetPaused>("SetPaused"); | Register(IsPaused); | ||||
scriptInterface.RegisterFunction<bool, &IsVisualReplay>("IsVisualReplay"); | Register(SetPaused); | ||||
scriptInterface.RegisterFunction<std::wstring, &GetCurrentReplayDirectory>("GetCurrentReplayDirectory"); | Register(IsVisualReplay); | ||||
scriptInterface.RegisterFunction<void, unsigned int, &EnableTimeWarpRecording>("EnableTimeWarpRecording"); | Register(GetCurrentReplayDirectory); | ||||
scriptInterface.RegisterFunction<void, &RewindTimeWarp>("RewindTimeWarp"); | Register(EnableTimeWarpRecording); | ||||
scriptInterface.RegisterFunction<void, &DumpTerrainMipmap>("DumpTerrainMipmap"); | Register(RewindTimeWarp); | ||||
Register(DumpTerrainMipmap); | |||||
#undef Register | |||||
} | } |
Wildfire Games · Phabricator