]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Merge branch 'master' into Lyberta/TeamplayFixes_
authorLyberta <lyberta@lyberta.net>
Wed, 5 Jul 2017 23:13:00 +0000 (02:13 +0300)
committerLyberta <lyberta@lyberta.net>
Wed, 5 Jul 2017 23:13:00 +0000 (02:13 +0300)
qcsrc/server/autocvars.qh
qcsrc/server/client.qc
qcsrc/server/command/cmd.qc
qcsrc/server/command/sv_cmd.qc
qcsrc/server/mutators/events.qh
qcsrc/server/player.qc
qcsrc/server/player.qh
qcsrc/server/teamplay.qc
qcsrc/server/teamplay.qh

index 0c2c1ed4feb90f96b025792af4737a1b715f967e..66cb5172a187e2b865c53e3e16bd98237973a785 100644 (file)
@@ -88,7 +88,7 @@ float autocvar_g_balance_superweapons_time;
 float autocvar_g_balance_selfdamagepercent;
 bool autocvar_g_balance_teams;
 bool autocvar_g_balance_teams_prevent_imbalance;
-float autocvar_g_balance_teams_scorefactor;
+//float autocvar_g_balance_teams_scorefactor;
 float autocvar_g_ballistics_density_corpse;
 float autocvar_g_ballistics_density_player;
 float autocvar_g_ballistics_mindistance;
index 381373ad2bcb713d93ccc4046efa6ac2341c14ed..c762ab4ee6c7706fa39de42a9fb16f3993841931 100644 (file)
@@ -271,7 +271,9 @@ void PutObserverInServer(entity this)
        if (mutator_returnvalue) {
            // mutator prevents resetting teams+score
        } else {
+               int oldteam = this.team;
                this.team = -1;  // move this as it is needed to log the player spectating in eventlog
+               MUTATOR_CALLHOOK(Player_ChangedTeam, this, oldteam, this.team); // Lyberta: added hook
         this.frags = FRAGS_SPECTATOR;
         PlayerScore_Clear(this);  // clear scores when needed
     }
@@ -886,8 +888,10 @@ void ClientKill_Now(entity this)
        if(this.killindicator_teamchange)
                ClientKill_Now_TeamChange(this);
 
-       if(!IS_SPEC(this) && !IS_OBSERVER(this))
+       if (!IS_SPEC(this) && !IS_OBSERVER(this) && MUTATOR_CALLHOOK(ClientKill_Now, this) == false)
+       {
                Damage(this, this, this, 100000, DEATH_KILL.m_id, this.origin, '0 0 0');
+       }
 
        // now I am sure the player IS dead
 }
