BVB Source Codes

CRYENGINE Show CommunicationPlayer.cpp Source code

Return Download CRYENGINE: download CommunicationPlayer.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 "CommunicationPlayer.h"
  5. #include "CommunicationManager.h"
  6. #include "Puppet.h"
  7.  
  8. void CommunicationPlayer::PlayState::OnCommunicationEvent(
  9.   IAICommunicationHandler::ECommunicationHandlerEvent event, EntityId actorID)
  10. {
  11.         const uint8 prevFlags = finishedFlags;
  12.  
  13.         switch (event)
  14.         {
  15.         case IAICommunicationHandler::ActionStarted:
  16.         case IAICommunicationHandler::SignalStarted:
  17.         case IAICommunicationHandler::SoundStarted:
  18.         case IAICommunicationHandler::VoiceStarted:
  19.                 //case IAICommunicationHandler::ActionCancelled:
  20.                 break;
  21.         case IAICommunicationHandler::ActionFailed:
  22.         case IAICommunicationHandler::SignalFailed:
  23.         case IAICommunicationHandler::SignalCancelled:
  24.         case IAICommunicationHandler::SignalFinished:
  25.                 finishedFlags |= FinishedAnimation;
  26.                 break;
  27.         case IAICommunicationHandler::SoundFailed:
  28.         case IAICommunicationHandler::SoundFinished:
  29.         case IAICommunicationHandler::SoundCancelled:
  30.                 finishedFlags |= FinishedSound;
  31.                 break;
  32.         case IAICommunicationHandler::VoiceFailed:
  33.         case IAICommunicationHandler::VoiceFinished:
  34.         case IAICommunicationHandler::VoiceCancelled:
  35.                 finishedFlags |= FinishedVoice;
  36.                 break;
  37.         default:
  38.                 break;
  39.         }
  40.  
  41.         if (gAIEnv.CVars.DebugDrawCommunication == 5 && prevFlags != finishedFlags)
  42.         {
  43.                 if (finishedFlags != FinishedAll)
  44.                 {
  45.                         CryLogAlways("CommunicationPlayer::PlayState: Still waiting for %x to finish commID[%u]", ~finishedFlags & FinishedAll, commID.id);
  46.                 }
  47.                 else
  48.                 {
  49.                         CryLogAlways("CommunicationPlayer::PlayState: All finished! commID[%u]", commID.id);
  50.                 }
  51.         }
  52. }
  53.  
  54. void CommunicationPlayer::Reset()
  55. {
  56.         PlayingCommunications::iterator it = m_playing.begin();
  57.         PlayingCommunications::iterator end = m_playing.end();
  58.  
  59.         for (; it != end; ++it)
  60.         {
  61.                 PlayState& playState = it->second;
  62.  
  63.                 IEntity* entity = gEnv->pEntitySystem->GetEntity(playState.actorID);
  64.                 if (!entity)
  65.                         continue;
  66.  
  67.                 IAIObject* aiObject = entity->GetAI();
  68.                 if (!aiObject)
  69.                         continue;
  70.  
  71.                 IAIActorProxy* aiProxy = aiObject->GetProxy();
  72.                 if (!aiProxy)
  73.                         continue;
  74.  
  75.                 IAICommunicationHandler* commHandler = aiProxy->GetCommunicationHandler();
  76.  
  77.                 if (!playState.animationName.empty())
  78.                 {
  79.                         IAICommunicationHandler::EAnimationMethod method = (playState.flags & SCommunication::AnimationAction)
  80.                                                                            ? IAICommunicationHandler::AnimationMethodAction : IAICommunicationHandler::AnimationMethodSignal;
  81.  
  82.                         commHandler->StopAnimation(it->first, playState.animationName.c_str(), method);
  83.                 }
  84.  
  85.                 if (playState.soundInfo.playSoundControlId != INVALID_AUDIO_CONTROL_ID)
  86.                         commHandler->StopSound(playState.soundInfo);
  87.  
  88.                 if (playState.voiceInfo.playSoundControlId != INVALID_AUDIO_CONTROL_ID)
  89.                         commHandler->StopVoice(playState.voiceInfo);
  90.         }
  91.  
  92.         m_playing.clear();
  93. }
  94.  
  95. void CommunicationPlayer::Clear()
  96. {
  97.         Reset();
  98. }
  99.  
  100. void CommunicationPlayer::OnCommunicationHandlerEvent(
  101.   IAICommunicationHandler::ECommunicationHandlerEvent event, CommPlayID playID, EntityId actorID)
  102. {
  103.         PlayingCommunications::iterator stateIt = m_playing.find(playID);
  104.         if (stateIt != m_playing.end())
  105.         {
  106.                 stateIt->second.OnCommunicationEvent(event, actorID);
  107.         }
  108. }
  109.  
  110. void CommunicationPlayer::BlockingSettings(IAIObject* aiObject, PlayState& playState, bool set)
  111. {
  112.         if (aiObject)
  113.         {
  114.                 if (playState.flags & SCommunication::BlockAll)
  115.                 {
  116.                         if (playState.flags & SCommunication::BlockMovement)
  117.                         {
  118.                                 if (CPipeUser* pipeUser = aiObject->CastToCPipeUser())
  119.                                 {
  120.                                         pipeUser->Pause(set);
  121.                                 }
  122.                         }
  123.                         if (playState.flags & SCommunication::BlockFire)
  124.                         {
  125.                                 if (CPuppet* puppet = aiObject->CastToCPuppet())
  126.                                 {
  127.                                         puppet->EnableFire(!set);
  128.                                 }
  129.                         }
  130.                 }
  131.         }
  132. }
  133.  
  134. bool CommunicationPlayer::Play(const CommPlayID& playID, const SCommunicationRequest& request,
  135.                                const SCommunication& comm, uint32 variationIdx, ICommunicationFinishedListener* listener,
  136.                                void* param)
  137. {
  138.         // Only return false when playing could not happen due to congestion
  139.         // If case of unrecoverable error return true to avoid the queue getting full
  140.         bool failed = true;
  141.  
  142.         IAIObject* aiObject = 0;
  143.         IAIActorProxy* aiProxy = 0;
  144.  
  145.         if (IEntity* entity = gEnv->pEntitySystem->GetEntity(request.actorID))
  146.                 if (aiObject = entity->GetAI())
  147.                         if (aiProxy = aiObject->GetProxy())
  148.                                 failed = false;
  149.  
  150.         assert(variationIdx < comm.variations.size());
  151.         const SCommunicationVariation& variation = comm.variations[variationIdx];
  152.  
  153.         PlayState& playState = stl::map_insert_or_get(m_playing, playID,
  154.                                                       PlayState(request, variation, listener, param));
  155.  
  156.         if (failed)
  157.         {
  158.                 playState.finishedFlags = PlayState::FinishedAll;
  159.                 return false;
  160.         }
  161.  
  162.         assert(playState.animationName.empty());
  163.  
  164.         assert(aiProxy);//shut up SCA
  165.         IAICommunicationHandler* commHandler = aiProxy->GetCommunicationHandler();
  166.  
  167.         if (!variation.soundName.empty() && !request.skipCommSound)
  168.         {
  169.                 playState.soundInfo = commHandler->PlaySound(playID, variation.soundName.c_str(),
  170.                                                              (variation.flags & SCommunication::FinishSound) ? this : 0);
  171.  
  172.                 if (playState.soundInfo.playSoundControlId == INVALID_AUDIO_CONTROL_ID)
  173.                 {
  174.                         AIWarning("Failed to play communication sound '%s'...", variation.soundName.c_str());
  175.  
  176.                         playState.finishedFlags |= PlayState::FinishedSound;
  177.                 }
  178.         }
  179.         else
  180.                 playState.finishedFlags |= PlayState::FinishedSound;
  181.  
  182.         if (!variation.voiceName.empty() && !request.skipCommSound)
  183.         {
  184.                 playState.voiceInfo = commHandler->PlayVoice(playID, variation.voiceName.c_str(),
  185.                                                              (variation.flags & SCommunication::FinishVoice) ? this : 0);
  186.  
  187.                 if (playState.voiceInfo.playSoundControlId == INVALID_AUDIO_CONTROL_ID)
  188.                 {
  189.                         AIWarning("Failed to play communication voice '%s'...", variation.voiceName.c_str());
  190.  
  191.                         playState.finishedFlags |= PlayState::FinishedVoice;
  192.                 }
  193.         }
  194.         else
  195.                 playState.finishedFlags |= PlayState::FinishedVoice;
  196.  
  197.         if (!variation.animationName.empty() && !request.skipCommAnimation)
  198.         {
  199.                 playState.animationName = variation.animationName;
  200.  
  201.                 IAICommunicationHandler::EAnimationMethod method = (variation.flags & SCommunication::AnimationAction)
  202.                                                                    ? IAICommunicationHandler::AnimationMethodAction : IAICommunicationHandler::AnimationMethodSignal;
  203.  
  204.                 IAICommunicationHandler::IEventListener* listener2 = (playState.flags & SCommunication::FinishAnimation) ? this : 0;
  205.                 commHandler->PlayAnimation(playID, variation.animationName.c_str(), method, listener2);
  206.         }
  207.         else
  208.                 playState.finishedFlags |= PlayState::FinishedAnimation;
  209.  
  210.         if (playState.timeout <= 0.000001f)
  211.                 playState.finishedFlags |= PlayState::FinishedTimeout;
  212.  
  213.         BlockingSettings(aiObject, playState, true);
  214.  
  215.         return true;
  216. }
  217.  
  218. void CommunicationPlayer::Update(float updateTime)
  219. {
  220.         PlayingCommunications::iterator it = m_playing.begin();
  221.         PlayingCommunications::iterator end = m_playing.end();
  222.  
  223.         for (; it != end; )
  224.         {
  225.                 PlayState& playState = it->second;
  226.                 const CommPlayID& playId = it->first;
  227.  
  228.                 if (!UpdatePlaying(playId, playState, updateTime))
  229.                 {
  230.                         CleanUpPlayState(playId, playState);
  231.  
  232.                         PlayingCommunications::iterator erased = it++;
  233.  
  234.                         if (gAIEnv.CVars.DebugDrawCommunication == 5)
  235.                                 CryLogAlways("CommunicationPlayer removed finished: %s[%u] as playID[%u] with listener[%p]", gAIEnv.pCommunicationManager->GetCommunicationName(erased->second.commID), erased->second.commID.id, erased->first.id, erased->second.listener);
  236.  
  237.                         m_playing.erase(erased);
  238.  
  239.                         continue;
  240.                 }
  241.                 ++it;
  242.         }
  243. }
  244.  
  245. bool CommunicationPlayer::UpdatePlaying(const CommPlayID& playId, PlayState& playState, float updateTime)
  246. {
  247.  
  248.         // Only return false when playing could not happen due to congestion
  249.         // If case of unrecoverable error return true to avoid the queue getting full
  250.         IEntity* entity = gEnv->pEntitySystem->GetEntity(playState.actorID);
  251.         if (!entity)
  252.                 return false;
  253.  
  254.         IAIObject* aiObject = entity->GetAI();
  255.         if (!aiObject)
  256.                 return false;
  257.  
  258.         if (playState.flags & SCommunication::LookAtTarget)
  259.         {
  260.                 if (playState.targetID)
  261.                 {
  262.                         if (IEntity* targetEntity = gEnv->pEntitySystem->GetEntity(playState.targetID))
  263.                                 playState.target = targetEntity->GetWorldPos();
  264.                 }
  265.  
  266.                 if (!playState.target.IsZeroFast())
  267.                 {
  268.                         if (CPipeUser* pipeUser = aiObject->CastToCPipeUser())
  269.                         {
  270.                                 pipeUser->SetLookStyle(LOOKSTYLE_HARD_NOLOWER);
  271.                                 pipeUser->SetLookAtPointPos(playState.target, true);
  272.                         }
  273.                 }
  274.         }
  275.  
  276.         if (playState.timeout > 0.0f)
  277.         {
  278.                 playState.timeout -= updateTime;
  279.                 if (playState.timeout <= 0.0f)
  280.                         playState.finishedFlags |= PlayState::FinishedTimeout;
  281.         }
  282.  
  283.         bool finished = ((playState.flags & SCommunication::FinishAnimation) == 0)
  284.                         || (playState.finishedFlags & PlayState::FinishedAnimation);
  285.  
  286.         finished = finished && (((playState.flags & SCommunication::FinishSound) == 0)
  287.                                 || (playState.finishedFlags & PlayState::FinishedSound));
  288.  
  289.         finished = finished && (((playState.flags & SCommunication::FinishVoice) == 0)
  290.                                 || (playState.finishedFlags & PlayState::FinishedVoice));
  291.  
  292.         finished = finished && (((playState.flags & SCommunication::FinishTimeout) == 0)
  293.                                 || (playState.finishedFlags & PlayState::FinishedTimeout));
  294.  
  295.         if (finished)
  296.         {
  297.                 if ((playState.flags & SCommunication::AnimationAction) && ((playState.finishedFlags & PlayState::FinishedAnimation) == 0))
  298.                 {
  299.                         IAIActorProxy* aiProxy = aiObject->GetProxy();
  300.                         if (!aiProxy)
  301.                                 return false;
  302.  
  303.                         IAICommunicationHandler* commHandler = aiProxy->GetCommunicationHandler();
  304.  
  305.                         commHandler->StopAnimation(playId, playState.animationName.c_str(), IAICommunicationHandler::AnimationMethodAction);
  306.  
  307.                         playState.finishedFlags |= PlayState::FinishedAnimation;
  308.                 }
  309.  
  310.                 BlockingSettings(aiObject, playState, false);
  311.  
  312.                 return false;
  313.         }
  314.  
  315.         return true;
  316. }
  317.  
  318. void CommunicationPlayer::CleanUpPlayState(const CommPlayID& playID, PlayState& playState)
  319. {
  320.         bool finished = ((playState.flags & SCommunication::FinishAnimation) == 0)
  321.                         || (playState.finishedFlags & PlayState::FinishedAnimation);
  322.         finished = finished && (((playState.flags & SCommunication::FinishSound) == 0)
  323.                                 || (playState.finishedFlags & PlayState::FinishedSound));
  324.         finished = finished && (((playState.flags & SCommunication::FinishVoice) == 0)
  325.                                 || (playState.finishedFlags & PlayState::FinishedVoice));
  326.         finished = finished && (((playState.flags & SCommunication::FinishTimeout) == 0)
  327.                                 || (playState.finishedFlags & PlayState::FinishedTimeout));
  328.  
  329.         if (!finished)
  330.         {
  331.                 IEntity* entity = gEnv->pEntitySystem->GetEntity(playState.actorID);
  332.                 IAIObject* aiObject = entity ? entity->GetAI() : 0;
  333.  
  334.                 if (IAIActorProxy* aiProxy = aiObject ? aiObject->GetProxy() : 0)
  335.                 {
  336.                         IAICommunicationHandler* commHandler = aiProxy->GetCommunicationHandler();
  337.                         //REINST(Stop currently playing comm sounds and voice lines)
  338.                         if ((playState.finishedFlags & PlayState::FinishedSound) == 0)
  339.                                 commHandler->StopSound(playState.soundInfo);
  340.  
  341.                         if ((playState.finishedFlags & PlayState::FinishedVoice) == 0)
  342.                                 commHandler->StopVoice(playState.voiceInfo);
  343.  
  344.                         if (!playState.animationName.empty() && ((playState.finishedFlags & PlayState::FinishedAnimation) == 0))
  345.                         {
  346.                                 IAICommunicationHandler::EAnimationMethod method = (playState.flags & SCommunication::AnimationAction)
  347.                                                                                    ? IAICommunicationHandler::AnimationMethodAction : IAICommunicationHandler::AnimationMethodSignal;
  348.  
  349.                                 commHandler->StopAnimation(playID, playState.animationName.c_str(), method);
  350.                         }
  351.  
  352.                         BlockingSettings(aiObject, playState, false);
  353.                 }
  354.         }
  355.  
  356.         if (playState.listener)
  357.                 playState.listener->OnCommunicationFinished(playID, playState.finishedFlags);
  358.  
  359. }
  360.  
  361. void CommunicationPlayer::Stop(const CommPlayID& playID)
  362. {
  363.         PlayingCommunications::iterator it = m_playing.find(playID);
  364.         if (it != m_playing.end())
  365.         {
  366.                 PlayState& playState = it->second;
  367.  
  368.                 CleanUpPlayState(playID, playState);
  369.  
  370.                 if (gAIEnv.CVars.DebugDrawCommunication == 5)
  371.                 {
  372.                         CryLogAlways("CommunicationPlayer finished playing: %s[%u] as playID[%u]", gAIEnv.pCommunicationManager->GetCommunicationName(playState.commID), playState.commID.id, it->first.id);
  373.                 }
  374.  
  375.                 m_playing.erase(it);
  376.         }
  377. }
  378.  
  379. bool CommunicationPlayer::IsPlaying(const CommPlayID& playID, float* remainingTime) const
  380. {
  381.         PlayingCommunications::const_iterator it = m_playing.find(playID);
  382.         if (it != m_playing.end())
  383.                 return true;
  384.  
  385.         return false;
  386. }
  387.  
  388. CommID CommunicationPlayer::GetCommunicationID(const CommPlayID& playID) const
  389. {
  390.         PlayingCommunications::const_iterator it = m_playing.find(playID);
  391.         if (it != m_playing.end())
  392.                 return it->second.commID;
  393.  
  394.         return CommID(0);
  395. }
  396.  
downloadCommunicationPlayer.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