Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unsigned int CDiv1DotAGame :: BalanceSlots2Recursive( list<CBalanceSlot>& slots_to_balance )
- {
- static unsigned int call = 0;
- ++call;
- unsigned int current_call = call;
- //cout << "call #" << call << endl;
- unsigned int team1_players_count = 0;
- unsigned int team2_players_count = 0;
- int team1_rating = 0;
- int team2_rating = 0;
- int rating_diff = 0;
- int next_rating_diff = 999999;
- // teams static variable must be resetted in the last function call execution
- static unsigned int teams = 0;
- unsigned int current_teams = teams++;
- int binary_position = 0;
- for( list<CBalanceSlot> :: iterator i = slots_to_balance.begin( ); i != slots_to_balance.end( ); ++i )
- {
- if( ( current_teams & (int)pow(2.0, binary_position) ) == 0 )
- {
- ++team1_players_count;
- team1_rating += (*i).m_Rating;
- }
- else
- {
- ++team2_players_count;
- team2_rating += (*i).m_Rating;
- }
- //cout << "teams:" << teams << " pow(2, binary_position):" << pow(2.0, binary_position) << endl;
- ++binary_position;
- }
- if( team1_players_count == 0 || team2_players_count == 0 || team1_players_count > 5 || team2_players_count > 5 )
- rating_diff = 999999;
- else
- rating_diff = abs( team1_rating - team2_rating );
- //cout << "team2_players_count:" << team2_players_count << " slots_to_balance.size( ):" << slots_to_balance.size( ) << endl;
- if( team2_players_count < slots_to_balance.size( ) )
- {
- next_rating_diff = BalanceSlots2Recursive( slots_to_balance );
- }
- else
- {
- //cout << "this should be the last call, call #" << call << endl;
- teams = 0; // this is the last function call execution, reset static variable
- }
- if( rating_diff < next_rating_diff )
- {
- binary_position = 0;
- //cout << "call #" << current_call << endl;
- //cout << "rating_diff:" << rating_diff << " next_rating_diff:" << next_rating_diff << endl;
- for( list<CBalanceSlot> :: iterator i = slots_to_balance.begin( ); i != slots_to_balance.end( ); ++i )
- {
- if( ( current_teams & (int)pow(2.0, binary_position) ) == 0 )
- {
- if( (*i).m_Locked && (*i).m_Team == 1 )
- {
- // this team layout is invalid
- // this player is locked to the scourge team, he can't be swaped to sentinel
- rating_diff = 999999;
- break;
- }
- (*i).m_Team = 0;
- }
- else
- {
- if( (*i).m_Locked && (*i).m_Team == 0 )
- {
- // this team layout is invalid
- // this player is locked to the sentinel team, he can't be swaped to scourge
- rating_diff = 999999;
- break;
- }
- (*i).m_Team = 1;
- }
- ++binary_position;
- }
- }
- return rating_diff < next_rating_diff ? rating_diff : next_rating_diff;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement