6 #include "scores_rules.qh"
10 #include "command/vote.qh"
12 #include <server/mutators/_mod.qh>
14 #include "../common/deathtypes/all.qh"
15 #include <common/gamemodes/_mod.qh>
16 #include "../common/teams.qh"
18 /// \brief Describes a state of team balance entity.
21 TEAM_BALANCE_UNINITIALIZED, ///< The team balance has not been initialized.
22 /// \brief TeamBalance_CheckAllowedTeams has been called.
23 TEAM_BALANCE_TEAMS_CHECKED,
24 /// \brief TeamBalance_GetTeamCounts has been called.
25 TEAM_BALANCE_TEAM_COUNTS_FILLED
28 /// \brief Indicates that the player is not allowed to join a team.
29 const int TEAM_NOT_ALLOWED = -1;
31 .float team_forced; // can be a team number to force a team, or 0 for default action, or -1 for forced spectator
33 .int m_team_balance_state; ///< Holds the state of the team balance entity.
34 .entity m_team_balance_team[NUM_TEAMS]; ///< ???
36 .float m_team_score; ///< The score of the team.
37 .int m_num_players; ///< Number of players (both humans and bots) in a team.
38 .int m_num_bots; ///< Number of bots in a team.
39 .int m_num_players_alive; ///< Number of alive players in a team.
40 .int m_num_control_points; ///< Number of control points owned by a team.
42 string autocvar_g_forced_team_red;
43 string autocvar_g_forced_team_blue;
44 string autocvar_g_forced_team_yellow;
45 string autocvar_g_forced_team_pink;
47 entity g_team_entities[NUM_TEAMS]; ///< Holds global team entities.
49 STATIC_INIT(g_team_entities)
51 for (int i = 0; i < NUM_TEAMS; ++i)
53 g_team_entities[i] = spawn();
57 entity Team_GetTeamFromIndex(int index)
59 if (!Team_IsValidIndex(index))
61 LOG_FATALF("Team_GetTeamFromIndex: Index is invalid: %f", index);
63 return g_team_entities[index - 1];
66 entity Team_GetTeam(int team_num)
68 if (!Team_IsValidTeam(team_num))
70 LOG_FATALF("Team_GetTeam: Value is invalid: %f", team_num);
72 return g_team_entities[Team_TeamToIndex(team_num) - 1];
75 float Team_GetTeamScore(entity team_ent)
77 return team_ent.m_team_score;
80 void Team_SetTeamScore(entity team_ent, float score)
82 team_ent.m_team_score = score;
85 int Team_GetNumberOfAlivePlayers(entity team_ent)
87 return team_ent.m_num_players_alive;
90 void Team_SetNumberOfAlivePlayers(entity team_ent, int number)
92 team_ent.m_num_players_alive = number;
95 int Team_GetNumberOfAliveTeams()
98 for (int i = 0; i < NUM_TEAMS; ++i)
100 if (g_team_entities[i].m_num_players_alive > 0)
108 int Team_GetNumberOfControlPoints(entity team_ent)
110 return team_ent.m_num_control_points;
113 void Team_SetNumberOfControlPoints(entity team_ent, int number)
115 team_ent.m_num_control_points = number;
118 int Team_GetNumberOfTeamsWithControlPoints()
121 for (int i = 0; i < NUM_TEAMS; ++i)
123 if (g_team_entities[i].m_num_control_points > 0)
131 void setcolor(entity this, int clr)
134 this.clientcolors = clr;
135 this.team = (clr & 15) + 1;
137 builtin_setcolor(this, clr);
141 bool Entity_HasValidTeam(entity this)
143 return Team_IsValidTeam(this.team);
146 int Entity_GetTeamIndex(entity this)
148 return Team_TeamToIndex(this.team);
151 entity Entity_GetTeam(entity this)
153 int index = Entity_GetTeamIndex(this);
154 if (!Team_IsValidIndex(index))
158 return Team_GetTeamFromIndex(index);
161 void SetPlayerColors(entity player, float _color)
163 float pants = _color & 0x0F;
164 float shirt = _color & 0xF0;
167 setcolor(player, 16 * pants + pants);
171 setcolor(player, shirt + pants);
175 bool Player_SetTeamIndex(entity player, int index)
177 int new_team = Team_IndexToTeam(index);
178 if (player.team == new_team)
182 // This is important when players join the game and one of their
183 // color matches the team color while other doesn't. For example
185 SetPlayerColors(player, new_team - 1);
189 int old_index = Team_TeamToIndex(player.team);
190 if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, old_index, index) == true)
192 // Mutator has blocked team change.
201 SetPlayerColors(player, new_team - 1);
203 MUTATOR_CALLHOOK(Player_ChangedTeam, player, old_index, index);
207 bool SetPlayerTeam(entity player, int team_index, int type)
209 int old_team_index = Entity_GetTeamIndex(player);
210 if (!Player_SetTeamIndex(player, team_index))
214 LogTeamchange(player.playerid, player.team, type);
215 if (team_index != old_team_index)
217 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(player.team,
218 INFO_JOIN_PLAY_TEAM), player.netname);
219 KillPlayerForTeamChange(player);
224 bool MoveToTeam(entity client, int team_index, int type)
226 //PrintToChatAll(sprintf("MoveToTeam: %s, %f", client.netname, team_index));
227 int lockteams_backup = lockteams; // backup any team lock
228 lockteams = 0; // disable locked teams
229 PlayerScore_Clear(client);
230 if (!SetPlayerTeam(client, team_index, type))
232 lockteams = lockteams_backup; // restore the team lock
235 lockteams = lockteams_backup; // restore the team lock
239 void KillPlayerForTeamChange(entity player)
245 if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
249 Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
250 player.origin, '0 0 0');
253 void LogTeamchange(float player_id, float team_number, int type)
255 if(!autocvar_sv_eventlog)
261 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
264 bool Player_HasRealForcedTeam(entity player)
266 return player.team_forced > TEAM_FORCE_DEFAULT;
269 int Player_GetForcedTeamIndex(entity player)
271 return player.team_forced;
274 void Player_SetForcedTeamIndex(entity player, int team_index)
276 player.team_forced = team_index;
279 void Player_DetermineForcedTeam(entity player)
281 if (autocvar_g_campaign)
283 if (IS_REAL_CLIENT(player)) // only players, not bots
285 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
287 player.team_forced = autocvar_g_campaign_forceteam;
291 player.team_forced = TEAM_FORCE_DEFAULT;
295 else if (PlayerInList(player, autocvar_g_forced_team_red))
297 player.team_forced = 1;
299 else if (PlayerInList(player, autocvar_g_forced_team_blue))
301 player.team_forced = 2;
303 else if (PlayerInList(player, autocvar_g_forced_team_yellow))
305 player.team_forced = 3;
307 else if (PlayerInList(player, autocvar_g_forced_team_pink))
309 player.team_forced = 4;
313 switch (autocvar_g_forced_team_otherwise)
317 player.team_forced = 1;
322 player.team_forced = 2;
327 player.team_forced = 3;
332 player.team_forced = 4;
338 player.team_forced = TEAM_FORCE_SPECTATOR;
343 player.team_forced = TEAM_FORCE_DEFAULT;
348 if (!teamplay && Player_HasRealForcedTeam(player))
350 player.team_forced = TEAM_FORCE_DEFAULT;
354 entity TeamBalance_CheckAllowedTeams(entity for_whom)
356 entity balance = spawn();
357 for (int i = 0; i < NUM_TEAMS; ++i)
359 entity team_ent = balance.m_team_balance_team[i] = spawn();
360 team_ent.m_team_score = g_team_entities[i].m_team_score;
361 team_ent.m_num_players = TEAM_NOT_ALLOWED;
362 team_ent.m_num_bots = 0;
364 setthink(balance, TeamBalance_Destroy);
367 string teament_name = string_null;
368 bool mutator_returnvalue = MUTATOR_CALLHOOK(TeamBalance_CheckAllowedTeams,
369 teams_mask, teament_name, for_whom);
370 teams_mask = M_ARGV(0, float);
371 teament_name = M_ARGV(1, string);
372 if (mutator_returnvalue)
374 for (int i = 0; i < NUM_TEAMS; ++i)
376 if (teams_mask & BIT(i))
378 balance.m_team_balance_team[i].m_num_players = 0;
385 entity head = find(NULL, classname, teament_name);
388 if (Team_IsValidTeam(head.team))
390 TeamBalance_GetTeam(balance, head.team).m_num_players = 0;
392 head = find(head, classname, teament_name);
396 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
397 if (AvailableTeams() == 2)
398 if (autocvar_bot_vs_human && for_whom)
400 if (autocvar_bot_vs_human > 0)
402 // find last team available
403 if (IS_BOT_CLIENT(for_whom))
405 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
407 TeamBalance_BanTeamsExcept(balance, 4);
409 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
411 TeamBalance_BanTeamsExcept(balance, 3);
415 TeamBalance_BanTeamsExcept(balance, 2);
417 // no further cases, we know at least 2 teams exist
421 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
423 TeamBalance_BanTeamsExcept(balance, 1);
425 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
427 TeamBalance_BanTeamsExcept(balance, 2);
431 TeamBalance_BanTeamsExcept(balance, 3);
433 // no further cases, bots have one of the teams
438 // find first team available
439 if (IS_BOT_CLIENT(for_whom))
441 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
443 TeamBalance_BanTeamsExcept(balance, 1);
445 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
447 TeamBalance_BanTeamsExcept(balance, 2);
451 TeamBalance_BanTeamsExcept(balance, 3);
453 // no further cases, we know at least 2 teams exist
457 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
459 TeamBalance_BanTeamsExcept(balance, 4);
461 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
463 TeamBalance_BanTeamsExcept(balance, 3);
467 TeamBalance_BanTeamsExcept(balance, 2);
469 // no further cases, bots have one of the teams
476 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
480 // if player has a forced team, ONLY allow that one
481 for (int i = 1; i <= NUM_TEAMS; ++i)
483 if (for_whom.team_forced == i &&
484 TeamBalance_IsTeamAllowedInternal(balance, i))
486 TeamBalance_BanTeamsExcept(balance, i);
490 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
494 void TeamBalance_Destroy(entity balance)
500 for (int i = 0; i < NUM_TEAMS; ++i)
502 delete(balance.(m_team_balance_team[i]));
507 int TeamBalance_GetAllowedTeams(entity balance)
511 LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
513 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
515 LOG_FATAL("TeamBalance_GetAllowedTeams: "
516 "Team balance entity is not initialized.");
519 for (int i = 1; i <= NUM_TEAMS; ++i)
521 if (TeamBalance_IsTeamAllowedInternal(balance, i))
523 result |= Team_IndexToBit(i);
529 bool TeamBalance_IsTeamAllowed(entity balance, int index)
533 LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
535 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
537 LOG_FATAL("TeamBalance_IsTeamAllowed: "
538 "Team balance entity is not initialized.");
540 if (!Team_IsValidIndex(index))
542 LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
545 return TeamBalance_IsTeamAllowedInternal(balance, index);
548 void TeamBalance_GetTeamCounts(entity balance, entity ignore)
552 LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
554 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
556 LOG_FATAL("TeamBalance_GetTeamCounts: "
557 "Team balance entity is not initialized.");
559 if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
561 // Mutator has overriden the configuration.
562 for (int i = 1; i <= NUM_TEAMS; ++i)
564 entity team_ent = TeamBalance_GetTeamFromIndex(balance, i);
565 if (TeamBalanceTeam_IsAllowed(team_ent))
567 MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, i, ignore);
568 team_ent.m_num_players = M_ARGV(2, float);
569 team_ent.m_num_bots = M_ARGV(3, float);
575 // Manually count all players.
583 if (IS_PLAYER(it) || it.caplayer)
587 else if (Player_HasRealForcedTeam(it))
589 team_num = Team_IndexToTeam(it.team_forced); // reserve the spot
595 if (!Team_IsValidTeam(team_num))
599 entity team_ent = TeamBalance_GetTeam(balance, team_num);
600 if (!TeamBalanceTeam_IsAllowed(team_ent))
604 ++team_ent.m_num_players;
605 if (IS_BOT_CLIENT(it))
607 ++team_ent.m_num_bots;
612 // if the player who has a forced team has not joined yet, reserve the spot
613 if (autocvar_g_campaign)
615 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
617 entity team_ent = TeamBalance_GetTeamFromIndex(balance,
618 autocvar_g_campaign_forceteam);
619 if (team_ent.m_num_players == team_ent.m_num_bots)
621 ++team_ent.m_num_players;
625 balance.m_team_balance_state = TEAM_BALANCE_TEAM_COUNTS_FILLED;
628 int TeamBalance_GetNumberOfPlayers(entity balance, int index)
632 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
633 "Team balance entity is NULL.");
635 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
637 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
638 "TeamBalance_GetTeamCounts has not been called.");
640 if (!Team_IsValidIndex(index))
642 LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
645 return balance.m_team_balance_team[index - 1].m_num_players;
648 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
652 LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
654 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
656 LOG_FATAL("TeamBalance_FindBestTeam: "
657 "Team balance entity is not initialized.");
659 // count how many players are in each team
662 TeamBalance_GetTeamCounts(balance, player);
666 TeamBalance_GetTeamCounts(balance, NULL);
668 int team_bits = TeamBalance_FindBestTeams(balance, player, true);
671 LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
672 MapInfo_Type_ToString(MapInfo_CurrentGametype()));
674 RandomSelection_Init();
675 for (int i = 1; i <= NUM_TEAMS; ++i)
677 if (team_bits & Team_IndexToBit(i))
679 RandomSelection_AddFloat(i, 1, 1);
682 return RandomSelection_chosen_float;
685 int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
689 LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
691 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
693 LOG_FATAL("TeamBalance_FindBestTeams: "
694 "TeamBalance_GetTeamCounts has not been called.");
696 if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
698 return M_ARGV(1, float);
701 int previous_team = 0;
702 for (int i = 1; i <= NUM_TEAMS; ++i)
704 if (!TeamBalance_IsTeamAllowedInternal(balance, i))
708 if (previous_team == 0)
710 team_bits = Team_IndexToBit(i);
714 int compare = TeamBalance_CompareTeams(balance, i, previous_team,
716 if (compare == TEAMS_COMPARE_LESS)
718 team_bits = Team_IndexToBit(i);
722 if (compare == TEAMS_COMPARE_EQUAL)
724 team_bits |= Team_IndexToBit(i);
731 void TeamBalance_JoinBestTeam(entity this)
733 //PrintToChatAll(sprintf("JoinBestTeam: %s", this.netname));
734 // don't join a team if we're not playing a team game
740 // find out what teams are available
741 entity balance = TeamBalance_CheckAllowedTeams(this);
743 if (this.bot_forced_team)
745 TeamBalance_Destroy(balance);
748 int best_team_index = TeamBalance_FindBestTeam(balance, this, true);
749 int old_team_index = Team_TeamToIndex(this.team);
750 TeamBalance_Destroy(balance);
751 PlayerScore_Clear(this);
752 if (!SetPlayerTeam(this, best_team_index, TEAM_CHANGE_AUTO))
756 if ((old_team_index != -1) && !IS_BOT_CLIENT(this))
758 TeamBalance_AutoBalanceBots(best_team_index, old_team_index);
762 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
763 entity player, bool use_score)
767 LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
769 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
771 LOG_FATAL("TeamBalance_CompareTeams: "
772 "TeamBalance_GetTeamCounts has not been called.");
774 if (!Team_IsValidIndex(team_index_a))
776 LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
779 if (!Team_IsValidIndex(team_index_b))
781 LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
784 if (team_index_a == team_index_b)
786 return TEAMS_COMPARE_EQUAL;
788 entity team_a = TeamBalance_GetTeamFromIndex(balance, team_index_a);
789 entity team_b = TeamBalance_GetTeamFromIndex(balance, team_index_b);
790 return TeamBalance_CompareTeamsInternal(team_a, team_b, player, use_score);
793 void TeamBalance_AutoBalanceBots(int source_team_index,
794 int destination_team_index)
796 if (!Team_IsValidIndex(source_team_index))
798 LOG_WARNF("TeamBalance_AutoBalanceBots: "
799 "Source team index is invalid: %f", source_team_index);
802 if (!Team_IsValidIndex(destination_team_index))
804 LOG_WARNF("TeamBalance_AutoBalanceBots: "
805 "Destination team index is invalid: %f", destination_team_index);
808 if (!autocvar_g_balance_teams ||
809 !autocvar_g_balance_teams_prevent_imbalance)
813 entity balance = TeamBalance_CheckAllowedTeams(NULL);
814 TeamBalance_GetTeamCounts(balance, NULL);
815 entity source_team = TeamBalance_GetTeamFromIndex(balance,
817 entity destination_team = TeamBalance_GetTeamFromIndex(balance,
818 destination_team_index);
819 if ((source_team.m_num_bots == 0) || (source_team.m_num_players <=
820 destination_team.m_num_players))
822 TeamBalance_Destroy(balance);
825 TeamBalance_Destroy(balance);
826 entity lowest_bot = NULL;
827 if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, source_team_index,
828 destination_team_index, true))
830 lowest_bot = M_ARGV(3, entity);
834 float lowest_score = FLOAT_MAX;
835 FOREACH_CLIENT(IS_BOT_CLIENT(it) && (Entity_GetTeamIndex(it) ==
838 float temp_score = PlayerScore_Get(it, SP_SCORE);
839 if (temp_score >= lowest_score)
843 balance = TeamBalance_CheckAllowedTeams(it);
844 if (TeamBalance_IsTeamAllowed(balance, destination_team_index))
847 lowest_score = temp_score;
849 TeamBalance_Destroy(balance);
852 if (lowest_bot == NULL)
856 if (!Player_SetTeamIndex(lowest_bot, destination_team_index))
860 KillPlayerForTeamChange(lowest_bot);
863 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
865 return balance.m_team_balance_team[index - 1].m_num_players !=
869 void TeamBalance_BanTeamsExcept(entity balance, int index)
871 for (int i = 1; i <= NUM_TEAMS; ++i)
875 balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
880 entity TeamBalance_GetTeamFromIndex(entity balance, int index)
882 if (!Team_IsValidIndex(index))
884 LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
886 return balance.m_team_balance_team[index - 1];
889 entity TeamBalance_GetTeam(entity balance, int team_num)
891 return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
894 bool TeamBalanceTeam_IsAllowed(entity team_ent)
896 return team_ent.m_num_players != TEAM_NOT_ALLOWED;
899 int TeamBalanceTeam_GetNumberOfPlayers(entity team_ent)
901 return team_ent.m_num_players;
904 int TeamBalanceTeam_GetNumberOfBots(entity team_ent)
906 return team_ent.m_num_bots;
909 int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
910 entity player, bool use_score)
912 if (team_a == team_b)
914 return TEAMS_COMPARE_EQUAL;
916 if (!TeamBalanceTeam_IsAllowed(team_a) ||
917 !TeamBalanceTeam_IsAllowed(team_b))
919 return TEAMS_COMPARE_INVALID;
921 int num_players_team_a = team_a.m_num_players;
922 int num_players_team_b = team_b.m_num_players;
923 if (IS_REAL_CLIENT(player) && bots_would_leave)
925 num_players_team_a -= team_a.m_num_bots;
926 num_players_team_b -= team_b.m_num_bots;
928 if (num_players_team_a < num_players_team_b)
930 return TEAMS_COMPARE_LESS;
932 if (num_players_team_a > num_players_team_b)
934 return TEAMS_COMPARE_GREATER;
938 return TEAMS_COMPARE_EQUAL;
940 if (team_a.m_team_score < team_b.m_team_score)
942 return TEAMS_COMPARE_LESS;
944 if (team_a.m_team_score > team_b.m_team_score)
946 return TEAMS_COMPARE_GREATER;
948 return TEAMS_COMPARE_EQUAL;
951 // Called when the player connects or when they change their color with "color"
953 void SV_ChangeTeam(entity this, float _color)
955 //PrintToChatAll(sprintf("SV_ChangeTeam: %s, %f", this.netname, _color));
957 // in normal deathmatch we can just apply the color and we're done
959 SetPlayerColors(this, _color);
963 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
964 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING, this.netname);
971 int source_color, destination_color;
972 int source_team_index, destination_team_index;
974 source_color = this.clientcolors & 0x0F;
975 destination_color = _color & 0x0F;
977 source_team_index = Team_TeamToIndex(source_color + 1);
978 destination_team_index = Team_TeamToIndex(destination_color + 1);
980 if (destination_team_index == -1)
985 entity balance = TeamBalance_CheckAllowedTeams(this);
987 if (destination_team_index == 1 && !TeamBalance_IsTeamAllowedInternal(
990 destination_team_index = 4;
992 if (destination_team_index == 4 && !TeamBalance_IsTeamAllowedInternal(
995 destination_team_index = 3;
997 if (destination_team_index == 3 && !TeamBalance_IsTeamAllowedInternal(
1000 destination_team_index = 2;
1002 if (destination_team_index == 2 && !TeamBalance_IsTeamAllowedInternal(
1005 destination_team_index = 1;
1008 // not changing teams
1009 if (source_color == destination_color)
1011 SetPlayerTeam(this, destination_team_index, TEAM_CHANGE_MANUAL);
1012 TeamBalance_Destroy(balance);
1016 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && CS(this).wasplayer)) {
1017 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
1018 return; // changing teams is not allowed
1021 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
1022 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
1024 TeamBalance_GetTeamCounts(balance, this);
1025 if ((Team_IndexToBit(destination_team_index) &
1026 TeamBalance_FindBestTeams(balance, this, false)) == 0)
1028 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
1029 TeamBalance_Destroy(balance);
1033 TeamBalance_Destroy(balance);
1034 if (IS_PLAYER(this) && source_team_index != destination_team_index)
1036 // reduce frags during a team change
1037 PlayerScore_Clear(this);
1039 if (!SetPlayerTeam(this, destination_team_index, TEAM_CHANGE_MANUAL))
1043 TeamBalance_AutoBalanceBots(destination_team_index, source_team_index);