]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/teamplay.qc
Merge remote-tracking branch 'origin/master' into samual/mutator_ctf
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / teamplay.qc
index 369956c034d2627680da0c1f366b33b4c06c5fbf..917aa11bcc3c586a97cea50d3d9bc3c02cf1fb78 100644 (file)
@@ -8,21 +8,6 @@ float cb1, cb2, cb3, cb4;
 
 float audit_teams_time;
 
-float IsTeamBalanceForced()
-{
-       if(intermission_running)
-               return 0; // no rebalancing whatsoever please
-       if(!teamplay)
-               return 0;
-       if(autocvar_g_campaign)
-               return 0;
-       if(autocvar_bot_vs_human && (c3==-1 && c4==-1))
-               return 0;
-       if(!autocvar_g_balance_teams_force)
-               return -1;
-       return 1;
-}
-
 void TeamchangeFrags(entity e)
 {
        PlayerScore_Clear(e);
@@ -226,6 +211,7 @@ void InitGameplayMode()
        {
                ActivateTeamplay();
                have_team_spawns = -1; // request team spawns
+               MUTATOR_ADD(gamemode_onslaught);
        }
 
        if(g_race)
@@ -560,8 +546,6 @@ void CheckAllowedTeams (entity for_whom)
 
 float PlayerValue(entity p)
 {
-       if(IsTeamBalanceForced() == 1)
-               return 1;
        return 1;
        // FIXME: it always returns 1...
 }
@@ -641,11 +625,73 @@ void GetTeamCounts(entity ignore)
        }
 }
 
