BVB Source Codes

CRYENGINE Show AIFaceManager.cpp Source code

Return Download CRYENGINE: download AIFaceManager.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /********************************************************************
  4.    CryGame Source File.
  5.    -------------------------------------------------------------------------
  6.    File name:   AIFaceManager.cpp
  7.    Version:     v1.00
  8.    Description:
  9.  
  10.    -------------------------------------------------------------------------
  11.    History:
  12.    - 05:05:2007   12:05 : Created by Kirill Bulatsev
  13.  
  14.  *********************************************************************/
  15. #include "StdAfx.h"
  16. #include "AIFaceManager.h"
  17. #include <CryAnimation/ICryAnimation.h>
  18. #include <CryNetwork/ISerialize.h>
  19. #include <IActorSystem.h>
  20.  
  21. CAIFaceManager::TExprState CAIFaceManager::s_Expressions[EE_Count + 1];
  22. //
  23. //------------------------------------------------------------------------------
  24. //
  25. //------------------------------------------------------------------------------
  26. CAIFaceManager::CAIFaceManager(IEntity* pEntity) :
  27.         m_pEntity(pEntity),
  28.         m_CurrentState(EE_IDLE),
  29.         m_CurrentExprIdx(0),
  30.         m_ExprStartTime(0.f),
  31.         m_ChangeExpressionTimeMs(0)
  32. {
  33.  
  34. }
  35.  
  36. //
  37. //------------------------------------------------------------------------------
  38. CAIFaceManager::~CAIFaceManager(void)
  39. {
  40.  
  41. }
  42.  
  43. //
  44. //------------------------------------------------------------------------------
  45. void CAIFaceManager::OnReused(IEntity* pEntity)
  46. {
  47.         m_pEntity = pEntity;
  48.  
  49.         Reset();
  50. }
  51.  
  52. //
  53. //------------------------------------------------------------------------------
  54. int CAIFaceManager::SelectExpressionTime() const
  55. {
  56.         return cry_random(30000, 50000);
  57. }
  58.  
  59. //
  60. //------------------------------------------------------------------------------
  61. void CAIFaceManager::Reset()
  62. {
  63.         m_CurrentState = EE_IDLE;
  64.         m_CurrentExprIdx = 0;
  65.         m_ExprStartTime = 0.f;
  66. }
  67.  
  68. //
  69. //------------------------------------------------------------------------------
  70. void CAIFaceManager::PrecacheSequences()
  71. {
  72.         IActorSystem* pASystem = gEnv->pGameFramework->GetIActorSystem();
  73.         IActor* pActor = (pASystem && m_pEntity ? pASystem->GetActor(m_pEntity->GetId()) : 0);
  74.  
  75.         for (e_ExpressionEvent eventType = e_ExpressionEvent(0); eventType <= EE_Count; eventType = e_ExpressionEvent(eventType + 1))
  76.         {
  77.                 const TExprState& stateSequences = s_Expressions[eventType];
  78.                 for (int stateSequenceIndex = 0, stateSequenceCount = stateSequences.size(); stateSequenceIndex < stateSequenceCount; ++stateSequenceIndex)
  79.                 {
  80.                         const string& expressionName = stateSequences[stateSequenceIndex];
  81.  
  82.                         if (pActor)
  83.                                 pActor->PrecacheFacialExpression(expressionName.c_str());
  84.                 }
  85.         }
  86. }
  87.  
  88. //
  89. //------------------------------------------------------------------------------
  90. bool CAIFaceManager::LoadStatic()
  91. {
  92.         stl::for_each_array(s_Expressions, stl::container_freer());
  93.         string sPath = PathUtil::Make("Libs/Readability/Faces", "Faces.xml");
  94.         return Load(sPath);
  95.         /*
  96.            m_SoundPacks.clear();
  97.            //   Load("korean_01.xml");
  98.  
  99.            string path( "Libs/Readability/Sound" );
  100.            ICryPak * pCryPak = gEnv->pCryPak;
  101.            _finddata_t fd;
  102.            string fileName;
  103.  
  104.            string searchPath(path + "/*.xml");
  105.            intptr_t handle = pCryPak->FindFirst( searchPath.c_str(), &fd );
  106.            if (handle != -1)
  107.            {
  108.             do
  109.             {
  110.               fileName = path;
  111.               fileName += "/" ;
  112.               fileName += fd.name;
  113.               Load(fileName);
  114.             } while ( pCryPak->FindNext( handle, &fd ) >= 0 );
  115.             pCryPak->FindClose( handle );
  116.            }
  117.            m_reloadID++;
  118.          */
  119. }
  120.  
  121. //
  122. //----------------------------------------------------------------------------------------------------------
  123. bool CAIFaceManager::Load(const char* szPackName)
  124. {
  125.         string sPath = PathUtil::Make("Libs/Readability/Faces", "Faces.xml");
  126.  
  127.         XmlNodeRef root = GetISystem()->LoadXmlFromFile(sPath);
  128.         if (!root)
  129.                 return false;
  130.  
  131.         XmlNodeRef nodeWorksheet = root->findChild("Worksheet");
  132.         if (!nodeWorksheet)
  133.                 return false;
  134.  
  135.         XmlNodeRef nodeTable = nodeWorksheet->findChild("Table");
  136.         if (!nodeTable)
  137.                 return false;
  138.  
  139.         e_ExpressionEvent curExpression(EE_NONE);
  140.         const char* szSignal = 0;
  141.         for (int rowCntr = 0, childN = 0; childN < nodeTable->getChildCount(); ++childN)
  142.         {
  143.                 XmlNodeRef nodeRow = nodeTable->getChild(childN);
  144.                 if (!nodeRow->isTag("Row"))
  145.                         continue;
  146.                 ++rowCntr;
  147.                 if (rowCntr == 1) // Skip the first row, it only have description/header.
  148.                         continue;
  149.                 const char* szFaceName = 0;
  150.  
  151.                 for (int childrenCntr = 0, cellIndex = 1; childrenCntr < nodeRow->getChildCount(); ++childrenCntr, ++cellIndex)
  152.                 {
  153.                         XmlNodeRef nodeCell = nodeRow->getChild(childrenCntr);
  154.                         if (!nodeCell->isTag("Cell"))
  155.                                 continue;
  156.  
  157.                         if (nodeCell->haveAttr("ss:Index"))
  158.                         {
  159.                                 cellIndex = 0;
  160.                                 nodeCell->getAttr("ss:Index", cellIndex);
  161.                         }
  162.                         XmlNodeRef nodeCellData = nodeCell->findChild("Data");
  163.                         if (!nodeCellData)
  164.                                 continue;
  165.  
  166.                         switch (cellIndex)
  167.                         {
  168.                         case 1:   // Readability Signal name
  169.                                 szSignal = nodeCellData->getContent();
  170.                                 curExpression = StringToEnum(szSignal);
  171.                                 continue;
  172.                         case 2:   // The expression name
  173.                                 szFaceName = nodeCellData->getContent();
  174.                                 break;
  175.                         }
  176.                 }
  177.                 if (szFaceName != NULL)
  178.                         s_Expressions[curExpression].push_back(szFaceName);
  179.         }
  180.         return true;
  181. }
  182.  
  183. //
  184. //------------------------------------------------------------------------------
  185. CAIFaceManager::e_ExpressionEvent CAIFaceManager::StringToEnum(const char* szFaceName)
  186. {
  187.         if (!strcmp(szFaceName, "IDLE"))
  188.                 return EE_IDLE;
  189.         else if (!strcmp(szFaceName, "ALERT"))
  190.                 return EE_ALERT;
  191.         else if (!strcmp(szFaceName, "COMBAT"))
  192.                 return EE_COMBAT;
  193.  
  194.         return EE_NONE;
  195. }
  196.  
  197. //
  198. //------------------------------------------------------------------------------
  199. void CAIFaceManager::Update()
  200. {
  201.         if (m_CurrentState == EE_NONE)
  202.         {
  203.                 MakeFace(NULL);
  204.                 return;
  205.         }
  206.         int64 timePassedMs((gEnv->pTimer->GetFrameStartTime() - m_ExprStartTime).GetMilliSecondsAsInt64());
  207.         if (timePassedMs > m_ChangeExpressionTimeMs)
  208.                 SetExpression(m_CurrentState, true);
  209. }
  210.  
  211. //
  212. //------------------------------------------------------------------------------
  213. void CAIFaceManager::SetExpression(e_ExpressionEvent expression, bool forceChange)
  214. {
  215.         if (m_CurrentState == expression && !forceChange)
  216.                 return;
  217.  
  218.         assert(expression >= 0 && expression < EE_Count);
  219.         TExprState& theState = s_Expressions[expression];
  220.         if (!theState.empty())
  221.         {
  222.                 m_CurrentExprIdx = cry_random(0, (int)theState.size() - 1);
  223.                 MakeFace(theState[m_CurrentExprIdx]);
  224.         }
  225.         m_ExprStartTime = gEnv->pTimer->GetFrameStartTime();
  226.         m_CurrentState = expression;
  227. }
  228.  
  229. //
  230. //------------------------------------------------------------------------------
  231. void CAIFaceManager::MakeFace(const char* pFaceName)
  232. {
  233.         IActorSystem* pASystem = gEnv->pGameFramework->GetIActorSystem();
  234.         if (pASystem)
  235.         {
  236.                 IActor* pActor = pASystem->GetActor(m_pEntity->GetId());
  237.                 if (pActor)
  238.                 {
  239.                         f32 length = -1.0f;
  240.                         pActor->RequestFacialExpression(pFaceName, &length);
  241.                         if (length > 0.0f)
  242.                                 m_ChangeExpressionTimeMs = (uint32)(length * 1000.0f);
  243.                 }
  244.         }
  245. }
  246.  
  247. //----------------------------------------------------------------------------------------------------------
  248. //
  249. //
  250. //----------------------------------------------------------------------------------------------------------
  251.  
downloadAIFaceManager.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