]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/teamplay.qc
Fix FL_WEAPON flag overlapping FL_JUMPRELEASED. This unintentional change was introdu...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / teamplay.qc
index 357685887e14dc236e9a6e46dd6bbd1e39fa1ea7..1cfdbf4a0538da331630210cdfbc92a4ef4333ec 100644 (file)
@@ -1,19 +1,19 @@
 #include "teamplay.qh"
 
-#include "client.qh"
-#include "race.qh"
-#include "scores.qh"
-#include "scores_rules.qh"
-
-#include "bot/api.qh"
-
-#include "command/vote.qh"
-
-#include <server/mutators/_mod.qh>
-
-#include "../common/deathtypes/all.qh"
+#include <common/deathtypes/all.qh>
 #include <common/gamemodes/_mod.qh>
-#include "../common/teams.qh"
+#include <common/teams.qh>
+#include <server/bot/api.qh>
+#include <server/bot/default/cvars.qh>
+#include <server/campaign.qh>
+#include <server/client.qh>
+#include <server/command/vote.qh>
+#include <server/damage.qh>
+#include <server/gamelog.qh>
+#include <server/mutators/_mod.qh>
+#include <server/race.qh>
+#include <server/scores.qh>
+#include <server/scores_rules.qh>
 
 /// \brief Describes a state of team balance entity.
 enum
@@ -28,8 +28,6 @@ enum
 /// \brief Indicates that the player is not allowed to join a team.
 const int TEAM_NOT_ALLOWED = -1;
 
-.float team_forced; // can be a team number to force a team, or 0 for default action, or -1 for forced spectator
-
 .int m_team_balance_state; ///< Holds the state of the team balance entity.
 .entity m_team_balance_team[NUM_TEAMS]; ///< ???
 
@@ -37,7 +35,7 @@ const int TEAM_NOT_ALLOWED = -1;
 .int m_num_players; ///< Number of players (both humans and bots) in a team.
 .int m_num_bots; ///< Number of bots in a team.
 .int m_num_players_alive; ///< Number of alive players in a team.
-.int m_num_control_points; ///< Number of control points owned by a team.
+.int m_num_owned_items; ///< Number of items owned by a team.
 
 string autocvar_g_forced_team_red;
 string autocvar_g_forced_team_blue;
@@ -46,11 +44,13 @@ string autocvar_g_forced_team_pink;
 
 entity g_team_entities[NUM_TEAMS]; ///< Holds global team entities.
 
-STATIC_INIT(g_team_entities)
+void Team_InitTeams()
 {
+       if (g_team_entities[0])
+               return;
        for (int i = 0; i < NUM_TEAMS; ++i)
        {
-               g_team_entities[i] = spawn();
+               g_team_entities[i] = new_pure(team_entity);
        }
 }
 
@@ -58,7 +58,7 @@ entity Team_GetTeamFromIndex(int index)
 {
        if (!Team_IsValidIndex(index))
        {
-               LOG_FATALF("Team_GetTeamFromIndex: Index is invalid: %f", index);
+               LOG_FATALF("Index is invalid: %f", index);
        }
        return g_team_entities[index - 1];
 }
@@ -67,7 +67,7 @@ entity Team_GetTeam(int team_num)
 {
        if (!Team_IsValidTeam(team_num))
        {
-               LOG_FATALF("Team_GetTeam: Value is invalid: %f", team_num);
+               LOG_FATALF("Value is invalid: %f", team_num);
        }
        return g_team_entities[Team_TeamToIndex(team_num) - 1];
 }
@@ -92,6 +92,21 @@ void Team_SetNumberOfAlivePlayers(entity team_ent, int number)
        team_ent.m_num_players_alive = number;
 }
 