index da9fd8621f171b34aed6ab9ca144b3e0a563fa37..51c2fa4f312c8c791b3f53c03f8b97dc9bd79743 100644 (file)
@@ -319,82 +319,98 @@ void ClientCommand_selectteam(entity caller, float request, float argc)
        {
                case CMD_REQUEST_COMMAND:
                {
-                       if (argv(1) != "")
+                       if (argv(1) == "")
                        {
-                               if (IS_CLIENT(caller))
+                               return;
+                       }
+                       if (!IS_CLIENT(caller))
+                       {
+                               return;
+                       }
+                       if (!teamplay)
+                       {
+                               sprint(caller, "^7selectteam can only be used in teamgames\n");
+                               return;
+                       }
+                       if (caller.team_forced > 0)
+                       {
+                               sprint(caller, "^7selectteam can not be used as your team is forced\n");
+                               return;
+                       }
+                       if (lockteams)
+                       {
+                               sprint(caller, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
+                               return;
+                       }
+                       float selection;
+                       switch (argv(1))
+                       {
+                               case "red":
                                {
-                                       if (teamplay)
-                                       {
-                                               if (caller.team_forced <= 0)
-                                               {
-                                                       if (!lockteams)
-                                                       {
-                                                               float selection;
-
-                                                               switch (argv(1))
-                                                               {
-                                                                       case "red": selection = NUM_TEAM_1;
-                                                                               break;
-                                                                       case "blue": selection = NUM_TEAM_2;
-                                                                               break;
-                                                                       case "yellow": selection = NUM_TEAM_3;
-                                                                               break;
-                                                                       case "pink": selection = NUM_TEAM_4;
-                                                                               break;
-                                                                       case "auto": selection = (-1);
-                                                                               break;
-
-                                                                       default: selection = 0;
-                                                                               break;
-                                                               }
-
-                                                               if (selection)
-                                                               {
-                                                                       if (caller.team == selection && selection != -1 && !IS_DEAD(caller))
-                                                                       {
-                                                                               sprint(caller, "^7You already are on that team.\n");
-                                                                       }
-                                                                       else if (caller.wasplayer && autocvar_g_changeteam_banned)
-                                                                       {
-                                                                               sprint(caller, "^1You cannot change team, forbidden by the server.\n");
-                                                                       }
-                                                                       else
-                                                                       {
-                                                                               if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
-                                                                               {
-                                                                                       CheckAllowedTeams(caller);
-                                                                                       GetTeamCounts(caller);
-                                                                                       if (!TeamSmallerEqThanTeam(Team_TeamToNumber(selection), Team_TeamToNumber(caller.team), caller))
-                                                                                       {
-                                                                                               Send_Notification(NOTIF_ONE, caller, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
-                                                                                               return;
-                                                                                       }
-                                                                               }
-                                                                               ClientKill_TeamChange(caller, selection);
-                                                                       }
-                                                                       if(!IS_PLAYER(caller))
-                                                                               caller.team_selected = true; // avoids asking again for team selection on join
-                                                               }
-                                                       }
-                                                       else
-                                                       {
-                                                               sprint(caller, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
-                                                       }
-                                               }
-                                               else
-                                               {
-                                                       sprint(caller, "^7selectteam can not be used as your team is forced\n");
-                                               }
-                                       }
-                                       else
-                                       {
-                                               sprint(caller, "^7selectteam can only be used in teamgames\n");
-                                       }
+                                       selection = NUM_TEAM_1;
+                                       break;
+                               }
+                               case "blue":
+                               {
+                                       selection = NUM_TEAM_2;
+                                       break;
                                }
+                               case "yellow":
+                               {
+                                       selection = NUM_TEAM_3;
+                                       break;
+                               }
+                               case "pink":
+                               {
+                                       selection = NUM_TEAM_4;
+                                       break;
+                               }
+                               case "auto":
+                               {
+                                       selection = (-1);
+                                       break;
+                               }
+                               default:
+                               {
+                                       return;
+                               }
+                       }
+                       if (caller.team == selection && selection != -1 && !IS_DEAD(caller))
+                       {
+                               sprint(caller, "^7You already are on that team.\n");
+                               return;
+                       }
+                       if (caller.wasplayer && autocvar_g_changeteam_banned)
+                       {
+                               sprint(caller, "^1You cannot change team, forbidden by the server.\n");
                                return;
                        }
+                       if (selection == -1)
+                       {
+                               ClientKill_TeamChange(caller, selection);
+                               if (!IS_PLAYER(caller))
+                               {
+                                       caller.team_selected = true; // avoids asking again for team selection on join
+                               }
+                               return;
+                       }
+                       if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
+                       {
+                               CheckAllowedTeams(caller);
+                               GetTeamCounts(caller);
+                               if ((BIT(Team_TeamToNumber(selection) - 1) & FindBestTeams(caller, false)) == 0)
+                               {
+                                       Send_Notification(NOTIF_ONE, caller, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
+                                       return;
+                               }
+                       }
+                       ClientKill_TeamChange(caller, selection);
+                       if (!IS_PLAYER(caller))
+                       {
+                               caller.team_selected = true; // avoids asking again for team selection on join
+                       }
+                       return;
                }
-
                default:
                        sprint(caller, "Incorrect parameters for ^2selectteam^7\n");
                case CMD_REQUEST_USAGE:
index 145e75952a81f9fd9e555292f26f666ee43473f8..44949da8ac8729d513cff5b317f5b7d35fdb4a43 100644 (file)
@@ -1091,9 +1091,15 @@ void GameCommand_moveplayer(float request, float argc)
 
                                                                // If so, lets continue and finally move the player
                                                                client.team_forced = 0;
-                                                               MoveToTeam(client, team_id, 6);
-                                                               successful = strcat(successful, (successful ? ", " : ""), playername(client, false));
-                                                               LOG_INFO("Player ", ftos(GetFilteredNumber(t)), " (", playername(client, false), ") has been moved to the ", Team_ColoredFullName(team_id), "^7.\n");
+                                                               if (MoveToTeam(client, team_id, 6))
+                                                               {
+                                                                       successful = strcat(successful, (successful ? ", " : ""), playername(client, false));
+                                                                       LOG_INFO("Player ", ftos(GetFilteredNumber(t)), " (", playername(client, false), ") has been moved to the ", Team_ColoredFullName(team_id), "^7.\n");
+                                                               }
+                                                               else
+                                                               {
+                                                                       LOG_INFO("Unable to move player ", ftos(GetFilteredNumber(t)), " (", playername(client, false), ")");
+                                                               }
                                                                continue;
                                                        }
                                                        else
index f186dd73bdd094c52ee113cfdd17b1f694ef412e..c8879918c3a89acb1a3cbf519ca96b002b5fd4ed 100644 (file)
@@ -130,6 +130,31 @@ MUTATOR_HOOKABLE(MatchEnd, EV_NO_ARGS);
     /**/
 MUTATOR_HOOKABLE(CheckAllowedTeams, EV_CheckAllowedTeams);
 
+/** return true to manually override team counts */
+MUTATOR_HOOKABLE(GetTeamCounts, EV_NO_ARGS);
+
+/** allow overriding of team counts */
+#define EV_GetTeamCount(i, o) \
+    /** team to count                   */ i(float, MUTATOR_ARGV_0_float) \
+    /** player to ignore                */ i(entity, MUTATOR_ARGV_1_entity) \
+    /** number of players in a team     */ i(float, MUTATOR_ARGV_2_float) \
+    /**/                                   o(float, MUTATOR_ARGV_2_float) \
+    /** number of bots in a team        */ i(float, MUTATOR_ARGV_3_float) \
+    /**/                                   o(float, MUTATOR_ARGV_3_float) \
+    /** lowest scoring player in a team */ i(entity, MUTATOR_ARGV_4_entity) \
+    /**/                                   o(entity, MUTATOR_ARGV_4_entity) \
+    /** lowest scoring bot in a team    */ i(entity, MUTATOR_ARGV_5_entity) \
+    /**/                                   o(entity, MUTATOR_ARGV_5_entity) \
+    /**/
+MUTATOR_HOOKABLE(GetTeamCount, EV_GetTeamCount);
+
+/** allows overriding best teams */
+#define EV_FindBestTeams(i, o) \
+    /** player checked   */ i(entity, MUTATOR_ARGV_0_entity) \
+    /** bitmask of teams */ o(float, MUTATOR_ARGV_1_float) \
+    /**/
+MUTATOR_HOOKABLE(FindBestTeams, EV_FindBestTeams);
+
 /** copies variables for spectating "spectatee" to "this" */
 #define EV_SpectateCopy(i, o) \
     /** spectatee   */ i(entity, MUTATOR_ARGV_0_entity) \
@@ -728,6 +753,12 @@ MUTATOR_HOOKABLE(Race_FinalCheckpoint, EV_Race_FinalCheckpoint);
     /**/
 MUTATOR_HOOKABLE(ClientKill, EV_ClientKill);
 
+/** called when player is about to be killed during kill command or changing teams */
+#define EV_ClientKill_Now(i, o) \
+    /** player */        i(entity, MUTATOR_ARGV_0_entity) \
+    /**/
+MUTATOR_HOOKABLE(ClientKill_Now, EV_ClientKill_Now);
+
 #define EV_FixClientCvars(i, o) \
     /** player */        i(entity, MUTATOR_ARGV_0_entity) \
     /**/
@@ -875,7 +906,9 @@ MUTATOR_HOOKABLE(PrepareExplosionByDamage, EV_PrepareExplosionByDamage);
     /**/
 MUTATOR_HOOKABLE(MonsterModel, EV_MonsterModel);
 
-/**/
+/**
+ * Called before player changes their team. Return true to block team change.
+ */
 #define EV_Player_ChangeTeam(i, o) \
     /** player */         i(entity, MUTATOR_ARGV_0_entity) \
        /** current team */   i(float, MUTATOR_ARGV_1_float) \
@@ -883,6 +916,24 @@ MUTATOR_HOOKABLE(MonsterModel, EV_MonsterModel);
     /**/
 MUTATOR_HOOKABLE(Player_ChangeTeam, EV_Player_ChangeTeam);
 
+/**
+ * Called after player has changed their team.
+ */
+#define EV_Player_ChangedTeam(i, o) \
+    /** player */         i(entity, MUTATOR_ARGV_0_entity) \
+       /** old team */       i(float, MUTATOR_ARGV_1_float) \
+       /** current team */   i(float, MUTATOR_ARGV_2_float) \
+    /**/
+MUTATOR_HOOKABLE(Player_ChangedTeam, EV_Player_ChangedTeam);
+
+/**
+ * Called when player is about to be killed when changing teams. Return true to block killing.
+ */
+#define EV_Player_ChangeTeamKill(i, o) \
+    /** player */    i(entity, MUTATOR_ARGV_0_entity) \
+    /**/
+MUTATOR_HOOKABLE(Player_ChangeTeamKill, EV_Player_ChangeTeamKill);
+
 /**/
 #define EV_URI_GetCallback(i, o) \
        /** id */       i(float, MUTATOR_ARGV_0_float) \
index faba40f3fc92de55833f334e72590c47ac2d2bac..7bd683c3065954f8a0260ae4bf5a368a97e1c8b5 100644 (file)
@@ -666,15 +666,19 @@ void PlayerDamage(entity this, entity inflictor, entity attacker, float damage,
        }
 }
 
-void MoveToTeam(entity client, int team_colour, int type)
+bool MoveToTeam(entity client, int team_colour, int type)
 {
        int lockteams_backup = lockteams;  // backup any team lock
        lockteams = 0;  // disable locked teams
        TeamchangeFrags(client);  // move the players frags
-       SetPlayerColors(client, team_colour - 1);  // set the players colour
+       if (!SetPlayerTeamSimple(client, team_colour))
+       {
+               return false;
+       }
        Damage(client, client, client, 100000, DEATH_AUTOTEAMCHANGE.m_id, client.origin, '0 0 0');  // kill the player
        lockteams = lockteams_backup;  // restore the team lock
        LogTeamchange(client.playerid, client.team, type);
+       return true;
 }
 
 /** print(), but only print if the server is not local */
index b9a5fa928835208119144e2d7749149c7685af4e..3b520dabcea413b61ce234bac27ea6732b7770d7 100644 (file)
@@ -69,7 +69,12 @@ void calculate_player_respawn_time(entity this);
 
 void ClientKill_Now_TeamChange(entity this);
 
-void MoveToTeam(entity client, float team_colour, float type);
+/// \brief Moves player to the specified team.
+/// \param[in,out] client Client to move.
+/// \param[in] team_colour Color of the team.
+/// \param[in] type ???
+/// \return True on success, false otherwise.
+bool MoveToTeam(entity client, float team_colour, float type);
 
 void PlayerDamage(entity this, entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force);
 
index d8bf72cd6b2a87bc27cc3700053716840c3bd8b3..985c683e8c2cc4d0dcd03fd42c3919fe0e041330 100644 (file)
@@ -169,49 +169,54 @@ void setcolor(entity this, int clr)
 #endif
 }
 
-void SetPlayerColors(entity pl, float _color)
+void SetPlayerColors(entity player, float _color)
 {
-       /*string s;
-       s = ftos(cl);
-       stuffcmd(pl, strcat("color ", s, " ", s, "\n")  );
-       pl.team = cl + 1;
-       //pl.clientcolors = pl.clientcolors - (pl.clientcolors & 15) + cl;
-       pl.clientcolors = 16*cl + cl;*/
-
-       float pants, shirt;
-       pants = _color & 0x0F;
-       shirt = _color & 0xF0;
-
-
-       if(teamplay) {
-               setcolor(pl, 16*pants + pants);
-       } else {
-               setcolor(pl, shirt + pants);
+       float pants = _color & 0x0F;
+       float shirt = _color & 0xF0;
+       if (teamplay)
+       {
+               setcolor(player, 16 * pants + pants);
+       }
+       else
+       {
+               setcolor(player, shirt + pants);
        }
 }
 
-void SetPlayerTeam(entity pl, float t, float s, float noprint)
+bool SetPlayerTeamSimple(entity player, int teamnum)
 {
-       float _color;
-
-       if(t == 4)
-               _color = NUM_TEAM_4 - 1;
-       else if(t == 3)
-               _color = NUM_TEAM_3 - 1;
-       else if(t == 2)
-               _color = NUM_TEAM_2 - 1;
-       else
-               _color = NUM_TEAM_1 - 1;
-
-       SetPlayerColors(pl,_color);
-
-       if(t != s) {
-               LogTeamchange(pl.playerid, pl.team, 3);  // log manual team join
-
-               if(!noprint)
-                       bprint(playername(pl, false), "^7 has changed from ", Team_NumberToColoredFullName(s), "^7 to ", Team_NumberToColoredFullName(t), "\n");
+       if (player.team == teamnum)
+       {
+               // This is important when players join the game and one of their color
+               // matches the team color while other doesn't. For example [BOT]Lion.
+               SetPlayerColors(player, teamnum - 1);
+               return true;
        }
+       if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, Team_TeamToNumber(
+               player.team), Team_TeamToNumber(teamnum)) == true)
+       {
+               // Mutator has blocked team change.
+               return false;
+       }
+       int oldteam = player.team;
+       SetPlayerColors(player, teamnum - 1);
+       MUTATOR_CALLHOOK(Player_ChangedTeam, player, oldteam, player.team);
+       return true;
+}
 
+void SetPlayerTeam(entity player, int destinationteam, int sourceteam, bool noprint)
+{
+       int teamnum = Team_NumberToTeam(destinationteam);
+       if (!SetPlayerTeamSimple(player, teamnum))
+       {
+               return;
+       }
+       LogTeamchange(player.playerid, player.team, 3);  // log manual team join
+       if (noprint)
+       {
+               return;
+       }
+       bprint(playername(player, false), "^7 has changed from ", Team_NumberToColoredFullName(sourceteam), "^7 to ", Team_NumberToColoredFullName(destinationteam), "\n");
 }
 
 // set c1...c4 to show what teams are allowed
@@ -220,7 +225,7 @@ void CheckAllowedTeams (entity for_whom)
        int teams_mask = 0;
 
        c1 = c2 = c3 = c4 = -1;
-       cb1 = cb2 = cb3 = cb4 = 0;
+       numbotsteam1 = numbotsteam2 = numbotsteam3 = numbotsteam4 = 0;
 
        string teament_name = string_null;
 
@@ -322,218 +327,430 @@ float PlayerValue(entity p)
 // teams that are allowed will now have their player counts stored in c1...c4
 void GetTeamCounts(entity ignore)
 {
-       float value, bvalue;
-       // now count how many players are on each team already
-
-       // FIXME: also find and memorize the lowest-scoring bot on each team (in case players must be shuffled around)
-       // also remember the lowest-scoring player
-
-       FOREACH_CLIENT(true, LAMBDA(
-               float t;
-               if(IS_PLAYER(it) || it.caplayer)
-                       t = it.team;
-               else if(it.team_forced > 0)
-                       t = it.team_forced; // reserve the spot
-               else
-                       continue;
-               if(it != ignore)// && it.netname != "")
+       if (MUTATOR_CALLHOOK(GetTeamCounts) == true)
+       {
+               if (c1 >= 0)
+               {
+                       MUTATOR_CALLHOOK(GetTeamCount, NUM_TEAM_1, ignore, c1, numbotsteam1,
+                               lowestplayerteam1, lowestbotteam1);
+                       c1 = M_ARGV(2, float);
+                       numbotsteam1 = M_ARGV(3, float);
+                       lowestplayerteam1 = M_ARGV(4, entity);
+                       lowestbotteam1 = M_ARGV(5, entity);
+               }
+               if (c2 >= 0)
                {
+                       MUTATOR_CALLHOOK(GetTeamCount, NUM_TEAM_2, ignore, c2, numbotsteam2,
+                               lowestplayerteam2, lowestbotteam2);
+                       c2 = M_ARGV(2, float);
+                       numbotsteam2 = M_ARGV(3, float);
+                       lowestplayerteam2 = M_ARGV(4, entity);
+                       lowestbotteam2 = M_ARGV(5, entity);
+               }
+               if (c3 >= 0)
+               {
+                       MUTATOR_CALLHOOK(GetTeamCount, NUM_TEAM_3, ignore, c3, numbotsteam3,
+                               lowestplayerteam3, lowestbotteam3);
+                       c3 = M_ARGV(2, float);
+                       numbotsteam3 = M_ARGV(3, float);
+                       lowestplayerteam3 = M_ARGV(4, entity);
+                       lowestbotteam3 = M_ARGV(5, entity);
+               }
+               if (c4 >= 0)
+               {
+                       MUTATOR_CALLHOOK(GetTeamCount, NUM_TEAM_4, ignore, c4, numbotsteam4,
+                               lowestplayerteam4, lowestbotteam4);
+                       c4 = M_ARGV(2, float);
+                       numbotsteam4 = M_ARGV(3, float);
+                       lowestplayerteam4 = M_ARGV(4, entity);
+                       lowestbotteam4 = M_ARGV(5, entity);
+               }
+       }
+       else
+       {
+               float value, bvalue;
+               // now count how many players are on each team already
+               float lowestplayerscore1 = FLOAT_MAX;
+               float lowestbotscore1 = FLOAT_MAX;
+               float lowestplayerscore2 = FLOAT_MAX;
+               float lowestbotscore2 = FLOAT_MAX;
+               float lowestplayerscore3 = FLOAT_MAX;
+               float lowestbotscore3 = FLOAT_MAX;
+               float lowestplayerscore4 = FLOAT_MAX;
+               float lowestbotscore4 = FLOAT_MAX;
+               FOREACH_CLIENT(true, LAMBDA(
+                       float t;
+                       if (IS_PLAYER(it) || it.caplayer)
+                       {
+                               t = it.team;
+                       }
+                       else if (it.team_forced > 0)
+                       {
+                               t = it.team_forced; // reserve the spot
+                       }
+                       else
+                       {
+                               continue;
+                       }
+                       if (it == ignore)
+                       {
+                               continue;
+                       }
                        value = PlayerValue(it);
-                       if(IS_BOT_CLIENT(it))
+                       if (IS_BOT_CLIENT(it))
+                       {
                                bvalue = value;
+                       }
                        else
+                       {
                                bvalue = 0;
-                       if(t == NUM_TEAM_1)
+                       }
+                       if (value == 0)
                        {
-                               if(c1 >= 0)
-                               {
-                                       c1 = c1 + value;
-                                       cb1 = cb1 + bvalue;
-                               }
+                               continue;
                        }
-                       if(t == NUM_TEAM_2)
+                       switch (t)
                        {
-                               if(c2 >= 0)
+                               case NUM_TEAM_1:
                                {
-                                       c2 = c2 + value;
-                                       cb2 = cb2 + bvalue;
+                                       if (c1 < 0)
+                                       {
+                                               break;
+                                       }
+                                       c1 += value;
+                                       numbotsteam1 += bvalue;
+                                       float tempscore = PlayerScore_Get(it, SP_SCORE);
+                                       if (!bvalue)
+                                       {
+                                               if (tempscore < lowestplayerscore1)
+                                               {
+                                                       lowestplayerteam1 = it;
+                                                       lowestplayerscore1 = tempscore;
+                                               }
+                                               break;
+                                       }
+                                       if (tempscore < lowestbotscore1)
+                                       {
+                                               lowestbotteam1 = it;
+                                               lowestbotscore1 = tempscore;
+                                       }
+                                       break;
                                }
-                       }
-                       if(t == NUM_TEAM_3)
-                       {
-                               if(c3 >= 0)
+                               case NUM_TEAM_2:
                                {
-                                       c3 = c3 + value;
-                                       cb3 = cb3 + bvalue;
+                                       if (c2 < 0)
+                                       {
+                                               break;
+                                       }
+                                       c2 += value;
+                                       numbotsteam2 += bvalue;
+                                       float tempscore = PlayerScore_Get(it, SP_SCORE);
+                                       if (!bvalue)
+                                       {
+                                               if (tempscore < lowestplayerscore2)
+                                               {
+                                                       lowestplayerteam2 = it;
+                                                       lowestplayerscore2 = tempscore;
+                                               }
+                                               break;
+                                       }
+                                       if (tempscore < lowestbotscore2)
+                                       {
+                                               lowestbotteam2 = it;
+                                               lowestbotscore2 = tempscore;
+                                       }
+                                       break;
                                }
-                       }
-                       if(t == NUM_TEAM_4)
-                       {
-                               if(c4 >= 0)
+                               case NUM_TEAM_3:
                                {
-                                       c4 = c4 + value;
-                                       cb4 = cb4 + bvalue;
+                                       if (c3 < 0)
+                                       {
+                                               break;
+                                       }
+                                       c3 += value;
+                                       numbotsteam3 += bvalue;
+                                       float tempscore = PlayerScore_Get(it, SP_SCORE);
+                                       if (!bvalue)
+                                       {
+                                               if (tempscore < lowestplayerscore3)
+                                               {
+                                                       lowestplayerteam3 = it;
+                                                       lowestplayerscore3 = tempscore;
+                                               }
+                                               break;
+                                       }
+                                       if (tempscore < lowestbotscore3)
+                                       {
+                                               lowestbotteam3 = it;
+                                               lowestbotscore3 = tempscore;
+                                       }
+                                       break;
+                               }
+                               case NUM_TEAM_4:
+                               {
+                                       if (c4 < 0)
+                                       {
+                                               break;
+                                       }
+                                       c4 += value;
+                                       numbotsteam4 += bvalue;
+                                       float tempscore = PlayerScore_Get(it, SP_SCORE);
+                                       if (!bvalue)
+                                       {
+                                               if (tempscore < lowestplayerscore4)
+                                               {
+                                                       lowestplayerteam4 = it;
+                                                       lowestplayerscore4 = tempscore;
+                                               }
+                                               break;
+                                       }
+                                       if (tempscore < lowestbotscore4)
+                                       {
+                                               lowestbotteam4 = it;
+                                               lowestbotscore4 = tempscore;
+                                       }
+                                       break;
                                }
                        }
-               }
-       ));
+               ));
+       }
 
        // if the player who has a forced team has not joined yet, reserve the spot
        if(autocvar_g_campaign)
        {
                switch(autocvar_g_campaign_forceteam)
                {
-                       case 1: if(c1 == cb1) ++c1; break;
-                       case 2: if(c2 == cb2) ++c2; break;
-                       case 3: if(c3 == cb3) ++c3; break;
-                       case 4: if(c4 == cb4) ++c4; break;
+                       case 1: if(c1 == numbotsteam1) ++c1; break;
+                       case 2: if(c2 == numbotsteam2) ++c2; break;
+                       case 3: if(c3 == numbotsteam3) ++c3; break;
+                       case 4: if(c4 == numbotsteam4) ++c4; break;
                }
        }
 }
 
-float TeamSmallerEqThanTeam(float ta, float tb, entity e)
+bool IsTeamSmallerThanTeam(int teama, int teamb, entity e, bool usescore)
 {
+       // equal
+       if (teama == teamb)
+       {
+               return false;
+       }
        // 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;
+       float numplayersteama = -1, numplayersteamb = -1;
+       float numbotsteama = 0, numbotsteamb = 0;
+       float scoreteama = 0, scoreteamb = 0;
 
-       switch(ta)
+       switch (teama)
        {
-               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;
+               case 1: numplayersteama = c1; numbotsteama = numbotsteam1; scoreteama = team1_score; break;
+               case 2: numplayersteama = c2; numbotsteama = numbotsteam2; scoreteama = team2_score; break;
+               case 3: numplayersteama = c3; numbotsteama = numbotsteam3; scoreteama = team3_score; break;
+               case 4: numplayersteama = c4; numbotsteama = numbotsteam4; scoreteama = team4_score; break;
        }
-       switch(tb)
+       switch (teamb)
        {
-               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;
+               case 1: numplayersteamb = c1; numbotsteamb = numbotsteam1; scoreteamb = team1_score; break;
+               case 2: numplayersteamb = c2; numbotsteamb = numbotsteam2; scoreteamb = team2_score; break;
+               case 3: numplayersteamb = c3; numbotsteamb = numbotsteam3; scoreteamb = team3_score; break;
+               case 4: numplayersteamb = c4; numbotsteamb = numbotsteam4; scoreteamb = team4_score; break;
        }
 
        // invalid
-       if(ca < 0 || cb < 0)
+       if (numplayersteama < 0 || numplayersteamb < 0)
                return false;
 
-       // equal
-       if(ta == tb)
+       if ((IS_REAL_CLIENT(e) && bots_would_leave))
+       {
+               numplayersteama -= numbotsteama;
+               numplayersteamb -= numbotsteamb;
+       }
+       if (!usescore)
+       {
+               return numplayersteama < numplayersteamb;
+       }
+       if (numplayersteama < numplayersteamb)
+       {
                return true;
-
-       if(IS_REAL_CLIENT(e))
+       }
+       if (numplayersteama > numplayersteamb)
        {
-               if(bots_would_leave)
-               {
-                       ca -= cba * 0.999;
-                       cb -= cbb * 0.999;
-               }
+               return false;
        }
-
-       // 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;
+       return scoreteama < scoreteamb; 
 }
 
-// returns # of smallest team (1, 2, 3, 4)
-// NOTE: Assumes CheckAllowedTeams has already been called!
-float FindSmallestTeam(entity pl, float ignore_pl)
+bool IsTeamEqualToTeam(int teama, int teamb, entity e, bool usescore)
 {
-       int totalteams = 0;
-       int t = 1; // initialize with a random team?
-       if(c4 >= 0) t = 4;
-       if(c3 >= 0) t = 3;
-       if(c2 >= 0) t = 2;
-       if(c1 >= 0) t = 1;
+       // equal
+       if (teama == teamb)
+       {
+               return true;
+       }
+       // we assume that CheckAllowedTeams and GetTeamCounts have already been called
+       float numplayersteama = -1, numplayersteamb = -1;
+       float numbotsteama = 0, numbotsteamb = 0;
+       float scoreteama = 0, scoreteamb = 0;
 
-       // find out what teams are available
-       //CheckAllowedTeams();
+       switch (teama)
+       {
+               case 1: numplayersteama = c1; numbotsteama = numbotsteam1; scoreteama = team1_score; break;
+               case 2: numplayersteama = c2; numbotsteama = numbotsteam2; scoreteama = team2_score; break;
+               case 3: numplayersteama = c3; numbotsteama = numbotsteam3; scoreteama = team3_score; break;
+               case 4: numplayersteama = c4; numbotsteama = numbotsteam4; scoreteama = team4_score; break;
+       }
+       switch (teamb)
+       {
+               case 1: numplayersteamb = c1; numbotsteamb = numbotsteam1; scoreteamb = team1_score; break;
+               case 2: numplayersteamb = c2; numbotsteamb = numbotsteam2; scoreteamb = team2_score; break;
+               case 3: numplayersteamb = c3; numbotsteamb = numbotsteam3; scoreteamb = team3_score; break;
+               case 4: numplayersteamb = c4; numbotsteamb = numbotsteam4; scoreteamb = team4_score; break;
+       }
 
-       // make sure there are at least 2 teams to join
-       if(c1 >= 0)
-               totalteams = totalteams + 1;
-       if(c2 >= 0)
-               totalteams = totalteams + 1;
-       if(c3 >= 0)
-               totalteams = totalteams + 1;
-       if(c4 >= 0)
-               totalteams = totalteams + 1;
+       // invalid
+       if (numplayersteama < 0 || numplayersteamb < 0)
+               return false;
 
-       if((autocvar_bot_vs_human || pl.team_forced > 0) && totalteams == 1)
-               totalteams += 1;
+       if ((IS_REAL_CLIENT(e) && bots_would_leave))
+       {
+               numplayersteama -= numbotsteama;
+               numplayersteamb -= numbotsteamb;
+       }
+       if (!usescore)
+       {
+               return numplayersteama == numplayersteamb;
+       }
+       if (numplayersteama < numplayersteamb)
+       {
+               return false;
+       }
+       if (numplayersteama > numplayersteamb)
+       {
+               return false;
+       }
+       return scoreteama == scoreteamb;        
+}
 
-       if(totalteams <= 1)
+int FindBestTeams(entity player, bool usescore)
+{
+       if (MUTATOR_CALLHOOK(FindBestTeams, player) == true)
+       {
+               return M_ARGV(1, float);
+       }
+       int teambits = 0;
+       int previousteam = 0;
+       if (c1 >= 0)
        {
-               if(autocvar_g_campaign && pl && IS_REAL_CLIENT(pl))
-                       return 1; // special case for campaign and player joining
-               else if(totalteams == 1) // single team
-                       LOG_TRACEF("Only 1 team available for %s, you may need to fix your map", MapInfo_Type_ToString(MapInfo_CurrentGametype()));
-               else // no teams, major no no
-                       error(sprintf("No teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
+               teambits = BIT(0);
+               previousteam = 1;
        }
+       if (c2 >= 0)
+       {
+               if (previousteam == 0)
+               {
+                       teambits = BIT(1);
+                       previousteam = 2;
+               }
+               else if (IsTeamSmallerThanTeam(2, previousteam, player, usescore))
+               {
+                       teambits = BIT(1);
+                       previousteam = 2;
+               }
+               else if (IsTeamEqualToTeam(2, previousteam, player, usescore))
+               {
+                       teambits |= BIT(1);
+                       previousteam = 2;
+               }
+       }
+       if (c3 >= 0)
+       {
+               if (previousteam == 0)
+               {
+                       teambits = BIT(2);
+                       previousteam = 3;
+               }
+               else if (IsTeamSmallerThanTeam(3, previousteam, player, usescore))
+               {
+                       teambits = BIT(2);
+                       previousteam = 3;
+               }
+               else if (IsTeamEqualToTeam(3, previousteam, player, usescore))
+               {
+                       teambits |= BIT(2);
+                       previousteam = 3;
+               }
+       }
+       if (c4 >= 0)
+       {
+               if (previousteam == 0)
+               {
+                       teambits = BIT(3);
+               }
+               else if (IsTeamSmallerThanTeam(4, previousteam, player, usescore))
+               {
+                       teambits = BIT(3);
+               }
+               else if (IsTeamEqualToTeam(4, previousteam, player, usescore))
+               {
+                       teambits |= BIT(3);
+               }
+       }
+       return teambits;
+}
 
+// returns # of smallest team (1, 2, 3, 4)
+// NOTE: Assumes CheckAllowedTeams has already been called!
+float FindSmallestTeam(entity pl, float ignore_pl)
+{
        // count how many players are in each team
-       if(ignore_pl)
+       if (ignore_pl)
+       {
                GetTeamCounts(pl);
+       }
        else
+       {
                GetTeamCounts(NULL);
-
+       }
+       int teambits = FindBestTeams(pl, true);
+       if (teambits == 0)
+       {
+               error(sprintf("No teams available for %s\n", MapInfo_Type_ToString(MapInfo_CurrentGametype())));
+       }
        RandomSelection_Init();
-
-       if(TeamSmallerEqThanTeam(1, t, pl))
-               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))
+       if ((teambits & BIT(0)) != 0)
+       {
                RandomSelection_AddFloat(1, 1, 1);
-       if(t == 2 || TeamSmallerEqThanTeam(2, t, pl))
+       }
+       if ((teambits & BIT(1)) != 0)
+       {
                RandomSelection_AddFloat(2, 1, 1);
-       if(t == 3 || TeamSmallerEqThanTeam(3, t, pl))
+       }
+       if ((teambits & BIT(2)) != 0)
+       {
                RandomSelection_AddFloat(3, 1, 1);
-       if(t == 4 || TeamSmallerEqThanTeam(4, t, pl))
+       }
+       if ((teambits & BIT(3)) != 0)
+       {
                RandomSelection_AddFloat(4, 1, 1);
-
+       }
        return RandomSelection_chosen_float;
 }
 
 int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam)
 {
-       float smallest, selectedteam;
-
        // don't join a team if we're not playing a team game
-       if(!teamplay)
+       if (!teamplay)
+       {
                return 0;
+       }
 
        // find out what teams are available
        CheckAllowedTeams(this);
 
+       float selectedteam;
+
        // if we don't care what team he ends up on, put him on whatever team he entered as.
        // if he's not on a valid team, then let other code put him on the smallest team
-       if(!forcebestteam)
+       if (!forcebestteam)
        {
                if(     c1 >= 0 && this.team == NUM_TEAM_1)
                        selectedteam = this.team;
@@ -546,11 +763,11 @@ int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam)
                else
                        selectedteam = -1;
 
-               if(selectedteam > 0)
+               if (selectedteam > 0)
                {
-                       if(!only_return_best)
+                       if (!only_return_best)
                        {
-                               SetPlayerColors(this, selectedteam - 1);
+                               SetPlayerTeamSimple(this, selectedteam);
 
                                // when JoinBestTeam is called by client.qc/ClientKill_Now_TeamChange the players team is -1 and thus skipped
                                // when JoinBestTeam is called by client.qc/ClientConnect the player_id is 0 the log attempt is rejected
@@ -561,45 +778,35 @@ int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam)
                // otherwise end up on the smallest team (handled below)
        }
 
-       smallest = FindSmallestTeam(this, true);
-
-       if(!only_return_best && !this.bot_forced_team)
+       float bestteam = FindSmallestTeam(this, true);
+       if (only_return_best || this.bot_forced_team)
        {
-               TeamchangeFrags(this);
-               if(smallest == 1)
-               {
-                       SetPlayerColors(this, NUM_TEAM_1 - 1);
-               }
-               else if(smallest == 2)
-               {
-                       SetPlayerColors(this, NUM_TEAM_2 - 1);
-               }
-               else if(smallest == 3)
-               {
-                       SetPlayerColors(this, NUM_TEAM_3 - 1);
-               }
-               else if(smallest == 4)
-               {
-                       SetPlayerColors(this, NUM_TEAM_4 - 1);
-               }
-               else
-               {
-                       error("smallest team: invalid team\n");
-               }
-
-               LogTeamchange(this.playerid, this.team, 2); // log auto join
-
-               if(!IS_DEAD(this))
-                       Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
+               return bestteam;
        }
-
-       return smallest;
+       bestteam = Team_NumberToTeam(bestteam);
+       if (bestteam == -1)
+       {
+               error("JoinBestTeam: invalid team\n");
+       }
+       int oldteam = Team_TeamToNumber(this.team);
+       TeamchangeFrags(this);
+       SetPlayerTeamSimple(this, bestteam);
+       LogTeamchange(this.playerid, this.team, 2); // log auto join
+       if (!IS_BOT_CLIENT(this))
+       {
+               AutoBalanceBots(oldteam, Team_TeamToNumber(bestteam));
+       }
+       if (!IS_DEAD(this) && (MUTATOR_CALLHOOK(Player_ChangeTeamKill, this) ==
+               false))
+       {
+               Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
+       }
+       return bestteam;
 }
 
