Changeset View
Changeset View
Standalone View
Standalone View
source/ps/GameSetup/GameSetup.cpp
Show First 20 Lines • Show All 514 Lines • ▼ Show 20 Lines | |||||
void InitPsAutostart(bool networked, JS::HandleValue attrs) | void InitPsAutostart(bool networked, JS::HandleValue attrs) | ||||
{ | { | ||||
// The GUI has not been initialized yet, so use the simulation scriptinterface for this variable | // The GUI has not been initialized yet, so use the simulation scriptinterface for this variable | ||||
ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); | ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedValue playerAssignments(cx); | JS::RootedValue playerAssignments(cx); | ||||
scriptInterface.CreateObject(&playerAssignments); | ScriptInterface::CreateObject(cx, &playerAssignments); | ||||
if (!networked) | if (!networked) | ||||
{ | { | ||||
JS::RootedValue localPlayer(cx); | JS::RootedValue localPlayer(cx); | ||||
scriptInterface.CreateObject(&localPlayer, "player", g_Game->GetPlayerID()); | ScriptInterface::CreateObject(cx, &localPlayer, "player", g_Game->GetPlayerID()); | ||||
scriptInterface.SetProperty(playerAssignments, "local", localPlayer); | scriptInterface.SetProperty(playerAssignments, "local", localPlayer); | ||||
} | } | ||||
JS::RootedValue sessionInitData(cx); | JS::RootedValue sessionInitData(cx); | ||||
scriptInterface.CreateObject( | ScriptInterface::CreateObject( | ||||
cx, | |||||
&sessionInitData, | &sessionInitData, | ||||
"attribs", attrs, | "attribs", attrs, | ||||
"playerAssignments", playerAssignments); | "playerAssignments", playerAssignments); | ||||
InitPs(true, L"page_loading.xml", &scriptInterface, sessionInitData); | InitPs(true, L"page_loading.xml", &scriptInterface, sessionInitData); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 555 Lines • ▼ Show 20 Lines | if (!AutostartVisualReplay(args.Get("replay-visual")) && !Autostart(args)) | ||||
const bool setup_gui = ((flags & INIT_NO_GUI) == 0); | const bool setup_gui = ((flags & INIT_NO_GUI) == 0); | ||||
// We only want to display the splash screen at startup | // We only want to display the splash screen at startup | ||||
shared_ptr<ScriptInterface> scriptInterface = g_GUI->GetScriptInterface(); | shared_ptr<ScriptInterface> scriptInterface = g_GUI->GetScriptInterface(); | ||||
JSContext* cx = scriptInterface->GetContext(); | JSContext* cx = scriptInterface->GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedValue data(cx); | JS::RootedValue data(cx); | ||||
if (g_GUI) | if (g_GUI) | ||||
{ | { | ||||
scriptInterface->CreateObject(&data); | ScriptInterface::CreateObject(cx, &data, "isStartup", true); | ||||
scriptInterface->SetProperty(data, "isStartup", true); | |||||
if (!installedMods.empty()) | if (!installedMods.empty()) | ||||
scriptInterface->SetProperty(data, "installedMods", installedMods); | scriptInterface->SetProperty(data, "installedMods", installedMods); | ||||
} | } | ||||
InitPs(setup_gui, installedMods.empty() ? L"page_pregame.xml" : L"page_modmod.xml", g_GUI->GetScriptInterface().get(), data); | InitPs(setup_gui, installedMods.empty() ? L"page_pregame.xml" : L"page_modmod.xml", g_GUI->GetScriptInterface().get(), data); | ||||
} | } | ||||
} | } | ||||
catch (PSERROR_Game_World_MapLoadFailed& e) | catch (PSERROR_Game_World_MapLoadFailed& e) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 145 Lines • ▼ Show 20 Lines | bool Autostart(const CmdLineArgs& args) | ||||
ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); | ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedValue attrs(cx); | JS::RootedValue attrs(cx); | ||||
JS::RootedValue settings(cx); | JS::RootedValue settings(cx); | ||||
JS::RootedValue playerData(cx); | JS::RootedValue playerData(cx); | ||||
scriptInterface.CreateObject(&attrs); | ScriptInterface::CreateObject(cx, &attrs); | ||||
scriptInterface.CreateObject(&settings); | ScriptInterface::CreateObject(cx, &settings); | ||||
scriptInterface.CreateArray(&playerData); | ScriptInterface::CreateArray(cx, &playerData); | ||||
// The directory in front of the actual map name indicates which type | // The directory in front of the actual map name indicates which type | ||||
// of map is being loaded. Drawback of this approach is the association | // of map is being loaded. Drawback of this approach is the association | ||||
// of map types and folders is hard-coded, but benefits are: | // of map types and folders is hard-coded, but benefits are: | ||||
// - No need to pass the map type via command line separately | // - No need to pass the map type via command line separately | ||||
// - Prevents mixing up of scenarios and skirmish maps to some degree | // - Prevents mixing up of scenarios and skirmish maps to some degree | ||||
Path mapPath = Path(autoStartName); | Path mapPath = Path(autoStartName); | ||||
std::wstring mapDirectory = mapPath.Parent().Filename().string(); | std::wstring mapDirectory = mapPath.Parent().Filename().string(); | ||||
Show All 39 Lines | if (mapDirectory == L"random") | ||||
// Set up player data | // Set up player data | ||||
for (size_t i = 0; i < numPlayers; ++i) | for (size_t i = 0; i < numPlayers; ++i) | ||||
{ | { | ||||
JS::RootedValue player(cx); | JS::RootedValue player(cx); | ||||
// We could load player_defaults.json here, but that would complicate the logic | // We could load player_defaults.json here, but that would complicate the logic | ||||
// even more and autostart is only intended for developers anyway | // even more and autostart is only intended for developers anyway | ||||
scriptInterface.CreateObject(&player, "Civ", std::string("athen")); | ScriptInterface::CreateObject(cx, &player, "Civ", "athen"); | ||||
scriptInterface.SetPropertyInt(playerData, i, player); | scriptInterface.SetPropertyInt(playerData, i, player); | ||||
} | } | ||||
mapType = "random"; | mapType = "random"; | ||||
} | } | ||||
else if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") | else if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") | ||||
{ | { | ||||
// Initialize general settings from the map data so some values | // Initialize general settings from the map data so some values | ||||
Show All 13 Lines | bool Autostart(const CmdLineArgs& args) | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
LOGERROR("Autostart: Unrecognized map type '%s'", utf8_from_wstring(mapDirectory)); | LOGERROR("Autostart: Unrecognized map type '%s'", utf8_from_wstring(mapDirectory)); | ||||
throw PSERROR_Game_World_MapLoadFailed("Unrecognized map type.\nConsult readme.txt for the currently supported types."); | throw PSERROR_Game_World_MapLoadFailed("Unrecognized map type.\nConsult readme.txt for the currently supported types."); | ||||
} | } | ||||
scriptInterface.SetProperty(attrs, "mapType", mapType); | scriptInterface.SetProperty(attrs, "mapType", mapType); | ||||
scriptInterface.SetProperty(attrs, "map", std::string("maps/" + autoStartName)); | scriptInterface.SetProperty(attrs, "map", "maps/" + autoStartName); | ||||
scriptInterface.SetProperty(settings, "mapType", mapType); | scriptInterface.SetProperty(settings, "mapType", mapType); | ||||
scriptInterface.SetProperty(settings, "CheatsEnabled", true); | scriptInterface.SetProperty(settings, "CheatsEnabled", true); | ||||
// The seed is used for both random map generation and simulation | // The seed is used for both random map generation and simulation | ||||
u32 seed = 0; | u32 seed = 0; | ||||
if (args.Has("autostart-seed")) | if (args.Has("autostart-seed")) | ||||
{ | { | ||||
CStr seedArg = args.Get("autostart-seed"); | CStr seedArg = args.Get("autostart-seed"); | ||||
Show All 37 Lines | for (size_t i = 0; i < civArgs.size(); ++i) | ||||
if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | ||||
{ | { | ||||
if (mapDirectory == L"skirmishes") | if (mapDirectory == L"skirmishes") | ||||
{ | { | ||||
// playerID is certainly bigger than this map player number | // playerID is certainly bigger than this map player number | ||||
LOGWARNING("Autostart: Invalid player %d in autostart-team option", playerID); | LOGWARNING("Autostart: Invalid player %d in autostart-team option", playerID); | ||||
continue; | continue; | ||||
} | } | ||||
scriptInterface.CreateObject(&player); | ScriptInterface::CreateObject(cx, &player); | ||||
} | } | ||||
int teamID = civArgs[i].AfterFirst(":").ToInt() - 1; | int teamID = civArgs[i].AfterFirst(":").ToInt() - 1; | ||||
scriptInterface.SetProperty(player, "Team", teamID); | scriptInterface.SetProperty(player, "Team", teamID); | ||||
scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | ||||
} | } | ||||
} | } | ||||
Show All 14 Lines | for (size_t i = 0; i < aiArgs.size(); ++i) | ||||
if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | ||||
{ | { | ||||
if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") | if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") | ||||
{ | { | ||||
// playerID is certainly bigger than this map player number | // playerID is certainly bigger than this map player number | ||||
LOGWARNING("Autostart: Invalid player %d in autostart-ai option", playerID); | LOGWARNING("Autostart: Invalid player %d in autostart-ai option", playerID); | ||||
continue; | continue; | ||||
} | } | ||||
scriptInterface.CreateObject(&player); | ScriptInterface::CreateObject(cx, &player); | ||||
} | } | ||||
CStr name = aiArgs[i].AfterFirst(":"); | scriptInterface.SetProperty(player, "AI", aiArgs[i].AfterFirst(":")); | ||||
scriptInterface.SetProperty(player, "AI", std::string(name)); | |||||
scriptInterface.SetProperty(player, "AIDiff", 3); | scriptInterface.SetProperty(player, "AIDiff", 3); | ||||
scriptInterface.SetProperty(player, "AIBehavior", std::string("balanced")); | scriptInterface.SetProperty(player, "AIBehavior", "balanced"); | ||||
scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | ||||
} | } | ||||
} | } | ||||
// Set AI difficulty | // Set AI difficulty | ||||
if (args.Has("autostart-aidiff")) | if (args.Has("autostart-aidiff")) | ||||
{ | { | ||||
std::vector<CStr> civArgs = args.GetMultiple("autostart-aidiff"); | std::vector<CStr> civArgs = args.GetMultiple("autostart-aidiff"); | ||||
for (size_t i = 0; i < civArgs.size(); ++i) | for (size_t i = 0; i < civArgs.size(); ++i) | ||||
{ | { | ||||
int playerID = civArgs[i].BeforeFirst(":").ToInt(); | int playerID = civArgs[i].BeforeFirst(":").ToInt(); | ||||
// Instead of overwriting existing player data, modify the array | // Instead of overwriting existing player data, modify the array | ||||
JS::RootedValue player(cx); | JS::RootedValue player(cx); | ||||
if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | ||||
{ | { | ||||
if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") | if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") | ||||
{ | { | ||||
// playerID is certainly bigger than this map player number | // playerID is certainly bigger than this map player number | ||||
LOGWARNING("Autostart: Invalid player %d in autostart-aidiff option", playerID); | LOGWARNING("Autostart: Invalid player %d in autostart-aidiff option", playerID); | ||||
continue; | continue; | ||||
} | } | ||||
scriptInterface.CreateObject(&player); | ScriptInterface::CreateObject(cx, &player); | ||||
} | } | ||||
int difficulty = civArgs[i].AfterFirst(":").ToInt(); | scriptInterface.SetProperty(player, "AIDiff", civArgs[i].AfterFirst(":").ToInt()); | ||||
scriptInterface.SetProperty(player, "AIDiff", difficulty); | |||||
scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | ||||
} | } | ||||
} | } | ||||
// Set player data for Civs | // Set player data for Civs | ||||
if (args.Has("autostart-civ")) | if (args.Has("autostart-civ")) | ||||
{ | { | ||||
if (mapDirectory != L"scenarios") | if (mapDirectory != L"scenarios") | ||||
{ | { | ||||
std::vector<CStr> civArgs = args.GetMultiple("autostart-civ"); | std::vector<CStr> civArgs = args.GetMultiple("autostart-civ"); | ||||
for (size_t i = 0; i < civArgs.size(); ++i) | for (size_t i = 0; i < civArgs.size(); ++i) | ||||
{ | { | ||||
int playerID = civArgs[i].BeforeFirst(":").ToInt(); | int playerID = civArgs[i].BeforeFirst(":").ToInt(); | ||||
// Instead of overwriting existing player data, modify the array | // Instead of overwriting existing player data, modify the array | ||||
JS::RootedValue player(cx); | JS::RootedValue player(cx); | ||||
if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | if (!scriptInterface.GetPropertyInt(playerData, playerID-offset, &player) || player.isUndefined()) | ||||
{ | { | ||||
if (mapDirectory == L"skirmishes") | if (mapDirectory == L"skirmishes") | ||||
{ | { | ||||
// playerID is certainly bigger than this map player number | // playerID is certainly bigger than this map player number | ||||
LOGWARNING("Autostart: Invalid player %d in autostart-civ option", playerID); | LOGWARNING("Autostart: Invalid player %d in autostart-civ option", playerID); | ||||
continue; | continue; | ||||
} | } | ||||
scriptInterface.CreateObject(&player); | ScriptInterface::CreateObject(cx, &player); | ||||
} | } | ||||
CStr name = civArgs[i].AfterFirst(":"); | scriptInterface.SetProperty(player, "Civ", civArgs[i].AfterFirst(":")); | ||||
scriptInterface.SetProperty(player, "Civ", std::string(name)); | |||||
scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | scriptInterface.SetPropertyInt(playerData, playerID-offset, player); | ||||
} | } | ||||
} | } | ||||
else | else | ||||
LOGWARNING("Autostart: Option 'autostart-civ' is invalid for scenarios"); | LOGWARNING("Autostart: Option 'autostart-civ' is invalid for scenarios"); | ||||
} | } | ||||
// Add player data to map settings | // Add player data to map settings | ||||
▲ Show 20 Lines • Show All 173 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator