6 #include "scores_rules.qh"
10 #include "command/vote.qh"
12 #include <server/mutators/_mod.qh>
14 #include "../common/deathtypes/all.qh"
15 #include <common/gamemodes/_mod.qh>
16 #include "../common/teams.qh"
18 /// \brief Describes a state of team balance entity.
21 TEAM_BALANCE_UNINITIALIZED, ///< The team balance has not been initialized.
22 /// \brief TeamBalance_CheckAllowedTeams has been called.
23 TEAM_BALANCE_TEAMS_CHECKED,
24 /// \brief TeamBalance_GetTeamCounts has been called.
25 TEAM_BALANCE_TEAM_COUNTS_FILLED
28 /// \brief Indicates that the player is not allowed to join a team.
29 const int TEAM_NOT_ALLOWED = -1;
31 .float team_forced; // can be a team number to force a team, or 0 for default action, or -1 for forced spectator
33 .int m_team_balance_state; ///< Holds the state of the team balance entity.
34 .entity m_team_balance_team[NUM_TEAMS]; ///< ???
36 .float m_team_score; ///< The score of the team.
37 .int m_num_players; ///< Number of players (both humans and bots) in a team.
38 .int m_num_bots; ///< Number of bots in a team.
39 .int m_num_players_alive; ///< Number of alive players in a team.
40 .int m_num_control_points; ///< Number of control points owned by a team.
42 string autocvar_g_forced_team_red;
43 string autocvar_g_forced_team_blue;
44 string autocvar_g_forced_team_yellow;
45 string autocvar_g_forced_team_pink;
47 entity g_team_entities[NUM_TEAMS]; ///< Holds global team entities.
49 STATIC_INIT(g_team_entities)
51 for (int i = 0; i < NUM_TEAMS; ++i)
53 g_team_entities[i] = spawn();
57 entity Team_GetTeamFromIndex(int index)
59 if (!Team_IsValidIndex(index))
61 LOG_FATALF("Team_GetTeamFromIndex: Index is invalid: %f", index);
63 return g_team_entities[index - 1];
66 entity Team_GetTeam(int team_num)
68 if (!Team_IsValidTeam(team_num))
70 LOG_FATALF("Team_GetTeam: Value is invalid: %f", team_num);
72 return g_team_entities[Team_TeamToIndex(team_num) - 1];
75 float Team_GetTeamScore(entity team_ent)
77 return team_ent.m_team_score;
80 void Team_SetTeamScore(entity team_ent, float score)
82 team_ent.m_team_score = score;
85 int Team_GetNumberOfAlivePlayers(entity team_ent)
87 return team_ent.m_num_players_alive;
90 void Team_SetNumberOfAlivePlayers(entity team_ent, int number)
92 team_ent.m_num_players_alive = number;
95 int Team_GetNumberOfAliveTeams()
98 for (int i = 0; i < NUM_TEAMS; ++i)
100 if (g_team_entities[i].m_num_players_alive > 0)
108 int Team_GetNumberOfControlPoints(entity team_ent)
110 return team_ent.m_num_control_points;
113 void Team_SetNumberOfControlPoints(entity team_ent, int number)
115 team_ent.m_num_control_points = number;
118 int Team_GetNumberOfTeamsWithControlPoints()
121 for (int i = 0; i < NUM_TEAMS; ++i)
123 if (g_team_entities[i].m_num_control_points > 0)
131 void setcolor(entity this, int clr)
134 this.clientcolors = clr;
135 this.team = (clr & 15) + 1;
137 builtin_setcolor(this, clr);
141 bool Entity_HasValidTeam(entity this)
143 return Team_IsValidTeam(this.team);
146 int Entity_GetTeamIndex(entity this)
148 return Team_TeamToIndex(this.team);
151 entity Entity_GetTeam(entity this)
153 int index = Entity_GetTeamIndex(this);
154 if (!Team_IsValidIndex(index))
158 return Team_GetTeamFromIndex(index);
161 void SetPlayerColors(entity player, float _color)
163 float pants = _color & 0x0F;
164 float shirt = _color & 0xF0;
167 setcolor(player, 16 * pants + pants);
171 setcolor(player, shirt + pants);
175 bool Player_SetTeamIndex(entity player, int index)
177 int new_team = Team_IndexToTeam(index);
178 if (player.team == new_team)
182 // This is important when players join the game and one of their
183 // color matches the team color while other doesn't. For example
185 SetPlayerColors(player, new_team - 1);
189 int old_index = Team_TeamToIndex(player.team);
190 if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, old_index, index) == true)
192 // Mutator has blocked team change.
201 SetPlayerColors(player, new_team - 1);
203 MUTATOR_CALLHOOK(Player_ChangedTeam, player, old_index, index);
207 bool SetPlayerTeam(entity player, int team_index, int type)
209 int old_team_index = Entity_GetTeamIndex(player);
210 if (!Player_SetTeamIndex(player, team_index))
214 LogTeamChange(player.playerid, player.team, type);
215 if (team_index != old_team_index)
217 PlayerScore_Clear(player);
218 if (team_index != -1)
220 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(
221 player.team, INFO_JOIN_PLAY_TEAM), player.netname);
225 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE,
228 KillPlayerForTeamChange(player);
229 if (!IS_BOT_CLIENT(player))
231 TeamBalance_AutoBalanceBots();
237 void Player_SetTeamIndexChecked(entity player, int team_index)
243 if (!Team_IsValidIndex(team_index))
247 if ((autocvar_g_campaign) || (autocvar_g_changeteam_banned &&
248 CS(player).wasplayer))
250 Send_Notification(NOTIF_ONE, player, MSG_INFO,
251 INFO_TEAMCHANGE_NOTALLOWED);
254 entity balance = TeamBalance_CheckAllowedTeams(player);
255 if (team_index == 1 && !TeamBalance_IsTeamAllowedInternal(balance, 1))
259 if (team_index == 4 && !TeamBalance_IsTeamAllowedInternal(balance, 4))
263 if (team_index == 3 && !TeamBalance_IsTeamAllowedInternal(balance, 3))
267 if (team_index == 2 && !TeamBalance_IsTeamAllowedInternal(balance, 2))
271 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
272 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
274 TeamBalance_GetTeamCounts(balance, player);
275 if ((Team_IndexToBit(team_index) & TeamBalance_FindBestTeams(balance,
276 player, false)) == 0)
278 Send_Notification(NOTIF_ONE, player, MSG_INFO,
279 INFO_TEAMCHANGE_LARGERTEAM);
280 TeamBalance_Destroy(balance);
284 TeamBalance_Destroy(balance);
285 SetPlayerTeam(player, team_index, TEAM_CHANGE_MANUAL);
288 bool MoveToTeam(entity client, int team_index, int type)
290 //PrintToChatAll(sprintf("MoveToTeam: %s, %f", client.netname, team_index));
291 int lockteams_backup = lockteams; // backup any team lock
292 lockteams = 0; // disable locked teams
293 if (!SetPlayerTeam(client, team_index, type))
295 lockteams = lockteams_backup; // restore the team lock
298 lockteams = lockteams_backup; // restore the team lock
302 bool Player_HasRealForcedTeam(entity player)
304 return player.team_forced > TEAM_FORCE_DEFAULT;
307 int Player_GetForcedTeamIndex(entity player)
309 return player.team_forced;
312 void Player_SetForcedTeamIndex(entity player, int team_index)
316 case TEAM_FORCE_SPECTATOR:
317 case TEAM_FORCE_DEFAULT:
319 player.team_forced = team_index;
324 if (!Team_IsValidIndex(team_index))
326 LOG_FATAL("Player_SetForcedTeamIndex: Invalid team index.");
330 player.team_forced = team_index;
337 void Player_DetermineForcedTeam(entity player)
339 if (autocvar_g_campaign)
341 if (IS_REAL_CLIENT(player)) // only players, not bots
343 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
345 player.team_forced = autocvar_g_campaign_forceteam;
349 player.team_forced = TEAM_FORCE_DEFAULT;
353 else if (PlayerInList(player, autocvar_g_forced_team_red))
355 player.team_forced = 1;
357 else if (PlayerInList(player, autocvar_g_forced_team_blue))
359 player.team_forced = 2;
361 else if (PlayerInList(player, autocvar_g_forced_team_yellow))
363 player.team_forced = 3;
365 else if (PlayerInList(player, autocvar_g_forced_team_pink))
367 player.team_forced = 4;
371 switch (autocvar_g_forced_team_otherwise)
375 player.team_forced = 1;
380 player.team_forced = 2;
385 player.team_forced = 3;
390 player.team_forced = 4;
396 player.team_forced = TEAM_FORCE_SPECTATOR;
401 player.team_forced = TEAM_FORCE_DEFAULT;
406 if (!teamplay && Player_HasRealForcedTeam(player))
408 player.team_forced = TEAM_FORCE_DEFAULT;
412 void TeamBalance_JoinBestTeam(entity player)
414 //PrintToChatAll(sprintf("TeamBalance_JoinBestTeam: %s", player.netname));
419 if (player.bot_forced_team)
423 entity balance = TeamBalance_CheckAllowedTeams(player);
424 if (Player_HasRealForcedTeam(player))
426 int forced_team_index = player.team_forced;
427 bool is_team_allowed = TeamBalance_IsTeamAllowedInternal(balance,
429 TeamBalance_Destroy(balance);
430 if (!is_team_allowed)
434 if (!SetPlayerTeam(player, forced_team_index, TEAM_CHANGE_AUTO))
440 int best_team_index = TeamBalance_FindBestTeam(balance, player, true);
441 TeamBalance_Destroy(balance);
442 if (!SetPlayerTeam(player, best_team_index, TEAM_CHANGE_AUTO))
448 entity TeamBalance_CheckAllowedTeams(entity for_whom)
450 entity balance = spawn();
451 for (int i = 0; i < NUM_TEAMS; ++i)
453 entity team_ent = balance.m_team_balance_team[i] = spawn();
454 team_ent.m_team_score = g_team_entities[i].m_team_score;
455 team_ent.m_num_players = TEAM_NOT_ALLOWED;
456 team_ent.m_num_bots = 0;
458 setthink(balance, TeamBalance_Destroy);
461 string teament_name = string_null;
462 bool mutator_returnvalue = MUTATOR_CALLHOOK(TeamBalance_CheckAllowedTeams,
463 teams_mask, teament_name, for_whom);
464 teams_mask = M_ARGV(0, float);
465 teament_name = M_ARGV(1, string);
466 if (mutator_returnvalue)
468 for (int i = 0; i < NUM_TEAMS; ++i)
470 if (teams_mask & BIT(i))
472 balance.m_team_balance_team[i].m_num_players = 0;
479 entity head = find(NULL, classname, teament_name);
482 if (Team_IsValidTeam(head.team))
484 TeamBalance_GetTeam(balance, head.team).m_num_players = 0;
486 head = find(head, classname, teament_name);
490 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
491 if (AvailableTeams() == 2)
492 if (autocvar_bot_vs_human && for_whom)
494 if (autocvar_bot_vs_human > 0)
496 // find last team available
497 if (IS_BOT_CLIENT(for_whom))
499 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
501 TeamBalance_BanTeamsExcept(balance, 4);
503 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
505 TeamBalance_BanTeamsExcept(balance, 3);
509 TeamBalance_BanTeamsExcept(balance, 2);
511 // no further cases, we know at least 2 teams exist
515 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
517 TeamBalance_BanTeamsExcept(balance, 1);
519 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
521 TeamBalance_BanTeamsExcept(balance, 2);
525 TeamBalance_BanTeamsExcept(balance, 3);
527 // no further cases, bots have one of the teams
532 // find first team available
533 if (IS_BOT_CLIENT(for_whom))
535 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
537 TeamBalance_BanTeamsExcept(balance, 1);
539 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
541 TeamBalance_BanTeamsExcept(balance, 2);
545 TeamBalance_BanTeamsExcept(balance, 3);
547 // no further cases, we know at least 2 teams exist
551 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
553 TeamBalance_BanTeamsExcept(balance, 4);
555 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
557 TeamBalance_BanTeamsExcept(balance, 3);
561 TeamBalance_BanTeamsExcept(balance, 2);
563 // no further cases, bots have one of the teams
570 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
574 // if player has a forced team, ONLY allow that one
575 for (int i = 1; i <= NUM_TEAMS; ++i)
577 if (for_whom.team_forced == i &&
578 TeamBalance_IsTeamAllowedInternal(balance, i))
580 TeamBalance_BanTeamsExcept(balance, i);
584 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
588 void TeamBalance_Destroy(entity balance)
594 for (int i = 0; i < NUM_TEAMS; ++i)
596 delete(balance.(m_team_balance_team[i]));
601 int TeamBalance_GetAllowedTeams(entity balance)
605 LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
607 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
609 LOG_FATAL("TeamBalance_GetAllowedTeams: "
610 "Team balance entity is not initialized.");
613 for (int i = 1; i <= NUM_TEAMS; ++i)
615 if (TeamBalance_IsTeamAllowedInternal(balance, i))
617 result |= Team_IndexToBit(i);
623 bool TeamBalance_IsTeamAllowed(entity balance, int index)
627 LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
629 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
631 LOG_FATAL("TeamBalance_IsTeamAllowed: "
632 "Team balance entity is not initialized.");
634 if (!Team_IsValidIndex(index))
636 LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
639 return TeamBalance_IsTeamAllowedInternal(balance, index);
642 void TeamBalance_GetTeamCounts(entity balance, entity ignore)
646 LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
648 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
650 LOG_FATAL("TeamBalance_GetTeamCounts: "
651 "Team balance entity is not initialized.");
653 if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
655 // Mutator has overriden the configuration.
656 for (int i = 1; i <= NUM_TEAMS; ++i)
658 entity team_ent = TeamBalance_GetTeamFromIndex(balance, i);
659 if (TeamBalanceTeam_IsAllowed(team_ent))
661 MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, i, ignore);
662 team_ent.m_num_players = M_ARGV(2, float);
663 team_ent.m_num_bots = M_ARGV(3, float);
669 // Manually count all players.
677 // TODO: Reconsider when the player is truly on the team.
678 if (IS_CLIENT(it) || (it.caplayer))
682 else if (Player_HasRealForcedTeam(it))
684 // Do we really need this? Probably not.
685 team_num = Team_IndexToTeam(it.team_forced); // reserve the spot
691 if (!Team_IsValidTeam(team_num))
695 entity team_ent = TeamBalance_GetTeam(balance, team_num);
696 if (!TeamBalanceTeam_IsAllowed(team_ent))
700 ++team_ent.m_num_players;
701 if (IS_BOT_CLIENT(it))
703 ++team_ent.m_num_bots;
708 // if the player who has a forced team has not joined yet, reserve the spot
709 if (autocvar_g_campaign)
711 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
713 entity team_ent = TeamBalance_GetTeamFromIndex(balance,
714 autocvar_g_campaign_forceteam);
715 if (team_ent.m_num_players == team_ent.m_num_bots)
717 ++team_ent.m_num_players;
721 balance.m_team_balance_state = TEAM_BALANCE_TEAM_COUNTS_FILLED;
724 int TeamBalance_GetNumberOfPlayers(entity balance, int index)
728 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
729 "Team balance entity is NULL.");
731 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
733 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
734 "TeamBalance_GetTeamCounts has not been called.");
736 if (!Team_IsValidIndex(index))
738 LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
741 return balance.m_team_balance_team[index - 1].m_num_players;
744 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
748 LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
750 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
752 LOG_FATAL("TeamBalance_FindBestTeam: "
753 "Team balance entity is not initialized.");
755 // count how many players are in each team
758 TeamBalance_GetTeamCounts(balance, player);
762 TeamBalance_GetTeamCounts(balance, NULL);
764 int team_bits = TeamBalance_FindBestTeams(balance, player, true);
767 LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
768 MapInfo_Type_ToString(MapInfo_CurrentGametype()));
770 RandomSelection_Init();
771 for (int i = 1; i <= NUM_TEAMS; ++i)
773 if (team_bits & Team_IndexToBit(i))
775 RandomSelection_AddFloat(i, 1, 1);
778 return RandomSelection_chosen_float;
781 int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
785 LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
787 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
789 LOG_FATAL("TeamBalance_FindBestTeams: "
790 "TeamBalance_GetTeamCounts has not been called.");
792 if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
794 return M_ARGV(1, float);
797 int previous_team = 0;
798 for (int i = 1; i <= NUM_TEAMS; ++i)
800 if (!TeamBalance_IsTeamAllowedInternal(balance, i))
804 if (previous_team == 0)
806 team_bits = Team_IndexToBit(i);
810 int compare = TeamBalance_CompareTeams(balance, i, previous_team,
812 if (compare == TEAMS_COMPARE_LESS)
814 team_bits = Team_IndexToBit(i);
818 if (compare == TEAMS_COMPARE_EQUAL)
820 team_bits |= Team_IndexToBit(i);
827 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
828 entity player, bool use_score)
832 LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
834 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
836 LOG_FATAL("TeamBalance_CompareTeams: "
837 "TeamBalance_GetTeamCounts has not been called.");
839 if (!Team_IsValidIndex(team_index_a))
841 LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
844 if (!Team_IsValidIndex(team_index_b))
846 LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
849 if (team_index_a == team_index_b)
851 return TEAMS_COMPARE_EQUAL;
853 entity team_a = TeamBalance_GetTeamFromIndex(balance, team_index_a);
854 entity team_b = TeamBalance_GetTeamFromIndex(balance, team_index_b);
855 return TeamBalance_CompareTeamsInternal(team_a, team_b, player, use_score);
858 void TeamBalance_AutoBalanceBots()
860 if (!autocvar_g_balance_teams ||
861 !autocvar_g_balance_teams_prevent_imbalance)
865 //PrintToChatAll("TeamBalance_AutoBalanceBots");
866 entity balance = TeamBalance_CheckAllowedTeams(NULL);
867 TeamBalance_GetTeamCounts(balance, NULL);
868 int smallest_team_index = 0;
869 int smallest_team_player_count = 0;
870 for (int i = 1; i <= NUM_TEAMS; ++i)
872 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
873 if (!TeamBalanceTeam_IsAllowed(team_))
877 int player_count = TeamBalanceTeam_GetNumberOfPlayers(team_);
878 if (smallest_team_index == 0)
880 smallest_team_index = i;
881 smallest_team_player_count = player_count;
883 else if (player_count < smallest_team_player_count)
885 smallest_team_index = i;
886 smallest_team_player_count = player_count;
889 //PrintToChatAll(sprintf("Smallest team: %f", smallest_team_index));
890 //PrintToChatAll(sprintf("Smallest team players: %f", smallest_team_player_count));
891 entity switchable_bot = NULL;
892 int teams = BITS(NUM_TEAMS);
895 int largest_team_index = TeamBalance_GetLargestTeamIndex(balance,
897 if (smallest_team_index == largest_team_index)
899 TeamBalance_Destroy(balance);
902 entity largest_team = TeamBalance_GetTeamFromIndex(balance,
904 int largest_team_player_count = TeamBalanceTeam_GetNumberOfPlayers(
906 if (largest_team_player_count - smallest_team_player_count < 2)
908 TeamBalance_Destroy(balance);
911 //PrintToChatAll(sprintf("Largest team: %f", largest_team_index));
912 //PrintToChatAll(sprintf("Largest team players: %f", largest_team_player_count));
913 switchable_bot = TeamBalance_GetPlayerForTeamSwitch(largest_team_index,
914 smallest_team_index, true);
915 if (switchable_bot != NULL)
919 teams &= ~Team_IndexToBit(largest_team_index);
921 TeamBalance_Destroy(balance);
922 if (switchable_bot == NULL)
924 //PrintToChatAll("No bot found after searching through all the teams");
927 SetPlayerTeam(switchable_bot, smallest_team_index, TEAM_CHANGE_AUTO);
930 int TeamBalance_GetLargestTeamIndex(entity balance, int teams)
932 int largest_team_index = 0;
933 int largest_team_player_count = 0;
934 for (int i = 1; i <= NUM_TEAMS; ++i)
936 if (!(Team_IndexToBit(i) & teams))
940 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
941 if (!TeamBalanceTeam_IsAllowed(team_))
945 int player_count = TeamBalanceTeam_GetNumberOfPlayers(team_);
946 if (largest_team_index == 0)
948 largest_team_index = i;
949 largest_team_player_count = player_count;
951 else if (player_count > largest_team_player_count)
953 largest_team_index = i;
954 largest_team_player_count = player_count;
957 return largest_team_index;
960 entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index,
961 int destination_team_index, bool is_bot)
963 if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, source_team_index,
964 destination_team_index, is_bot))
966 return M_ARGV(3, entity);
968 entity lowest_player = NULL;
969 float lowest_score = FLOAT_MAX;
970 FOREACH_CLIENT(Entity_GetTeamIndex(it) == source_team_index,
972 if (IS_BOT_CLIENT(it) != is_bot)
976 float temp_score = PlayerScore_Get(it, SP_SCORE);
977 if (temp_score >= lowest_score)
981 //PrintToChatAll(sprintf(
982 // "Found %s with lowest score, checking allowed teams", it.netname));
983 entity balance = TeamBalance_CheckAllowedTeams(it);
984 if (TeamBalance_IsTeamAllowed(balance, source_team_index))
986 //PrintToChatAll("Allowed");
988 lowest_score = temp_score;
992 //PrintToChatAll("Not allowed");
994 TeamBalance_Destroy(balance);
996 return lowest_player;
999 void LogTeamChange(float player_id, float team_number, int type)
1001 if (!autocvar_sv_eventlog)
1009 GameLogEcho(sprintf(":team:%f:%f:%f", player_id, team_number, type));
1012 void KillPlayerForTeamChange(entity player)
1014 if (IS_DEAD(player))
1018 if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
1022 Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
1023 player.origin, '0 0 0');
1026 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
1028 return balance.m_team_balance_team[index - 1].m_num_players !=
1032 void TeamBalance_BanTeamsExcept(entity balance, int index)
1034 for (int i = 1; i <= NUM_TEAMS; ++i)
1038 balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
1043 entity TeamBalance_GetTeamFromIndex(entity balance, int index)
1045 if (!Team_IsValidIndex(index))
1047 LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
1049 return balance.m_team_balance_team[index - 1];
1052 entity TeamBalance_GetTeam(entity balance, int team_num)
1054 return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
1057 bool TeamBalanceTeam_IsAllowed(entity team_ent)
1059 return team_ent.m_num_players != TEAM_NOT_ALLOWED;
1062 int TeamBalanceTeam_GetNumberOfPlayers(entity team_ent)
1064 return team_ent.m_num_players;
1067 int TeamBalanceTeam_GetNumberOfBots(entity team_ent)
1069 return team_ent.m_num_bots;
1072 int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
1073 entity player, bool use_score)
1075 if (team_a == team_b)
1077 return TEAMS_COMPARE_EQUAL;
1079 if (!TeamBalanceTeam_IsAllowed(team_a) ||
1080 !TeamBalanceTeam_IsAllowed(team_b))
1082 return TEAMS_COMPARE_INVALID;
1084 int num_players_team_a = team_a.m_num_players;
1085 int num_players_team_b = team_b.m_num_players;
1086 if (IS_REAL_CLIENT(player) && bots_would_leave)
1088 num_players_team_a -= team_a.m_num_bots;
1089 num_players_team_b -= team_b.m_num_bots;
1091 if (num_players_team_a < num_players_team_b)
1093 return TEAMS_COMPARE_LESS;
1095 if (num_players_team_a > num_players_team_b)
1097 return TEAMS_COMPARE_GREATER;
1101 return TEAMS_COMPARE_EQUAL;
1103 if (team_a.m_team_score < team_b.m_team_score)
1105 return TEAMS_COMPARE_LESS;
1107 if (team_a.m_team_score > team_b.m_team_score)
1109 return TEAMS_COMPARE_GREATER;
1111 return TEAMS_COMPARE_EQUAL;
1114 void SV_ChangeTeam(entity player, int new_color)
1118 SetPlayerColors(player, new_color);
1120 // TODO: Should we really bother with this?
1121 if(!IS_CLIENT(player))
1123 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
1124 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING,
1132 Player_SetTeamIndexChecked(player, Team_TeamToIndex((new_color & 0x0F) +