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.

250 lines
6.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "ai_default.h"
  8. #include "ai_task.h"
  9. #include "ai_schedule.h"
  10. #include "ai_node.h"
  11. #include "ai_hull.h"
  12. #include "ai_hint.h"
  13. #include "ai_squad.h"
  14. #include "ai_senses.h"
  15. #include "ai_navigator.h"
  16. #include "ai_motor.h"
  17. #include "ai_behavior.h"
  18. #include "ai_baseactor.h"
  19. #include "ai_behavior_lead.h"
  20. #include "ai_behavior_follow.h"
  21. #include "ai_behavior_standoff.h"
  22. #include "ai_behavior_assault.h"
  23. #include "npc_playercompanion.h"
  24. #include "soundent.h"
  25. #include "game.h"
  26. #include "npcevent.h"
  27. #include "activitylist.h"
  28. #include "vstdlib/random.h"
  29. #include "engine/IEngineSound.h"
  30. #include "sceneentity.h"
  31. #include "ai_behavior_functank.h"
  32. // memdbgon must be the last include file in a .cpp file!!!
  33. #include "tier0/memdbgon.h"
  34. #define BARNEY_MODEL "models/barney.mdl"
  35. ConVar sk_barney_health( "sk_barney_health","0");
  36. //=========================================================
  37. // Barney activities
  38. //=========================================================
  39. class CNPC_Barney : public CNPC_PlayerCompanion
  40. {
  41. public:
  42. DECLARE_CLASS( CNPC_Barney, CNPC_PlayerCompanion );
  43. DECLARE_SERVERCLASS();
  44. DECLARE_DATADESC();
  45. virtual void Precache()
  46. {
  47. // Prevents a warning
  48. SelectModel( );
  49. BaseClass::Precache();
  50. PrecacheScriptSound( "NPC_Barney.FootstepLeft" );
  51. PrecacheScriptSound( "NPC_Barney.FootstepRight" );
  52. PrecacheScriptSound( "NPC_Barney.Die" );
  53. PrecacheInstancedScene( "scenes/Expressions/BarneyIdle.vcd" );
  54. PrecacheInstancedScene( "scenes/Expressions/BarneyAlert.vcd" );
  55. PrecacheInstancedScene( "scenes/Expressions/BarneyCombat.vcd" );
  56. }
  57. void Spawn( void );
  58. void SelectModel();
  59. Class_T Classify( void );
  60. void Weapon_Equip( CBaseCombatWeapon *pWeapon );
  61. bool CreateBehaviors( void );
  62. void HandleAnimEvent( animevent_t *pEvent );
  63. bool ShouldLookForBetterWeapon() { return false; }
  64. void OnChangeRunningBehavior( CAI_BehaviorBase *pOldBehavior, CAI_BehaviorBase *pNewBehavior );
  65. void DeathSound( const CTakeDamageInfo &info );
  66. void GatherConditions();
  67. void UseFunc( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
  68. CAI_FuncTankBehavior m_FuncTankBehavior;
  69. COutputEvent m_OnPlayerUse;
  70. DEFINE_CUSTOM_AI;
  71. };
  72. LINK_ENTITY_TO_CLASS( npc_barney, CNPC_Barney );
  73. //---------------------------------------------------------
  74. //
  75. //---------------------------------------------------------
  76. IMPLEMENT_SERVERCLASS_ST(CNPC_Barney, DT_NPC_Barney)
  77. END_SEND_TABLE()
  78. //---------------------------------------------------------
  79. // Save/Restore
  80. //---------------------------------------------------------
  81. BEGIN_DATADESC( CNPC_Barney )
  82. // m_FuncTankBehavior
  83. DEFINE_OUTPUT( m_OnPlayerUse, "OnPlayerUse" ),
  84. DEFINE_USEFUNC( UseFunc ),
  85. END_DATADESC()
  86. //-----------------------------------------------------------------------------
  87. // Purpose:
  88. //-----------------------------------------------------------------------------
  89. void CNPC_Barney::SelectModel()
  90. {
  91. SetModelName( AllocPooledString( BARNEY_MODEL ) );
  92. }
  93. //-----------------------------------------------------------------------------
  94. // Purpose:
  95. //-----------------------------------------------------------------------------
  96. void CNPC_Barney::Spawn( void )
  97. {
  98. Precache();
  99. m_iHealth = 80;
  100. m_iszIdleExpression = MAKE_STRING("scenes/Expressions/BarneyIdle.vcd");
  101. m_iszAlertExpression = MAKE_STRING("scenes/Expressions/BarneyAlert.vcd");
  102. m_iszCombatExpression = MAKE_STRING("scenes/Expressions/BarneyCombat.vcd");
  103. BaseClass::Spawn();
  104. AddEFlags( EFL_NO_DISSOLVE | EFL_NO_MEGAPHYSCANNON_RAGDOLL | EFL_NO_PHYSCANNON_INTERACTION );
  105. NPCInit();
  106. SetUse( &CNPC_Barney::UseFunc );
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Purpose:
  110. // Output :
  111. //-----------------------------------------------------------------------------
  112. Class_T CNPC_Barney::Classify( void )
  113. {
  114. return CLASS_PLAYER_ALLY_VITAL;
  115. }
  116. //-----------------------------------------------------------------------------
  117. //-----------------------------------------------------------------------------
  118. void CNPC_Barney::Weapon_Equip( CBaseCombatWeapon *pWeapon )
  119. {
  120. BaseClass::Weapon_Equip( pWeapon );
  121. if( hl2_episodic.GetBool() && FClassnameIs( pWeapon, "weapon_ar2" ) )
  122. {
  123. // Allow Barney to defend himself at point-blank range in c17_05.
  124. pWeapon->m_fMinRange1 = 0.0f;
  125. }
  126. }
  127. //---------------------------------------------------------
  128. //---------------------------------------------------------
  129. void CNPC_Barney::HandleAnimEvent( animevent_t *pEvent )
  130. {
  131. switch( pEvent->event )
  132. {
  133. case NPC_EVENT_LEFTFOOT:
  134. {
  135. EmitSound( "NPC_Barney.FootstepLeft", pEvent->eventtime );
  136. }
  137. break;
  138. case NPC_EVENT_RIGHTFOOT:
  139. {
  140. EmitSound( "NPC_Barney.FootstepRight", pEvent->eventtime );
  141. }
  142. break;
  143. default:
  144. BaseClass::HandleAnimEvent( pEvent );
  145. break;
  146. }
  147. }
  148. //------------------------------------------------------------------------------
  149. //------------------------------------------------------------------------------
  150. void CNPC_Barney::DeathSound( const CTakeDamageInfo &info )
  151. {
  152. // Sentences don't play on dead NPCs
  153. SentenceStop();
  154. EmitSound( "npc_barney.die" );
  155. }
  156. bool CNPC_Barney::CreateBehaviors( void )
  157. {
  158. BaseClass::CreateBehaviors();
  159. AddBehavior( &m_FuncTankBehavior );
  160. return true;
  161. }
  162. void CNPC_Barney::OnChangeRunningBehavior( CAI_BehaviorBase *pOldBehavior, CAI_BehaviorBase *pNewBehavior )
  163. {
  164. if ( pNewBehavior == &m_FuncTankBehavior )
  165. {
  166. m_bReadinessCapable = false;
  167. }
  168. else if ( pOldBehavior == &m_FuncTankBehavior )
  169. {
  170. m_bReadinessCapable = IsReadinessCapable();
  171. }
  172. BaseClass::OnChangeRunningBehavior( pOldBehavior, pNewBehavior );
  173. }
  174. //-----------------------------------------------------------------------------
  175. //-----------------------------------------------------------------------------
  176. void CNPC_Barney::GatherConditions()
  177. {
  178. BaseClass::GatherConditions();
  179. // Handle speech AI. Don't do AI speech if we're in scripts unless permitted by the EnableSpeakWhileScripting input.
  180. if ( m_NPCState == NPC_STATE_IDLE || m_NPCState == NPC_STATE_ALERT || m_NPCState == NPC_STATE_COMBAT ||
  181. ( ( m_NPCState == NPC_STATE_SCRIPT ) && CanSpeakWhileScripting() ) )
  182. {
  183. DoCustomSpeechAI();
  184. }
  185. }
  186. //-----------------------------------------------------------------------------
  187. //-----------------------------------------------------------------------------
  188. void CNPC_Barney::UseFunc( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
  189. {
  190. m_bDontUseSemaphore = true;
  191. SpeakIfAllowed( TLK_USE );
  192. m_bDontUseSemaphore = false;
  193. m_OnPlayerUse.FireOutput( pActivator, pCaller );
  194. }
  195. //-----------------------------------------------------------------------------
  196. //
  197. // Schedules
  198. //
  199. //-----------------------------------------------------------------------------
  200. AI_BEGIN_CUSTOM_NPC( npc_barney, CNPC_Barney )
  201. AI_END_CUSTOM_NPC()