-//void() ctf_playerchanged;
 void SV_ChangeTeam(entity this, float _color)
 {
-       float scolor, dcolor, steam, dteam; //, dbotcount, scount, dcount;
+       float sourcecolor, destinationcolor, sourceteam, destinationteam;
 
        // in normal deathmatch we can just apply the color and we're done
        if(!teamplay)
@@ -615,75 +822,139 @@ void SV_ChangeTeam(entity this, float _color)
        if(!teamplay)
                return;
 
-       scolor = this.clientcolors & 0x0F;
-       dcolor = _color & 0x0F;
-
-       if(scolor == NUM_TEAM_1 - 1)
-               steam = 1;
-       else if(scolor == NUM_TEAM_2 - 1)
-               steam = 2;
-       else if(scolor == NUM_TEAM_3 - 1)
-               steam = 3;
-       else // if(scolor == NUM_TEAM_4 - 1)
-               steam = 4;
-       if(dcolor == NUM_TEAM_1 - 1)
-               dteam = 1;
-       else if(dcolor == NUM_TEAM_2 - 1)
-               dteam = 2;
-       else if(dcolor == NUM_TEAM_3 - 1)
-               dteam = 3;
-       else // if(dcolor == NUM_TEAM_4 - 1)
-               dteam = 4;
+       sourcecolor = this.clientcolors & 0x0F;
+       destinationcolor = _color & 0x0F;
 
+       sourceteam = Team_TeamToNumber(sourcecolor + 1);
+       destinationteam = Team_TeamToNumber(destinationcolor + 1);
+       
        CheckAllowedTeams(this);
 
-       if(dteam == 1 && c1 < 0) dteam = 4;
-       if(dteam == 4 && c4 < 0) dteam = 3;
-       if(dteam == 3 && c3 < 0) dteam = 2;
-       if(dteam == 2 && c2 < 0) dteam = 1;
+       if (destinationteam == 1 && c1 < 0) destinationteam = 4;
+       if (destinationteam == 4 && c4 < 0) destinationteam = 3;
+       if (destinationteam == 3 && c3 < 0) destinationteam = 2;
+       if (destinationteam == 2 && c2 < 0) destinationteam = 1;
 
        // not changing teams
-       if(scolor == dcolor)
+       if (sourcecolor == destinationcolor)
        {
-               //bprint("same team change\n");
-               SetPlayerTeam(this, dteam, steam, true);
+               SetPlayerTeam(this, destinationteam, sourceteam, true);
                return;
        }
 
-       if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && this.wasplayer)) {
+       if (autocvar_g_campaign || (autocvar_g_changeteam_banned && this.wasplayer))
+       {
                Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
                return; // changing teams is not allowed
        }
 
        // 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)
+       if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
        {
                GetTeamCounts(this);
-               if(!TeamSmallerEqThanTeam(dteam, steam, this))
+               if ((BIT(destinationteam - 1) & FindBestTeams(this, false)) == 0)
                {
                        Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
                        return;
                }
        }
-
-//     bprint("allow change teams from ", ftos(steam), " to ", ftos(dteam), "\n");
-
-       if(IS_PLAYER(this) && steam != dteam)
+       if(IS_PLAYER(this) && sourceteam != destinationteam)
        {
                // reduce frags during a team change
                TeamchangeFrags(this);
        }
+       SetPlayerTeam(this, destinationteam, sourceteam, !IS_CLIENT(this));
+       AutoBalanceBots(sourceteam, destinationteam);
+       if (!IS_PLAYER(this) || (sourceteam == destinationteam))
+       {
+               return;
+       }
+       // kill player when changing teams
+       if (IS_DEAD(this) || (MUTATOR_CALLHOOK(Player_ChangeTeamKill, this) == true))
+       {
+               return;
+       }
+       Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
+}
 
