3 #include <common/deathtypes/all.qh>
4 #include <common/gamemodes/_mod.qh>
5 #include <common/teams.qh>
6 #include <server/bot/api.qh>
7 #include <server/client.qh>
8 #include <server/command/vote.qh>
9 #include <server/damage.qh>
10 #include <server/gamelog.qh>
11 #include <server/mutators/_mod.qh>
12 #include <server/race.qh>
13 #include <server/scores.qh>
14 #include <server/scores_rules.qh>
16 /// \brief Describes a state of team balance entity.
19 TEAM_BALANCE_UNINITIALIZED, ///< The team balance has not been initialized.
20 /// \brief TeamBalance_CheckAllowedTeams has been called.
21 TEAM_BALANCE_TEAMS_CHECKED,
22 /// \brief TeamBalance_GetTeamCounts has been called.
23 TEAM_BALANCE_TEAM_COUNTS_FILLED
26 /// \brief Indicates that the player is not allowed to join a team.
27 const int TEAM_NOT_ALLOWED = -1;
29 .float team_forced; // can be a team number to force a team, or 0 for default action, or -1 for forced spectator
31 .int m_team_balance_state; ///< Holds the state of the team balance entity.
32 .entity m_team_balance_team[NUM_TEAMS]; ///< ???
34 .float m_team_score; ///< The score of the team.
35 .int m_num_players; ///< Number of players (both humans and bots) in a team.
36 .int m_num_bots; ///< Number of bots in a team.
37 .int m_num_players_alive; ///< Number of alive players in a team.
38 .int m_num_control_points; ///< Number of control points owned by a team.
40 string autocvar_g_forced_team_red;
41 string autocvar_g_forced_team_blue;
42 string autocvar_g_forced_team_yellow;
43 string autocvar_g_forced_team_pink;
45 entity g_team_entities[NUM_TEAMS]; ///< Holds global team entities.
47 STATIC_INIT(g_team_entities)
49 for (int i = 0; i < NUM_TEAMS; ++i)
51 g_team_entities[i] = spawn();
55 entity Team_GetTeamFromIndex(int index)
57 if (!Team_IsValidIndex(index))
59 LOG_FATALF("Team_GetTeamFromIndex: Index is invalid: %f", index);
61 return g_team_entities[index - 1];
64 entity Team_GetTeam(int team_num)
66 if (!Team_IsValidTeam(team_num))
68 LOG_FATALF("Team_GetTeam: Value is invalid: %f", team_num);
70 return g_team_entities[Team_TeamToIndex(team_num) - 1];
73 float Team_GetTeamScore(entity team_ent)
75 return team_ent.m_team_score;
78 void Team_SetTeamScore(entity team_ent, float score)
80 team_ent.m_team_score = score;
83 int Team_GetNumberOfAlivePlayers(entity team_ent)
85 return team_ent.m_num_players_alive;
88 void Team_SetNumberOfAlivePlayers(entity team_ent, int number)
90 team_ent.m_num_players_alive = number;
93 int Team_GetNumberOfAliveTeams()
96 for (int i = 0; i < NUM_TEAMS; ++i)
98 if (g_team_entities[i].m_num_players_alive > 0)
106 int Team_GetNumberOfControlPoints(entity team_ent)
108 return team_ent.m_num_control_points;
111 void Team_SetNumberOfControlPoints(entity team_ent, int number)
113 team_ent.m_num_control_points = number;
116 int Team_GetNumberOfTeamsWithControlPoints()
119 for (int i = 0; i < NUM_TEAMS; ++i)
121 if (g_team_entities[i].m_num_control_points > 0)
129 void setcolor(entity this, int clr)
132 this.clientcolors = clr;
133 this.team = (clr & 15) + 1;
135 builtin_setcolor(this, clr);
139 bool Entity_HasValidTeam(entity this)
141 return Team_IsValidTeam(this.team);
144 int Entity_GetTeamIndex(entity this)
146 return Team_TeamToIndex(this.team);
149 entity Entity_GetTeam(entity this)
151 int index = Entity_GetTeamIndex(this);
152 if (!Team_IsValidIndex(index))
156 return Team_GetTeamFromIndex(index);
159 void SetPlayerColors(entity player, float _color)
161 float pants = _color & 0x0F;
162 float shirt = _color & 0xF0;
165 setcolor(player, 16 * pants + pants);
169 setcolor(player, shirt + pants);
173 bool Player_SetTeamIndex(entity player, int index)
175 int new_team = Team_IndexToTeam(index);
176 if (player.team == new_team)
180 // This is important when players join the game and one of their
181 // color matches the team color while other doesn't. For example
182 // [BOT]Lion: color 0 4.
183 SetPlayerColors(player, new_team - 1);
187 int old_index = Team_TeamToIndex(player.team);
188 if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, old_index, index) == true)
190 // Mutator has blocked team change.
199 SetPlayerColors(player, new_team - 1);
201 MUTATOR_CALLHOOK(Player_ChangedTeam, player, old_index, index);
205 bool SetPlayerTeam(entity player, int team_index, int type)
207 int old_team_index = Entity_GetTeamIndex(player);
208 if (!Player_SetTeamIndex(player, team_index))
212 LogTeamChange(player.playerid, player.team, type);
213 if (team_index != old_team_index)
215 PlayerScore_Clear(player);
216 if (team_index != -1)
218 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(
219 player.team, INFO_JOIN_PLAY_TEAM), player.netname);
223 if (!CS(player).just_joined)
225 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE,
229 KillPlayerForTeamChange(player);
230 if (!IS_BOT_CLIENT(player))
232 TeamBalance_AutoBalanceBots();
235 else if (team_index == -1)
237 if (!CS(player).just_joined && player.frags != FRAGS_SPECTATOR)
238 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE, player.netname);
243 void Player_SetTeamIndexChecked(entity player, int team_index)
249 if (!Team_IsValidIndex(team_index))
253 if ((autocvar_g_campaign) || (autocvar_g_changeteam_banned &&
254 CS(player).wasplayer))
256 Send_Notification(NOTIF_ONE, player, MSG_INFO,
257 INFO_TEAMCHANGE_NOTALLOWED);
260 entity balance = TeamBalance_CheckAllowedTeams(player);
261 if (team_index == 1 && !TeamBalance_IsTeamAllowedInternal(balance, 1))
265 if (team_index == 4 && !TeamBalance_IsTeamAllowedInternal(balance, 4))
269 if (team_index == 3 && !TeamBalance_IsTeamAllowedInternal(balance, 3))
273 if (team_index == 2 && !TeamBalance_IsTeamAllowedInternal(balance, 2))
277 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
278 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
280 TeamBalance_GetTeamCounts(balance, player);
281 if ((Team_IndexToBit(team_index) & TeamBalance_FindBestTeams(balance,
282 player, false)) == 0)
284 Send_Notification(NOTIF_ONE, player, MSG_INFO,
285 INFO_TEAMCHANGE_LARGERTEAM);
286 TeamBalance_Destroy(balance);
290 TeamBalance_Destroy(balance);
291 SetPlayerTeam(player, team_index, TEAM_CHANGE_MANUAL);
294 bool MoveToTeam(entity client, int team_index, int type)
296 //PrintToChatAll(sprintf("MoveToTeam: %s, %f", client.netname, team_index));
297 int lockteams_backup = lockteams; // backup any team lock
298 lockteams = 0; // disable locked teams
299 if (!SetPlayerTeam(client, team_index, type))
301 lockteams = lockteams_backup; // restore the team lock
304 lockteams = lockteams_backup; // restore the team lock
308 bool Player_HasRealForcedTeam(entity player)
310 return player.team_forced > TEAM_FORCE_DEFAULT;
313 int Player_GetForcedTeamIndex(entity player)
315 return player.team_forced;
318 void Player_SetForcedTeamIndex(entity player, int team_index)
322 case TEAM_FORCE_SPECTATOR:
323 case TEAM_FORCE_DEFAULT:
325 player.team_forced = team_index;
330 if (!Team_IsValidIndex(team_index))
332 LOG_FATAL("Player_SetForcedTeamIndex: Invalid team index.");
336 player.team_forced = team_index;
343 void Player_DetermineForcedTeam(entity player)
345 if (autocvar_g_campaign)
347 if (IS_REAL_CLIENT(player)) // only players, not bots
349 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
351 player.team_forced = autocvar_g_campaign_forceteam;
355 player.team_forced = TEAM_FORCE_DEFAULT;
359 else if (PlayerInList(player, autocvar_g_forced_team_red))
361 player.team_forced = 1;
363 else if (PlayerInList(player, autocvar_g_forced_team_blue))
365 player.team_forced = 2;
367 else if (PlayerInList(player, autocvar_g_forced_team_yellow))
369 player.team_forced = 3;
371 else if (PlayerInList(player, autocvar_g_forced_team_pink))
373 player.team_forced = 4;
377 switch (autocvar_g_forced_team_otherwise)
381 player.team_forced = 1;
386 player.team_forced = 2;
391 player.team_forced = 3;
396 player.team_forced = 4;
402 player.team_forced = TEAM_FORCE_SPECTATOR;
407 player.team_forced = TEAM_FORCE_DEFAULT;
412 if (!teamplay && Player_HasRealForcedTeam(player))
414 player.team_forced = TEAM_FORCE_DEFAULT;
418 void TeamBalance_JoinBestTeam(entity player)
420 //PrintToChatAll(sprintf("TeamBalance_JoinBestTeam: %s", player.netname));
425 if (player.bot_forced_team)
429 entity balance = TeamBalance_CheckAllowedTeams(player);
430 if (Player_HasRealForcedTeam(player))
432 int forced_team_index = player.team_forced;
433 bool is_team_allowed = TeamBalance_IsTeamAllowedInternal(balance,
435 TeamBalance_Destroy(balance);
436 if (!is_team_allowed)
440 if (!SetPlayerTeam(player, forced_team_index, TEAM_CHANGE_AUTO))
446 int best_team_index = TeamBalance_FindBestTeam(balance, player, true);
447 TeamBalance_Destroy(balance);
448 if (!SetPlayerTeam(player, best_team_index, TEAM_CHANGE_AUTO))
454 entity TeamBalance_CheckAllowedTeams(entity for_whom)
456 entity balance = spawn();
457 for (int i = 0; i < NUM_TEAMS; ++i)
459 entity team_ent = balance.m_team_balance_team[i] = spawn();
460 team_ent.m_team_score = g_team_entities[i].m_team_score;
461 team_ent.m_num_players = TEAM_NOT_ALLOWED;
462 team_ent.m_num_bots = 0;
464 setthink(balance, TeamBalance_Destroy);
465 balance.nextthink = time;
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 (autocvar_bot_vs_human && AvailableTeams() == 2 && for_whom)
500 if (autocvar_bot_vs_human > 0)
502 // find last team available
503 if (IS_BOT_CLIENT(for_whom))
505 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
507 TeamBalance_BanTeamsExcept(balance, 4);
509 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
511 TeamBalance_BanTeamsExcept(balance, 3);
515 TeamBalance_BanTeamsExcept(balance, 2);
517 // no further cases, we know at least 2 teams exist
521 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
523 TeamBalance_BanTeamsExcept(balance, 1);
525 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
527 TeamBalance_BanTeamsExcept(balance, 2);
531 TeamBalance_BanTeamsExcept(balance, 3);
533 // no further cases, bots have one of the teams
538 // find first team available
539 if (IS_BOT_CLIENT(for_whom))
541 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
543 TeamBalance_BanTeamsExcept(balance, 1);
545 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
547 TeamBalance_BanTeamsExcept(balance, 2);
551 TeamBalance_BanTeamsExcept(balance, 3);
553 // no further cases, we know at least 2 teams exist
557 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
559 TeamBalance_BanTeamsExcept(balance, 4);
561 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
563 TeamBalance_BanTeamsExcept(balance, 3);
567 TeamBalance_BanTeamsExcept(balance, 2);
569 // no further cases, bots have one of the teams
576 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
580 // if player has a forced team, ONLY allow that one
581 for (int i = 1; i <= NUM_TEAMS; ++i)
583 if (for_whom.team_forced == i &&
584 TeamBalance_IsTeamAllowedInternal(balance, i))
586 TeamBalance_BanTeamsExcept(balance, i);
590 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
594 void TeamBalance_Destroy(entity balance)
600 for (int i = 0; i < NUM_TEAMS; ++i)
602 delete(balance.(m_team_balance_team[i]));
607 int TeamBalance_GetAllowedTeams(entity balance)
611 LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
613 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
615 LOG_FATAL("TeamBalance_GetAllowedTeams: "
616 "Team balance entity is not initialized.");
619 for (int i = 1; i <= NUM_TEAMS; ++i)
621 if (TeamBalance_IsTeamAllowedInternal(balance, i))
623 result |= Team_IndexToBit(i);
629 bool TeamBalance_IsTeamAllowed(entity balance, int index)
633 LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
635 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
637 LOG_FATAL("TeamBalance_IsTeamAllowed: "
638 "Team balance entity is not initialized.");
640 if (!Team_IsValidIndex(index))
642 LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
645 return TeamBalance_IsTeamAllowedInternal(balance, index);
648 void TeamBalance_GetTeamCounts(entity balance, entity ignore)
652 LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
654 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
656 LOG_FATAL("TeamBalance_GetTeamCounts: "
657 "Team balance entity is not initialized.");
659 if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
661 // Mutator has overriden the configuration.
662 for (int i = 1; i <= NUM_TEAMS; ++i)
664 entity team_ent = TeamBalance_GetTeamFromIndex(balance, i);
665 if (TeamBalanceTeam_IsAllowed(team_ent))
667 MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, i, ignore);
668 team_ent.m_num_players = M_ARGV(2, float);
669 team_ent.m_num_bots = M_ARGV(3, float);
675 // Manually count all players.
683 // TODO: Reconsider when the player is truly on the team.
684 if (IS_CLIENT(it) || (it.caplayer))
688 else if (Player_HasRealForcedTeam(it))
690 // Do we really need this? Probably not.
691 team_num = Team_IndexToTeam(it.team_forced); // reserve the spot
697 if (!Team_IsValidTeam(team_num))
701 entity team_ent = TeamBalance_GetTeam(balance, team_num);
702 if (!TeamBalanceTeam_IsAllowed(team_ent))
706 ++team_ent.m_num_players;
707 if (IS_BOT_CLIENT(it))
709 ++team_ent.m_num_bots;
714 // if the player who has a forced team has not joined yet, reserve the spot
715 if (autocvar_g_campaign)
717 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
719 entity team_ent = TeamBalance_GetTeamFromIndex(balance,
720 autocvar_g_campaign_forceteam);
721 if (team_ent.m_num_players == team_ent.m_num_bots)
723 ++team_ent.m_num_players;
727 balance.m_team_balance_state = TEAM_BALANCE_TEAM_COUNTS_FILLED;
730 int TeamBalance_GetNumberOfPlayers(entity balance, int index)
734 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
735 "Team balance entity is NULL.");
737 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
739 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
740 "TeamBalance_GetTeamCounts has not been called.");
742 if (!Team_IsValidIndex(index))
744 LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
747 return balance.m_team_balance_team[index - 1].m_num_players;
750 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
754 LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
756 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
758 LOG_FATAL("TeamBalance_FindBestTeam: "
759 "Team balance entity is not initialized.");
761 // count how many players are in each team
764 TeamBalance_GetTeamCounts(balance, player);
768 TeamBalance_GetTeamCounts(balance, NULL);
770 int team_bits = TeamBalance_FindBestTeams(balance, player, true);
773 LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
774 MapInfo_Type_ToString(MapInfo_CurrentGametype()));
776 RandomSelection_Init();
777 for (int i = 1; i <= NUM_TEAMS; ++i)
779 if (team_bits & Team_IndexToBit(i))
781 RandomSelection_AddFloat(i, 1, 1);
784 return RandomSelection_chosen_float;
787 int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
791 LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
793 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
795 LOG_FATAL("TeamBalance_FindBestTeams: "
796 "TeamBalance_GetTeamCounts has not been called.");
798 if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
800 return M_ARGV(1, float);
803 int previous_team = 0;
804 for (int i = 1; i <= NUM_TEAMS; ++i)
806 if (!TeamBalance_IsTeamAllowedInternal(balance, i))
810 if (previous_team == 0)
812 team_bits = Team_IndexToBit(i);
816 int compare = TeamBalance_CompareTeams(balance, i, previous_team,
818 if (compare == TEAMS_COMPARE_LESS)
820 team_bits = Team_IndexToBit(i);
824 if (compare == TEAMS_COMPARE_EQUAL)
826 team_bits |= Team_IndexToBit(i);
833 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
834 entity player, bool use_score)
838 LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
840 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
842 LOG_FATAL("TeamBalance_CompareTeams: "
843 "TeamBalance_GetTeamCounts has not been called.");
845 if (!Team_IsValidIndex(team_index_a))
847 LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
850 if (!Team_IsValidIndex(team_index_b))
852 LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
855 if (team_index_a == team_index_b)
857 return TEAMS_COMPARE_EQUAL;
859 entity team_a = TeamBalance_GetTeamFromIndex(balance, team_index_a);
860 entity team_b = TeamBalance_GetTeamFromIndex(balance, team_index_b);
861 return TeamBalance_CompareTeamsInternal(team_a, team_b, player, use_score);
864 void TeamBalance_AutoBalanceBots()
866 // checks disabled because we always want auto-balanced bots
867 //if (!(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance))
870 entity balance = TeamBalance_CheckAllowedTeams(NULL);
871 TeamBalance_GetTeamCounts(balance, NULL);
872 int smallest_team_index = 0;
873 int smallest_team_player_count = 0;
874 for (int i = 1; i <= NUM_TEAMS; ++i)
876 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
877 if (!TeamBalanceTeam_IsAllowed(team_))
881 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
882 if (smallest_team_index == 0)
884 smallest_team_index = i;
885 smallest_team_player_count = playercount;
887 else if (playercount < smallest_team_player_count)
889 smallest_team_index = i;
890 smallest_team_player_count = playercount;
893 //PrintToChatAll(sprintf("Smallest team: %f", smallest_team_index));
894 //PrintToChatAll(sprintf("Smallest team players: %f", smallest_team_player_count));
895 entity switchable_bot = NULL;
896 int teams = BITS(NUM_TEAMS);
899 int largest_team_index = TeamBalance_GetLargestTeamIndex(balance,
901 if (smallest_team_index == largest_team_index)
903 TeamBalance_Destroy(balance);
906 entity largest_team = TeamBalance_GetTeamFromIndex(balance,
908 int largest_team_player_count = TeamBalanceTeam_GetNumberOfPlayers(
910 if (largest_team_player_count - smallest_team_player_count < 2)
912 TeamBalance_Destroy(balance);
915 //PrintToChatAll(sprintf("Largest team: %f", largest_team_index));
916 //PrintToChatAll(sprintf("Largest team players: %f", largest_team_player_count));
917 switchable_bot = TeamBalance_GetPlayerForTeamSwitch(largest_team_index,
918 smallest_team_index, true);
919 if (switchable_bot != NULL)
923 teams &= ~Team_IndexToBit(largest_team_index);
925 TeamBalance_Destroy(balance);
926 if (switchable_bot == NULL)
928 //PrintToChatAll("No bot found after searching through all the teams");
931 SetPlayerTeam(switchable_bot, smallest_team_index, TEAM_CHANGE_AUTO);
934 int TeamBalance_GetLargestTeamIndex(entity balance, int teams)
936 int largest_team_index = 0;
937 int largest_team_player_count = 0;
938 for (int i = 1; i <= NUM_TEAMS; ++i)
940 if (!(Team_IndexToBit(i) & teams))
944 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
945 if (!TeamBalanceTeam_IsAllowed(team_))
949 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
950 if (largest_team_index == 0)
952 largest_team_index = i;
953 largest_team_player_count = playercount;
955 else if (playercount > largest_team_player_count)
957 largest_team_index = i;
958 largest_team_player_count = playercount;
961 return largest_team_index;
964 entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index,
965 int destination_team_index, bool is_bot)
967 if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, source_team_index,
968 destination_team_index, is_bot))
970 return M_ARGV(3, entity);
972 entity lowest_player = NULL;
973 float lowest_score = FLOAT_MAX;
974 FOREACH_CLIENT(Entity_GetTeamIndex(it) == source_team_index,
976 if (IS_BOT_CLIENT(it) != is_bot)
980 float temp_score = PlayerScore_Get(it, SP_SCORE);
981 if (temp_score >= lowest_score)
985 //PrintToChatAll(sprintf(
986 // "Found %s with lowest score, checking allowed teams", it.netname));
987 entity balance = TeamBalance_CheckAllowedTeams(it);
988 if (TeamBalance_IsTeamAllowed(balance, source_team_index))
990 //PrintToChatAll("Allowed");
992 lowest_score = temp_score;
996 //PrintToChatAll("Not allowed");
998 TeamBalance_Destroy(balance);
1000 return lowest_player;
1003 void LogTeamChange(float player_id, float team_number, int type)
1005 if (!autocvar_sv_eventlog)
1013 GameLogEcho(sprintf(":team:%d:%d:%d", player_id, team_number, type));
1016 void KillPlayerForTeamChange(entity player)
1018 if (IS_DEAD(player))
1022 if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
1026 Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
1027 player.origin, '0 0 0');
1030 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
1032 return balance.m_team_balance_team[index - 1].m_num_players !=
1036 void TeamBalance_BanTeamsExcept(entity balance, int index)
1038 for (int i = 1; i <= NUM_TEAMS; ++i)
1042 balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
1047 entity TeamBalance_GetTeamFromIndex(entity balance, int index)
1049 if (!Team_IsValidIndex(index))
1051 LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
1053 return balance.m_team_balance_team[index - 1];
1056 entity TeamBalance_GetTeam(entity balance, int team_num)
1058 return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
1061 bool TeamBalanceTeam_IsAllowed(entity team_ent)
1063 return team_ent.m_num_players != TEAM_NOT_ALLOWED;
1066 int TeamBalanceTeam_GetNumberOfPlayers(entity team_ent)
1068 return team_ent.m_num_players;
1071 int TeamBalanceTeam_GetNumberOfBots(entity team_ent)
1073 return team_ent.m_num_bots;
1076 int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
1077 entity player, bool use_score)
1079 if (team_a == team_b)
1081 return TEAMS_COMPARE_EQUAL;
1083 if (!TeamBalanceTeam_IsAllowed(team_a) ||
1084 !TeamBalanceTeam_IsAllowed(team_b))
1086 return TEAMS_COMPARE_INVALID;
1088 int num_players_team_a = team_a.m_num_players;
1089 int num_players_team_b = team_b.m_num_players;
1090 if (IS_REAL_CLIENT(player) && bots_would_leave)
1092 num_players_team_a -= team_a.m_num_bots;
1093 num_players_team_b -= team_b.m_num_bots;
1095 if (num_players_team_a < num_players_team_b)
1097 return TEAMS_COMPARE_LESS;
1099 if (num_players_team_a > num_players_team_b)
1101 return TEAMS_COMPARE_GREATER;
1105 return TEAMS_COMPARE_EQUAL;
1107 if (team_a.m_team_score < team_b.m_team_score)
1109 return TEAMS_COMPARE_LESS;
1111 if (team_a.m_team_score > team_b.m_team_score)
1113 return TEAMS_COMPARE_GREATER;
1115 return TEAMS_COMPARE_EQUAL;
1118 void SV_ChangeTeam(entity player, int new_color)
1122 SetPlayerColors(player, new_color);
1124 // TODO: Should we really bother with this?
1125 if(!IS_CLIENT(player))
1127 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
1128 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING,
1136 Player_SetTeamIndexChecked(player, Team_TeamToIndex((new_color & 0x0F) +