+int Team_GetWinnerAliveTeam()
+{
+       int winner = 0;
+       for (int i = 0; i < NUM_TEAMS; ++i)
+       {
+               if (g_team_entities[i].m_num_players_alive > 0)
+               {
+                       if (winner)
+                               return 0;
+                       winner = Team_IndexToTeam(i + 1);
+               }
+       }
+       return (winner ? winner : -1);
+}
+
 int Team_GetNumberOfAliveTeams()
 {
        int result = 0;
@@ -105,22 +120,37 @@ int Team_GetNumberOfAliveTeams()
        return result;
 }
 
-int Team_GetNumberOfControlPoints(entity team_ent)
+int Team_GetWinnerTeam_WithOwnedItems(int min_control_points)
+{
+       int winner = 0;
+       for (int i = 0; i < NUM_TEAMS; ++i)
+       {
+               if (g_team_entities[i].m_num_owned_items >= min_control_points)
+               {
+                       if (winner)
+                               return 0;
+                       winner = Team_IndexToTeam(i + 1);
+               }
+       }
+       return (winner ? winner : -1);
+}
+
+int Team_GetNumberOfOwnedItems(entity team_ent)
 {
-       return team_ent.m_num_control_points;
+       return team_ent.m_num_owned_items;
 }
 
-void Team_SetNumberOfControlPoints(entity team_ent, int number)
+void Team_SetNumberOfOwnedItems(entity team_ent, int number)
 {
-       team_ent.m_num_control_points = number;
+       team_ent.m_num_owned_items = number;
 }
 
-int Team_GetNumberOfTeamsWithControlPoints()
+int Team_GetNumberOfTeamsWithOwnedItems()
 {
        int result = 0;
        for (int i = 0; i < NUM_TEAMS; ++i)
        {
-               if (g_team_entities[i].m_num_control_points > 0)
+               if (g_team_entities[i].m_num_owned_items > 0)
                {
                        ++result;
                }
@@ -130,7 +160,7 @@ int Team_GetNumberOfTeamsWithControlPoints()
 
 void setcolor(entity this, int clr)
 {
-#if 0
+#if 1
        this.clientcolors = clr;
        this.team = (clr & 15) + 1;
 #else
@@ -181,7 +211,7 @@ bool Player_SetTeamIndex(entity player, int index)
                {
                        // 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.
+                       // [BOT]Lion: color 0 4.
                        SetPlayerColors(player, new_team - 1);
                }
                return true;
@@ -207,58 +237,104 @@ bool Player_SetTeamIndex(entity player, int index)
 bool SetPlayerTeam(entity player, int team_index, int type)
 {
        int old_team_index = Entity_GetTeamIndex(player);
+
        if (!Player_SetTeamIndex(player, team_index))
-       {
                return false;
-       }
-       LogTeamchange(player.playerid, player.team, type);
+
+       LogTeamChange(player.playerid, player.team, type);
+
        if (team_index != old_team_index)
        {
-               Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(player.team,
-                       INFO_JOIN_PLAY_TEAM), player.netname);
                KillPlayerForTeamChange(player);
+               PlayerScore_Clear(player); // works only in game modes without teams
+
+               if (!IS_BOT_CLIENT(player))
+                       TeamBalance_AutoBalanceBots();
+
+               if (team_index != -1)
+                       Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(player.team, INFO_JOIN_PLAY_TEAM), player.netname);
        }
-       return true;
-}
 
-bool MoveToTeam(entity client, int team_index, int type)
-{
-       //PrintToChatAll(sprintf("MoveToTeam: %s, %f", client.netname, team_index));
-       int lockteams_backup = lockteams;  // backup any team lock
-       lockteams = 0;  // disable locked teams
-       PlayerScore_Clear(client);
-       if (!SetPlayerTeam(client, team_index, type))
+       if (team_index == -1)
        {
-               lockteams = lockteams_backup;  // restore the team lock
-               return false;
+               if (autocvar_sv_maxidle_playertospectator > 0 && CS(player).idlekick_lasttimeleft)
+               {
+                       // this done here so it happens even when manually speccing during the countdown
+                       Kill_Notification(NOTIF_ONE_ONLY, player, MSG_CENTER, CPID_IDLING);
+                       CS(player).idlekick_lasttimeleft = 0;
+               }
+               else if (!CS(player).just_joined && player.frags != FRAGS_SPECTATOR)
+               {
+                       Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE, player.netname);
+               }
        }
-       lockteams = lockteams_backup;  // restore the team lock
+
        return true;
 }
 
