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.

303 lines
7.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ====
  2. //
  3. // To give an NPC the ability to shoot while moving:
  4. //
  5. // - In the NPC's Spawn function, add:
  6. // CapabilitiesAdd( bits_CAP_MOVE_SHOOT );
  7. //
  8. // - The NPC must either have a weapon (return non-NULL from GetActiveWeapon)
  9. // or must have bits_CAP_INNATE_RANGE_ATTACK1 or bits_CAP_INNATE_RANGE_ATTACK2.
  10. //
  11. // - Support the activities ACT_WALK_AIM and/or ACT_RUN_AIM in the NPC.
  12. //
  13. // - Support the activity ACT_GESTURE_RANGE_ATTACK1 as a gesture that plays
  14. // over ACT_WALK_AIM and ACT_RUN_AIM.
  15. //
  16. //=============================================================================
  17. #include "cbase.h"
  18. #include "ai_moveshoot.h"
  19. #include "ai_basenpc.h"
  20. #include "ai_navigator.h"
  21. #include "ai_memory.h"
  22. // memdbgon must be the last include file in a .cpp file!!!
  23. #include "tier0/memdbgon.h"
  24. //-----------------------------------------------------------------------------
  25. BEGIN_SIMPLE_DATADESC( CAI_MoveAndShootOverlay )
  26. DEFINE_FIELD( m_bMovingAndShooting, FIELD_BOOLEAN ),
  27. DEFINE_FIELD( m_bNoShootWhileMove, FIELD_BOOLEAN ),
  28. DEFINE_FIELD( m_initialDelay, FIELD_FLOAT ),
  29. DEFINE_FIELD( m_flSuspendUntilTime, FIELD_TIME ),
  30. END_DATADESC()
  31. #define MOVESHOOT_DO_NOT_SUSPEND -1.0f
  32. //-------------------------------------
  33. CAI_MoveAndShootOverlay::CAI_MoveAndShootOverlay() : m_bMovingAndShooting(false), m_initialDelay(0)
  34. {
  35. m_flSuspendUntilTime = MOVESHOOT_DO_NOT_SUSPEND;
  36. m_bNoShootWhileMove = false;
  37. }
  38. //-------------------------------------
  39. void CAI_MoveAndShootOverlay::NoShootWhileMove()
  40. {
  41. m_bNoShootWhileMove = true;
  42. }
  43. //-------------------------------------
  44. bool CAI_MoveAndShootOverlay::HasAvailableRangeAttack()
  45. {
  46. return ( ( GetOuter()->GetActiveWeapon() != NULL ) ||
  47. ( GetOuter()->CapabilitiesGet() & bits_CAP_INNATE_RANGE_ATTACK1 ) ||
  48. ( GetOuter()->CapabilitiesGet() & bits_CAP_INNATE_RANGE_ATTACK2 ) );
  49. }
  50. //-------------------------------------
  51. void CAI_MoveAndShootOverlay::StartShootWhileMove()
  52. {
  53. if ( GetOuter()->GetState() == NPC_STATE_SCRIPT ||
  54. !HasAvailableRangeAttack() ||
  55. !GetOuter()->HaveSequenceForActivity( GetOuter()->TranslateActivity( ACT_WALK_AIM ) ) ||
  56. !GetOuter()->HaveSequenceForActivity( GetOuter()->TranslateActivity( ACT_RUN_AIM ) ) )
  57. {
  58. NoShootWhileMove();
  59. return;
  60. }
  61. GetOuter()->GetShotRegulator()->FireNoEarlierThan( gpGlobals->curtime + m_initialDelay );
  62. m_bNoShootWhileMove = false;
  63. }
  64. //-------------------------------------
  65. bool CAI_MoveAndShootOverlay::CanAimAtEnemy()
  66. {
  67. CAI_BaseNPC *pOuter = GetOuter();
  68. bool result = false;
  69. bool resetConditions = false;
  70. CAI_ScheduleBits savedConditions;
  71. if ( !GetOuter()->ConditionsGathered() )
  72. {
  73. savedConditions = GetOuter()->AccessConditionBits();
  74. GetOuter()->GatherEnemyConditions( GetOuter()->GetEnemy() );
  75. }
  76. if ( pOuter->HasCondition( COND_CAN_RANGE_ATTACK1 ) )
  77. {
  78. result = true;
  79. }
  80. else if ( !pOuter->HasCondition( COND_ENEMY_DEAD ) &&
  81. !pOuter->HasCondition( COND_TOO_FAR_TO_ATTACK ) &&
  82. !pOuter->HasCondition( COND_ENEMY_TOO_FAR ) &&
  83. !pOuter->HasCondition( COND_ENEMY_OCCLUDED ) )
  84. {
  85. result = true;
  86. }
  87. // If we don't have a weapon, stop
  88. // This catches NPCs who holster their weapons while running
  89. if ( !HasAvailableRangeAttack() )
  90. {
  91. result = false;
  92. }
  93. if ( resetConditions )
  94. {
  95. GetOuter()->AccessConditionBits() = savedConditions;
  96. }
  97. return result;
  98. }
  99. //-------------------------------------
  100. void CAI_MoveAndShootOverlay::UpdateMoveShootActivity( bool bMoveAimAtEnemy )
  101. {
  102. // FIXME: should be able to query that transition/state is happening
  103. // FIXME: needs to not try to shoot if the movement type isn't understood
  104. Activity curActivity = GetOuter()->GetNavigator()->GetMovementActivity();
  105. Activity newActivity = curActivity;
  106. if (bMoveAimAtEnemy)
  107. {
  108. switch( curActivity )
  109. {
  110. case ACT_WALK:
  111. newActivity = ACT_WALK_AIM;
  112. break;
  113. case ACT_RUN:
  114. newActivity = ACT_RUN_AIM;
  115. break;
  116. }
  117. }
  118. else
  119. {
  120. switch( curActivity )
  121. {
  122. case ACT_WALK_AIM:
  123. newActivity = ACT_WALK;
  124. break;
  125. case ACT_RUN_AIM:
  126. newActivity = ACT_RUN;
  127. break;
  128. }
  129. }
  130. if ( curActivity != newActivity )
  131. {
  132. // Transitioning, wait a bit
  133. GetOuter()->GetShotRegulator()->FireNoEarlierThan( gpGlobals->curtime + 0.3f );
  134. GetOuter()->GetNavigator()->SetMovementActivity( newActivity );
  135. }
  136. }
  137. //-------------------------------------
  138. void CAI_MoveAndShootOverlay::RunShootWhileMove()
  139. {
  140. if ( m_bNoShootWhileMove )
  141. return;
  142. if ( gpGlobals->curtime < m_flSuspendUntilTime )
  143. return;
  144. m_flSuspendUntilTime = MOVESHOOT_DO_NOT_SUSPEND;
  145. CAI_BaseNPC *pOuter = GetOuter();
  146. // keep enemy if dead but try to look for a new one
  147. if (!pOuter->GetEnemy() || !pOuter->GetEnemy()->IsAlive())
  148. {
  149. CBaseEntity *pNewEnemy = pOuter->BestEnemy();
  150. if( pNewEnemy != NULL )
  151. {
  152. //New enemy! Clear the timers and set conditions.
  153. pOuter->SetEnemy( pNewEnemy );
  154. pOuter->SetState( NPC_STATE_COMBAT );
  155. }
  156. else
  157. {
  158. pOuter->ClearAttackConditions();
  159. }
  160. // SetEnemy( NULL );
  161. }
  162. if( !pOuter->GetNavigator()->IsGoalActive() )
  163. return;
  164. if ( GetEnemy() == NULL )
  165. {
  166. if ( pOuter->GetAlternateMoveShootTarget() )
  167. {
  168. // Aim at this other thing if I can't aim at my enemy.
  169. pOuter->AddFacingTarget( pOuter->GetAlternateMoveShootTarget(), pOuter->GetAlternateMoveShootTarget()->GetAbsOrigin(), 1.0, 0.2 );
  170. }
  171. return;
  172. }
  173. bool bMoveAimAtEnemy = CanAimAtEnemy();
  174. UpdateMoveShootActivity( bMoveAimAtEnemy );
  175. if ( !bMoveAimAtEnemy )
  176. {
  177. EndShootWhileMove();
  178. return;
  179. }
  180. Assert( HasAvailableRangeAttack() ); // This should have been caught at task start
  181. Activity activity;
  182. bool bIsReloading = false;
  183. if ( ( activity = pOuter->TranslateActivity( ACT_GESTURE_RELOAD ) ) != ACT_INVALID )
  184. {
  185. bIsReloading = pOuter->IsPlayingGesture( activity );
  186. }
  187. if ( !bIsReloading && HasAvailableRangeAttack() )
  188. {
  189. // time to fire?
  190. if ( pOuter->HasCondition( COND_CAN_RANGE_ATTACK1, false ) )
  191. {
  192. if ( pOuter->GetShotRegulator()->IsInRestInterval() )
  193. {
  194. EndShootWhileMove();
  195. }
  196. else if ( pOuter->GetShotRegulator()->ShouldShoot() )
  197. {
  198. if ( m_bMovingAndShooting || pOuter->OnBeginMoveAndShoot() )
  199. {
  200. m_bMovingAndShooting = true;
  201. pOuter->OnRangeAttack1();
  202. activity = pOuter->TranslateActivity( ACT_GESTURE_RANGE_ATTACK1 );
  203. Assert( activity != ACT_INVALID );
  204. pOuter->RestartGesture( activity );
  205. // FIXME: this seems a bit wacked
  206. pOuter->Weapon_SetActivity( pOuter->Weapon_TranslateActivity( ACT_RANGE_ATTACK1 ), 0 );
  207. }
  208. }
  209. }
  210. else if ( pOuter->HasCondition( COND_NO_PRIMARY_AMMO, false ) )
  211. {
  212. if ( pOuter->GetNavigator()->GetPathTimeToGoal() > 1.0 )
  213. {
  214. activity = pOuter->TranslateActivity( ACT_GESTURE_RELOAD );
  215. if ( activity != ACT_INVALID && GetOuter()->HaveSequenceForActivity( activity ) )
  216. pOuter->AddGesture( activity );
  217. }
  218. }
  219. }
  220. // try to keep facing towards the last known position of the enemy
  221. Vector vecEnemyLKP = pOuter->GetEnemyLKP();
  222. pOuter->AddFacingTarget( pOuter->GetEnemy(), vecEnemyLKP, 1.0, 0.8 );
  223. }
  224. //-------------------------------------
  225. void CAI_MoveAndShootOverlay::EndShootWhileMove()
  226. {
  227. if ( m_bMovingAndShooting )
  228. {
  229. // Reset the shot regulator so that we always start the next motion with a new burst
  230. if ( !GetOuter()->GetShotRegulator()->IsInRestInterval() )
  231. {
  232. GetOuter()->GetShotRegulator()->Reset( false );
  233. }
  234. m_bMovingAndShooting = false;
  235. GetOuter()->OnEndMoveAndShoot();
  236. }
  237. }
  238. //-------------------------------------
  239. void CAI_MoveAndShootOverlay::SuspendMoveAndShoot( float flDuration )
  240. {
  241. EndShootWhileMove();
  242. m_flSuspendUntilTime = gpGlobals->curtime + flDuration;
  243. }
  244. //-------------------------------------
  245. void CAI_MoveAndShootOverlay::SetInitialDelay( float delay )
  246. {
  247. m_initialDelay = delay;
  248. }
  249. //-----------------------------------------------------------------------------