2011-01-12 13:29:00 +01:00
|
|
|
/* Copyright (C) 2011 Wildfire Games.
|
2010-01-09 20:20:14 +01:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "Simulation2.h"
|
|
|
|
|
|
|
|
#include "simulation2/MessageTypes.h"
|
|
|
|
#include "simulation2/system/ComponentManager.h"
|
|
|
|
#include "simulation2/system/ParamNode.h"
|
|
|
|
#include "simulation2/system/SimContext.h"
|
2011-01-12 13:29:00 +01:00
|
|
|
#include "simulation2/components/ICmpAIManager.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
#include "simulation2/components/ICmpCommandQueue.h"
|
2011-01-12 13:29:00 +01:00
|
|
|
#include "simulation2/components/ICmpTemplateManager.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2011-03-05 02:56:59 +01:00
|
|
|
#include "lib/timer.h"
|
|
|
|
#include "lib/file/file_system_util.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
#include "maths/MathUtil.h"
|
|
|
|
#include "ps/CLogger.h"
|
|
|
|
#include "ps/Filesystem.h"
|
2010-05-20 02:59:01 +02:00
|
|
|
#include "ps/Profile.h"
|
|
|
|
#include "ps/Pyrogenesis.h"
|
2010-05-28 01:31:03 +02:00
|
|
|
#include "ps/XML/Xeromyces.h"
|
2010-05-20 02:59:01 +02:00
|
|
|
|
|
|
|
#include <iomanip>
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-05-20 22:05:38 +02:00
|
|
|
#if MSC_VERSION
|
|
|
|
#include <process.h>
|
|
|
|
#define getpid _getpid // use the non-deprecated function name
|
|
|
|
#endif
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
class CSimulation2Impl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CSimulation2Impl(CUnitManager* unitManager, CTerrain* terrain) :
|
2010-05-20 02:59:01 +02:00
|
|
|
m_SimContext(), m_ComponentManager(m_SimContext), m_EnableOOSLog(false)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
|
|
|
m_SimContext.m_UnitManager = unitManager;
|
|
|
|
m_SimContext.m_Terrain = terrain;
|
|
|
|
m_ComponentManager.LoadComponentTypes();
|
|
|
|
|
2010-06-03 03:29:43 +02:00
|
|
|
RegisterFileReloadFunc(ReloadChangedFileCB, this);
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
// m_EnableOOSLog = true; // TODO: this should be a command-line flag or similar
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2010-06-03 03:29:43 +02:00
|
|
|
~CSimulation2Impl()
|
|
|
|
{
|
|
|
|
UnregisterFileReloadFunc(ReloadChangedFileCB, this);
|
|
|
|
}
|
|
|
|
|
2011-01-12 13:29:00 +01:00
|
|
|
void ResetState(bool skipScriptedComponents, bool skipAI)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-01-14 21:36:29 +01:00
|
|
|
m_ComponentManager.ResetState();
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
m_DeltaTime = 0.0;
|
2010-09-27 01:05:25 +02:00
|
|
|
m_LastFrameOffset = 0.0f;
|
2010-04-30 01:22:18 +02:00
|
|
|
m_TurnNumber = 0;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
CParamNode noParam;
|
|
|
|
CComponentManager::ComponentTypeId cid;
|
|
|
|
|
|
|
|
// Add native system components:
|
|
|
|
m_ComponentManager.AddComponent(SYSTEM_ENTITY, CID_TemplateManager, noParam);
|
2010-03-07 22:38:39 +01:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
m_ComponentManager.AddComponent(SYSTEM_ENTITY, CID_CommandQueue, noParam);
|
2010-03-18 00:01:12 +01:00
|
|
|
m_ComponentManager.AddComponent(SYSTEM_ENTITY, CID_ObstructionManager, noParam);
|
2010-10-23 21:59:40 +02:00
|
|
|
m_ComponentManager.AddComponent(SYSTEM_ENTITY, CID_Pathfinder, noParam);
|
2010-03-07 22:38:39 +01:00
|
|
|
m_ComponentManager.AddComponent(SYSTEM_ENTITY, CID_ProjectileManager, noParam);
|
2010-07-29 22:39:23 +02:00
|
|
|
m_ComponentManager.AddComponent(SYSTEM_ENTITY, CID_RangeManager, noParam);
|
2010-04-04 23:24:39 +02:00
|
|
|
m_ComponentManager.AddComponent(SYSTEM_ENTITY, CID_SoundManager, noParam);
|
2010-03-07 22:38:39 +01:00
|
|
|
m_ComponentManager.AddComponent(SYSTEM_ENTITY, CID_Terrain, noParam);
|
2010-05-28 01:23:53 +02:00
|
|
|
m_ComponentManager.AddComponent(SYSTEM_ENTITY, CID_WaterManager, noParam);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2011-01-12 13:29:00 +01:00
|
|
|
if (!skipAI)
|
|
|
|
{
|
|
|
|
m_ComponentManager.AddComponent(SYSTEM_ENTITY, CID_AIManager, noParam);
|
|
|
|
}
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
// Add scripted system components:
|
2010-01-22 21:03:14 +01:00
|
|
|
if (!skipScriptedComponents)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-01-22 21:03:14 +01:00
|
|
|
#define LOAD_SCRIPTED_COMPONENT(name) \
|
|
|
|
cid = m_ComponentManager.LookupCID(name); \
|
|
|
|
if (cid == CID__Invalid) \
|
2010-01-22 21:17:49 +01:00
|
|
|
LOGERROR(L"Can't find component type " L##name); \
|
2010-01-22 21:03:14 +01:00
|
|
|
m_ComponentManager.AddComponent(SYSTEM_ENTITY, cid, noParam)
|
|
|
|
|
2011-01-12 13:29:00 +01:00
|
|
|
LOAD_SCRIPTED_COMPONENT("AIInterface");
|
|
|
|
LOAD_SCRIPTED_COMPONENT("EndGameManager");
|
2010-01-22 21:03:14 +01:00
|
|
|
LOAD_SCRIPTED_COMPONENT("GuiInterface");
|
|
|
|
LOAD_SCRIPTED_COMPONENT("PlayerManager");
|
2010-02-05 23:00:39 +01:00
|
|
|
LOAD_SCRIPTED_COMPONENT("Timer");
|
2010-01-22 21:03:14 +01:00
|
|
|
|
|
|
|
#undef LOAD_SCRIPTED_COMPONENT
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LoadScripts(const VfsPath& path);
|
2011-05-03 14:38:42 +02:00
|
|
|
Status ReloadChangedFile(const VfsPath& path);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
static Status ReloadChangedFileCB(void* param, const VfsPath& path)
|
2010-06-03 03:29:43 +02:00
|
|
|
{
|
|
|
|
return static_cast<CSimulation2Impl*>(param)->ReloadChangedFile(path);
|
|
|
|
}
|
|
|
|
|
2011-03-05 02:56:59 +01:00
|
|
|
int ProgressiveLoad();
|
2010-05-20 02:59:01 +02:00
|
|
|
bool Update(int turnLength, const std::vector<SimulationCommand>& commands);
|
|
|
|
void Interpolate(float frameLength, float frameOffset);
|
|
|
|
|
|
|
|
void DumpState();
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
CSimContext m_SimContext;
|
|
|
|
CComponentManager m_ComponentManager;
|
|
|
|
double m_DeltaTime;
|
2010-09-27 01:05:25 +02:00
|
|
|
float m_LastFrameOffset;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-04-30 01:22:18 +02:00
|
|
|
std::wstring m_StartupScript;
|
2010-08-04 23:15:41 +02:00
|
|
|
CScriptValRooted m_MapSettings;
|
2010-04-30 01:22:18 +02:00
|
|
|
|
2011-03-21 18:53:13 +01:00
|
|
|
std::set<VfsPath> m_LoadedScripts;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-04-30 01:22:18 +02:00
|
|
|
uint32_t m_TurnNumber;
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
bool m_EnableOOSLog;
|
2010-01-09 20:20:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
bool CSimulation2Impl::LoadScripts(const VfsPath& path)
|
|
|
|
{
|
|
|
|
VfsPaths pathnames;
|
|
|
|
if (fs_util::GetPathnames(g_VFS, path, L"*.js", pathnames) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
for (VfsPaths::iterator it = pathnames.begin(); it != pathnames.end(); ++it)
|
|
|
|
{
|
2011-03-21 18:53:13 +01:00
|
|
|
VfsPath filename = *it;
|
2010-01-09 20:20:14 +01:00
|
|
|
m_LoadedScripts.insert(filename);
|
2011-03-23 14:36:20 +01:00
|
|
|
LOGMESSAGE(L"Loading simulation script '%ls'", filename.string().c_str());
|
2010-01-09 20:20:14 +01:00
|
|
|
if (! m_ComponentManager.LoadScript(filename))
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
Status CSimulation2Impl::ReloadChangedFile(const VfsPath& path)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2011-03-21 18:53:13 +01:00
|
|
|
const VfsPath& filename = path;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
// Ignore if this file wasn't loaded as a script
|
|
|
|
// (TODO: Maybe we ought to load in any new .js files that are created in the right directories)
|
|
|
|
if (m_LoadedScripts.find(filename) == m_LoadedScripts.end())
|
|
|
|
return INFO::OK;
|
|
|
|
|
|
|
|
// If the file doesn't exist (e.g. it was deleted), don't bother loading it since that'll give an error message.
|
|
|
|
// (Also don't bother trying to 'unload' it from the component manager, because that's not possible)
|
2011-02-25 17:30:55 +01:00
|
|
|
if (!VfsFileExists(path))
|
2010-01-09 20:20:14 +01:00
|
|
|
return INFO::OK;
|
|
|
|
|
2011-03-23 14:36:20 +01:00
|
|
|
LOGMESSAGE(L"Reloading simulation script '%ls'", filename.string().c_str());
|
2010-01-09 20:20:14 +01:00
|
|
|
if (!m_ComponentManager.LoadScript(filename, true))
|
|
|
|
return ERR::FAIL;
|
|
|
|
|
|
|
|
return INFO::OK;
|
|
|
|
}
|
|
|
|
|
2011-03-05 02:56:59 +01:00
|
|
|
int CSimulation2Impl::ProgressiveLoad()
|
|
|
|
{
|
|
|
|
// yield after this time is reached. balances increased progress bar
|
|
|
|
// smoothness vs. slowing down loading.
|
|
|
|
const double end_time = timer_Time() + 200e-3;
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
bool progressed = false;
|
|
|
|
int total = 0;
|
|
|
|
int progress = 0;
|
|
|
|
|
|
|
|
CMessageProgressiveLoad msg(&progressed, &total, &progress);
|
|
|
|
|
|
|
|
m_ComponentManager.BroadcastMessage(msg);
|
|
|
|
|
|
|
|
if (!progressed || total == 0)
|
|
|
|
return 0; // we have nothing left to load
|
|
|
|
|
|
|
|
ret = Clamp(100*progress / total, 1, 100);
|
|
|
|
}
|
|
|
|
while (timer_Time() < end_time);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
bool CSimulation2Impl::Update(int turnLength, const std::vector<SimulationCommand>& commands)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-05-20 02:59:01 +02:00
|
|
|
fixed turnLengthFixed = fixed::FromInt(turnLength) / 1000;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-09-03 11:55:14 +02:00
|
|
|
// TODO: the update process is pretty ugly, with lots of messages and dependencies
|
|
|
|
// between different components. Ought to work out a nicer way to do this.
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
CMessageTurnStart msgTurnStart;
|
|
|
|
m_ComponentManager.BroadcastMessage(msgTurnStart);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-09-03 11:55:14 +02:00
|
|
|
CmpPtr<ICmpPathfinder> cmpPathfinder(m_SimContext, SYSTEM_ENTITY);
|
|
|
|
if (!cmpPathfinder.null())
|
|
|
|
cmpPathfinder->FinishAsyncRequests();
|
|
|
|
|
2011-01-12 13:29:00 +01:00
|
|
|
// Push AI commands onto the queue before we use them
|
|
|
|
CmpPtr<ICmpAIManager> cmpAIManager(m_SimContext, SYSTEM_ENTITY);
|
|
|
|
if (!cmpAIManager.null())
|
|
|
|
cmpAIManager->PushCommands();
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
CmpPtr<ICmpCommandQueue> cmpCommandQueue(m_SimContext, SYSTEM_ENTITY);
|
|
|
|
if (!cmpCommandQueue.null())
|
|
|
|
cmpCommandQueue->FlushTurn(commands);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-09-03 11:55:14 +02:00
|
|
|
// Send all the update phases
|
|
|
|
{
|
|
|
|
CMessageUpdate msgUpdate(turnLengthFixed);
|
|
|
|
m_ComponentManager.BroadcastMessage(msgUpdate);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
CMessageUpdate_MotionFormation msgUpdate(turnLengthFixed);
|
|
|
|
m_ComponentManager.BroadcastMessage(msgUpdate);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
CMessageUpdate_MotionUnit msgUpdate(turnLengthFixed);
|
|
|
|
m_ComponentManager.BroadcastMessage(msgUpdate);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
CMessageUpdate_Final msgUpdate(turnLengthFixed);
|
|
|
|
m_ComponentManager.BroadcastMessage(msgUpdate);
|
|
|
|
}
|
2010-01-22 21:03:14 +01:00
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
// Clean up any entities destroyed during the simulation update
|
|
|
|
m_ComponentManager.FlushDestroyedComponents();
|
2010-04-30 01:22:18 +02:00
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
// if (m_TurnNumber == 0)
|
|
|
|
// m_ComponentManager.GetScriptInterface().DumpHeap();
|
2010-05-07 02:24:22 +02:00
|
|
|
|
2010-11-15 16:03:40 +01:00
|
|
|
// Run the GC occasionally
|
|
|
|
// (TODO: we ought to schedule this for a frame where we're not
|
|
|
|
// running the sim update, to spread the load)
|
|
|
|
if (m_TurnNumber % 10 == 0)
|
|
|
|
m_ComponentManager.GetScriptInterface().MaybeGC();
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
if (m_EnableOOSLog)
|
|
|
|
DumpState();
|
2010-05-01 18:20:58 +02:00
|
|
|
|
2011-01-12 13:29:00 +01:00
|
|
|
// Start computing AI for the next turn
|
|
|
|
if (!cmpAIManager.null())
|
|
|
|
cmpAIManager->StartComputation();
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
++m_TurnNumber;
|
|
|
|
|
|
|
|
return true; // TODO: don't bother with bool return
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
void CSimulation2Impl::Interpolate(float frameLength, float frameOffset)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-09-27 01:05:25 +02:00
|
|
|
m_LastFrameOffset = frameOffset;
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
CMessageInterpolate msg(frameLength, frameOffset);
|
2010-03-20 17:26:25 +01:00
|
|
|
m_ComponentManager.BroadcastMessage(msg);
|
2010-12-08 17:09:53 +01:00
|
|
|
|
|
|
|
// Clean up any entities destroyed during interpolate (e.g. local corpses)
|
|
|
|
m_ComponentManager.FlushDestroyedComponents();
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
void CSimulation2Impl::DumpState()
|
|
|
|
{
|
|
|
|
PROFILE("DumpState");
|
|
|
|
|
|
|
|
std::wstringstream name;
|
|
|
|
name << L"sim_log/" << getpid() << L"/" << std::setw(5) << std::setfill(L'0') << m_TurnNumber << L".txt";
|
2011-03-23 14:36:20 +01:00
|
|
|
OsPath path = psLogDir() / name.str();
|
|
|
|
CreateDirectories(path.Parent(), 0700);
|
|
|
|
std::ofstream file (OsString(path).c_str(), std::ofstream::out | std::ofstream::trunc);
|
2010-05-20 02:59:01 +02:00
|
|
|
|
|
|
|
file << "State hash: " << std::hex;
|
|
|
|
std::string hashRaw;
|
2011-03-05 23:30:32 +01:00
|
|
|
m_ComponentManager.ComputeStateHash(hashRaw, false);
|
2010-05-20 02:59:01 +02:00
|
|
|
for (size_t i = 0; i < hashRaw.size(); ++i)
|
|
|
|
file << std::setfill('0') << std::setw(2) << (int)(unsigned char)hashRaw[i];
|
|
|
|
file << std::dec << "\n";
|
|
|
|
|
|
|
|
file << "\n";
|
|
|
|
|
2010-05-23 01:02:07 +02:00
|
|
|
m_ComponentManager.DumpDebugState(file);
|
|
|
|
|
2011-03-23 14:36:20 +01:00
|
|
|
std::ofstream binfile (OsString(path.ChangeExtension(L".dat")).c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
|
2010-05-23 01:02:07 +02:00
|
|
|
m_ComponentManager.SerializeState(binfile);
|
2010-05-20 02:59:01 +02:00
|
|
|
}
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CSimulation2::CSimulation2(CUnitManager* unitManager, CTerrain* terrain) :
|
|
|
|
m(new CSimulation2Impl(unitManager, terrain))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CSimulation2::~CSimulation2()
|
|
|
|
{
|
|
|
|
delete m;
|
|
|
|
}
|
|
|
|
|
2010-01-22 21:03:14 +01:00
|
|
|
// Forward all method calls to the appropriate CSimulation2Impl/CComponentManager methods:
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
void CSimulation2::EnableOOSLog()
|
|
|
|
{
|
|
|
|
m->m_EnableOOSLog = true;
|
|
|
|
}
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
entity_id_t CSimulation2::AddEntity(const std::wstring& templateName)
|
|
|
|
{
|
2010-01-22 21:03:14 +01:00
|
|
|
return m->m_ComponentManager.AddEntity(templateName, m->m_ComponentManager.AllocateNewEntity());
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
entity_id_t CSimulation2::AddEntity(const std::wstring& templateName, entity_id_t preferredId)
|
|
|
|
{
|
2010-01-22 21:03:14 +01:00
|
|
|
return m->m_ComponentManager.AddEntity(templateName, m->m_ComponentManager.AllocateNewEntity(preferredId));
|
2010-01-14 21:36:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
entity_id_t CSimulation2::AddLocalEntity(const std::wstring& templateName)
|
|
|
|
{
|
2010-01-22 21:03:14 +01:00
|
|
|
return m->m_ComponentManager.AddEntity(templateName, m->m_ComponentManager.AllocateNewLocalEntity());
|
2010-01-14 21:36:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSimulation2::DestroyEntity(entity_id_t ent)
|
|
|
|
{
|
|
|
|
m->m_ComponentManager.DestroyComponentsSoon(ent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSimulation2::FlushDestroyedEntities()
|
|
|
|
{
|
|
|
|
m->m_ComponentManager.FlushDestroyedComponents();
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
IComponent* CSimulation2::QueryInterface(entity_id_t ent, int iid) const
|
|
|
|
{
|
|
|
|
return m->m_ComponentManager.QueryInterface(ent, iid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSimulation2::PostMessage(entity_id_t ent, const CMessage& msg) const
|
|
|
|
{
|
|
|
|
m->m_ComponentManager.PostMessage(ent, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSimulation2::BroadcastMessage(const CMessage& msg) const
|
|
|
|
{
|
|
|
|
m->m_ComponentManager.BroadcastMessage(msg);
|
|
|
|
}
|
|
|
|
|
2011-03-03 01:16:14 +01:00
|
|
|
CSimulation2::InterfaceList CSimulation2::GetEntitiesWithInterface(int iid)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
|
|
|
return m->m_ComponentManager.GetEntitiesWithInterface(iid);
|
|
|
|
}
|
|
|
|
|
2011-03-03 01:16:14 +01:00
|
|
|
const CSimulation2::InterfaceListUnordered& CSimulation2::GetEntitiesWithInterfaceUnordered(int iid)
|
|
|
|
{
|
|
|
|
return m->m_ComponentManager.GetEntitiesWithInterfaceUnordered(iid);
|
|
|
|
}
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
const CSimContext& CSimulation2::GetSimContext() const
|
|
|
|
{
|
|
|
|
return m->m_SimContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptInterface& CSimulation2::GetScriptInterface() const
|
|
|
|
{
|
|
|
|
return m->m_ComponentManager.GetScriptInterface();
|
|
|
|
}
|
|
|
|
|
2010-01-22 21:03:14 +01:00
|
|
|
void CSimulation2::InitGame(const CScriptVal& data)
|
|
|
|
{
|
|
|
|
CScriptVal ret; // ignored
|
|
|
|
GetScriptInterface().CallFunction(GetScriptInterface().GetGlobalObject(), "InitGame", data, ret);
|
|
|
|
}
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
bool CSimulation2::Update(int turnLength)
|
|
|
|
{
|
|
|
|
std::vector<SimulationCommand> commands;
|
|
|
|
return m->Update(turnLength, commands);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSimulation2::Update(int turnLength, const std::vector<SimulationCommand>& commands)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-05-20 02:59:01 +02:00
|
|
|
return m->Update(turnLength, commands);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
void CSimulation2::Interpolate(float frameLength, float frameOffset)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2010-05-20 02:59:01 +02:00
|
|
|
m->Interpolate(frameLength, frameOffset);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2010-03-20 17:26:25 +01:00
|
|
|
void CSimulation2::RenderSubmit(SceneCollector& collector, const CFrustum& frustum, bool culling)
|
|
|
|
{
|
|
|
|
CMessageRenderSubmit msg(collector, frustum, culling);
|
|
|
|
m->m_ComponentManager.BroadcastMessage(msg);
|
|
|
|
}
|
|
|
|
|
2010-09-27 01:05:25 +02:00
|
|
|
float CSimulation2::GetLastFrameOffset() const
|
|
|
|
{
|
|
|
|
return m->m_LastFrameOffset;
|
|
|
|
}
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
bool CSimulation2::LoadScripts(const VfsPath& path)
|
|
|
|
{
|
|
|
|
return m->LoadScripts(path);
|
|
|
|
}
|
|
|
|
|
2010-04-03 13:07:42 +02:00
|
|
|
bool CSimulation2::LoadDefaultScripts()
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
m->LoadScripts(L"simulation/components/interfaces/") &&
|
|
|
|
m->LoadScripts(L"simulation/helpers/") &&
|
|
|
|
m->LoadScripts(L"simulation/components/")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-04-30 01:22:18 +02:00
|
|
|
void CSimulation2::SetStartupScript(const std::wstring& code)
|
|
|
|
{
|
|
|
|
m->m_StartupScript = code;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::wstring& CSimulation2::GetStartupScript()
|
|
|
|
{
|
|
|
|
return m->m_StartupScript;
|
|
|
|
}
|
|
|
|
|
2011-02-17 21:08:20 +01:00
|
|
|
void CSimulation2::SetMapSettings(const std::string& settings)
|
2010-08-04 23:15:41 +02:00
|
|
|
{
|
|
|
|
m->m_MapSettings = m->m_ComponentManager.GetScriptInterface().ParseJSON(settings);
|
|
|
|
}
|
|
|
|
|
2010-10-30 06:02:42 +02:00
|
|
|
void CSimulation2::SetMapSettings(const CScriptValRooted& settings)
|
|
|
|
{
|
|
|
|
m->m_MapSettings = settings;
|
|
|
|
}
|
|
|
|
|
2011-02-19 04:14:37 +01:00
|
|
|
std::string CSimulation2::GetMapSettingsString()
|
2010-08-04 23:15:41 +02:00
|
|
|
{
|
|
|
|
return m->m_ComponentManager.GetScriptInterface().StringifyJSON(m->m_MapSettings.get());
|
|
|
|
}
|
|
|
|
|
2011-02-19 04:14:37 +01:00
|
|
|
CScriptVal CSimulation2::GetMapSettings()
|
|
|
|
{
|
|
|
|
return m->m_MapSettings.get();
|
|
|
|
}
|
|
|
|
|
2010-10-30 06:02:42 +02:00
|
|
|
void CSimulation2::LoadPlayerSettings()
|
|
|
|
{
|
|
|
|
GetScriptInterface().CallFunctionVoid(GetScriptInterface().GetGlobalObject(), "LoadPlayerSettings", m->m_MapSettings);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSimulation2::LoadMapSettings()
|
|
|
|
{
|
|
|
|
// Initialize here instead of in Update()
|
|
|
|
GetScriptInterface().CallFunctionVoid(GetScriptInterface().GetGlobalObject(), "LoadMapSettings", m->m_MapSettings);
|
|
|
|
|
|
|
|
if (!m->m_StartupScript.empty())
|
|
|
|
GetScriptInterface().LoadScript(L"map startup script", m->m_StartupScript);
|
|
|
|
}
|
|
|
|
|
2011-03-05 02:56:59 +01:00
|
|
|
int CSimulation2::ProgressiveLoad()
|
|
|
|
{
|
|
|
|
return m->ProgressiveLoad();
|
|
|
|
}
|
|
|
|
|
2011-05-03 14:38:42 +02:00
|
|
|
Status CSimulation2::ReloadChangedFile(const VfsPath& path)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
|
|
|
return m->ReloadChangedFile(path);
|
|
|
|
}
|
|
|
|
|
2011-01-12 13:29:00 +01:00
|
|
|
void CSimulation2::ResetState(bool skipScriptedComponents, bool skipAI)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2011-01-12 13:29:00 +01:00
|
|
|
m->ResetState(skipScriptedComponents, skipAI);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2011-03-05 23:30:32 +01:00
|
|
|
bool CSimulation2::ComputeStateHash(std::string& outHash, bool quick)
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2011-03-05 23:30:32 +01:00
|
|
|
return m->m_ComponentManager.ComputeStateHash(outHash, quick);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CSimulation2::DumpDebugState(std::ostream& stream)
|
|
|
|
{
|
|
|
|
return m->m_ComponentManager.DumpDebugState(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSimulation2::SerializeState(std::ostream& stream)
|
|
|
|
{
|
|
|
|
return m->m_ComponentManager.SerializeState(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSimulation2::DeserializeState(std::istream& stream)
|
|
|
|
{
|
|
|
|
// TODO: need to make sure the required SYSTEM_ENTITY components get constructed
|
|
|
|
return m->m_ComponentManager.DeserializeState(stream);
|
|
|
|
}
|
2010-04-09 21:02:39 +02:00
|
|
|
|
|
|
|
std::string CSimulation2::GenerateSchema()
|
|
|
|
{
|
|
|
|
return m->m_ComponentManager.GenerateSchema();
|
|
|
|
}
|
2011-04-14 06:01:59 +02:00
|
|
|
|
|
|
|
std::vector<std::string> CSimulation2::GetRMSData()
|
|
|
|
{
|
|
|
|
VfsPath path(L"maps/random/");
|
|
|
|
VfsPaths pathnames;
|
|
|
|
|
|
|
|
std::vector<std::string> data;
|
|
|
|
|
|
|
|
// Find all ../maps/random/*.json
|
2011-05-03 14:38:42 +02:00
|
|
|
Status ret = fs_util::GetPathnames(g_VFS, path, L"*.json", pathnames);
|
2011-04-14 06:01:59 +02:00
|
|
|
if (ret == INFO::OK)
|
|
|
|
{
|
|
|
|
for (VfsPaths::iterator it = pathnames.begin(); it != pathnames.end(); ++it)
|
|
|
|
{
|
|
|
|
// Load JSON file
|
|
|
|
CVFSFile file;
|
|
|
|
PSRETURN ret = file.Load(g_VFS, *it);
|
|
|
|
if (ret != PSRETURN_OK)
|
|
|
|
{
|
|
|
|
LOGERROR(L"Failed to load file '%ls': %hs", path.string().c_str(), GetErrorString(ret));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data.push_back(std::string(file.GetBuffer(), file.GetBuffer() + file.GetBufferSize()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Some error reading directory
|
|
|
|
wchar_t error[200];
|
2011-05-03 14:38:42 +02:00
|
|
|
LOGERROR(L"Error reading directory '%ls': %hs", path.string().c_str(), StatusDescription(ret, error, ARRAY_SIZE(error)));
|
2011-04-14 06:01:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> CSimulation2::GetCivData()
|
|
|
|
{
|
|
|
|
VfsPath path(L"civs/");
|
|
|
|
VfsPaths pathnames;
|
|
|
|
|
|
|
|
std::vector<std::string> data;
|
|
|
|
|
|
|
|
// Load all JSON files in civs directory
|
2011-05-03 14:38:42 +02:00
|
|
|
Status ret = fs_util::GetPathnames(g_VFS, path, L"*.json", pathnames);
|
2011-04-14 06:01:59 +02:00
|
|
|
if (ret == INFO::OK)
|
|
|
|
{
|
|
|
|
for (VfsPaths::iterator it = pathnames.begin(); it != pathnames.end(); ++it)
|
|
|
|
{
|
|
|
|
// Load JSON file
|
|
|
|
CVFSFile file;
|
|
|
|
PSRETURN ret = file.Load(g_VFS, *it);
|
|
|
|
if (ret != PSRETURN_OK)
|
|
|
|
{
|
2011-05-04 23:24:25 +02:00
|
|
|
LOGERROR(L"CSimulation2::GetCivData: Failed to load file '%ls': %hs", path.string().c_str(), GetErrorString(ret));
|
2011-04-14 06:01:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data.push_back(std::string(file.GetBuffer(), file.GetBuffer() + file.GetBufferSize()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Some error reading directory
|
|
|
|
wchar_t error[200];
|
2011-05-04 23:24:25 +02:00
|
|
|
LOGERROR(L"CSimulation2::GetCivData: Error reading directory '%ls': %ls", path.string().c_str(), StatusDescription(ret, error, ARRAY_SIZE(error)));
|
2011-04-14 06:01:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CSimulation2::GetPlayerDefaults()
|
|
|
|
{
|
|
|
|
VfsPath path = VfsPath(L"simulation/data/player_defaults.json");
|
|
|
|
|
|
|
|
std::string data;
|
|
|
|
|
|
|
|
if (!VfsFileExists(g_VFS, path))
|
|
|
|
{
|
|
|
|
LOGERROR(L"File '%ls' does not exist", path.string().c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Load JSON file
|
|
|
|
CVFSFile file;
|
|
|
|
PSRETURN ret = file.Load(g_VFS, path);
|
|
|
|
if (ret != PSRETURN_OK)
|
|
|
|
{
|
|
|
|
LOGERROR(L"Failed to load file '%ls': %hs", path.string().c_str(), GetErrorString(ret));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data = std::string(file.GetBuffer(), file.GetBuffer() + file.GetBufferSize());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|