-void KillPlayerForTeamChange(entity player)
+void Player_SetTeamIndexChecked(entity player, int team_index)
 {
-       if (IS_DEAD(player))
+       if (!teamplay)
        {
                return;
        }
-       if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
+       if (!Team_IsValidIndex(team_index))
        {
                return;
        }
-       Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
-               player.origin, '0 0 0');
+       if ((autocvar_g_campaign) || (autocvar_g_changeteam_banned &&
+               CS(player).wasplayer))
+       {
+               Send_Notification(NOTIF_ONE, player, MSG_INFO,
+                       INFO_TEAMCHANGE_NOTALLOWED);
+               return;
+       }
+       entity balance = TeamBalance_CheckAllowedTeams(player);
+       if (team_index == 1 && !TeamBalance_IsTeamAllowedInternal(balance, 1))
+       {
+               team_index = 4;
+       }
+       if (team_index == 4 && !TeamBalance_IsTeamAllowedInternal(balance, 4))
+       {
+               team_index = 3;
+       }
+       if (team_index == 3 && !TeamBalance_IsTeamAllowedInternal(balance, 3))
+       {
+               team_index = 2;
+       }
+       if (team_index == 2 && !TeamBalance_IsTeamAllowedInternal(balance, 2))
+       {
+               team_index = 1;
+       }
+       // 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)
+       {
+               TeamBalance_GetTeamCounts(balance, player);
+               if ((Team_IndexToBit(team_index) & TeamBalance_FindBestTeams(balance,
+                       player, false)) == 0)
+               {
+                       Send_Notification(NOTIF_ONE, player, MSG_INFO,
+                               INFO_TEAMCHANGE_LARGERTEAM);
+                       TeamBalance_Destroy(balance);
+                       return;
+               }
+       }
+       TeamBalance_Destroy(balance);
+       SetPlayerTeam(player, team_index, TEAM_CHANGE_MANUAL);
 }
 
-void LogTeamchange(float player_id, float team_number, int type)
+bool MoveToTeam(entity client, int team_index, int type)
 {
-       if(!autocvar_sv_eventlog)
-               return;
-
-       if(player_id < 1)
-               return;
-
-       GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
+       //PrintToChatAll(sprintf("MoveToTeam: %s, %f", client.netname, team_index));
+       int lockteams_backup = lockteams;  // backup any team lock
+       lockteams = 0;  // disable locked teams
+       if (!SetPlayerTeam(client, team_index, type))
+       {
+               lockteams = lockteams_backup;  // restore the team lock
+               return false;
+       }
+       lockteams = lockteams_backup;  // restore the team lock
+       return true;
 }
 
 bool Player_HasRealForcedTeam(entity player)
@@ -285,7 +361,7 @@ void Player_SetForcedTeamIndex(entity player, int team_index)
                {
                        if (!Team_IsValidIndex(team_index))
                        {
-                               LOG_FATAL("Player_SetForcedTeamIndex: Invalid team index.");
+                               LOG_FATAL("Invalid team index.");
                        }
                        else
                        {
@@ -371,6 +447,42 @@ void Player_DetermineForcedTeam(entity player)
        }
 }
 
