Ticket #2931: seed.diff

File seed.diff, 7.2 KB (added by mimo, 9 years ago)
  • binaries/data/mods/public/simulation/ai/common-api/shared.js

     
    66{
    77    if (!settings)
    88        return;
     9
     10    // TODO should be replaced by a seeded random generator
     11    if (settings.seed)
     12        for (let i = 0; i < settings.seed%1000; ++i)
     13            Math.random();
    914   
    1015    this._players = settings.players;
    1116    this._templates = settings.templates;
  • binaries/data/mods/public/simulation/helpers/InitGame.js

     
    5151            cmpPlayer.SetResourceCounts(newResourceCounts);
    5252        }
    5353    }
    54     cmpAIManager.TryLoadSharedComponent();
     54    let randomSeed = settings.Seed ? settings.Seed : 0;
     55    cmpAIManager.TryLoadSharedComponent(randomSeed);
    5556    cmpAIManager.RunGamestateInit();
    5657}
    5758
  • 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

     
    361361        tex_write(&t, filename);
    362362    }
    363363
    364     bool TryLoadSharedComponent(bool hasTechs)
     364    bool TryLoadSharedComponent(bool hasTechs, uint32_t randomSeed)
    365365    {
    366366        JSContext* cx = m_ScriptInterface->GetContext();
    367367        JSAutoRequest rq(cx);
     
    412412            m_ScriptInterface->SetPropertyInt(playersID, i, val, true);
    413413        }
    414414       
     415        m_ScriptInterface->SetProperty(settings, "seed", randomSeed);
    415416        m_ScriptInterface->SetProperty(settings, "players", playersID);
    416417        ENSURE(m_HasLoadedEntityTemplates);
    417418        m_ScriptInterface->SetProperty(settings, "templates", m_EntityTemplates, false);
     
    671672        deserializer.NumberU32_Unbounded("num ais", numAis);
    672673
    673674        deserializer.Bool("useSharedScript", m_HasSharedComponent);
    674         TryLoadSharedComponent(false);
     675        TryLoadSharedComponent(false, 0);
    675676        if (m_HasSharedComponent)
    676677        {
    677678            JS::RootedValue sharedData(cx);
     
    917918            cmpRangeManager->SetLosRevealAll(player, true);
    918919    }
    919920   
    920     virtual void TryLoadSharedComponent()
     921    virtual void TryLoadSharedComponent(uint32_t randomSeed)
    921922    {
    922923        ScriptInterface& scriptInterface = GetSimContext().GetScriptInterface();
    923924        JSContext* cx = scriptInterface.GetContext();
     
    931932        JS::RootedValue techTemplates(cx, cmpTechTemplateManager->GetAllTechs().get());
    932933       
    933934        m_Worker.RegisterTechTemplates(scriptInterface.WriteStructuredClone(techTemplates));
    934         m_Worker.TryLoadSharedComponent(true);
     935        m_Worker.TryLoadSharedComponent(true, randomSeed);
    935936    }
    936937
    937938    virtual void RunGamestateInit()
  • 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)
    29 DEFINE_INTERFACE_METHOD_0("TryLoadSharedComponent", void, ICmpAIManager, TryLoadSharedComponent)
     29DEFINE_INTERFACE_METHOD_1("TryLoadSharedComponent", void, ICmpAIManager, TryLoadSharedComponent, uint32_t)
    3030DEFINE_INTERFACE_METHOD_0("RunGamestateInit", void, ICmpAIManager, RunGamestateInit)
    3131END_INTERFACE_WRAPPER(AIManager)
    3232
  • 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 TryLoadSharedComponent() = 0;
     34    virtual void TryLoadSharedComponent(uint32_t randomSeed) = 0;
    3535    virtual void RunGamestateInit() = 0;
    3636
    3737    /**