+float TeamSmallerEqThanTeam(float ta, float tb, entity e)
+{
+       // we assume that CheckAllowedTeams and GetTeamCounts have already been called
+       float f;
+       float ca = -1, cb = -1, cba = 0, cbb = 0, sa = 0, sb = 0;
+
+       switch(ta)
+       {
+               case 1: ca = c1; cba = cb1; sa = team1_score; break;
+               case 2: ca = c2; cba = cb2; sa = team2_score; break;
+               case 3: ca = c3; cba = cb3; sa = team3_score; break;
+               case 4: ca = c4; cba = cb4; sa = team4_score; break;
+       }
+       switch(tb)
+       {
+               case 1: cb = c1; cbb = cb1; sb = team1_score; break;
+               case 2: cb = c2; cbb = cb2; sb = team2_score; break;
+               case 3: cb = c3; cbb = cb3; sb = team3_score; break;
+               case 4: cb = c4; cbb = cb4; sb = team4_score; break;
+       }
+
+       // invalid
+       if(ca < 0 || cb < 0)
+               return FALSE;
+
+       // equal
+       if(ta == tb)
+               return TRUE;
+
+       if(clienttype(e) == CLIENTTYPE_REAL)
+       {
+               if(bots_would_leave)
+               {
+                       ca -= cba * 0.999;
+                       cb -= cbb * 0.999;
+               }
+       }
+       
+       // keep teams alive (teams of size 0 always count as smaller, ignoring score)
+       if(ca < 1)
+               if(cb >= 1)
+                       return TRUE;
+       if(ca >= 1)
+               if(cb < 1)
+                       return FALSE;
+
+       // first, normalize
+       f = max(ca, cb, 1);
+       ca /= f;
+       cb /= f;
+       f = max(sa, sb, 1);
+       sa /= f;
+       sb /= f;
+
+       // the more we're at the end of the match, the more take scores into account
+       f = bound(0, game_completion_ratio * autocvar_g_balance_teams_scorefactor, 1);
+       ca += (sa - ca) * f;
+       cb += (sb - cb) * f;
+
+       return ca <= cb;
+}
+
 // returns # of smallest team (1, 2, 3, 4)
 // NOTE: Assumes CheckAllowedTeams has already been called!
 float FindSmallestTeam(entity pl, float ignore_pl)
 {
-       float totalteams, balance_type, maxc;
+       float totalteams, t;
        totalteams = 0;
 
        // find out what teams are available
@@ -686,49 +732,26 @@ float FindSmallestTeam(entity pl, float ignore_pl)
        else
                GetTeamCounts(world);
 
-       // c1...c4 now have counts of each team
-       // figure out which is smallest, giving priority to the team the player is already on as a tie-breaker
-
-       // 2 gives priority to what team you're already on, 1 goes in order
-       // 2 doesn't seem to work though...
-       balance_type = 1;
-
-       if(bots_would_leave)
-       //if(pl.classname != "player")
-       if(clienttype(pl) != CLIENTTYPE_BOT)
-       {
-               c1 -= cb1 * 255.0/256.0;
-               c2 -= cb2 * 255.0/256.0;
-               c3 -= cb3 * 255.0/256.0;
-               c4 -= cb4 * 255.0/256.0;
-       }
-       maxc = max4(c1, c2, c3, c4);
-
        RandomSelection_Init();
-       if(balance_type == 1)
-       {
-               // 1: use team count, then score (note: can only use 8 significant bits of score)
-               if(c1 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + float2range01(-team1_score) / 256.0);
-               if(c2 >= 0) RandomSelection_Add(world, 2, string_null, 1, (maxc - c2) + float2range01(-team2_score) / 256.0);
-               if(c3 >= 0) RandomSelection_Add(world, 3, string_null, 1, (maxc - c3) + float2range01(-team3_score) / 256.0);
-               if(c4 >= 0) RandomSelection_Add(world, 4, string_null, 1, (maxc - c4) + float2range01(-team4_score) / 256.0);
-       }
-       else if(balance_type == 2)
-       {
-               // 1: use team count, if equal prefer own team
-               if(c1 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM1) / 512.0);
-               if(c2 >= 0) RandomSelection_Add(world, 2, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM2) / 512.0);
-               if(c3 >= 0) RandomSelection_Add(world, 3, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM3) / 512.0);
-               if(c4 >= 0) RandomSelection_Add(world, 4, string_null, 1, (maxc - c1) + (self.team == COLOR_TEAM4) / 512.0);
-       }
-       else if(balance_type == 3)
-       {
-               // 1: use team count, then score, if equal prefer own team (probably fails due to float accuracy problems)
-               if(c1 >= 0) RandomSelection_Add(world, 1, string_null, 1, (maxc - c1) + float2range01(-team1_score + 0.5 * (self.team == COLOR_TEAM1)) / 256.0);
-               if(c2 >= 0) RandomSelection_Add(world, 2, string_null, 1, (maxc - c2) + float2range01(-team2_score + 0.5 * (self.team == COLOR_TEAM2)) / 256.0);
-               if(c3 >= 0) RandomSelection_Add(world, 3, string_null, 1, (maxc - c3) + float2range01(-team3_score + 0.5 * (self.team == COLOR_TEAM3)) / 256.0);
-               if(c4 >= 0) RandomSelection_Add(world, 4, string_null, 1, (maxc - c4) + float2range01(-team4_score + 0.5 * (self.team == COLOR_TEAM4)) / 256.0);
-       }
+       
+       t = 1;
+       if(TeamSmallerEqThanTeam(2, t, pl))
+               t = 2;
+       if(TeamSmallerEqThanTeam(3, t, pl))
+               t = 3;
+       if(TeamSmallerEqThanTeam(4, t, pl))
+               t = 4;
+
+       // now t is the minimum, or A minimum!
+       if(t == 1 || TeamSmallerEqThanTeam(1, t, pl))
+               RandomSelection_Add(world, 1, string_null, 1, 1);
+       if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
+               RandomSelection_Add(world, 2, string_null, 1, 1);
+       if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
+               RandomSelection_Add(world, 3, string_null, 1, 1);
+       if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
+               RandomSelection_Add(world, 4, string_null, 1, 1);
+
        return RandomSelection_chosen_float;
 }
 
@@ -859,62 +882,15 @@ void SV_ChangeTeam(float _color)
                return; // changing teams is not allowed
        }
 
