Index: source/ps/GameSetup/GameSetup.cpp =================================================================== --- source/ps/GameSetup/GameSetup.cpp +++ source/ps/GameSetup/GameSetup.cpp @@ -1178,6 +1178,67 @@ return mapElement.GetText(); } +std::string LoadRandomMap(const CmdLineArgs& args, + CStr& autoStartName, + ScriptInterface& + scriptInterface, + JSContext* cx, + JS::RootedValue& settings, + JS::RootedValue& attrs, + JS::RootedValue& playerData, + std::string& mapType) +{ + // Random map definition will be loaded from JSON file, so we need to parse it + std::wstring scriptPath = L"maps/" + autoStartName.FromUTF8() + L".json"; + JS::RootedValue scriptData(cx); + scriptInterface.ReadJSONFile(scriptPath, &scriptData); + if (!scriptData.isUndefined() && scriptInterface.GetProperty(scriptData, "settings", &settings)) + { + // JSON loaded ok - copy script name over to game attributes + std::wstring scriptFile; + scriptInterface.GetProperty(settings, "Script", scriptFile); + scriptInterface.SetProperty(attrs, "script", scriptFile); // RMS filename + } + else + { + // Problem with JSON file + LOGERROR("Autostart: Error reading random map script '%s'", utf8_from_wstring(scriptPath)); + throw PSERROR_Game_World_MapLoadFailed("Error reading random map script.\nCheck application log for details."); + } + + // Get optional map size argument (default 192) + uint mapSize = 192; + if (args.Has("autostart-size")) + { + CStr size = args.Get("autostart-size"); + mapSize = size.ToUInt(); + } + + scriptInterface.SetProperty(settings, "Size", mapSize); // Random map size (in patches) + + // Get optional number of players (default 2) + size_t numPlayers = 2; + if (args.Has("autostart-players")) + { + CStr num = args.Get("autostart-players"); + numPlayers = num.ToUInt(); + } + + // Set up player data + for (size_t i = 0; i < numPlayers; ++i) + { + JS::RootedValue player(cx); + scriptInterface.Eval("({})", &player); + + // We could load player_defaults.json here, but that would complicate the logic + // even more and autostart is only intended for developers anyway + scriptInterface.SetProperty(player, "Civ", std::string("athen")); + scriptInterface.SetPropertyInt(playerData, i, player); + } + + return "random"; +} + /* * Command line options for autostart * (keep synchronized with binaries/system/readme.txt): @@ -1264,54 +1325,7 @@ if (mapDirectory == L"random") { - // Random map definition will be loaded from JSON file, so we need to parse it - std::wstring scriptPath = L"maps/" + autoStartName.FromUTF8() + L".json"; - JS::RootedValue scriptData(cx); - scriptInterface.ReadJSONFile(scriptPath, &scriptData); - if (!scriptData.isUndefined() && scriptInterface.GetProperty(scriptData, "settings", &settings)) - { - // JSON loaded ok - copy script name over to game attributes - std::wstring scriptFile; - scriptInterface.GetProperty(settings, "Script", scriptFile); - scriptInterface.SetProperty(attrs, "script", scriptFile); // RMS filename - } - else - { - // Problem with JSON file - LOGERROR("Autostart: Error reading random map script '%s'", utf8_from_wstring(scriptPath)); - throw PSERROR_Game_World_MapLoadFailed("Error reading random map script.\nCheck application log for details."); - } - - // Get optional map size argument (default 192) - uint mapSize = 192; - if (args.Has("autostart-size")) - { - CStr size = args.Get("autostart-size"); - mapSize = size.ToUInt(); - } - - scriptInterface.SetProperty(settings, "Size", mapSize); // Random map size (in patches) - - // Get optional number of players (default 2) - size_t numPlayers = 2; - if (args.Has("autostart-players")) - { - CStr num = args.Get("autostart-players"); - numPlayers = num.ToUInt(); - } - - // Set up player data - for (size_t i = 0; i < numPlayers; ++i) - { - JS::RootedValue player(cx); - scriptInterface.Eval("({})", &player); - - // We could load player_defaults.json here, but that would complicate the logic - // even more and autostart is only intended for developers anyway - scriptInterface.SetProperty(player, "Civ", std::string("athen")); - scriptInterface.SetPropertyInt(playerData, i, player); - } - mapType = "random"; + mapType = LoadRandomMap(args, autoStartName, scriptInterface, cx, settings, attrs, playerData, mapType); } else if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") {