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 .int m_team_balance_state; ///< Holds the state of the team balance entity.
30 .entity m_team_balance_team[NUM_TEAMS]; ///< ???
32 .float m_team_score; ///< The score of the team.
33 .int m_num_players; ///< Number of players (both humans and bots) in a team.
34 .int m_num_bots; ///< Number of bots in a team.
35 .int m_num_players_alive; ///< Number of alive players in a team.
36 .int m_num_control_points; ///< Number of control points owned by a team.
38 string autocvar_g_forced_team_red;
39 string autocvar_g_forced_team_blue;
40 string autocvar_g_forced_team_yellow;
41 string autocvar_g_forced_team_pink;
43 entity g_team_entities[NUM_TEAMS]; ///< Holds global team entities.
45 STATIC_INIT(g_team_entities)
47 for (int i = 0; i < NUM_TEAMS; ++i)
49 g_team_entities[i] = spawn();
53 entity Team_GetTeamFromIndex(int index)
55 if (!Team_IsValidIndex(index))
57 LOG_FATALF("Team_GetTeamFromIndex: Index is invalid: %f", index);
59 return g_team_entities[index - 1];
62 entity Team_GetTeam(int team_num)
64 if (!Team_IsValidTeam(team_num))
66 LOG_FATALF("Team_GetTeam: Value is invalid: %f", team_num);
68 return g_team_entities[Team_TeamToIndex(team_num) - 1];
71 float Team_GetTeamScore(entity team_ent)
73 return team_ent.m_team_score;
76 void Team_SetTeamScore(entity team_ent, float score)
78 team_ent.m_team_score = score;
81 int Team_GetNumberOfAlivePlayers(entity team_ent)
83 return team_ent.m_num_players_alive;
86 void Team_SetNumberOfAlivePlayers(entity team_ent, int number)
88 team_ent.m_num_players_alive = number;
91 int Team_GetNumberOfAliveTeams()
94 for (int i = 0; i < NUM_TEAMS; ++i)
96 if (g_team_entities[i].m_num_players_alive > 0)
104 int Team_GetNumberOfControlPoints(entity team_ent)
106 return team_ent.m_num_control_points;
109 void Team_SetNumberOfControlPoints(entity team_ent, int number)
111 team_ent.m_num_control_points = number;
114 int Team_GetNumberOfTeamsWithControlPoints()
117 for (int i = 0; i < NUM_TEAMS; ++i)
119 if (g_team_entities[i].m_num_control_points > 0)
127 void setcolor(entity this, int clr)
130 this.clientcolors = clr;
131 this.team = (clr & 15) + 1;
133 builtin_setcolor(this, clr);
137 bool Entity_HasValidTeam(entity this)
139 return Team_IsValidTeam(this.team);
142 int Entity_GetTeamIndex(entity this)
144 return Team_TeamToIndex(this.team);
147 entity Entity_GetTeam(entity this)
149 int index = Entity_GetTeamIndex(this);
150 if (!Team_IsValidIndex(index))
154 return Team_GetTeamFromIndex(index);
157 void SetPlayerColors(entity player, float _color)
159 float pants = _color & 0x0F;
160 float shirt = _color & 0xF0;
163 setcolor(player, 16 * pants + pants);
167 setcolor(player, shirt + pants);
171 bool Player_SetTeamIndex(entity player, int index)
173 int new_team = Team_IndexToTeam(index);
174 if (player.team == new_team)
178 // This is important when players join the game and one of their
179 // color matches the team color while other doesn't. For example
180 // [BOT]Lion: color 0 4.
181 SetPlayerColors(player, new_team - 1);
185 int old_index = Team_TeamToIndex(player.team);
186 if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, old_index, index) == true)
188 // Mutator has blocked team change.
197 SetPlayerColors(player, new_team - 1);
199 MUTATOR_CALLHOOK(Player_ChangedTeam, player, old_index, index);
203 bool SetPlayerTeam(entity player, int team_index, int type)
205 int old_team_index = Entity_GetTeamIndex(player);
206 if (!Player_SetTeamIndex(player, team_index))
210 LogTeamChange(player.playerid, player.team, type);
211 if (team_index != old_team_index)
213 PlayerScore_Clear(player);
214 if (team_index != -1)
216 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(
217 player.team, INFO_JOIN_PLAY_TEAM), player.netname);
221 if (!CS(player).just_joined)
223 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE,
227 KillPlayerForTeamChange(player);
228 if (!IS_BOT_CLIENT(player))
230 TeamBalance_AutoBalanceBots();
233 else if (team_index == -1)
235 if (!CS(player).just_joined && player.frags != FRAGS_SPECTATOR)
236 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE, player.netname);
241 void Player_SetTeamIndexChecked(entity player, int team_index)
247 if (!Team_IsValidIndex(team_index))
251 if ((autocvar_g_campaign) || (autocvar_g_changeteam_banned &&
252 CS(player).wasplayer))
254 Send_Notification(NOTIF_ONE, player, MSG_INFO,
255 INFO_TEAMCHANGE_NOTALLOWED);
258 entity balance = TeamBalance_CheckAllowedTeams(player);
259 if (team_index == 1 && !TeamBalance_IsTeamAllowedInternal(balance, 1))
263 if (team_index == 4 && !TeamBalance_IsTeamAllowedInternal(balance, 4))
267 if (team_index == 3 && !TeamBalance_IsTeamAllowedInternal(balance, 3))
271 if (team_index == 2 && !TeamBalance_IsTeamAllowedInternal(balance, 2))
275 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
276 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
278 TeamBalance_GetTeamCounts(balance, player);
279 if ((Team_IndexToBit(team_index) & TeamBalance_FindBestTeams(balance,
280 player, false)) == 0)
282 Send_Notification(NOTIF_ONE, player, MSG_INFO,
283 INFO_TEAMCHANGE_LARGERTEAM);
284 TeamBalance_Destroy(balance);
288 TeamBalance_Destroy(balance);
289 SetPlayerTeam(player, team_index, TEAM_CHANGE_MANUAL);
292 bool MoveToTeam(entity client, int team_index, int type)
294 //PrintToChatAll(sprintf("MoveToTeam: %s, %f", client.netname, team_index));
295 int lockteams_backup = lockteams; // backup any team lock
296 lockteams = 0; // disable locked teams
297 if (!SetPlayerTeam(client, team_index, type))
299 lockteams = lockteams_backup; // restore the team lock
302 lockteams = lockteams_backup; // restore the team lock
306 bool Player_HasRealForcedTeam(entity player)
308 return player.team_forced > TEAM_FORCE_DEFAULT;
311 int Player_GetForcedTeamIndex(entity player)
313 return player.team_forced;
316 void Player_SetForcedTeamIndex(entity player, int team_index)
320 case TEAM_FORCE_SPECTATOR:
321 case TEAM_FORCE_DEFAULT:
323 player.team_forced = team_index;
328 if (!Team_IsValidIndex(team_index))
330 LOG_FATAL("Player_SetForcedTeamIndex: Invalid team index.");
334 player.team_forced = team_index;
341 void Player_DetermineForcedTeam(entity player)
343 if (autocvar_g_campaign)
345 if (IS_REAL_CLIENT(player)) // only players, not bots
347 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
349 player.team_forced = autocvar_g_campaign_forceteam;
353 player.team_forced = TEAM_FORCE_DEFAULT;
357 else if (PlayerInList(player, autocvar_g_forced_team_red))
359 player.team_forced = 1;
361 else if (PlayerInList(player, autocvar_g_forced_team_blue))
363 player.team_forced = 2;
365 else if (PlayerInList(player, autocvar_g_forced_team_yellow))
367 player.team_forced = 3;
369 else if (PlayerInList(player, autocvar_g_forced_team_pink))
371 player.team_forced = 4;
375 switch (autocvar_g_forced_team_otherwise)
379 player.team_forced = 1;
384 player.team_forced = 2;
389 player.team_forced = 3;
394 player.team_forced = 4;
400 player.team_forced = TEAM_FORCE_SPECTATOR;
405 player.team_forced = TEAM_FORCE_DEFAULT;
410 if (!teamplay && Player_HasRealForcedTeam(player))
412 player.team_forced = TEAM_FORCE_DEFAULT;
416 void TeamBalance_JoinBestTeam(entity player)
418 //PrintToChatAll(sprintf("TeamBalance_JoinBestTeam: %s", player.netname));
423 if (player.bot_forced_team)
427 entity balance = TeamBalance_CheckAllowedTeams(player);
428 if (Player_HasRealForcedTeam(player))
430 int forced_team_index = player.team_forced;
431 bool is_team_allowed = TeamBalance_IsTeamAllowedInternal(balance,
433 TeamBalance_Destroy(balance);
434 if (!is_team_allowed)
438 if (!SetPlayerTeam(player, forced_team_index, TEAM_CHANGE_AUTO))
444 int best_team_index = TeamBalance_FindBestTeam(balance, player, true);
445 TeamBalance_Destroy(balance);
446 if (!SetPlayerTeam(player, best_team_index, TEAM_CHANGE_AUTO))
452 entity TeamBalance_CheckAllowedTeams(entity for_whom)
454 entity balance = spawn();
455 for (int i = 0; i < NUM_TEAMS; ++i)
457 entity team_ent = balance.m_team_balance_team[i] = spawn();
458 team_ent.m_team_score = g_team_entities[i].m_team_score;
459 team_ent.m_num_players = TEAM_NOT_ALLOWED;
460 team_ent.m_num_bots = 0;
462 setthink(balance, TeamBalance_Destroy);
463 balance.nextthink = time;
466 string teament_name = string_null;
467 bool mutator_returnvalue = MUTATOR_CALLHOOK(TeamBalance_CheckAllowedTeams,
468 teams_mask, teament_name, for_whom);
469 teams_mask = M_ARGV(0, float);
470 teament_name = M_ARGV(1, string);
471 if (mutator_returnvalue)
473 for (int i = 0; i < NUM_TEAMS; ++i)
475 if (teams_mask & BIT(i))
477 balance.m_team_balance_team[i].m_num_players = 0;
484 entity head = find(NULL, classname, teament_name);
487 if (Team_IsValidTeam(head.team))
489 TeamBalance_GetTeam(balance, head.team).m_num_players = 0;
491 head = find(head, classname, teament_name);
495 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
496 if (autocvar_bot_vs_human && AvailableTeams() == 2 && for_whom)
498 if (autocvar_bot_vs_human > 0)
500 // find last team available
501 if (IS_BOT_CLIENT(for_whom))
503 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
505 TeamBalance_BanTeamsExcept(balance, 4);
507 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
509 TeamBalance_BanTeamsExcept(balance, 3);
513 TeamBalance_BanTeamsExcept(balance, 2);
515 // no further cases, we know at least 2 teams exist
519 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
521 TeamBalance_BanTeamsExcept(balance, 1);
523 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
525 TeamBalance_BanTeamsExcept(balance, 2);
529 TeamBalance_BanTeamsExcept(balance, 3);
531 // no further cases, bots have one of the teams
536 // find first team available
537 if (IS_BOT_CLIENT(for_whom))
539 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
541 TeamBalance_BanTeamsExcept(balance, 1);
543 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
545 TeamBalance_BanTeamsExcept(balance, 2);
549 TeamBalance_BanTeamsExcept(balance, 3);
551 // no further cases, we know at least 2 teams exist
555 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
557 TeamBalance_BanTeamsExcept(balance, 4);
559 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
561 TeamBalance_BanTeamsExcept(balance, 3);
565 TeamBalance_BanTeamsExcept(balance, 2);
567 // no further cases, bots have one of the teams
574 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
578 // if player has a forced team, ONLY allow that one
579 for (int i = 1; i <= NUM_TEAMS; ++i)
581 if (for_whom.team_forced == i &&
582 TeamBalance_IsTeamAllowedInternal(balance, i))
584 TeamBalance_BanTeamsExcept(balance, i);
588 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
592 void TeamBalance_Destroy(entity balance)
598 for (int i = 0; i < NUM_TEAMS; ++i)
600 delete(balance.(m_team_balance_team[i]));
605 int TeamBalance_GetAllowedTeams(entity balance)
609 LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
611 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
613 LOG_FATAL("TeamBalance_GetAllowedTeams: "
614 "Team balance entity is not initialized.");
617 for (int i = 1; i <= NUM_TEAMS; ++i)
619 if (TeamBalance_IsTeamAllowedInternal(balance, i))
621 result |= Team_IndexToBit(i);
627 bool TeamBalance_IsTeamAllowed(entity balance, int index)
631 LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
633 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
635 LOG_FATAL("TeamBalance_IsTeamAllowed: "
636 "Team balance entity is not initialized.");
638 if (!Team_IsValidIndex(index))
640 LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
643 return TeamBalance_IsTeamAllowedInternal(balance, index);
646 void TeamBalance_GetTeamCounts(entity balance, entity ignore)
650 LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
652 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
654 LOG_FATAL("TeamBalance_GetTeamCounts: "
655 "Team balance entity is not initialized.");
657 if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
659 // Mutator has overriden the configuration.
660 for (int i = 1; i <= NUM_TEAMS; ++i)
662 entity team_ent = TeamBalance_GetTeamFromIndex(balance, i);
663 if (TeamBalanceTeam_IsAllowed(team_ent))
665 MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, i, ignore);
666 team_ent.m_num_players = M_ARGV(2, float);
667 team_ent.m_num_bots = M_ARGV(3, float);
673 // Manually count all players.
681 // TODO: Reconsider when the player is truly on the team.
682 if (IS_CLIENT(it) || (it.caplayer))
686 else if (Player_HasRealForcedTeam(it))
688 // Do we really need this? Probably not.
689 team_num = Team_IndexToTeam(it.team_forced); // reserve the spot
695 if (!Team_IsValidTeam(team_num))
699 entity team_ent = TeamBalance_GetTeam(balance, team_num);
700 if (!TeamBalanceTeam_IsAllowed(team_ent))
704 ++team_ent.m_num_players;
705 if (IS_BOT_CLIENT(it))
707 ++team_ent.m_num_bots;
712 // if the player who has a forced team has not joined yet, reserve the spot
713 if (autocvar_g_campaign)
715 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
717 entity team_ent = TeamBalance_GetTeamFromIndex(balance,
718 autocvar_g_campaign_forceteam);
719 if (team_ent.m_num_players == team_ent.m_num_bots)
721 ++team_ent.m_num_players;
725 balance.m_team_balance_state = TEAM_BALANCE_TEAM_COUNTS_FILLED;
728 int TeamBalance_GetNumberOfPlayers(entity balance, int index)
732 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
733 "Team balance entity is NULL.");
735 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
737 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
738 "TeamBalance_GetTeamCounts has not been called.");
740 if (!Team_IsValidIndex(index))
742 LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
745 return balance.m_team_balance_team[index - 1].m_num_players;
748 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
752 LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
754 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
756 LOG_FATAL("TeamBalance_FindBestTeam: "
757 "Team balance entity is not initialized.");
759 // count how many players are in each team
762 TeamBalance_GetTeamCounts(balance, player);
766 TeamBalance_GetTeamCounts(balance, NULL);
768 int team_bits = TeamBalance_FindBestTeams(balance, player, true);
771 LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
772 MapInfo_Type_ToString(MapInfo_CurrentGametype()));
774 RandomSelection_Init();
775 for (int i = 1; i <= NUM_TEAMS; ++i)
777 if (team_bits & Team_IndexToBit(i))
779 RandomSelection_AddFloat(i, 1, 1);
782 return RandomSelection_chosen_float;
785 int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
789 LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
791 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
793 LOG_FATAL("TeamBalance_FindBestTeams: "
794 "TeamBalance_GetTeamCounts has not been called.");
796 if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
798 return M_ARGV(1, float);
801 int previous_team = 0;
802 for (int i = 1; i <= NUM_TEAMS; ++i)
804 if (!TeamBalance_IsTeamAllowedInternal(balance, i))
808 if (previous_team == 0)
810 team_bits = Team_IndexToBit(i);
814 int compare = TeamBalance_CompareTeams(balance, i, previous_team,
816 if (compare == TEAMS_COMPARE_LESS)
818 team_bits = Team_IndexToBit(i);
822 if (compare == TEAMS_COMPARE_EQUAL)
824 team_bits |= Team_IndexToBit(i);
831 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
832 entity player, bool use_score)
836 LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
838 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
840 LOG_FATAL("TeamBalance_CompareTeams: "
841 "TeamBalance_GetTeamCounts has not been called.");
843 if (!Team_IsValidIndex(team_index_a))
845 LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
848 if (!Team_IsValidIndex(team_index_b))
850 LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
853 if (team_index_a == team_index_b)
855 return TEAMS_COMPARE_EQUAL;
857 entity team_a = TeamBalance_GetTeamFromIndex(balance, team_index_a);
858 entity team_b = TeamBalance_GetTeamFromIndex(balance, team_index_b);
859 return TeamBalance_CompareTeamsInternal(team_a, team_b, player, use_score);
862 void TeamBalance_AutoBalanceBots()
864 // checks disabled because we always want auto-balanced bots
865 //if (!(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance))
868 entity balance = TeamBalance_CheckAllowedTeams(NULL);
869 TeamBalance_GetTeamCounts(balance, NULL);
870 int smallest_team_index = 0;
871 int smallest_team_player_count = 0;
872 for (int i = 1; i <= NUM_TEAMS; ++i)
874 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
875 if (!TeamBalanceTeam_IsAllowed(team_))
879 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
880 if (smallest_team_index == 0)
882 smallest_team_index = i;
883 smallest_team_player_count = playercount;
885 else if (playercount < smallest_team_player_count)
887 smallest_team_index = i;
888 smallest_team_player_count = playercount;
891 //PrintToChatAll(sprintf("Smallest team: %f", smallest_team_index));
892 //PrintToChatAll(sprintf("Smallest team players: %f", smallest_team_player_count));
893 entity switchable_bot = NULL;
894 int teams = BITS(NUM_TEAMS);
897 int largest_team_index = TeamBalance_GetLargestTeamIndex(balance,
899 if (smallest_team_index == largest_team_index)
901 TeamBalance_Destroy(balance);
904 entity largest_team = TeamBalance_GetTeamFromIndex(balance,
906 int largest_team_player_count = TeamBalanceTeam_GetNumberOfPlayers(
908 if (largest_team_player_count - smallest_team_player_count < 2)
910 TeamBalance_Destroy(balance);
913 //PrintToChatAll(sprintf("Largest team: %f", largest_team_index));
914 //PrintToChatAll(sprintf("Largest team players: %f", largest_team_player_count));
915 switchable_bot = TeamBalance_GetPlayerForTeamSwitch(largest_team_index,
916 smallest_team_index, true);
917 if (switchable_bot != NULL)
921 teams &= ~Team_IndexToBit(largest_team_index);
923 TeamBalance_Destroy(balance);
924 if (switchable_bot == NULL)
926 //PrintToChatAll("No bot found after searching through all the teams");
929 SetPlayerTeam(switchable_bot, smallest_team_index, TEAM_CHANGE_AUTO);
932 int TeamBalance_GetLargestTeamIndex(entity balance, int teams)
934 int largest_team_index = 0;
935 int largest_team_player_count = 0;
936 for (int i = 1; i <= NUM_TEAMS; ++i)
938 if (!(Team_IndexToBit(i) & teams))
942 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
943 if (!TeamBalanceTeam_IsAllowed(team_))
947 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
948 if (largest_team_index == 0)
950 largest_team_index = i;
951 largest_team_player_count = playercount;
953 else if (playercount > largest_team_player_count)
955 largest_team_index = i;
956 largest_team_player_count = playercount;
959 return largest_team_index;
962 entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index,
963 int destination_team_index, bool is_bot)
965 if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, source_team_index,
966 destination_team_index, is_bot))
968 return M_ARGV(3, entity);
970 entity lowest_player = NULL;
971 float lowest_score = FLOAT_MAX;
972 FOREACH_CLIENT(Entity_GetTeamIndex(it) == source_team_index,
974 if (IS_BOT_CLIENT(it) != is_bot)
978 float temp_score = PlayerScore_Get(it, SP_SCORE);
979 if (temp_score >= lowest_score)
983 //PrintToChatAll(sprintf(
984 // "Found %s with lowest score, checking allowed teams", it.netname));
985 entity balance = TeamBalance_CheckAllowedTeams(it);
986 if (TeamBalance_IsTeamAllowed(balance, source_team_index))
988 //PrintToChatAll("Allowed");
990 lowest_score = temp_score;
994 //PrintToChatAll("Not allowed");
996 TeamBalance_Destroy(balance);
998 return lowest_player;
1001 void LogTeamChange(float player_id, float team_number, int type)
1003 if (!autocvar_sv_eventlog)
1011 GameLogEcho(sprintf(":team:%d:%d:%d", player_id, team_number, type));
1014 void KillPlayerForTeamChange(entity player)
1016 if (IS_DEAD(player))
1020 if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
1024 Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
1025 player.origin, '0 0 0');
1028 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
1030 return balance.m_team_balance_team[index - 1].m_num_players !=
1034 void TeamBalance_BanTeamsExcept(entity balance, int index)
1036 for (int i = 1; i <= NUM_TEAMS; ++i)
1040 balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
1045 entity TeamBalance_GetTeamFromIndex(entity balance, int index)
1047 if (!Team_IsValidIndex(index))
1049 LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
1051 return balance.m_team_balance_team[index - 1];
1054 entity TeamBalance_GetTeam(entity balance, int team_num)
1056 return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
1059 bool TeamBalanceTeam_IsAllowed(entity team_ent)
1061 return team_ent.m_num_players != TEAM_NOT_ALLOWED;
1064 int TeamBalanceTeam_GetNumberOfPlayers(entity team_ent)
1066 return team_ent.m_num_players;
1069 int TeamBalanceTeam_GetNumberOfBots(entity team_ent)
1071 return team_ent.m_num_bots;
1074 int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
1075 entity player, bool use_score)
1077 if (team_a == team_b)
1079 return TEAMS_COMPARE_EQUAL;
1081 if (!TeamBalanceTeam_IsAllowed(team_a) ||
1082 !TeamBalanceTeam_IsAllowed(team_b))
1084 return TEAMS_COMPARE_INVALID;
1086 int num_players_team_a = team_a.m_num_players;
1087 int num_players_team_b = team_b.m_num_players;
1088 if (IS_REAL_CLIENT(player) && bots_would_leave)
1090 num_players_team_a -= team_a.m_num_bots;
1091 num_players_team_b -= team_b.m_num_bots;
1093 if (num_players_team_a < num_players_team_b)
1095 return TEAMS_COMPARE_LESS;
1097 if (num_players_team_a > num_players_team_b)
1099 return TEAMS_COMPARE_GREATER;
1103 return TEAMS_COMPARE_EQUAL;
1105 if (team_a.m_team_score < team_b.m_team_score)
1107 return TEAMS_COMPARE_LESS;
1109 if (team_a.m_team_score > team_b.m_team_score)
1111 return TEAMS_COMPARE_GREATER;
1113 return TEAMS_COMPARE_EQUAL;
1116 void SV_ChangeTeam(entity player, int new_color)
1120 SetPlayerColors(player, new_color);
1122 // TODO: Should we really bother with this?
1123 if(!IS_CLIENT(player))
1125 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
1126 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING,
1134 Player_SetTeamIndexChecked(player, Team_TeamToIndex((new_color & 0x0F) +