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/bot/default/cvars.qh>
8 #include <server/campaign.qh>
9 #include <server/client.qh>
10 #include <server/command/vote.qh>
11 #include <server/damage.qh>
12 #include <server/gamelog.qh>
13 #include <server/mutators/_mod.qh>
14 #include <server/race.qh>
15 #include <server/scores.qh>
16 #include <server/scores_rules.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 .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_owned_items; ///< Number of items 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.
49 if (g_team_entities[0])
51 for (int i = 0; i < NUM_TEAMS; ++i)
53 g_team_entities[i] = new_pure(team_entity);
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_GetWinnerAliveTeam()
98 for (int i = 0; i < NUM_TEAMS; ++i)
100 if (g_team_entities[i].m_num_players_alive > 0)
104 winner = Team_IndexToTeam(i + 1);
107 return (winner ? winner : -1);
110 int Team_GetNumberOfAliveTeams()
113 for (int i = 0; i < NUM_TEAMS; ++i)
115 if (g_team_entities[i].m_num_players_alive > 0)
123 int Team_GetWinnerTeam_WithOwnedItems(int min_control_points)
126 for (int i = 0; i < NUM_TEAMS; ++i)
128 if (g_team_entities[i].m_num_owned_items >= min_control_points)
132 winner = Team_IndexToTeam(i + 1);
135 return (winner ? winner : -1);
138 int Team_GetNumberOfOwnedItems(entity team_ent)
140 return team_ent.m_num_owned_items;
143 void Team_SetNumberOfOwnedItems(entity team_ent, int number)
145 team_ent.m_num_owned_items = number;
148 int Team_GetNumberOfTeamsWithOwnedItems()
151 for (int i = 0; i < NUM_TEAMS; ++i)
153 if (g_team_entities[i].m_num_owned_items > 0)
161 void setcolor(entity this, int clr)
164 this.clientcolors = clr;
165 this.team = (clr & 15) + 1;
167 builtin_setcolor(this, clr);
171 bool Entity_HasValidTeam(entity this)
173 return Team_IsValidTeam(this.team);
176 int Entity_GetTeamIndex(entity this)
178 return Team_TeamToIndex(this.team);
181 entity Entity_GetTeam(entity this)
183 int index = Entity_GetTeamIndex(this);
184 if (!Team_IsValidIndex(index))
188 return Team_GetTeamFromIndex(index);
191 void SetPlayerColors(entity player, float _color)
193 float pants = _color & 0x0F;
194 float shirt = _color & 0xF0;
197 setcolor(player, 16 * pants + pants);
201 setcolor(player, shirt + pants);
205 bool Player_SetTeamIndex(entity player, int index)
207 int new_team = Team_IndexToTeam(index);
208 if (player.team == new_team)
212 // This is important when players join the game and one of their
213 // color matches the team color while other doesn't. For example
214 // [BOT]Lion: color 0 4.
215 SetPlayerColors(player, new_team - 1);
219 int old_index = Team_TeamToIndex(player.team);
220 if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, old_index, index) == true)
222 // Mutator has blocked team change.
231 SetPlayerColors(player, new_team - 1);
233 MUTATOR_CALLHOOK(Player_ChangedTeam, player, old_index, index);
237 bool SetPlayerTeam(entity player, int team_index, int type)
239 int old_team_index = Entity_GetTeamIndex(player);
241 if (!Player_SetTeamIndex(player, team_index))
244 LogTeamChange(player.playerid, player.team, type);
246 if (team_index != old_team_index)
248 KillPlayerForTeamChange(player);
249 PlayerScore_Clear(player); // works only in game modes without teams
251 if (!IS_BOT_CLIENT(player))
252 TeamBalance_AutoBalanceBots();
254 if (team_index != -1)
255 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(player.team, INFO_JOIN_PLAY_TEAM), player.netname);
258 if (team_index == -1)
260 if (autocvar_sv_maxidle_playertospectator > 0 && CS(player).idlekick_lasttimeleft)
262 // this done here so it happens even when manually speccing during the countdown
263 Kill_Notification(NOTIF_ONE_ONLY, player, MSG_CENTER, CPID_IDLING);
264 CS(player).idlekick_lasttimeleft = 0;
266 else if (!CS(player).just_joined && player.frags != FRAGS_SPECTATOR)
268 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE, player.netname);
275 void Player_SetTeamIndexChecked(entity player, int team_index)
281 if (!Team_IsValidIndex(team_index))
285 if ((autocvar_g_campaign) || (autocvar_g_changeteam_banned &&
286 CS(player).wasplayer))
288 Send_Notification(NOTIF_ONE, player, MSG_INFO,
289 INFO_TEAMCHANGE_NOTALLOWED);
292 entity balance = TeamBalance_CheckAllowedTeams(player);
293 if (team_index == 1 && !TeamBalance_IsTeamAllowedInternal(balance, 1))
297 if (team_index == 4 && !TeamBalance_IsTeamAllowedInternal(balance, 4))
301 if (team_index == 3 && !TeamBalance_IsTeamAllowedInternal(balance, 3))
305 if (team_index == 2 && !TeamBalance_IsTeamAllowedInternal(balance, 2))
309 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
310 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
312 TeamBalance_GetTeamCounts(balance, player);
313 if ((Team_IndexToBit(team_index) & TeamBalance_FindBestTeams(balance,
314 player, false)) == 0)
316 Send_Notification(NOTIF_ONE, player, MSG_INFO,
317 INFO_TEAMCHANGE_LARGERTEAM);
318 TeamBalance_Destroy(balance);
322 TeamBalance_Destroy(balance);
323 SetPlayerTeam(player, team_index, TEAM_CHANGE_MANUAL);
326 bool MoveToTeam(entity client, int team_index, int type)
328 //PrintToChatAll(sprintf("MoveToTeam: %s, %f", client.netname, team_index));
329 int lockteams_backup = lockteams; // backup any team lock
330 lockteams = 0; // disable locked teams
331 if (!SetPlayerTeam(client, team_index, type))
333 lockteams = lockteams_backup; // restore the team lock
336 lockteams = lockteams_backup; // restore the team lock
340 bool Player_HasRealForcedTeam(entity player)
342 return player.team_forced > TEAM_FORCE_DEFAULT;
345 int Player_GetForcedTeamIndex(entity player)
347 return player.team_forced;
350 void Player_SetForcedTeamIndex(entity player, int team_index)
354 case TEAM_FORCE_SPECTATOR:
355 case TEAM_FORCE_DEFAULT:
357 player.team_forced = team_index;
362 if (!Team_IsValidIndex(team_index))
364 LOG_FATAL("Player_SetForcedTeamIndex: Invalid team index.");
368 player.team_forced = team_index;
375 void Player_DetermineForcedTeam(entity player)
377 if (autocvar_g_campaign)
379 if (IS_REAL_CLIENT(player)) // only players, not bots
381 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
383 player.team_forced = autocvar_g_campaign_forceteam;
387 player.team_forced = TEAM_FORCE_DEFAULT;
391 else if (PlayerInList(player, autocvar_g_forced_team_red))
393 player.team_forced = 1;
395 else if (PlayerInList(player, autocvar_g_forced_team_blue))
397 player.team_forced = 2;
399 else if (PlayerInList(player, autocvar_g_forced_team_yellow))
401 player.team_forced = 3;
403 else if (PlayerInList(player, autocvar_g_forced_team_pink))
405 player.team_forced = 4;
409 switch (autocvar_g_forced_team_otherwise)
413 player.team_forced = 1;
418 player.team_forced = 2;
423 player.team_forced = 3;
428 player.team_forced = 4;
434 player.team_forced = TEAM_FORCE_SPECTATOR;
439 player.team_forced = TEAM_FORCE_DEFAULT;
444 if (!teamplay && Player_HasRealForcedTeam(player))
446 player.team_forced = TEAM_FORCE_DEFAULT;
450 void TeamBalance_JoinBestTeam(entity player)
452 //PrintToChatAll(sprintf("TeamBalance_JoinBestTeam: %s", player.netname));
457 if (player.bot_forced_team)
461 entity balance = TeamBalance_CheckAllowedTeams(player);
462 if (Player_HasRealForcedTeam(player))
464 int forced_team_index = player.team_forced;
465 bool is_team_allowed = TeamBalance_IsTeamAllowedInternal(balance,
467 TeamBalance_Destroy(balance);
468 if (!is_team_allowed)
472 if (!SetPlayerTeam(player, forced_team_index, TEAM_CHANGE_AUTO))
478 int best_team_index = TeamBalance_FindBestTeam(balance, player, true);
479 TeamBalance_Destroy(balance);
480 if (!SetPlayerTeam(player, best_team_index, TEAM_CHANGE_AUTO))
486 entity TeamBalance_CheckAllowedTeams(entity for_whom)
488 entity balance = spawn();
489 for (int i = 0; i < NUM_TEAMS; ++i)
491 entity team_ent = balance.m_team_balance_team[i] = spawn();
492 team_ent.m_team_score = g_team_entities[i].m_team_score;
493 team_ent.m_num_players = TEAM_NOT_ALLOWED;
494 team_ent.m_num_bots = 0;
496 setthink(balance, TeamBalance_Destroy);
497 balance.nextthink = time;
500 string teament_name = string_null;
501 bool mutator_returnvalue = MUTATOR_CALLHOOK(TeamBalance_CheckAllowedTeams,
502 teams_mask, teament_name, for_whom);
503 teams_mask = M_ARGV(0, float);
504 teament_name = M_ARGV(1, string);
505 if (mutator_returnvalue)
507 for (int i = 0; i < NUM_TEAMS; ++i)
509 if (teams_mask & BIT(i))
511 balance.m_team_balance_team[i].m_num_players = 0;
518 entity head = find(NULL, classname, teament_name);
521 if (Team_IsValidTeam(head.team))
523 TeamBalance_GetTeam(balance, head.team).m_num_players = 0;
525 head = find(head, classname, teament_name);
529 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
530 if (autocvar_bot_vs_human && AvailableTeams() == 2 && for_whom)
532 if (autocvar_bot_vs_human > 0)
534 // find last team available
535 if (IS_BOT_CLIENT(for_whom))
537 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
539 TeamBalance_BanTeamsExcept(balance, 4);
541 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
543 TeamBalance_BanTeamsExcept(balance, 3);
547 TeamBalance_BanTeamsExcept(balance, 2);
549 // no further cases, we know at least 2 teams exist
553 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
555 TeamBalance_BanTeamsExcept(balance, 1);
557 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
559 TeamBalance_BanTeamsExcept(balance, 2);
563 TeamBalance_BanTeamsExcept(balance, 3);
565 // no further cases, bots have one of the teams
570 // find first team available
571 if (IS_BOT_CLIENT(for_whom))
573 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
575 TeamBalance_BanTeamsExcept(balance, 1);
577 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
579 TeamBalance_BanTeamsExcept(balance, 2);
583 TeamBalance_BanTeamsExcept(balance, 3);
585 // no further cases, we know at least 2 teams exist
589 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
591 TeamBalance_BanTeamsExcept(balance, 4);
593 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
595 TeamBalance_BanTeamsExcept(balance, 3);
599 TeamBalance_BanTeamsExcept(balance, 2);
601 // no further cases, bots have one of the teams
608 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
612 // if player has a forced team, ONLY allow that one
613 for (int i = 1; i <= NUM_TEAMS; ++i)
615 if (for_whom.team_forced == i &&
616 TeamBalance_IsTeamAllowedInternal(balance, i))
618 TeamBalance_BanTeamsExcept(balance, i);
622 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
626 void TeamBalance_Destroy(entity balance)
632 for (int i = 0; i < NUM_TEAMS; ++i)
634 delete(balance.(m_team_balance_team[i]));
639 int TeamBalance_GetAllowedTeams(entity balance)
643 LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
645 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
647 LOG_FATAL("TeamBalance_GetAllowedTeams: "
648 "Team balance entity is not initialized.");
651 for (int i = 1; i <= NUM_TEAMS; ++i)
653 if (TeamBalance_IsTeamAllowedInternal(balance, i))
655 result |= Team_IndexToBit(i);
661 bool TeamBalance_IsTeamAllowed(entity balance, int index)
665 LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
667 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
669 LOG_FATAL("TeamBalance_IsTeamAllowed: "
670 "Team balance entity is not initialized.");
672 if (!Team_IsValidIndex(index))
674 LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
677 return TeamBalance_IsTeamAllowedInternal(balance, index);
680 void TeamBalance_GetTeamCounts(entity balance, entity ignore)
684 LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
686 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
688 LOG_FATAL("TeamBalance_GetTeamCounts: "
689 "Team balance entity is not initialized.");
691 if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
693 // Mutator has overriden the configuration.
694 for (int i = 1; i <= NUM_TEAMS; ++i)
696 entity team_ent = TeamBalance_GetTeamFromIndex(balance, i);
697 if (TeamBalanceTeam_IsAllowed(team_ent))
699 MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, i, ignore);
700 team_ent.m_num_players = M_ARGV(2, float);
701 team_ent.m_num_bots = M_ARGV(3, float);
707 // Manually count all players.
715 // TODO: Reconsider when the player is truly on the team.
716 if (IS_CLIENT(it) || INGAME(it))
720 else if (Player_HasRealForcedTeam(it))
722 // Do we really need this? Probably not.
723 team_num = Team_IndexToTeam(it.team_forced); // reserve the spot
729 if (!Team_IsValidTeam(team_num))
733 entity team_ent = TeamBalance_GetTeam(balance, team_num);
734 if (!TeamBalanceTeam_IsAllowed(team_ent))
738 ++team_ent.m_num_players;
739 if (IS_BOT_CLIENT(it))
741 ++team_ent.m_num_bots;
746 // if the player who has a forced team has not joined yet, reserve the spot
747 if (autocvar_g_campaign)
749 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
751 entity team_ent = TeamBalance_GetTeamFromIndex(balance,
752 autocvar_g_campaign_forceteam);
753 if (team_ent.m_num_players == team_ent.m_num_bots)
755 ++team_ent.m_num_players;
759 balance.m_team_balance_state = TEAM_BALANCE_TEAM_COUNTS_FILLED;
762 int TeamBalance_GetNumberOfPlayers(entity balance, int index)
766 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
767 "Team balance entity is NULL.");
769 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
771 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
772 "TeamBalance_GetTeamCounts has not been called.");
774 if (!Team_IsValidIndex(index))
776 LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
779 return balance.m_team_balance_team[index - 1].m_num_players;
782 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
786 LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
788 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
790 LOG_FATAL("TeamBalance_FindBestTeam: "
791 "Team balance entity is not initialized.");
793 // count how many players are in each team
796 TeamBalance_GetTeamCounts(balance, player);
800 TeamBalance_GetTeamCounts(balance, NULL);
802 int team_bits = TeamBalance_FindBestTeams(balance, player, true);
805 LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
806 MapInfo_Type_ToString(MapInfo_CurrentGametype()));
808 RandomSelection_Init();
809 for (int i = 1; i <= NUM_TEAMS; ++i)
811 if (team_bits & Team_IndexToBit(i))
813 RandomSelection_AddFloat(i, 1, 1);
816 return RandomSelection_chosen_float;
819 int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
823 LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
825 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
827 LOG_FATAL("TeamBalance_FindBestTeams: "
828 "TeamBalance_GetTeamCounts has not been called.");
830 if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
832 return M_ARGV(1, float);
835 int previous_team = 0;
836 for (int i = 1; i <= NUM_TEAMS; ++i)
838 if (!TeamBalance_IsTeamAllowedInternal(balance, i))
842 if (previous_team == 0)
844 team_bits = Team_IndexToBit(i);
848 int compare = TeamBalance_CompareTeams(balance, i, previous_team,
850 if (compare == TEAMS_COMPARE_LESS)
852 team_bits = Team_IndexToBit(i);
856 if (compare == TEAMS_COMPARE_EQUAL)
858 team_bits |= Team_IndexToBit(i);
865 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
866 entity player, bool use_score)
870 LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
872 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
874 LOG_FATAL("TeamBalance_CompareTeams: "
875 "TeamBalance_GetTeamCounts has not been called.");
877 if (!Team_IsValidIndex(team_index_a))
879 LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
882 if (!Team_IsValidIndex(team_index_b))
884 LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
887 if (team_index_a == team_index_b)
889 return TEAMS_COMPARE_EQUAL;
891 entity team_a = TeamBalance_GetTeamFromIndex(balance, team_index_a);
892 entity team_b = TeamBalance_GetTeamFromIndex(balance, team_index_b);
893 return TeamBalance_CompareTeamsInternal(team_a, team_b, player, use_score);
896 void TeamBalance_AutoBalanceBots()
898 // checks disabled because we always want auto-balanced bots
899 //if (!(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance))
902 entity balance = TeamBalance_CheckAllowedTeams(NULL);
903 TeamBalance_GetTeamCounts(balance, NULL);
904 int smallest_team_index = 0;
905 int smallest_team_player_count = 0;
906 for (int i = 1; i <= NUM_TEAMS; ++i)
908 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
909 if (!TeamBalanceTeam_IsAllowed(team_))
913 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
914 if (smallest_team_index == 0)
916 smallest_team_index = i;
917 smallest_team_player_count = playercount;
919 else if (playercount < smallest_team_player_count)
921 smallest_team_index = i;
922 smallest_team_player_count = playercount;
925 //PrintToChatAll(sprintf("Smallest team: %f", smallest_team_index));
926 //PrintToChatAll(sprintf("Smallest team players: %f", smallest_team_player_count));
927 entity switchable_bot = NULL;
928 int teams = BITS(NUM_TEAMS);
931 int largest_team_index = TeamBalance_GetLargestTeamIndex(balance,
933 if (smallest_team_index == largest_team_index)
935 TeamBalance_Destroy(balance);
938 entity largest_team = TeamBalance_GetTeamFromIndex(balance,
940 int largest_team_player_count = TeamBalanceTeam_GetNumberOfPlayers(
942 if (largest_team_player_count - smallest_team_player_count < 2)
944 TeamBalance_Destroy(balance);
947 //PrintToChatAll(sprintf("Largest team: %f", largest_team_index));
948 //PrintToChatAll(sprintf("Largest team players: %f", largest_team_player_count));
949 switchable_bot = TeamBalance_GetPlayerForTeamSwitch(largest_team_index,
950 smallest_team_index, true);
951 if (switchable_bot != NULL)
955 teams &= ~Team_IndexToBit(largest_team_index);
957 TeamBalance_Destroy(balance);
958 if (switchable_bot == NULL)
960 //PrintToChatAll("No bot found after searching through all the teams");
963 SetPlayerTeam(switchable_bot, smallest_team_index, TEAM_CHANGE_AUTO);
966 int TeamBalance_GetLargestTeamIndex(entity balance, int teams)
968 int largest_team_index = 0;
969 int largest_team_player_count = 0;
970 for (int i = 1; i <= NUM_TEAMS; ++i)
972 if (!(Team_IndexToBit(i) & teams))
976 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
977 if (!TeamBalanceTeam_IsAllowed(team_))
981 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
982 if (largest_team_index == 0)
984 largest_team_index = i;
985 largest_team_player_count = playercount;
987 else if (playercount > largest_team_player_count)
989 largest_team_index = i;
990 largest_team_player_count = playercount;
993 return largest_team_index;
996 entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index,
997 int destination_team_index, bool is_bot)
999 if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, source_team_index,
1000 destination_team_index, is_bot))
1002 return M_ARGV(3, entity);
1004 entity lowest_player = NULL;
1005 float lowest_score = FLOAT_MAX;
1006 FOREACH_CLIENT(Entity_GetTeamIndex(it) == source_team_index,
1008 if (IS_BOT_CLIENT(it) != is_bot)
1012 float temp_score = PlayerScore_Get(it, SP_SCORE);
1013 if (temp_score >= lowest_score)
1017 //PrintToChatAll(sprintf(
1018 // "Found %s with lowest score, checking allowed teams", it.netname));
1019 entity balance = TeamBalance_CheckAllowedTeams(it);
1020 if (TeamBalance_IsTeamAllowed(balance, source_team_index))
1022 //PrintToChatAll("Allowed");
1024 lowest_score = temp_score;
1028 //PrintToChatAll("Not allowed");
1030 TeamBalance_Destroy(balance);
1032 return lowest_player;
1035 void LogTeamChange(float player_id, float team_number, int type)
1037 if (!autocvar_sv_eventlog)
1045 GameLogEcho(sprintf(":team:%d:%d:%d", player_id, team_number, type));
1048 void KillPlayerForTeamChange(entity player)
1050 if (IS_DEAD(player))
1054 if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
1058 Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
1059 player.origin, '0 0 0');
1062 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
1064 return balance.m_team_balance_team[index - 1].m_num_players !=
1068 void TeamBalance_BanTeamsExcept(entity balance, int index)
1070 for (int i = 1; i <= NUM_TEAMS; ++i)
1074 balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
1079 entity TeamBalance_GetTeamFromIndex(entity balance, int index)
1081 if (!Team_IsValidIndex(index))
1083 LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
1085 return balance.m_team_balance_team[index - 1];
1088 entity TeamBalance_GetTeam(entity balance, int team_num)
1090 return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
1093 bool TeamBalanceTeam_IsAllowed(entity team_ent)
1095 return team_ent.m_num_players != TEAM_NOT_ALLOWED;
1098 int TeamBalanceTeam_GetNumberOfPlayers(entity team_ent)
1100 return team_ent.m_num_players;
1103 int TeamBalanceTeam_GetNumberOfBots(entity team_ent)
1105 return team_ent.m_num_bots;
1108 int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
1109 entity player, bool use_score)
1111 if (team_a == team_b)
1113 return TEAMS_COMPARE_EQUAL;
1115 if (!TeamBalanceTeam_IsAllowed(team_a) ||
1116 !TeamBalanceTeam_IsAllowed(team_b))
1118 return TEAMS_COMPARE_INVALID;
1120 int num_players_team_a = team_a.m_num_players;
1121 int num_players_team_b = team_b.m_num_players;
1122 if (IS_REAL_CLIENT(player) && bots_would_leave)
1124 num_players_team_a -= team_a.m_num_bots;
1125 num_players_team_b -= team_b.m_num_bots;
1127 if (num_players_team_a < num_players_team_b)
1129 return TEAMS_COMPARE_LESS;
1131 if (num_players_team_a > num_players_team_b)
1133 return TEAMS_COMPARE_GREATER;
1137 return TEAMS_COMPARE_EQUAL;
1139 if (team_a.m_team_score < team_b.m_team_score)
1141 return TEAMS_COMPARE_LESS;
1143 if (team_a.m_team_score > team_b.m_team_score)
1145 return TEAMS_COMPARE_GREATER;
1147 return TEAMS_COMPARE_EQUAL;
1150 void SV_ChangeTeam(entity player, int new_color)
1154 SetPlayerColors(player, new_color);
1156 if(!IS_CLIENT(player))
1164 Player_SetTeamIndexChecked(player, Team_TeamToIndex((new_color & 0x0F) + 1));