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
250 CS(player).parm_idlesince = time;
252 if (!IS_BOT_CLIENT(player))
253 TeamBalance_AutoBalanceBots();
255 if (team_index != -1)
256 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(player.team, INFO_JOIN_PLAY_TEAM), player.netname);
259 if (team_index == -1)
261 if (autocvar_sv_maxidle_playertospectator > 0 && CS(player).idlekick_lasttimeleft)
263 // this done here so it happens even when manually speccing during the countdown
264 Kill_Notification(NOTIF_ONE_ONLY, player, MSG_CENTER, CPID_IDLING);
265 CS(player).idlekick_lasttimeleft = 0;
267 else if (!CS(player).just_joined && player.frags != FRAGS_SPECTATOR)
269 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE, player.netname);
276 void Player_SetTeamIndexChecked(entity player, int team_index)
282 if (!Team_IsValidIndex(team_index))
286 if ((autocvar_g_campaign) || (autocvar_g_changeteam_banned &&
287 CS(player).wasplayer))
289 Send_Notification(NOTIF_ONE, player, MSG_INFO,
290 INFO_TEAMCHANGE_NOTALLOWED);
293 entity balance = TeamBalance_CheckAllowedTeams(player);
294 if (team_index == 1 && !TeamBalance_IsTeamAllowedInternal(balance, 1))
298 if (team_index == 4 && !TeamBalance_IsTeamAllowedInternal(balance, 4))
302 if (team_index == 3 && !TeamBalance_IsTeamAllowedInternal(balance, 3))
306 if (team_index == 2 && !TeamBalance_IsTeamAllowedInternal(balance, 2))
310 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
311 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
313 TeamBalance_GetTeamCounts(balance, player);
314 if ((Team_IndexToBit(team_index) & TeamBalance_FindBestTeams(balance,
315 player, false)) == 0)
317 Send_Notification(NOTIF_ONE, player, MSG_INFO,
318 INFO_TEAMCHANGE_LARGERTEAM);
319 TeamBalance_Destroy(balance);
323 TeamBalance_Destroy(balance);
324 SetPlayerTeam(player, team_index, TEAM_CHANGE_MANUAL);
327 bool MoveToTeam(entity client, int team_index, int type)
329 //PrintToChatAll(sprintf("MoveToTeam: %s, %f", client.netname, team_index));
330 int lockteams_backup = lockteams; // backup any team lock
331 lockteams = 0; // disable locked teams
332 if (!SetPlayerTeam(client, team_index, type))
334 lockteams = lockteams_backup; // restore the team lock
337 lockteams = lockteams_backup; // restore the team lock
341 bool Player_HasRealForcedTeam(entity player)
343 return player.team_forced > TEAM_FORCE_DEFAULT;
346 int Player_GetForcedTeamIndex(entity player)
348 return player.team_forced;
351 void Player_SetForcedTeamIndex(entity player, int team_index)
355 case TEAM_FORCE_SPECTATOR:
356 case TEAM_FORCE_DEFAULT:
358 player.team_forced = team_index;
363 if (!Team_IsValidIndex(team_index))
365 LOG_FATAL("Player_SetForcedTeamIndex: Invalid team index.");
369 player.team_forced = team_index;
376 void Player_DetermineForcedTeam(entity player)
378 if (autocvar_g_campaign)
380 if (IS_REAL_CLIENT(player)) // only players, not bots
382 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
384 player.team_forced = autocvar_g_campaign_forceteam;
388 player.team_forced = TEAM_FORCE_DEFAULT;
392 else if (PlayerInList(player, autocvar_g_forced_team_red))
394 player.team_forced = 1;
396 else if (PlayerInList(player, autocvar_g_forced_team_blue))
398 player.team_forced = 2;
400 else if (PlayerInList(player, autocvar_g_forced_team_yellow))
402 player.team_forced = 3;
404 else if (PlayerInList(player, autocvar_g_forced_team_pink))
406 player.team_forced = 4;
410 switch (autocvar_g_forced_team_otherwise)
414 player.team_forced = 1;
419 player.team_forced = 2;
424 player.team_forced = 3;
429 player.team_forced = 4;
435 player.team_forced = TEAM_FORCE_SPECTATOR;
440 player.team_forced = TEAM_FORCE_DEFAULT;
445 if (!teamplay && Player_HasRealForcedTeam(player))
447 player.team_forced = TEAM_FORCE_DEFAULT;
451 void TeamBalance_JoinBestTeam(entity player)
453 //PrintToChatAll(sprintf("TeamBalance_JoinBestTeam: %s", player.netname));
458 if (player.bot_forced_team)
462 entity balance = TeamBalance_CheckAllowedTeams(player);
463 if (Player_HasRealForcedTeam(player))
465 int forced_team_index = player.team_forced;
466 bool is_team_allowed = TeamBalance_IsTeamAllowedInternal(balance,
468 TeamBalance_Destroy(balance);
469 if (!is_team_allowed)
473 if (!SetPlayerTeam(player, forced_team_index, TEAM_CHANGE_AUTO))
479 int best_team_index = TeamBalance_FindBestTeam(balance, player, true);
480 TeamBalance_Destroy(balance);
481 if (!SetPlayerTeam(player, best_team_index, TEAM_CHANGE_AUTO))
487 entity TeamBalance_CheckAllowedTeams(entity for_whom)
489 entity balance = spawn();
490 for (int i = 0; i < NUM_TEAMS; ++i)
492 entity team_ent = balance.m_team_balance_team[i] = spawn();
493 team_ent.m_team_score = g_team_entities[i].m_team_score;
494 team_ent.m_num_players = TEAM_NOT_ALLOWED;
495 team_ent.m_num_bots = 0;
497 setthink(balance, TeamBalance_Destroy);
498 balance.nextthink = time;
501 string teament_name = string_null;
502 bool mutator_returnvalue = MUTATOR_CALLHOOK(TeamBalance_CheckAllowedTeams,
503 teams_mask, teament_name, for_whom);
504 teams_mask = M_ARGV(0, float);
505 teament_name = M_ARGV(1, string);
506 if (mutator_returnvalue)
508 for (int i = 0; i < NUM_TEAMS; ++i)
510 if (teams_mask & BIT(i))
512 balance.m_team_balance_team[i].m_num_players = 0;
519 entity head = find(NULL, classname, teament_name);
522 if (Team_IsValidTeam(head.team))
524 TeamBalance_GetTeam(balance, head.team).m_num_players = 0;
526 head = find(head, classname, teament_name);
530 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
531 if (autocvar_bot_vs_human && AvailableTeams() == 2 && for_whom)
533 if (autocvar_bot_vs_human > 0)
535 // find last team available
536 if (IS_BOT_CLIENT(for_whom))
538 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
540 TeamBalance_BanTeamsExcept(balance, 4);
542 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
544 TeamBalance_BanTeamsExcept(balance, 3);
548 TeamBalance_BanTeamsExcept(balance, 2);
550 // no further cases, we know at least 2 teams exist
554 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
556 TeamBalance_BanTeamsExcept(balance, 1);
558 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
560 TeamBalance_BanTeamsExcept(balance, 2);
564 TeamBalance_BanTeamsExcept(balance, 3);
566 // no further cases, bots have one of the teams
571 // find first team available
572 if (IS_BOT_CLIENT(for_whom))
574 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
576 TeamBalance_BanTeamsExcept(balance, 1);
578 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
580 TeamBalance_BanTeamsExcept(balance, 2);
584 TeamBalance_BanTeamsExcept(balance, 3);
586 // no further cases, we know at least 2 teams exist
590 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
592 TeamBalance_BanTeamsExcept(balance, 4);
594 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
596 TeamBalance_BanTeamsExcept(balance, 3);
600 TeamBalance_BanTeamsExcept(balance, 2);
602 // no further cases, bots have one of the teams
609 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
613 // if player has a forced team, ONLY allow that one
614 for (int i = 1; i <= NUM_TEAMS; ++i)
616 if (for_whom.team_forced == i &&
617 TeamBalance_IsTeamAllowedInternal(balance, i))
619 TeamBalance_BanTeamsExcept(balance, i);
623 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
627 void TeamBalance_Destroy(entity balance)
633 for (int i = 0; i < NUM_TEAMS; ++i)
635 delete(balance.(m_team_balance_team[i]));
640 int TeamBalance_GetAllowedTeams(entity balance)
644 LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
646 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
648 LOG_FATAL("TeamBalance_GetAllowedTeams: "
649 "Team balance entity is not initialized.");
652 for (int i = 1; i <= NUM_TEAMS; ++i)
654 if (TeamBalance_IsTeamAllowedInternal(balance, i))
656 result |= Team_IndexToBit(i);
662 bool TeamBalance_IsTeamAllowed(entity balance, int index)
666 LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
668 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
670 LOG_FATAL("TeamBalance_IsTeamAllowed: "
671 "Team balance entity is not initialized.");
673 if (!Team_IsValidIndex(index))
675 LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
678 return TeamBalance_IsTeamAllowedInternal(balance, index);
681 void TeamBalance_GetTeamCounts(entity balance, entity ignore)
685 LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
687 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
689 LOG_FATAL("TeamBalance_GetTeamCounts: "
690 "Team balance entity is not initialized.");
692 if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
694 // Mutator has overriden the configuration.
695 for (int i = 1; i <= NUM_TEAMS; ++i)
697 entity team_ent = TeamBalance_GetTeamFromIndex(balance, i);
698 if (TeamBalanceTeam_IsAllowed(team_ent))
700 MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, i, ignore);
701 team_ent.m_num_players = M_ARGV(2, float);
702 team_ent.m_num_bots = M_ARGV(3, float);
708 // Manually count all players.
716 // TODO: Reconsider when the player is truly on the team.
717 if (IS_CLIENT(it) || INGAME(it))
721 else if (Player_HasRealForcedTeam(it))
723 // Do we really need this? Probably not.
724 team_num = Team_IndexToTeam(it.team_forced); // reserve the spot
730 if (!Team_IsValidTeam(team_num))
734 entity team_ent = TeamBalance_GetTeam(balance, team_num);
735 if (!TeamBalanceTeam_IsAllowed(team_ent))
739 ++team_ent.m_num_players;
740 if (IS_BOT_CLIENT(it))
742 ++team_ent.m_num_bots;
747 // if the player who has a forced team has not joined yet, reserve the spot
748 if (autocvar_g_campaign)
750 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
752 entity team_ent = TeamBalance_GetTeamFromIndex(balance,
753 autocvar_g_campaign_forceteam);
754 if (team_ent.m_num_players == team_ent.m_num_bots)
756 ++team_ent.m_num_players;
760 balance.m_team_balance_state = TEAM_BALANCE_TEAM_COUNTS_FILLED;
763 int TeamBalance_GetNumberOfPlayers(entity balance, int index)
767 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
768 "Team balance entity is NULL.");
770 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
772 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
773 "TeamBalance_GetTeamCounts has not been called.");
775 if (!Team_IsValidIndex(index))
777 LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
780 return balance.m_team_balance_team[index - 1].m_num_players;
783 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
787 LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
789 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
791 LOG_FATAL("TeamBalance_FindBestTeam: "
792 "Team balance entity is not initialized.");
794 // count how many players are in each team
797 TeamBalance_GetTeamCounts(balance, player);
801 TeamBalance_GetTeamCounts(balance, NULL);
803 int team_bits = TeamBalance_FindBestTeams(balance, player, true);
806 LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
807 MapInfo_Type_ToString(MapInfo_CurrentGametype()));
809 RandomSelection_Init();
810 for (int i = 1; i <= NUM_TEAMS; ++i)
812 if (team_bits & Team_IndexToBit(i))
814 RandomSelection_AddFloat(i, 1, 1);
817 return RandomSelection_chosen_float;
820 int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
824 LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
826 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
828 LOG_FATAL("TeamBalance_FindBestTeams: "
829 "TeamBalance_GetTeamCounts has not been called.");
831 if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
833 return M_ARGV(1, float);
836 int previous_team = 0;
837 for (int i = 1; i <= NUM_TEAMS; ++i)
839 if (!TeamBalance_IsTeamAllowedInternal(balance, i))
843 if (previous_team == 0)
845 team_bits = Team_IndexToBit(i);
849 int compare = TeamBalance_CompareTeams(balance, i, previous_team,
851 if (compare == TEAMS_COMPARE_LESS)
853 team_bits = Team_IndexToBit(i);
857 if (compare == TEAMS_COMPARE_EQUAL)
859 team_bits |= Team_IndexToBit(i);
866 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
867 entity player, bool use_score)
871 LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
873 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
875 LOG_FATAL("TeamBalance_CompareTeams: "
876 "TeamBalance_GetTeamCounts has not been called.");
878 if (!Team_IsValidIndex(team_index_a))
880 LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
883 if (!Team_IsValidIndex(team_index_b))
885 LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
888 if (team_index_a == team_index_b)
890 return TEAMS_COMPARE_EQUAL;
892 entity team_a = TeamBalance_GetTeamFromIndex(balance, team_index_a);
893 entity team_b = TeamBalance_GetTeamFromIndex(balance, team_index_b);
894 return TeamBalance_CompareTeamsInternal(team_a, team_b, player, use_score);
897 void TeamBalance_AutoBalanceBots()
899 // checks disabled because we always want auto-balanced bots
900 //if (!(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance))
903 entity balance = TeamBalance_CheckAllowedTeams(NULL);
904 TeamBalance_GetTeamCounts(balance, NULL);
905 int smallest_team_index = 0;
906 int smallest_team_player_count = 0;
907 for (int i = 1; i <= NUM_TEAMS; ++i)
909 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
910 if (!TeamBalanceTeam_IsAllowed(team_))
914 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
915 if (smallest_team_index == 0)
917 smallest_team_index = i;
918 smallest_team_player_count = playercount;
920 else if (playercount < smallest_team_player_count)
922 smallest_team_index = i;
923 smallest_team_player_count = playercount;
926 //PrintToChatAll(sprintf("Smallest team: %f", smallest_team_index));
927 //PrintToChatAll(sprintf("Smallest team players: %f", smallest_team_player_count));
928 entity switchable_bot = NULL;
929 int teams = BITS(NUM_TEAMS);
932 int largest_team_index = TeamBalance_GetLargestTeamIndex(balance,
934 if (smallest_team_index == largest_team_index)
936 TeamBalance_Destroy(balance);
939 entity largest_team = TeamBalance_GetTeamFromIndex(balance,
941 int largest_team_player_count = TeamBalanceTeam_GetNumberOfPlayers(
943 if (largest_team_player_count - smallest_team_player_count < 2)
945 TeamBalance_Destroy(balance);
948 //PrintToChatAll(sprintf("Largest team: %f", largest_team_index));
949 //PrintToChatAll(sprintf("Largest team players: %f", largest_team_player_count));
950 switchable_bot = TeamBalance_GetPlayerForTeamSwitch(largest_team_index,
951 smallest_team_index, true);
952 if (switchable_bot != NULL)
956 teams &= ~Team_IndexToBit(largest_team_index);
958 TeamBalance_Destroy(balance);
959 if (switchable_bot == NULL)
961 //PrintToChatAll("No bot found after searching through all the teams");
964 SetPlayerTeam(switchable_bot, smallest_team_index, TEAM_CHANGE_AUTO);
967 int TeamBalance_GetLargestTeamIndex(entity balance, int teams)
969 int largest_team_index = 0;
970 int largest_team_player_count = 0;
971 for (int i = 1; i <= NUM_TEAMS; ++i)
973 if (!(Team_IndexToBit(i) & teams))
977 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
978 if (!TeamBalanceTeam_IsAllowed(team_))
982 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
983 if (largest_team_index == 0)
985 largest_team_index = i;
986 largest_team_player_count = playercount;
988 else if (playercount > largest_team_player_count)
990 largest_team_index = i;
991 largest_team_player_count = playercount;
994 return largest_team_index;
997 entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index,
998 int destination_team_index, bool is_bot)
1000 if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, source_team_index,
1001 destination_team_index, is_bot))
1003 return M_ARGV(3, entity);
1005 entity lowest_player = NULL;
1006 float lowest_score = FLOAT_MAX;
1007 FOREACH_CLIENT(Entity_GetTeamIndex(it) == source_team_index,
1009 if (IS_BOT_CLIENT(it) != is_bot)
1013 float temp_score = PlayerScore_Get(it, SP_SCORE);
1014 if (temp_score >= lowest_score)
1018 //PrintToChatAll(sprintf(
1019 // "Found %s with lowest score, checking allowed teams", it.netname));
1020 entity balance = TeamBalance_CheckAllowedTeams(it);
1021 if (TeamBalance_IsTeamAllowed(balance, source_team_index))
1023 //PrintToChatAll("Allowed");
1025 lowest_score = temp_score;
1029 //PrintToChatAll("Not allowed");
1031 TeamBalance_Destroy(balance);
1033 return lowest_player;
1036 void LogTeamChange(float player_id, float team_number, int type)
1038 if (!autocvar_sv_eventlog)
1046 GameLogEcho(sprintf(":team:%d:%d:%d", player_id, team_number, type));
1049 void KillPlayerForTeamChange(entity player)
1051 if (IS_DEAD(player))
1055 if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
1059 Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
1060 player.origin, '0 0 0');
1063 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
1065 return balance.m_team_balance_team[index - 1].m_num_players !=
1069 void TeamBalance_BanTeamsExcept(entity balance, int index)
1071 for (int i = 1; i <= NUM_TEAMS; ++i)
1075 balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
1080 entity TeamBalance_GetTeamFromIndex(entity balance, int index)
1082 if (!Team_IsValidIndex(index))
1084 LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
1086 return balance.m_team_balance_team[index - 1];
1089 entity TeamBalance_GetTeam(entity balance, int team_num)
1091 return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
1094 bool TeamBalanceTeam_IsAllowed(entity team_ent)
1096 return team_ent.m_num_players != TEAM_NOT_ALLOWED;
1099 int TeamBalanceTeam_GetNumberOfPlayers(entity team_ent)
1101 return team_ent.m_num_players;
1104 int TeamBalanceTeam_GetNumberOfBots(entity team_ent)
1106 return team_ent.m_num_bots;
1109 int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
1110 entity player, bool use_score)
1112 if (team_a == team_b)
1114 return TEAMS_COMPARE_EQUAL;
1116 if (!TeamBalanceTeam_IsAllowed(team_a) ||
1117 !TeamBalanceTeam_IsAllowed(team_b))
1119 return TEAMS_COMPARE_INVALID;
1121 int num_players_team_a = team_a.m_num_players;
1122 int num_players_team_b = team_b.m_num_players;
1123 if (IS_REAL_CLIENT(player) && bots_would_leave)
1125 num_players_team_a -= team_a.m_num_bots;
1126 num_players_team_b -= team_b.m_num_bots;
1128 if (num_players_team_a < num_players_team_b)
1130 return TEAMS_COMPARE_LESS;
1132 if (num_players_team_a > num_players_team_b)
1134 return TEAMS_COMPARE_GREATER;
1138 return TEAMS_COMPARE_EQUAL;
1140 if (team_a.m_team_score < team_b.m_team_score)
1142 return TEAMS_COMPARE_LESS;
1144 if (team_a.m_team_score > team_b.m_team_score)
1146 return TEAMS_COMPARE_GREATER;
1148 return TEAMS_COMPARE_EQUAL;
1151 void SV_ChangeTeam(entity player, int new_color)
1155 SetPlayerColors(player, new_color);
1157 if(!IS_CLIENT(player))
1165 Player_SetTeamIndexChecked(player, Team_TeamToIndex((new_color & 0x0F) + 1));