-       if(autocvar_g_balance_teams_prevent_imbalance)
+       // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
+       if(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
        {
-               // only allow changing to a smaller or equal size team
-
-               // find out what teams are available
-               //CheckAllowedTeams();
-               // count how many players on each team
-               GetTeamCounts(world);
-
-               // get desired team
-               if(dteam == 1 && c1 >= 0)//dcolor == COLOR_TEAM1 - 1)
-               {
-                       dcount = c1;
-                       dbotcount = cb1;
-               }
-               else if(dteam == 2 && c2 >= 0)//dcolor == COLOR_TEAM2 - 1)
+               GetTeamCounts(self);
+               if(!TeamSmallerEqThanTeam(dteam, steam, self))
                {
-                       dcount = c2;
-                       dbotcount = cb2;
-               }
-               else if(dteam == 3 && c3 >= 0)//dcolor == COLOR_TEAM3 - 1)
-               {
-                       dcount = c3;
-                       dbotcount = cb3;
-               }
-               else if(dteam == 4 && c4 >= 0)//dcolor == COLOR_TEAM4 - 1)
-               {
-                       dcount = c4;
-                       dbotcount = cb4;
-               }
-               else
-               {
-                       sprint(self, "Cannot change to an invalid team\n");
-
+                       sprint(self, "Cannot change to a larger/better/shinier team\n");
                        return;
                }
-
-               // get starting team
-               if(steam == 1)//scolor == COLOR_TEAM1 - 1)
-                       scount = c1;
-               else if(steam == 2)//scolor == COLOR_TEAM2 - 1)
-                       scount = c2;
-               else if(steam == 3)//scolor == COLOR_TEAM3 - 1)
-                       scount = c3;
-               else // if(steam == 4)//scolor == COLOR_TEAM4 - 1)
-                       scount = c4;
-
-               if(scount) // started at a valid, nonempty team
-               {
-                       // check if we're trying to change to a larger team that doens't have bots to swap with
-                       if(dcount >= scount && dbotcount <= 0)
-                       {
-                               sprint(self, "Cannot change to a larger team\n");
-                               return; // can't change to a larger team
-                       }
-               }
        }
 
 //     bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
@@ -1076,87 +1052,6 @@ void ShufflePlayerOutOfTeam (float source_team)
        centerprint(selected, strcat("You have been moved into a different team to improve team balance\nYou are now on: ", ColoredTeamName(selected.team)));
 }
 
-void CauseRebalance(float source_team, float howmany_toomany)
-{
-       if(IsTeamBalanceForced() == 1)
-       {
-               bprint("Rebalancing Teams\n");
-               ShufflePlayerOutOfTeam(source_team);
-       }
-}
-
-// part of g_balance_teams_force
-// occasionally perform an audit of the teams to make
-// sure they're more or less balanced in player count.
-void AuditTeams()
-{
-       float numplayers, numteams, smallest, toomany;
-       float balance;
-       balance = IsTeamBalanceForced();
-       if(balance == 0)
-               return;
-
-       if(audit_teams_time > time)
-               return;
-
-       audit_teams_time = time + 4 + random();
-
-//     bprint("Auditing teams\n");
-
-       CheckAllowedTeams(world);
-       GetTeamCounts(world);
-
-
-       numteams = numplayers = smallest = 0;
-       if(c1 >= 0)
-       {
-               numteams = numteams + 1;
-               numplayers = numplayers + c1;
-               smallest = c1;
-       }
-       if(c2 >= 0)
-       {
-               numteams = numteams + 1;
-               numplayers = numplayers + c2;
-               if(c2 < smallest)
-                       smallest = c2;
-       }
-       if(c3 >= 0)
-       {
-               numteams = numteams + 1;
-               numplayers = numplayers + c3;
-               if(c3 < smallest)
-                       smallest = c3;
-       }
-       if(c4 >= 0)
-       {
-               numteams = numteams + 1;
-               numplayers = numplayers + c4;
-               if(c4 < smallest)
-                       smallest = c4;
-       }
-
-       if(numplayers <= 0)
-               return; // no players to move around
-       if(numteams < 2)
-               return; // don't bother shuffling if for some reason there aren't any teams
-
-       toomany = smallest + 1;
-
-       if(c1 && c1 > toomany)
-               CauseRebalance(1, c1 - toomany);
-       if(c2 && c2 > toomany)
-               CauseRebalance(2, c2 - toomany);
-       if(c3 && c3 > toomany)
-               CauseRebalance(3, c3 - toomany);
-       if(c4 && c4 > toomany)
-               CauseRebalance(4, c4 - toomany);
-
-       // if teams are still unbalanced, balance them further in the next audit,
-       // which will happen sooner (keep doing rapid audits until things are in order)
-       audit_teams_time = time + 0.7 + random()*0.3;
-}
-
 // code from here on is just to support maps that don't have team entities
 void tdm_spawnteam (string teamname, float teamcolor)
 {