BVB Source Codes

CRYENGINE Show Sequence.cpp Source code

Return Download CRYENGINE: download Sequence.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 "Sequence.h"
  5. #include "SequenceAgent.h"
  6. #include "SequenceFlowNodes.h"
  7. #include "AIBubblesSystem/AIBubblesSystem.h"
  8.  
  9. namespace
  10. {
  11. typedef std::set<TFlowNodeId> FlowNodeIdSet;
  12. }
  13.  
  14. namespace AIActionSequence
  15. {
  16.  
  17. bool Sequence::TraverseAndValidateSequence()
  18. {
  19.         assert(m_flowGraph);
  20.         if (!m_flowGraph)
  21.                 return false;
  22.  
  23.         m_actionNodeIds.clear();
  24.  
  25.         FlowNodeIdSet nodesToVisit;
  26.         FlowNodeIdSet visitedNodes;
  27.         nodesToVisit.insert(m_startNodeId);
  28.  
  29.         IFlowEdgeIteratorPtr edgeIterator;
  30.         IFlowEdgeIterator::Edge edge;
  31.  
  32.         while (!nodesToVisit.empty())
  33.         {
  34.                 FlowNodeIdSet::iterator it = nodesToVisit.begin();
  35.                 TFlowNodeId visitingNodeId = *it;
  36.  
  37.                 SFlowNodeConfig visitingNodeConfig;
  38.                 m_flowGraph->GetNodeConfiguration(visitingNodeId, visitingNodeConfig);
  39.                 uint32 typeFlags = visitingNodeConfig.GetTypeFlags();
  40.  
  41.                 if (!(typeFlags & EFLN_AISEQUENCE_ACTION || typeFlags & EFLN_AISEQUENCE_SUPPORTED || typeFlags & EFLN_AISEQUENCE_END))
  42.                 {
  43.                         CryStackStringT<char, 256> message;
  44.                         message.Format("AI Sequence failed to register. It contains a flownode not supported to by the AI Sequence system : %s", m_flowGraph->GetNodeTypeName(visitingNodeId));
  45.                         AIQueueBubbleMessage("AI Sequence - Flownode not supported", m_entityId, message, eBNS_LogWarning | eBNS_Balloon | eBNS_BlockingPopup);
  46.                         return false;
  47.                 }
  48.  
  49.                 const EntityId entityIdFromNode = GetEntityIdFromNode(visitingNodeId);
  50.  
  51.                 if ((typeFlags & EFLN_AISEQUENCE_ACTION || typeFlags & EFLN_AISEQUENCE_END) && entityIdFromNode == m_entityId)
  52.                 {
  53.                         m_actionNodeIds.push_back(visitingNodeId);
  54.                 }
  55.  
  56.                 visitedNodes.insert(visitingNodeId);
  57.                 nodesToVisit.erase(it);
  58.  
  59.                 // Don't check connection past the end nodes
  60.                 if (typeFlags & EFLN_AISEQUENCE_END)
  61.                         continue;
  62.  
  63.                 edgeIterator = m_flowGraph->CreateEdgeIterator();
  64.                 while (edgeIterator->Next(edge))
  65.                 {
  66.                         if (edge.fromNodeId != visitingNodeId)
  67.                                 continue;
  68.  
  69.                         FlowNodeIdSet::iterator nodesIt = visitedNodes.find(edge.toNodeId);
  70.                         if (nodesIt != visitedNodes.end())
  71.                                 continue;
  72.  
  73.                         nodesToVisit.insert(edge.toNodeId);
  74.                 }
  75.         }
  76.  
  77.         AssignSequenceToActionNodes();
  78.         return true;
  79. }
  80.  
  81. void Sequence::Start()
  82. {
  83.         assert(!m_active);
  84.         PrepareAgentSequenceBehavior();
  85.         m_active = true;
  86.         m_bookmarkNodeId = 0;
  87. }
  88.  
  89. void Sequence::PrepareAgentSequenceBehavior()
  90. {
  91.         SequenceAgent agent(m_entityId);
  92.         agent.SetSequenceBehavior(m_sequenceProperties.interruptible);
  93.  
  94.         if (agent.IsRunningSequenceBehavior(m_sequenceProperties.interruptible))
  95.         {
  96.                 agent.ClearGoalPipe();
  97.                 SequenceBehaviorReady();
  98.         }
  99. }
  100.  
  101. void Sequence::SequenceBehaviorReady()
  102. {
  103.         if (m_bookmarkNodeId)
  104.         {
  105.                 SendEventToNode(TriggerBookmark, m_bookmarkNodeId);
  106.         }
  107.         else
  108.         {
  109.                 SendEventToNode(StartSequence, m_startNodeId);
  110.         }
  111. }
  112.  
  113. void Sequence::SequenceInterruptibleBehaviorLeft()
  114. {
  115.         if (m_sequenceProperties.resumeAfterInterrupt)
  116.         {
  117.                 Stop();
  118.         }
  119.         else
  120.         {
  121.                 Cancel();
  122.         }
  123. }
  124.  
  125. void Sequence::Stop()
  126. {
  127.         if (m_currentActionNodeId != InvalidFlowNodeId)
  128.                 SendEventToNode(SequenceStopped, m_currentActionNodeId);
  129.  
  130.         m_currentActionNodeId = InvalidFlowNodeId;
  131. }
  132.  
  133. void Sequence::Cancel()
  134. {
  135.         Stop();
  136.         m_active = false;
  137.         SequenceAgent agent(m_entityId);
  138.         agent.ClearSequenceBehavior();
  139. }
  140.  
  141. void Sequence::RequestActionStart(TFlowNodeId actionNodeId)
  142. {
  143.         CRY_ASSERT_MESSAGE(actionNodeId != InvalidFlowNodeId, "Sequence::RequestActionStart: clash between passed in actionNodeId and the magic value that specifies an invalid node");
  144.  
  145.         if (!m_active || actionNodeId == m_currentActionNodeId)
  146.                 return;
  147.  
  148.         if (m_currentActionNodeId == InvalidFlowNodeId)
  149.         {
  150.                 m_currentActionNodeId = actionNodeId;
  151.                 SendEventToNode(StartAction, actionNodeId);
  152.         }
  153.         else
  154.         {
  155.                 CryStackStringT<char, 256> message;
  156.                 message.Format("AI Sequence action (%s) failed to start. There is already an active action (%s) running in this sequence.", m_flowGraph->GetNodeTypeName(actionNodeId), m_flowGraph->GetNodeTypeName(m_currentActionNodeId));
  157.                 AIQueueBubbleMessage("AI Sequence - Action failed to start", m_entityId, message, eBNS_LogWarning | eBNS_Balloon | eBNS_BlockingPopup);
  158.         }
  159. }
  160.  
  161. void Sequence::ActionComplete()
  162. {
  163.         SequenceAgent agent(m_entityId);
  164.         agent.ClearGoalPipe();
  165.         m_currentActionNodeId = InvalidFlowNodeId;
  166. }
  167.  
  168. void Sequence::SetBookmark(TFlowNodeId bookmarkNodeId)
  169. {
  170.         if (m_active)
  171.         {
  172.                 m_bookmarkNodeId = bookmarkNodeId;
  173.         }
  174. }
  175.  
  176. void Sequence::AssignSequenceToActionNodes()
  177. {
  178.         FlowNodeIdVector::iterator it = m_actionNodeIds.begin();
  179.         FlowNodeIdVector::iterator itEnd = m_actionNodeIds.end();
  180.         for (; it != itEnd; ++it)
  181.         {
  182.                 AssignSequenceToNode(*it);
  183.         }
  184. }
  185.  
  186. void Sequence::AssignSequenceToNode(TFlowNodeId nodeId)
  187. {
  188.         IFlowNodeData* flowNodeData = m_flowGraph->GetNodeData(nodeId);
  189.         assert(flowNodeData);
  190.         SequenceFlowNodeBase* flowNode = static_cast<SequenceFlowNodeBase*>(flowNodeData->GetNode());
  191.         assert(flowNode);
  192.         flowNode->AssignSequence(m_sequenceId, m_entityId);
  193. }
  194.  
  195. void Sequence::SendEventToNode(SequenceEvent event, TFlowNodeId nodeId)
  196. {
  197.         IFlowNodeData* flowNodeData = m_flowGraph->GetNodeData(nodeId);
  198.         assert(flowNodeData);
  199.         SequenceFlowNodeBase* flowNode = static_cast<SequenceFlowNodeBase*>(flowNodeData->GetNode());
  200.         assert(flowNode);
  201.         flowNode->HandleSequenceEvent(event);
  202. }
  203.  
  204. EntityId Sequence::GetEntityIdFromNode(TFlowNodeId nodeId) const
  205. {
  206.         IFlowNodeData* flowNodeData = m_flowGraph->GetNodeData(nodeId);
  207.         assert(flowNodeData);
  208.         if (!flowNodeData)
  209.                 return 0;
  210.  
  211.         EntityId entityId = flowNodeData->GetCurrentForwardingEntity();
  212.         if (entityId == (EntityId)0)
  213.         {
  214.                 entityId = m_flowGraph->GetEntityId(nodeId);
  215.                 // If the sequence is using the "graph entity" as entity id of the nodes
  216.                 // then we need to call the GetGraphEntity to retrieve the correct entity id
  217.                 if (entityId == EFLOWNODE_ENTITY_ID_GRAPH1 || entityId == EFLOWNODE_ENTITY_ID_GRAPH2)
  218.                 {
  219.                         const int entityIndex = EFLOWNODE_ENTITY_ID_GRAPH1 ? 0 : 1;
  220.                         entityId = m_flowGraph->GetGraphEntity(entityIndex);
  221.                 }
  222.         }
  223.  
  224.         return entityId;
  225. }
  226.  
  227. } // namespace AIActionSequence
  228.  
downloadSequence.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