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.

1690 lines
46 KiB

  1. //========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "ai_behavior_lead.h"
  8. #include "ai_goalentity.h"
  9. #include "ai_navigator.h"
  10. #include "ai_speech.h"
  11. #include "ai_senses.h"
  12. #include "ai_playerally.h"
  13. #include "ai_route.h"
  14. #include "ai_pathfinder.h"
  15. #include "sceneentity.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. // Minimum time between leader nags
  19. #define LEAD_NAG_TIME 3.0
  20. #define LEAD_MIN_RETRIEVEDIST_OFFSET 24
  21. //-----------------------------------------------------------------------------
  22. // class CAI_LeadBehavior
  23. //
  24. // Purpose:
  25. //
  26. //-----------------------------------------------------------------------------
  27. BEGIN_SIMPLE_DATADESC( AI_LeadArgs_t )
  28. // Only the flags needs saving
  29. DEFINE_FIELD( flags, FIELD_INTEGER ),
  30. //DEFINE_FIELD( pszGoal, FIELD_STRING ),
  31. //DEFINE_FIELD( pszWaitPoint, FIELD_STRING ),
  32. //DEFINE_FIELD( flWaitDistance, FIELD_FLOAT ),
  33. //DEFINE_FIELD( flLeadDistance, FIELD_FLOAT ),
  34. //DEFINE_FIELD( flRetrieveDistance, FIELD_FLOAT ),
  35. //DEFINE_FIELD( flSuccessDistance, FIELD_FLOAT ),
  36. //DEFINE_FIELD( bRun, FIELD_BOOLEAN ),
  37. //DEFINE_FIELD( bDontSpeakStart, FIELD_BOOLEAN ),
  38. //DEFINE_FIELD( bGagLeader, FIELD_BOOLEAN ),
  39. DEFINE_FIELD( iRetrievePlayer, FIELD_INTEGER ),
  40. DEFINE_FIELD( iRetrieveWaitForSpeak, FIELD_INTEGER ),
  41. DEFINE_FIELD( iComingBackWaitForSpeak, FIELD_INTEGER ),
  42. DEFINE_FIELD( bStopScenesWhenPlayerLost, FIELD_BOOLEAN ),
  43. DEFINE_FIELD( bLeadDuringCombat, FIELD_BOOLEAN ),
  44. END_DATADESC();
  45. BEGIN_DATADESC( CAI_LeadBehavior )
  46. DEFINE_EMBEDDED( m_args ),
  47. // m_pSink (reconnected on load)
  48. DEFINE_FIELD( m_hSinkImplementor, FIELD_EHANDLE ),
  49. DEFINE_FIELD( m_goal, FIELD_POSITION_VECTOR ),
  50. DEFINE_FIELD( m_goalyaw, FIELD_FLOAT ),
  51. DEFINE_FIELD( m_waitpoint, FIELD_POSITION_VECTOR ),
  52. DEFINE_FIELD( m_waitdistance, FIELD_FLOAT ),
  53. DEFINE_FIELD( m_leaddistance, FIELD_FLOAT ),
  54. DEFINE_FIELD( m_retrievedistance, FIELD_FLOAT ),
  55. DEFINE_FIELD( m_successdistance, FIELD_FLOAT ),
  56. DEFINE_FIELD( m_weaponname, FIELD_STRING ),
  57. DEFINE_FIELD( m_run, FIELD_BOOLEAN ),
  58. DEFINE_FIELD( m_gagleader, FIELD_BOOLEAN ),
  59. DEFINE_FIELD( m_hasspokenstart, FIELD_BOOLEAN ),
  60. DEFINE_FIELD( m_hasspokenarrival, FIELD_BOOLEAN ),
  61. DEFINE_FIELD( m_hasPausedScenes, FIELD_BOOLEAN ),
  62. DEFINE_FIELD( m_flSpeakNextNagTime, FIELD_TIME ),
  63. DEFINE_FIELD( m_flWeaponSafetyTimeOut, FIELD_TIME ),
  64. DEFINE_FIELD( m_flNextLeadIdle, FIELD_TIME ),
  65. DEFINE_FIELD( m_bInitialAheadTest, FIELD_BOOLEAN ),
  66. DEFINE_EMBEDDED( m_MoveMonitor ),
  67. DEFINE_EMBEDDED( m_LostTimer ),
  68. DEFINE_EMBEDDED( m_LostLOSTimer ),
  69. END_DATADESC();
  70. //-----------------------------------------------------------------------------
  71. void CAI_LeadBehavior::OnRestore()
  72. {
  73. CBaseEntity *pSinkImplementor = m_hSinkImplementor;
  74. if ( pSinkImplementor )
  75. {
  76. m_pSink = dynamic_cast<CAI_LeadBehaviorHandler *>(pSinkImplementor);
  77. if ( !m_pSink )
  78. {
  79. DevMsg( "Failed to reconnect to CAI_LeadBehaviorHandler\n" );
  80. m_hSinkImplementor = NULL;
  81. }
  82. }
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose: Draw any text overlays
  86. // Input : Previous text offset from the top
  87. // Output : Current text offset from the top
  88. //-----------------------------------------------------------------------------
  89. int CAI_LeadBehavior::DrawDebugTextOverlays( int text_offset )
  90. {
  91. char tempstr[ 512 ];
  92. int offset;
  93. offset = BaseClass::DrawDebugTextOverlays( text_offset );
  94. if ( GetOuter()->m_debugOverlays & OVERLAY_TEXT_BIT )
  95. {
  96. if ( HasGoal() )
  97. {
  98. Q_snprintf( tempstr, sizeof(tempstr), "Goal: %s %s", m_args.pszGoal, VecToString( m_goal ) );
  99. GetOuter()->EntityText( offset, tempstr, 0 );
  100. offset++;
  101. }
  102. else
  103. {
  104. Q_snprintf( tempstr, sizeof(tempstr), "Goal: None" );
  105. GetOuter()->EntityText( offset, tempstr, 0 );
  106. offset++;
  107. }
  108. }
  109. return offset;
  110. }
  111. //-----------------------------------------------------------------------------
  112. //-----------------------------------------------------------------------------
  113. bool CAI_LeadBehavior::IsNavigationUrgent( void )
  114. {
  115. #if defined( HL2_DLL )
  116. if( HasGoal() && !hl2_episodic.GetBool() )
  117. {
  118. return (GetOuter()->Classify() == CLASS_PLAYER_ALLY_VITAL);
  119. }
  120. #endif
  121. return BaseClass::IsNavigationUrgent();
  122. }
  123. //-------------------------------------
  124. void CAI_LeadBehavior::LeadPlayer( const AI_LeadArgs_t &leadArgs, CAI_LeadBehaviorHandler *pSink )
  125. {
  126. #ifndef CSTRIKE_DLL
  127. CAI_PlayerAlly *pOuter = dynamic_cast<CAI_PlayerAlly*>(GetOuter());
  128. if ( pOuter && AI_IsSinglePlayer() )
  129. {
  130. pOuter->SetSpeechTarget( UTIL_GetLocalPlayer() );
  131. }
  132. #endif
  133. if( SetGoal( leadArgs ) )
  134. {
  135. SetCondition( COND_PROVOKED );
  136. Connect( pSink );
  137. NotifyChangeBehaviorStatus();
  138. }
  139. else
  140. {
  141. DevMsg( "*** Warning! LeadPlayer() has a NULL Goal Ent\n" );
  142. }
  143. }
  144. //-------------------------------------
  145. void CAI_LeadBehavior::StopLeading( void )
  146. {
  147. ClearGoal();
  148. m_pSink = NULL;
  149. NotifyChangeBehaviorStatus();
  150. }
  151. //-------------------------------------
  152. bool CAI_LeadBehavior::CanSelectSchedule()
  153. {
  154. if ( !AI_GetSinglePlayer() || AI_GetSinglePlayer()->IsDead() )
  155. return false;
  156. bool fAttacked = ( HasCondition( COND_LIGHT_DAMAGE ) || HasCondition( COND_HEAVY_DAMAGE ) );
  157. bool fNonCombat = ( GetNpcState() == NPC_STATE_IDLE || GetNpcState() == NPC_STATE_ALERT );
  158. return ( !fAttacked && (fNonCombat || m_args.bLeadDuringCombat) && HasGoal() );
  159. }
  160. //-------------------------------------
  161. void CAI_LeadBehavior::BeginScheduleSelection()
  162. {
  163. SetTarget( AI_GetSinglePlayer() );
  164. CAI_Expresser *pExpresser = GetOuter()->GetExpresser();
  165. if ( pExpresser )
  166. pExpresser->ClearSpokeConcept( TLK_LEAD_ARRIVAL );
  167. }
  168. //-------------------------------------
  169. bool CAI_LeadBehavior::SetGoal( const AI_LeadArgs_t &args )
  170. {
  171. CBaseEntity *pGoalEnt;
  172. pGoalEnt = gEntList.FindEntityByName( NULL, args.pszGoal );
  173. if ( !pGoalEnt )
  174. return false;
  175. m_args = args; // @Q (toml 08-13-02): need to copy string?
  176. m_goal = pGoalEnt->GetLocalOrigin();
  177. m_goalyaw = (args.flags & AILF_USE_GOAL_FACING) ? pGoalEnt->GetLocalAngles().y : -1;
  178. m_waitpoint = vec3_origin;
  179. m_waitdistance = args.flWaitDistance;
  180. m_leaddistance = args.flLeadDistance ? args.flLeadDistance : 64;
  181. m_retrievedistance = args.flRetrieveDistance ? args.flRetrieveDistance : (m_leaddistance + LEAD_MIN_RETRIEVEDIST_OFFSET);
  182. m_successdistance = args.flSuccessDistance ? args.flSuccessDistance : 0;
  183. m_run = args.bRun;
  184. m_gagleader = args.bGagLeader;
  185. m_hasspokenstart = args.bDontSpeakStart;
  186. m_hasspokenarrival = false;
  187. m_hasPausedScenes = false;
  188. m_flSpeakNextNagTime = 0;
  189. m_flWeaponSafetyTimeOut = 0;
  190. m_flNextLeadIdle = gpGlobals->curtime + 10;
  191. m_bInitialAheadTest = true;
  192. if ( args.pszWaitPoint && args.pszWaitPoint[0] )
  193. {
  194. CBaseEntity *pWaitPoint = gEntList.FindEntityByName( NULL, args.pszWaitPoint );
  195. if ( pWaitPoint )
  196. {
  197. m_waitpoint = pWaitPoint->GetLocalOrigin();
  198. }
  199. }
  200. return true;
  201. }
  202. //-----------------------------------------------------------------------------
  203. // Purpose:
  204. //-----------------------------------------------------------------------------
  205. bool CAI_LeadBehavior::GetClosestPointOnRoute( const Vector &targetPos, Vector *pVecClosestPoint )
  206. {
  207. AI_Waypoint_t *waypoint = GetOuter()->GetNavigator()->GetPath()->GetCurWaypoint();
  208. AI_Waypoint_t *builtwaypoints = NULL;
  209. if ( !waypoint )
  210. {
  211. // We arrive here twice when lead behaviour starts:
  212. // - When the lead behaviour is first enabled. We have no schedule. We want to know if the player is ahead of us.
  213. // - A frame later when we've chosen to lead the player, but we still haven't built our route. We know that the
  214. // the player isn't lagging, so it's safe to go ahead and simply say he's ahead of us. This avoids building
  215. // the temp route twice.
  216. if ( IsCurSchedule( SCHED_LEAD_PLAYER, false ) )
  217. return true;
  218. // Build a temp route to the gold and use that
  219. builtwaypoints = GetOuter()->GetPathfinder()->BuildRoute( GetOuter()->GetAbsOrigin(), m_goal, NULL, GetOuter()->GetDefaultNavGoalTolerance(), GetOuter()->GetNavType(), bits_BUILD_GET_CLOSE );
  220. if ( !builtwaypoints )
  221. return false;
  222. GetOuter()->GetPathfinder()->UnlockRouteNodes( builtwaypoints );
  223. waypoint = builtwaypoints;
  224. }
  225. // Find the nearest node to the target (going forward)
  226. float flNearestDist2D = 999999999;
  227. float flNearestDist = 999999999;
  228. float flPathDist, flPathDist2D;
  229. Vector vecNearestPoint;
  230. Vector vecPrevPos = GetOuter()->GetAbsOrigin();
  231. for ( ; (waypoint != NULL) ; waypoint = waypoint->GetNext() )
  232. {
  233. // Find the closest point on the line segment on the path
  234. Vector vecClosest;
  235. CalcClosestPointOnLineSegment( targetPos, vecPrevPos, waypoint->GetPos(), vecClosest );
  236. /*
  237. if ( builtwaypoints )
  238. {
  239. NDebugOverlay::Line( vecPrevPos, waypoint->GetPos(), 0,0,255,true, 10.0 );
  240. }
  241. */
  242. vecPrevPos = waypoint->GetPos();
  243. // Find the distance between this test point and our goal point
  244. flPathDist2D = vecClosest.AsVector2D().DistToSqr( targetPos.AsVector2D() );
  245. if ( flPathDist2D > flNearestDist2D )
  246. continue;
  247. flPathDist = vecClosest.z - targetPos.z;
  248. flPathDist *= flPathDist;
  249. flPathDist += flPathDist2D;
  250. if (( flPathDist2D == flNearestDist2D ) && ( flPathDist >= flNearestDist ))
  251. continue;
  252. flNearestDist2D = flPathDist2D;
  253. flNearestDist = flPathDist;
  254. vecNearestPoint = vecClosest;
  255. }
  256. if ( builtwaypoints )
  257. {
  258. //NDebugOverlay::Line( vecNearestPoint, targetPos, 0,255,0,true, 10.0 );
  259. DeleteAll( builtwaypoints );
  260. }
  261. *pVecClosestPoint = vecNearestPoint;
  262. return true;
  263. }
  264. //-----------------------------------------------------------------------------
  265. // Purpose: Return true if the player is further ahead on the lead route than I am
  266. //-----------------------------------------------------------------------------
  267. bool CAI_LeadBehavior::PlayerIsAheadOfMe( bool bForce )
  268. {
  269. // Find the nearest point on our route to the player, and see if that's further
  270. // ahead of us than our nearest point.
  271. // If we're not leading, our route doesn't lead to the goal, so we can't use it.
  272. // If we just started leading, go ahead and test, and we'll build a temp route.
  273. if ( !m_bInitialAheadTest && !IsCurSchedule( SCHED_LEAD_PLAYER, false ) && !bForce )
  274. return false;
  275. m_bInitialAheadTest = false;
  276. Vector vecClosestPoint;
  277. if ( GetClosestPointOnRoute( AI_GetSinglePlayer()->GetAbsOrigin(), &vecClosestPoint ) )
  278. {
  279. // If the closest point is not right next to me, then
  280. // the player is somewhere ahead of me on the route.
  281. if ( (vecClosestPoint - GetOuter()->GetAbsOrigin()).LengthSqr() > (32*32) )
  282. return true;
  283. }
  284. return false;
  285. }
  286. //-----------------------------------------------------------------------------
  287. // Purpose:
  288. //-----------------------------------------------------------------------------
  289. void CAI_LeadBehavior::GatherConditions( void )
  290. {
  291. BaseClass::GatherConditions();
  292. if ( HasGoal() )
  293. {
  294. // Fix for bad transition case (to investigate)
  295. if ( ( WorldSpaceCenter() - m_goal ).LengthSqr() > (64*64) && IsCurSchedule( SCHED_LEAD_AWAIT_SUCCESS, false) )
  296. {
  297. GetOuter()->ClearSchedule( "Lead behavior - bad transition?" );
  298. }
  299. // We have to collect data about the person we're leading around.
  300. CBaseEntity *pFollower = AI_GetSinglePlayer();
  301. if( pFollower )
  302. {
  303. ClearCondition( COND_LEAD_FOLLOWER_VERY_CLOSE );
  304. ClearCondition( COND_LEAD_FOLLOWER_MOVING_TOWARDS_ME );
  305. // Check distance to the follower
  306. float flFollowerDist = ( WorldSpaceCenter() - pFollower->WorldSpaceCenter() ).Length();
  307. bool bLagging = flFollowerDist > (m_leaddistance*4);
  308. if ( bLagging )
  309. {
  310. if ( PlayerIsAheadOfMe() )
  311. {
  312. bLagging = false;
  313. }
  314. }
  315. // Player heading towards me?
  316. // Only factor this in if you're not too far from them
  317. if ( flFollowerDist < (m_leaddistance*4) )
  318. {
  319. Vector vecVelocity = pFollower->GetSmoothedVelocity();
  320. if ( VectorNormalize(vecVelocity) > 50 )
  321. {
  322. Vector vecToPlayer = (GetAbsOrigin() - pFollower->GetAbsOrigin());
  323. VectorNormalize( vecToPlayer );
  324. if ( DotProduct( vecVelocity, vecToPlayer ) > 0.5 )
  325. {
  326. SetCondition( COND_LEAD_FOLLOWER_MOVING_TOWARDS_ME );
  327. bLagging = false;
  328. }
  329. }
  330. }
  331. // If he's outside our lag range, consider him lagging
  332. if ( bLagging )
  333. {
  334. SetCondition( COND_LEAD_FOLLOWER_LAGGING );
  335. ClearCondition( COND_LEAD_FOLLOWER_NOT_LAGGING );
  336. }
  337. else
  338. {
  339. ClearCondition( COND_LEAD_FOLLOWER_LAGGING );
  340. SetCondition( COND_LEAD_FOLLOWER_NOT_LAGGING );
  341. // If he's really close, note that
  342. if ( flFollowerDist < m_leaddistance )
  343. {
  344. SetCondition( COND_LEAD_FOLLOWER_VERY_CLOSE );
  345. }
  346. }
  347. // To be considered not lagging, the follower must be visible, and within the lead distance
  348. if ( GetOuter()->FVisible( pFollower ) && GetOuter()->GetSenses()->ShouldSeeEntity( pFollower ) )
  349. {
  350. SetCondition( COND_LEAD_HAVE_FOLLOWER_LOS );
  351. m_LostLOSTimer.Stop();
  352. }
  353. else
  354. {
  355. ClearCondition( COND_LEAD_HAVE_FOLLOWER_LOS );
  356. // We don't have a LOS. But if we did have LOS, don't clear it until the timer is up.
  357. if ( m_LostLOSTimer.IsRunning() )
  358. {
  359. if ( m_LostLOSTimer.Expired() )
  360. {
  361. SetCondition( COND_LEAD_FOLLOWER_LAGGING );
  362. ClearCondition( COND_LEAD_FOLLOWER_NOT_LAGGING );
  363. }
  364. }
  365. else
  366. {
  367. m_LostLOSTimer.Start();
  368. }
  369. }
  370. // Now we want to see if the follower is lost. Being lost means being (far away || out of LOS )
  371. // && some time has passed. Also, lagging players are considered lost if the NPC's never delivered
  372. // the start speech, because it means the NPC should run to the player to start the lead.
  373. if( HasCondition( COND_LEAD_FOLLOWER_LAGGING ) )
  374. {
  375. if ( !m_hasspokenstart )
  376. {
  377. SetCondition( COND_LEAD_FOLLOWER_LOST );
  378. }
  379. else
  380. {
  381. if ( m_args.bStopScenesWhenPlayerLost )
  382. {
  383. // Try and stop me speaking my monolog, if I am
  384. if ( !m_hasPausedScenes && IsRunningScriptedScene( GetOuter() ) )
  385. {
  386. //Msg("Stopping scenes.\n");
  387. PauseActorsScriptedScenes( GetOuter(), false );
  388. m_hasPausedScenes = true;
  389. }
  390. }
  391. if( m_LostTimer.IsRunning() )
  392. {
  393. if( m_LostTimer.Expired() )
  394. {
  395. SetCondition( COND_LEAD_FOLLOWER_LOST );
  396. }
  397. }
  398. else
  399. {
  400. m_LostTimer.Start();
  401. }
  402. }
  403. }
  404. else
  405. {
  406. // If I was speaking a monolog, resume it
  407. if ( m_args.bStopScenesWhenPlayerLost && m_hasPausedScenes )
  408. {
  409. if ( IsRunningScriptedScene( GetOuter() ) )
  410. {
  411. //Msg("Resuming scenes.\n");
  412. ResumeActorsScriptedScenes( GetOuter(), false );
  413. }
  414. m_hasPausedScenes = false;
  415. }
  416. m_LostTimer.Stop();
  417. ClearCondition( COND_LEAD_FOLLOWER_LOST );
  418. }
  419. // Evaluate for success
  420. // Success right now means being stationary, close to the goal, and having the player close by
  421. if ( !( m_args.flags & AILF_NO_DEF_SUCCESS ) )
  422. {
  423. ClearCondition( COND_LEAD_SUCCESS );
  424. // Check Z first, and only check 2d if we're within that
  425. bool bWithinZ = fabs(GetLocalOrigin().z - m_goal.z) < 64;
  426. if ( bWithinZ && (GetLocalOrigin() - m_goal).Length2D() <= 64 )
  427. {
  428. if ( HasCondition( COND_LEAD_FOLLOWER_VERY_CLOSE ) )
  429. {
  430. SetCondition( COND_LEAD_SUCCESS );
  431. }
  432. else if ( m_successdistance )
  433. {
  434. float flDistSqr = (pFollower->GetAbsOrigin() - GetLocalOrigin()).Length2DSqr();
  435. if ( flDistSqr < (m_successdistance*m_successdistance) )
  436. {
  437. SetCondition( COND_LEAD_SUCCESS );
  438. }
  439. }
  440. }
  441. }
  442. if ( m_MoveMonitor.IsMarkSet() && m_MoveMonitor.TargetMoved( pFollower ) )
  443. SetCondition( COND_LEAD_FOLLOWER_MOVED_FROM_MARK );
  444. else
  445. ClearCondition( COND_LEAD_FOLLOWER_MOVED_FROM_MARK );
  446. }
  447. }
  448. if( m_args.bLeadDuringCombat )
  449. {
  450. ClearCondition( COND_LIGHT_DAMAGE );
  451. ClearCondition( COND_HEAVY_DAMAGE );
  452. }
  453. }
  454. //-------------------------------------
  455. int CAI_LeadBehavior::SelectSchedule()
  456. {
  457. if ( HasGoal() )
  458. {
  459. if( HasCondition(COND_LEAD_SUCCESS) )
  460. {
  461. return SCHED_LEAD_SUCCEED;
  462. }
  463. // Player's here, but does he have the weapon we want him to have?
  464. if ( m_weaponname != NULL_STRING )
  465. {
  466. CBasePlayer *pFollower = AI_GetSinglePlayer();
  467. if ( pFollower && !pFollower->Weapon_OwnsThisType( STRING(m_weaponname) ) )
  468. {
  469. // If the safety timeout has run out, just give the player the weapon
  470. if ( !m_flWeaponSafetyTimeOut || (m_flWeaponSafetyTimeOut > gpGlobals->curtime) )
  471. return SCHED_LEAD_PLAYERNEEDSWEAPON;
  472. string_t iszItem = AllocPooledString( "weapon_bugbait" );
  473. pFollower->GiveNamedItem( STRING(iszItem) );
  474. }
  475. }
  476. // If we have a waitpoint, we want to wait at it for the player.
  477. if( HasWaitPoint() && !PlayerIsAheadOfMe( true ) )
  478. {
  479. bool bKeepWaiting = true;
  480. // If we have no wait distance, trigger as soon as the player comes in view
  481. if ( !m_waitdistance )
  482. {
  483. if ( HasCondition( COND_SEE_PLAYER ) )
  484. {
  485. // We've spotted the player, so stop waiting
  486. bKeepWaiting = false;
  487. }
  488. }
  489. else
  490. {
  491. // We have to collect data about the person we're leading around.
  492. CBaseEntity *pFollower = AI_GetSinglePlayer();
  493. if( pFollower )
  494. {
  495. float flFollowerDist = ( WorldSpaceCenter() - pFollower->WorldSpaceCenter() ).Length();
  496. if ( flFollowerDist < m_waitdistance )
  497. {
  498. bKeepWaiting = false;
  499. }
  500. }
  501. }
  502. // Player still not here?
  503. if ( bKeepWaiting )
  504. return SCHED_LEAD_WAITFORPLAYER;
  505. // We're finished waiting
  506. m_waitpoint = vec3_origin;
  507. Speak( TLK_LEAD_WAITOVER );
  508. // Don't speak the start line, because we've said
  509. m_hasspokenstart = true;
  510. return SCHED_WAIT_FOR_SPEAK_FINISH;
  511. }
  512. // If we haven't spoken our start speech, do that first
  513. if ( !m_hasspokenstart )
  514. {
  515. if ( HasCondition(COND_LEAD_HAVE_FOLLOWER_LOS) && HasCondition(COND_LEAD_FOLLOWER_VERY_CLOSE) )
  516. return SCHED_LEAD_SPEAK_START;
  517. // We haven't spoken to him, and we still need to. Go get him.
  518. return SCHED_LEAD_RETRIEVE;
  519. }
  520. if( HasCondition( COND_LEAD_FOLLOWER_LOST ) )
  521. {
  522. if( m_args.iRetrievePlayer )
  523. {
  524. // If not, we want to go get the player.
  525. DevMsg( GetOuter(), "Follower lost. Spoke COMING_BACK.\n");
  526. Speak( TLK_LEAD_COMINGBACK );
  527. m_MoveMonitor.ClearMark();
  528. // If we spoke something, wait for it to finish
  529. if ( m_args.iComingBackWaitForSpeak && IsSpeaking() )
  530. return SCHED_LEAD_SPEAK_THEN_RETRIEVE_PLAYER;
  531. return SCHED_LEAD_RETRIEVE;
  532. }
  533. else
  534. {
  535. // Just stay right here and wait.
  536. return SCHED_LEAD_WAITFORPLAYERIDLE;
  537. }
  538. }
  539. if( HasCondition( COND_LEAD_FOLLOWER_LAGGING ) )
  540. {
  541. DevMsg( GetOuter(), "Follower lagging. Spoke CATCHUP.\n");
  542. Speak( TLK_LEAD_CATCHUP );
  543. return SCHED_LEAD_PAUSE;
  544. }
  545. else
  546. {
  547. // If we're at the goal, wait for the player to get here
  548. if ( ( WorldSpaceCenter() - m_goal ).LengthSqr() < (64*64) )
  549. return SCHED_LEAD_AWAIT_SUCCESS;
  550. // If we were retrieving the player, speak the resume
  551. if ( IsCurSchedule( SCHED_LEAD_RETRIEVE, false ) || IsCurSchedule( SCHED_LEAD_WAITFORPLAYERIDLE, false ) )
  552. {
  553. Speak( TLK_LEAD_RETRIEVE );
  554. // If we spoke something, wait for it to finish, if the mapmakers wants us to
  555. if ( m_args.iRetrieveWaitForSpeak && IsSpeaking() )
  556. return SCHED_LEAD_SPEAK_THEN_LEAD_PLAYER;
  557. }
  558. DevMsg( GetOuter(), "Leading Follower.\n");
  559. return SCHED_LEAD_PLAYER;
  560. }
  561. }
  562. return BaseClass::SelectSchedule();
  563. }
  564. //-------------------------------------
  565. int CAI_LeadBehavior::TranslateSchedule( int scheduleType )
  566. {
  567. bool bInCombat = (m_args.bLeadDuringCombat && GetOuter()->GetState() == NPC_STATE_COMBAT);
  568. switch( scheduleType )
  569. {
  570. case SCHED_LEAD_PAUSE:
  571. if( bInCombat )
  572. return SCHED_LEAD_PAUSE_COMBAT;
  573. break;
  574. }
  575. return BaseClass::TranslateSchedule( scheduleType );
  576. }
  577. //-------------------------------------
  578. bool CAI_LeadBehavior::IsCurTaskContinuousMove()
  579. {
  580. const Task_t *pCurTask = GetCurTask();
  581. if ( pCurTask && pCurTask->iTask == TASK_LEAD_MOVE_TO_RANGE )
  582. return true;
  583. return BaseClass::IsCurTaskContinuousMove();
  584. }
  585. //-------------------------------------
  586. void CAI_LeadBehavior::StartTask( const Task_t *pTask )
  587. {
  588. switch ( pTask->iTask )
  589. {
  590. case TASK_LEAD_FACE_GOAL:
  591. {
  592. if ( m_goalyaw != -1 )
  593. {
  594. GetMotor()->SetIdealYaw( m_goalyaw );
  595. }
  596. TaskComplete();
  597. break;
  598. }
  599. case TASK_LEAD_SUCCEED:
  600. {
  601. Speak( TLK_LEAD_SUCCESS );
  602. NotifyEvent( LBE_SUCCESS );
  603. break;
  604. }
  605. case TASK_LEAD_ARRIVE:
  606. {
  607. // Only speak the first time we arrive
  608. if ( !m_hasspokenarrival )
  609. {
  610. Speak( TLK_LEAD_ARRIVAL );
  611. NotifyEvent( LBE_ARRIVAL );
  612. m_hasspokenarrival = true;
  613. }
  614. else
  615. {
  616. TaskComplete();
  617. }
  618. break;
  619. }
  620. case TASK_STOP_LEADING:
  621. {
  622. ClearGoal();
  623. TaskComplete();
  624. break;
  625. }
  626. case TASK_GET_PATH_TO_LEAD_GOAL:
  627. {
  628. if ( GetNavigator()->SetGoal( m_goal ) )
  629. {
  630. TaskComplete();
  631. }
  632. else
  633. {
  634. TaskFail("NO PATH");
  635. }
  636. break;
  637. }
  638. case TASK_LEAD_GET_PATH_TO_WAITPOINT:
  639. {
  640. if ( GetNavigator()->SetGoal( m_waitpoint ) )
  641. {
  642. TaskComplete();
  643. }
  644. else
  645. {
  646. TaskFail("NO PATH");
  647. }
  648. break;
  649. }
  650. case TASK_LEAD_WALK_PATH:
  651. {
  652. // If we're leading, and we're supposed to run, run instead of walking
  653. if ( m_run &&
  654. ( IsCurSchedule( SCHED_LEAD_WAITFORPLAYER, false ) || IsCurSchedule( SCHED_LEAD_PLAYER, false ) || IsCurSchedule( SCHED_LEAD_SPEAK_THEN_LEAD_PLAYER, false )|| IsCurSchedule( SCHED_LEAD_RETRIEVE, false ) ) )
  655. {
  656. ChainStartTask( TASK_RUN_PATH );
  657. }
  658. else
  659. {
  660. ChainStartTask( TASK_WALK_PATH );
  661. }
  662. break;
  663. }
  664. case TASK_LEAD_WAVE_TO_PLAYER:
  665. {
  666. // Wave to the player if we can see him. Otherwise, just idle.
  667. if ( HasCondition( COND_SEE_PLAYER ) )
  668. {
  669. Speak( TLK_LEAD_ATTRACTPLAYER );
  670. if ( HaveSequenceForActivity(ACT_SIGNAL1) )
  671. {
  672. SetActivity(ACT_SIGNAL1);
  673. }
  674. }
  675. else
  676. {
  677. SetActivity(ACT_IDLE);
  678. }
  679. TaskComplete();
  680. break;
  681. }
  682. case TASK_LEAD_PLAYER_NEEDS_WEAPON:
  683. {
  684. float flAvailableTime = GetOuter()->GetExpresser()->GetSemaphoreAvailableTime( GetOuter() );
  685. // if someone else is talking, don't speak
  686. if ( flAvailableTime <= gpGlobals->curtime )
  687. {
  688. Speak( TLK_LEAD_MISSINGWEAPON );
  689. }
  690. SetActivity(ACT_IDLE);
  691. TaskComplete();
  692. break;
  693. }
  694. case TASK_LEAD_SPEAK_START:
  695. {
  696. m_hasspokenstart = true;
  697. Speak( TLK_LEAD_START );
  698. SetActivity(ACT_IDLE);
  699. TaskComplete();
  700. break;
  701. }
  702. case TASK_LEAD_MOVE_TO_RANGE:
  703. {
  704. // If we haven't spoken our start speech, move closer
  705. if ( !m_hasspokenstart)
  706. {
  707. ChainStartTask( TASK_MOVE_TO_GOAL_RANGE, m_leaddistance - 24 );
  708. }
  709. else
  710. {
  711. ChainStartTask( TASK_MOVE_TO_GOAL_RANGE, m_retrievedistance );
  712. }
  713. break;
  714. }
  715. case TASK_LEAD_RETRIEVE_WAIT:
  716. {
  717. m_MoveMonitor.SetMark( AI_GetSinglePlayer(), 24 );
  718. ChainStartTask( TASK_WAIT_INDEFINITE );
  719. break;
  720. }
  721. case TASK_STOP_MOVING:
  722. {
  723. BaseClass::StartTask( pTask);
  724. if ( IsCurSchedule( SCHED_LEAD_PAUSE, false ) && pTask->flTaskData == 1 )
  725. {
  726. GetNavigator()->SetArrivalDirection( GetTarget() );
  727. }
  728. break;
  729. }
  730. case TASK_WAIT_FOR_SPEAK_FINISH:
  731. {
  732. BaseClass::StartTask( pTask);
  733. if( GetOuter()->GetState() == NPC_STATE_COMBAT )
  734. {
  735. // Don't stand around jabbering in combat.
  736. TaskComplete();
  737. }
  738. // If we're not supposed to wait for the player, don't wait for speech to finish.
  739. // Instead, just wait a wee tad, and then start moving. NPC will speak on the go.
  740. if ( TaskIsRunning() && !m_args.iRetrievePlayer )
  741. {
  742. if ( gpGlobals->curtime - GetOuter()->GetTimeTaskStarted() > 0.3 )
  743. {
  744. TaskComplete();
  745. }
  746. }
  747. break;
  748. }
  749. default:
  750. BaseClass::StartTask( pTask);
  751. }
  752. }
  753. //-------------------------------------
  754. void CAI_LeadBehavior::RunTask( const Task_t *pTask )
  755. {
  756. switch ( pTask->iTask )
  757. {
  758. case TASK_LEAD_SUCCEED:
  759. {
  760. if ( !IsSpeaking() )
  761. {
  762. TaskComplete();
  763. NotifyEvent( LBE_DONE );
  764. }
  765. break;
  766. }
  767. case TASK_LEAD_ARRIVE:
  768. {
  769. if ( !IsSpeaking() )
  770. {
  771. TaskComplete();
  772. NotifyEvent( LBE_ARRIVAL_DONE );
  773. }
  774. break;
  775. }
  776. case TASK_LEAD_MOVE_TO_RANGE:
  777. {
  778. // If we haven't spoken our start speech, move closer
  779. if ( !m_hasspokenstart)
  780. {
  781. ChainRunTask( TASK_MOVE_TO_GOAL_RANGE, m_leaddistance - 24 );
  782. }
  783. else
  784. {
  785. ChainRunTask( TASK_MOVE_TO_GOAL_RANGE, m_retrievedistance );
  786. if ( !TaskIsComplete() )
  787. {
  788. // Transition to a walk when we get near the player
  789. // Check Z first, and only check 2d if we're within that
  790. Vector vecGoalPos = GetNavigator()->GetGoalPos();
  791. float distance = fabs(vecGoalPos.z - GetLocalOrigin().z);
  792. bool bWithinZ = false;
  793. if ( distance < m_retrievedistance )
  794. {
  795. distance = ( vecGoalPos - GetLocalOrigin() ).Length2D();
  796. bWithinZ = true;
  797. }
  798. if ( distance > m_retrievedistance )
  799. {
  800. Activity followActivity = ACT_WALK;
  801. if ( GetOuter()->GetState() == NPC_STATE_COMBAT || (!bWithinZ || distance < (m_retrievedistance*4)) && GetOuter()->GetState() != NPC_STATE_COMBAT )
  802. {
  803. followActivity = ACT_RUN;
  804. }
  805. // Don't confuse move and shoot by resetting the activity every think
  806. Activity curActivity = GetNavigator()->GetMovementActivity();
  807. switch( curActivity )
  808. {
  809. case ACT_WALK_AIM: curActivity = ACT_WALK; break;
  810. case ACT_RUN_AIM: curActivity = ACT_RUN; break;
  811. }
  812. if ( curActivity != followActivity )
  813. {
  814. GetNavigator()->SetMovementActivity(followActivity);
  815. }
  816. GetNavigator()->SetArrivalDirection( GetOuter()->GetTarget() );
  817. }
  818. }
  819. }
  820. break;
  821. }
  822. case TASK_LEAD_RETRIEVE_WAIT:
  823. {
  824. ChainRunTask( TASK_WAIT_INDEFINITE );
  825. break;
  826. }
  827. case TASK_LEAD_WALK_PATH:
  828. {
  829. // If we're leading, and we're supposed to run, run instead of walking
  830. if ( m_run &&
  831. ( IsCurSchedule( SCHED_LEAD_WAITFORPLAYER, false ) || IsCurSchedule( SCHED_LEAD_PLAYER, false ) || IsCurSchedule( SCHED_LEAD_SPEAK_THEN_LEAD_PLAYER, false )|| IsCurSchedule( SCHED_LEAD_RETRIEVE, false ) ) )
  832. {
  833. ChainRunTask( TASK_RUN_PATH );
  834. }
  835. else
  836. {
  837. ChainRunTask( TASK_WALK_PATH );
  838. }
  839. // While we're walking
  840. if ( TaskIsRunning() && IsCurSchedule( SCHED_LEAD_PLAYER, false ) )
  841. {
  842. // If we're not speaking, and we haven't tried for a while, try to speak lead idle
  843. if ( m_flNextLeadIdle < gpGlobals->curtime && !IsSpeaking() )
  844. {
  845. m_flNextLeadIdle = gpGlobals->curtime + RandomFloat( 10,15 );
  846. if ( !m_args.iRetrievePlayer && HasCondition( COND_LEAD_FOLLOWER_LOST ) && HasCondition(COND_SEE_PLAYER) )
  847. {
  848. Speak( TLK_LEAD_COMINGBACK );
  849. }
  850. else
  851. {
  852. Speak( TLK_LEAD_IDLE );
  853. }
  854. }
  855. }
  856. break;
  857. }
  858. default:
  859. BaseClass::RunTask( pTask);
  860. }
  861. }
  862. //-------------------------------------
  863. bool CAI_LeadBehavior::Speak( AIConcept_t concept )
  864. {
  865. CAI_Expresser *pExpresser = GetOuter()->GetExpresser();
  866. if ( !pExpresser )
  867. return false;
  868. // If the leader is gagged, don't speak any lead speech
  869. if ( m_gagleader )
  870. return false;
  871. // If we haven't said the start speech, don't nag
  872. bool bNag = ( FStrEq(concept,TLK_LEAD_COMINGBACK) || FStrEq(concept, TLK_LEAD_CATCHUP) || FStrEq(concept, TLK_LEAD_RETRIEVE) );
  873. if ( !m_hasspokenstart && bNag )
  874. return false;
  875. if ( hl2_episodic.GetBool() )
  876. {
  877. // If we're a player ally, only speak the concept if we're allowed to.
  878. // This allows the response rules to control it better (i.e. handles respeakdelay)
  879. // We ignore nag timers for this, because the response rules will control refire rates.
  880. CAI_PlayerAlly *pAlly = dynamic_cast<CAI_PlayerAlly*>(GetOuter());
  881. if ( pAlly )
  882. return pAlly->SpeakIfAllowed( concept, GetConceptModifiers( concept ) );
  883. }
  884. // Don't spam Nags
  885. if ( bNag )
  886. {
  887. if ( m_flSpeakNextNagTime > gpGlobals->curtime )
  888. {
  889. DevMsg( GetOuter(), "Leader didn't speak due to Nag timer.\n");
  890. return false;
  891. }
  892. }
  893. if ( pExpresser->Speak( concept, GetConceptModifiers( concept ) ) )
  894. {
  895. m_flSpeakNextNagTime = gpGlobals->curtime + LEAD_NAG_TIME;
  896. return true;
  897. }
  898. return false;
  899. }
  900. //-------------------------------------
  901. bool CAI_LeadBehavior::IsSpeaking()
  902. {
  903. CAI_Expresser *pExpresser = GetOuter()->GetExpresser();
  904. if ( !pExpresser )
  905. return false;
  906. return pExpresser->IsSpeaking();
  907. }
  908. //-------------------------------------
  909. bool CAI_LeadBehavior::Connect( CAI_LeadBehaviorHandler *pSink )
  910. {
  911. m_pSink = pSink;
  912. m_hSinkImplementor = dynamic_cast<CBaseEntity *>(pSink);
  913. if ( m_hSinkImplementor == NULL )
  914. DevMsg( 2, "Note: CAI_LeadBehaviorHandler connected to a sink that isn't an entity. Manual fixup on load will be necessary\n" );
  915. return true;
  916. }
  917. //-------------------------------------
  918. bool CAI_LeadBehavior::Disconnect( CAI_LeadBehaviorHandler *pSink )
  919. {
  920. Assert( pSink == m_pSink );
  921. m_pSink = NULL;
  922. m_hSinkImplementor = NULL;
  923. return true;
  924. }
  925. //-------------------------------------
  926. AI_BEGIN_CUSTOM_SCHEDULE_PROVIDER( CAI_LeadBehavior )
  927. DECLARE_CONDITION( COND_LEAD_FOLLOWER_LOST )
  928. DECLARE_CONDITION( COND_LEAD_FOLLOWER_LAGGING )
  929. DECLARE_CONDITION( COND_LEAD_FOLLOWER_NOT_LAGGING )
  930. DECLARE_CONDITION( COND_LEAD_FOLLOWER_VERY_CLOSE )
  931. DECLARE_CONDITION( COND_LEAD_SUCCESS )
  932. DECLARE_CONDITION( COND_LEAD_HAVE_FOLLOWER_LOS )
  933. DECLARE_CONDITION( COND_LEAD_FOLLOWER_MOVED_FROM_MARK )
  934. DECLARE_CONDITION( COND_LEAD_FOLLOWER_MOVING_TOWARDS_ME )
  935. //---------------------------------
  936. //
  937. // Lead
  938. //
  939. DECLARE_TASK( TASK_GET_PATH_TO_LEAD_GOAL )
  940. DECLARE_TASK( TASK_STOP_LEADING )
  941. DECLARE_TASK( TASK_LEAD_ARRIVE )
  942. DECLARE_TASK( TASK_LEAD_SUCCEED )
  943. DECLARE_TASK( TASK_LEAD_FACE_GOAL )
  944. DECLARE_TASK( TASK_LEAD_GET_PATH_TO_WAITPOINT )
  945. DECLARE_TASK( TASK_LEAD_WAVE_TO_PLAYER )
  946. DECLARE_TASK( TASK_LEAD_PLAYER_NEEDS_WEAPON )
  947. DECLARE_TASK( TASK_LEAD_MOVE_TO_RANGE )
  948. DECLARE_TASK( TASK_LEAD_SPEAK_START )
  949. DECLARE_TASK( TASK_LEAD_RETRIEVE_WAIT )
  950. DECLARE_TASK( TASK_LEAD_WALK_PATH )
  951. DEFINE_SCHEDULE
  952. (
  953. SCHED_LEAD_RETRIEVE,
  954. " Tasks"
  955. " TASK_GET_PATH_TO_PLAYER 0"
  956. " TASK_LEAD_MOVE_TO_RANGE 0"
  957. " TASK_STOP_MOVING 0"
  958. " TASK_WAIT_FOR_SPEAK_FINISH 1"
  959. " TASK_SET_SCHEDULE SCHEDULE:SCHED_LEAD_RETRIEVE_WAIT"
  960. " "
  961. " Interrupts"
  962. " COND_LEAD_FOLLOWER_VERY_CLOSE"
  963. " COND_LEAD_FOLLOWER_MOVING_TOWARDS_ME"
  964. " COND_LIGHT_DAMAGE"
  965. " COND_NEW_ENEMY"
  966. " COND_HEAR_DANGER"
  967. )
  968. //-------------------------------------
  969. DEFINE_SCHEDULE
  970. (
  971. SCHED_LEAD_SPEAK_THEN_RETRIEVE_PLAYER,
  972. " Tasks"
  973. " TASK_WAIT_FOR_SPEAK_FINISH 1"
  974. " TASK_SET_SCHEDULE SCHEDULE:SCHED_LEAD_RETRIEVE"
  975. " "
  976. " Interrupts"
  977. " COND_LEAD_FOLLOWER_VERY_CLOSE"
  978. " COND_LEAD_FOLLOWER_MOVING_TOWARDS_ME"
  979. " COND_LIGHT_DAMAGE"
  980. " COND_NEW_ENEMY"
  981. " COND_HEAR_DANGER"
  982. )
  983. //-------------------------------------
  984. DEFINE_SCHEDULE
  985. (
  986. SCHED_LEAD_RETRIEVE_WAIT,
  987. " Tasks"
  988. " TASK_LEAD_RETRIEVE_WAIT 0"
  989. " "
  990. " Interrupts"
  991. " COND_LEAD_FOLLOWER_LOST"
  992. " COND_LEAD_FOLLOWER_LAGGING"
  993. " COND_LEAD_FOLLOWER_VERY_CLOSE"
  994. " COND_LEAD_FOLLOWER_MOVING_TOWARDS_ME"
  995. " COND_LEAD_FOLLOWER_MOVED_FROM_MARK"
  996. " COND_LIGHT_DAMAGE"
  997. " COND_NEW_ENEMY"
  998. " COND_HEAR_DANGER"
  999. )
  1000. //---------------------------------
  1001. DEFINE_SCHEDULE
  1002. (
  1003. SCHED_LEAD_PLAYER,
  1004. " Tasks"
  1005. " TASK_WAIT_FOR_SPEAK_FINISH 1"
  1006. " TASK_GET_PATH_TO_LEAD_GOAL 0"
  1007. " TASK_LEAD_WALK_PATH 0"
  1008. " TASK_WAIT_FOR_MOVEMENT 0"
  1009. " TASK_STOP_MOVING 0"
  1010. ""
  1011. " Interrupts"
  1012. " COND_LEAD_FOLLOWER_LOST"
  1013. " COND_LEAD_FOLLOWER_LAGGING"
  1014. " COND_LIGHT_DAMAGE"
  1015. " COND_NEW_ENEMY"
  1016. " COND_HEAR_DANGER"
  1017. )
  1018. //---------------------------------
  1019. DEFINE_SCHEDULE
  1020. (
  1021. SCHED_LEAD_AWAIT_SUCCESS,
  1022. " Tasks"
  1023. " TASK_LEAD_FACE_GOAL 0"
  1024. " TASK_FACE_IDEAL 0"
  1025. " TASK_LEAD_ARRIVE 0"
  1026. " TASK_WAIT_INDEFINITE 0"
  1027. ""
  1028. " Interrupts"
  1029. " COND_LEAD_FOLLOWER_LOST"
  1030. " COND_LEAD_FOLLOWER_LAGGING"
  1031. " COND_LEAD_SUCCESS"
  1032. " COND_LIGHT_DAMAGE"
  1033. " COND_NEW_ENEMY"
  1034. " COND_HEAR_DANGER"
  1035. )
  1036. //---------------------------------
  1037. DEFINE_SCHEDULE
  1038. (
  1039. SCHED_LEAD_SUCCEED,
  1040. " Tasks"
  1041. " TASK_LEAD_SUCCEED 0"
  1042. " TASK_STOP_LEADING 0"
  1043. ""
  1044. )
  1045. //---------------------------------
  1046. // This is the schedule Odell uses to pause the tour momentarily
  1047. // if the player lags behind. If the player shows up in a
  1048. // couple of seconds, the tour will resume. Otherwise, Odell
  1049. // moves to retrieve.
  1050. //---------------------------------
  1051. DEFINE_SCHEDULE
  1052. (
  1053. SCHED_LEAD_PAUSE,
  1054. " Tasks"
  1055. " TASK_STOP_MOVING 1"
  1056. " TASK_FACE_TARGET 0"
  1057. " TASK_WAIT 5"
  1058. " TASK_WAIT_RANDOM 5"
  1059. " TASK_SET_SCHEDULE SCHEDULE:SCHED_LEAD_RETRIEVE"
  1060. ""
  1061. " Interrupts"
  1062. " COND_LEAD_FOLLOWER_VERY_CLOSE"
  1063. " COND_LEAD_FOLLOWER_MOVING_TOWARDS_ME"
  1064. " COND_LEAD_FOLLOWER_NOT_LAGGING"
  1065. " COND_LEAD_FOLLOWER_LOST"
  1066. " COND_LIGHT_DAMAGE"
  1067. " COND_NEW_ENEMY"
  1068. " COND_HEAR_DANGER"
  1069. )
  1070. DEFINE_SCHEDULE
  1071. (
  1072. SCHED_LEAD_PAUSE_COMBAT,
  1073. " Tasks"
  1074. " TASK_STOP_MOVING 1"
  1075. " TASK_FACE_TARGET 0"
  1076. " TASK_WAIT 1"
  1077. " TASK_SET_SCHEDULE SCHEDULE:SCHED_LEAD_RETRIEVE"
  1078. ""
  1079. " Interrupts"
  1080. " COND_LEAD_FOLLOWER_VERY_CLOSE"
  1081. " COND_LEAD_FOLLOWER_MOVING_TOWARDS_ME"
  1082. " COND_LEAD_FOLLOWER_NOT_LAGGING"
  1083. " COND_LEAD_FOLLOWER_LOST"
  1084. " COND_HEAR_DANGER"
  1085. )
  1086. //---------------------------------
  1087. DEFINE_SCHEDULE
  1088. (
  1089. SCHED_LEAD_WAITFORPLAYER,
  1090. " Tasks"
  1091. " TASK_LEAD_GET_PATH_TO_WAITPOINT 0"
  1092. " TASK_LEAD_WALK_PATH 0"
  1093. " TASK_WAIT_FOR_MOVEMENT 0"
  1094. " TASK_STOP_MOVING 0"
  1095. " TASK_WAIT 0.5"
  1096. " TASK_FACE_TARGET 0"
  1097. " TASK_LEAD_WAVE_TO_PLAYER 0"
  1098. " TASK_WAIT 5.0"
  1099. " "
  1100. " Interrupts"
  1101. " COND_LEAD_FOLLOWER_VERY_CLOSE"
  1102. " COND_LIGHT_DAMAGE"
  1103. " COND_NEW_ENEMY"
  1104. " COND_HEAR_DANGER"
  1105. )
  1106. //---------------------------------
  1107. DEFINE_SCHEDULE
  1108. (
  1109. SCHED_LEAD_WAITFORPLAYERIDLE,
  1110. " Tasks"
  1111. " TASK_STOP_MOVING 0"
  1112. " TASK_WAIT 0.5"
  1113. " TASK_FACE_TARGET 0"
  1114. " TASK_PLAY_SEQUENCE ACTIVITY:ACT_IDLE"
  1115. " TASK_WAIT 2"
  1116. " "
  1117. " Interrupts"
  1118. " COND_LEAD_FOLLOWER_VERY_CLOSE"
  1119. " COND_LIGHT_DAMAGE"
  1120. " COND_NEW_ENEMY"
  1121. " COND_HEAR_DANGER"
  1122. )
  1123. //---------------------------------
  1124. DEFINE_SCHEDULE
  1125. (
  1126. SCHED_LEAD_PLAYERNEEDSWEAPON,
  1127. " Tasks"
  1128. " TASK_FACE_PLAYER 0"
  1129. " TASK_LEAD_PLAYER_NEEDS_WEAPON 0"
  1130. " TASK_WAIT_FOR_SPEAK_FINISH 1"
  1131. " TASK_WAIT 8"
  1132. " "
  1133. " Interrupts"
  1134. )
  1135. //---------------------------------
  1136. DEFINE_SCHEDULE
  1137. (
  1138. SCHED_LEAD_SPEAK_START,
  1139. " Tasks"
  1140. " TASK_LEAD_SPEAK_START 0"
  1141. " TASK_WAIT_FOR_SPEAK_FINISH 1"
  1142. ""
  1143. " Interrupts"
  1144. )
  1145. //---------------------------------
  1146. DEFINE_SCHEDULE
  1147. (
  1148. SCHED_LEAD_SPEAK_THEN_LEAD_PLAYER,
  1149. " Tasks"
  1150. " TASK_STOP_MOVING 0"
  1151. " TASK_WAIT_FOR_SPEAK_FINISH 1"
  1152. " TASK_SET_SCHEDULE SCHEDULE:SCHED_LEAD_PLAYER"
  1153. ""
  1154. " Interrupts"
  1155. " COND_LEAD_FOLLOWER_LOST"
  1156. " COND_LEAD_FOLLOWER_LAGGING"
  1157. " COND_LIGHT_DAMAGE"
  1158. " COND_NEW_ENEMY"
  1159. " COND_HEAR_DANGER"
  1160. )
  1161. AI_END_CUSTOM_SCHEDULE_PROVIDER()
  1162. //-----------------------------------------------------------------------------
  1163. //
  1164. // Purpose: A level tool to control the lead behavior. Use is not required
  1165. // in order to use behavior.
  1166. //
  1167. class CAI_LeadGoal : public CAI_GoalEntity,
  1168. public CAI_LeadBehaviorHandler
  1169. {
  1170. DECLARE_CLASS( CAI_LeadGoal, CAI_GoalEntity );
  1171. public:
  1172. CAI_LeadGoal()
  1173. : m_fArrived( false )
  1174. {
  1175. // These fields got added after existing levels shipped, so we set
  1176. // the default values here in the constructor.
  1177. m_iRetrievePlayer = 1;
  1178. m_iRetrieveWaitForSpeak = 0;
  1179. m_iComingBackWaitForSpeak = 0;
  1180. m_bStopScenesWhenPlayerLost = false;
  1181. m_bDontSpeakStart = false;
  1182. m_bLeadDuringCombat = false;
  1183. m_bGagLeader = false;
  1184. }
  1185. CAI_LeadBehavior *GetLeadBehavior();
  1186. virtual const char *GetConceptModifiers( const char *pszConcept );
  1187. virtual void InputActivate( inputdata_t &inputdata );
  1188. virtual void InputDeactivate( inputdata_t &inputdata );
  1189. DECLARE_DATADESC();
  1190. private:
  1191. virtual void OnEvent( int event );
  1192. void InputSetSuccess( inputdata_t &inputdata );
  1193. void InputSetFailure( inputdata_t &inputdata );
  1194. bool m_fArrived; // @TODO (toml 08-16-02): move arrived tracking onto behavior
  1195. float m_flWaitDistance;
  1196. float m_flLeadDistance;
  1197. float m_flRetrieveDistance;
  1198. float m_flSuccessDistance;
  1199. bool m_bRun;
  1200. int m_iRetrievePlayer;
  1201. int m_iRetrieveWaitForSpeak;
  1202. int m_iComingBackWaitForSpeak;
  1203. bool m_bStopScenesWhenPlayerLost;
  1204. bool m_bDontSpeakStart;
  1205. bool m_bLeadDuringCombat;
  1206. bool m_bGagLeader;
  1207. string_t m_iszWaitPointName;
  1208. string_t m_iszStartConceptModifier;
  1209. string_t m_iszAttractPlayerConceptModifier;
  1210. string_t m_iszWaitOverConceptModifier;
  1211. string_t m_iszArrivalConceptModifier;
  1212. string_t m_iszPostArrivalConceptModifier;
  1213. string_t m_iszSuccessConceptModifier;
  1214. string_t m_iszFailureConceptModifier;
  1215. string_t m_iszRetrieveConceptModifier;
  1216. string_t m_iszComingBackConceptModifier;
  1217. // Output handlers
  1218. COutputEvent m_OnArrival;
  1219. COutputEvent m_OnArrivalDone;
  1220. COutputEvent m_OnSuccess;
  1221. COutputEvent m_OnFailure;
  1222. COutputEvent m_OnDone;
  1223. };
  1224. //-----------------------------------------------------------------------------
  1225. //
  1226. // CAI_LeadGoal implementation
  1227. //
  1228. LINK_ENTITY_TO_CLASS( ai_goal_lead, CAI_LeadGoal );
  1229. BEGIN_DATADESC( CAI_LeadGoal )
  1230. DEFINE_FIELD( m_fArrived, FIELD_BOOLEAN ),
  1231. DEFINE_KEYFIELD(m_flWaitDistance, FIELD_FLOAT, "WaitDistance"),
  1232. DEFINE_KEYFIELD(m_iszWaitPointName, FIELD_STRING, "WaitPointName"),
  1233. DEFINE_KEYFIELD(m_flLeadDistance, FIELD_FLOAT, "LeadDistance"),
  1234. DEFINE_KEYFIELD(m_flRetrieveDistance, FIELD_FLOAT, "RetrieveDistance"),
  1235. DEFINE_KEYFIELD(m_flSuccessDistance, FIELD_FLOAT, "SuccessDistance"),
  1236. DEFINE_KEYFIELD(m_bRun, FIELD_BOOLEAN, "Run"),
  1237. DEFINE_KEYFIELD(m_iRetrievePlayer, FIELD_INTEGER, "Retrieve"),
  1238. DEFINE_KEYFIELD(m_iRetrieveWaitForSpeak, FIELD_INTEGER, "RetrieveWaitForSpeak"),
  1239. DEFINE_KEYFIELD(m_iComingBackWaitForSpeak, FIELD_INTEGER, "ComingBackWaitForSpeak"),
  1240. DEFINE_KEYFIELD(m_bStopScenesWhenPlayerLost, FIELD_BOOLEAN, "StopScenes"),
  1241. DEFINE_KEYFIELD(m_bDontSpeakStart, FIELD_BOOLEAN, "DontSpeakStart"),
  1242. DEFINE_KEYFIELD(m_bLeadDuringCombat, FIELD_BOOLEAN, "LeadDuringCombat"),
  1243. DEFINE_KEYFIELD(m_bGagLeader, FIELD_BOOLEAN, "GagLeader"),
  1244. DEFINE_KEYFIELD(m_iszStartConceptModifier, FIELD_STRING, "StartConceptModifier"),
  1245. DEFINE_KEYFIELD(m_iszAttractPlayerConceptModifier, FIELD_STRING, "AttractPlayerConceptModifier"),
  1246. DEFINE_KEYFIELD(m_iszWaitOverConceptModifier, FIELD_STRING, "WaitOverConceptModifier"),
  1247. DEFINE_KEYFIELD(m_iszArrivalConceptModifier, FIELD_STRING, "ArrivalConceptModifier"),
  1248. DEFINE_KEYFIELD(m_iszPostArrivalConceptModifier, FIELD_STRING, "PostArrivalConceptModifier"),
  1249. DEFINE_KEYFIELD(m_iszSuccessConceptModifier, FIELD_STRING, "SuccessConceptModifier"),
  1250. DEFINE_KEYFIELD(m_iszFailureConceptModifier, FIELD_STRING, "FailureConceptModifier"),
  1251. DEFINE_KEYFIELD(m_iszRetrieveConceptModifier, FIELD_STRING, "RetrieveConceptModifier"),
  1252. DEFINE_KEYFIELD(m_iszComingBackConceptModifier, FIELD_STRING, "ComingBackConceptModifier"),
  1253. DEFINE_OUTPUT( m_OnSuccess, "OnSuccess" ),
  1254. DEFINE_OUTPUT( m_OnArrival, "OnArrival" ),
  1255. DEFINE_OUTPUT( m_OnArrivalDone, "OnArrivalDone" ),
  1256. DEFINE_OUTPUT( m_OnFailure, "OnFailure" ),
  1257. DEFINE_OUTPUT( m_OnDone, "OnDone" ),
  1258. // Inputs
  1259. DEFINE_INPUTFUNC( FIELD_VOID, "SetSuccess", InputSetSuccess ),
  1260. DEFINE_INPUTFUNC( FIELD_VOID, "SetFailure", InputSetFailure ),
  1261. END_DATADESC()
  1262. //-----------------------------------------------------------------------------
  1263. CAI_LeadBehavior *CAI_LeadGoal::GetLeadBehavior()
  1264. {
  1265. CAI_BaseNPC *pActor = GetActor();
  1266. if ( !pActor )
  1267. return NULL;
  1268. CAI_LeadBehavior *pBehavior;
  1269. if ( !pActor->GetBehavior( &pBehavior ) )
  1270. {
  1271. return NULL;
  1272. }
  1273. return pBehavior;
  1274. }
  1275. //-----------------------------------------------------------------------------
  1276. void CAI_LeadGoal::InputSetSuccess( inputdata_t &inputdata )
  1277. {
  1278. CAI_LeadBehavior *pBehavior = GetLeadBehavior();
  1279. if ( !pBehavior )
  1280. return;
  1281. // @TODO (toml 02-14-03): Hackly!
  1282. pBehavior->SetCondition( CAI_LeadBehavior::COND_LEAD_SUCCESS);
  1283. }
  1284. //-----------------------------------------------------------------------------
  1285. void CAI_LeadGoal::InputSetFailure( inputdata_t &inputdata )
  1286. {
  1287. DevMsg( "SetFailure unimplemented\n" );
  1288. }
  1289. //-----------------------------------------------------------------------------
  1290. void CAI_LeadGoal::InputActivate( inputdata_t &inputdata )
  1291. {
  1292. BaseClass::InputActivate( inputdata );
  1293. CAI_LeadBehavior *pBehavior = GetLeadBehavior();
  1294. if ( !pBehavior )
  1295. {
  1296. DevMsg( "Lead goal entity activated for an NPC that doesn't have the lead behavior\n" );
  1297. return;
  1298. }
  1299. #ifdef HL2_EPISODIC
  1300. if ( (m_flLeadDistance*4) < m_flRetrieveDistance )
  1301. {
  1302. Warning("ai_goal_lead '%s': lead distance (%.2f) * 4 is < retrieve distance (%.2f). This will make the NPC act stupid. Either reduce the retrieve distance, or increase the lead distance.\n", GetDebugName(), m_flLeadDistance, m_flRetrieveDistance );
  1303. }
  1304. #endif
  1305. if ( m_flRetrieveDistance < (m_flLeadDistance + LEAD_MIN_RETRIEVEDIST_OFFSET) )
  1306. {
  1307. #ifdef HL2_EPISODIC
  1308. Warning("ai_goal_lead '%s': retrieve distance (%.2f) < lead distance (%.2f) + %d. Retrieve distance should be at least %d greater than the lead distance, or NPC will ping-pong while retrieving.\n", GetDebugName(), m_flRetrieveDistance, m_flLeadDistance, LEAD_MIN_RETRIEVEDIST_OFFSET, LEAD_MIN_RETRIEVEDIST_OFFSET );
  1309. #endif
  1310. m_flRetrieveDistance = m_flLeadDistance + LEAD_MIN_RETRIEVEDIST_OFFSET;
  1311. }
  1312. AI_LeadArgs_t leadArgs = {
  1313. GetGoalEntityName(),
  1314. STRING(m_iszWaitPointName),
  1315. m_spawnflags,
  1316. m_flWaitDistance,
  1317. m_flLeadDistance,
  1318. m_flRetrieveDistance,
  1319. m_flSuccessDistance,
  1320. m_bRun,
  1321. m_iRetrievePlayer,
  1322. m_iRetrieveWaitForSpeak,
  1323. m_iComingBackWaitForSpeak,
  1324. m_bStopScenesWhenPlayerLost,
  1325. m_bDontSpeakStart,
  1326. m_bLeadDuringCombat,
  1327. m_bGagLeader,
  1328. };
  1329. pBehavior->LeadPlayer( leadArgs, this );
  1330. }
  1331. //-----------------------------------------------------------------------------
  1332. void CAI_LeadGoal::InputDeactivate( inputdata_t &inputdata )
  1333. {
  1334. BaseClass::InputDeactivate( inputdata );
  1335. CAI_LeadBehavior *pBehavior = GetLeadBehavior();
  1336. if ( !pBehavior )
  1337. return;
  1338. pBehavior->StopLeading();
  1339. }
  1340. //-----------------------------------------------------------------------------
  1341. void CAI_LeadGoal::OnEvent( int event )
  1342. {
  1343. COutputEvent *pOutputEvent = NULL;
  1344. switch ( event )
  1345. {
  1346. case LBE_ARRIVAL: pOutputEvent = &m_OnArrival; break;
  1347. case LBE_ARRIVAL_DONE: pOutputEvent = &m_OnArrivalDone; break;
  1348. case LBE_SUCCESS: pOutputEvent = &m_OnSuccess; break;
  1349. case LBE_FAILURE: pOutputEvent = &m_OnFailure; break;
  1350. case LBE_DONE: pOutputEvent = &m_OnDone; break;
  1351. }
  1352. // @TODO (toml 08-16-02): move arrived tracking onto behavior
  1353. if ( event == LBE_ARRIVAL )
  1354. m_fArrived = true;
  1355. if ( pOutputEvent )
  1356. pOutputEvent->FireOutput( this, this );
  1357. }
  1358. //-----------------------------------------------------------------------------
  1359. const char *CAI_LeadGoal::GetConceptModifiers( const char *pszConcept )
  1360. {
  1361. if ( m_iszStartConceptModifier != NULL_STRING && *STRING(m_iszStartConceptModifier) && strcmp( pszConcept, TLK_LEAD_START) == 0 )
  1362. return STRING( m_iszStartConceptModifier );
  1363. if ( m_iszAttractPlayerConceptModifier != NULL_STRING && *STRING(m_iszAttractPlayerConceptModifier) && strcmp( pszConcept, TLK_LEAD_ATTRACTPLAYER) == 0 )
  1364. return STRING( m_iszAttractPlayerConceptModifier );
  1365. if ( m_iszWaitOverConceptModifier != NULL_STRING && *STRING(m_iszWaitOverConceptModifier) && strcmp( pszConcept, TLK_LEAD_WAITOVER) == 0 )
  1366. return STRING( m_iszWaitOverConceptModifier );
  1367. if ( m_iszArrivalConceptModifier != NULL_STRING && *STRING(m_iszArrivalConceptModifier) && strcmp( pszConcept, TLK_LEAD_ARRIVAL) == 0 )
  1368. return STRING( m_iszArrivalConceptModifier );
  1369. if ( m_iszSuccessConceptModifier != NULL_STRING && *STRING(m_iszSuccessConceptModifier) && strcmp( pszConcept, TLK_LEAD_SUCCESS) == 0 )
  1370. return STRING( m_iszSuccessConceptModifier );
  1371. if (m_iszFailureConceptModifier != NULL_STRING && *STRING(m_iszFailureConceptModifier) && strcmp( pszConcept, TLK_LEAD_FAILURE) == 0 )
  1372. return STRING( m_iszFailureConceptModifier );
  1373. if (m_iszRetrieveConceptModifier != NULL_STRING && *STRING(m_iszRetrieveConceptModifier) && strcmp( pszConcept, TLK_LEAD_RETRIEVE) == 0 )
  1374. return STRING( m_iszRetrieveConceptModifier );
  1375. if (m_iszComingBackConceptModifier != NULL_STRING && *STRING(m_iszComingBackConceptModifier) && strcmp( pszConcept, TLK_LEAD_COMINGBACK) == 0 )
  1376. return STRING( m_iszComingBackConceptModifier );
  1377. if ( m_fArrived && m_iszPostArrivalConceptModifier != NULL_STRING && *STRING(m_iszPostArrivalConceptModifier) )
  1378. return STRING( m_iszPostArrivalConceptModifier );
  1379. return NULL;
  1380. }
  1381. //-----------------------------------------------------------------------------
  1382. //-----------------------------------------------------------------------------
  1383. //
  1384. // Purpose: A custom lead goal that waits until the player has a weapon.
  1385. //
  1386. class CAI_LeadGoal_Weapon : public CAI_LeadGoal
  1387. {
  1388. DECLARE_CLASS( CAI_LeadGoal_Weapon, CAI_LeadGoal );
  1389. public:
  1390. virtual const char *GetConceptModifiers( const char *pszConcept );
  1391. virtual void InputActivate( inputdata_t &inputdata );
  1392. private:
  1393. string_t m_iszWeaponName;
  1394. string_t m_iszMissingWeaponConceptModifier;
  1395. DECLARE_DATADESC();
  1396. };
  1397. //-----------------------------------------------------------------------------
  1398. //
  1399. // CAI_LeadGoal_Weapon implementation
  1400. //
  1401. LINK_ENTITY_TO_CLASS( ai_goal_lead_weapon, CAI_LeadGoal_Weapon );
  1402. BEGIN_DATADESC( CAI_LeadGoal_Weapon )
  1403. DEFINE_KEYFIELD( m_iszWeaponName, FIELD_STRING, "WeaponName"),
  1404. DEFINE_KEYFIELD( m_iszMissingWeaponConceptModifier, FIELD_STRING, "MissingWeaponConceptModifier"),
  1405. END_DATADESC()
  1406. //-----------------------------------------------------------------------------
  1407. const char *CAI_LeadGoal_Weapon::GetConceptModifiers( const char *pszConcept )
  1408. {
  1409. if ( m_iszMissingWeaponConceptModifier != NULL_STRING && *STRING(m_iszMissingWeaponConceptModifier) && strcmp( pszConcept, TLK_LEAD_MISSINGWEAPON) == 0 )
  1410. return STRING( m_iszMissingWeaponConceptModifier );
  1411. return BaseClass::GetConceptModifiers( pszConcept );
  1412. }
  1413. //-----------------------------------------------------------------------------
  1414. void CAI_LeadGoal_Weapon::InputActivate( inputdata_t &inputdata )
  1415. {
  1416. BaseClass::InputActivate( inputdata );
  1417. CAI_LeadBehavior *pBehavior = GetLeadBehavior();
  1418. if ( pBehavior )
  1419. {
  1420. pBehavior->SetWaitForWeapon( m_iszWeaponName );
  1421. }
  1422. }