# HG changeset patch # User Adam Kaminski # Date 1622682597 14400 # Wed Jun 02 21:09:57 2021 -0400 # Node ID 35b6888d84f4785340507afdc2cb6cdf2350e347 # Parent 0af15cf6c0bb7df95dc2449638f0b62898d4d832 Optimized how the number of votes are returned; instead of always counting how many players voted yes or no in their respective functions, just keep track of how many players voted yes or no during the vote and return those numbers instead. diff -r 0af15cf6c0bb -r 35b6888d84f4 src/callvote.cpp --- a/src/callvote.cpp Thu Jun 10 13:09:52 2021 -0400 +++ b/src/callvote.cpp Wed Jun 02 21:09:57 2021 -0400 @@ -86,6 +86,8 @@ static NETADDRESS_s g_KickVoteVictimAddress; static std::list g_PreviousVotes; static ULONG g_ulPlayerVoteChoice[MAXPLAYERS]; // [AK] +static ULONG g_ulNumYesVotes = 0; // [AK] +static ULONG g_ulNumNoVotes = 0; // [AK] //***************************************************************************** // PROTOTYPES @@ -172,7 +174,7 @@ g_VoteCommand.Format( "addban %s 10min \"Vote kick", g_KickVoteVictimAddress.ToString() ); else g_VoteCommand.Format( "forcespec_idx %d \"Vote forcespec", static_cast(SERVER_FindClientByAddress ( g_KickVoteVictimAddress )) ); - g_VoteCommand.AppendFormat( ", %u to %u", CALLVOTE_CountPlayersWhoVotedYes( ), CALLVOTE_CountPlayersWhoVotedNo( ) ); + g_VoteCommand.AppendFormat( ", %u to %u", g_ulNumYesVotes, g_ulNumNoVotes ); if ( g_VoteReason.IsNotEmpty() ) g_VoteCommand.AppendFormat ( " (%s)", g_VoteReason.GetChars( ) ); g_VoteCommand += ".\""; @@ -283,6 +285,8 @@ g_ulVoteCaller = MAXPLAYERS; g_ulVoteCountdownTicks = 0; g_ulShowVoteScreenTicks = 0; + g_ulNumYesVotes = 0; + g_ulNumNoVotes = 0; for ( ulIdx = 0; ulIdx < (( MAXPLAYERS / 2 ) + 1 ); ulIdx++ ) { @@ -358,8 +362,9 @@ } } - // [AK] Update this player's choice to "yes". + // [AK] Update this player's choice to "yes" and increment the tally. g_ulPlayerVoteChoice[ulPlayer] = VOTE_YES; + g_ulNumYesVotes++; // Display the message in the console. if ( NETWORK_GetState( ) == NETSTATE_SERVER ) @@ -455,8 +460,9 @@ } } - // [AK] Update this player's choice to "no". + // [AK] Update this player's choice to "no" and increment the tally. g_ulPlayerVoteChoice[ulPlayer] = VOTE_NO; + g_ulNumNoVotes++; // Display the message in the console. if ( NETWORK_GetState( ) == NETSTATE_SERVER ) @@ -619,24 +625,17 @@ // void CALLVOTE_TallyVotes( void ) { - ULONG ulNumYes; - ULONG ulNumNo; - - // Count up all the Yes/No votes. - ulNumYes = CALLVOTE_CountPlayersWhoVotedYes(); - ulNumNo = CALLVOTE_CountPlayersWhoVotedNo(); - // If More than half of the total eligible voters have voted, we must have a majority! - if ( MAX( ulNumYes, ulNumNo ) > ( CALLVOTE_CountNumEligibleVoters( ) / 2 )) + if ( MAX( g_ulNumYesVotes, g_ulNumNoVotes ) > ( CALLVOTE_CountNumEligibleVoters( ) / 2 )) { - g_bVotePassed = ( ulNumYes > ulNumNo ); + g_bVotePassed = ( g_ulNumYesVotes > g_ulNumNoVotes ); callvote_EndVote(); } // This will serve as the final tally. if ( g_ulVoteCountdownTicks == 0 ) { - if (( ulNumYes > 0 ) && ( ulNumYes > ulNumNo )) + if (( g_ulNumYesVotes > 0 ) && ( g_ulNumYesVotes > g_ulNumNoVotes )) g_bVotePassed = true; else g_bVotePassed = false; @@ -694,36 +693,16 @@ //***************************************************************************** // -ULONG CALLVOTE_CountPlayersWhoVotedYes( void ) +ULONG CALLVOTE_GetYesVoteCount( void ) { - ULONG ulIdx; - ULONG ulNumYes; - - ulNumYes = 0; - for ( ulIdx = 0; ulIdx < ( MAXPLAYERS / 2 ) + 1; ulIdx++ ) - { - if ( g_ulPlayersWhoVotedYes[ulIdx] != MAXPLAYERS && SERVER_IsValidClient( g_ulPlayersWhoVotedYes[ulIdx] )) - ulNumYes++; - } - - return ( ulNumYes ); + return ( g_ulNumYesVotes ); } //***************************************************************************** // -ULONG CALLVOTE_CountPlayersWhoVotedNo( void ) +ULONG CALLVOTE_GetNoVoteCount( void ) { - ULONG ulIdx; - ULONG ulNumNo; - - ulNumNo = 0; - for ( ulIdx = 0; ulIdx < ( MAXPLAYERS / 2 ) + 1; ulIdx++ ) - { - if ( g_ulPlayersWhoVotedNo[ulIdx] != MAXPLAYERS && SERVER_IsValidClient( g_ulPlayersWhoVotedNo[ulIdx] )) - ulNumNo++; - } - - return ( ulNumNo ); + return ( g_ulNumNoVotes ); } //***************************************************************************** diff -r 0af15cf6c0bb -r 35b6888d84f4 src/callvote.h --- a/src/callvote.h Thu Jun 10 13:09:52 2021 -0400 +++ b/src/callvote.h Wed Jun 02 21:09:57 2021 -0400 @@ -133,8 +133,8 @@ const char *CALLVOTE_GetReason( void ); void CALLVOTE_DisconnectedVoter( ULONG ulPlayer ); void CALLVOTE_TallyVotes( void ); -ULONG CALLVOTE_CountPlayersWhoVotedYes( void ); -ULONG CALLVOTE_CountPlayersWhoVotedNo( void ); +ULONG CALLVOTE_GetYesVoteCount( void ); +ULONG CALLVOTE_GetNoVoteCount( void ); ULONG CALLVOTE_GetVoteCaller( void ); VOTESTATE_e CALLVOTE_GetVoteState( void ); ULONG CALLVOTE_GetCountdownTicks( void ); diff -r 0af15cf6c0bb -r 35b6888d84f4 src/scoreboard.cpp --- a/src/scoreboard.cpp Thu Jun 10 13:09:52 2021 -0400 +++ b/src/scoreboard.cpp Wed Jun 02 21:09:57 2021 -0400 @@ -666,8 +666,6 @@ // void SCOREBOARD_RenderInVoteClassic( void ) { - ULONG ulNumYes = CALLVOTE_CountPlayersWhoVotedYes( ); - ULONG ulNumNo = CALLVOTE_CountPlayersWhoVotedNo( ); ULONG *pulPlayersWhoVotedYes = CALLVOTE_GetPlayersWhoVotedYes( ); ULONG *pulPlayersWhoVotedNo = CALLVOTE_GetPlayersWhoVotedNo( ); ULONG ulMaxYesOrNoVoters = ( MAXPLAYERS / 2 ) + 1; @@ -701,10 +699,10 @@ // Display how many have voted for "Yes" and "No". ulYPos += 16; - text.Format( "Yes: %d", static_cast( ulNumYes )); + text.Format( "Yes: %d", static_cast( CALLVOTE_GetYesVoteCount( ))); HUD_DrawTextClean( SmallFont, CR_UNTRANSLATED, 32, ulYPos, text ); - text.Format( "No: %d", static_cast( ulNumNo )); + text.Format( "No: %d", static_cast( CALLVOTE_GetNoVoteCount( ))); HUD_DrawTextClean( SmallFont, CR_UNTRANSLATED, 320 - 32 - SmallFont->StringWidth( text ), ulYPos, text ); ulYPos += 8; @@ -741,8 +739,6 @@ // void SCOREBOARD_RenderInVote( void ) { - ULONG ulNumYes = CALLVOTE_CountPlayersWhoVotedYes( ); - ULONG ulNumNo = CALLVOTE_CountPlayersWhoVotedNo( ); ULONG ulVoteChoice = CALLVOTE_GetPlayerVoteChoice( consoleplayer ); FString text; @@ -768,8 +764,8 @@ ulYPos += 8; // Render the number of votes. - text.Format( "%sYes: %d", ulVoteChoice == VOTE_YES ? TEXTCOLOR_YELLOW : "", static_cast( ulNumYes )); - text.AppendFormat( TEXTCOLOR_NORMAL ", %sNo: %d", ulVoteChoice == VOTE_NO ? TEXTCOLOR_YELLOW : "", static_cast( ulNumNo )); + text.Format( "%sYes: %d", ulVoteChoice == VOTE_YES ? TEXTCOLOR_YELLOW : "", static_cast( CALLVOTE_GetYesVoteCount( ))); + text.AppendFormat( TEXTCOLOR_NORMAL ", %sNo: %d", ulVoteChoice == VOTE_NO ? TEXTCOLOR_YELLOW : "", static_cast( CALLVOTE_GetNoVoteCount( ))); HUD_DrawTextCentered( SmallFont, CR_DARKBROWN, ulYPos, text, g_bScale ); // Render the explanation of keys.