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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_COMPONENTMANAGER
|
|
|
|
#define INCLUDED_COMPONENTMANAGER
|
|
|
|
|
|
|
|
#include "Entity.h"
|
|
|
|
#include "Components.h"
|
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2011-01-12 13:29:00 +01:00
|
|
|
#include "simulation2/helpers/Player.h"
|
2012-08-16 00:25:25 +02:00
|
|
|
#include "ps/Filesystem.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-05-22 02:38:33 +02:00
|
|
|
#include <boost/random/linear_congruential.hpp>
|
2011-03-03 01:16:14 +01:00
|
|
|
#include <boost/unordered_map.hpp>
|
2010-05-22 02:38:33 +02:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
class IComponent;
|
|
|
|
class CParamNode;
|
|
|
|
class CMessage;
|
|
|
|
class CSimContext;
|
|
|
|
|
|
|
|
class CComponentManager
|
|
|
|
{
|
|
|
|
NONCOPYABLE(CComponentManager);
|
|
|
|
public:
|
|
|
|
// We can't use EInterfaceId/etc directly, since scripts dynamically generate new IDs
|
|
|
|
// and casting arbitrary ints to enums is undefined behaviour, so use 'int' typedefs
|
|
|
|
typedef int InterfaceId;
|
|
|
|
typedef int ComponentTypeId;
|
|
|
|
typedef int MessageTypeId;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Component allocation types
|
|
|
|
typedef IComponent* (*AllocFunc)(ScriptInterface& scriptInterface, jsval ctor);
|
|
|
|
typedef void (*DeallocFunc)(IComponent*);
|
|
|
|
|
|
|
|
// ComponentTypes come in three types:
|
|
|
|
// Native: normal C++ component
|
|
|
|
// ScriptWrapper: C++ component that wraps a JS component implementation
|
|
|
|
// Script: a ScriptWrapper linked to a specific JS component implementation
|
|
|
|
enum EComponentTypeType
|
|
|
|
{
|
|
|
|
CT_Native,
|
|
|
|
CT_ScriptWrapper,
|
|
|
|
CT_Script
|
|
|
|
};
|
|
|
|
|
|
|
|
// Representation of a component type, to be used when instantiating components
|
|
|
|
struct ComponentType
|
|
|
|
{
|
|
|
|
EComponentTypeType type;
|
|
|
|
InterfaceId iid;
|
|
|
|
AllocFunc alloc;
|
|
|
|
DeallocFunc dealloc;
|
|
|
|
std::string name;
|
2010-04-09 21:02:39 +02:00
|
|
|
std::string schema; // RelaxNG fragment
|
2010-11-17 00:00:52 +01:00
|
|
|
CScriptValRooted ctor; // only valid if type == CT_Script
|
2010-01-09 20:20:14 +01:00
|
|
|
};
|
2012-08-16 00:25:25 +02:00
|
|
|
|
|
|
|
struct FindJSONFilesCallbackData {
|
|
|
|
VfsPath path;
|
|
|
|
std::vector<std::string> templates;
|
|
|
|
};
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
public:
|
2010-04-14 19:22:32 +02:00
|
|
|
CComponentManager(CSimContext&, bool skipScriptFunctions = false);
|
2010-01-09 20:20:14 +01:00
|
|
|
~CComponentManager();
|
|
|
|
|
|
|
|
void LoadComponentTypes();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a script and execute it in a new function scope.
|
|
|
|
* @param filename VFS path to load
|
|
|
|
* @param hotload set to true if this script has been loaded before, and redefinitions of
|
|
|
|
* existing components should not be considered errors
|
|
|
|
*/
|
2011-03-21 18:53:13 +01:00
|
|
|
bool LoadScript(const VfsPath& filename, bool hotload = false);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
void RegisterMessageType(MessageTypeId mtid, const char* name);
|
|
|
|
|
2010-04-09 21:02:39 +02:00
|
|
|
void RegisterComponentType(InterfaceId, ComponentTypeId, AllocFunc, DeallocFunc, const char*, const std::string& schema);
|
|
|
|
void RegisterComponentTypeScriptWrapper(InterfaceId, ComponentTypeId, AllocFunc, DeallocFunc, const char*, const std::string& schema);
|
2010-04-15 21:59:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe the current component type to the given message type.
|
|
|
|
* Each component's HandleMessage will be called on any BroadcastMessage of this message type,
|
|
|
|
* or on any PostMessage of this type targeted at the component's entity.
|
|
|
|
* Must only be called by a component type's ClassInit.
|
|
|
|
*/
|
|
|
|
void SubscribeToMessageType(MessageTypeId mtid);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe the current component type to all messages of the given message type.
|
|
|
|
* Each component's HandleMessage will be called on any BroadcastMessage or PostMessage of this message type,
|
|
|
|
* regardless of the entity.
|
|
|
|
* Must only be called by a component type's ClassInit.
|
|
|
|
*/
|
|
|
|
void SubscribeGloballyToMessageType(MessageTypeId mtid);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param cname Requested component type name (not including any "CID" or "CCmp" prefix)
|
|
|
|
* @return The component type id, or CID__Invalid if not found
|
|
|
|
*/
|
|
|
|
ComponentTypeId LookupCID(const std::string& cname) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The name of the given component type, or "" if not found
|
|
|
|
*/
|
|
|
|
std::string LookupComponentTypeName(ComponentTypeId cid) const;
|
|
|
|
|
2013-09-11 22:41:53 +02:00
|
|
|
/**
|
|
|
|
* Set up an empty SYSTEM_ENTITY. Must be called after ResetState() and before GetSystemEntity().
|
|
|
|
*/
|
|
|
|
void InitSystemEntity();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a CEntityHandle with id SYSTEM_ENTITY.
|
|
|
|
*/
|
|
|
|
CEntityHandle GetSystemEntity() { ASSERT(m_SystemEntity.GetId() == SYSTEM_ENTITY); return m_SystemEntity; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a CEntityHandle with id @p ent.
|
|
|
|
* If @p allowCreate is true and there is no existing CEntityHandle, a new handle will be allocated.
|
|
|
|
*/
|
|
|
|
CEntityHandle LookupEntityHandle(entity_id_t ent, bool allowCreate = false);
|
|
|
|
|
2010-01-14 21:36:29 +01:00
|
|
|
/**
|
|
|
|
* Returns a new entity ID that has never been used before.
|
|
|
|
* This affects the simulation state so it must only be called in network-synchronised ways.
|
|
|
|
*/
|
|
|
|
entity_id_t AllocateNewEntity();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new local entity ID that has never been used before.
|
|
|
|
* This entity will not be synchronised over the network, stored in saved games, etc.
|
|
|
|
*/
|
|
|
|
entity_id_t AllocateNewLocalEntity();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new entity ID that has never been used before.
|
|
|
|
* If possible, returns preferredId, and ensures this ID won't be allocated again.
|
|
|
|
* This affects the simulation state so it must only be called in network-synchronised ways.
|
|
|
|
*/
|
|
|
|
entity_id_t AllocateNewEntity(entity_id_t preferredId);
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
/**
|
|
|
|
* Constructs a component of type 'cid', initialised with data 'paramNode',
|
|
|
|
* and attaches it to entity 'ent'.
|
|
|
|
*
|
|
|
|
* @return true on success; false on failure, and logs an error message
|
|
|
|
*/
|
2013-09-11 22:41:53 +02:00
|
|
|
bool AddComponent(CEntityHandle ent, ComponentTypeId cid, const CParamNode& paramNode);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an externally-created component, so that it is returned by QueryInterface
|
|
|
|
* but does not get destroyed and does not receive messages from the component manager.
|
|
|
|
* (This is intended for unit tests that need to add mock objects the tested components
|
|
|
|
* expect to exist.)
|
|
|
|
*/
|
2013-09-11 22:41:53 +02:00
|
|
|
void AddMockComponent(CEntityHandle ent, InterfaceId iid, IComponent& component);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocates a component object of type 'cid', and attaches it to entity 'ent'.
|
|
|
|
* (The component's Init is not called here - either Init or Deserialize must be called
|
|
|
|
* before using the returned object.)
|
|
|
|
*/
|
2013-09-11 22:41:53 +02:00
|
|
|
IComponent* ConstructComponent(CEntityHandle ent, ComponentTypeId cid);
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-01-22 21:03:14 +01:00
|
|
|
/**
|
|
|
|
* Constructs an entity based on the given template, and adds it the world with
|
|
|
|
* entity ID @p ent. There should not be any existing components with that entity ID.
|
|
|
|
* @return ent, or INVALID_ENTITY on error
|
|
|
|
*/
|
|
|
|
entity_id_t AddEntity(const std::wstring& templateName, entity_id_t ent);
|
|
|
|
|
2010-01-14 21:36:29 +01:00
|
|
|
/**
|
|
|
|
* Destroys all the components belonging to the specified entity when FlushDestroyedComponents is called.
|
|
|
|
* Has no effect if the entity does not exist, or has already been added to the destruction queue.
|
|
|
|
*/
|
|
|
|
void DestroyComponentsSoon(entity_id_t ent);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does the actual destruction of components from DestroyComponentsSoon.
|
|
|
|
* This must not be called if the component manager is on the call stack (since it
|
|
|
|
* will break internal iterators).
|
|
|
|
*/
|
|
|
|
void FlushDestroyedComponents();
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
IComponent* QueryInterface(entity_id_t ent, InterfaceId iid) const;
|
2010-01-22 21:03:14 +01:00
|
|
|
|
2011-03-03 01:16:14 +01:00
|
|
|
typedef std::vector<std::pair<entity_id_t, IComponent*> > InterfaceList;
|
|
|
|
typedef boost::unordered_map<entity_id_t, IComponent*> InterfaceListUnordered;
|
|
|
|
|
|
|
|
InterfaceList GetEntitiesWithInterface(InterfaceId iid) const;
|
|
|
|
const InterfaceListUnordered& GetEntitiesWithInterfaceUnordered(InterfaceId iid) const;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-01-22 21:03:14 +01:00
|
|
|
/**
|
|
|
|
* Send a message, targeted at a particular entity. The message will be received by any
|
|
|
|
* components of that entity which subscribed to the message type, and by any other components
|
|
|
|
* that subscribed globally to the message type.
|
|
|
|
*/
|
2010-01-09 20:20:14 +01:00
|
|
|
void PostMessage(entity_id_t ent, const CMessage& msg) const;
|
2010-01-22 21:03:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a message, not targeted at any particular entity. The message will be received by any
|
|
|
|
* components that subscribed (either globally or not) to the message type.
|
|
|
|
*/
|
2010-01-09 20:20:14 +01:00
|
|
|
void BroadcastMessage(const CMessage& msg) const;
|
|
|
|
|
2010-01-14 21:36:29 +01:00
|
|
|
/**
|
|
|
|
* Resets the dynamic simulation state (deletes all entities, resets entity ID counters;
|
|
|
|
* doesn't unload/reload component scripts).
|
|
|
|
*/
|
|
|
|
void ResetState();
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-01-14 21:36:29 +01:00
|
|
|
// Various state serialization functions:
|
2011-03-05 23:30:32 +01:00
|
|
|
bool ComputeStateHash(std::string& outHash, bool quick);
|
2011-10-24 16:31:05 +02:00
|
|
|
bool DumpDebugState(std::ostream& stream, bool includeDebugInfo);
|
2010-01-14 21:36:29 +01:00
|
|
|
// FlushDestroyedComponents must be called before SerializeState (since the destruction queue
|
|
|
|
// won't get serialized)
|
2010-01-09 20:20:14 +01:00
|
|
|
bool SerializeState(std::ostream& stream);
|
|
|
|
bool DeserializeState(std::istream& stream);
|
|
|
|
|
2010-04-09 21:02:39 +02:00
|
|
|
std::string GenerateSchema();
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
ScriptInterface& GetScriptInterface() { return m_ScriptInterface; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Implementations of functions exposed to scripts
|
|
|
|
static void Script_RegisterComponentType(void* cbdata, int iid, std::string cname, CScriptVal ctor);
|
2010-01-22 21:03:14 +01:00
|
|
|
static void Script_RegisterInterface(void* cbdata, std::string name);
|
2010-07-18 17:19:49 +02:00
|
|
|
static void Script_RegisterMessageType(void* cbdata, std::string name);
|
2010-01-09 20:20:14 +01:00
|
|
|
static void Script_RegisterGlobal(void* cbdata, std::string name, CScriptVal value);
|
|
|
|
static IComponent* Script_QueryInterface(void* cbdata, int ent, int iid);
|
2010-08-04 23:15:41 +02:00
|
|
|
static std::vector<int> Script_GetEntitiesWithInterface(void* cbdata, int iid);
|
2011-01-16 00:35:20 +01:00
|
|
|
static std::vector<IComponent*> Script_GetComponentsWithInterface(void* cbdata, int iid);
|
2010-01-09 20:20:14 +01:00
|
|
|
static void Script_PostMessage(void* cbdata, int ent, int mtid, CScriptVal data);
|
|
|
|
static void Script_BroadcastMessage(void* cbdata, int mtid, CScriptVal data);
|
2010-01-22 21:03:14 +01:00
|
|
|
static int Script_AddEntity(void* cbdata, std::string templateName);
|
2010-01-24 18:24:35 +01:00
|
|
|
static int Script_AddLocalEntity(void* cbdata, std::string templateName);
|
|
|
|
static void Script_DestroyEntity(void* cbdata, int ent);
|
2011-03-21 23:59:00 +01:00
|
|
|
static CScriptVal Script_ReadJSONFile(void* cbdata, std::wstring fileName);
|
2012-05-04 01:32:10 +02:00
|
|
|
static CScriptVal Script_ReadCivJSONFile(void* cbdata, std::wstring fileName);
|
2012-08-16 00:25:25 +02:00
|
|
|
static std::vector<std::string> Script_FindJSONFiles(void* cbdata, std::wstring subPath, bool recursive);
|
2010-01-22 21:03:14 +01:00
|
|
|
|
2012-05-04 01:32:10 +02:00
|
|
|
static CScriptVal ReadJSONFile(void *cbdata, std::wstring filePath, std::wstring fileName);
|
2012-08-16 00:25:25 +02:00
|
|
|
|
|
|
|
// callback function to handle recursively finding files in a directory
|
2013-09-10 16:17:04 +02:00
|
|
|
static Status FindJSONFilesCallback(const VfsPath&, const CFileInfo&, const uintptr_t);
|
2012-05-04 01:32:10 +02:00
|
|
|
|
2010-07-18 17:19:49 +02:00
|
|
|
CMessage* ConstructMessage(int mtid, CScriptVal data);
|
2011-01-12 13:29:00 +01:00
|
|
|
void SendGlobalMessage(entity_id_t ent, const CMessage& msg) const;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
|
|
|
ComponentTypeId GetScriptWrapper(InterfaceId iid);
|
|
|
|
|
2013-09-11 22:41:53 +02:00
|
|
|
CEntityHandle AllocateEntityHandle(entity_id_t ent);
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
ScriptInterface m_ScriptInterface;
|
2013-09-11 22:41:53 +02:00
|
|
|
CSimContext& m_SimContext;
|
|
|
|
|
|
|
|
CEntityHandle m_SystemEntity;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-01-22 21:03:14 +01:00
|
|
|
ComponentTypeId m_CurrentComponent; // used when loading component types
|
2010-01-09 20:20:14 +01:00
|
|
|
bool m_CurrentlyHotloading;
|
|
|
|
|
|
|
|
// TODO: some of these should be vectors
|
|
|
|
std::map<ComponentTypeId, ComponentType> m_ComponentTypesById;
|
2011-03-03 01:16:14 +01:00
|
|
|
std::vector<boost::unordered_map<entity_id_t, IComponent*> > m_ComponentsByInterface; // indexed by InterfaceId
|
2010-01-09 20:20:14 +01:00
|
|
|
std::map<ComponentTypeId, std::map<entity_id_t, IComponent*> > m_ComponentsByTypeId;
|
2010-01-22 21:03:14 +01:00
|
|
|
std::map<MessageTypeId, std::vector<ComponentTypeId> > m_LocalMessageSubscriptions;
|
|
|
|
std::map<MessageTypeId, std::vector<ComponentTypeId> > m_GlobalMessageSubscriptions;
|
2010-01-09 20:20:14 +01:00
|
|
|
std::map<std::string, ComponentTypeId> m_ComponentTypeIdsByName;
|
|
|
|
std::map<std::string, MessageTypeId> m_MessageTypeIdsByName;
|
2010-07-18 17:19:49 +02:00
|
|
|
std::map<MessageTypeId, std::string> m_MessageTypeNamesById;
|
2010-01-22 21:03:14 +01:00
|
|
|
std::map<std::string, InterfaceId> m_InterfaceIdsByName;
|
2010-07-18 17:19:49 +02:00
|
|
|
|
2013-09-11 22:41:53 +02:00
|
|
|
std::map<entity_id_t, SEntityComponentCache*> m_ComponentCaches;
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
// TODO: maintaining both ComponentsBy* is nasty; can we get rid of one,
|
|
|
|
// while keeping QueryInterface and PostMessage sufficiently efficient?
|
|
|
|
|
2010-01-14 21:36:29 +01:00
|
|
|
std::vector<entity_id_t> m_DestructionQueue;
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
ComponentTypeId m_NextScriptComponentTypeId;
|
2010-01-14 21:36:29 +01:00
|
|
|
entity_id_t m_NextEntityId;
|
|
|
|
entity_id_t m_NextLocalEntityId;
|
2010-01-09 20:20:14 +01:00
|
|
|
|
2010-05-22 02:38:33 +02:00
|
|
|
boost::rand48 m_RNG;
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
friend class TestComponentManager;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // INCLUDED_COMPONENTMANAGER
|