Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/simulation2/Simulation2.cpp
Show All 13 Lines | |||||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "Simulation2.h" | #include "Simulation2.h" | ||||
#include "scriptinterface/ScriptContext.h" | |||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "scriptinterface/ScriptRuntime.h" | |||||
#include "simulation2/MessageTypes.h" | #include "simulation2/MessageTypes.h" | ||||
#include "simulation2/system/ComponentManager.h" | #include "simulation2/system/ComponentManager.h" | ||||
#include "simulation2/system/ParamNode.h" | #include "simulation2/system/ParamNode.h" | ||||
#include "simulation2/system/SimContext.h" | #include "simulation2/system/SimContext.h" | ||||
#include "simulation2/components/ICmpAIManager.h" | #include "simulation2/components/ICmpAIManager.h" | ||||
#include "simulation2/components/ICmpCommandQueue.h" | #include "simulation2/components/ICmpCommandQueue.h" | ||||
#include "simulation2/components/ICmpTemplateManager.h" | #include "simulation2/components/ICmpTemplateManager.h" | ||||
Show All 12 Lines | |||||
#include "ps/Util.h" | #include "ps/Util.h" | ||||
#include "ps/XML/Xeromyces.h" | #include "ps/XML/Xeromyces.h" | ||||
#include <iomanip> | #include <iomanip> | ||||
class CSimulation2Impl | class CSimulation2Impl | ||||
{ | { | ||||
public: | public: | ||||
CSimulation2Impl(CUnitManager* unitManager, shared_ptr<ScriptRuntime> rt, CTerrain* terrain) : | CSimulation2Impl(CUnitManager* unitManager, shared_ptr<ScriptContext> 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->GetJSRuntime()), m_InitAttributes(rt->GetJSRuntime()) | m_MapSettings(rt->GetJSRuntime()), m_InitAttributes(rt->GetJSRuntime()) | ||||
{ | { | ||||
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(); | ||||
▲ Show 20 Lines • Show All 345 Lines • ▼ Show 20 Lines | if (m_EnableSerializationTest || startRejoinTest) | ||||
delete m_SecondaryTerrain; | delete m_SecondaryTerrain; | ||||
m_SecondaryTerrain = new CTerrain(); | m_SecondaryTerrain = new CTerrain(); | ||||
delete m_SecondaryContext; | delete m_SecondaryContext; | ||||
m_SecondaryContext = new CSimContext(); | m_SecondaryContext = new CSimContext(); | ||||
m_SecondaryContext->m_Terrain = m_SecondaryTerrain; | m_SecondaryContext->m_Terrain = m_SecondaryTerrain; | ||||
delete m_SecondaryComponentManager; | delete m_SecondaryComponentManager; | ||||
m_SecondaryComponentManager = new CComponentManager(*m_SecondaryContext, scriptInterface.GetRuntime()); | m_SecondaryComponentManager = new CComponentManager(*m_SecondaryContext, scriptInterface.GetContext()); | ||||
m_SecondaryComponentManager->LoadComponentTypes(); | m_SecondaryComponentManager->LoadComponentTypes(); | ||||
delete m_SecondaryLoadedScripts; | delete m_SecondaryLoadedScripts; | ||||
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. | ||||
▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | void CSimulation2Impl::Update(int turnLength, const std::vector<SimulationCommand>& commands) | ||||
// Every 500 turns we run a shrinking GC, which decommits unused memory and frees all JIT code. | // Every 500 turns we run a shrinking GC, which decommits unused memory and frees all JIT code. | ||||
// Based on testing, this seems to be a good compromise between memory usage and performance. | // Based on testing, this seems to be a good compromise between memory usage and performance. | ||||
// Also check the comment about gcPreserveCode in the ScriptInterface code and this forum topic: | // Also check the comment about gcPreserveCode in the ScriptInterface code and this forum topic: | ||||
// http://www.wildfiregames.com/forum/index.php?showtopic=18466&p=300323 | // http://www.wildfiregames.com/forum/index.php?showtopic=18466&p=300323 | ||||
// | // | ||||
// (TODO: we ought to schedule this for a frame where we're not | // (TODO: we ought to schedule this for a frame where we're not | ||||
// running the sim update, to spread the load) | // running the sim update, to spread the load) | ||||
if (m_TurnNumber % 500 == 0) | if (m_TurnNumber % 500 == 0) | ||||
scriptInterface.GetRuntime()->ShrinkingGC(); | scriptInterface.GetContext()->ShrinkingGC(); | ||||
else | else | ||||
scriptInterface.GetRuntime()->MaybeIncrementalGC(0.0f); | scriptInterface.GetContext()->MaybeIncrementalGC(0.0f); | ||||
if (m_EnableOOSLog) | if (m_EnableOOSLog) | ||||
DumpState(); | DumpState(); | ||||
// Start computing AI for the next turn | // Start computing AI for the next turn | ||||
CmpPtr<ICmpAIManager> cmpAIManager(m_SimContext, SYSTEM_ENTITY); | CmpPtr<ICmpAIManager> cmpAIManager(m_SimContext, SYSTEM_ENTITY); | ||||
if (cmpAIManager) | if (cmpAIManager) | ||||
cmpAIManager->StartComputation(); | cmpAIManager->StartComputation(); | ||||
▲ Show 20 Lines • Show All 114 Lines • ▼ Show 20 Lines | void CSimulation2Impl::DumpState() | ||||
m_ComponentManager.DumpDebugState(file, true); | m_ComponentManager.DumpDebugState(file, true); | ||||
std::ofstream binfile (OsString(path.ChangeExtension(L".dat")).c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary); | std::ofstream binfile (OsString(path.ChangeExtension(L".dat")).c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary); | ||||
m_ComponentManager.SerializeState(binfile); | m_ComponentManager.SerializeState(binfile); | ||||
} | } | ||||
//////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////// | ||||
CSimulation2::CSimulation2(CUnitManager* unitManager, shared_ptr<ScriptRuntime> rt, CTerrain* terrain) : | CSimulation2::CSimulation2(CUnitManager* unitManager, shared_ptr<ScriptContext> rt, CTerrain* terrain) : | ||||
m(new CSimulation2Impl(unitManager, rt, terrain)) | m(new CSimulation2Impl(unitManager, rt, terrain)) | ||||
{ | { | ||||
} | } | ||||
CSimulation2::~CSimulation2() | CSimulation2::~CSimulation2() | ||||
{ | { | ||||
delete m; | delete m; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 346 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator