Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/Simulation2.cpp
Show First 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | |||||
class CSimulation2Impl | class CSimulation2Impl | ||||
{ | { | ||||
public: | public: | ||||
CSimulation2Impl(CUnitManager* unitManager, shared_ptr<ScriptRuntime> rt, CTerrain* terrain) : | CSimulation2Impl(CUnitManager* unitManager, shared_ptr<ScriptRuntime> rt, CTerrain* terrain) : | ||||
m_SimContext(), m_ComponentManager(m_SimContext, rt), | m_SimContext(), m_ComponentManager(m_SimContext, rt), | ||||
m_EnableOOSLog(false), m_EnableSerializationTest(false), m_RejoinTestTurn(-1), m_TestingRejoin(false), | m_EnableOOSLog(false), m_EnableSerializationTest(false), m_RejoinTestTurn(-1), m_TestingRejoin(false), | ||||
m_SecondaryTerrain(nullptr), m_SecondaryContext(nullptr), m_SecondaryComponentManager(nullptr), m_SecondaryLoadedScripts(nullptr), | m_SecondaryTerrain(nullptr), m_SecondaryContext(nullptr), m_SecondaryComponentManager(nullptr), m_SecondaryLoadedScripts(nullptr), | ||||
m_MapSettings(rt->m_rt), m_InitAttributes(rt->m_rt) | m_InitAttributes(rt->m_rt) | ||||
{ | { | ||||
m_SimContext.m_UnitManager = unitManager; | m_SimContext.m_UnitManager = unitManager; | ||||
m_SimContext.m_Terrain = terrain; | m_SimContext.m_Terrain = terrain; | ||||
m_ComponentManager.LoadComponentTypes(); | m_ComponentManager.LoadComponentTypes(); | ||||
RegisterFileReloadFunc(ReloadChangedFileCB, this); | RegisterFileReloadFunc(ReloadChangedFileCB, this); | ||||
// Tests won't have config initialised | // Tests won't have config initialised | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | public: | ||||
CSimContext m_SimContext; | CSimContext m_SimContext; | ||||
CComponentManager m_ComponentManager; | CComponentManager m_ComponentManager; | ||||
double m_DeltaTime; | double m_DeltaTime; | ||||
float m_LastFrameOffset; | float m_LastFrameOffset; | ||||
std::string m_StartupScript; | std::string m_StartupScript; | ||||
JS::PersistentRootedValue m_InitAttributes; | JS::PersistentRootedValue m_InitAttributes; | ||||
JS::PersistentRootedValue m_MapSettings; | |||||
std::set<VfsPath> m_LoadedScripts; | std::set<VfsPath> m_LoadedScripts; | ||||
uint32_t m_TurnNumber; | uint32_t m_TurnNumber; | ||||
bool m_EnableOOSLog; | bool m_EnableOOSLog; | ||||
OsPath m_OOSLogPath; | OsPath m_OOSLogPath; | ||||
Show All 17 Lines | public: | ||||
}; | }; | ||||
void DumpSerializationTestState(SerializationTestState& state, const OsPath& path, const OsPath::String& suffix); | void DumpSerializationTestState(SerializationTestState& state, const OsPath& path, const OsPath::String& suffix); | ||||
void ReportSerializationFailure( | void ReportSerializationFailure( | ||||
SerializationTestState* primaryStateBefore, SerializationTestState* primaryStateAfter, | SerializationTestState* primaryStateBefore, SerializationTestState* primaryStateAfter, | ||||
SerializationTestState* secondaryStateBefore, SerializationTestState* secondaryStateAfter); | SerializationTestState* secondaryStateBefore, SerializationTestState* secondaryStateAfter); | ||||
void InitRNGSeedSimulation(); | /** | ||||
void InitRNGSeedAI(); | * This returns a reference to the settings object of m_InitAttributes, not a copy! | ||||
*/ | |||||
JS::Value GetMapSettings(); | |||||
void InitRNGSeedSimulation(JS::HandleValue settings); | |||||
void InitRNGSeedAI(JS::HandleValue settings); | |||||
static std::vector<SimulationCommand> CloneCommandsFromOtherContext(const ScriptInterface& oldScript, const ScriptInterface& newScript, | static std::vector<SimulationCommand> CloneCommandsFromOtherContext(const ScriptInterface& oldScript, const ScriptInterface& newScript, | ||||
const std::vector<SimulationCommand>& commands) | const std::vector<SimulationCommand>& commands) | ||||
{ | { | ||||
JSContext* cxOld = oldScript.GetContext(); | JSContext* cxOld = oldScript.GetContext(); | ||||
JSAutoRequest rqOld(cxOld); | JSAutoRequest rqOld(cxOld); | ||||
std::vector<SimulationCommand> newCommands; | std::vector<SimulationCommand> newCommands; | ||||
newCommands.reserve(commands.size()); | newCommands.reserve(commands.size()); | ||||
for (const SimulationCommand& command : commands) | for (const SimulationCommand& command : commands) | ||||
{ | { | ||||
JSContext* cxNew = newScript.GetContext(); | JSContext* cxNew = newScript.GetContext(); | ||||
JSAutoRequest rqNew(cxNew); | JSAutoRequest rqNew(cxNew); | ||||
JS::RootedValue tmpCommand(cxNew, newScript.CloneValueFromOtherContext(oldScript, command.data)); | JS::RootedValue tmpCommand(cxNew, newScript.CloneValueFromOtherContext(oldScript, command.data)); | ||||
newScript.FreezeObject(tmpCommand, true); | newScript.FreezeObject(tmpCommand, true); | ||||
SimulationCommand cmd(command.player, cxNew, tmpCommand); | SimulationCommand cmd(command.player, cxNew, tmpCommand); | ||||
newCommands.emplace_back(std::move(cmd)); | newCommands.emplace_back(std::move(cmd)); | ||||
} | } | ||||
return newCommands; | return newCommands; | ||||
} | } | ||||
}; | }; | ||||
JS::Value CSimulation2Impl::GetMapSettings() | |||||
{ | |||||
// Please consider that this function returns a reference, not a copy! | |||||
Stan: Isn't that already implied by the comment you put in the header file ? | |||||
ScriptInterface& scriptInterface = m_ComponentManager.GetScriptInterface(); | |||||
JSContext* cx = scriptInterface.GetContext(); | |||||
JSAutoRequest rq(cx); | |||||
JS::RootedValue settingsVal(cx); | |||||
scriptInterface.GetProperty(m_InitAttributes, "settings", &settingsVal); | |||||
return settingsVal; | |||||
} | |||||
bool CSimulation2Impl::LoadDefaultScripts(CComponentManager& componentManager, std::set<VfsPath>* loadedScripts) | bool CSimulation2Impl::LoadDefaultScripts(CComponentManager& componentManager, std::set<VfsPath>* loadedScripts) | ||||
{ | { | ||||
return ( | return ( | ||||
LoadScripts(componentManager, loadedScripts, L"simulation/components/interfaces/") && | LoadScripts(componentManager, loadedScripts, L"simulation/components/interfaces/") && | ||||
LoadScripts(componentManager, loadedScripts, L"simulation/helpers/") && | LoadScripts(componentManager, loadedScripts, L"simulation/helpers/") && | ||||
LoadScripts(componentManager, loadedScripts, L"simulation/components/") | LoadScripts(componentManager, loadedScripts, L"simulation/components/") | ||||
); | ); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | void CSimulation2Impl::ReportSerializationFailure( | ||||
if (secondaryStateBefore) | if (secondaryStateBefore) | ||||
DumpSerializationTestState(*secondaryStateBefore, path, L"before.b"); | DumpSerializationTestState(*secondaryStateBefore, path, L"before.b"); | ||||
if (secondaryStateAfter) | if (secondaryStateAfter) | ||||
DumpSerializationTestState(*secondaryStateAfter, path, L"after.b"); | DumpSerializationTestState(*secondaryStateAfter, path, L"after.b"); | ||||
debug_warn(L"Serialization test failure"); | debug_warn(L"Serialization test failure"); | ||||
} | } | ||||
void CSimulation2Impl::InitRNGSeedSimulation() | void CSimulation2Impl::InitRNGSeedSimulation(JS::HandleValue settings) | ||||
{ | { | ||||
const ScriptInterface& scriptInterface = m_ComponentManager.GetScriptInterface(); | |||||
u32 seed = 0; | u32 seed = 0; | ||||
if (!m_ComponentManager.GetScriptInterface().HasProperty(m_MapSettings, "Seed") || | if (!scriptInterface.HasProperty(settings, "Seed") || | ||||
StanUnsubmitted Not Done Inline Actionsa && b instead of !a || !b ? Stan: a && b instead of !a || !b ? | |||||
KrinkleUnsubmitted Not Done Inline Actions!a || !b !== ( a && b ). !a || !b === !( a && b ). If there was an if and an else, I think a && b would be more readable, and then you'd swap the if/else body. But in this case, there is no else. The line after it is unconditional. Krinkle: `!a || !b` !== `( a && b )`.
`!a || !b` === `!( a && b )`.
If there was an if and an else, I… | |||||
StanUnsubmitted Not Done Inline ActionsMakes sense :) Stan: Makes sense :) | |||||
!m_ComponentManager.GetScriptInterface().GetProperty(m_MapSettings, "Seed", seed)) | !scriptInterface.GetProperty(settings, "Seed", seed)) | ||||
LOGWARNING("CSimulation2Impl::InitRNGSeedSimulation: No seed value specified - using %d", seed); | LOGWARNING("CSimulation2Impl::InitRNGSeedSimulation: No seed value specified - using %d", seed); | ||||
m_ComponentManager.SetRNGSeed(seed); | m_ComponentManager.SetRNGSeed(seed); | ||||
} | } | ||||
void CSimulation2Impl::InitRNGSeedAI() | void CSimulation2Impl::InitRNGSeedAI(JS::HandleValue settings) | ||||
{ | { | ||||
const ScriptInterface& scriptInterface = m_ComponentManager.GetScriptInterface(); | |||||
u32 seed = 0; | u32 seed = 0; | ||||
if (!m_ComponentManager.GetScriptInterface().HasProperty(m_MapSettings, "AISeed") || | if (!scriptInterface.HasProperty(settings, "AISeed") || | ||||
StanUnsubmitted Not Done Inline Actionsa && b instead of !a || !b ? Stan: a && b instead of !a || !b ? | |||||
!m_ComponentManager.GetScriptInterface().GetProperty(m_MapSettings, "AISeed", seed)) | !scriptInterface.GetProperty(settings, "AISeed", seed)) | ||||
LOGWARNING("CSimulation2Impl::InitRNGSeedAI: No seed value specified - using %d", seed); | LOGWARNING("CSimulation2Impl::InitRNGSeedAI: No seed value specified - using %d", seed); | ||||
CmpPtr<ICmpAIManager> cmpAIManager(m_SimContext, SYSTEM_ENTITY); | CmpPtr<ICmpAIManager> cmpAIManager(m_SimContext, SYSTEM_ENTITY); | ||||
if (cmpAIManager) | if (cmpAIManager) | ||||
cmpAIManager->SetRNGSeed(seed); | cmpAIManager->SetRNGSeed(seed); | ||||
} | } | ||||
void CSimulation2Impl::Update(int turnLength, const std::vector<SimulationCommand>& commands) | void CSimulation2Impl::Update(int turnLength, const std::vector<SimulationCommand>& commands) | ||||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | if (m_EnableSerializationTest || startRejoinTest) | ||||
m_SecondaryLoadedScripts = new std::set<VfsPath>(); | m_SecondaryLoadedScripts = new std::set<VfsPath>(); | ||||
ENSURE(LoadDefaultScripts(*m_SecondaryComponentManager, m_SecondaryLoadedScripts)); | ENSURE(LoadDefaultScripts(*m_SecondaryComponentManager, m_SecondaryLoadedScripts)); | ||||
ResetComponentState(*m_SecondaryComponentManager, false, false); | ResetComponentState(*m_SecondaryComponentManager, false, false); | ||||
// Load the trigger scripts after we have loaded the simulation. | // Load the trigger scripts after we have loaded the simulation. | ||||
{ | { | ||||
JSContext* cx2 = m_SecondaryComponentManager->GetScriptInterface().GetContext(); | JSContext* cx2 = m_SecondaryComponentManager->GetScriptInterface().GetContext(); | ||||
JSAutoRequest rq2(cx2); | JSAutoRequest rq2(cx2); | ||||
JS::RootedValue mapSettings(cx2, GetMapSettings()); | |||||
JS::RootedValue mapSettingsCloned(cx2, | JS::RootedValue mapSettingsCloned(cx2, | ||||
m_SecondaryComponentManager->GetScriptInterface().CloneValueFromOtherContext( | m_SecondaryComponentManager->GetScriptInterface().CloneValueFromOtherContext(scriptInterface, mapSettings)); | ||||
scriptInterface, m_MapSettings)); | |||||
ENSURE(LoadTriggerScripts(*m_SecondaryComponentManager, mapSettingsCloned, m_SecondaryLoadedScripts)); | ENSURE(LoadTriggerScripts(*m_SecondaryComponentManager, mapSettingsCloned, m_SecondaryLoadedScripts)); | ||||
} | } | ||||
// Load the map into the secondary simulation | // Load the map into the secondary simulation | ||||
LDR_BeginRegistering(); | LDR_BeginRegistering(); | ||||
std::unique_ptr<CMapReader> mapReader(new CMapReader); | std::unique_ptr<CMapReader> mapReader(new CMapReader); | ||||
▲ Show 20 Lines • Show All 299 Lines • ▼ Show 20 Lines | |||||
void CSimulation2::InitGame() | void CSimulation2::InitGame() | ||||
{ | { | ||||
JSContext* cx = GetScriptInterface().GetContext(); | JSContext* cx = GetScriptInterface().GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedValue global(cx, GetScriptInterface().GetGlobalObject()); | JS::RootedValue global(cx, GetScriptInterface().GetGlobalObject()); | ||||
JS::RootedValue settings(cx); | JS::RootedValue settings(cx); | ||||
JS::RootedValue tmpInitAttributes(cx, GetInitAttributes()); | GetScriptInterface().GetProperty(m->m_InitAttributes, "settings", &settings); | ||||
GetScriptInterface().GetProperty(tmpInitAttributes, "settings", &settings); | |||||
GetScriptInterface().CallFunctionVoid(global, "InitGame", settings); | GetScriptInterface().CallFunctionVoid(global, "InitGame", settings); | ||||
} | } | ||||
void CSimulation2::Update(int turnLength) | void CSimulation2::Update(int turnLength) | ||||
{ | { | ||||
std::vector<SimulationCommand> commands; | std::vector<SimulationCommand> commands; | ||||
m->Update(turnLength, commands); | m->Update(turnLength, commands); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | void CSimulation2::SetInitAttributes(JS::HandleValue attribs) | ||||
m->m_InitAttributes = attribs; | m->m_InitAttributes = attribs; | ||||
} | } | ||||
JS::Value CSimulation2::GetInitAttributes() | JS::Value CSimulation2::GetInitAttributes() | ||||
{ | { | ||||
return m->m_InitAttributes.get(); | return m->m_InitAttributes.get(); | ||||
} | } | ||||
void CSimulation2::GetInitAttributes(JS::MutableHandleValue ret) | |||||
{ | |||||
ret.set(m->m_InitAttributes); | |||||
} | |||||
void CSimulation2::SetMapSettings(const std::string& settings) | void CSimulation2::SetMapSettings(const std::string& settings) | ||||
{ | { | ||||
m->m_ComponentManager.GetScriptInterface().ParseJSON(settings, &m->m_MapSettings); | const ScriptInterface& scriptInterface = GetScriptInterface(); | ||||
JSContext* cx = scriptInterface.GetContext(); | |||||
JSAutoRequest rq(cx); | |||||
JS::RootedValue settingsVal(cx); | |||||
scriptInterface.ParseJSON(settings, &settingsVal); | |||||
scriptInterface.SetProperty(m->m_InitAttributes, "settings", settingsVal); | |||||
} | } | ||||
void CSimulation2::SetMapSettings(JS::HandleValue settings) | void CSimulation2::SetMapSettings(JS::HandleValue settings) | ||||
{ | { | ||||
m->m_MapSettings = settings; | GetScriptInterface().SetProperty(m->m_InitAttributes, "settings", settings); | ||||
m->InitRNGSeedSimulation(settings); | |||||
m->InitRNGSeedSimulation(); | m->InitRNGSeedAI(settings); | ||||
m->InitRNGSeedAI(); | |||||
} | } | ||||
std::string CSimulation2::GetMapSettingsString() | std::string CSimulation2::GetMapSettingsString() | ||||
{ | { | ||||
return m->m_ComponentManager.GetScriptInterface().StringifyJSON(&m->m_MapSettings); | JSContext* cx = GetScriptInterface().GetContext(); | ||||
} | JSAutoRequest rq(cx); | ||||
JS::RootedValue settingsVal(cx, m->GetMapSettings()); | |||||
void CSimulation2::GetMapSettings(JS::MutableHandleValue ret) | return GetScriptInterface().StringifyJSON(&settingsVal); | ||||
{ | |||||
ret.set(m->m_MapSettings); | |||||
} | } | ||||
void CSimulation2::LoadPlayerSettings(bool newPlayers) | void CSimulation2::LoadPlayerSettings(bool newPlayers) | ||||
{ | { | ||||
JSContext* cx = GetScriptInterface().GetContext(); | const ScriptInterface& scriptInterface = GetScriptInterface(); | ||||
JSContext* cx = scriptInterface.GetContext(); | |||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedValue global(cx, GetScriptInterface().GetGlobalObject()); | JS::RootedValue global(cx, scriptInterface.GetGlobalObject()); | ||||
GetScriptInterface().CallFunctionVoid(global, "LoadPlayerSettings", m->m_MapSettings, newPlayers); | JS::RootedValue mapSettings(cx, m->GetMapSettings()); | ||||
scriptInterface.CallFunctionVoid(global, "LoadPlayerSettings", mapSettings, newPlayers); | |||||
} | } | ||||
void CSimulation2::LoadMapSettings() | void CSimulation2::LoadMapSettings() | ||||
{ | { | ||||
JSContext* cx = GetScriptInterface().GetContext(); | JSContext* cx = GetScriptInterface().GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedValue global(cx, GetScriptInterface().GetGlobalObject()); | JS::RootedValue global(cx, GetScriptInterface().GetGlobalObject()); | ||||
// Initialize here instead of in Update() | // Initialize here instead of in Update() | ||||
GetScriptInterface().CallFunctionVoid(global, "LoadMapSettings", m->m_MapSettings); | JS::RootedValue mapSettings(cx, m->GetMapSettings()); | ||||
GetScriptInterface().CallFunctionVoid(global, "LoadMapSettings", mapSettings); | |||||
if (!m->m_StartupScript.empty()) | if (!m->m_StartupScript.empty()) | ||||
GetScriptInterface().LoadScript(L"map startup script", m->m_StartupScript); | GetScriptInterface().LoadScript(L"map startup script", m->m_StartupScript); | ||||
// Load the trigger scripts after we have loaded the simulation and the map. | // Load the trigger scripts after we have loaded the simulation and the map. | ||||
m->LoadTriggerScripts(m->m_ComponentManager, m->m_MapSettings, &m->m_LoadedScripts); | m->LoadTriggerScripts(m->m_ComponentManager, mapSettings, &m->m_LoadedScripts); | ||||
} | } | ||||
int CSimulation2::ProgressiveLoad() | int CSimulation2::ProgressiveLoad() | ||||
{ | { | ||||
return m->ProgressiveLoad(); | return m->ProgressiveLoad(); | ||||
} | } | ||||
Status CSimulation2::ReloadChangedFile(const VfsPath& path) | Status CSimulation2::ReloadChangedFile(const VfsPath& path) | ||||
▲ Show 20 Lines • Show All 120 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
Isn't that already implied by the comment you put in the header file ?