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 PlayerScore_Clear(player);
218 if (team_index != -1)
220 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(
221 player.team, INFO_JOIN_PLAY_TEAM), player.netname);
225 if (!CS(player).just_joined)
227 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE,
232 CS(player).just_joined = false;
235 KillPlayerForTeamChange(player);
236 if (!IS_BOT_CLIENT(player))
238 TeamBalance_AutoBalanceBots();
244 void Player_SetTeamIndexChecked(entity player, int team_index)
250 if (!Team_IsValidIndex(team_index))
254 if ((autocvar_g_campaign) || (autocvar_g_changeteam_banned &&
255 CS(player).wasplayer))
257 Send_Notification(NOTIF_ONE, player, MSG_INFO,
258 INFO_TEAMCHANGE_NOTALLOWED);
261 entity balance = TeamBalance_CheckAllowedTeams(player);
262 if (team_index == 1 && !TeamBalance_IsTeamAllowedInternal(balance, 1))
266 if (team_index == 4 && !TeamBalance_IsTeamAllowedInternal(balance, 4))
270 if (team_index == 3 && !TeamBalance_IsTeamAllowedInternal(balance, 3))
274 if (team_index == 2 && !TeamBalance_IsTeamAllowedInternal(balance, 2))
278 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
279 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
281 TeamBalance_GetTeamCounts(balance, player);
282 if ((Team_IndexToBit(team_index) & TeamBalance_FindBestTeams(balance,
283 player, false)) == 0)
285 Send_Notification(NOTIF_ONE, player, MSG_INFO,
286 INFO_TEAMCHANGE_LARGERTEAM);
287 TeamBalance_Destroy(balance);
291 TeamBalance_Destroy(balance);
292 SetPlayerTeam(player, team_index, TEAM_CHANGE_MANUAL);
295 bool MoveToTeam(entity client, int team_index, int type)
297 //PrintToChatAll(sprintf("MoveToTeam: %s, %f", client.netname, team_index));
298 int lockteams_backup = lockteams; // backup any team lock
299 lockteams = 0; // disable locked teams
300 if (!SetPlayerTeam(client, team_index, type))
302 lockteams = lockteams_backup; // restore the team lock
305 lockteams = lockteams_backup; // restore the team lock
309 bool Player_HasRealForcedTeam(entity player)
311 return player.team_forced > TEAM_FORCE_DEFAULT;
314 int Player_GetForcedTeamIndex(entity player)
316 return player.team_forced;
319 void Player_SetForcedTeamIndex(entity player, int team_index)
323 case TEAM_FORCE_SPECTATOR:
324 case TEAM_FORCE_DEFAULT:
326 player.team_forced = team_index;
331 if (!Team_IsValidIndex(team_index))
333 LOG_FATAL("Player_SetForcedTeamIndex: Invalid team index.");
337 player.team_forced = team_index;
344 void Player_DetermineForcedTeam(entity player)
346 if (autocvar_g_campaign)
348 if (IS_REAL_CLIENT(player)) // only players, not bots
350 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
352 player.team_forced = autocvar_g_campaign_forceteam;
356 player.team_forced = TEAM_FORCE_DEFAULT;
360 else if (PlayerInList(player, autocvar_g_forced_team_red))
362 player.team_forced = 1;
364 else if (PlayerInList(player, autocvar_g_forced_team_blue))
366 player.team_forced = 2;
368 else if (PlayerInList(player, autocvar_g_forced_team_yellow))
370 player.team_forced = 3;
372 else if (PlayerInList(player, autocvar_g_forced_team_pink))
374 player.team_forced = 4;
378 switch (autocvar_g_forced_team_otherwise)
382 player.team_forced = 1;
387 player.team_forced = 2;
392 player.team_forced = 3;
397 player.team_forced = 4;
403 player.team_forced = TEAM_FORCE_SPECTATOR;
408 player.team_forced = TEAM_FORCE_DEFAULT;
413 if (!teamplay && Player_HasRealForcedTeam(player))
415 player.team_forced = TEAM_FORCE_DEFAULT;
419 void TeamBalance_JoinBestTeam(entity player)
421 //PrintToChatAll(sprintf("TeamBalance_JoinBestTeam: %s", player.netname));
426 if (player.bot_forced_team)
430 entity balance = TeamBalance_CheckAllowedTeams(player);
431 if (Player_HasRealForcedTeam(player))
433 int forced_team_index = player.team_forced;
434 bool is_team_allowed = TeamBalance_IsTeamAllowedInternal(balance,
436 TeamBalance_Destroy(balance);
437 if (!is_team_allowed)
441 if (!SetPlayerTeam(player, forced_team_index, TEAM_CHANGE_AUTO))
447 int best_team_index = TeamBalance_FindBestTeam(balance, player, true);
448 TeamBalance_Destroy(balance);
449 if (!SetPlayerTeam(player, best_team_index, TEAM_CHANGE_AUTO))
455 entity TeamBalance_CheckAllowedTeams(entity for_whom)
457 entity balance = spawn();
458 for (int i = 0; i < NUM_TEAMS; ++i)
460 entity team_ent = balance.m_team_balance_team[i] = spawn();
461 team_ent.m_team_score = g_team_entities[i].m_team_score;
462 team_ent.m_num_players = TEAM_NOT_ALLOWED;
463 team_ent.m_num_bots = 0;
465 setthink(balance, TeamBalance_Destroy);
468 string teament_name = string_null;
469 bool mutator_returnvalue = MUTATOR_CALLHOOK(TeamBalance_CheckAllowedTeams,
470 teams_mask, teament_name, for_whom);
471 teams_mask = M_ARGV(0, float);
472 teament_name = M_ARGV(1, string);
473 if (mutator_returnvalue)
475 for (int i = 0; i < NUM_TEAMS; ++i)
477 if (teams_mask & BIT(i))
479 balance.m_team_balance_team[i].m_num_players = 0;
486 entity head = find(NULL, classname, teament_name);
489 if (Team_IsValidTeam(head.team))
491 TeamBalance_GetTeam(balance, head.team).m_num_players = 0;
493 head = find(head, classname, teament_name);
497 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
498 if (AvailableTeams() == 2)
499 if (autocvar_bot_vs_human && for_whom)
501 if (autocvar_bot_vs_human > 0)
503 // find last team available
504 if (IS_BOT_CLIENT(for_whom))
506 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
508 TeamBalance_BanTeamsExcept(balance, 4);
510 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
512 TeamBalance_BanTeamsExcept(balance, 3);
516 TeamBalance_BanTeamsExcept(balance, 2);
518 // no further cases, we know at least 2 teams exist
522 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
524 TeamBalance_BanTeamsExcept(balance, 1);
526 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
528 TeamBalance_BanTeamsExcept(balance, 2);
532 TeamBalance_BanTeamsExcept(balance, 3);
534 // no further cases, bots have one of the teams
539 // find first team available
540 if (IS_BOT_CLIENT(for_whom))
542 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
544 TeamBalance_BanTeamsExcept(balance, 1);
546 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
548 TeamBalance_BanTeamsExcept(balance, 2);
552 TeamBalance_BanTeamsExcept(balance, 3);
554 // no further cases, we know at least 2 teams exist
558 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
560 TeamBalance_BanTeamsExcept(balance, 4);
562 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
564 TeamBalance_BanTeamsExcept(balance, 3);
568 TeamBalance_BanTeamsExcept(balance, 2);
570 // no further cases, bots have one of the teams
577 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
581 // if player has a forced team, ONLY allow that one
582 for (int i = 1; i <= NUM_TEAMS; ++i)
584 if (for_whom.team_forced == i &&
585 TeamBalance_IsTeamAllowedInternal(balance, i))
587 TeamBalance_BanTeamsExcept(balance, i);
591 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
595 void TeamBalance_Destroy(entity balance)
601 for (int i = 0; i < NUM_TEAMS; ++i)
603 delete(balance.(m_team_balance_team[i]));
608 int TeamBalance_GetAllowedTeams(entity balance)
612 LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
614 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
616 LOG_FATAL("TeamBalance_GetAllowedTeams: "
617 "Team balance entity is not initialized.");
620 for (int i = 1; i <= NUM_TEAMS; ++i)
622 if (TeamBalance_IsTeamAllowedInternal(balance, i))
624 result |= Team_IndexToBit(i);
630 bool TeamBalance_IsTeamAllowed(entity balance, int index)
634 LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
636 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
638 LOG_FATAL("TeamBalance_IsTeamAllowed: "
639 "Team balance entity is not initialized.");
641 if (!Team_IsValidIndex(index))
643 LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
646 return TeamBalance_IsTeamAllowedInternal(balance, index);
649 void TeamBalance_GetTeamCounts(entity balance, entity ignore)
653 LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
655 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
657 LOG_FATAL("TeamBalance_GetTeamCounts: "
658 "Team balance entity is not initialized.");
660 if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
662 // Mutator has overriden the configuration.
663 for (int i = 1; i <= NUM_TEAMS; ++i)
665 entity team_ent = TeamBalance_GetTeamFromIndex(balance, i);
666 if (TeamBalanceTeam_IsAllowed(team_ent))
668 MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, i, ignore);
669 team_ent.m_num_players = M_ARGV(2, float);
670 team_ent.m_num_bots = M_ARGV(3, float);
676 // Manually count all players.
684 // TODO: Reconsider when the player is truly on the team.
685 if (IS_CLIENT(it) || (it.caplayer))
689 else if (Player_HasRealForcedTeam(it))
691 // Do we really need this? Probably not.
692 team_num = Team_IndexToTeam(it.team_forced); // reserve the spot
698 if (!Team_IsValidTeam(team_num))
702 entity team_ent = TeamBalance_GetTeam(balance, team_num);
703 if (!TeamBalanceTeam_IsAllowed(team_ent))
707 ++team_ent.m_num_players;
708 if (IS_BOT_CLIENT(it))
710 ++team_ent.m_num_bots;
715 // if the player who has a forced team has not joined yet, reserve the spot
716 if (autocvar_g_campaign)
718 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
720 entity team_ent = TeamBalance_GetTeamFromIndex(balance,
721 autocvar_g_campaign_forceteam);
722 if (team_ent.m_num_players == team_ent.m_num_bots)
724 ++team_ent.m_num_players;
728 balance.m_team_balance_state = TEAM_BALANCE_TEAM_COUNTS_FILLED;
731 int TeamBalance_GetNumberOfPlayers(entity balance, int index)
735 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
736 "Team balance entity is NULL.");
738 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
740 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
741 "TeamBalance_GetTeamCounts has not been called.");
743 if (!Team_IsValidIndex(index))
745 LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
748 return balance.m_team_balance_team[index - 1].m_num_players;
751 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
755 LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
757 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
759 LOG_FATAL("TeamBalance_FindBestTeam: "
760 "Team balance entity is not initialized.");
762 // count how many players are in each team
765 TeamBalance_GetTeamCounts(balance, player);
769 TeamBalance_GetTeamCounts(balance, NULL);
771 int team_bits = TeamBalance_FindBestTeams(balance, player, true);
774 LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
775 MapInfo_Type_ToString(MapInfo_CurrentGametype()));
777 RandomSelection_Init();
778 for (int i = 1; i <= NUM_TEAMS; ++i)
780 if (team_bits & Team_IndexToBit(i))
782 RandomSelection_AddFloat(i, 1, 1);
785 return RandomSelection_chosen_float;
788 int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
792 LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
794 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
796 LOG_FATAL("TeamBalance_FindBestTeams: "
797 "TeamBalance_GetTeamCounts has not been called.");
799 if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
801 return M_ARGV(1, float);
804 int previous_team = 0;
805 for (int i = 1; i <= NUM_TEAMS; ++i)
807 if (!TeamBalance_IsTeamAllowedInternal(balance, i))
811 if (previous_team == 0)
813 team_bits = Team_IndexToBit(i);
817 int compare = TeamBalance_CompareTeams(balance, i, previous_team,
819 if (compare == TEAMS_COMPARE_LESS)
821 team_bits = Team_IndexToBit(i);
825 if (compare == TEAMS_COMPARE_EQUAL)
827 team_bits |= Team_IndexToBit(i);
834 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
835 entity player, bool use_score)
839 LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
841 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
843 LOG_FATAL("TeamBalance_CompareTeams: "
844 "TeamBalance_GetTeamCounts has not been called.");
846 if (!Team_IsValidIndex(team_index_a))
848 LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
851 if (!Team_IsValidIndex(team_index_b))
853 LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
856 if (team_index_a == team_index_b)
858 return TEAMS_COMPARE_EQUAL;
860 entity team_a = TeamBalance_GetTeamFromIndex(balance, team_index_a);
861 entity team_b = TeamBalance_GetTeamFromIndex(balance, team_index_b);
862 return TeamBalance_CompareTeamsInternal(team_a, team_b, player, use_score);
865 void TeamBalance_AutoBalanceBots()
867 if (!autocvar_g_balance_teams ||
868 !autocvar_g_balance_teams_prevent_imbalance)
872 //PrintToChatAll("TeamBalance_AutoBalanceBots");
873 entity balance = TeamBalance_CheckAllowedTeams(NULL);
874 TeamBalance_GetTeamCounts(balance, NULL);
875 int smallest_team_index = 0;
876 int smallest_team_player_count = 0;
877 for (int i = 1; i <= NUM_TEAMS; ++i)
879 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
880 if (!TeamBalanceTeam_IsAllowed(team_))
884 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
885 if (smallest_team_index == 0)
887 smallest_team_index = i;
888 smallest_team_player_count = playercount;
890 else if (playercount < smallest_team_player_count)
892 smallest_team_index = i;
893 smallest_team_player_count = playercount;
896 //PrintToChatAll(sprintf("Smallest team: %f", smallest_team_index));
897 //PrintToChatAll(sprintf("Smallest team players: %f", smallest_team_player_count));
898 entity switchable_bot = NULL;
899 int teams = BITS(NUM_TEAMS);
902 int largest_team_index = TeamBalance_GetLargestTeamIndex(balance,
904 if (smallest_team_index == largest_team_index)
906 TeamBalance_Destroy(balance);
909 entity largest_team = TeamBalance_GetTeamFromIndex(balance,
911 int largest_team_player_count = TeamBalanceTeam_GetNumberOfPlayers(
913 if (largest_team_player_count - smallest_team_player_count < 2)
915 TeamBalance_Destroy(balance);
918 //PrintToChatAll(sprintf("Largest team: %f", largest_team_index));
919 //PrintToChatAll(sprintf("Largest team players: %f", largest_team_player_count));
920 switchable_bot = TeamBalance_GetPlayerForTeamSwitch(largest_team_index,
921 smallest_team_index, true);
922 if (switchable_bot != NULL)
926 teams &= ~Team_IndexToBit(largest_team_index);
928 TeamBalance_Destroy(balance);
929 if (switchable_bot == NULL)
931 //PrintToChatAll("No bot found after searching through all the teams");
934 SetPlayerTeam(switchable_bot, smallest_team_index, TEAM_CHANGE_AUTO);
937 int TeamBalance_GetLargestTeamIndex(entity balance, int teams)
939 int largest_team_index = 0;
940 int largest_team_player_count = 0;
941 for (int i = 1; i <= NUM_TEAMS; ++i)
943 if (!(Team_IndexToBit(i) & teams))
947 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
948 if (!TeamBalanceTeam_IsAllowed(team_))
952 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
953 if (largest_team_index == 0)
955 largest_team_index = i;
956 largest_team_player_count = playercount;
958 else if (playercount > largest_team_player_count)
960 largest_team_index = i;
961 largest_team_player_count = playercount;
964 return largest_team_index;
967 entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index,
968 int destination_team_index, bool is_bot)
970 if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, source_team_index,
971 destination_team_index, is_bot))
973 return M_ARGV(3, entity);
975 entity lowest_player = NULL;
976 float lowest_score = FLOAT_MAX;
977 FOREACH_CLIENT(Entity_GetTeamIndex(it) == source_team_index,
979 if (IS_BOT_CLIENT(it) != is_bot)
983 float temp_score = PlayerScore_Get(it, SP_SCORE);
984 if (temp_score >= lowest_score)
988 //PrintToChatAll(sprintf(
989 // "Found %s with lowest score, checking allowed teams", it.netname));
990 entity balance = TeamBalance_CheckAllowedTeams(it);
991 if (TeamBalance_IsTeamAllowed(balance, source_team_index))
993 //PrintToChatAll("Allowed");
995 lowest_score = temp_score;
999 //PrintToChatAll("Not allowed");
1001 TeamBalance_Destroy(balance);
1003 return lowest_player;
1006 void LogTeamChange(float player_id, float team_number, int type)
1008 if (!autocvar_sv_eventlog)
1016 GameLogEcho(sprintf(":team:%f:%f:%f", player_id, team_number, type));
1019 void KillPlayerForTeamChange(entity player)
1021 if (IS_DEAD(player))
1025 if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
1029 Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
1030 player.origin, '0 0 0');
1033 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
1035 return balance.m_team_balance_team[index - 1].m_num_players !=
1039 void TeamBalance_BanTeamsExcept(entity balance, int index)
1041 for (int i = 1; i <= NUM_TEAMS; ++i)
1045 balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
1050 entity TeamBalance_GetTeamFromIndex(entity balance, int index)
1052 if (!Team_IsValidIndex(index))
1054 LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
1056 return balance.m_team_balance_team[index - 1];
1059 entity TeamBalance_GetTeam(entity balance, int team_num)
1061 return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
1064 bool TeamBalanceTeam_IsAllowed(entity team_ent)
1066 return team_ent.m_num_players != TEAM_NOT_ALLOWED;
1069 int TeamBalanceTeam_GetNumberOfPlayers(entity team_ent)
1071 return team_ent.m_num_players;
1074 int TeamBalanceTeam_GetNumberOfBots(entity team_ent)
1076 return team_ent.m_num_bots;
1079 int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
1080 entity player, bool use_score)
1082 if (team_a == team_b)
1084 return TEAMS_COMPARE_EQUAL;
1086 if (!TeamBalanceTeam_IsAllowed(team_a) ||
1087 !TeamBalanceTeam_IsAllowed(team_b))
1089 return TEAMS_COMPARE_INVALID;
1091 int num_players_team_a = team_a.m_num_players;
1092 int num_players_team_b = team_b.m_num_players;
1093 if (IS_REAL_CLIENT(player) && bots_would_leave)
1095 num_players_team_a -= team_a.m_num_bots;
1096 num_players_team_b -= team_b.m_num_bots;
1098 if (num_players_team_a < num_players_team_b)
1100 return TEAMS_COMPARE_LESS;
1102 if (num_players_team_a > num_players_team_b)
1104 return TEAMS_COMPARE_GREATER;
1108 return TEAMS_COMPARE_EQUAL;
1110 if (team_a.m_team_score < team_b.m_team_score)
1112 return TEAMS_COMPARE_LESS;
1114 if (team_a.m_team_score > team_b.m_team_score)
1116 return TEAMS_COMPARE_GREATER;
1118 return TEAMS_COMPARE_EQUAL;
1121 void SV_ChangeTeam(entity player, int new_color)
1125 SetPlayerColors(player, new_color);
1127 // TODO: Should we really bother with this?
1128 if(!IS_CLIENT(player))
1130 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
1131 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING,
1139 Player_SetTeamIndexChecked(player, Team_TeamToIndex((new_color & 0x0F) +