BVB Source Codes

CRYENGINE Show BehaviorTreeNodes_Action.cpp Source code

Return Download CRYENGINE: download BehaviorTreeNodes_Action.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. #include "BehaviorTreeNodes_Action.h"
  5. #include <CryAISystem/BehaviorTree/IBehaviorTree.h>
  6. #include <CryAISystem/BehaviorTree/Action.h>
  7. #include <CryAISystem/BehaviorTree/Decorator.h>
  8. #include "IActorSystem.h"
  9. #include "Mannequin/AnimActionTriState.h"
  10. #include <CrySystem/XML/XMLAttrReader.h>
  11.  
  12. namespace BehaviorTree
  13. {
  14. // Play an animation fragment directly through Mannequin (start a fragment
  15. // for a specific FragmentID), and wait until it is done.
  16. class AnimateFragment : public Action
  17. {
  18. public:
  19.         // TODO: Move mannequin priorities into CryAction or get this from a central location
  20.         static const int NORMAL_ACTION_PRIORITY = 3;   // currently equal to PP_Action, just above movement, but underneath urgent actions, hit death, etc.
  21.         static const int URGENT_ACTION_PRIORITY = 4;   // currently equal to PP_Urgent_Action, just above normal actions, but underneath hit death, etc.
  22.  
  23.         struct RuntimeData
  24.         {
  25.                 // The action animation action that is running or NULL if none.
  26.                 typedef _smart_ptr<CAnimActionTriState> CAnimActionTriStatePtr;
  27.                 CAnimActionTriStatePtr action;
  28.  
  29.                 // True if the action was queued; false if an error occured.
  30.                 bool actionQueuedFlag;
  31.  
  32.                 RuntimeData()
  33.                         : actionQueuedFlag(false)
  34.                 {
  35.                 }
  36.  
  37.                 ~RuntimeData()
  38.                 {
  39.                         ReleaseAction();
  40.                 }
  41.  
  42.                 void ReleaseAction()
  43.                 {
  44.                         if (this->action.get() != NULL)
  45.                         {
  46.                                 this->action->ForceFinish();
  47.                                 this->action.reset();
  48.                         }
  49.                 }
  50.         };
  51.  
  52.         AnimateFragment() :
  53.                 m_fragNameCrc32(0)
  54. #ifdef USING_BEHAVIOR_TREE_NODE_CUSTOM_DEBUG_TEXT
  55.                 , m_fragName()
  56. #endif
  57.         {
  58.         }
  59.  
  60.         virtual LoadResult LoadFromXml(const XmlNodeRef& xml, const LoadContext& context) override
  61.         {
  62.                 const string fragName = xml->getAttr("name");
  63.                 IF_UNLIKELY (fragName.empty())
  64.                 {
  65.                         ErrorReporter(*this, context).LogError("Missing attribute 'name'.");
  66.                         return LoadFailure;
  67.                 }
  68. #ifdef USING_BEHAVIOR_TREE_NODE_CUSTOM_DEBUG_TEXT
  69.                 m_fragName = fragName;
  70. #endif
  71.                 m_fragNameCrc32 = CCrc32::ComputeLowercase(fragName);
  72.  
  73.                 return LoadSuccess;
  74.         }
  75.  
  76. #ifdef USING_BEHAVIOR_TREE_NODE_CUSTOM_DEBUG_TEXT
  77.         virtual void GetCustomDebugText(const UpdateContext& updateContext, stack_string& debugText) const override
  78.         {
  79.                 debugText = m_fragName;
  80.         }
  81. #endif
  82.  
  83. protected:
  84.  
  85.         virtual void OnInitialize(const UpdateContext& context) override
  86.         {
  87.                 QueueAction(context);
  88.         }
  89.  
  90.         virtual void OnTerminate(const UpdateContext& context) override
  91.         {
  92.                 RuntimeData& runtimeData = GetRuntimeData<RuntimeData>(context);
  93.                 runtimeData.ReleaseAction();
  94.         }
  95.  
  96.         virtual Status Update(const UpdateContext& context) override
  97.         {
  98.                 RuntimeData& runtimeData = GetRuntimeData<RuntimeData>(context);
  99.  
  100.                 IF_UNLIKELY (!runtimeData.actionQueuedFlag)
  101.                 {
  102.                         return Failure;
  103.                 }
  104.  
  105.                 assert(runtimeData.action.get() != NULL);
  106.                 const IAction::EStatus currentActionStatus = runtimeData.action->GetStatus();
  107.                 IF_UNLIKELY ((currentActionStatus == IAction::None) || (currentActionStatus == IAction::Finished))
  108.                 {
  109.                         return Success;
  110.                 }
  111.  
  112.                 return Running;
  113.         }
  114.  
  115. private:
  116.  
  117.         void QueueAction(const UpdateContext& context)
  118.         {
  119.                 RuntimeData& runtimeData = GetRuntimeData<RuntimeData>(context);
  120.  
  121.                 IActor* actor = CCryAction::GetCryAction()->GetIActorSystem()->GetActor(context.entityId);
  122.  
  123.                 IF_UNLIKELY (actor == NULL)
  124.                 {
  125.                         assert(false);
  126.                         return;
  127.                 }
  128.  
  129.                 IAnimatedCharacter* animChar = actor->GetAnimatedCharacter();
  130.                 IF_UNLIKELY (animChar == NULL)
  131.                 {
  132.                         assert(false);
  133.                         return;
  134.                 }
  135.  
  136.                 IF_LIKELY (runtimeData.action.get() == NULL)
  137.                 {
  138.                         IActionController* pIActionController = animChar->GetActionController();
  139.                         const FragmentID fragID = pIActionController->GetFragID(m_fragNameCrc32);
  140.                         IF_LIKELY (fragID == FRAGMENT_ID_INVALID)
  141.                         {
  142. #ifdef USING_BEHAVIOR_TREE_NODE_CUSTOM_DEBUG_TEXT
  143.                                 ErrorReporter(*this, context).LogError("Invalid fragment name '%s'", m_fragName);
  144. #else
  145.                                 ErrorReporter(*this, context).LogError("Invalid fragment name!");
  146. #endif
  147.                                 return;
  148.                         }
  149.                         runtimeData.action = new CAnimActionTriState(NORMAL_ACTION_PRIORITY, fragID, *animChar, true /*oneshot*/);
  150.                         pIActionController->Queue(*runtimeData.action);
  151.                 }
  152.  
  153.                 runtimeData.actionQueuedFlag = true;
  154.         }
  155.  
  156. private:
  157.  
  158.         // The CRC value for the fragment name.
  159.         uint32 m_fragNameCrc32;
  160.  
  161. #ifdef USING_BEHAVIOR_TREE_NODE_CUSTOM_DEBUG_TEXT
  162.         // The actual fragment name.
  163.         string m_fragName;
  164. #endif
  165. };
  166. }
  167.  
  168. void RegisterActionBehaviorTreeNodes()
  169. {
  170.         using namespace BehaviorTree;
  171.  
  172.         assert(gEnv->pAISystem->GetIBehaviorTreeManager());
  173.  
  174.         IBehaviorTreeManager& manager = *gEnv->pAISystem->GetIBehaviorTreeManager();
  175.  
  176.         REGISTER_BEHAVIOR_TREE_NODE(manager, AnimateFragment);
  177. }
  178.  
downloadBehaviorTreeNodes_Action.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