BVB Source Codes

CRYENGINE Show CommunicationManager.h Source code

Return Download CRYENGINE: download CommunicationManager.h Source code - Download CRYENGINE Source code - Type:.h
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #ifndef __CommunicationManager_h__
  4. #define __CommunicationManager_h__
  5.  
  6. #pragma once
  7.  
  8. #include "Communication.h"
  9. #include "CommunicationChannelManager.h"
  10. #include "CommunicationPlayer.h"
  11. #include "CommunicationTestManager.h"
  12.  
  13. #include <CryAISystem/ICommunicationManager.h>
  14. #include <CryCore/Containers/CryListenerSet.h>
  15.  
  16. #include <CryCore/StlUtils.h>
  17.  
  18. /**
  19.  
  20.    The CommunicationManager acts as the heart of the communication system.
  21.    Communication can be used to give the sense of AI reacting and interacting with each other.
  22.    Currently the system support sound and animation based communication.
  23.  
  24.    System overview:
  25.  
  26.    The CommunicationManager is a global singleton managing:
  27.  
  28.  * The map of playing communications (CommPlayID : PlayingCommunication).
  29.  * An ordered queue of pending communications (CommPlayID & SCommunicationRequest).
  30.  * An unordered queue of pending communications (CommPlayID & SCommunicationRequest).
  31.  * A set of communication configurations:
  32.  * Used to define the communication events for a particular AI.
  33.  * Defined in Scripts/AI/Communication/BasicCommunications.xml
  34.  * A set of communication channels controlled by CommunicationChannelManager:
  35.  * Used to control and limit when AI can use play certain communications.
  36.  * Helps manage excess noise and repetition.
  37.  * Defined in Scripts/AI/Communication/ChannelConfig.xml.
  38.  * An OnCommunicationFinished() callback to detect finished communications.
  39.  * A CommunicationPlayer instance...
  40.  
  41.    The CommunicationPlayer is owned by the CommunicationManager singleton and manages:
  42.  
  43.  * Tracking a map of playing communication state (CommPlayID : PlayState).
  44.  * Starting the playing of communications via CommunicationHandler(s) (owned by AIProxy(s)).
  45.  * Callbacks from the CommunicationHandler(s) via PlayState::OnCommunicationHandlerEvent().
  46.  * Detects finished playback (based on PlayState finish flags) during update.
  47.  * Informs CommunicationManager via listener of finished communications.
  48.  
  49.    The CommunicationHandler is created with each AIProxy and is responsible for:
  50.  
  51.  * Dispatching the play sound/animation requests to the relivant systems.
  52.  * Listening for sound events by implementing ISoundEventListener.
  53.  * Listening for animation events by implementing IAnimationgGraphStateListener.
  54.  * Translating sound/animation stopped/failed/cancelled events in to ECommunicationHandlerEvents.
  55.  * Informing the CommunicationPlayer of finished events.
  56.  
  57.  */
  58.  
  59. class CCommunicationManager
  60.         : public CommunicationPlayer::ICommunicationFinishedListener
  61.           , public ICommunicationManager
  62. {
  63. public:
  64.  
  65.         enum
  66.         {
  67.                 MaxVariationCount = 32,
  68.         };
  69.  
  70. public:
  71.         CCommunicationManager(const char* configurationFileName = NULL);
  72.  
  73.         // ICommunicationManager
  74.         virtual uint32        GetConfigCount() const override;
  75.         virtual const char*   GetConfigName(uint32 index) const override;
  76.         virtual CommConfigID  GetConfigIDByIndex(uint32 index) const override;
  77.  
  78.         virtual CommChannelID GetChannelID(const char* name) const override;
  79.         virtual CommConfigID  GetConfigID(const char* name) const override;
  80.         virtual CommID        GetCommunicationID(const char* name) const override;
  81.         virtual const char*   GetCommunicationName(const CommID& communicationID) const override;
  82.  
  83.         virtual bool          CanCommunicationPlay(const SCommunicationRequest& request, float* estimatedWaitTime = 0) override;
  84.         virtual CommPlayID    PlayCommunication(SCommunicationRequest& request) override;
  85.         virtual void          StopCommunication(const CommPlayID& playID) override;
  86.         virtual bool          IsPlaying(const CommPlayID& playID, float* timeRemaining = 0) const override;
  87.         virtual bool          IsQueued(const CommPlayID& playID, float* estimatedWaitTime = 0) const override;
  88.  
  89.         virtual void          RegisterListener(ICommGlobalListener* eventListener, const char* name = NULL) override { m_globalListeners.Add(eventListener, name); }
  90.         virtual void          UnregisterListener(ICommGlobalListener* eventListener) override                        { m_globalListeners.Remove(eventListener); }
  91.         virtual void          RemoveInstanceListener(const CommPlayID& playID) override;
  92.  
  93.         virtual uint32        GetConfigCommunicationCount(const CommConfigID& configID) const override;
  94.         virtual const char*   GetConfigCommunicationNameByIndex(const CommConfigID& configID, uint32 index) const override;
  95.  
  96.         //Sets silence durations for actors, exluding them from communication sounds/animations for the length of the duration.
  97.         virtual void                      SetRestrictedDuration(EntityId actorId, float voiceDuration, float animDuration) override;
  98.         //Adds restriction on actor, excluding them from communiction sounds/animations until explicityly removed.
  99.         virtual void                      AddActorRestriction(EntityId actorId, bool restrictVoice, bool restrictAnimation) override;
  100.         //Removes restriction on actor, if no more restrictions are present actor will be available for communication sounds/animations.
  101.         virtual void                      RemoveActorRestriction(EntityId actorId, bool unrestrictVoice, bool unrestrictAnimation) override;
  102.  
  103.         virtual const WWiseConfiguration& GetWiseConfiguration() const override { return m_wiseConfiguration; }
  104.  
  105.         // Set the specified new value to the specified variable
  106.         virtual void SetVariableValue(const char* variableName, const bool newVariableValue) override;
  107.         virtual void GetVariablesNames(const char** variableNames, const size_t maxSize, size_t& actualSize) const override;
  108.         //~ICommunicationManager
  109.  
  110.         size_t GetPriority(const CommPlayID& playID) const;
  111.  
  112.         void   Reload();
  113.         void   LoadConfigurationAndScanRootFolder(const char* rootFolder);
  114.  
  115.         void   Reset();
  116.         void   ResetHistory();
  117.         void   Update(float updateTime);
  118.  
  119.         void   DebugDraw();
  120.  
  121.         /// Returns boolean indicating if the queried request can be forcibly played, even if channel is occupied.
  122.         bool CanForceAnimation(const SCommunication& comm, const SCommunicationRequest& request);
  123.  
  124.         // ICommunicationFinishedListener
  125.         /// playID is the id of the communication to stop.
  126.         /// StateFlags contains whether the components of the communication are finished or not (animation, sound, etc).
  127.         virtual void OnCommunicationFinished(const CommPlayID& playID, uint8 stateFlags) override;
  128.         //~ICommunicationFinishedListener
  129.  
  130.         void WriteStatistics();
  131.         void ResetStatistics() { m_debugStatisticsMap.clear(); }
  132.  
  133.         typedef std::map<CommID, SCommunication> Communications;
  134.         struct CommunicationsConfig
  135.         {
  136.                 string         name;
  137.                 Communications comms;
  138.         };
  139.  
  140.         const CommunicationsConfig& GetConfig(const CommConfigID& configID) const;
  141.  
  142.         CommunicationTestManager&   GetTestManager();
  143.  
  144. private:
  145.         void ScanFolder(const char* folderName);
  146.  
  147.         bool LoadCommunicationSettingsXML(const char* fileName);
  148.         bool LoadCommunication(const XmlNodeRef& commNode, SCommunication& comm);
  149.         bool LoadVariation(const XmlNodeRef& varNode, SCommunicationVariation& variation);
  150.         bool LoadVariable(const XmlNodeRef& varNode, Variables::Collection& variable);
  151.         void LoadVariables(const XmlNodeRef& rootNode, const char* fileName);
  152.         void LoadGlobalConfiguration(const XmlNodeRef& rootNode);
  153.  
  154.         bool ChooseVariation(const SCommunication& comm, const SCommunication::EVariationChoiceMethod method, const uint32 index, uint32& outputIndex) const;
  155.         bool CanVariationBePlayed(const SCommunication& comm, const uint32 variationIndex) const;
  156.         void UpdateHistory(SCommunication& comm, uint32 variationPlayed);
  157.  
  158.         bool Play(const CommPlayID& playID, SCommunicationRequest& request,
  159.                   const CommunicationChannel::Ptr& channel, SCommunication& comm);
  160.  
  161.         struct QueuedCommunication
  162.         {
  163.                 CommPlayID            playID;
  164.                 SCommunicationRequest request;
  165.                 float                 age;
  166.         };
  167.         typedef std::list<QueuedCommunication> QueuedCommunications;
  168.  
  169.         void FlushQueue(QueuedCommunications& queue, const CommunicationChannel::Ptr& channel, EntityId targetId);
  170.         void Queue(QueuedCommunications& queue, const CommPlayID& playID, const SCommunicationRequest& request);
  171.         bool RemoveFromQueue(QueuedCommunications& queue, const CommPlayID& playID);
  172.         void UpdateQueue(QueuedCommunications& queue, float updateTime);
  173.         void ProcessQueues();
  174.         bool PlayFromQueue(QueuedCommunication& queued);
  175.  
  176.         //Clear out lower priority playing communications, and clean up lower priority unfinalized communications
  177.         void FlushCommunications(const SCommunicationRequest& request, CommunicationChannel::Ptr channel);
  178.  
  179.         bool CheckAnimationGraphState(EntityId actorId, const string& name);
  180.         void SendAnimationGraphReset(EntityId entityId);
  181.  
  182.         void UpdateStateOfInternalVariables(const SCommunicationRequest& request);
  183.         void ResetStateOfInternalVariables();
  184.         void UpdateStateOfTargetAboveBelowVariables(const Vec3& entityPos, const Vec3& targetPos);
  185.  
  186.         string m_rootFolder;
  187.         string m_configurationFileName;
  188.  
  189.         typedef std::map<CommConfigID, CommunicationsConfig> Configs;
  190.         Configs                 m_configs;
  191.  
  192.         Variables::Declarations m_variablesDeclaration;
  193.         Variables::Collection   m_variables;
  194.  
  195.         struct PlayingCommunication
  196.         {
  197.                 PlayingCommunication() : actorID(0), eventListener(NULL), skipSound(false), minSilence(-1.0f), startTime(-1.0f){};
  198.                 PlayingCommunication(const PlayingCommunication& comm) : actorID(comm.actorID), channel(comm.channel), eventListener(NULL), animName(comm.animName), skipSound(comm.skipSound), minSilence(comm.minSilence), startTime(comm.startTime){}
  199.  
  200.                 EntityId                                      actorID;
  201.                 CommunicationChannel::Ptr                     channel;
  202.                 ICommunicationManager::ICommInstanceListener* eventListener;
  203.                 string animName;
  204.                 bool   skipSound;
  205.                 float  minSilence;
  206.                 float  startTime;
  207.         };
  208.  
  209.         typedef std::map<CommPlayID, PlayingCommunication> PlayingCommunications;
  210.         PlayingCommunications m_playing;
  211.  
  212.         typedef std::vector<PlayingCommunication> CommunicationVec;
  213.         /// Vector of entries that have stopped playing in the manager, but have outstanding animations.
  214.         CommunicationVec            m_unfinalized;
  215.  
  216.         CommunicationChannelManager m_channels;
  217.         CommunicationPlayer         m_player;
  218.  
  219.         //Map of actorIds to silence times. If an entry exists for an actor, he will not be able to play new communications until the silence timer runs out.
  220.         typedef std::map<EntityId, SRestrictedActorParams> TRestrictedActorMap;
  221.         TRestrictedActorMap m_restrictedActors;
  222.  
  223.         //Object to store entity properties related to readability
  224.         struct SActorReadabilitySettings
  225.         {
  226.                 SActorReadabilitySettings() : m_bIgnoreAnimations(false), m_bIgnoreVoice(false) {}
  227.  
  228.                 bool m_bIgnoreAnimations;
  229.                 bool m_bIgnoreVoice;
  230.         };
  231.  
  232.         //Map of actorIds to entity properties related to readability
  233.         typedef std::map<EntityId, SActorReadabilitySettings> TActorReadabilityProperties;
  234.  
  235.         TActorReadabilityProperties m_actorPropertiesMap;
  236.  
  237.         //Helper function to retrieve readability settings for an actor
  238.         void GetActorData(EntityId actorId, SActorReadabilitySettings& actorReadabilitySettings);
  239.         //Helper to retrieve table data for readability settings
  240.         void ReadDataFromTable(const SmartScriptTable& ssTable, SActorReadabilitySettings& actorReadabilitySettings);
  241.  
  242.         //Updates duration based actor restrictions, and deletes restrictions that are no longer valid
  243.         void UpdateActorRestrictions(float updateTime);
  244.         bool IsVoiceRestricted(EntityId actorId);
  245.         bool IsAnimationRestricted(EntityId actorId);
  246.  
  247.         QueuedCommunications       m_orderedQueue;
  248.         QueuedCommunications       m_unorderedQueue;
  249.  
  250.         CommPlayID                 m_playGenID;
  251.         mutable std::vector<uint8> m_randomPool;
  252.  
  253.         CommunicationTestManager   m_testManager;
  254.  
  255.         struct CommDebugEntry
  256.         {
  257.                 CommDebugEntry() : m_id(CommPlayID()), m_displayed(false), m_timeTracked(0.0f) {}
  258.  
  259.                 CommDebugEntry(const string& val, CommPlayID pid, const string& type)
  260.                         : m_commName(val), m_id(pid), m_type(type), m_displayed(false), m_timeTracked(0.0f) {}
  261.  
  262.                 CommDebugEntry(const string& val, CommPlayID pid, const string& type, CommChannelID cid)
  263.                         : m_commName(val), m_id(pid), m_type(type), m_displayed(false), m_timeTracked(0.0f) { m_channels.insert(cid); }
  264.  
  265.                 void UpdateChannelInfo(const CommDebugEntry& target) { if (!target.m_channels.empty()) m_channels.insert(*(target.m_channels.begin())); }
  266.  
  267.                 string                  m_commName;
  268.                 CommPlayID              m_id;
  269.  
  270.                 string                  m_type;
  271.  
  272.                 std::set<CommChannelID> m_channels;
  273.  
  274.                 bool                    m_displayed;
  275.                 float                   m_timeTracked;
  276.         };
  277.  
  278.         typedef std::map<EntityId, std::vector<CommDebugEntry>> TDebugMapHistory;
  279.  
  280.         struct CommKeys
  281.         {
  282.                 CommKeys(const CommConfigID& c1, const CommID& c2) : configId(c1), commId(c2) {}
  283.                 bool operator<(const CommKeys& right) const
  284.                 {
  285.                         return ((configId < right.configId) || (configId == right.configId && commId < right.commId));
  286.                 }
  287.                 CommConfigID configId;
  288.                 CommID       commId;
  289.         };
  290.  
  291.         struct CommDebugCount
  292.         {
  293.                 CommDebugCount() : requestCount(0), playCount(0) {}
  294.  
  295.                 unsigned int requestCount;
  296.                 unsigned int playCount;
  297.         };
  298.  
  299.         typedef std::map<CommKeys, CommDebugCount> TDebugMapStatistics;
  300.  
  301.         TDebugMapStatistics m_debugStatisticsMap;
  302.  
  303.         ///Looks for an existing record in the debug history, and either inserts a new record if not found, or updates an existing one
  304.         void UpdateDebugHistoryEntry(IEntity* pEntity, const CommDebugEntry& entryToUpdate);
  305.  
  306.         ///Updates the time displayed of all tracked debug records
  307.         void UpdateDebugHistory(float updateTime);
  308.  
  309.         ///Checks for a matching entry based on separate possible criteria
  310.         struct FindMatchingRecord
  311.         {
  312.                 FindMatchingRecord(const CommDebugEntry& entry)
  313.                         : m_searchEntry(entry) {}
  314.  
  315.                 bool operator()(const CommDebugEntry& entry) const
  316.                 {
  317.                         if (entry.m_commName == m_searchEntry.m_commName && entry.m_id == m_searchEntry.m_id) return true;
  318.                         if (m_searchEntry.m_timeTracked > 0.0f && entry.m_timeTracked > m_searchEntry.m_timeTracked) return true;
  319.                         return false;
  320.                 }
  321.  
  322.                 CommDebugEntry m_searchEntry;
  323.         };
  324.  
  325.         TDebugMapHistory m_debugHistoryMap;
  326.  
  327.         typedef CListenerSet<ICommGlobalListener*> TCommunicationListeners;
  328.         TCommunicationListeners m_globalListeners;
  329.  
  330.         void UpdateGlobalListeners(ECommunicationEvent event, EntityId actorId, const CommID& commId)
  331.         {
  332.                 for (TCommunicationListeners::Notifier notifier(m_globalListeners); notifier.IsValid(); notifier.Next())
  333.                         notifier->OnCommunicationEvent(event, actorId, commId);
  334.         }
  335.  
  336.         void CullPlayingCommunications();
  337.  
  338.         static const char* m_kTargetIsAbove_VariableName;
  339.         static const char* m_kTargetIsBelow_VariableName;
  340.  
  341.         WWiseConfiguration m_wiseConfiguration;
  342. };
  343.  
  344. #endif // __CommunicationManager_h__
  345.  
downloadCommunicationManager.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