+void TeamBalance_JoinBestTeam(entity player)
+{
+       //PrintToChatAll(sprintf("TeamBalance_JoinBestTeam: %s", player.netname));
+       if (!teamplay)
+       {
+               return;
+       }
+       if (player.bot_forced_team)
+       {
+               return;
+       }
+       entity balance = TeamBalance_CheckAllowedTeams(player);
+       if (Player_HasRealForcedTeam(player))
+       {
+               int forced_team_index = player.team_forced;
+               bool is_team_allowed = TeamBalance_IsTeamAllowedInternal(balance,
+                       forced_team_index);
+               TeamBalance_Destroy(balance);
+               if (!is_team_allowed)
+               {
+                       return;
+               }
+               if (!SetPlayerTeam(player, forced_team_index, TEAM_CHANGE_AUTO))
+               {
+                       return;
+               }
+               return;
+       }
+       int best_team_index = TeamBalance_FindBestTeam(balance, player, true);
+       TeamBalance_Destroy(balance);
+       if (!SetPlayerTeam(player, best_team_index, TEAM_CHANGE_AUTO))
+       {
+               return;
+       }
+}
+
 entity TeamBalance_CheckAllowedTeams(entity for_whom)
 {
        entity balance = spawn();
@@ -382,8 +494,9 @@ entity TeamBalance_CheckAllowedTeams(entity for_whom)
                team_ent.m_num_bots = 0;
        }
        setthink(balance, TeamBalance_Destroy);
-       
-       int teams_mask = 0;     
+       balance.nextthink = time;
+
+       int teams_mask = 0;
        string teament_name = string_null;
        bool mutator_returnvalue = MUTATOR_CALLHOOK(TeamBalance_CheckAllowedTeams,
                teams_mask, teament_name, for_whom);
@@ -414,8 +527,7 @@ entity TeamBalance_CheckAllowedTeams(entity for_whom)
        }
 
        // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
-       if (AvailableTeams() == 2)
-       if (autocvar_bot_vs_human && for_whom)
+       if (autocvar_bot_vs_human && AVAILABLE_TEAMS == 2 && for_whom)
        {
                if (autocvar_bot_vs_human > 0)
                {
@@ -504,8 +616,8 @@ entity TeamBalance_CheckAllowedTeams(entity for_whom)
                        TeamBalance_IsTeamAllowedInternal(balance, i))
                {
                        TeamBalance_BanTeamsExcept(balance, i);
+                       break;
                }
-               break;
        }
        balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
        return balance;
@@ -528,12 +640,11 @@ int TeamBalance_GetAllowedTeams(entity balance)
 {
        if (balance == NULL)
        {
-               LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
+               LOG_FATAL("Team balance entity is NULL.");
        }
        if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
        {
-               LOG_FATAL("TeamBalance_GetAllowedTeams: "
-                       "Team balance entity is not initialized.");
+               LOG_FATAL("Team balance entity is not initialized.");
        }
        int result = 0;
        for (int i = 1; i <= NUM_TEAMS; ++i)
@@ -550,16 +661,15 @@ bool TeamBalance_IsTeamAllowed(entity balance, int index)
 {
        if (balance == NULL)
        {
-               LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
+               LOG_FATAL("Team balance entity is NULL.");
        }
        if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
        {
-               LOG_FATAL("TeamBalance_IsTeamAllowed: "
-                       "Team balance entity is not initialized.");
+               LOG_FATAL("Team balance entity is not initialized.");
        }
        if (!Team_IsValidIndex(index))
        {
-               LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
+               LOG_FATALF("Team index is invalid: %f",
                        index);
        }
        return TeamBalance_IsTeamAllowedInternal(balance, index);
@@ -569,12 +679,11 @@ void TeamBalance_GetTeamCounts(entity balance, entity ignore)
 {
        if (balance == NULL)
        {
-               LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
+               LOG_FATAL("Team balance entity is NULL.");
        }
        if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
        {
-               LOG_FATAL("TeamBalance_GetTeamCounts: "
-                       "Team balance entity is not initialized.");
+               LOG_FATAL("Team balance entity is not initialized.");
        }
        if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
        {
@@ -601,7 +710,7 @@ void TeamBalance_GetTeamCounts(entity balance, entity ignore)
                        }
                        int team_num;
                        // TODO: Reconsider when the player is truly on the team.
-                       if (IS_CLIENT(it) || (it.caplayer))
+                       if (IS_CLIENT(it) || INGAME(it))
                        {
                                team_num = it.team;
                        }
@@ -651,18 +760,15 @@ int TeamBalance_GetNumberOfPlayers(entity balance, int index)
 {
        if (balance == NULL)
        {
-               LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
-                       "Team balance entity is NULL.");
+               LOG_FATAL("Team balance entity is NULL.");
        }
        if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
        {
-               LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
-                       "TeamBalance_GetTeamCounts has not been called.");
+               LOG_FATAL("TeamBalance_GetTeamCounts has not been called.");
        }
        if (!Team_IsValidIndex(index))
        {
-               LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
-                       index);
+               LOG_FATALF("Team index is invalid: %f", index);
        }
        return balance.m_team_balance_team[index - 1].m_num_players;
 }
