The second loop is unnecessary. You can keep track of the winner player in
the first loop and then just print the message after. Also, pull out
iHighestScore from the loop and don't make it static. Making it static saves
the value across function calls which isn't what you want.

void CHL2MPRules::DecideRoundWinner2( void )
{
#ifndef CLIENT_DLL
    if ( g_fGameOver )
      return;

    CBasePlayer *pWinner = NULL;
    int iHighestScore = 0;

    // count players, leave out spectators
    for ( int i = 0; i < MAX_PLAYERS; i++ )
    {
        CBasePlayer *pCurrentPlayer = UTIL_PlayerByIndex( i );

        if ( !pCurrentPlayer || pCurrentPlayer->GetTeamNumber() ==
TEAM_SPECTATOR  )
            continue;

        // if this player's score is higher than the previous highest or no
winner is set yet then set current player as the winner
        if( !pWinner || pWinner->GetPlayerScore() > iHighestScore )
        {
            // change the highest score so it can be compared again next
time this loops
            iHighestScore = pCurrentPlayer->GetPlayerScore();
            pWinner = pCurrentPlayer;
        }
    }

    if(pWinner)
    {
        pWinner->SetRoundWinner( true );
        UTIL_ClientPrintAll( HUD_PRINTCENTERCONSOLE, "Player '%s' Wins",
pWinner->GetPlayerName() );
    }
#endif
}

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Yorg Kuijs
Sent: Sunday, March 01, 2009 6:26 AM
To: Discussion of Half-Life Programming
Subject: [hlcoders] can this work/is it efficient?

Hey list,

In my mod I want the winner of a round to be announced with a hud print 
center and print console.
This will work by score(mind you, score is seperated from the frags 
already).

I don't really have any experience when it comes to selecting the 
highest score out of multiple objects(players in this case)
With the help of a example I could remember from a C++ guide, I cooked 
up the following:

void CHL2MPRules::DecideRoundWinner2( void )
{
#ifndef CLIENT_DLL
    if ( g_fGameOver )
      return;

    int previouswinner = 0;

    // count players, leave out spectators
    for ( int i = 0; i < MAX_PLAYERS; i++ )
    {
        CBasePlayer *pPlayer = UTIL_PlayerByIndex( i );
        CBasePlayer *pPreviousPlayer = UTIL_PlayerByIndex( previouswinner );

        if ( !pPlayer || pPlayer->GetTeamNumber() == TEAM_SPECTATOR  )
            continue;

        // should only be initialized once in the loop
        static int iHighestScore = 0;

        // if this player's score is higher then the previous highest 
then set current player as the winner
        if( pPlayer->GetPlayerScore() > iHighestScore )
        {
            // change the highest score so it can be compared again next 
time this loops
            iHighestScore = pPlayer->GetPlayerScore();
            // mark this player as (potential) winner
            pPlayer->SetRoundWinner( true );

            // if i is not 0, then there is likely a previous player 
with a lower score
            if( i )
                pPreviousPlayer->SetRoundWinner( false );

            // mark this player so he can be recognized as the previous 
winner if needed
            previouswinner = i;
        }
    }

    // second player counter loop, cycles through the players, then 
displays a message when it's found the winner
    for ( int j = 0; j < MAX_PLAYERS; j++ )
    {
        CBasePlayer *pPlayer = UTIL_PlayerByIndex( j );

        if ( !pPlayer )
            continue;

        // did the loop find the winner? if so display the message and 
break the loop
        if ( pPlayer->IsRoundWinner() )
        {
            UTIL_ClientPrintAll( HUD_PRINTCENTERCONSOLE, "Player '%s1' 
Wins", pPlayer->GetPlayerName() );
            break;
        }
    }
#endif
}

-------

As far as I know it should work, but may not be very efficient.
Also I do realize that players with the same score will not be counted 
as winners, I first want to know if this way of finding the winner is 
correct and works, once I know that I can fiddle with it so players with 
the same score won't be left out. Which brings me to my next question: 
Does printcenter and/or printconsole, support newlines?

Thanks in advance

_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives,
please visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders



_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives, please 
visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders

Reply via email to