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.

304 lines
10 KiB

  1. //========= Copyright 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. struct AILookTargetArgs_t
  25. {
  26. EHANDLE hTarget;
  27. Vector vTarget;
  28. float flDuration;
  29. float flInfluence;
  30. float flRamp;
  31. bool bExcludePlayers;
  32. CAI_InterestTarget *pQueue;
  33. };
  34. class CAI_BaseActor : public CAI_ExpresserHost<CAI_BaseHumanoid>
  35. {
  36. DECLARE_CLASS( CAI_BaseActor, CAI_ExpresserHost<CAI_BaseHumanoid> );
  37. //friend CPoseParameter;
  38. //friend CFlexWeight;
  39. public:
  40. // FIXME: this method is lame, isn't there some sort of template thing that would get rid of the Outer pointer?
  41. void Init( PoseParameter_t &index, const char *szName ) { index = (PoseParameter_t)LookupPoseParameter( szName ); };
  42. void Set( PoseParameter_t index, float flValue ) { SetPoseParameter( (int)index, flValue ); }
  43. float Get( PoseParameter_t index ) { return GetPoseParameter( (int)index ); }
  44. float ClampWithBias( PoseParameter_t index, float value, float base );
  45. // Note, you must add all names to this static function in order for Init to work
  46. static bool IsServerSideFlexController( char const *szName );
  47. void Init( FlexWeight_t &index, const char *szName )
  48. {
  49. // Make this fatal!!!
  50. if ( !IsServerSideFlexController( szName ) )
  51. {
  52. Error( "You forgot to add flex controller %s to list in CAI_BaseActor::IsServerSideFlexController().", szName );
  53. }
  54. index = (FlexWeight_t)FindFlexController( szName );
  55. }
  56. void Set( FlexWeight_t index, float flValue ) { SetFlexWeight( (LocalFlexController_t)index, flValue ); }
  57. float Get( FlexWeight_t index ) { return GetFlexWeight( (LocalFlexController_t)index ); }
  58. public:
  59. CAI_BaseActor()
  60. : m_fLatchedPositions( 0 ),
  61. m_latchedEyeOrigin( vec3_origin ),
  62. m_latchedEyeDirection( vec3_origin ),
  63. m_latchedHeadDirection( vec3_origin ),
  64. m_flBlinktime( 0 ),
  65. m_hLookTarget( NULL ),
  66. m_iszExpressionScene( NULL_STRING ),
  67. m_iszIdleExpression( NULL_STRING ),
  68. m_iszAlertExpression( NULL_STRING ),
  69. m_iszCombatExpression( NULL_STRING ),
  70. m_iszDeathExpression( NULL_STRING ),
  71. m_iszExpressionOverride( NULL_STRING )
  72. {
  73. memset( m_flextarget, 0, 64 * sizeof( m_flextarget[0] ) );
  74. }
  75. ~CAI_BaseActor()
  76. {
  77. delete m_pExpresser;
  78. }
  79. virtual void StudioFrameAdvance();
  80. virtual void Precache();
  81. virtual void SetModel( const char *szModelName );
  82. virtual bool StartSceneEvent( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event, CChoreoActor *actor, CBaseEntity *pTarget );
  83. virtual bool ProcessSceneEvent( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event );
  84. virtual bool ClearSceneEvent( CSceneEventInfo *info, bool fastKill, bool canceled );
  85. virtual bool CheckSceneEventCompletion( CSceneEventInfo *info, float currenttime, CChoreoScene *scene, CChoreoEvent *event );
  86. Vector EyePosition( );
  87. virtual Vector HeadDirection2D( void );
  88. virtual Vector HeadDirection3D( void );
  89. virtual Vector EyeDirection2D( void );
  90. virtual Vector EyeDirection3D( void );
  91. CBaseEntity *GetLooktarget() { return m_hLookTarget.Get(); }
  92. virtual void OnNewLookTarget() {};
  93. // CBaseFlex
  94. virtual void SetViewtarget( const Vector &viewtarget );
  95. // CAI_BaseNPC
  96. virtual float PickLookTarget( bool bExcludePlayers = false, float minTime = 1.5, float maxTime = 2.5 );
  97. virtual float PickLookTarget( CAI_InterestTarget &queue, bool bExcludePlayers = false, float minTime = 1.5, float maxTime = 2.5 );
  98. virtual bool PickTacticalLookTarget( AILookTargetArgs_t *pArgs );
  99. virtual bool PickRandomLookTarget( AILookTargetArgs_t *pArgs );
  100. virtual void MakeRandomLookTarget( AILookTargetArgs_t *pArgs, float minTime, float maxTime );
  101. virtual bool HasActiveLookTargets( void );
  102. virtual void OnSelectedLookTarget( AILookTargetArgs_t *pArgs ) { return; }
  103. virtual void ClearLookTarget( CBaseEntity *pTarget );
  104. virtual void ExpireCurrentRandomLookTarget() { m_flNextRandomLookTime = gpGlobals->curtime - 0.1f; }
  105. virtual void StartTaskRangeAttack1( const Task_t *pTask );
  106. virtual void AddLookTarget( CBaseEntity *pTarget, float flImportance, float flDuration, float flRamp = 0.0 );
  107. virtual void AddLookTarget( const Vector &vecPosition, float flImportance, float flDuration, float flRamp = 0.0 );
  108. virtual void SetHeadDirection( const Vector &vTargetPos, float flInterval );
  109. void UpdateBodyControl( void );
  110. void UpdateHeadControl( const Vector &vHeadTarget, float flHeadInfluence );
  111. virtual float GetHeadDebounce( void ) { return 0.3; } // how much of previous head turn to use
  112. virtual void MaintainLookTargets( float flInterval );
  113. virtual bool ValidEyeTarget(const Vector &lookTargetPos);
  114. virtual bool ValidHeadTarget(const Vector &lookTargetPos);
  115. virtual float HeadTargetValidity(const Vector &lookTargetPos);
  116. virtual bool ShouldBruteForceFailedNav() { return true; }
  117. void AccumulateIdealYaw( float flYaw, float flIntensity );
  118. bool SetAccumulatedYawAndUpdate( void );
  119. float m_flAccumYawDelta;
  120. float m_flAccumYawScale;
  121. //---------------------------------
  122. virtual void OnStateChange( NPC_STATE OldState, NPC_STATE NewState );
  123. //---------------------------------
  124. virtual void PlayExpressionForState( NPC_STATE state );
  125. virtual const char *SelectRandomExpressionForState( NPC_STATE state );
  126. float SetExpression( const char * );
  127. void ClearExpression();
  128. const char * GetExpression();
  129. enum
  130. {
  131. SCENE_AI_BLINK = 1,
  132. SCENE_AI_HOLSTER,
  133. SCENE_AI_UNHOLSTER,
  134. SCENE_AI_AIM,
  135. SCENE_AI_RANDOMLOOK,
  136. SCENE_AI_RANDOMFACEFLEX,
  137. SCENE_AI_RANDOMHEADFLEX,
  138. SCENE_AI_IGNORECOLLISION,
  139. SCENE_AI_DISABLEAI
  140. };
  141. DECLARE_DATADESC();
  142. private:
  143. enum
  144. {
  145. HUMANOID_LATCHED_EYE = 0x0001,
  146. HUMANOID_LATCHED_HEAD = 0x0002,
  147. HUMANOID_LATCHED_ALL = 0x0003,
  148. };
  149. //---------------------------------
  150. void UpdateLatchedValues( void );
  151. // Input handlers.
  152. void InputSetExpressionOverride( inputdata_t &inputdata );
  153. //---------------------------------
  154. int m_fLatchedPositions;
  155. Vector m_latchedEyeOrigin;
  156. Vector m_latchedEyeDirection; // direction eyes are looking
  157. Vector m_latchedHeadDirection; // direction head is aiming
  158. void ClearHeadAdjustment( void );
  159. Vector m_goalHeadDirection;
  160. float m_goalHeadInfluence;
  161. //---------------------------------
  162. float m_goalSpineYaw;
  163. float m_goalBodyYaw;
  164. Vector m_goalHeadCorrection;
  165. //---------------------------------
  166. float m_flBlinktime;
  167. EHANDLE m_hLookTarget;
  168. CAI_InterestTarget m_lookQueue;
  169. CAI_InterestTarget m_syntheticLookQueue;
  170. CAI_InterestTarget m_randomLookQueue;
  171. float m_flNextRandomLookTime; // FIXME: move to scene
  172. //---------------------------------
  173. string_t m_iszExpressionScene;
  174. EHANDLE m_hExpressionSceneEnt;
  175. float m_flNextRandomExpressionTime;
  176. string_t m_iszExpressionOverride;
  177. protected:
  178. string_t m_iszIdleExpression;
  179. string_t m_iszAlertExpression;
  180. string_t m_iszCombatExpression;
  181. string_t m_iszDeathExpression;
  182. private:
  183. //---------------------------------
  184. //PoseParameter_t m_ParameterBodyTransY; // "body_trans_Y"
  185. //PoseParameter_t m_ParameterBodyTransX; // "body_trans_X"
  186. //PoseParameter_t m_ParameterBodyLift; // "body_lift"
  187. PoseParameter_t m_ParameterBodyYaw; // "body_yaw"
  188. //PoseParameter_t m_ParameterBodyPitch; // "body_pitch"
  189. //PoseParameter_t m_ParameterBodyRoll; // "body_roll"
  190. PoseParameter_t m_ParameterSpineYaw; // "spine_yaw"
  191. //PoseParameter_t m_ParameterSpinePitch; // "spine_pitch"
  192. //PoseParameter_t m_ParameterSpineRoll; // "spine_roll"
  193. PoseParameter_t m_ParameterNeckTrans; // "neck_trans"
  194. PoseParameter_t m_ParameterHeadYaw; // "head_yaw"
  195. PoseParameter_t m_ParameterHeadPitch; // "head_pitch"
  196. PoseParameter_t m_ParameterHeadRoll; // "head_roll"
  197. //FlexWeight_t m_FlexweightMoveRightLeft; // "move_rightleft"
  198. //FlexWeight_t m_FlexweightMoveForwardBack;// "move_forwardback"
  199. //FlexWeight_t m_FlexweightMoveUpDown; // "move_updown"
  200. FlexWeight_t m_FlexweightBodyRightLeft; // "body_rightleft"
  201. //FlexWeight_t m_FlexweightBodyUpDown; // "body_updown"
  202. //FlexWeight_t m_FlexweightBodyTilt; // "body_tilt"
  203. FlexWeight_t m_FlexweightChestRightLeft; // "chest_rightleft"
  204. //FlexWeight_t m_FlexweightChestUpDown; // "chest_updown"
  205. //FlexWeight_t m_FlexweightChestTilt; // "chest_tilt"
  206. FlexWeight_t m_FlexweightHeadForwardBack;// "head_forwardback"
  207. FlexWeight_t m_FlexweightHeadRightLeft; // "head_rightleft"
  208. FlexWeight_t m_FlexweightHeadUpDown; // "head_updown"
  209. FlexWeight_t m_FlexweightHeadTilt; // "head_tilt"
  210. PoseParameter_t m_ParameterGestureHeight; // "gesture_height"
  211. PoseParameter_t m_ParameterGestureWidth; // "gesture_width"
  212. FlexWeight_t m_FlexweightGestureUpDown; // "gesture_updown"
  213. FlexWeight_t m_FlexweightGestureRightLeft; // "gesture_rightleft"
  214. private:
  215. //---------------------------------
  216. bool RandomFaceFlex( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event );
  217. bool RandomHeadFlex( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event );
  218. float m_flextarget[64];
  219. public:
  220. virtual bool UseSemaphore( void );
  221. protected:
  222. bool m_bDontUseSemaphore;
  223. public:
  224. //---------------------------------
  225. //
  226. // Speech support
  227. //
  228. virtual CAI_Expresser *GetExpresser();
  229. protected:
  230. bool CreateComponents();
  231. virtual CAI_Expresser *CreateExpresser();
  232. private:
  233. //---------------------------------
  234. CAI_Expresser *m_pExpresser;
  235. };
  236. //-----------------------------------------------------------------------------
  237. #endif // AI_BASEACTOR_H