@@ -671,12 +777,11 @@ int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
 {
        if (balance == NULL)
        {
-               LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
+               LOG_FATAL("Team balance entity is NULL.");
        }
        if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
        {
-               LOG_FATAL("TeamBalance_FindBestTeam: "
-                       "Team balance entity is not initialized.");
+               LOG_FATAL("Team balance entity is not initialized.");
        }
        // count how many players are in each team
        if (ignore_player)
@@ -690,8 +795,7 @@ int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
        int team_bits = TeamBalance_FindBestTeams(balance, player, true);
        if (team_bits == 0)
        {
-               LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
-                       MapInfo_Type_ToString(MapInfo_CurrentGametype()));
+               LOG_FATALF("No teams available for %s\n", GetGametype());
        }
        RandomSelection_Init();
        for (int i = 1; i <= NUM_TEAMS; ++i)
@@ -708,12 +812,11 @@ int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
 {
        if (balance == NULL)
        {
-               LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
+               LOG_FATAL("Team balance entity is NULL.");
        }
        if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
        {
-               LOG_FATAL("TeamBalance_FindBestTeams: "
-                       "TeamBalance_GetTeamCounts has not been called.");
+               LOG_FATAL("TeamBalance_GetTeamCounts has not been called.");
        }
        if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
        {
@@ -750,71 +853,25 @@ int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
        return team_bits;
 }
 
-void TeamBalance_JoinBestTeam(entity this)
-{
-       //PrintToChatAll(sprintf("JoinBestTeam: %s", this.netname));
-       if (!teamplay)
-       {
-               return;
-       }
-       if (this.bot_forced_team)
-       {
-               return;
-       }
-       entity balance = TeamBalance_CheckAllowedTeams(this);
-       if (Player_HasRealForcedTeam(this))
-       {
-               int forced_team_index = this.team_forced;
-               bool is_team_allowed = TeamBalance_IsTeamAllowedInternal(balance,
-                       forced_team_index);
-               TeamBalance_Destroy(balance);
-               if (!is_team_allowed)
-               {
-                       return;
-               }
-               if (!SetPlayerTeam(this, forced_team_index, TEAM_CHANGE_AUTO))
-               {
-                       return;
-               }
-               if (!IS_BOT_CLIENT(this))
-               {
-                       TeamBalance_AutoBalanceBots();
-               }
-               return;
-       }
-       int best_team_index = TeamBalance_FindBestTeam(balance, this, true);
-       TeamBalance_Destroy(balance);
-       PlayerScore_Clear(this);
-       if (!SetPlayerTeam(this, best_team_index, TEAM_CHANGE_AUTO))
-       {
-               return;
-       }
-       if (!IS_BOT_CLIENT(this))
-       {
-               TeamBalance_AutoBalanceBots();
-       }
-}
-
 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
        entity player, bool use_score)
 {
        if (balance == NULL)
        {
-               LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
+               LOG_FATAL("Team balance entity is NULL.");
        }
        if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
        {
-               LOG_FATAL("TeamBalance_CompareTeams: "
-                       "TeamBalance_GetTeamCounts has not been called.");
+               LOG_FATAL("TeamBalance_GetTeamCounts has not been called.");
        }
        if (!Team_IsValidIndex(team_index_a))
        {
-               LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
+               LOG_FATALF("team_index_a is invalid: %f",
                        team_index_a);
        }
        if (!Team_IsValidIndex(team_index_b))
        {
-               LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
+               LOG_FATALF("team_index_b is invalid: %f",
                        team_index_b);
        }
        if (team_index_a == team_index_b)
@@ -828,18 +885,14 @@ int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
 
 void TeamBalance_AutoBalanceBots()
 {
-       if (!autocvar_g_balance_teams ||
-               !autocvar_g_balance_teams_prevent_imbalance)
-       {
-               return;
-       }
-       //PrintToChatAll("TeamBalance_AutoBalanceBots");
+       // checks disabled because we always want auto-balanced bots
+       //if (!(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance))
+       //      return;
+
        entity balance = TeamBalance_CheckAllowedTeams(NULL);
        TeamBalance_GetTeamCounts(balance, NULL);
        int smallest_team_index = 0;
        int smallest_team_player_count = 0;
-       int largest_team_index = 0;
-       int largest_team_player_count = 0;
        for (int i = 1; i <= NUM_TEAMS; ++i)
        {
                entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
@@ -847,90 +900,153 @@ void TeamBalance_AutoBalanceBots()
                {
                        continue;
                }
-               int player_count = TeamBalanceTeam_GetNumberOfPlayers(team_);
+               int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
                if (smallest_team_index == 0)
                {
                        smallest_team_index = i;
-                       smallest_team_player_count = player_count;
+                       smallest_team_player_count = playercount;
                }
-               else if (player_count < smallest_team_player_count)
+               else if (playercount < smallest_team_player_count)
                {
                        smallest_team_index = i;
-                       smallest_team_player_count = player_count;
+                       smallest_team_player_count = playercount;
                }
-               if (largest_team_index == 0)
+       }
+       //PrintToChatAll(sprintf("Smallest team: %f", smallest_team_index));
+       //PrintToChatAll(sprintf("Smallest team players: %f", smallest_team_player_count));
+       entity switchable_bot = NULL;
+       int teams = BITS(NUM_TEAMS);
+       while (teams != 0)
+       {
+               int largest_team_index = TeamBalance_GetLargestTeamIndex(balance,
+                       teams);
+               if (smallest_team_index == largest_team_index)
                {
-                       largest_team_index = i;
-                       largest_team_player_count = player_count;
+                       TeamBalance_Destroy(balance);
+                       return;
                }
-               else if (player_count > largest_team_player_count)
+               entity largest_team = TeamBalance_GetTeamFromIndex(balance,
+                       largest_team_index);
+               int largest_team_player_count = TeamBalanceTeam_GetNumberOfPlayers(
+                       largest_team);
+               if (largest_team_player_count - smallest_team_player_count < 2)
                {
-                       largest_team_index = i;
-                       largest_team_player_count = player_count;
+                       TeamBalance_Destroy(balance);
+                       return;
+               }
+               //PrintToChatAll(sprintf("Largest team: %f", largest_team_index));
+               //PrintToChatAll(sprintf("Largest team players: %f", largest_team_player_count));
+               switchable_bot = TeamBalance_GetPlayerForTeamSwitch(largest_team_index,
+                       smallest_team_index, true);
+               if (switchable_bot != NULL)
+               {
+                       break;
                }
+               teams &= ~Team_IndexToBit(largest_team_index);
        }
-       //PrintToChatAll(sprintf("Smallest team: %f", smallest_team_index));
-       //PrintToChatAll(sprintf("Largest team: %f", largest_team_index));
-       //PrintToChatAll(sprintf("Smallest team players: %f", smallest_team_player_count));
-       //PrintToChatAll(sprintf("Largest team players: %f", largest_team_player_count));
-       if (smallest_team_index == largest_team_index)
+       TeamBalance_Destroy(balance);
+       if (switchable_bot == NULL)
        {
+               //PrintToChatAll("No bot found after searching through all the teams");
                return;
        }
-       if (largest_team_player_count - smallest_team_player_count < 2)
+       SetPlayerTeam(switchable_bot, smallest_team_index, TEAM_CHANGE_AUTO);
+}
+
+int TeamBalance_GetLargestTeamIndex(entity balance, int teams)
+{
+       int largest_team_index = 0;
+       int largest_team_player_count = 0;
+       for (int i = 1; i <= NUM_TEAMS; ++i)
        {
-               return;
+               if (!(Team_IndexToBit(i) & teams))
+               {
+                       continue;
+               }
+               entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
+               if (!TeamBalanceTeam_IsAllowed(team_))
+               {
+                       continue;
+               }
+               int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
+               if (largest_team_index == 0)
+               {
+                       largest_team_index = i;
+                       largest_team_player_count = playercount;
+               }
+               else if (playercount > largest_team_player_count)
+               {
+                       largest_team_index = i;
+                       largest_team_player_count = playercount;
+               }
        }
-       entity source_team = TeamBalance_GetTeamFromIndex(balance,
-               largest_team_index);
-       if (source_team.m_num_bots == 0)
+       return largest_team_index;
+}
+
+entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index,
+       int destination_team_index, bool is_bot)
+{
+       if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, source_team_index,
+               destination_team_index, is_bot))
        {
-               TeamBalance_Destroy(balance);
-               return;
+               return M_ARGV(3, entity);
        }