-       MUTATOR_CALLHOOK(Player_ChangeTeam, this, steam, dteam);
-
-       SetPlayerTeam(this, dteam, steam, !IS_CLIENT(this));
-
-       if(IS_PLAYER(this) && steam != dteam)
+void AutoBalanceBots(int sourceteam, int destinationteam)
+{
+       if ((sourceteam == -1) || (destinationteam == -1))
+       {
+               return;
+       }
+       if (!autocvar_g_balance_teams ||
+               !autocvar_g_balance_teams_prevent_imbalance)
+       {
+               return;
+       }
+       int numplayerssourceteam = 0;
+       int numplayersdestinationteam = 0;
+       entity lowestbotdestinationteam = NULL;
+       switch (sourceteam)
+       {
+               case 1:
+               {
+                       numplayerssourceteam = c1;
+                       break;
+               }
+               case 2:
+               {
+                       numplayerssourceteam = c2;
+                       break;
+               }
+               case 3:
+               {
+                       numplayerssourceteam = c3;
+                       break;
+               }
+               case 4:
+               {
+                       numplayerssourceteam = c4;
+                       break;
+               }
+       }
+       switch (destinationteam)
+       {
+               case 1:
+               {
+                       numplayersdestinationteam = c1;
+                       lowestbotdestinationteam = lowestbotteam1;
+                       break;
+               }
+               case 2:
+               {
+                       numplayersdestinationteam = c2;
+                       lowestbotdestinationteam = lowestbotteam2;
+                       break;
+               }
+               case 3:
+               {
+                       numplayersdestinationteam = c3;
+                       lowestbotdestinationteam = lowestbotteam3;
+                       break;
+               }
+               case 4:
+               {
+                       numplayersdestinationteam = c4;
+                       lowestbotdestinationteam = lowestbotteam4;
+                       break;
+               }
+       }
+       if ((numplayersdestinationteam <= numplayerssourceteam) ||
+               (lowestbotdestinationteam == NULL))
        {
-               // kill player when changing teams
-               if(!IS_DEAD(this))
-                       Damage(this, this, this, 100000, DEATH_TEAMCHANGE.m_id, this.origin, '0 0 0');
+               return;
+       }
+       SetPlayerTeamSimple(lowestbotdestinationteam, Team_NumberToTeam(sourceteam));
+       if (IS_DEAD(lowestbotdestinationteam) || (MUTATOR_CALLHOOK(
+               Player_ChangeTeamKill, lowestbotdestinationteam) == true))
+       {
+               return;
        }
+       Damage(lowestbotdestinationteam, lowestbotdestinationteam,
+               lowestbotdestinationteam, 100000, DEATH_TEAMCHANGE.m_id,
+               lowestbotdestinationteam.origin, '0 0 0');
 }
 
 void ShufflePlayerOutOfTeam (float source_team)
