Team Fortress 2 Source Code as on 22/4/2020
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.

254 lines
7.8 KiB

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