Ticket #2931: seed.2.diff

File seed.2.diff, 5.8 KB (added by mimo, 9 years ago)
  • binaries/data/mods/public/simulation/helpers/InitGame.js

     
    5151            cmpPlayer.SetResourceCounts(newResourceCounts);
    5252        }
    5353    }
     54    let seed = settings.Seed ? settings.Seed : 0;
     55    cmpAIManager.SetRNGSeed(seed);
    5456    cmpAIManager.TryLoadSharedComponent();
    5557    cmpAIManager.RunGamestateInit();
    5658}
  • source/ps/GameSetup/GameSetup.cpp

     
    11621162 * Command line options for autostart (keep synchronized with readme.txt):
    11631163 *
    11641164 * -autostart="TYPEDIR/MAPNAME"     enables autostart and sets MAPNAME; TYPEDIR is skirmishes, scenarios, or random
    1165  * -autostart-ai=PLAYER:AI          sets the AI for PLAYER (e.g. 2:petra)
     1165 * -autostart-ai=PLAYER:AI          sets the AI for PLAYER (e.g. 2:petra)
    11661166 * -autostart-aidiff=PLAYER:DIFF    sets the DIFFiculty of PLAYER's AI (0: easy, 3: very hard)
    11671167 * -autostart-civ=PLAYER:CIV        sets PLAYER's civilisation to CIV (skirmish and random maps only)
     1168 * -autostart-seed=SEED         sets (AI + random map) SEED value (default 0, use -1 for random)
    11681169 * Multiplayer:
    11691170 * -autostart-playername=NAME       sets local player NAME (default 'anonymous')
    1170  * -autostart-host                  sets multiplayer host mode
     1171 * -autostart-host              sets multiplayer host mode
    11711172 * -autostart-host-players=NUMBER   sets NUMBER of human players for multiplayer game (default 2)
    1172  * -autostart-client=IP             sets multiplayer client to join host at given IP address
     1173 * -autostart-client=IP         sets multiplayer client to join host at given IP address
    11731174 * Random maps only:
    1174  * -autostart-seed=SEED             sets random map SEED value (default 0, use -1 for random)
    1175  * -autostart-size=TILES            sets random map size in TILES (default 192)
     1175 * -autostart-size=TILES        sets random map size in TILES (default 192)
    11761176 * -autostart-players=NUMBER        sets NUMBER of players on random map (default 2)
    11771177 *
    11781178 * Examples:
     
    12181218
    12191219    if (mapDirectory == L"random")
    12201220    {
    1221         // Default seed is 0
    1222         u32 seed = 0;
    1223         CStr seedArg = args.Get("autostart-seed");
    1224 
    1225         if (!seedArg.empty())
    1226         {
    1227             // Random seed value
    1228             if (seedArg != "-1")
    1229                 seed = rand();
    1230             else
    1231                 seed = seedArg.ToULong();
    1232         }
    1233        
    12341221        // Random map definition will be loaded from JSON file, so we need to parse it
    12351222        std::wstring scriptPath = L"maps/" + autoStartName.FromUTF8() + L".json";
    12361223        JS::RootedValue scriptData(cx);
     
    12571244            mapSize = size.ToUInt();
    12581245        }
    12591246
    1260         scriptInterface.SetProperty(settings, "Seed", seed);        // Random seed
    12611247        scriptInterface.SetProperty(settings, "Size", mapSize);     // Random map size (in patches)
    12621248
    12631249        // Get optional number of players (default 2)
     
    13071293    scriptInterface.SetProperty(attrs, "map", std::string("maps/" + autoStartName));
    13081294    scriptInterface.SetProperty(settings, "mapType", mapType);
    13091295
     1296    // Random seed value for random maps and AIs, default is 0
     1297    u32 seed = 0;
     1298    if (args.Has("autostart-seed"))
     1299    {
     1300        CStr seedArg = args.Get("autostart-seed");
     1301        if (seedArg == "-1")
     1302            seed = rand();
     1303        else
     1304            seed = seedArg.ToULong();
     1305        LOGWARNING(L"Autostart: random seed %d ", seed);
     1306    }
     1307        scriptInterface.SetProperty(settings, "Seed", seed);
     1308
    13101309    // Set player data for AIs
    13111310    //      attrs.settings = { PlayerData: [ { AI: ... }, ... ] }:
    13121311    if (args.Has("autostart-ai"))
  • source/simulation2/components/CCmpAIManager.cpp

     
    225225        m_HasSharedComponent(false)
    226226    {
    227227
    228         // TODO: ought to seed the RNG (in a network-synchronised way) before we use it
    229228        m_ScriptInterface->ReplaceNondeterministicRNG(m_RNG);
    230229        m_ScriptInterface->LoadGlobalScripts();
    231230
     
    361360        tex_write(&t, filename);
    362361    }
    363362
     363    void SetRNGSeed(uint32_t seed)
     364    {
     365        m_RNG.seed(seed);
     366    }
     367
    364368    bool TryLoadSharedComponent(bool hasTechs)
    365369    {
    366370        JSContext* cx = m_ScriptInterface->GetContext();
     
    917921            cmpRangeManager->SetLosRevealAll(player, true);
    918922    }
    919923   
     924    virtual void SetRNGSeed(uint32_t seed)
     925    {
     926        m_Worker.SetRNGSeed(seed);
     927    }
     928
    920929    virtual void TryLoadSharedComponent()
    921930    {
    922931        ScriptInterface& scriptInterface = GetSimContext().GetScriptInterface();
  • source/simulation2/components/ICmpAIManager.cpp

     
    2626
    2727BEGIN_INTERFACE_WRAPPER(AIManager)
    2828DEFINE_INTERFACE_METHOD_3("AddPlayer", void, ICmpAIManager, AddPlayer, std::wstring, player_id_t, uint8_t)
     29DEFINE_INTERFACE_METHOD_1("SetRNGSeed", void, ICmpAIManager, SetRNGSeed, uint32_t)
    2930DEFINE_INTERFACE_METHOD_0("TryLoadSharedComponent", void, ICmpAIManager, TryLoadSharedComponent)
    3031DEFINE_INTERFACE_METHOD_0("RunGamestateInit", void, ICmpAIManager, RunGamestateInit)
    3132END_INTERFACE_WRAPPER(AIManager)
  • source/simulation2/components/ICmpAIManager.h

     
    3131     * to control player @p player.
    3232     */
    3333    virtual void AddPlayer(std::wstring id, player_id_t player, uint8_t difficulty) = 0;
     34    virtual void SetRNGSeed(uint32_t seed) = 0;
    3435    virtual void TryLoadSharedComponent() = 0;
    3536    virtual void RunGamestateInit() = 0;
    3637