Ticket #2931: seed.diff
File seed.diff, 7.2 KB (added by , 9 years ago) |
---|
-
binaries/data/mods/public/simulation/ai/common-api/shared.js
6 6 { 7 7 if (!settings) 8 8 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(); 9 14 10 15 this._players = settings.players; 11 16 this._templates = settings.templates; -
binaries/data/mods/public/simulation/helpers/InitGame.js
51 51 cmpPlayer.SetResourceCounts(newResourceCounts); 52 52 } 53 53 } 54 cmpAIManager.TryLoadSharedComponent(); 54 let randomSeed = settings.Seed ? settings.Seed : 0; 55 cmpAIManager.TryLoadSharedComponent(randomSeed); 55 56 cmpAIManager.RunGamestateInit(); 56 57 } 57 58 -
source/ps/GameSetup/GameSetup.cpp
1162 1162 * Command line options for autostart (keep synchronized with readme.txt): 1163 1163 * 1164 1164 * -autostart="TYPEDIR/MAPNAME" enables autostart and sets MAPNAME; TYPEDIR is skirmishes, scenarios, or random 1165 * -autostart-ai=PLAYER:AI 1165 * -autostart-ai=PLAYER:AI sets the AI for PLAYER (e.g. 2:petra) 1166 1166 * -autostart-aidiff=PLAYER:DIFF sets the DIFFiculty of PLAYER's AI (0: easy, 3: very hard) 1167 1167 * -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) 1168 1169 * Multiplayer: 1169 1170 * -autostart-playername=NAME sets local player NAME (default 'anonymous') 1170 * -autostart-host 1171 * -autostart-host sets multiplayer host mode 1171 1172 * -autostart-host-players=NUMBER sets NUMBER of human players for multiplayer game (default 2) 1172 * -autostart-client=IP 1173 * -autostart-client=IP sets multiplayer client to join host at given IP address 1173 1174 * 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) 1176 1176 * -autostart-players=NUMBER sets NUMBER of players on random map (default 2) 1177 1177 * 1178 1178 * Examples: … … 1218 1218 1219 1219 if (mapDirectory == L"random") 1220 1220 { 1221 // Default seed is 01222 u32 seed = 0;1223 CStr seedArg = args.Get("autostart-seed");1224 1225 if (!seedArg.empty())1226 {1227 // Random seed value1228 if (seedArg != "-1")1229 seed = rand();1230 else1231 seed = seedArg.ToULong();1232 }1233 1234 1221 // Random map definition will be loaded from JSON file, so we need to parse it 1235 1222 std::wstring scriptPath = L"maps/" + autoStartName.FromUTF8() + L".json"; 1236 1223 JS::RootedValue scriptData(cx); … … 1257 1244 mapSize = size.ToUInt(); 1258 1245 } 1259 1246 1260 scriptInterface.SetProperty(settings, "Seed", seed); // Random seed1261 1247 scriptInterface.SetProperty(settings, "Size", mapSize); // Random map size (in patches) 1262 1248 1263 1249 // Get optional number of players (default 2) … … 1307 1293 scriptInterface.SetProperty(attrs, "map", std::string("maps/" + autoStartName)); 1308 1294 scriptInterface.SetProperty(settings, "mapType", mapType); 1309 1295 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 1310 1309 // Set player data for AIs 1311 1310 // attrs.settings = { PlayerData: [ { AI: ... }, ... ] }: 1312 1311 if (args.Has("autostart-ai")) -
source/simulation2/components/CCmpAIManager.cpp
361 361 tex_write(&t, filename); 362 362 } 363 363 364 bool TryLoadSharedComponent(bool hasTechs )364 bool TryLoadSharedComponent(bool hasTechs, uint32_t randomSeed) 365 365 { 366 366 JSContext* cx = m_ScriptInterface->GetContext(); 367 367 JSAutoRequest rq(cx); … … 412 412 m_ScriptInterface->SetPropertyInt(playersID, i, val, true); 413 413 } 414 414 415 m_ScriptInterface->SetProperty(settings, "seed", randomSeed); 415 416 m_ScriptInterface->SetProperty(settings, "players", playersID); 416 417 ENSURE(m_HasLoadedEntityTemplates); 417 418 m_ScriptInterface->SetProperty(settings, "templates", m_EntityTemplates, false); … … 671 672 deserializer.NumberU32_Unbounded("num ais", numAis); 672 673 673 674 deserializer.Bool("useSharedScript", m_HasSharedComponent); 674 TryLoadSharedComponent(false );675 TryLoadSharedComponent(false, 0); 675 676 if (m_HasSharedComponent) 676 677 { 677 678 JS::RootedValue sharedData(cx); … … 917 918 cmpRangeManager->SetLosRevealAll(player, true); 918 919 } 919 920 920 virtual void TryLoadSharedComponent( )921 virtual void TryLoadSharedComponent(uint32_t randomSeed) 921 922 { 922 923 ScriptInterface& scriptInterface = GetSimContext().GetScriptInterface(); 923 924 JSContext* cx = scriptInterface.GetContext(); … … 931 932 JS::RootedValue techTemplates(cx, cmpTechTemplateManager->GetAllTechs().get()); 932 933 933 934 m_Worker.RegisterTechTemplates(scriptInterface.WriteStructuredClone(techTemplates)); 934 m_Worker.TryLoadSharedComponent(true );935 m_Worker.TryLoadSharedComponent(true, randomSeed); 935 936 } 936 937 937 938 virtual void RunGamestateInit() -
source/simulation2/components/ICmpAIManager.cpp
26 26 27 27 BEGIN_INTERFACE_WRAPPER(AIManager) 28 28 DEFINE_INTERFACE_METHOD_3("AddPlayer", void, ICmpAIManager, AddPlayer, std::wstring, player_id_t, uint8_t) 29 DEFINE_INTERFACE_METHOD_ 0("TryLoadSharedComponent", void, ICmpAIManager, TryLoadSharedComponent)29 DEFINE_INTERFACE_METHOD_1("TryLoadSharedComponent", void, ICmpAIManager, TryLoadSharedComponent, uint32_t) 30 30 DEFINE_INTERFACE_METHOD_0("RunGamestateInit", void, ICmpAIManager, RunGamestateInit) 31 31 END_INTERFACE_WRAPPER(AIManager) 32 32 -
source/simulation2/components/ICmpAIManager.h
31 31 * to control player @p player. 32 32 */ 33 33 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; 35 35 virtual void RunGamestateInit() = 0; 36 36 37 37 /**