BVB Source Codes

CRYENGINE Show CustomActionManager.cpp Source code

Return Download CRYENGINE: download CustomActionManager.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #include "StdAfx.h"
  4.  
  5. #include <CrySystem/ISystem.h>
  6. #include <CrySystem/File/ICryPak.h>
  7. #include <CryEntitySystem/IEntity.h>
  8. #include <CryEntitySystem/IEntitySystem.h>
  9.  
  10. #include "CustomActions/CustomActionManager.h"
  11.  
  12. ///////////////////////////////////////////////////
  13. // CCustomActionManager keeps track of all CustomActions
  14. ///////////////////////////////////////////////////
  15.  
  16. CCustomActionManager::CCustomActionManager()
  17. {
  18. }
  19.  
  20. CCustomActionManager::~CCustomActionManager()
  21. {
  22.         ClearActiveActions();
  23.         ClearLibraryActions();
  24. }
  25.  
  26. //------------------------------------------------------------------------------------------------------------------------
  27. bool CCustomActionManager::StartAction(IEntity* pObject, const char* szCustomActionGraphName, ICustomActionListener* pListener)
  28. {
  29.         CRY_ASSERT(pObject);
  30.         if (!pObject)
  31.                 return false;
  32.  
  33.         ICustomAction* pActiveCustomAction = GetActiveCustomAction(pObject);
  34.         if (pActiveCustomAction != NULL)
  35.         {
  36.                 CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_WARNING, "CCustomActionManager::StartAction: Action already running on entity, can't manually start");
  37.                 return false;
  38.         }
  39.  
  40.         pActiveCustomAction = AddActiveCustomAction(pObject, szCustomActionGraphName, pListener);
  41.         if (pActiveCustomAction == NULL)
  42.         {
  43.                 CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_WARNING, "CCustomActionManager::StartAction: Failed to add action");
  44.                 return false;
  45.         }
  46.  
  47.         pActiveCustomAction->StartAction();
  48.  
  49.         return true;
  50. }
  51.  
  52. //------------------------------------------------------------------------------------------------------------------------
  53. bool CCustomActionManager::SucceedAction(IEntity* pObject, const char* szCustomActionGraphName, ICustomActionListener* pListener)
  54. {
  55.         CRY_ASSERT(pObject);
  56.         if (!pObject)
  57.                 return false;
  58.  
  59.         ICustomAction* pActiveCustomAction = GetActiveCustomAction(pObject);
  60.         if (pActiveCustomAction != NULL)
  61.         {
  62.                 CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_WARNING, "CCustomActionManager::SucceedAction: Action already running on entity, can't manually succeed");
  63.                 return false;
  64.         }
  65.  
  66.         pActiveCustomAction = AddActiveCustomAction(pObject, szCustomActionGraphName, pListener);
  67.         if (pActiveCustomAction == NULL)
  68.         {
  69.                 CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_WARNING, "CCustomActionManager::SucceedAction: Failed to add action");
  70.                 return false;
  71.         }
  72.  
  73.         pActiveCustomAction->SucceedAction();
  74.  
  75.         return true;
  76. }
  77.  
  78. bool CCustomActionManager::SucceedWaitAction(IEntity* pObject)
  79. {
  80.         CRY_ASSERT(pObject);
  81.         if (!pObject)
  82.                 return false;
  83.  
  84.         ICustomAction* pActiveCustomAction = GetActiveCustomAction(pObject);
  85.         if (pActiveCustomAction == NULL)
  86.         {
  87.                 CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_WARNING, "CCustomActionManager::SucceedWaitAction: Failed find active action");
  88.                 return false;
  89.         }
  90.  
  91.         return pActiveCustomAction->SucceedWaitAction();
  92. }
  93.  
  94. bool CCustomActionManager::SucceedWaitCompleteAction(IEntity* pObject)
  95. {
  96.         CRY_ASSERT(pObject);
  97.         if (!pObject)
  98.                 return false;
  99.  
  100.         ICustomAction* pActiveCustomAction = GetActiveCustomAction(pObject);
  101.         if (pActiveCustomAction == NULL)
  102.         {
  103.                 CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_WARNING, "CCustomActionManager::SucceedWaitCompleteAction: Failed find active action");
  104.                 return false;
  105.         }
  106.  
  107.         return pActiveCustomAction->SucceedWaitCompleteAction();
  108. }
  109.  
  110. //------------------------------------------------------------------------------------------------------------------------
  111. bool CCustomActionManager::AbortAction(IEntity* pObject)
  112. {
  113.         CRY_ASSERT(pObject);
  114.         if (!pObject)
  115.                 return false;
  116.  
  117.         ICustomAction* pActiveCustomAction = GetActiveCustomAction(pObject);
  118.         if (pActiveCustomAction == NULL)
  119.         {
  120.                 CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_WARNING, "CCustomActionManager::AbortAction: Failed find active action");
  121.                 return false;
  122.         }
  123.  
  124.         pActiveCustomAction->AbortAction();
  125.  
  126.         return true;
  127. }
  128.  
  129. //------------------------------------------------------------------------------------------------------------------------
  130. bool CCustomActionManager::EndAction(IEntity* pObject, bool bSuccess)
  131. {
  132.         CRY_ASSERT(pObject);
  133.         if (!pObject)
  134.                 return false;
  135.  
  136.         ICustomAction* pActiveCustomAction = GetActiveCustomAction(pObject);
  137.         if (pActiveCustomAction == NULL)
  138.         {
  139.                 CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_WARNING, "CCustomActionManager::EndAction: Failed find active action");
  140.                 return false;
  141.         }
  142.  
  143.         if (bSuccess)
  144.         {
  145.                 pActiveCustomAction->EndActionSuccess();
  146.         }
  147.         else
  148.         {
  149.                 pActiveCustomAction->EndActionFailure();
  150.         }
  151.  
  152.         return true;
  153. }
  154.  
  155. //------------------------------------------------------------------------------------------------------------------------
  156. void CCustomActionManager::LoadLibraryActions(const char* sPath)
  157. {
  158.         m_activeActions.clear();
  159.  
  160.         // don't delete all actions - only those which are added or modified will be reloaded
  161.         //m_actionsLib.clear();
  162.  
  163.         string path(sPath);
  164.         ICryPak* pCryPak = gEnv->pCryPak;
  165.         _finddata_t fd;
  166.         string filename;
  167.  
  168.         path.TrimRight("/\\");
  169.         string search = path + "/*.xml";
  170.         intptr_t handle = pCryPak->FindFirst(search.c_str(), &fd);
  171.         if (handle != -1)
  172.         {
  173.                 do
  174.                 {
  175.                         filename = path;
  176.                         filename += "/";
  177.                         filename += fd.name;
  178.  
  179.                         XmlNodeRef root = GetISystem()->LoadXmlFromFile(filename);
  180.                         if (root)
  181.                         {
  182.                                 if (gEnv->pFlowSystem)
  183.                                 {
  184.                                         filename = PathUtil::GetFileName(filename);
  185.                                         CCustomAction& action = m_actionsLib[filename];   // this creates a new entry in m_actionsLib
  186.                                         if (!action.m_pFlowGraph)
  187.                                         {
  188.                                                 action.m_customActionGraphName = filename;
  189.                                                 action.m_pFlowGraph = gEnv->pFlowSystem->CreateFlowGraph();
  190.                                                 action.m_pFlowGraph->SetSuspended(true);
  191.                                                 action.m_pFlowGraph->SetCustomAction(&action);
  192.                                                 action.m_pFlowGraph->SerializeXML(root, true);
  193.                                         }
  194.                                 }
  195.                         }
  196.                 }
  197.                 while (pCryPak->FindNext(handle, &fd) >= 0);
  198.  
  199.                 pCryPak->FindClose(handle);
  200.         }
  201. }
  202.  
  203. //------------------------------------------------------------------------------------------------------------------------
  204. void CCustomActionManager::ClearActiveActions()
  205. {
  206.         TActiveActions::iterator it, itEnd = m_activeActions.end();
  207.         for (it = m_activeActions.begin(); it != itEnd; ++it)
  208.         {
  209.                 CCustomAction& action = *it;
  210.                 action.TerminateAction();
  211.         }
  212.         m_activeActions.clear();
  213. }
  214.  
  215. //------------------------------------------------------------------------------------------------------------------------
  216. void CCustomActionManager::ClearLibraryActions()
  217. {
  218.         m_actionsLib.clear();
  219. }
  220.  
  221. //------------------------------------------------------------------------------------------------------------------------
  222. ICustomAction* CCustomActionManager::GetCustomActionFromLibrary(const char* szCustomActionGraphName)
  223. {
  224.         if (!szCustomActionGraphName || !*szCustomActionGraphName)
  225.                 return NULL;
  226.  
  227.         TCustomActionsLib::iterator it = m_actionsLib.find(CONST_TEMP_STRING(szCustomActionGraphName));
  228.         if (it != m_actionsLib.end())
  229.                 return &it->second;
  230.         else
  231.                 return NULL;
  232. }
  233.  
  234. //------------------------------------------------------------------------------------------------------------------------
  235. ICustomAction* CCustomActionManager::GetCustomActionFromLibrary(const size_t index)
  236. {
  237.         if (index >= m_actionsLib.size())
  238.                 return NULL;
  239.  
  240.         TCustomActionsLib::iterator it = m_actionsLib.begin();
  241.         for (size_t i = 0; i < index; i++)
  242.         {
  243.                 ++it;
  244.         }
  245.  
  246.         return &it->second;
  247. }
  248.  
  249. //------------------------------------------------------------------------------------------------------------------------
  250. size_t CCustomActionManager::GetNumberOfActiveCustomActions() const
  251. {
  252.         size_t numActiveCustomActions = 0;
  253.  
  254.         TActiveActions::const_iterator it = m_activeActions.begin();
  255.         const TActiveActions::const_iterator itEnd = m_activeActions.end();
  256.         for (; it != itEnd; ++it)
  257.         {
  258.                 const CCustomAction& action = *it;
  259.                 if (!(action.GetCurrentState() == CAS_Ended))   // Ignore inactive actions waiting to be removed
  260.                 {
  261.                         numActiveCustomActions++;
  262.                 }
  263.         }
  264.  
  265.         return numActiveCustomActions;
  266. }
  267.  
  268. //------------------------------------------------------------------------------------------------------------------------
  269. ICustomAction* CCustomActionManager::GetActiveCustomAction(const IEntity* pObject)
  270. {
  271.         CRY_ASSERT(pObject != NULL);
  272.         if (!pObject)
  273.                 return NULL;
  274.  
  275.         TActiveActions::iterator it = m_activeActions.begin();
  276.         const TActiveActions::const_iterator itEnd = m_activeActions.end();
  277.         for (; it != itEnd; ++it)
  278.         {
  279.                 CCustomAction& action = *it;
  280.                 if ((!(action.GetCurrentState() == CAS_Ended)) &&  // Ignore inactive actions waiting to be removed
  281.                     (action.m_pObjectEntity == pObject))
  282.                 {
  283.                         break;
  284.                 }
  285.         }
  286.  
  287.         if (it != m_activeActions.end())
  288.                 return &(*it);
  289.         else
  290.                 return NULL;
  291. }
  292.  
  293. //------------------------------------------------------------------------------------------------------------------------
  294. ICustomAction* CCustomActionManager::GetActiveCustomAction(const size_t index)
  295. {
  296.         if (index >= m_activeActions.size())
  297.                 return NULL;
  298.  
  299.         TActiveActions::iterator it = m_activeActions.begin();
  300.         const TActiveActions::const_iterator itEnd = m_activeActions.end();
  301.         size_t curActiveIndex = -1;
  302.         CCustomAction* pFoundAction = NULL;
  303.  
  304.         for (; it != itEnd; ++it)
  305.         {
  306.                 CCustomAction& customAction = *it;
  307.                 if (!(customAction.GetCurrentState() == CAS_Ended))   // Ignore marked for deletion but awaiting cleanup
  308.                 {
  309.                         curActiveIndex++;
  310.  
  311.                         if (curActiveIndex == index)
  312.                         {
  313.                                 pFoundAction = &customAction;
  314.                                 break;
  315.                         }
  316.                 }
  317.         }
  318.  
  319.         return pFoundAction;
  320. }
  321.  
  322. //------------------------------------------------------------------------------------------------------------------------
  323. bool CCustomActionManager::UnregisterListener(ICustomActionListener* pEventListener)
  324. {
  325.         TActiveActions::iterator it = m_activeActions.begin();
  326.         const TActiveActions::const_iterator itEnd = m_activeActions.end();
  327.         bool bSuccessfullyUnregistered = false;
  328.         for (; it != itEnd; ++it)
  329.         {
  330.                 CCustomAction& customAction = *it;
  331.                 if (!(customAction.GetCurrentState() == CAS_Ended))   // Ignore marked for deletion but awaiting cleanup
  332.                 {
  333.                         if (customAction.m_listeners.Contains(pEventListener) == true)
  334.                         {
  335.                                 bSuccessfullyUnregistered = true;
  336.                                 customAction.UnregisterListener(pEventListener);
  337.                         }
  338.                 }
  339.         }
  340.  
  341.         return bSuccessfullyUnregistered;
  342. }
  343.  
  344. //------------------------------------------------------------------------------------------------------------------------
  345. void CCustomActionManager::Update()
  346. {
  347.         FUNCTION_PROFILER(gEnv->pSystem, PROFILE_ACTION)
  348.  
  349.         TActiveActions::iterator it = m_activeActions.begin();
  350.         while (it != m_activeActions.end())
  351.         {
  352.                 CCustomAction& action = *it;
  353.                 if (!action.m_pObjectEntity)
  354.                 {
  355.                         // The action was terminated, just remove it from the list
  356.                         m_activeActions.erase(it++);
  357.                         continue;
  358.                 }
  359.                 ++it;
  360.  
  361.                 if (action.GetCurrentState() == CAS_Ended)
  362.                 {
  363.                         // Remove the pointer to this action in the flow graph
  364.                         IFlowGraph* pFlowGraph = action.GetFlowGraph();
  365.                         if (pFlowGraph && pFlowGraph->GetCustomAction() != NULL)   // Might be null if terminated
  366.                                 pFlowGraph->SetCustomAction(NULL);
  367.  
  368.                         // Remove in the actual list
  369.                         m_activeActions.remove(action);
  370.                 }
  371.         }
  372. }
  373.  
  374. //------------------------------------------------------------------------------------------------------------------------
  375. ICustomAction* CCustomActionManager::AddActiveCustomAction(IEntity* pObject, const char* szCustomActionGraphName, ICustomActionListener* pListener)
  376. {
  377.         CRY_ASSERT(pObject != NULL);
  378.         if (!pObject)
  379.                 return NULL;
  380.  
  381.         ICustomAction* pActiveCustomAction = GetActiveCustomAction(pObject);
  382.         CRY_ASSERT(pActiveCustomAction == NULL);
  383.         if (pActiveCustomAction != NULL)
  384.                 return NULL;
  385.  
  386.         // Instance custom actions don't need to have a custom action graph
  387.         IFlowGraphPtr pFlowGraph = NULL;
  388.         if (szCustomActionGraphName != NULL && szCustomActionGraphName[0] != 0)
  389.         {
  390.                 ICustomAction* pCustomActionFromLibrary = GetCustomActionFromLibrary(szCustomActionGraphName);
  391.                 if (pCustomActionFromLibrary == NULL)
  392.                 {
  393.                         CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_WARNING, "CCustomActionManager::AddActiveCustomAction: Failed find action: %s in library", szCustomActionGraphName);
  394.                         return NULL;
  395.                 }
  396.                 // Create a clone of the flow graph
  397.  
  398.                 if (IFlowGraph* pLibraryActionFlowGraph = pCustomActionFromLibrary->GetFlowGraph())
  399.                         pFlowGraph = pLibraryActionFlowGraph->Clone();
  400.  
  401.                 if (!pFlowGraph)
  402.                 {
  403.                         CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_WARNING, "CCustomActionManager::AddActiveCustomAction: No flow graph in action: %s in library", szCustomActionGraphName);
  404.                         return NULL;
  405.                 }
  406.         }
  407.  
  408.         // create active action and add it to the list
  409.         m_activeActions.push_front(CCustomAction());
  410.  
  411.         CCustomAction& addedAction = m_activeActions.front();
  412.  
  413.         addedAction.m_pFlowGraph = pFlowGraph;
  414.         addedAction.m_customActionGraphName = szCustomActionGraphName;
  415.         addedAction.m_pObjectEntity = pObject;
  416.  
  417.         if (pListener)
  418.         {
  419.                 addedAction.RegisterListener(pListener, "ListenerFlowGraphCustomAction");
  420.         }
  421.  
  422.         if (pFlowGraph)
  423.         {
  424.                 // The Object will be first graph entity.
  425.                 pFlowGraph->SetGraphEntity(pObject->GetId(), 0);
  426.  
  427.                 // Initialize the graph
  428.                 pFlowGraph->InitializeValues();
  429.  
  430.                 pFlowGraph->SetCustomAction(&addedAction);
  431.  
  432.                 pFlowGraph->SetSuspended(false);
  433.         }
  434.  
  435.         return &addedAction;
  436. }
  437.  
  438. //------------------------------------------------------------------------------------------------------------------------
  439. void CCustomActionManager::OnEntityRemove(IEntity* pEntity)
  440. {
  441.         // Can't remove action here from list since will crash if the entity gets deleted while the action flow graph is being updated
  442.  
  443.         TActiveActions::iterator it, itEnd = m_activeActions.end();
  444.         for (it = m_activeActions.begin(); it != itEnd; ++it)
  445.         {
  446.                 CCustomAction& action = *it;
  447.  
  448.                 if (pEntity == action.GetObjectEntity())
  449.                 {
  450.                         action.TerminateAction();
  451.                 }
  452.         }
  453. }
  454.  
  455. //------------------------------------------------------------------------------------------------------------------------
  456. void CCustomActionManager::Serialize(TSerialize ser)
  457. {
  458.         if (ser.BeginOptionalGroup("CustomActionManager", true))
  459.         {
  460.                 int numActiveActions = m_activeActions.size();
  461.                 ser.Value("numActiveActions", numActiveActions);
  462.                 if (ser.IsReading())
  463.                 {
  464.                         ClearActiveActions();
  465.                         while (numActiveActions--)
  466.                                 m_activeActions.push_back(CCustomAction());
  467.                 }
  468.  
  469.                 TActiveActions::iterator it, itEnd = m_activeActions.end();
  470.                 for (it = m_activeActions.begin(); it != itEnd; ++it)
  471.                         it->Serialize(ser);
  472.                 ser.EndGroup();
  473.         }
  474. }
  475.  
downloadCustomActionManager.cpp 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