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.

782 lines
22 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Weapon selection handling
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "weapon_selection.h"
  9. #include "hud_macros.h"
  10. #include "history_resource.h"
  11. #include "menu.h"
  12. #include "in_buttons.h"
  13. #include <keyvalues.h>
  14. #include "filesystem.h"
  15. #include "iinput.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. #define HISTORY_DRAW_TIME "5"
  19. ConVar hud_drawhistory_time( "hud_drawhistory_time", HISTORY_DRAW_TIME, 0 );
  20. ConVar hud_fastswitch( "hud_fastswitch", "1", FCVAR_SS ); // [hpe:jason] Enabling weapon fastswitch by default
  21. //-----------------------------------------------------------------------------
  22. // Purpose: Weapon Selection commands
  23. //-----------------------------------------------------------------------------
  24. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, Slot1, "CHudWeaponSelection");
  25. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, Slot2, "CHudWeaponSelection");
  26. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, Slot3, "CHudWeaponSelection");
  27. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, Slot4, "CHudWeaponSelection");
  28. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, Slot5, "CHudWeaponSelection");
  29. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, Slot6, "CHudWeaponSelection");
  30. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, Slot7, "CHudWeaponSelection");
  31. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, Slot8, "CHudWeaponSelection");
  32. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, Slot9, "CHudWeaponSelection");
  33. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, Slot0, "CHudWeaponSelection");
  34. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, Slot10, "CHudWeaponSelection");
  35. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, Slot11, "CHudWeaponSelection");
  36. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, Close, "CHudWeaponSelection");
  37. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, NextWeapon, "CHudWeaponSelection");
  38. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, PrevWeapon, "CHudWeaponSelection");
  39. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, LastWeapon, "CHudWeaponSelection");
  40. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, NextGrenadeWeapon, "CHudWeaponSelection");
  41. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, NextItemWeapon, "CHudWeaponSelection");
  42. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, NextNonGrenadeWeapon, "CHudWeaponSelection");
  43. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, GamePadSlot1, "CHudWeaponSelection");
  44. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, GamePadSlot2, "CHudWeaponSelection");
  45. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, GamePadSlot3, "CHudWeaponSelection");
  46. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, GamePadSlot4, "CHudWeaponSelection");
  47. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, GamePadSlot5, "CHudWeaponSelection");
  48. DECLARE_HUD_COMMAND_NAME(CBaseHudWeaponSelection, GamePadSlot6, "CHudWeaponSelection");
  49. HOOK_COMMAND( slot1, Slot1 );
  50. HOOK_COMMAND( slot2, Slot2 );
  51. HOOK_COMMAND( slot3, Slot3 );
  52. HOOK_COMMAND( slot4, Slot4 );
  53. HOOK_COMMAND( slot5, Slot5 );
  54. HOOK_COMMAND( slot6, Slot6 );
  55. HOOK_COMMAND( slot7, Slot7 );
  56. HOOK_COMMAND( slot8, Slot8 );
  57. HOOK_COMMAND( slot9, Slot9 );
  58. HOOK_COMMAND( slot0, Slot0 );
  59. HOOK_COMMAND( slot10, Slot10 );
  60. HOOK_COMMAND( slot11, Slot11 );
  61. static ConCommand cancelselect( "cancelselect", __CmdFunc_Close, "", FCVAR_SERVER_CAN_EXECUTE | FCVAR_CLIENTCMD_CAN_EXECUTE ); // REI: Was HOOK_COMMAND( cancelselect, Close ), but that doesn't have CLIENTCMD_CAN_EXECUTE on it
  62. HOOK_COMMAND( invnext, NextWeapon );
  63. HOOK_COMMAND( invprev, PrevWeapon );
  64. HOOK_COMMAND( lastinv, LastWeapon );
  65. HOOK_COMMAND( invnextgrenade, NextGrenadeWeapon);
  66. HOOK_COMMAND( invnextitem, NextItemWeapon);
  67. HOOK_COMMAND( invnextnongrenade, NextNonGrenadeWeapon);
  68. HOOK_COMMAND( gamepadslot1, GamePadSlot1 );
  69. HOOK_COMMAND( gamepadslot2, GamePadSlot2 );
  70. HOOK_COMMAND( gamepadslot3, GamePadSlot3 );
  71. HOOK_COMMAND( gamepadslot4, GamePadSlot4 );
  72. HOOK_COMMAND( gamepadslot5, GamePadSlot5 );
  73. HOOK_COMMAND( gamepadslot6, GamePadSlot6 );
  74. // instance info
  75. CBaseHudWeaponSelection *CBaseHudWeaponSelection::s_pInstance[MAX_SPLITSCREEN_PLAYERS];
  76. CBaseHudWeaponSelection *CBaseHudWeaponSelection::GetInstance()
  77. {
  78. return s_pInstance[GET_ACTIVE_SPLITSCREEN_SLOT()];
  79. }
  80. CBaseHudWeaponSelection *GetHudWeaponSelection()
  81. {
  82. return CBaseHudWeaponSelection::GetInstance();
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose: Constructor
  86. //-----------------------------------------------------------------------------
  87. CBaseHudWeaponSelection::CBaseHudWeaponSelection( const char *pElementName ) : CHudElement( pElementName )
  88. {
  89. s_pInstance[GET_ACTIVE_SPLITSCREEN_SLOT()] = this;
  90. #ifdef PORTAL2
  91. SetHiddenBits( HIDEHUD_WEAPONSELECTION | HIDEHUD_PLAYERDEAD );
  92. #else
  93. SetHiddenBits( HIDEHUD_WEAPONSELECTION | HIDEHUD_NEEDSUIT | HIDEHUD_PLAYERDEAD | HIDEHUD_INVEHICLE );
  94. #endif // PORTAL2
  95. }
  96. //-----------------------------------------------------------------------------
  97. // Purpose:
  98. //-----------------------------------------------------------------------------
  99. void CBaseHudWeaponSelection::Init(void)
  100. {
  101. Reset();
  102. // Initialise the weapons resource
  103. gWR.Init();
  104. m_flSelectionTime = gpGlobals->curtime;
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Purpose:
  108. //-----------------------------------------------------------------------------
  109. void CBaseHudWeaponSelection::Reset(void)
  110. {
  111. gWR.Reset();
  112. // Start hidden
  113. m_bSelectionVisible = false;
  114. m_flSelectionTime = gpGlobals->curtime;
  115. }
  116. //-----------------------------------------------------------------------------
  117. // Purpose:
  118. //-----------------------------------------------------------------------------
  119. void CBaseHudWeaponSelection::UpdateSelectionTime( void )
  120. {
  121. m_flSelectionTime = gpGlobals->curtime;
  122. }
  123. //-----------------------------------------------------------------------------
  124. // Purpose:
  125. //-----------------------------------------------------------------------------
  126. void CBaseHudWeaponSelection::VidInit(void)
  127. {
  128. // If we've already loaded weapons, let's get new sprites
  129. gWR.LoadAllWeaponSprites();
  130. #if !defined( CSTRIKE15 )
  131. // set spacing of pickup history
  132. CHudHistoryResource *pHudHR = GET_HUDELEMENT( CHudHistoryResource );
  133. if( pHudHR )
  134. {
  135. pHudHR->SetHistoryGap( 21 );
  136. }
  137. #endif // !CSTRIKE15
  138. Reset();
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose:
  142. //-----------------------------------------------------------------------------
  143. void CBaseHudWeaponSelection::OnThink( void )
  144. {
  145. // Don't allow weapon selection if we're frozen in place
  146. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  147. if ( pPlayer->GetFlags() & FL_FROZEN || pPlayer->IsPlayerDead() )
  148. {
  149. if ( IsInSelectionMode() )
  150. {
  151. CancelWeaponSelection();
  152. }
  153. }
  154. }
  155. //-----------------------------------------------------------------------------
  156. // Purpose: Think used for selection of weapon menu item.
  157. //-----------------------------------------------------------------------------
  158. void CBaseHudWeaponSelection::ProcessInput()
  159. {
  160. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  161. if ( !pPlayer )
  162. return;
  163. // Check to see if the player is in VGUI mode...
  164. if ( pPlayer->IsInVGuiInputMode() && !pPlayer->IsInViewModelVGuiInputMode() )
  165. {
  166. // If so, close weapon selection when they press fire
  167. if ( GetHud().m_iKeyBits & IN_ATTACK )
  168. {
  169. if ( HUDTYPE_PLUS != hud_fastswitch.GetInt() )
  170. {
  171. // Swallow the button
  172. GetHud().m_iKeyBits &= ~IN_ATTACK;
  173. input->ClearInputButton( IN_ATTACK );
  174. }
  175. engine->ClientCmd( "cancelselect\n" );
  176. }
  177. return;
  178. }
  179. // Has the player selected a weapon?
  180. if ( GetHud().m_iKeyBits & ( IN_ATTACK | IN_ATTACK2 | IN_ZOOM ) )
  181. {
  182. if ( IsWeaponSelectable() )
  183. {
  184. #ifndef TF_CLIENT_DLL
  185. if ( HUDTYPE_PLUS != hud_fastswitch.GetInt() )
  186. #endif
  187. {
  188. // Swallow the button
  189. GetHud().m_iKeyBits &= ~( IN_ATTACK | IN_ATTACK2 | IN_ZOOM );
  190. input->ClearInputButton( IN_ATTACK );
  191. input->ClearInputButton( IN_ATTACK2 );
  192. input->ClearInputButton( IN_ZOOM );
  193. }
  194. // select weapon
  195. SelectWeapon();
  196. }
  197. }
  198. }
  199. //-----------------------------------------------------------------------------
  200. // Purpose:
  201. //-----------------------------------------------------------------------------
  202. bool CBaseHudWeaponSelection::IsInSelectionMode()
  203. {
  204. return m_bSelectionVisible;
  205. }
  206. //-----------------------------------------------------------------------------
  207. // Purpose:
  208. //-----------------------------------------------------------------------------
  209. void CBaseHudWeaponSelection::OpenSelection( void )
  210. {
  211. m_bSelectionVisible = true;
  212. }
  213. //-----------------------------------------------------------------------------
  214. // Purpose:
  215. //-----------------------------------------------------------------------------
  216. void CBaseHudWeaponSelection::HideSelection( void )
  217. {
  218. m_bSelectionVisible = false;
  219. }
  220. //-----------------------------------------------------------------------------
  221. // Purpose: Returns whether a weapon can be selected in the HUD, based on hud type
  222. //-----------------------------------------------------------------------------
  223. bool CBaseHudWeaponSelection::CanBeSelectedInHUD( C_BaseCombatWeapon *pWeapon )
  224. {
  225. // Xbox: In plus type, weapons without ammo can still be selected in the HUD
  226. if( HUDTYPE_PLUS == hud_fastswitch.GetInt() )
  227. {
  228. return pWeapon->VisibleInWeaponSelection();
  229. }
  230. if ( !pWeapon->VisibleInWeaponSelection() )
  231. {
  232. return false;
  233. }
  234. // All other current hud types
  235. return pWeapon->CanBeSelected();
  236. }
  237. //-----------------------------------------------------------------------------
  238. // Purpose: handles keyboard input
  239. //-----------------------------------------------------------------------------
  240. int CBaseHudWeaponSelection::KeyInput( int down, ButtonCode_t keynum, const char *pszCurrentBinding )
  241. {
  242. if (IsInSelectionMode() && pszCurrentBinding && !stricmp(pszCurrentBinding, "cancelselect"))
  243. {
  244. HideSelection();
  245. // returning 0 indicates, we've handled it, no more action needs to be taken
  246. return 0;
  247. }
  248. if ( down >= 1 && keynum >= KEY_1 && keynum <= KEY_9 )
  249. {
  250. if ( HandleHudMenuInput( keynum - KEY_0 ) )
  251. return 0;
  252. }
  253. // let someone else handle it
  254. return 1;
  255. }
  256. //-----------------------------------------------------------------------------
  257. // Purpose: called when a weapon has been picked up
  258. //-----------------------------------------------------------------------------
  259. void CBaseHudWeaponSelection::OnWeaponPickup( C_BaseCombatWeapon *pWeapon )
  260. {
  261. RANDOM_CEG_TEST_SECRET_PERIOD( 2, 3 )
  262. #if !defined( CSTRIKE15 )
  263. // add to pickup history
  264. CHudHistoryResource *pHudHR = GET_HUDELEMENT( CHudHistoryResource );
  265. if ( pHudHR )
  266. {
  267. pHudHR->AddToHistory( pWeapon );
  268. }
  269. #endif // !CSTRIKE15
  270. }
  271. //------------------------------------------------------------------------
  272. // Command Handlers
  273. //------------------------------------------------------------------------
  274. void CBaseHudWeaponSelection::UserCmd_Slot1(void)
  275. {
  276. if( HUDTYPE_CAROUSEL == hud_fastswitch.GetInt() )
  277. {
  278. UserCmd_LastWeapon();
  279. }
  280. else
  281. {
  282. SelectSlot( 1 );
  283. }
  284. }
  285. void CBaseHudWeaponSelection::UserCmd_Slot2(void)
  286. {
  287. if( HUDTYPE_CAROUSEL == hud_fastswitch.GetInt() )
  288. {
  289. UserCmd_NextWeapon();
  290. }
  291. else
  292. {
  293. SelectSlot( 2 );
  294. }
  295. }
  296. void CBaseHudWeaponSelection::UserCmd_Slot3(void)
  297. {
  298. if( HUDTYPE_CAROUSEL == hud_fastswitch.GetInt() )
  299. {
  300. engine->ClientCmd( "phys_swap" );
  301. }
  302. else
  303. {
  304. SelectSlot( 3 );
  305. }
  306. }
  307. void CBaseHudWeaponSelection::UserCmd_Slot4(void)
  308. {
  309. if( HUDTYPE_CAROUSEL == hud_fastswitch.GetInt() )
  310. {
  311. UserCmd_PrevWeapon();
  312. }
  313. else
  314. {
  315. SelectSlot( 4 );
  316. }
  317. }
  318. void CBaseHudWeaponSelection::UserCmd_Slot5(void)
  319. {
  320. SelectSlot( 5 );
  321. }
  322. void CBaseHudWeaponSelection::UserCmd_Slot6(void)
  323. {
  324. SelectSlot( 6 );
  325. }
  326. void CBaseHudWeaponSelection::UserCmd_Slot7(void)
  327. {
  328. SelectSlot( 7 );
  329. }
  330. void CBaseHudWeaponSelection::UserCmd_Slot8(void)
  331. {
  332. SelectSlot( 8 );
  333. }
  334. void CBaseHudWeaponSelection::UserCmd_Slot9(void)
  335. {
  336. SelectSlot( 9 );
  337. }
  338. void CBaseHudWeaponSelection::UserCmd_Slot0(void)
  339. {
  340. SelectSlot( 0 );
  341. }
  342. void CBaseHudWeaponSelection::UserCmd_Slot10(void)
  343. {
  344. SelectSlot( 10 );
  345. }
  346. void CBaseHudWeaponSelection::UserCmd_Slot11(void)
  347. {
  348. SelectSlot( 11 );
  349. }
  350. //void CBaseHudWeaponSelection::UserCmd_GamePadSlot1( void )
  351. //{
  352. //
  353. //}
  354. //
  355. //void CBaseHudWeaponSelection::UserCmd_GamePadSlot2( void )
  356. //{
  357. //
  358. //}
  359. //
  360. //void CBaseHudWeaponSelection::UserCmd_GamePadSlot3( void )
  361. //{
  362. //
  363. //}
  364. //
  365. //void CBaseHudWeaponSelection::UserCmd_GamePadSlot4( void )
  366. //{
  367. //
  368. //}
  369. //
  370. //void CBaseHudWeaponSelection::UserCmd_GamePadSlot5( void )
  371. //{
  372. //
  373. //}
  374. //
  375. //void CBaseHudWeaponSelection::UserCmd_GamePadSlot6( void )
  376. //{
  377. //
  378. //}
  379. //-----------------------------------------------------------------------------
  380. // Purpose: returns true if the CHudMenu should take slot1, etc commands
  381. //-----------------------------------------------------------------------------
  382. bool CBaseHudWeaponSelection::IsHudMenuTakingInput()
  383. {
  384. CHudMenu *pHudMenu = GET_HUDELEMENT( CHudMenu );
  385. return ( pHudMenu && pHudMenu->IsMenuOpen() );
  386. }
  387. //-----------------------------------------------------------------------------
  388. // Purpose: returns true if the CHudMenu handles the slot command
  389. //-----------------------------------------------------------------------------
  390. bool CBaseHudWeaponSelection::HandleHudMenuInput( int iSlot )
  391. {
  392. CHudMenu *pHudMenu = GET_HUDELEMENT( CHudMenu );
  393. if ( !pHudMenu || !pHudMenu->IsMenuOpen() )
  394. return false;
  395. pHudMenu->SelectMenuItem( iSlot );
  396. return true;
  397. }
  398. //-----------------------------------------------------------------------------
  399. // Purpose: returns true if the weapon selection hud should be hidden because
  400. // the CHudMenu is open
  401. //-----------------------------------------------------------------------------
  402. bool CBaseHudWeaponSelection::IsHudMenuPreventingWeaponSelection()
  403. {
  404. // Don't allow weapon selection if we're frozen in place
  405. C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
  406. if ( pPlayer->GetFlags() & FL_FROZEN || pPlayer->IsPlayerDead() )
  407. return true;
  408. return IsHudMenuTakingInput();
  409. }
  410. //-----------------------------------------------------------------------------
  411. // Purpose: Menu Selection Code
  412. //-----------------------------------------------------------------------------
  413. void CBaseHudWeaponSelection::SelectSlot( int iSlot )
  414. {
  415. // A menu may be overriding weapon selection commands
  416. if ( HandleHudMenuInput( iSlot ) )
  417. {
  418. return;
  419. }
  420. // If we're not allowed to draw, ignore weapon selections
  421. if ( !BaseClass::ShouldDraw() )
  422. {
  423. return;
  424. }
  425. UpdateSelectionTime();
  426. SelectWeaponSlot( iSlot );
  427. }
  428. //-----------------------------------------------------------------------------
  429. // Purpose: Close the weapon selection
  430. //-----------------------------------------------------------------------------
  431. void CBaseHudWeaponSelection::UserCmd_Close(void)
  432. {
  433. CancelWeaponSelection();
  434. }
  435. //-----------------------------------------------------------------------------
  436. // Purpose: Selects the next item in the weapon menu
  437. //-----------------------------------------------------------------------------
  438. void CBaseHudWeaponSelection::UserCmd_NextWeapon(void)
  439. {
  440. // If we're not allowed to draw, ignore weapon selections
  441. if ( !BaseClass::ShouldDraw() )
  442. return;
  443. CycleToNextWeapon();
  444. #if !defined ( CSTRIKE15 )
  445. if( hud_fastswitch.GetInt() > 0 )
  446. #endif
  447. {
  448. SelectWeapon();
  449. }
  450. UpdateSelectionTime();
  451. }
  452. //-----------------------------------------------------------------------------
  453. // Purpose: Selects the next grenade or bom in the weapon menu
  454. //-----------------------------------------------------------------------------
  455. void CBaseHudWeaponSelection::UserCmd_NextGrenadeWeapon(void)
  456. {
  457. // If we're not allowed to draw, ignore weapon selections
  458. if ( !BaseClass::ShouldDraw() )
  459. return;
  460. CycleToNextGrenadeOrBomb();
  461. #if !defined ( CSTRIKE15 )
  462. if( hud_fastswitch.GetInt() > 0 )
  463. #endif
  464. {
  465. SelectWeapon();
  466. }
  467. UpdateSelectionTime();
  468. }
  469. //-----------------------------------------------------------------------------
  470. // Purpose: Selects the next grenade, bomb or melee weapon in the weapon menu
  471. //-----------------------------------------------------------------------------
  472. void CBaseHudWeaponSelection::UserCmd_NextItemWeapon(void)
  473. {
  474. // If we're not allowed to draw, ignore weapon selections
  475. if ( !BaseClass::ShouldDraw() )
  476. return;
  477. CycleToNextGrenadeBombOrMelee();
  478. #if !defined ( CSTRIKE15 )
  479. if( hud_fastswitch.GetInt() > 0 )
  480. #endif
  481. {
  482. SelectWeapon();
  483. }
  484. UpdateSelectionTime();
  485. }
  486. //-----------------------------------------------------------------------------
  487. // Purpose: Selects the next non grenade item in the weapon menu
  488. //-----------------------------------------------------------------------------
  489. void CBaseHudWeaponSelection::UserCmd_NextNonGrenadeWeapon(void)
  490. {
  491. // If we're not allowed to draw, ignore weapon selections
  492. if ( !BaseClass::ShouldDraw() )
  493. return;
  494. CycleToNextNonGrenadeOrBomb();
  495. #if !defined ( CSTRIKE15 )
  496. if( hud_fastswitch.GetInt() > 0 )
  497. #endif
  498. {
  499. SelectWeapon();
  500. }
  501. UpdateSelectionTime();
  502. }
  503. //-----------------------------------------------------------------------------
  504. // Purpose: Selects the previous item in the menu
  505. //-----------------------------------------------------------------------------
  506. void CBaseHudWeaponSelection::UserCmd_PrevWeapon(void)
  507. {
  508. // If we're not allowed to draw, ignore weapon selections
  509. if ( !BaseClass::ShouldDraw() )
  510. return;
  511. CycleToPrevWeapon();
  512. #if !defined ( CSTRIKE15 )
  513. if( hud_fastswitch.GetInt() > 0 )
  514. #endif
  515. {
  516. SelectWeapon();
  517. }
  518. UpdateSelectionTime();
  519. }
  520. //-----------------------------------------------------------------------------
  521. // Purpose: Switches the last weapon the player was using
  522. //-----------------------------------------------------------------------------
  523. void CBaseHudWeaponSelection::UserCmd_LastWeapon(void)
  524. {
  525. // If we're not allowed to draw, ignore weapon selections
  526. if ( !BaseClass::ShouldDraw() )
  527. return;
  528. /*
  529. if ( IsHudMenuPreventingWeaponSelection() )
  530. {
  531. return;
  532. }
  533. */
  534. SwitchToLastWeapon();
  535. }
  536. //-----------------------------------------------------------------------------
  537. // Purpose: Switches the last weapon the player was using
  538. //-----------------------------------------------------------------------------
  539. void CBaseHudWeaponSelection::SwitchToLastWeapon( void )
  540. {
  541. // Get the player's last weapon
  542. C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
  543. if ( !player )
  544. return;
  545. input->MakeWeaponSelection( player->GetLastWeapon() );
  546. }
  547. //-----------------------------------------------------------------------------
  548. // Purpose:
  549. //-----------------------------------------------------------------------------
  550. void CBaseHudWeaponSelection::SetWeaponSelected( void )
  551. {
  552. Assert( GetSelectedWeapon() );
  553. // Mark selection so that it's placed into next CUserCmd created
  554. input->MakeWeaponSelection( GetSelectedWeapon() );
  555. }
  556. //-----------------------------------------------------------------------------
  557. // Purpose: Player has chosen to draw the currently selected weapon
  558. //-----------------------------------------------------------------------------
  559. void CBaseHudWeaponSelection::SelectWeapon( void )
  560. {
  561. if ( !GetSelectedWeapon() )
  562. {
  563. engine->ClientCmd( "cancelselect\n" );
  564. return;
  565. }
  566. C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
  567. if ( !player )
  568. return;
  569. // Don't allow selections of weapons that can't be selected (out of ammo, etc)
  570. if ( !GetSelectedWeapon()->CanBeSelected() )
  571. {
  572. player->EmitSound( "Player.DenyWeaponSelection" );
  573. }
  574. else
  575. {
  576. SetWeaponSelected();
  577. m_hSelectedWeapon = NULL;
  578. engine->ClientCmd( "cancelselect\n" );
  579. if (player->GetTeamNumber() == TEAM_CT)
  580. {
  581. // Play the "weapon selected" sound
  582. player->EmitSound("Player.WeaponSelected_CT");
  583. }
  584. else
  585. {
  586. // Play the "weapon selected" sound
  587. player->EmitSound("Player.WeaponSelected_T");
  588. }
  589. }
  590. }
  591. //-----------------------------------------------------------------------------
  592. // Purpose: Abort selecting a weapon
  593. //-----------------------------------------------------------------------------
  594. void CBaseHudWeaponSelection::CancelWeaponSelection( void )
  595. {
  596. C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
  597. if ( !player )
  598. return;
  599. // Fastswitches happen in a single frame, so the Weapon Selection HUD Element isn't visible
  600. // yet, but it's going to be next frame. We need to ask it if it thinks it's going to draw,
  601. // instead of checking it's IsActive flag.
  602. if ( ShouldDraw() )
  603. {
  604. HideSelection();
  605. m_hSelectedWeapon = NULL;
  606. // Play the "close weapon selection" sound based on faction
  607. //player->EmitSound( "Player.WeaponSelectionClose" );
  608. if (player->GetTeamNumber() == TEAM_CT)
  609. {
  610. // Play the CT Suit sound
  611. player->EmitSound("Player.WeaponSelectionClose_CT");
  612. }
  613. else
  614. {
  615. // Play the T Suit sound
  616. player->EmitSound("Player.WeaponSelectionClose_T");
  617. }
  618. }
  619. else
  620. {
  621. engine->ClientCmd("escape");
  622. }
  623. }
  624. //-----------------------------------------------------------------------------
  625. // Purpose: Returns the first weapon for a given slot.
  626. //-----------------------------------------------------------------------------
  627. C_BaseCombatWeapon *CBaseHudWeaponSelection::GetFirstPos( int iSlot )
  628. {
  629. int iLowestPosition = MAX_WEAPON_POSITIONS;
  630. C_BaseCombatWeapon *pFirstWeapon = NULL;
  631. C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
  632. if ( !player )
  633. return NULL;
  634. for ( int i = 0; i < MAX_WEAPONS; i++ )
  635. {
  636. C_BaseCombatWeapon *pWeapon = player->GetWeapon( i );
  637. if ( !pWeapon )
  638. continue;
  639. if ( ( pWeapon->GetSlot() == iSlot ) && (pWeapon->VisibleInWeaponSelection()) )
  640. {
  641. // If this weapon is lower in the slot than the current lowest, it's our new winner
  642. if ( pWeapon->GetPosition() <= iLowestPosition )
  643. {
  644. iLowestPosition = pWeapon->GetPosition();
  645. pFirstWeapon = pWeapon;
  646. }
  647. }
  648. }
  649. return pFirstWeapon;
  650. }
  651. //-----------------------------------------------------------------------------
  652. // Purpose:
  653. //-----------------------------------------------------------------------------
  654. C_BaseCombatWeapon *CBaseHudWeaponSelection::GetNextActivePos( int iSlot, int iSlotPos )
  655. {
  656. if ( iSlotPos >= MAX_WEAPON_POSITIONS || iSlot >= MAX_WEAPON_SLOTS )
  657. return NULL;
  658. int iLowestPosition = MAX_WEAPON_POSITIONS;
  659. C_BaseCombatWeapon *pNextWeapon = NULL;
  660. C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
  661. if ( !player )
  662. return NULL;
  663. for ( int i = 0; i < MAX_WEAPONS; i++ )
  664. {
  665. C_BaseCombatWeapon *pWeapon = player->GetWeapon( i );
  666. if ( !pWeapon )
  667. continue;
  668. if ( CanBeSelectedInHUD( pWeapon ) && pWeapon->GetSlot() == iSlot )
  669. {
  670. // If this weapon is lower in the slot than the current lowest, and above our desired position, it's our new winner
  671. if ( pWeapon->GetPosition() <= iLowestPosition && pWeapon->GetPosition() >= iSlotPos )
  672. {
  673. iLowestPosition = pWeapon->GetPosition();
  674. pNextWeapon = pWeapon;
  675. }
  676. }
  677. }
  678. return pNextWeapon;
  679. }