- //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))
- {
- // 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))
- {
- destination_team_index = 2;
- }
- if (destination_team_index == 2 && !TeamBalance_IsTeamAllowedInternal(
- balance, 2))
- {
- destination_team_index = 1;
- }
-
- // not changing teams
- if (source_color == destination_color)
- {
- SetPlayerTeam(this, destination_team_index, TEAM_CHANGE_MANUAL);
- TeamBalance_Destroy(balance);
- 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)