-       TeamBalance_Destroy(balance);
-       entity lowest_bot = NULL;
-       if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, largest_team_index,
-               smallest_team_index, true))
+       entity lowest_player = NULL;
+       float lowest_score = FLOAT_MAX;
+       FOREACH_CLIENT(Entity_GetTeamIndex(it) == source_team_index,
        {
-               lowest_bot = M_ARGV(3, entity);
+               if (IS_BOT_CLIENT(it) != is_bot)
+               {
+                       continue;
+               }
+               float temp_score = PlayerScore_Get(it, SP_SCORE);
+               if (temp_score >= lowest_score)
+               {
+                       continue;
+               }
+               //PrintToChatAll(sprintf(
+               //      "Found %s with lowest score, checking allowed teams", it.netname));
+               entity balance = TeamBalance_CheckAllowedTeams(it);
+               if (TeamBalance_IsTeamAllowed(balance, source_team_index))
+               {
+                       //PrintToChatAll("Allowed");
+                       lowest_player = it;
+                       lowest_score = temp_score;
+               }
+               else
+               {
+                       //PrintToChatAll("Not allowed");
+               }
+               TeamBalance_Destroy(balance);
+       });
+       return lowest_player;
+}
+
+void LogTeamChange(float player_id, float team_number, int type)
+{
+       if (!autocvar_sv_eventlog)
+       {
+               return;
        }
-       else
+       if (player_id < 1)
        {
-               float lowest_score = FLOAT_MAX;
-               FOREACH_CLIENT(IS_BOT_CLIENT(it) && (Entity_GetTeamIndex(it) ==
-                       largest_team_index),
-               {
-                       float temp_score = PlayerScore_Get(it, SP_SCORE);
-                       if (temp_score >= lowest_score)
-                       {
-                               continue;
-                       }
-                       //PrintToChatAll(sprintf("Found %s with lowest score, checking allowed teams", it.netname));
-                       balance = TeamBalance_CheckAllowedTeams(it);
-                       if (TeamBalance_IsTeamAllowed(balance, smallest_team_index))
-                       {
-                               //PrintToChatAll("Allowed");
-                               lowest_bot = it;
-                               lowest_score = temp_score;
-                       }
-                       else
-                       {
-                               //PrintToChatAll("Not allowed");
-                       }
-                       TeamBalance_Destroy(balance);
-               });
+               return;
        }
-       if (lowest_bot == NULL)
+       GameLogEcho(sprintf(":team:%d:%d:%d", player_id, team_number, type));
+}
+
+void KillPlayerForTeamChange(entity player)
+{
+       if (IS_DEAD(player))
        {
-               //PrintToChatAll("No bot found");
                return;
        }
-       if (!Player_SetTeamIndex(lowest_bot, smallest_team_index))
+       if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
        {
                return;
        }
-       KillPlayerForTeamChange(lowest_bot);
+       Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
+               player.origin, '0 0 0');
 }
 
 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
