Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
7.8 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #ifndef TALKNPC_H
  10. #define TALKNPC_H
  11. #ifdef POSIX
  12. #undef time
  13. #include <time.h>
  14. #endif
  15. // #ifndef _GAMECONSOLE
  16. // #ifdef POSIX
  17. // #undef min
  18. // #undef max
  19. // #endif
  20. // #pragma warning(push)
  21. // #include <set>
  22. // #pragma warning(pop)
  23. // #endif
  24. #ifdef _WIN32
  25. #pragma once
  26. #endif
  27. // #ifdef POSIX
  28. // // the include <set> monkey's with the max() define, unbreak it
  29. // #undef MINMAX_H
  30. // #include "minmax.h"
  31. // #endif
  32. #include "ai_playerally.h"
  33. #include "soundflags.h"
  34. #include "ai_task.h"
  35. #include "ai_schedule.h"
  36. #include "ai_default.h"
  37. #include "ai_speech.h"
  38. #include "ai_basenpc.h"
  39. #include "ai_behavior.h"
  40. #include "ai_behavior_follow.h"
  41. #include "tier0/memdbgon.h"
  42. //=========================================================
  43. // Talking NPC base class
  44. // Used for scientists and barneys
  45. //=========================================================
  46. #define TLK_CFRIENDS 4
  47. //=============================================================================
  48. // >> CNPCSimpleTalker
  49. //=============================================================================
  50. #define MONOLOGNAME_LEN 16 // sentence names passed as monolog may be no longer than this.
  51. #define AI_SP_START_MONOLOG '~'
  52. #define AI_SP_MONOLOG_LINE '@'
  53. class CNPCSimpleTalker;
  54. class CNPCSimpleTalkerExpresser : public CAI_ComponentWithOuter<CNPCSimpleTalker, CAI_Expresser>
  55. {
  56. public:
  57. CNPCSimpleTalkerExpresser( CNPCSimpleTalker *pOuter )
  58. : CAI_ComponentWithOuter<CNPCSimpleTalker, CAI_Expresser>( pOuter )
  59. {
  60. EndMonolog();
  61. }
  62. virtual int SpeakRawSentence( const char *pszSentence, float delay, float volume = VOL_NORM, soundlevel_t soundlevel = SNDLVL_TALKING, CBaseEntity *pListener = NULL );
  63. // --------------------------------
  64. //
  65. // Monologue operations
  66. //
  67. bool HasMonolog( void ) { return m_iMonologIndex != -1; };
  68. void BeginMonolog( char *pszSentenceName, CBaseEntity *pListener );
  69. void EndMonolog( void );
  70. void SpeakMonolog( void );
  71. void SuspendMonolog( float flInterval );
  72. void ResumeMonolog( void );
  73. CBaseEntity *GetMonologueTarget() { return m_hMonologTalkTarget.Get(); }
  74. // --------------------------------
  75. //
  76. // Monologue data
  77. //
  78. char m_szMonologSentence[MONOLOGNAME_LEN]; // The name of the sentence group for the monolog I'm speaking.
  79. int m_iMonologIndex; // Which sentence from the group I should be speaking.
  80. bool m_fMonologSuspended;
  81. EHANDLE m_hMonologTalkTarget; // Who I'm trying to deliver my monolog to.
  82. DECLARE_SIMPLE_DATADESC();
  83. };
  84. //-------------------------------------
  85. class CNPCSimpleTalker : public CAI_PlayerAlly
  86. {
  87. DECLARE_CLASS( CNPCSimpleTalker, CAI_PlayerAlly );
  88. public:
  89. void Precache( void );
  90. virtual bool KeyValue( const char *szKeyName, const char *szValue );
  91. virtual CAI_Expresser *CreateExpresser() { return new CNPCSimpleTalkerExpresser(this); }
  92. virtual void StartFollowing( CBaseEntity *pLeader ) { m_FollowBehavior.SetFollowTarget( pLeader ); DeferSchedulingToBehavior( &m_FollowBehavior ); }
  93. virtual void StopFollowing( ) { m_FollowBehavior.SetFollowTarget( NULL ); DeferSchedulingToBehavior( NULL ); }
  94. CBaseEntity *GetFollowTarget( void ) { return m_FollowBehavior.GetFollowTarget(); }
  95. virtual void OnChangeRunningBehavior( CAI_BehaviorBase *pOldBehavior, CAI_BehaviorBase *pNewBehavior );
  96. bool OnBehaviorChangeStatus( CAI_BehaviorBase *pBehavior, bool fCanFinishSchedule );
  97. int PlayScriptedSentence( const char *pszSentence, float delay, float volume, soundlevel_t soundlevel, bool bConcurrent, CBaseEntity *pListener );
  98. virtual void FollowerUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
  99. void Event_Killed( const CTakeDamageInfo &info );
  100. int OnTakeDamage_Alive( const CTakeDamageInfo &info );
  101. bool CreateBehaviors()
  102. {
  103. AddBehavior( &m_FollowBehavior );
  104. return BaseClass::CreateBehaviors();
  105. }
  106. void BuildScheduleTestBits( void );
  107. void PrescheduleThink( void );
  108. virtual int SelectSchedule( void );
  109. virtual int SelectNonCombatSpeechSchedule();
  110. void StartTask( const Task_t *pTask );
  111. void RunTask( const Task_t *pTask );
  112. void HandleAnimEvent( animevent_t *pEvent );
  113. Activity NPC_TranslateActivity( Activity newActivity );
  114. virtual void OnStartingFollow( CBaseEntity *pTarget );
  115. virtual void OnStoppingFollow( CBaseEntity *pTarget );
  116. virtual void DeferAllIdleSpeech( float flDelay, CAI_BaseNPC *pIgnore = NULL );
  117. bool ShouldSpeakRandom( int iChance, float flModifier );
  118. // For following
  119. virtual void DeclineFollowing( void ) {}
  120. void LimitFollowers( CBaseEntity *pPlayer, int maxFollowers );
  121. float GetUseTime() const { return m_useTime; }
  122. //=========================================================
  123. // TalkNPC schedules
  124. //=========================================================
  125. enum
  126. {
  127. SCHED_TALKER_IDLE_RESPONSE = BaseClass::NEXT_SCHEDULE,
  128. SCHED_TALKER_IDLE_SPEAK,
  129. SCHED_TALKER_IDLE_HELLO,
  130. SCHED_TALKER_IDLE_STOP_SHOOTING,
  131. SCHED_TALKER_IDLE_WATCH_CLIENT,
  132. SCHED_TALKER_IDLE_WATCH_CLIENT_STARE,
  133. SCHED_TALKER_IDLE_EYE_CONTACT,
  134. SCHED_TALKER_BETRAYED,
  135. // !ALWAYS LAST!
  136. NEXT_SCHEDULE,
  137. };
  138. //=========================================================
  139. // TalkNPC tasks
  140. //=========================================================
  141. enum
  142. {
  143. TASK_TALKER_RESPOND = BaseClass::NEXT_TASK, // say my response
  144. TASK_TALKER_SPEAK, // question or remark
  145. TASK_TALKER_HELLO, // Try to say hello to player
  146. TASK_TALKER_BETRAYED, // Player killed an ally
  147. TASK_TALKER_HEADRESET, // reset head position
  148. TASK_TALKER_STOPSHOOTING, // tell player to stop shooting friend
  149. TASK_TALKER_STARE, // let the player know I know he's staring at me.
  150. TASK_TALKER_LOOK_AT_CLIENT,// faces player if not moving and not talking and in idle.
  151. TASK_TALKER_CLIENT_STARE, // same as look at client, but says something if the player stares.
  152. TASK_TALKER_EYECONTACT, // maintain eyecontact with person who I'm talking to
  153. TASK_TALKER_IDEALYAW, // set ideal yaw to face who I'm talking to
  154. TASK_FIND_LOCK_HINTNODE_HEALTH, // Find & lock a nearby healthkit hintnode to heal myself at
  155. TASK_TALKER_WAIT_FOR_SEMAPHORE,
  156. // !ALWAYS LAST!
  157. NEXT_TASK,
  158. };
  159. //private:
  160. virtual bool IsValidSpeechTarget( int flags, CBaseEntity *pEntity );
  161. CBaseEntity *FindNearestFriend(bool fPlayer);
  162. bool IsOkToSpeak( void );
  163. void SayHelloToPlayer( CBaseEntity *pPlayer );
  164. virtual bool CanSayHello( void );
  165. virtual int FIdleHello( void );
  166. // Inputs
  167. void InputIdleRespond( inputdata_t &inputdata );
  168. // Conversations / communication
  169. void IdleRespond( void );
  170. int FIdleSpeak( void );
  171. void FIdleSpeakWhileMoving( void );
  172. int FIdleStare( void );
  173. bool SpeakQuestionFriend( CBaseEntity *pFriend );
  174. bool SpeakAnswerFriend( CBaseEntity *pFriend );
  175. void TrySmellTalk( void );
  176. virtual void SetAnswerQuestion( CNPCSimpleTalker *pSpeaker );
  177. bool ShouldSuspendMonolog( void );
  178. bool ShouldResumeMonolog( void );
  179. void OnResumeMonolog() { Speak( TLK_RESUME ); }
  180. int m_nSpeak; // number of times initiated talking
  181. float m_flNextIdleSpeechTime;
  182. static char *m_szFriends[TLK_CFRIENDS]; // array of friend names
  183. CBaseEntity *EnumFriends( CBaseEntity *pentPrevious, int listNumber, bool bTrace );
  184. virtual int FriendNumber( int arrayNumber ) { return arrayNumber; }
  185. void ShutUpFriends( void );
  186. void AlertFriends( CBaseEntity *pKiller );
  187. string_t m_iszUse; // Custom +USE sentence group (follow)
  188. string_t m_iszUnUse; // Custom +USE sentence group (stop following)
  189. protected:
  190. CAI_FollowBehavior m_FollowBehavior;
  191. float m_useTime; // Don't allow +USE until this time
  192. //---------------------------------
  193. DECLARE_DATADESC();
  194. DEFINE_CUSTOM_AI;
  195. };
  196. #include "tier0/memdbgoff.h"
  197. #endif //TALKNPC_H