Ticket #4127: init_simulation_seed_v1.patch
File init_simulation_seed_v1.patch, 6.9 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/gui/gamesetup/gamesetup.js
function launchGame() 1280 1280 let player = g_PlayerAssignments[guid]; 1281 1281 if (player.player > 0) // not observer or GAIA 1282 1282 g_GameAttributes.settings.PlayerData[player.player - 1].Name = player.name; 1283 1283 } 1284 1284 1285 // This seed is only used for map-generation 1286 if (g_GameAttributes.mapType == "random") 1287 g_GameAttributes.settings.Seed = Math.floor(Math.random() * 65536); 1288 1289 g_GameAttributes.settings.AISeed = Math.floor(Math.random() * 65536); 1285 // Seed used for both map generation and simulation 1286 g_GameAttributes.settings.Seed = Math.floor(Math.random() * Math.pow(2, 32)); 1287 g_GameAttributes.settings.AISeed = Math.floor(Math.random() * Math.pow(2, 32)); 1290 1288 1291 1289 // Used for identifying rated game reports for the lobby 1292 1290 g_GameAttributes.matchID = Engine.GetMatchID(); 1293 1291 1294 1292 if (g_IsNetworked) -
source/simulation2/Simulation2.cpp
void CSimulation2::SetMapSettings(const 759 759 } 760 760 761 761 void CSimulation2::SetMapSettings(JS::HandleValue settings) 762 762 { 763 763 m->m_MapSettings = settings; 764 765 u32 seed = 0; 766 if (!m->m_ComponentManager.GetScriptInterface().GetProperty(m->m_MapSettings, "Seed", seed)) 767 LOGWARNING("CSimulation2::SetInitAttributes: No seed value specified - using %d", seed); 768 769 m->m_ComponentManager.SetRNGSeed(seed); 764 770 } 765 771 766 772 std::string CSimulation2::GetMapSettingsString() 767 773 { 768 774 return m->m_ComponentManager.GetScriptInterface().StringifyJSON(&m->m_MapSettings); -
source/simulation2/Simulation2.h
public: 106 106 void GetInitAttributes(JS::MutableHandleValue ret); 107 107 108 108 /** 109 109 * Set the initial map settings (as a UTF-8-encoded JSON string), 110 110 * which will be used to set up the simulation state. 111 * Called from atlas. 111 112 */ 112 113 void SetMapSettings(const std::string& settings); 113 114 114 115 /** 115 116 * Set the initial map settings, which will be used 116 117 * to set up the simulation state. 118 * Called from MapReader (for all map-types). 117 119 */ 118 120 void SetMapSettings(JS::HandleValue settings); 119 121 120 122 /** 121 123 * Get the current map settings as a UTF-8 JSON string. … … public: 266 268 */ 267 269 std::string GetAIData(); 268 270 269 271 private: 270 272 CSimulation2Impl* m; 273 271 274 }; 272 275 273 276 #endif // INCLUDED_SIMULATION2 -
source/simulation2/system/ComponentManager.cpp
1 /* Copyright (C) 201 5Wildfire Games.1 /* Copyright (C) 2016 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * 4 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 5 * it under the terms of the GNU General Public License as published by 6 6 * the Free Software Foundation, either version 2 of the License, or … … CComponentManager::CComponentManager(CSi 59 59 m_SimContext(context), m_CurrentlyHotloading(false) 60 60 { 61 61 context.SetComponentManager(this); 62 62 63 63 m_ScriptInterface.SetCallbackData(static_cast<void*> (this)); 64 65 // TODO: ought to seed the RNG (in a network-synchronised way) before we use it66 64 m_ScriptInterface.ReplaceNondeterministicRNG(m_RNG); 67 65 m_ScriptInterface.LoadGlobalScripts(); 68 66 69 67 // For component script tests, the test system sets up its own scripted implementation of 70 68 // these functions, so we skip registering them here in those cases … … void CComponentManager::ResetState() 538 536 // Reset IDs 539 537 m_NextEntityId = SYSTEM_ENTITY + 1; 540 538 m_NextLocalEntityId = FIRST_LOCAL_ENTITY; 541 539 } 542 540 541 void CComponentManager::SetRNGSeed(u32 seed) 542 { 543 m_RNG.seed(seed); 544 } 545 543 546 void CComponentManager::RegisterComponentType(InterfaceId iid, ComponentTypeId cid, AllocFunc alloc, DeallocFunc dealloc, 544 547 const char* name, const std::string& schema) 545 548 { 546 549 ComponentType c(CT_Native, iid, alloc, dealloc, name, schema, std::move(DefPersistentRooted<JS::Value>())); 547 550 m_ComponentTypesById.insert(std::make_pair(cid, std::move(c))); -
source/simulation2/system/ComponentManager.h
1 /* Copyright (C) 201 5Wildfire Games.1 /* Copyright (C) 2016 Wildfire Games. 2 2 * This file is part of 0 A.D. 3 3 * 4 4 * 0 A.D. is free software: you can redistribute it and/or modify 5 5 * it under the terms of the GNU General Public License as published by 6 6 * the Free Software Foundation, either version 2 of the License, or … … public: 293 293 * Resets the dynamic simulation state (deletes all entities, resets entity ID counters; 294 294 * doesn't unload/reload component scripts). 295 295 */ 296 296 void ResetState(); 297 297 298 /** 299 * Initializes the random number generator with a seed determined by the host. 300 */ 301 void SetRNGSeed(u32 seed); 302 298 303 // Various state serialization functions: 299 304 bool ComputeStateHash(std::string& outHash, bool quick); 300 305 bool DumpDebugState(std::ostream& stream, bool includeDebugInfo); 301 306 // FlushDestroyedComponents must be called before SerializeState (since the destruction queue 302 307 // won't get serialized) -
source/simulation2/tests/test_ComponentManager.h
public: 94 94 TS_ASSERT_EQUALS(man.AllocateNewEntity(), (u32)2); 95 95 TS_ASSERT_EQUALS(man.AllocateNewEntity(3), (u32)3); 96 96 TS_ASSERT_EQUALS(man.AllocateNewLocalEntity(), (u32)FIRST_LOCAL_ENTITY); 97 97 } 98 98 99 void test_rng() 100 { 101 // Ensure we get the same random number with the same seed 102 // and two different numbers with two different seeds 103 for (int test=0; test<2; ++test) 104 { 105 CSimContext context; 106 CComponentManager man(context, g_ScriptRuntime); 107 man.SetRNGSeed(1); 108 double first; 109 if (!man.m_ScriptInterface.MathRandom(first)) 110 TS_FAIL("Couldn't get random number!"); 111 112 CSimContext context2; 113 CComponentManager man2(context2, g_ScriptRuntime); 114 double second; 115 man2.SetRNGSeed(test == 0 ? 1 : 2); 116 if (!man2.m_ScriptInterface.MathRandom(second)) 117 TS_FAIL("Couldn't get random number!"); 118 119 if (test == 0) 120 TS_ASSERT_EQUALS(first, second); 121 122 if (test == 1) 123 TS_ASSERT_DIFFERS(first, second); 124 } 125 } 126 99 127 void test_AddComponent_errors() 100 128 { 101 129 CSimContext context; 102 130 CComponentManager man(context, g_ScriptRuntime); 103 131 man.LoadComponentTypes();