Ticket #2798: autostart.diff

File autostart.diff, 5.8 KB (added by mimo, 10 years ago)
  • source/ps/GameSetup/GameSetup.cpp

     
    12761276        }
    12771277        mapType = "random";
    12781278    }
    1279     else if (mapDirectory == L"scenarios")
     1279    else if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes")
    12801280    {
    12811281        // Initialize general settings from the map data so some values
    12821282        // (e.g. name of map) are always present, even when autostart is
     
    12851285        // for example...)
    12861286        CStr8 mapSettingsJSON = LoadSettingsOfScenarioMap("maps/" + autoStartName + ".xml");
    12871287        scriptInterface.ParseJSON(mapSettingsJSON, &settings);
    1288         mapType = "scenario";
    1289     }
    1290     else if (mapDirectory == L"skirmishes")
    1291     {
    1292         // In skirmish mode, the player initialization data is taken from the
    1293         // game-setup settings (see CGame::RegisterInit(...)). If some player
    1294         // is not initialized (PlayerData[] holding fewer/more entries than
    1295         // defined in map), there's a crash.
    1296         // To prevent this, we mimic the behavior of the game setup screen by
    1297         // retrieving the map settings from the actual map xml...
    1298         CStr8 mapSettingsJSON = LoadSettingsOfScenarioMap("maps/" + autoStartName + ".xml");
    1299         scriptInterface.ParseJSON(mapSettingsJSON, &settings);
    13001288       
    13011289        // ...and initialize the playerData array being edited by
    13021290        // autostart-civ et.al. with the real map data, so sensible values
    13031291        // are always present:
    13041292        scriptInterface.GetProperty(settings, "PlayerData", &playerData);
    1305         mapType = "skirmish";
     1293
     1294        if (mapDirectory == L"scenarios")
     1295            mapType = "scenario";
     1296        else
     1297            mapType = "skirmish";
    13061298    }
     1299
    13071300    if (mapType.empty())
    13081301    {
    13091302        LOGERROR(L"Unrecognized map type '%ls' detected", mapType.c_str());
     
    13111304    }
    13121305    scriptInterface.SetProperty(attrs, "mapType", mapType);
    13131306    scriptInterface.SetProperty(attrs, "map", std::string("maps/" + autoStartName));
    1314 
    13151307    scriptInterface.SetProperty(settings, "mapType", mapType);
    13161308
    13171309    // Set player data for AIs
     
    13211313        std::vector<CStr> aiArgs = args.GetMultiple("autostart-ai");
    13221314        for (size_t i = 0; i < aiArgs.size(); ++i)
    13231315        {
     1316            int playerID = aiArgs[i].BeforeFirst(":").ToInt();
     1317
    13241318            // Instead of overwriting existing player data, modify the array
    13251319            JS::RootedValue player(cx);
    1326             if (!scriptInterface.GetPropertyInt(playerData, i, &player) || player.isUndefined())
     1320            if (!scriptInterface.GetPropertyInt(playerData, playerID-1, &player) || player.isUndefined())
    13271321            {
     1322                if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes")
     1323                {
     1324                    // playerID is certainly bigger than this map player number
     1325                    LOGWARNING(L"Ai settings for player %d ignored.", playerID);
     1326                    continue;
     1327                }
    13281328                scriptInterface.Eval("({})", &player);
    13291329            }
    13301330
    1331             int playerID = aiArgs[i].BeforeFirst(":").ToInt();
    13321331            CStr name = aiArgs[i].AfterFirst(":");
    1333 
    13341332            scriptInterface.SetProperty(player, "AI", std::string(name));
    13351333            scriptInterface.SetProperty(player, "AIDiff", 2);
    13361334            scriptInterface.SetPropertyInt(playerData, playerID-1, player);
     
    13421340        std::vector<CStr> civArgs = args.GetMultiple("autostart-aidiff");
    13431341        for (size_t i = 0; i < civArgs.size(); ++i)
    13441342        {
     1343            int playerID = civArgs[i].BeforeFirst(":").ToInt();
     1344
    13451345            // Instead of overwriting existing player data, modify the array
    13461346            JS::RootedValue player(cx);
    1347             if (!scriptInterface.GetPropertyInt(playerData, i, &player) || player.isUndefined())
     1347            if (!scriptInterface.GetPropertyInt(playerData, playerID-1, &player) || player.isUndefined())
    13481348            {
     1349                if (mapDirectory == L"scenarios" || mapDirectory == L"skirmishes")
     1350                {
     1351                    // playerID is certainly bigger than this map player number
     1352                    LOGWARNING(L"Aidiff settings for player %d ignored.", playerID);
     1353                    continue;
     1354                }
    13491355                scriptInterface.Eval("({})", &player);
    13501356            }
    1351            
    1352             int playerID = civArgs[i].BeforeFirst(":").ToInt();
    1353             int difficulty = civArgs[i].AfterFirst(":").ToInt();
    1354            
     1357
     1358            int difficulty = civArgs[i].AfterFirst(":").ToInt();           
    13551359            scriptInterface.SetProperty(player, "AIDiff", difficulty);
    13561360            scriptInterface.SetPropertyInt(playerData, playerID-1, player);
    13571361        }
     
    13591363    // Set player data for Civs
    13601364    if (args.Has("autostart-civ"))
    13611365    {
    1362         std::vector<CStr> civArgs = args.GetMultiple("autostart-civ");
    1363         for (size_t i = 0; i < civArgs.size(); ++i)
     1366        if (mapDirectory != L"scenarios")
    13641367        {
    1365             // Instead of overwriting existing player data, modify the array
    1366             JS::RootedValue player(cx);
    1367             if (!scriptInterface.GetPropertyInt(playerData, i, &player) || player.isUndefined())
     1368            std::vector<CStr> civArgs = args.GetMultiple("autostart-civ");
     1369            for (size_t i = 0; i < civArgs.size(); ++i)
    13681370            {
    1369                 scriptInterface.Eval("({})", &player);
     1371                int playerID = civArgs[i].BeforeFirst(":").ToInt();
     1372
     1373                // Instead of overwriting existing player data, modify the array
     1374                JS::RootedValue player(cx);
     1375                if (!scriptInterface.GetPropertyInt(playerData, playerID-1, &player) || player.isUndefined())
     1376                {
     1377                    if (mapDirectory == L"skirmishes")
     1378                    {
     1379                        // playerID is certainly bigger than this map player number
     1380                        LOGWARNING(L"Civ settings for player %d ignored.", playerID);
     1381                        continue;
     1382                    }
     1383                    scriptInterface.Eval("({})", &player);
     1384                }
     1385           
     1386                CStr name = civArgs[i].AfterFirst(":");         
     1387                scriptInterface.SetProperty(player, "Civ", std::string(name));
     1388                scriptInterface.SetPropertyInt(playerData, playerID-1, player);
    13701389            }
    1371            
    1372             int playerID = civArgs[i].BeforeFirst(":").ToInt();
    1373             CStr name = civArgs[i].AfterFirst(":");
    1374            
    1375             scriptInterface.SetProperty(player, "Civ", std::string(name));
    1376             scriptInterface.SetPropertyInt(playerData, playerID-1, player);
    13771390        }
     1391        else
     1392            LOGWARNING(L"For scenarios, autostart civs are ignored.");
    13781393    }
    13791394
    13801395    // Add player data to map settings