Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. unsigned int CDiv1DotAGame :: BalanceSlots2Recursive( list<CBalanceSlot>& slots_to_balance )
  2. {
  3. static unsigned int call = 0;
  4. ++call;
  5. unsigned int current_call = call;
  6. //cout << "call #" << call << endl;
  7.  
  8. unsigned int team1_players_count = 0;
  9. unsigned int team2_players_count = 0;
  10. int team1_rating = 0;
  11. int team2_rating = 0;
  12. int rating_diff = 0;
  13. int next_rating_diff = 999999;
  14.  
  15. // teams static variable must be resetted in the last function call execution
  16. static unsigned int teams = 0;
  17. unsigned int current_teams = teams++;
  18.  
  19. int binary_position = 0;
  20.  
  21. for( list<CBalanceSlot> :: iterator i = slots_to_balance.begin( ); i != slots_to_balance.end( ); ++i )
  22. {
  23. if( ( current_teams & (int)pow(2.0, binary_position) ) == 0 )
  24. {
  25. ++team1_players_count;
  26. team1_rating += (*i).m_Rating;
  27. }
  28. else
  29. {
  30. ++team2_players_count;
  31. team2_rating += (*i).m_Rating;
  32. }
  33.  
  34. //cout << "teams:" << teams << " pow(2, binary_position):" << pow(2.0, binary_position) << endl;
  35.  
  36. ++binary_position;
  37. }
  38.  
  39. if( team1_players_count == 0 || team2_players_count == 0 || team1_players_count > 5 || team2_players_count > 5 )
  40. rating_diff = 999999;
  41. else
  42. rating_diff = abs( team1_rating - team2_rating );
  43.  
  44. //cout << "team2_players_count:" << team2_players_count << " slots_to_balance.size( ):" << slots_to_balance.size( ) << endl;
  45.  
  46. if( team2_players_count < slots_to_balance.size( ) )
  47. {
  48. next_rating_diff = BalanceSlots2Recursive( slots_to_balance );
  49. }
  50. else
  51. {
  52. //cout << "this should be the last call, call #" << call << endl;
  53. teams = 0; // this is the last function call execution, reset static variable
  54. }
  55.  
  56. if( rating_diff < next_rating_diff )
  57. {
  58. binary_position = 0;
  59.  
  60. //cout << "call #" << current_call << endl;
  61. //cout << "rating_diff:" << rating_diff << " next_rating_diff:" << next_rating_diff << endl;
  62.  
  63. for( list<CBalanceSlot> :: iterator i = slots_to_balance.begin( ); i != slots_to_balance.end( ); ++i )
  64. {
  65. if( ( current_teams & (int)pow(2.0, binary_position) ) == 0 )
  66. {
  67. if( (*i).m_Locked && (*i).m_Team == 1 )
  68. {
  69. // this team layout is invalid
  70. // this player is locked to the scourge team, he can't be swaped to sentinel
  71.  
  72. rating_diff = 999999;
  73. break;
  74. }
  75.  
  76. (*i).m_Team = 0;
  77. }
  78. else
  79. {
  80. if( (*i).m_Locked && (*i).m_Team == 0 )
  81. {
  82. // this team layout is invalid
  83. // this player is locked to the sentinel team, he can't be swaped to scourge
  84.  
  85. rating_diff = 999999;
  86. break;
  87. }
  88.  
  89. (*i).m_Team = 1;
  90. }
  91.  
  92. ++binary_position;
  93. }
  94. }
  95.  
  96. return rating_diff < next_rating_diff ? rating_diff : next_rating_diff;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement