BVB Source Codes

CRYENGINE Show Module.h Source code

Return Download CRYENGINE: download Module.h Source code - Download CRYENGINE Source code - Type:.h
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /*
  4.  * A Module is a container and interface for a graph.
  5.  * The module has a set of instances that can run independently.
  6.  * Modules have an external 'call' node and internally a 'start' and an 'end'. - see FlowModuleNodes.h
  7.  * Modules are managed by an editor manager Editor:FlowGraphModuleManager.h and a runtime manager - CryAction:ModuleManager.h
  8.  */
  9.  
  10. #pragma once
  11.  
  12. #include <CryFlowGraph/IFlowGraphModuleManager.h>
  13. #include <CryCore/Containers/VectorMap.h>
  14.  
  15. class CFlowModuleCallNode;
  16. class CFlowModuleStartNode;
  17. class CFlowModuleEndNode;
  18.  
  19. // notes on Instance IDs:
  20. // - a Module runs a set of instances with unique IDs that are associated with an entity (or with invalid_entityid)
  21. // - if a call node has a manually specified instance ID different from '-1', it will be used
  22. // - the first of the same specific IID to be called will create the instance which in turn will output for all all call nodes with that IID.
  23. // - IIDs specified at runtime may refer to the "offline" manually specified IIDs.
  24. // - an IID generated at runtime will not collide with one manually specified (the ID generator starts from the biggest manual ID found)
  25. // - an IID '-1' generates new instances on every call if there is no entity associated
  26. // - the call node will hold the outputs of the last generated instance until it is called again
  27. // - if there is an entity id specified, the IID will refer within that entity, meaning entities can have their own separate instance 1,2,3.. of the same module
  28. // - an IID -1 with a specified entity ID will not generate new IID. '-1' is the instance ID.
  29.  
  30. struct SModuleInstance : public IModuleInstance
  31. {
  32.         SModuleInstance(IFlowGraphModule* pModule, TModuleInstanceId id)
  33.                 : IModuleInstance(pModule, id)
  34.                 , m_entityId(INVALID_ENTITYID)
  35.                 , m_pStartNode(nullptr)
  36.         {}
  37.  
  38.         EntityId m_entityId;
  39.  
  40.         CFlowModuleStartNode* GetStartNode() const                           { return m_pStartNode; }
  41.         void                  SetStartNode(CFlowModuleStartNode* pStartNode) { m_pStartNode = pStartNode; }
  42.  
  43. protected:
  44.         CFlowModuleStartNode* m_pStartNode;
  45. };
  46.  
  47. class CFlowGraphModule : public IFlowGraphModule
  48. {
  49.         friend class CFlowGraphModuleManager;
  50.  
  51. public:
  52.  
  53.         CFlowGraphModule(TModuleId moduleId);
  54.         virtual ~CFlowGraphModule();
  55.  
  56.         // IFlowGraphModule
  57.         virtual const char*                                GetName() const                       { return m_name.c_str(); }
  58.         virtual const char*                                GetPath() const                       { return m_fileName.c_str(); }
  59.         virtual TModuleId                                  GetId() const                         { return m_Id; }
  60.  
  61.         virtual IFlowGraphModule::EType                    GetType() const                       { return m_type; }
  62.         virtual void                                       SetType(IFlowGraphModule::EType type) { m_type = type; }
  63.  
  64.         virtual IFlowGraph*                                GetRootGraph() const                  { return m_pRootGraph; }
  65.         bool                                               HasInstanceGraph(IFlowGraphPtr pGraph);
  66.  
  67.         virtual size_t                                     GetModuleInputPortCount() const;
  68.         virtual size_t                                     GetModuleOutputPortCount() const;
  69.         virtual const IFlowGraphModule::SModulePortConfig* GetModuleInputPort(size_t index) const;
  70.         virtual const IFlowGraphModule::SModulePortConfig* GetModuleOutputPort(size_t index) const;
  71.         virtual bool                                       AddModulePort(const SModulePortConfig& port);
  72.         virtual void                                       RemoveModulePorts();
  73.  
  74.         virtual IModuleInstanceIteratorPtr                 CreateInstanceIterator();
  75.         virtual size_t                                     GetRunningInstancesCount() const;
  76.         // ~IFlowGraphModule
  77.  
  78.         inline SModuleInstance* GetInstance(EntityId entityId, TModuleInstanceId instanceId);
  79.         bool                    HasRunningInstances() const;
  80.  
  81.         // Call Nodes for Instances Registry
  82.         void RegisterCallNodeForInstance(EntityId entityId, TModuleInstanceId instanceId, uint callNodeId, CFlowModuleCallNode* pCallNode);
  83.         void UnregisterCallNodeForInstance(EntityId entityId, TModuleInstanceId instanceId, uint callNodeId);
  84.         void ChangeRegisterCallNodeForInstance(
  85.           EntityId oldEntityId, TModuleInstanceId oldInstanceId,
  86.           EntityId newEntityId, TModuleInstanceId newInstanceId,
  87.           uint callNodeId, CFlowModuleCallNode* pCallNode);
  88.         void ClearCallNodesForInstances();
  89.  
  90.         // Global Control Nodes Registry
  91.         void RegisterGlobalControlNode(uint callNodeId, CFlowModuleCallNode* pCallNode);
  92.         void UnregisterGlobalControlNode(uint callNodeId);
  93.         void ClearGlobalControlNodes();
  94.  
  95.         //! Create a new Instance, register it in the running instances and pass event to call nodes, global controllers and listeners
  96.         void CreateInstance(EntityId entityId, TModuleInstanceId runningInstanceId);
  97.  
  98.         //! Call/Update an Instance with inputs. InstanceId may be invalid in which case a new instance will be generated
  99.         TModuleInstanceId CallModuleInstance(EntityId entityId, TModuleInstanceId instanceId, const TModuleParams& params, CFlowModuleCallNode* pCallingNode);
  100.  
  101.         //! Call/Update all Instances with inputs
  102.         void CallAllModuleInstances(const TModuleParams& params, CFlowModuleCallNode* pCallingNode);
  103.  
  104.         //! Create and call a new Instance associated with and Entity.
  105.         void CallDefaultInstanceForEntity(IEntity* pEntity);
  106.  
  107.         //! Activate the cancel port in the start node of the instance
  108.         void CancelInstance(EntityId entityId, TModuleInstanceId instanceId);
  109.  
  110.         //! Activate the cancel port in the start node of all the running instances of this module
  111.         void CancelAllInstances();
  112.  
  113.         //! Send one input port to the instance's Start node
  114.         void UpdateInstanceInputPort(EntityId entityId, TModuleInstanceId instanceId, size_t paramIdx, const TFlowInputData& value);
  115.  
  116.         //! Send one input port to the Start node of all running instances of this module
  117.         void UpdateAllInstancesInputPort(size_t paramIdx, const TFlowInputData& value);
  118.  
  119.         //! Send one output port of the instance to all its Call nodes
  120.         void UpdateInstanceOutputPort(EntityId entityId, TModuleInstanceId instanceId, size_t paramIdx, const TFlowInputData& value);
  121.  
  122.         //! Update call nodes with final outputs and destroy instance
  123.         void OnInstanceFinished(EntityId entityId, TModuleInstanceId instanceId, bool bSuccess, const TModuleParams& params);
  124.  
  125.         // For setting up connections between the instance and it's nodes at creation time
  126.         TModuleInstanceId GetInstanceIdOfInstanceBeingCreated() const { return m_pInstanceBeingCreated->m_instanceId; }
  127.         EntityId          GetEntityOfInstanceBeingCreated() const     { return m_pInstanceBeingCreated->m_entityId; }
  128.         void              RegisterStartNodeForInstanceBeingCreated(CFlowModuleStartNode* pStartNode);
  129.  
  130. private:
  131.  
  132.         bool PreLoadModule(const char* fileName);                           //! load initial data, create nodes
  133.         bool LoadModuleGraph(const char* moduleName, const char* fileName); //! load actual flowgraph
  134.         bool SaveModuleXml(XmlNodeRef saveTo);
  135.  
  136.         void Destroy();
  137.  
  138.         // create flowgraph nodes (Start, Return, Call) for this module
  139.         void RegisterNodes();
  140.         void UnregisterNodes();
  141.  
  142.         // Instance handling
  143.         void DeactivateInstanceGraph(SModuleInstance* pInstance) const;
  144.         bool DestroyInstance(EntityId entityId, TModuleInstanceId instanceId);
  145.         void RemoveCompletedInstances();
  146.         void RemoveAllInstances();
  147.  
  148.         // attributes
  149.         TModuleId               m_Id;
  150.         IFlowGraphModule::EType m_type;
  151.         string                  m_name;
  152.         string                  m_fileName;
  153.         IFlowGraphPtr           m_pRootGraph; //! Root graph all others are cloned from
  154.  
  155.         // Inputs and outputs for this module
  156.         typedef std::vector<SModulePortConfig> TModulePorts;
  157.         TModulePorts m_moduleInpPorts;
  158.         TModulePorts m_moduleOutPorts;
  159.  
  160.         // Instance Registries
  161.         typedef std::map<TModuleInstanceId, SModuleInstance> TInstanceMap;
  162.         typedef std::map<EntityId, TInstanceMap>             TEntityInstanceMap;
  163.         TEntityInstanceMap m_runningInstances; //! Instances of this module currently running
  164.  
  165.         typedef VectorMap<uint, CFlowModuleCallNode*>       TCallNodesMap;
  166.         typedef VectorMap<TModuleInstanceId, TCallNodesMap> TInstanceCallNodesMap;
  167.         typedef VectorMap<EntityId, TInstanceCallNodesMap>  TEntityInstancesToCallNodes;
  168.         TEntityInstancesToCallNodes m_requestedInstanceIds;  //! map for Instance IDs that are manually requested in the Call nodes at game start
  169.         TModuleInstanceId           m_nextInstanceId;        //! ID for the next generated instance. Starts after the last manually requested ID.
  170.  
  171.         SModuleInstance*            m_pInstanceBeingCreated; //! for Start and Return nodes to read when they are being cloned.
  172.  
  173.         // Global control nodes registry
  174.         TCallNodesMap m_globalControlNodes; //! Instances of this module currently running
  175.  
  176.         // flow node registration
  177.         TFlowNodeTypeId m_startNodeTypeId;
  178.         TFlowNodeTypeId m_returnNodeTypeId;
  179.         TFlowNodeTypeId m_callNodeTypeId;
  180.  
  181.         // Instance Iterator
  182.         class CInstanceIterator : public IFlowGraphModuleInstanceIterator
  183.         {
  184.         public:
  185.                 CInstanceIterator(CFlowGraphModule* pFM)
  186.                 {
  187.                         m_pModule = pFM;
  188.                         m_curEnt = m_pModule->m_runningInstances.begin();
  189.                         if (m_curEnt != m_pModule->m_runningInstances.end())
  190.                         {
  191.                                 m_curInst = m_curEnt->second.begin();
  192.                         }
  193.                         m_nRefs = 0;
  194.                 }
  195.  
  196.                 void AddRef()
  197.                 {
  198.                         ++m_nRefs;
  199.                 }
  200.  
  201.                 void Release()
  202.                 {
  203.                         if (--m_nRefs <= 0)
  204.                         {
  205.                                 this->~CInstanceIterator();
  206.                                 m_pModule->m_iteratorPool.push_back(this);
  207.                         }
  208.                 }
  209.  
  210.                 IModuleInstance* Next()
  211.                 {
  212.                         if (m_curEnt == m_pModule->m_runningInstances.end())
  213.                         {
  214.                                 return nullptr;
  215.                         }
  216.  
  217.                         IModuleInstance* pCurInst = &(m_curInst->second);
  218.  
  219.                         //advance
  220.                         ++m_curInst;
  221.                         if (m_curInst == m_curEnt->second.end())
  222.                         {
  223.                                 ++m_curEnt;
  224.                                 if (m_curEnt != m_pModule->m_runningInstances.end())
  225.                                 {
  226.                                         m_curInst = m_curEnt->second.begin();
  227.                                 }
  228.                         }
  229.  
  230.                         return pCurInst;
  231.                 }
  232.  
  233.                 size_t Count()
  234.                 {
  235.                         return m_pModule->GetRunningInstancesCount();
  236.                 }
  237.  
  238.                 CFlowGraphModule*            m_pModule;
  239.                 TInstanceMap::iterator       m_curInst;
  240.                 TEntityInstanceMap::iterator m_curEnt;
  241.                 int                          m_nRefs;
  242.         };
  243.  
  244.         std::vector<CInstanceIterator*> m_iteratorPool;
  245.  
  246. };
  247.  
  248. TYPEDEF_AUTOPTR(CFlowGraphModule);
  249. typedef CFlowGraphModule_AutoPtr CFlowGraphModulePtr;
  250.  
downloadModule.h Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top