@@ -818,7 +1089,10 @@ void ShufflePlayerOutOfTeam (float source_team)
        TeamchangeFrags(selected);
        SetPlayerTeam(selected, smallestteam, source_team, false);
 
-       if(!IS_DEAD(selected))
-               Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE.m_id, selected.origin, '0 0 0');
+       if (IS_DEAD(selected) || MUTATOR_CALLHOOK(Player_ChangeTeamKill, selected) == true)
+       {
+               return;
+       }
+       Damage(selected, selected, selected, 100000, DEATH_AUTOTEAMCHANGE.m_id, selected.origin, '0 0 0');
        Send_Notification(NOTIF_ONE, selected, MSG_CENTER, CENTER_DEATH_SELF_AUTOTEAMCHANGE, selected.team);
 }
index 127ac7a6d30caffc887e9571c40104148813eadb..6d6ccbe2b48e20091cc0724804e62dd039b11cbd 100644 (file)
@@ -6,7 +6,18 @@ string cache_lastmutatormsg;
 // client counts for each team
 //float c1, c2, c3, c4;
 // # of bots on those teams
-float cb1, cb2, cb3, cb4;
+float numbotsteam1;
+float numbotsteam2;
+float numbotsteam3;
+float numbotsteam4;
+entity lowestplayerteam1;
+entity lowestplayerteam2;
+entity lowestplayerteam3;
+entity lowestplayerteam4;
+entity lowestbotteam1;
+entity lowestbotteam2;
+entity lowestbotteam3;
+entity lowestbotteam4;
 
 int redowned, blueowned, yellowowned, pinkowned;
 
