Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. bool balanceteams(int ftr)  // pro vs noobs never more
  2. {
  3.     if(mastermode != MM_OPEN || totalclients < 3 ) return true;
  4.     int tsize[2] = {0, 0}, tscore[2] = {0, 0};
  5.     int totalscore = 0, nplayers = 0;
  6.     int flagmult = (m_ctf ? 50 : (m_htf ? 25 : 12));
  7.  
  8.     loopv(clients) if(clients[i]->type!=ST_EMPTY)
  9.     {
  10.         client *c = clients[i];
  11.         if(c->isauthed && team_isactive(c->team))
  12.         {
  13.             int time = servmillis - c->connectmillis + 5000;
  14.             if ( time > gamemillis ) time = gamemillis + 5000;
  15.             tsize[c->team]++;
  16.             // effective score per minute, thanks to wtfthisgame for the nice idea
  17.             // in a normal game, normal players will do 500 points in 10 minutes
  18.             c->eff_score = c->state.points * 60 * 1000 / time + c->state.points / 6 + c->state.flagscore * flagmult;
  19.             tscore[c->team] += c->eff_score;
  20.             nplayers++;
  21.             totalscore += c->state.points;
  22.         }
  23.     }
  24.  
  25.     int h = 0, l = 1;
  26.     if ( tscore[1] > tscore[0] ) { h = 1; l = 0; }
  27.     if ( 2 * tscore[h] < 3 * tscore[l] || totalscore < nplayers * 100 ) return true;
  28.     if ( tscore[h] > 3 * tscore[l] && tscore[h] > 150 * nplayers )
  29.     {
  30. //        sendf(-1, 1, "ri2", SV_SERVERMODE, sendservermode(false) | AT_SHUFFLE);
  31.         shuffleteams();
  32.         return true;
  33.     }
  34.  
  35.     float diffscore = tscore[h] - tscore[l];
  36.  
  37.     int besth = 0, hid = -1;
  38.     int bestdiff = 0, bestpair[2] = {-1, -1};
  39.     if ( tsize[h] - tsize[l] > 0 ) // the h team has more players, so we will force only one player
  40.     {
  41.         loopv(clients) if( clients[i]->type!=ST_EMPTY )
  42.         {
  43.             client *c = clients[i]; // loop for h
  44.             // client from the h team, not forced in this game, and without the flag
  45.             if( c->isauthed && c->team == h && !c->state.forced && clienthasflag(i) < 0 )
  46.             {
  47.                 // do not exchange in the way that weaker team becomes the stronger or the change is less than 20% effective
  48.                 if ( 2 * c->eff_score <= diffscore && 10 * c->eff_score >= diffscore && c->eff_score > besth )
  49.                 {
  50.                     besth = c->eff_score;
  51.                     hid = i;
  52.                 }
  53.             }
  54.         }
  55.         if ( hid >= 0 )
  56.         {
  57.             updateclientteam(hid, l, ftr);
  58.             clients[hid]->at3_lastforce = gamemillis;
  59.             clients[hid]->state.forced = true;
  60.             return true;
  61.         }
  62.     } else { // the h score team has less or the same player number, so, lets exchange
  63.         loopv(clients) if(clients[i]->type!=ST_EMPTY)
  64.         {
  65.             client *c = clients[i]; // loop for h
  66.             if( c->isauthed && c->team == h && !c->state.forced && clienthasflag(i) < 0 )
  67.             {
  68.                 loopvj(clients) if(clients[j]->type!=ST_EMPTY && j != i )
  69.                 {
  70.                     client *cj = clients[j]; // loop for l
  71.                     if( cj->isauthed && cj->team == l && !cj->state.forced && clienthasflag(j) < 0 )
  72.                     {
  73.                         int pairdiff = 2 * (c->eff_score - cj->eff_score);
  74.                         if ( pairdiff <= diffscore && 5 * pairdiff >= diffscore && pairdiff > bestdiff )
  75.                         {
  76.                             bestdiff = pairdiff;
  77.                             bestpair[h] = i;
  78.                             bestpair[l] = j;
  79.                         }
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.         if ( bestpair[h] >= 0 && bestpair[l] >= 0 )
  85.         {
  86.             updateclientteam(bestpair[h], l, ftr);
  87.             updateclientteam(bestpair[l], h, ftr);
  88.             clients[bestpair[h]]->at3_lastforce = clients[bestpair[l]]->at3_lastforce = gamemillis;
  89.             clients[bestpair[h]]->state.forced = clients[bestpair[l]]->state.forced = true;
  90.             return true;
  91.         }
  92.     }
  93.     return false;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement