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.

343 lines
12 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Hooks and classes for the support of humanoid NPCs with
  4. // groovy facial animation capabilities, aka, "Actors"
  5. //
  6. //=============================================================================//
  7. #ifndef AI_BASEACTOR_H
  8. #define AI_BASEACTOR_H
  9. #include "ai_basehumanoid.h"
  10. #include "ai_speech.h"
  11. #include "AI_Interest_Target.h"
  12. #include <limits.h>
  13. #if defined( _WIN32 )
  14. #pragma once
  15. #endif
  16. //-----------------------------------------------------------------------------
  17. // CAI_BaseActor
  18. //
  19. // Purpose: The base class for all head/body/eye expressive NPCS.
  20. //
  21. //-----------------------------------------------------------------------------
  22. enum PoseParameter_t { POSE_END=INT_MAX };
  23. enum FlexWeight_t { FLEX_END=INT_MAX };
  24. class CInfoRemarkable;
  25. struct AILookTargetArgs_t
  26. {
  27. EHANDLE hTarget;
  28. Vector vTarget;
  29. float flDuration;
  30. float flInfluence;
  31. float flRamp;
  32. bool bExcludePlayers;
  33. CAI_InterestTarget *pQueue;
  34. };
  35. class CAI_BaseActor : public CAI_ExpresserHost<CAI_BaseHumanoid>
  36. {
  37. DECLARE_CLASS( CAI_BaseActor, CAI_ExpresserHost<CAI_BaseHumanoid> );
  38. //friend CPoseParameter;
  39. //friend CFlexWeight;
  40. #pragma region PoseParameter and FlexWeight get/set
  41. public:
  42. void Init( PoseParameter_t &index, const char *szName ) { index = (PoseParameter_t)LookupPoseParameter( szName ); };
  43. void Set( PoseParameter_t index, float flValue ) { SetPoseParameter( (int)index, flValue ); }
  44. float Get( PoseParameter_t index ) { return GetPoseParameter( (int)index ); }
  45. float ClampWithBias( PoseParameter_t index, float value, float base );
  46. // Note, you must add all names to this static function in order for Init to work
  47. static bool IsServerSideFlexController( char const *szName );
  48. void Init( FlexWeight_t &index, const char *szName )
  49. {
  50. // Make this fatal!!!
  51. if ( !IsServerSideFlexController( szName ) )
  52. {
  53. Error( "You forgot to add flex controller %s to list in CAI_BaseActor::IsServerSideFlexController().", szName );
  54. }
  55. index = (FlexWeight_t)FindFlexController( szName );
  56. }
  57. void Set( FlexWeight_t index, float flValue ) { SetFlexWeight( (LocalFlexController_t)index, flValue ); }
  58. float Get( FlexWeight_t index ) { return GetFlexWeight( (LocalFlexController_t)index ); }
  59. #pragma endregion
  60. public:
  61. CAI_BaseActor()
  62. : m_fLatchedPositions( 0 ),
  63. m_latchedEyeOrigin( vec3_origin ),
  64. m_latchedEyeDirection( vec3_origin ),
  65. m_latchedHeadDirection( vec3_origin ),
  66. m_flBlinktime( 0 ),
  67. m_hLookTarget( INVALID_EHANDLE ),
  68. m_iszExpressionScene( NULL_STRING ),
  69. m_iszIdleExpression( NULL_STRING ),
  70. m_iszAlertExpression( NULL_STRING ),
  71. m_iszCombatExpression( NULL_STRING ),
  72. m_iszDeathExpression( NULL_STRING ),
  73. m_iszExpressionOverride( NULL_STRING ),
  74. m_bRemarkablePolling( false )
  75. {
  76. memset( m_flextarget, 0, 64 * sizeof( m_flextarget[0] ) );
  77. }
  78. ~CAI_BaseActor()
  79. {
  80. delete m_pExpresser;
  81. }
  82. virtual void StudioFrameAdvance();
  83. virtual void Precache();
  84. virtual void SetModel( const char *szModelName );
  85. virtual bool StartSceneEvent( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event, CChoreoActor *actor, CBaseEntity *pTarget );
  86. virtual bool ProcessSceneEvent( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event );
  87. virtual bool ClearSceneEvent( CSceneEventInfo *info, bool fastKill, bool canceled );
  88. virtual bool CheckSceneEventCompletion( CSceneEventInfo *info, float currenttime, CChoreoScene *scene, CChoreoEvent *event );
  89. Vector EyePosition( );
  90. virtual Vector HeadDirection2D( void );
  91. virtual Vector HeadDirection3D( void );
  92. virtual Vector EyeDirection2D( void );
  93. virtual Vector EyeDirection3D( void );
  94. CBaseEntity *GetLooktarget() { return m_hLookTarget.Get(); }
  95. virtual void OnNewLookTarget() {};
  96. // CBaseFlex
  97. virtual void SetViewtarget( const Vector &viewtarget );
  98. // CAI_BaseNPC
  99. virtual float PickLookTarget( bool bExcludePlayers = false, float minTime = 1.5, float maxTime = 2.5 );
  100. virtual float PickLookTarget( CAI_InterestTarget &queue, bool bExcludePlayers = false, float minTime = 1.5, float maxTime = 2.5 );
  101. virtual bool PickTacticalLookTarget( AILookTargetArgs_t *pArgs );
  102. virtual bool PickRandomLookTarget( AILookTargetArgs_t *pArgs );
  103. virtual void MakeRandomLookTarget( AILookTargetArgs_t *pArgs, float minTime, float maxTime );
  104. virtual bool HasActiveLookTargets( void );
  105. virtual void OnSelectedLookTarget( AILookTargetArgs_t *pArgs ) { return; }
  106. virtual void ClearLookTarget( CBaseEntity *pTarget );
  107. virtual void ExpireCurrentRandomLookTarget() { m_flNextRandomLookTime = gpGlobals->curtime - 0.1f; }
  108. virtual void AddLookTarget( CBaseEntity *pTarget, float flImportance, float flDuration, float flRamp = 0.0 );
  109. virtual void AddLookTarget( const Vector &vecPosition, float flImportance, float flDuration, float flRamp = 0.0 );
  110. virtual void MaintainLookTargets( float flInterval );
  111. virtual bool ValidEyeTarget(const Vector &lookTargetPos);
  112. virtual float HeadTargetValidity(const Vector &lookTargetPos);
  113. virtual void StartTaskRangeAttack1( const Task_t *pTask );
  114. virtual void SetHeadDirection( const Vector &vTargetPos, float flInterval );
  115. void UpdateBodyControl( void );
  116. void UpdateHeadControl( const Vector &vHeadTarget, float flHeadInfluence );
  117. virtual float GetHeadDebounce( void ) { return 0.3; } // how much of previous head turn to use
  118. virtual bool ShouldBruteForceFailedNav() { return true; }
  119. virtual void GatherConditions( void );
  120. bool ValidHeadTarget( const Vector &lookTargetDir, float flDist );
  121. void AccumulateIdealYaw( float flYaw, float flIntensity );
  122. bool SetAccumulatedYawAndUpdate( void );
  123. float m_flAccumYawDelta;
  124. float m_flAccumYawScale;
  125. //---------------------------------
  126. virtual void OnStateChange( NPC_STATE OldState, NPC_STATE NewState );
  127. //-- Code for responding to INFO_REMARKABLES --
  128. #pragma region Info_Remarkable Code
  129. // INFO_REMARKABLEs are objects in the world for which AIs poll and
  130. // potentially say context-sensitive things.
  131. // The code is here because for the moment only AI_BaseActors do this.
  132. // However any other ExpresserHost theoretically could; if you want this
  133. // on, say, a player, we'd need to create some new common base class or
  134. // some such.
  135. // called from GatherConditions() for now because can't think of a better
  136. // place to poll it from. Returns true if speaking was tried.
  137. bool UpdateRemarkableSpeech() RESTRICT;
  138. inline void EnableRemarkables( bool bEnabled ) { m_bRemarkablePolling = bEnabled; }
  139. protected:
  140. /// true iff the character is allowed to poll for remarkables at all (eg,
  141. /// rate limiting). You can make it virtual if you need to.
  142. bool CanPollRemarkables();
  143. /// Test to see if a particular remarkable can be commented upon.
  144. virtual bool TestRemarkingUpon( CInfoRemarkable * pRemarkable );
  145. public:
  146. #pragma endregion
  147. //---------------------------------
  148. virtual void PlayExpressionForState( NPC_STATE state );
  149. virtual const char *SelectRandomExpressionForState( NPC_STATE state );
  150. float SetExpression( const char * );
  151. void ClearExpression();
  152. const char * GetExpression();
  153. enum
  154. {
  155. SCENE_AI_BLINK = 1,
  156. SCENE_AI_HOLSTER,
  157. SCENE_AI_UNHOLSTER,
  158. SCENE_AI_AIM,
  159. SCENE_AI_RANDOMLOOK,
  160. SCENE_AI_RANDOMFACEFLEX,
  161. SCENE_AI_RANDOMHEADFLEX,
  162. SCENE_AI_IGNORECOLLISION,
  163. SCENE_AI_DISABLEAI
  164. };
  165. public:
  166. //---------------------------------
  167. virtual void Teleport( const Vector *newPosition, const QAngle *newAngles, const Vector *newVelocity, bool bUseSlowHighAccuracyContacts = true );
  168. void InvalidateBoneCache( void );
  169. DECLARE_DATADESC();
  170. private:
  171. enum
  172. {
  173. HUMANOID_LATCHED_EYE = 0x0001,
  174. HUMANOID_LATCHED_HEAD = 0x0002,
  175. HUMANOID_LATCHED_ALL = 0x0003,
  176. };
  177. //---------------------------------
  178. void UpdateLatchedValues( void );
  179. // Input handlers.
  180. void InputSetExpressionOverride( inputdata_t &inputdata );
  181. //---------------------------------
  182. int m_fLatchedPositions;
  183. Vector m_latchedEyeOrigin;
  184. Vector m_latchedEyeDirection; // direction eyes are looking
  185. Vector m_latchedHeadDirection; // direction head is aiming
  186. void ClearHeadAdjustment( void );
  187. Vector m_goalHeadDirection;
  188. float m_goalHeadInfluence;
  189. //---------------------------------
  190. float m_goalSpineYaw;
  191. float m_goalBodyYaw;
  192. Vector m_goalHeadCorrection;
  193. //---------------------------------
  194. float m_flBlinktime;
  195. EHANDLE m_hLookTarget;
  196. CAI_InterestTarget m_lookQueue;
  197. CAI_InterestTarget m_syntheticLookQueue;
  198. CAI_InterestTarget m_randomLookQueue;
  199. float m_flNextRandomLookTime; // FIXME: move to scene
  200. //---------------------------------
  201. string_t m_iszExpressionScene;
  202. EHANDLE m_hExpressionSceneEnt;
  203. float m_flNextRandomExpressionTime;
  204. string_t m_iszExpressionOverride;
  205. protected:
  206. string_t m_iszIdleExpression;
  207. string_t m_iszAlertExpression;
  208. string_t m_iszCombatExpression;
  209. string_t m_iszDeathExpression;
  210. bool m_bRemarkablePolling;
  211. float m_fNextIdleVocalizeTime; ///< Not a CoundownTimer because it doesn't need to be networked
  212. float m_fNextRemarkPollTime; ///< we only poll for TLK_REMARK once per second or so
  213. #pragma region PoseParameters and FlexWeights
  214. private:
  215. //---------------------------------
  216. //PoseParameter_t m_ParameterBodyTransY; // "body_trans_Y"
  217. //PoseParameter_t m_ParameterBodyTransX; // "body_trans_X"
  218. //PoseParameter_t m_ParameterBodyLift; // "body_lift"
  219. PoseParameter_t m_ParameterBodyYaw; // "body_yaw"
  220. //PoseParameter_t m_ParameterBodyPitch; // "body_pitch"
  221. //PoseParameter_t m_ParameterBodyRoll; // "body_roll"
  222. PoseParameter_t m_ParameterSpineYaw; // "spine_yaw"
  223. //PoseParameter_t m_ParameterSpinePitch; // "spine_pitch"
  224. //PoseParameter_t m_ParameterSpineRoll; // "spine_roll"
  225. PoseParameter_t m_ParameterNeckTrans; // "neck_trans"
  226. PoseParameter_t m_ParameterHeadYaw; // "head_yaw"
  227. PoseParameter_t m_ParameterHeadPitch; // "head_pitch"
  228. PoseParameter_t m_ParameterHeadRoll; // "head_roll"
  229. //FlexWeight_t m_FlexweightMoveRightLeft; // "move_rightleft"
  230. //FlexWeight_t m_FlexweightMoveForwardBack;// "move_forwardback"
  231. //FlexWeight_t m_FlexweightMoveUpDown; // "move_updown"
  232. FlexWeight_t m_FlexweightBodyRightLeft; // "body_rightleft"
  233. //FlexWeight_t m_FlexweightBodyUpDown; // "body_updown"
  234. //FlexWeight_t m_FlexweightBodyTilt; // "body_tilt"
  235. FlexWeight_t m_FlexweightChestRightLeft; // "chest_rightleft"
  236. //FlexWeight_t m_FlexweightChestUpDown; // "chest_updown"
  237. //FlexWeight_t m_FlexweightChestTilt; // "chest_tilt"
  238. FlexWeight_t m_FlexweightHeadForwardBack;// "head_forwardback"
  239. FlexWeight_t m_FlexweightHeadRightLeft; // "head_rightleft"
  240. FlexWeight_t m_FlexweightHeadUpDown; // "head_updown"
  241. FlexWeight_t m_FlexweightHeadTilt; // "head_tilt"
  242. PoseParameter_t m_ParameterGestureHeight; // "gesture_height"
  243. PoseParameter_t m_ParameterGestureWidth; // "gesture_width"
  244. FlexWeight_t m_FlexweightGestureUpDown; // "gesture_updown"
  245. FlexWeight_t m_FlexweightGestureRightLeft; // "gesture_rightleft"
  246. #pragma endregion Cached indices
  247. private:
  248. //---------------------------------
  249. bool RandomFaceFlex( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event );
  250. bool RandomHeadFlex( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event );
  251. float m_flextarget[64];
  252. public:
  253. virtual bool UseSemaphore( void );
  254. protected:
  255. bool m_bDontUseSemaphore;
  256. public:
  257. //---------------------------------
  258. //
  259. // Speech support
  260. //
  261. virtual CAI_Expresser *GetExpresser();
  262. protected:
  263. bool CreateComponents();
  264. virtual CAI_Expresser *CreateExpresser();
  265. private:
  266. //---------------------------------
  267. CAI_Expresser *m_pExpresser;
  268. };
  269. //-----------------------------------------------------------------------------
  270. #endif // AI_BASEACTOR_H