@@ -26,9 +37,21 @@ string GetClientVersionMessage(entity this);
 
 string getwelcomemessage(entity this);
 
-void SetPlayerColors(entity pl, float _color);
+void SetPlayerColors(entity player, float _color);
 
-void SetPlayerTeam(entity pl, float t, float s, float noprint);
+/// \brief Sets the team of the player.
+/// \param[in,out] player Player to adjust.
+/// \param[in] teamnum Team number to set. See TEAM_NUM constants.
+/// \return True if team switch was successful, false otherwise.
+bool SetPlayerTeamSimple(entity player, int teamnum);
+
+/// \brief Sets the team of the player.
+/// \param[in,out] player Player to adjust.
+/// \param[in] destinationteam Team to set.
+/// \param[in] sourceteam Previous team of the player.
+/// \param[in] noprint Whether to print this event to players' console.
+/// \return No return.
+void SetPlayerTeam(entity player, int destinationteam, int sourceteam, bool noprint);
 
 // set c1...c4 to show what teams are allowed
 void CheckAllowedTeams (entity for_whom);
@@ -39,7 +62,33 @@ float PlayerValue(entity p);
 // teams that are allowed will now have their player counts stored in c1...c4
 void GetTeamCounts(entity ignore);
 
-float TeamSmallerEqThanTeam(float ta, float tb, entity e);
+/// \brief Returns whether one team is smaller than the other.
+/// \param[in] teama First team.
+/// \param[in] teamb Second team.
+/// \param[in] e Player to check.
+/// \param[in] usescore Whether to take into account team scores.
+/// \return True if first team is smaller than the second one, false otherwise.
+/// \note This function assumes that CheckAllowedTeams and GetTeamCounts have
+/// been called.
+bool IsTeamSmallerThanTeam(float teama, float teamb, entity e, bool usescore);
+
+/// \brief Returns whether one team is equal to the other.
+/// \param[in] teama First team.
+/// \param[in] teamb Second team.
+/// \param[in] e Player to check.
+/// \param[in] usescore Whether to take into account team scores.
+/// \return True if first team is equal to the second one, false otherwise.
+/// \note This function assumes that CheckAllowedTeams and GetTeamCounts have
+/// been called.
+bool IsTeamEqualToTeam(float teama, float teamb, entity e, bool usescore);
+
+/// \brief Returns the bitmask of the best teams for the player to join.
+/// \param[in] player Player to check.
+/// \param[in] usescore Whether to take into account team scores.
+/// \return Bitmask of the best teams for the player to join.
+/// \note This function assumes that CheckAllowedTeams and GetTeamCounts have
+/// been called.
+int FindBestTeams(entity player, bool usescore);
 
 // returns # of smallest team (1, 2, 3, 4)
 // NOTE: Assumes CheckAllowedTeams has already been called!
@@ -47,7 +96,13 @@ float FindSmallestTeam(entity pl, float ignore_pl);
 
 int JoinBestTeam(entity this, bool only_return_best, bool forcebestteam);
 
-//void() ctf_playerchanged;
+/// \brief Auto balances bots in teams after the player has changed team.
+/// \param[in] sourceteam Previous team of the player (1, 2, 3, 4).
+/// \param[in] destinationteam Current team of the player (1, 2, 3, 4).
+/// \return No return.
+/// \note This function assumes that CheckAllowedTeams and GetTeamCounts have
+/// been called.
+void AutoBalanceBots(int sourceteam, int destinationteam);
 
 void ShufflePlayerOutOfTeam (float source_team);