Ticket #4127: init_simulation_seed_v1.1.patch
File init_simulation_seed_v1.1.patch, 6.6 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 750 750 } 751 751 752 752 void CSimulation2::SetMapSettings(JS::HandleValue settings) 753 753 { 754 754 m->m_MapSettings = settings; 755 756 u32 seed = 0; 757 if (!m->m_ComponentManager.GetScriptInterface().GetProperty(m->m_MapSettings, "Seed", seed)) 758 LOGWARNING("CSimulation2::SetInitAttributes: No seed value specified - using %d", seed); 759 760 m->m_ComponentManager.SetRNGSeed(seed); 755 761 } 756 762 757 763 std::string CSimulation2::GetMapSettingsString() 758 764 { 759 765 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. -
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 double first; 103 { 104 CSimContext context; 105 CComponentManager man(context, g_ScriptRuntime); 106 man.SetRNGSeed(123); 107 108 if (!man.m_ScriptInterface.MathRandom(first)) 109 TS_FAIL("Couldn't get random number!"); 110 } 111 112 double second; 113 { 114 CSimContext context; 115 CComponentManager man(context, g_ScriptRuntime); 116 man.SetRNGSeed(123); 117 118 if (!man.m_ScriptInterface.MathRandom(second)) 119 TS_FAIL("Couldn't get random number!"); 120 } 121 122 TS_ASSERT_EQUALS(first, second); 123 } 124 99 125 void test_AddComponent_errors() 100 126 { 101 127 CSimContext context; 102 128 CComponentManager man(context, g_ScriptRuntime); 103 129 man.LoadComponentTypes();