@@ -954,7 +1070,7 @@ entity TeamBalance_GetTeamFromIndex(entity balance, int index)
 {
        if (!Team_IsValidIndex(index))
        {
-               LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
+               LOG_FATALF("Index is invalid: %f", index);
        }
        return balance.m_team_balance_team[index - 1];
 }
@@ -1021,99 +1137,19 @@ int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
        return TEAMS_COMPARE_EQUAL;
 }
 
-// Called when the player connects or when they change their color with "color"
-// command.
-void SV_ChangeTeam(entity this, float _color)
+void SV_ChangeTeam(entity player, int new_color)
 {
-       //PrintToChatAll(sprintf("SV_ChangeTeam: %s, %f", this.netname, _color));
-
-       // in normal deathmatch we can just apply the color and we're done
-       if(!teamplay)
-               SetPlayerColors(this, _color);
-
-       if(!IS_CLIENT(this))
-       {
-               //PrintToChatAll("Not a client yet");
-               // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
-               Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING, this.netname);
-               return;
-       }
-
-       if(!teamplay)
-               return;
-
-       int source_color, destination_color;
-       int source_team_index, destination_team_index;
-
-       source_color = this.clientcolors & 0x0F;
-       destination_color = _color & 0x0F;
-
-       source_team_index = Team_TeamToIndex(source_color + 1);
-       destination_team_index = Team_TeamToIndex(destination_color + 1);
-
-       if (destination_team_index == -1)
-       {
-               return;
-       }
-
-       entity balance = TeamBalance_CheckAllowedTeams(this);
-
-       if (destination_team_index == 1 && !TeamBalance_IsTeamAllowedInternal(
-               balance, 1))
-       {
-               destination_team_index = 4;
-       }
-       if (destination_team_index == 4 && !TeamBalance_IsTeamAllowedInternal(
-               balance, 4))
-       {
-               destination_team_index = 3;
-       }
-       if (destination_team_index == 3 && !TeamBalance_IsTeamAllowedInternal(
-               balance, 3))
+       if (!teamplay)
        {
-               destination_team_index = 2;
+               SetPlayerColors(player, new_color);
        }
