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.

192 lines
6.0 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: CS's custom C_VoteController
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "c_vote_controller.h"
  9. #include "shareddefs.h"
  10. #include "hud.h"
  11. #include "cdll_client_int.h"
  12. #include "igameevents.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. IMPLEMENT_CLIENTCLASS_DT( C_VoteController, DT_VoteController, CVoteController )
  16. RecvPropInt( RECVINFO( m_iActiveIssueIndex ), 0, C_VoteController::RecvProxy_VoteType ),
  17. RecvPropInt( RECVINFO( m_iOnlyTeamToVote ) ),
  18. RecvPropArray3( RECVINFO_ARRAY( m_nVoteOptionCount ), RecvPropInt( RECVINFO( m_nVoteOptionCount[0] ), 0, C_VoteController::RecvProxy_VoteOption ) ),
  19. RecvPropInt( RECVINFO( m_nPotentialVotes ) ),
  20. RecvPropBool( RECVINFO( m_bIsYesNoVote ) )
  21. END_RECV_TABLE()
  22. //-----------------------------------------------------------------------------
  23. // Purpose:
  24. //-----------------------------------------------------------------------------
  25. void C_VoteController::RecvProxy_VoteType( const CRecvProxyData *pData, void *pStruct, void *pOut )
  26. {
  27. C_VoteController *pMe = (C_VoteController *)pStruct;
  28. if( pMe->m_iActiveIssueIndex == pData->m_Value.m_Int )
  29. return;
  30. pMe->m_iActiveIssueIndex = pData->m_Value.m_Int;
  31. pMe->m_bTypeDirty = true;
  32. // Since the contents of a new vote are in three parts, we can't directly send an event to the Hud
  33. // because we don't really know if we have all three parts yet. So we'll mark dirty, and our think
  34. // can notice that and send the event.
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Purpose:
  38. //-----------------------------------------------------------------------------
  39. void C_VoteController::RecvProxy_VoteOption( const CRecvProxyData *pData, void *pStruct, void *pOut )
  40. {
  41. int index = pData->m_pRecvProp->GetOffset() / sizeof(int);
  42. size_t offset = offsetof( C_VoteController, m_nVoteOptionCount );
  43. C_VoteController *pMe = (C_VoteController *)((byte *)pStruct - offset );
  44. if( pMe->m_nVoteOptionCount[index] == pData->m_Value.m_Int )
  45. return;
  46. pMe->m_nVoteOptionCount[index] = pData->m_Value.m_Int;
  47. pMe->m_bVotesDirty = true;
  48. pMe->SetNextClientThink( gpGlobals->curtime );
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Purpose:
  52. //-----------------------------------------------------------------------------
  53. C_VoteController::C_VoteController()
  54. {
  55. ResetData();
  56. ListenForGameEvent( "vote_cast" );
  57. }
  58. //-----------------------------------------------------------------------------
  59. // Purpose:
  60. //-----------------------------------------------------------------------------
  61. C_VoteController::~C_VoteController()
  62. {
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Purpose:
  66. //-----------------------------------------------------------------------------
  67. void C_VoteController::ResetData()
  68. {
  69. m_iActiveIssueIndex = INVALID_ISSUE;
  70. m_iOnlyTeamToVote = TEAM_INVALID;
  71. for( int i = 0; i < MAX_VOTE_OPTIONS; i++ )
  72. {
  73. m_nVoteOptionCount[i] = 0;
  74. }
  75. m_nPotentialVotes = 0;
  76. m_bVotesDirty = false;
  77. m_bTypeDirty = false;
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Purpose:
  81. //-----------------------------------------------------------------------------
  82. void C_VoteController::Spawn( void )
  83. {
  84. ResetData();
  85. BaseClass::Spawn();
  86. SetNextClientThink( gpGlobals->curtime );
  87. }
  88. //-----------------------------------------------------------------------------
  89. // Purpose:
  90. //-----------------------------------------------------------------------------
  91. void C_VoteController::ClientThink()
  92. {
  93. BaseClass::ClientThink();
  94. C_BasePlayer *pLocalPlayer = C_BasePlayer::GetLocalPlayer();
  95. if ( pLocalPlayer && ( (m_iOnlyTeamToVote == TEAM_INVALID) || (m_iOnlyTeamToVote == pLocalPlayer->GetTeamNumber()) ) )
  96. {
  97. if ( m_bTypeDirty )
  98. {
  99. m_bTypeDirty = false;
  100. m_bVotesDirty = true;
  101. }
  102. if ( m_bVotesDirty )
  103. {
  104. if ( m_nPotentialVotes > 0 )
  105. {
  106. // Currently hard-coded to MAX_VOTE_COUNT options per issue
  107. DevMsg( "Votes: Option1 - %d, Option2 - %d, Option3 - %d, Option4 - %d, Option5 - %d\n",
  108. m_nVoteOptionCount[ 0 ], m_nVoteOptionCount[ 1 ], m_nVoteOptionCount[ 2 ], m_nVoteOptionCount[ 3 ], m_nVoteOptionCount[ 4 ] );
  109. IGameEvent *event = gameeventmanager->CreateEvent( "vote_changed" );
  110. if ( event )
  111. {
  112. for ( int index = 0; index < MAX_VOTE_OPTIONS; index++ )
  113. {
  114. char szOption[ 2 ];
  115. Q_snprintf( szOption, sizeof( szOption ), "%i", index + 1 );
  116. char szVoteOption[ 13 ] = "vote_option";
  117. Q_strncat( szVoteOption, szOption, sizeof( szVoteOption ), COPY_ALL_CHARACTERS );
  118. event->SetInt( szVoteOption, m_nVoteOptionCount[ index ] );
  119. }
  120. event->SetInt( "potentialVotes", m_nPotentialVotes );
  121. gameeventmanager->FireEventClientSide( event );
  122. }
  123. }
  124. m_bVotesDirty = false;
  125. }
  126. }
  127. SetNextClientThink( gpGlobals->curtime + 0.5f );
  128. }
  129. //-----------------------------------------------------------------------------
  130. // Purpose:
  131. //-----------------------------------------------------------------------------
  132. void C_VoteController::FireGameEvent( IGameEvent *event )
  133. {
  134. //CHudVote *pHudVote = GET_HUDELEMENT( CHudVote );
  135. //if ( pHudVote && pHudVote->IsVisible() )
  136. {
  137. const char *eventName = event->GetName();
  138. if ( !eventName )
  139. return;
  140. C_BasePlayer *pLocalPlayer = C_BasePlayer::GetLocalPlayer();
  141. if ( !pLocalPlayer )
  142. return;
  143. int team = event->GetInt( "team", TEAM_UNASSIGNED );
  144. if ( team > TEAM_UNASSIGNED && team != pLocalPlayer->GetTeamNumber() )
  145. return;
  146. if ( FStrEq( eventName, "vote_cast" ) )
  147. {
  148. if ( m_bIsYesNoVote )
  149. {
  150. int vote_option = event->GetInt( "vote_option", TEAM_UNASSIGNED );
  151. if( vote_option == VOTE_OPTION2 )
  152. {
  153. pLocalPlayer->EmitSound( "Vote.Cast.No" );
  154. }
  155. else if( vote_option == VOTE_OPTION1 )
  156. {
  157. pLocalPlayer->EmitSound( "Vote.Cast.Yes" );
  158. }
  159. }
  160. else
  161. {
  162. pLocalPlayer->EmitSound( "Vote.Cast.Yes" );
  163. }
  164. }
  165. }
  166. }