BVB Source Codes

CRYENGINE Show AIActions.h Source code

Return Download CRYENGINE: download AIActions.h Source code - Download CRYENGINE Source code - Type:.h
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #ifndef _AIACTIONS_H_
  4. #define _AIACTIONS_H_
  5.  
  6. #if _MSC_VER > 1000
  7.         #pragma once
  8. #endif
  9.  
  10. #include <CryFlowGraph/IFlowSystem.h>
  11. #include <CryAISystem/IAIAction.h>
  12. #include <CryCore/Containers/CryListenerSet.h>
  13.  
  14. #include "SmartObjects.h"
  15.  
  16. // forward declaration
  17. class CAIActionManager;
  18.  
  19. ///////////////////////////////////////////////////
  20. // CAIAction references a Flow Graph - sequence of elementary actions
  21. // which should be executed to "use" a smart object
  22. ///////////////////////////////////////////////////
  23. class CAIAction : public IAIAction
  24. {
  25. protected:
  26.         // CAIActionManager needs right to access and modify these private data members
  27.         friend class CAIActionManager;
  28.  
  29.         // unique name of this AI Action
  30.         string m_Name;
  31.  
  32.         // points to a flow graph which would be used to execute the AI Action
  33.         IFlowGraphPtr m_pFlowGraph;
  34.  
  35.         void NotifyListeners(EActionEvent event)
  36.         {
  37.                 for (TActionListeners::Notifier notifier(m_listeners); notifier.IsValid(); notifier.Next())
  38.                         notifier->OnActionEvent(event);
  39.         }
  40.  
  41. public:
  42.         CAIAction(const CAIAction& src) : m_listeners(1)
  43.         {
  44.                 m_Name = src.m_Name;
  45.                 m_pFlowGraph = src.m_pFlowGraph;
  46.  
  47.                 // [11/11/2010 willw] Do not copy the listeners, as they registered to listen to src not this.
  48.                 //m_listeners = src.m_listeners;
  49.         }
  50.         CAIAction() : m_listeners(1) {}
  51.         virtual ~CAIAction() {}
  52.  
  53.         // traverses all nodes of the underlying flow graph and checks for any nodes that are incompatible when being run in the context of an IAIAction
  54.         virtual bool TraverseAndValidateAction(EntityId idOfUser) const;
  55.  
  56.         // returns the unique name of this AI Action
  57.         virtual const char* GetName() const { return m_Name; }
  58.  
  59.         // returns the goal pipe which executes this AI Action
  60.         virtual IGoalPipe* GetGoalPipe() const { return NULL; };
  61.  
  62.         // returns the Flow Graph associated to this AI Action
  63.         virtual IFlowGraph* GetFlowGraph() const { return m_pFlowGraph; }
  64.  
  65.         // returns the User entity associated to this AI Action
  66.         // no entities could be assigned to base class
  67.         virtual IEntity* GetUserEntity() const { return NULL; }
  68.  
  69.         // returns the Object entity associated to this AI Action
  70.         // no entities could be assigned to base class
  71.         virtual IEntity* GetObjectEntity() const { return NULL; }
  72.  
  73.         // ends execution of this AI Action
  74.         // don't do anything since this action can't be executed
  75.         virtual void EndAction() {}
  76.  
  77.         // cancels execution of this AI Action
  78.         // don't do anything since this action can't be executed
  79.         virtual void CancelAction() {}
  80.  
  81.         // aborts execution of this AI Action - will start clean up procedure
  82.         virtual bool AbortAction() { AIAssert(!"Aborting inactive AI action!"); return false; }
  83.  
  84.         // marks this AI Action as modified
  85.         virtual void Invalidate() { m_pFlowGraph = NULL; }
  86.  
  87.         // removes pointers to entities
  88.         virtual void        OnEntityRemove()                                                     {}
  89.  
  90.         virtual bool        IsSignaledAnimation() const                                          { return false; }
  91.         virtual bool        IsExactPositioning() const                                           { return false; }
  92.         virtual const char* GetAnimationName() const                                             { return NULL; }
  93.  
  94.         virtual const Vec3& GetAnimationPos() const                                              { return Vec3Constants<float>::fVec3_Zero; }
  95.         virtual const Vec3& GetAnimationDir() const                                              { return Vec3Constants<float>::fVec3_OneY; }
  96.         virtual bool        IsUsingAutoAssetAlignment() const                                    { return false; }
  97.  
  98.         virtual const Vec3& GetApproachPos() const                                               { return Vec3Constants<float>::fVec3_Zero; }
  99.  
  100.         virtual float       GetStartWidth() const                                                { return 0; }
  101.         virtual float       GetStartArcAngle() const                                             { return 0; }
  102.         virtual float       GetDirectionTolerance() const                                        { return 0; }
  103.  
  104.         virtual void        RegisterListener(IAIActionListener* eventListener, const char* name) { m_listeners.Add(eventListener, name); }
  105.         virtual void        UnregisterListener(IAIActionListener* eventListener)                 { m_listeners.Remove(eventListener); }
  106.  
  107. private:
  108.  
  109.         typedef CListenerSet<IAIActionListener*> TActionListeners;
  110.  
  111.         TActionListeners m_listeners;
  112.  
  113. };
  114.  
  115. ///////////////////////////////////////////////////
  116. // CAnimationAction references a goal pipe
  117. // which should be executed to "use" a smart object
  118. ///////////////////////////////////////////////////
  119. class CAnimationAction : public IAIAction
  120. {
  121. protected:
  122.         // animation parameters
  123.         bool   bSignaledAnimation;
  124.         bool   bExactPositioning;
  125.         string sAnimationName;
  126.  
  127.         // approaching parameters
  128.         Vec3  vApproachPos;
  129.         float fApproachSpeed;
  130.         int   iApproachStance;
  131.  
  132.         // exact positioning parameters
  133.         Vec3  vAnimationPos;
  134.         Vec3  vAnimationDir;
  135.         float fStartWidth;
  136.         float fDirectionTolerance;
  137.         float fStartArcAngle;
  138.         bool  bAutoTarget;
  139.  
  140.         void NotifyListeners(EActionEvent event)
  141.         {
  142.                 for (TActionListeners::Notifier notifier(m_listeners); notifier.IsValid(); notifier.Next())
  143.                         notifier->OnActionEvent(event);
  144.         }
  145.  
  146. public:
  147.         CAnimationAction(
  148.           const char* animationName,
  149.           bool signaled,
  150.           float approachSpeed,
  151.           int approachStance,
  152.           float startWidth,
  153.           float directionTolerance,
  154.           float startArcAngle)
  155.                 : sAnimationName(animationName)
  156.                 , bSignaledAnimation(signaled)
  157.                 , fApproachSpeed(approachSpeed)
  158.                 , iApproachStance(approachStance)
  159.                 , fStartWidth(startWidth)
  160.                 , fDirectionTolerance(directionTolerance)
  161.                 , fStartArcAngle(startArcAngle)
  162.                 , bExactPositioning(true)
  163.                 , vApproachPos(ZERO)
  164.                 , bAutoTarget(false)
  165.                 , m_listeners(1)
  166.         {}
  167.         CAnimationAction(
  168.           const char* animationName,
  169.           bool signaled)
  170.                 : sAnimationName(animationName)
  171.                 , bSignaledAnimation(signaled)
  172.                 , fApproachSpeed(0.0f)
  173.                 , iApproachStance(0)
  174.                 , fStartWidth(0.0f)
  175.                 , fDirectionTolerance(0.0f)
  176.                 , fStartArcAngle(0.0f)
  177.                 , bExactPositioning(false)
  178.                 , vApproachPos(ZERO)
  179.                 , bAutoTarget(false)
  180.                 , m_listeners(1)
  181.         {}
  182.         virtual ~CAnimationAction() {}
  183.  
  184.         virtual bool TraverseAndValidateAction(EntityId idOfUser) const { return true; }  // CAnimationAction doesn't deal with flow graph, hence no "incompatible" nodes
  185.  
  186.         // returns the unique name of this AI Action
  187.         virtual const char* GetName() const { return NULL; }
  188.  
  189.         // returns the goal pipe which executes this AI Action
  190.         virtual IGoalPipe* GetGoalPipe() const;
  191.  
  192.         // returns the Flow Graph associated to this AI Action
  193.         virtual IFlowGraph* GetFlowGraph() const { return NULL; }
  194.  
  195.         // returns the User entity associated to this AI Action
  196.         // no entities could be assigned to base class
  197.         virtual IEntity* GetUserEntity() const { return NULL; }
  198.  
  199.         // returns the Object entity associated to this AI Action
  200.         // no entities could be assigned to base class
  201.         virtual IEntity* GetObjectEntity() const { return NULL; }
  202.  
  203.         // ends execution of this AI Action
  204.         // don't do anything since this action can't be executed
  205.         virtual void EndAction() {}
  206.  
  207.         // cancels execution of this AI Action
  208.         // don't do anything since this action can't be executed
  209.         virtual void CancelAction() {}
  210.  
  211.         // aborts execution of this AI Action - will start clean up procedure
  212.         virtual bool AbortAction() { AIAssert(!"Aborting inactive AI action!"); return false; }
  213.  
  214.         // marks this AI Action as modified
  215.         virtual void Invalidate() {}
  216.  
  217.         // removes pointers to entities
  218.         virtual void OnEntityRemove() {}
  219.  
  220.         void         SetTarget(const Vec3& position, const Vec3& direction)
  221.         {
  222.                 vAnimationPos = position;
  223.                 vAnimationDir = direction;
  224.         }
  225.  
  226.         void SetAutoTarget(const Vec3& position, const Vec3& direction)
  227.         {
  228.                 vAnimationPos = position;
  229.                 vAnimationDir = direction;
  230.                 bAutoTarget = true;
  231.         }
  232.  
  233.         void SetApproachPos(const Vec3& position)
  234.         {
  235.                 vApproachPos = position;
  236.         }
  237.  
  238.         virtual bool        IsSignaledAnimation() const                                          { return bSignaledAnimation; }
  239.         virtual bool        IsExactPositioning() const                                           { return bExactPositioning; }
  240.         virtual const char* GetAnimationName() const                                             { return sAnimationName; }
  241.  
  242.         virtual const Vec3& GetAnimationPos() const                                              { return vAnimationPos; }
  243.         virtual const Vec3& GetAnimationDir() const                                              { return vAnimationDir; }
  244.         virtual bool        IsUsingAutoAssetAlignment() const                                    { return bAutoTarget; }
  245.  
  246.         virtual const Vec3& GetApproachPos() const                                               { return vApproachPos; }
  247.  
  248.         virtual float       GetStartWidth() const                                                { return fStartWidth; }
  249.         virtual float       GetStartArcAngle() const                                             { return fStartArcAngle; }
  250.         virtual float       GetDirectionTolerance() const                                        { return fDirectionTolerance; }
  251.  
  252.         virtual void        RegisterListener(IAIActionListener* eventListener, const char* name) { m_listeners.Add(eventListener, name); }
  253.         virtual void        UnregisterListener(IAIActionListener* eventListener)                 { m_listeners.Remove(eventListener); }
  254.  
  255. private:
  256.  
  257.         typedef CListenerSet<IAIActionListener*> TActionListeners;
  258.  
  259.         TActionListeners m_listeners;
  260.  
  261. };
  262.  
  263. ///////////////////////////////////////////////////
  264. // CActiveAction represents single active CAIAction
  265. ///////////////////////////////////////////////////
  266. class CActiveAction
  267.         : public CAIAction
  268. {
  269. protected:
  270.         // CAIActionManager needs right to access and modify these private data members
  271.         friend class CAIActionManager;
  272.  
  273.         // entities participants in this AI Action
  274.         IEntity* m_pUserEntity;
  275.         IEntity* m_pObjectEntity;
  276.  
  277.         // AI Action is suspended if this counter isn't zero
  278.         // it shows by how many other AI Actions this was suspended
  279.         int m_SuspendCounter;
  280.  
  281.         // id of goal pipe used for tracking
  282.         int m_idGoalPipe;
  283.  
  284.         // alertness threshold level
  285.         int m_iThreshold;
  286.  
  287.         // next user's and object's states
  288.         string m_nextUserState;
  289.         string m_nextObjectState;
  290.  
  291.         // canceled user's and object's states
  292.         string m_canceledUserState;
  293.         string m_canceledObjectState;
  294.  
  295.         // set to true if action should be deleted on next update
  296.         bool m_bDeleted;
  297.  
  298.         // set to true if action is high priority
  299.         bool m_bHighPriority;
  300.  
  301.         // exact positioning parameters
  302.         bool   m_bSignaledAnimation;
  303.         bool   m_bExactPositioning;
  304.         string m_sAnimationName;
  305.         Vec3   m_vAnimationPos;
  306.         Vec3   m_vAnimationDir;
  307.         Vec3   m_vApproachPos;
  308.         bool   m_bAutoTarget;
  309.         float  m_fStartWidth;
  310.         float  m_fDirectionTolerance;
  311.         float  m_fStartArcAngle;
  312.  
  313. public:
  314.         CActiveAction()
  315.                 : m_vAnimationPos(ZERO)
  316.                 , m_vAnimationDir(ZERO)
  317.                 , m_vApproachPos(ZERO)
  318.                 , m_bSignaledAnimation(0)
  319.                 , m_bExactPositioning(0)
  320.                 , m_bAutoTarget(0)
  321.                 , m_fStartWidth(0)
  322.                 , m_fStartArcAngle(0)
  323.                 , m_fDirectionTolerance(0)
  324.                 , m_pUserEntity(NULL)
  325.                 , m_pObjectEntity(NULL)
  326.                 , m_SuspendCounter(0)
  327.                 , m_idGoalPipe(0)
  328.                 , m_iThreshold(0)
  329.                 , m_bDeleted(false)
  330.                 , m_bHighPriority(false)
  331.         {}
  332.  
  333.         CActiveAction(const CActiveAction& src)
  334.                 : CAIAction()
  335.         {
  336.                 *this = src;
  337.         }
  338.  
  339.         CActiveAction& operator=(const CActiveAction& src)
  340.         {
  341.                 m_Name = src.m_Name;
  342.                 m_pFlowGraph = src.m_pFlowGraph;
  343.  
  344.                 m_pUserEntity = src.m_pUserEntity;
  345.                 m_pObjectEntity = src.m_pObjectEntity;
  346.                 m_SuspendCounter = src.m_SuspendCounter;
  347.                 m_idGoalPipe = src.m_idGoalPipe;
  348.                 m_iThreshold = src.m_iThreshold;
  349.                 m_nextUserState = src.m_nextUserState;
  350.                 m_nextObjectState = src.m_nextObjectState;
  351.                 m_canceledUserState = src.m_canceledUserState;
  352.                 m_canceledObjectState = src.m_canceledObjectState;
  353.                 m_bDeleted = src.m_bDeleted;
  354.                 m_bHighPriority = src.m_bHighPriority;
  355.                 m_bSignaledAnimation = src.m_bSignaledAnimation;
  356.                 m_bExactPositioning = src.m_bExactPositioning;
  357.                 m_sAnimationName = src.m_sAnimationName;
  358.                 m_vAnimationPos = src.m_vAnimationPos;
  359.                 m_vAnimationDir = src.m_vAnimationDir;
  360.                 m_vApproachPos = src.m_vApproachPos;
  361.                 m_bAutoTarget = src.m_bAutoTarget;
  362.                 m_fStartWidth = src.m_fStartWidth;
  363.                 m_fStartArcAngle = src.m_fStartArcAngle;
  364.                 m_fDirectionTolerance = src.m_fDirectionTolerance;
  365.                 return *this;
  366.         }
  367.  
  368.         // returns the User entity associated to this AI Action
  369.         virtual IEntity* GetUserEntity() const { return m_pUserEntity; }
  370.  
  371.         // returns the Object entity associated to this AI Action
  372.         virtual IEntity* GetObjectEntity() const { return m_pObjectEntity; }
  373.  
  374.         // returns true if action is active and marked as high priority
  375.         virtual bool IsHighPriority() const { return m_bHighPriority; }
  376.  
  377.         // ends execution of this AI Action
  378.         virtual void EndAction();
  379.  
  380.         // cancels execution of this AI Action
  381.         virtual void CancelAction();
  382.  
  383.         // aborts execution of this AI Action - will start clean up procedure
  384.         virtual bool AbortAction();
  385.  
  386.         // removes pointers to entities
  387.         virtual void        OnEntityRemove() { m_pUserEntity = m_pObjectEntity = NULL; }
  388.  
  389.         bool                operator==(const CActiveAction& other) const;
  390.  
  391.         virtual bool        IsSignaledAnimation() const       { return m_bSignaledAnimation; }
  392.         virtual bool        IsExactPositioning() const        { return m_bExactPositioning; }
  393.         virtual const char* GetAnimationName() const          { return m_sAnimationName; }
  394.  
  395.         virtual const Vec3& GetAnimationPos() const           { return m_vAnimationPos; }
  396.         virtual const Vec3& GetAnimationDir() const           { return m_vAnimationDir; }
  397.         virtual bool        IsUsingAutoAssetAlignment() const { return m_bAutoTarget; }
  398.  
  399.         virtual const Vec3& GetApproachPos() const            { return m_vApproachPos; }
  400.  
  401.         virtual float       GetStartWidth() const             { return m_fStartWidth; }
  402.         virtual float       GetStartArcAngle() const          { return m_fStartArcAngle; }
  403.         virtual float       GetDirectionTolerance() const     { return m_fDirectionTolerance; }
  404.  
  405.         void                Serialize(TSerialize ser);
  406. };
  407.  
  408. #undef LoadLibrary
  409. ///////////////////////////////////////////////////
  410. // CAIActionManager keeps track of all AIActions
  411. ///////////////////////////////////////////////////
  412. class CAIActionManager : public IAIActionManager
  413. {
  414. private:
  415.         // library of all defined AI Actions
  416.         typedef std::map<string, CAIAction> TActionsLib;
  417.         TActionsLib m_ActionsLib;
  418.  
  419.         // list of all active AI Actions (including suspended)
  420.         typedef std::list<CActiveAction> TActiveActions;
  421.         TActiveActions m_ActiveActions;
  422.  
  423. protected:
  424.         // suspends all active AI Actions in which the entity is involved
  425.         // note: it's safe to pass pEntity == NULL
  426.         int SuspendActionsOnEntity(IEntity* pEntity, int goalPipeId, const IAIAction* pAction, bool bHighPriority, int& numHigherPriority);
  427.  
  428.         // resumes all active AI Actions in which the entity is involved
  429.         // (resuming depends on it how many times it was suspended)
  430.         // note: it's safe to pass pEntity == NULL
  431.         void ResumeActionsOnEntity(IEntity* pEntity, int goalPipeId);
  432.  
  433.         // implementing IGoalPipeListener interface
  434.         virtual void OnGoalPipeEvent(IPipeUser* pPipeUser, EGoalPipeEvent event, int goalPipeId, bool& unregisterListenerAfterEvent);
  435.  
  436. public:
  437.         CAIActionManager();
  438.         virtual ~CAIActionManager();
  439.  
  440.         // stops all active actions
  441.         void Reset();
  442.  
  443.         // returns an existing AI Action from the library or NULL if not found
  444.         virtual IAIAction* GetAIAction(const char* sName);
  445.  
  446.         // returns an existing AI Action by its index in the library or NULL index is out of range
  447.         virtual IAIAction* GetAIAction(size_t index);
  448.  
  449.         virtual void       ReloadActions();
  450.  
  451.         // returns an existing AI Action by its name specified in the rule
  452.         // or creates a new temp. action for playing the animation specified in the rule
  453.         IAIAction* GetAIAction(const CCondition* pRule);
  454.  
  455.         // adds an AI Action in the list of active actions
  456.         void ExecuteAIAction(const IAIAction* pAction, IEntity* pUser, IEntity* pObject, int maxAlertness, int goalPipeId,
  457.                              const char* userState, const char* objectState, const char* userCanceledState, const char* objectCanceledState, IAIAction::IAIActionListener* pListener = NULL);
  458.  
  459.         /// Looks up the action by name and returns a handle to it.
  460.         void ExecuteAIAction(const char* sActionName, IEntity* pUser, IEntity* pObject, int maxAlertness, int goalPipeId, IAIAction::IAIActionListener* pListener = NULL);
  461.  
  462.         // aborts specific AI Action (specified by goalPipeId) or all AI Actions (goalPipeId == 0) in which pEntity is a user
  463.         virtual void AbortAIAction(IEntity* pEntity, int goalPipeId = 0);
  464.  
  465.         // finishes specific AI Action (specified by goalPipeId) for the pEntity as a user
  466.         virtual void FinishAIAction(IEntity* pEntity, int goalPipeId);
  467.  
  468.         // marks AI Action from the list of active actions as deleted
  469.         void ActionDone(CActiveAction& action, bool bRemoveAction = true);
  470.  
  471.         // removes deleted AI Action from the list of active actions
  472.         void Update();
  473.  
  474.         // loads the library of AI Action Flow Graphs
  475.         void LoadLibrary(const char* sPath);
  476.  
  477.         // notification sent by smart objects system
  478.         void OnEntityRemove(IEntity* pEntity);
  479.  
  480.         void Serialize(TSerialize ser);
  481. };
  482.  
  483. #endif
  484.  
downloadAIActions.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