-       if (destination_team_index == 2 && !TeamBalance_IsTeamAllowedInternal(
-               balance, 2))
+       if(!IS_CLIENT(player))
        {
-               destination_team_index = 1;
-       }
-
-       // not changing teams
-       if (source_color == destination_color)
-       {
-               SetPlayerTeam(this, destination_team_index, TEAM_CHANGE_MANUAL);
-               TeamBalance_Destroy(balance);
-               TeamBalance_AutoBalanceBots(); // Hack
                return;
        }
-
-       if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && CS(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)
-       {
-               TeamBalance_GetTeamCounts(balance, this);
-               if ((Team_IndexToBit(destination_team_index) &
-                       TeamBalance_FindBestTeams(balance, this, false)) == 0)
-               {
-                       Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
-                       TeamBalance_Destroy(balance);
-                       return;
-               }
-       }
-       TeamBalance_Destroy(balance);
-       if (IS_PLAYER(this) && source_team_index != destination_team_index)
-       {
-               // reduce frags during a team change
-               PlayerScore_Clear(this);
-       }
-       if (!SetPlayerTeam(this, destination_team_index, TEAM_CHANGE_MANUAL))
+       if (!teamplay)
        {
                return;
        }
-       TeamBalance_AutoBalanceBots();
+       Player_SetTeamIndexChecked(player, Team_TeamToIndex((new_color & 0x0F) + 1));
 }