Changeset View
Changeset View
Standalone View
Standalone View
source/ps/GameSetup/GameSetup.cpp
Context not available. | |||||
return mapElement.GetText(); | return mapElement.GetText(); | ||||
elexis: Should we move this to `Autostart.cpp` and maybe pack it into a namespace rather than these… | |||||
} | } | ||||
Done Inline ActionsI think, it'd be better to have these lines with the same indentation as the const CmdLineArgs& args. vladislavbelov: I think, it'd be better to have these lines with the same indentation as the `const… | |||||
Not Done Inline Actionsconst ScriptInterface& scriptInterface AFAIK by leper's changes. vladislavbelov: `const ScriptInterface& scriptInterface` AFAIK by leper's changes. | |||||
std::string LoadRandomMap(const CmdLineArgs& args, | |||||
CStr& autoStartName, | |||||
ScriptInterface& | |||||
Not Done Inline ActionsJS values are passed as JS::Value rather than JS::RootedValue (so as to prevent rooting issues I suspect) elexis: JS values are passed as JS::Value rather than JS::RootedValue (so as to prevent rooting issues… | |||||
scriptInterface, | |||||
JSContext* cx, | |||||
Not Done Inline Actionsuse whitespace like we do in this file, directory and elsewhere (Coding_Conventions) elexis: use whitespace like we do in this file, directory and elsewhere (Trac:Coding_Conventions) | |||||
Done Inline ActionsWhy not as a return value? vladislavbelov: Why not as a return value? | |||||
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 | * Command line options for autostart | ||||
* (keep synchronized with binaries/system/readme.txt): | * (keep synchronized with binaries/system/readme.txt): | ||||
Done Inline Actionsmissing spaces after comma elexis: missing spaces after comma | |||||
Context not available. | |||||
if (mapDirectory == L"random") | if (mapDirectory == L"random") | ||||
{ | { | ||||
// Random map definition will be loaded from JSON file, so we need to parse it | mapType = LoadRandomMap(args, autoStartName, scriptInterface, cx, settings, attrs, playerData, mapType); | ||||
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"; | |||||
} | } | ||||
else if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") | else if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes") | ||||
{ | { | ||||
Context not available. | |||||
Not Done Inline Actionsdont need these newlines elexis: dont need these newlines |
Wildfire Games · Phabricator
Should we move this to Autostart.cpp and maybe pack it into a namespace rather than these unsorted globals?