Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
5.8 KiB

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