Changeset View
Changeset View
Standalone View
Standalone View
source/tools/atlas/GameInterface/Handlers/MapHandlers.cpp
Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | |||||
#include "simulation2/components/ICmpPosition.h" | #include "simulation2/components/ICmpPosition.h" | ||||
#include "simulation2/components/ICmpRangeManager.h" | #include "simulation2/components/ICmpRangeManager.h" | ||||
#include "simulation2/components/ICmpTerrain.h" | #include "simulation2/components/ICmpTerrain.h" | ||||
namespace | namespace | ||||
{ | { | ||||
void InitGame() | void InitGame() | ||||
{ | { | ||||
if (g_Game) | CGame::initGame(false, false); | ||||
{ | |||||
delete g_Game; | |||||
g_Game = NULL; | |||||
} | |||||
g_Game = new CGame(false, false); | |||||
// Default to player 1 for playtesting | // Default to player 1 for playtesting | ||||
g_Game->SetPlayerID(1); | CGame::getGame()->SetPlayerID(1); | ||||
} | } | ||||
void StartGame(JS::MutableHandleValue attrs) | void StartGame(JS::MutableHandleValue attrs) | ||||
{ | { | ||||
g_Game->StartGame(attrs, ""); | CGame::getGame()->StartGame(attrs, ""); | ||||
// TODO: Non progressive load can fail - need a decent way to handle this | // TODO: Non progressive load can fail - need a decent way to handle this | ||||
LDR_NonprogressiveLoad(); | LDR_NonprogressiveLoad(); | ||||
// Disable fog-of-war - this must be done before starting the game, | // Disable fog-of-war - this must be done before starting the game, | ||||
// as visual actors cache their visibility state on first render. | // as visual actors cache their visibility state on first render. | ||||
CmpPtr<ICmpRangeManager> cmpRangeManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY); | CmpPtr<ICmpRangeManager> cmpRangeManager(*CGame::getGame()->GetSimulation2(), SYSTEM_ENTITY); | ||||
if (cmpRangeManager) | if (cmpRangeManager) | ||||
cmpRangeManager->SetLosRevealAll(-1, true); | cmpRangeManager->SetLosRevealAll(-1, true); | ||||
PSRETURN ret = g_Game->ReallyStartGame(); | PSRETURN ret = CGame::getGame()->ReallyStartGame(); | ||||
ENSURE(ret == PSRETURN_OK); | ENSURE(ret == PSRETURN_OK); | ||||
} | } | ||||
} | } | ||||
namespace AtlasMessage { | namespace AtlasMessage { | ||||
QUERYHANDLER(GenerateMap) | QUERYHANDLER(GenerateMap) | ||||
{ | { | ||||
try | try | ||||
{ | { | ||||
InitGame(); | InitGame(); | ||||
// Random map | // Random map | ||||
const ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); | const ScriptInterface& scriptInterface = CGame::getGame()->GetSimulation2()->GetScriptInterface(); | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedValue settings(cx); | JS::RootedValue settings(cx); | ||||
scriptInterface.ParseJSON(*msg->settings, &settings); | scriptInterface.ParseJSON(*msg->settings, &settings); | ||||
scriptInterface.SetProperty(settings, "mapType", std::string("random")); | scriptInterface.SetProperty(settings, "mapType", std::string("random")); | ||||
JS::RootedValue attrs(cx); | JS::RootedValue attrs(cx); | ||||
Show All 10 Lines | QUERYHANDLER(GenerateMap) | ||||
{ | { | ||||
// Cancel loading | // Cancel loading | ||||
LDR_Cancel(); | LDR_Cancel(); | ||||
// Since map generation failed and we don't know why, use the blank map as a fallback | // Since map generation failed and we don't know why, use the blank map as a fallback | ||||
InitGame(); | InitGame(); | ||||
const ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); | const ScriptInterface& scriptInterface = CGame::getGame()->GetSimulation2()->GetScriptInterface(); | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedValue settings(cx); | JS::RootedValue settings(cx); | ||||
scriptInterface.Eval("({})", &settings); | scriptInterface.Eval("({})", &settings); | ||||
// Set up 8-element array of empty objects to satisfy init | // Set up 8-element array of empty objects to satisfy init | ||||
JS::RootedValue playerData(cx); | JS::RootedValue playerData(cx); | ||||
scriptInterface.Eval("([])", &playerData); | scriptInterface.Eval("([])", &playerData); | ||||
Show All 16 Lines | catch (PSERROR_Game_World_MapLoadFailed&) | ||||
msg->status = -1; | msg->status = -1; | ||||
} | } | ||||
} | } | ||||
MESSAGEHANDLER(LoadMap) | MESSAGEHANDLER(LoadMap) | ||||
{ | { | ||||
InitGame(); | InitGame(); | ||||
const ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); | const ScriptInterface& scriptInterface = CGame::getGame()->GetSimulation2()->GetScriptInterface(); | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
// Scenario | // Scenario | ||||
CStrW map = *msg->filename; | CStrW map = *msg->filename; | ||||
CStrW mapBase = map.BeforeLast(L".pmp"); // strip the file extension, if any | CStrW mapBase = map.BeforeLast(L".pmp"); // strip the file extension, if any | ||||
JS::RootedValue attrs(cx); | JS::RootedValue attrs(cx); | ||||
Show All 11 Lines | MESSAGEHANDLER(ImportHeightmap) | ||||
{ | { | ||||
LOGERROR("Failed to decode heightmap."); | LOGERROR("Failed to decode heightmap."); | ||||
return; | return; | ||||
} | } | ||||
// resize terrain to heightmap size | // resize terrain to heightmap size | ||||
// Notice that the number of tiles/pixels per side of the heightmap image is | // Notice that the number of tiles/pixels per side of the heightmap image is | ||||
// one less than the number of vertices per side of the heightmap. | // one less than the number of vertices per side of the heightmap. | ||||
CTerrain* terrain = g_Game->GetWorld()->GetTerrain(); | CTerrain* terrain = CGame::getGame()->GetWorld()->GetTerrain(); | ||||
terrain->Resize((sqrt(heightmap_source.size()) - 1) / PATCH_SIZE); | terrain->Resize((sqrt(heightmap_source.size()) - 1) / PATCH_SIZE); | ||||
// copy heightmap data into map | // copy heightmap data into map | ||||
u16* heightmap = g_Game->GetWorld()->GetTerrain()->GetHeightMap(); | u16* heightmap = CGame::getGame()->GetWorld()->GetTerrain()->GetHeightMap(); | ||||
ENSURE(heightmap_source.size() == (std::size_t) SQR(g_Game->GetWorld()->GetTerrain()->GetVerticesPerSide())); | ENSURE(heightmap_source.size() == (std::size_t) SQR(CGame::getGame()->GetWorld()->GetTerrain()->GetVerticesPerSide())); | ||||
std::copy(heightmap_source.begin(), heightmap_source.end(), heightmap); | std::copy(heightmap_source.begin(), heightmap_source.end(), heightmap); | ||||
// update simulation | // update simulation | ||||
CmpPtr<ICmpTerrain> cmpTerrain(*g_Game->GetSimulation2(), SYSTEM_ENTITY); | CmpPtr<ICmpTerrain> cmpTerrain(*CGame::getGame()->GetSimulation2(), SYSTEM_ENTITY); | ||||
if (cmpTerrain) | if (cmpTerrain) | ||||
cmpTerrain->ReloadTerrain(); | cmpTerrain->ReloadTerrain(); | ||||
g_Game->GetView()->GetLOSTexture().MakeDirty(); | CGame::getGame()->GetView()->GetLOSTexture().MakeDirty(); | ||||
} | } | ||||
MESSAGEHANDLER(SaveMap) | MESSAGEHANDLER(SaveMap) | ||||
{ | { | ||||
CMapWriter writer; | CMapWriter writer; | ||||
VfsPath pathname = VfsPath(*msg->filename).ChangeExtension(L".pmp"); | VfsPath pathname = VfsPath(*msg->filename).ChangeExtension(L".pmp"); | ||||
writer.SaveMap(pathname, | writer.SaveMap(pathname, | ||||
g_Game->GetWorld()->GetTerrain(), | CGame::getGame()->GetWorld()->GetTerrain(), | ||||
g_Renderer.GetWaterManager(), g_Renderer.GetSkyManager(), | g_Renderer.GetWaterManager(), g_Renderer.GetSkyManager(), | ||||
&g_LightEnv, g_Game->GetView()->GetCamera(), g_Game->GetView()->GetCinema(), | &g_LightEnv, CGame::getGame()->GetView()->GetCamera(), CGame::getGame()->GetView()->GetCinema(), | ||||
&g_Renderer.GetPostprocManager(), | &g_Renderer.GetPostprocManager(), | ||||
g_Game->GetSimulation2()); | CGame::getGame()->GetSimulation2()); | ||||
} | } | ||||
QUERYHANDLER(GetMapSettings) | QUERYHANDLER(GetMapSettings) | ||||
{ | { | ||||
msg->settings = g_Game->GetSimulation2()->GetMapSettingsString(); | msg->settings = CGame::getGame()->GetSimulation2()->GetMapSettingsString(); | ||||
} | } | ||||
BEGIN_COMMAND(SetMapSettings) | BEGIN_COMMAND(SetMapSettings) | ||||
{ | { | ||||
std::string m_OldSettings, m_NewSettings; | std::string m_OldSettings, m_NewSettings; | ||||
void SetSettings(const std::string& settings) | void SetSettings(const std::string& settings) | ||||
{ | { | ||||
g_Game->GetSimulation2()->SetMapSettings(settings); | CGame::getGame()->GetSimulation2()->SetMapSettings(settings); | ||||
} | } | ||||
void Do() | void Do() | ||||
{ | { | ||||
m_OldSettings = g_Game->GetSimulation2()->GetMapSettingsString(); | m_OldSettings = CGame::getGame()->GetSimulation2()->GetMapSettingsString(); | ||||
m_NewSettings = *msg->settings; | m_NewSettings = *msg->settings; | ||||
SetSettings(m_NewSettings); | SetSettings(m_NewSettings); | ||||
} | } | ||||
// TODO: we need some way to notify the Atlas UI when the settings are changed | // TODO: we need some way to notify the Atlas UI when the settings are changed | ||||
// externally, otherwise this will have no visible effect | // externally, otherwise this will have no visible effect | ||||
void Undo() | void Undo() | ||||
Show All 10 Lines | BEGIN_COMMAND(SetMapSettings) | ||||
{ | { | ||||
prev->m_NewSettings = m_NewSettings; | prev->m_NewSettings = m_NewSettings; | ||||
} | } | ||||
}; | }; | ||||
END_COMMAND(SetMapSettings) | END_COMMAND(SetMapSettings) | ||||
MESSAGEHANDLER(LoadPlayerSettings) | MESSAGEHANDLER(LoadPlayerSettings) | ||||
{ | { | ||||
g_Game->GetSimulation2()->LoadPlayerSettings(msg->newplayers); | CGame::getGame()->GetSimulation2()->LoadPlayerSettings(msg->newplayers); | ||||
} | } | ||||
QUERYHANDLER(GetMapSizes) | QUERYHANDLER(GetMapSizes) | ||||
{ | { | ||||
msg->sizes = g_Game->GetSimulation2()->GetMapSizes(); | msg->sizes = CGame::getGame()->GetSimulation2()->GetMapSizes(); | ||||
} | } | ||||
QUERYHANDLER(GetRMSData) | QUERYHANDLER(GetRMSData) | ||||
{ | { | ||||
msg->data = g_Game->GetSimulation2()->GetRMSData(); | msg->data = CGame::getGame()->GetSimulation2()->GetRMSData(); | ||||
} | } | ||||
BEGIN_COMMAND(ResizeMap) | BEGIN_COMMAND(ResizeMap) | ||||
{ | { | ||||
int m_OldTiles, m_NewTiles; | int m_OldTiles, m_NewTiles; | ||||
cResizeMap() | cResizeMap() | ||||
{ | { | ||||
} | } | ||||
void MakeDirty() | void MakeDirty() | ||||
{ | { | ||||
CmpPtr<ICmpTerrain> cmpTerrain(*g_Game->GetSimulation2(), SYSTEM_ENTITY); | CmpPtr<ICmpTerrain> cmpTerrain(*CGame::getGame()->GetSimulation2(), SYSTEM_ENTITY); | ||||
if (cmpTerrain) | if (cmpTerrain) | ||||
cmpTerrain->ReloadTerrain(); | cmpTerrain->ReloadTerrain(); | ||||
// The LOS texture won't normally get updated when running Atlas | // The LOS texture won't normally get updated when running Atlas | ||||
// (since there's no simulation updates), so explicitly dirty it | // (since there's no simulation updates), so explicitly dirty it | ||||
g_Game->GetView()->GetLOSTexture().MakeDirty(); | CGame::getGame()->GetView()->GetLOSTexture().MakeDirty(); | ||||
} | } | ||||
void ResizeTerrain(int tiles) | void ResizeTerrain(int tiles) | ||||
{ | { | ||||
CTerrain* terrain = g_Game->GetWorld()->GetTerrain(); | CTerrain* terrain = CGame::getGame()->GetWorld()->GetTerrain(); | ||||
terrain->Resize(tiles / PATCH_SIZE); | terrain->Resize(tiles / PATCH_SIZE); | ||||
MakeDirty(); | MakeDirty(); | ||||
} | } | ||||
void Do() | void Do() | ||||
{ | { | ||||
CmpPtr<ICmpTerrain> cmpTerrain(*g_Game->GetSimulation2(), SYSTEM_ENTITY); | CmpPtr<ICmpTerrain> cmpTerrain(*CGame::getGame()->GetSimulation2(), SYSTEM_ENTITY); | ||||
if (!cmpTerrain) | if (!cmpTerrain) | ||||
{ | { | ||||
m_OldTiles = m_NewTiles = 0; | m_OldTiles = m_NewTiles = 0; | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
m_OldTiles = (int)cmpTerrain->GetTilesPerSide(); | m_OldTiles = (int)cmpTerrain->GetTilesPerSide(); | ||||
m_NewTiles = msg->tiles; | m_NewTiles = msg->tiles; | ||||
▲ Show 20 Lines • Show All 53 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator