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 bool MoveToTeam(entity client, int team_index, int type)
239 //PrintToChatAll(sprintf("MoveToTeam: %s, %f", client.netname, team_index));
240 int lockteams_backup = lockteams; // backup any team lock
241 lockteams = 0; // disable locked teams
242 if (!SetPlayerTeam(client, team_index, type))
244 lockteams = lockteams_backup; // restore the team lock
247 lockteams = lockteams_backup; // restore the team lock
251 bool Player_HasRealForcedTeam(entity player)
253 return player.team_forced > TEAM_FORCE_DEFAULT;
256 int Player_GetForcedTeamIndex(entity player)
258 return player.team_forced;
261 void Player_SetForcedTeamIndex(entity player, int team_index)
265 case TEAM_FORCE_SPECTATOR:
266 case TEAM_FORCE_DEFAULT:
268 player.team_forced = team_index;
273 if (!Team_IsValidIndex(team_index))
275 LOG_FATAL("Player_SetForcedTeamIndex: Invalid team index.");
279 player.team_forced = team_index;
286 void Player_DetermineForcedTeam(entity player)
288 if (autocvar_g_campaign)
290 if (IS_REAL_CLIENT(player)) // only players, not bots
292 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
294 player.team_forced = autocvar_g_campaign_forceteam;
298 player.team_forced = TEAM_FORCE_DEFAULT;
302 else if (PlayerInList(player, autocvar_g_forced_team_red))
304 player.team_forced = 1;
306 else if (PlayerInList(player, autocvar_g_forced_team_blue))
308 player.team_forced = 2;
310 else if (PlayerInList(player, autocvar_g_forced_team_yellow))
312 player.team_forced = 3;
314 else if (PlayerInList(player, autocvar_g_forced_team_pink))
316 player.team_forced = 4;
320 switch (autocvar_g_forced_team_otherwise)
324 player.team_forced = 1;
329 player.team_forced = 2;
334 player.team_forced = 3;
339 player.team_forced = 4;
345 player.team_forced = TEAM_FORCE_SPECTATOR;
350 player.team_forced = TEAM_FORCE_DEFAULT;
355 if (!teamplay && Player_HasRealForcedTeam(player))
357 player.team_forced = TEAM_FORCE_DEFAULT;
361 void TeamBalance_JoinBestTeam(entity player)
363 //PrintToChatAll(sprintf("TeamBalance_JoinBestTeam: %s", player.netname));
368 if (player.bot_forced_team)
372 entity balance = TeamBalance_CheckAllowedTeams(player);
373 if (Player_HasRealForcedTeam(player))
375 int forced_team_index = player.team_forced;
376 bool is_team_allowed = TeamBalance_IsTeamAllowedInternal(balance,
378 TeamBalance_Destroy(balance);
379 if (!is_team_allowed)
383 if (!SetPlayerTeam(player, forced_team_index, TEAM_CHANGE_AUTO))
389 int best_team_index = TeamBalance_FindBestTeam(balance, player, true);
390 TeamBalance_Destroy(balance);
391 if (!SetPlayerTeam(player, best_team_index, TEAM_CHANGE_AUTO))
397 entity TeamBalance_CheckAllowedTeams(entity for_whom)
399 entity balance = spawn();
400 for (int i = 0; i < NUM_TEAMS; ++i)
402 entity team_ent = balance.m_team_balance_team[i] = spawn();
403 team_ent.m_team_score = g_team_entities[i].m_team_score;
404 team_ent.m_num_players = TEAM_NOT_ALLOWED;
405 team_ent.m_num_bots = 0;
407 setthink(balance, TeamBalance_Destroy);
410 string teament_name = string_null;
411 bool mutator_returnvalue = MUTATOR_CALLHOOK(TeamBalance_CheckAllowedTeams,
412 teams_mask, teament_name, for_whom);
413 teams_mask = M_ARGV(0, float);
414 teament_name = M_ARGV(1, string);
415 if (mutator_returnvalue)
417 for (int i = 0; i < NUM_TEAMS; ++i)
419 if (teams_mask & BIT(i))
421 balance.m_team_balance_team[i].m_num_players = 0;
428 entity head = find(NULL, classname, teament_name);
431 if (Team_IsValidTeam(head.team))
433 TeamBalance_GetTeam(balance, head.team).m_num_players = 0;
435 head = find(head, classname, teament_name);
439 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
440 if (AvailableTeams() == 2)
441 if (autocvar_bot_vs_human && for_whom)
443 if (autocvar_bot_vs_human > 0)
445 // find last team available
446 if (IS_BOT_CLIENT(for_whom))
448 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
450 TeamBalance_BanTeamsExcept(balance, 4);
452 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
454 TeamBalance_BanTeamsExcept(balance, 3);
458 TeamBalance_BanTeamsExcept(balance, 2);
460 // no further cases, we know at least 2 teams exist
464 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
466 TeamBalance_BanTeamsExcept(balance, 1);
468 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
470 TeamBalance_BanTeamsExcept(balance, 2);
474 TeamBalance_BanTeamsExcept(balance, 3);
476 // no further cases, bots have one of the teams
481 // find first team available
482 if (IS_BOT_CLIENT(for_whom))
484 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
486 TeamBalance_BanTeamsExcept(balance, 1);
488 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
490 TeamBalance_BanTeamsExcept(balance, 2);
494 TeamBalance_BanTeamsExcept(balance, 3);
496 // no further cases, we know at least 2 teams exist
500 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
502 TeamBalance_BanTeamsExcept(balance, 4);
504 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
506 TeamBalance_BanTeamsExcept(balance, 3);
510 TeamBalance_BanTeamsExcept(balance, 2);
512 // no further cases, bots have one of the teams
519 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
523 // if player has a forced team, ONLY allow that one
524 for (int i = 1; i <= NUM_TEAMS; ++i)
526 if (for_whom.team_forced == i &&
527 TeamBalance_IsTeamAllowedInternal(balance, i))
529 TeamBalance_BanTeamsExcept(balance, i);
533 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
537 void TeamBalance_Destroy(entity balance)
543 for (int i = 0; i < NUM_TEAMS; ++i)
545 delete(balance.(m_team_balance_team[i]));
550 int TeamBalance_GetAllowedTeams(entity balance)
554 LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
556 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
558 LOG_FATAL("TeamBalance_GetAllowedTeams: "
559 "Team balance entity is not initialized.");
562 for (int i = 1; i <= NUM_TEAMS; ++i)
564 if (TeamBalance_IsTeamAllowedInternal(balance, i))
566 result |= Team_IndexToBit(i);
572 bool TeamBalance_IsTeamAllowed(entity balance, int index)
576 LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
578 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
580 LOG_FATAL("TeamBalance_IsTeamAllowed: "
581 "Team balance entity is not initialized.");
583 if (!Team_IsValidIndex(index))
585 LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
588 return TeamBalance_IsTeamAllowedInternal(balance, index);
591 void TeamBalance_GetTeamCounts(entity balance, entity ignore)
595 LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
597 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
599 LOG_FATAL("TeamBalance_GetTeamCounts: "
600 "Team balance entity is not initialized.");
602 if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
604 // Mutator has overriden the configuration.
605 for (int i = 1; i <= NUM_TEAMS; ++i)
607 entity team_ent = TeamBalance_GetTeamFromIndex(balance, i);
608 if (TeamBalanceTeam_IsAllowed(team_ent))
610 MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, i, ignore);
611 team_ent.m_num_players = M_ARGV(2, float);
612 team_ent.m_num_bots = M_ARGV(3, float);
618 // Manually count all players.
626 // TODO: Reconsider when the player is truly on the team.
627 if (IS_CLIENT(it) || (it.caplayer))
631 else if (Player_HasRealForcedTeam(it))
633 // Do we really need this? Probably not.
634 team_num = Team_IndexToTeam(it.team_forced); // reserve the spot
640 if (!Team_IsValidTeam(team_num))
644 entity team_ent = TeamBalance_GetTeam(balance, team_num);
645 if (!TeamBalanceTeam_IsAllowed(team_ent))
649 ++team_ent.m_num_players;
650 if (IS_BOT_CLIENT(it))
652 ++team_ent.m_num_bots;
657 // if the player who has a forced team has not joined yet, reserve the spot
658 if (autocvar_g_campaign)
660 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
662 entity team_ent = TeamBalance_GetTeamFromIndex(balance,
663 autocvar_g_campaign_forceteam);
664 if (team_ent.m_num_players == team_ent.m_num_bots)
666 ++team_ent.m_num_players;
670 balance.m_team_balance_state = TEAM_BALANCE_TEAM_COUNTS_FILLED;
673 int TeamBalance_GetNumberOfPlayers(entity balance, int index)
677 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
678 "Team balance entity is NULL.");
680 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
682 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
683 "TeamBalance_GetTeamCounts has not been called.");
685 if (!Team_IsValidIndex(index))
687 LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
690 return balance.m_team_balance_team[index - 1].m_num_players;
693 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
697 LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
699 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
701 LOG_FATAL("TeamBalance_FindBestTeam: "
702 "Team balance entity is not initialized.");
704 // count how many players are in each team
707 TeamBalance_GetTeamCounts(balance, player);
711 TeamBalance_GetTeamCounts(balance, NULL);
713 int team_bits = TeamBalance_FindBestTeams(balance, player, true);
716 LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
717 MapInfo_Type_ToString(MapInfo_CurrentGametype()));
719 RandomSelection_Init();
720 for (int i = 1; i <= NUM_TEAMS; ++i)
722 if (team_bits & Team_IndexToBit(i))
724 RandomSelection_AddFloat(i, 1, 1);
727 return RandomSelection_chosen_float;
730 int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
734 LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
736 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
738 LOG_FATAL("TeamBalance_FindBestTeams: "
739 "TeamBalance_GetTeamCounts has not been called.");
741 if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
743 return M_ARGV(1, float);
746 int previous_team = 0;
747 for (int i = 1; i <= NUM_TEAMS; ++i)
749 if (!TeamBalance_IsTeamAllowedInternal(balance, i))
753 if (previous_team == 0)
755 team_bits = Team_IndexToBit(i);
759 int compare = TeamBalance_CompareTeams(balance, i, previous_team,
761 if (compare == TEAMS_COMPARE_LESS)
763 team_bits = Team_IndexToBit(i);
767 if (compare == TEAMS_COMPARE_EQUAL)
769 team_bits |= Team_IndexToBit(i);
776 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
777 entity player, bool use_score)
781 LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
783 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
785 LOG_FATAL("TeamBalance_CompareTeams: "
786 "TeamBalance_GetTeamCounts has not been called.");
788 if (!Team_IsValidIndex(team_index_a))
790 LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
793 if (!Team_IsValidIndex(team_index_b))
795 LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
798 if (team_index_a == team_index_b)
800 return TEAMS_COMPARE_EQUAL;
802 entity team_a = TeamBalance_GetTeamFromIndex(balance, team_index_a);
803 entity team_b = TeamBalance_GetTeamFromIndex(balance, team_index_b);
804 return TeamBalance_CompareTeamsInternal(team_a, team_b, player, use_score);
807 void TeamBalance_AutoBalanceBots()
809 if (!autocvar_g_balance_teams ||
810 !autocvar_g_balance_teams_prevent_imbalance)
814 //PrintToChatAll("TeamBalance_AutoBalanceBots");
815 entity balance = TeamBalance_CheckAllowedTeams(NULL);
816 TeamBalance_GetTeamCounts(balance, NULL);
817 int smallest_team_index = 0;
818 int smallest_team_player_count = 0;
819 for (int i = 1; i <= NUM_TEAMS; ++i)
821 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
822 if (!TeamBalanceTeam_IsAllowed(team_))
826 int player_count = TeamBalanceTeam_GetNumberOfPlayers(team_);
827 if (smallest_team_index == 0)
829 smallest_team_index = i;
830 smallest_team_player_count = player_count;
832 else if (player_count < smallest_team_player_count)
834 smallest_team_index = i;
835 smallest_team_player_count = player_count;
838 //PrintToChatAll(sprintf("Smallest team: %f", smallest_team_index));
839 //PrintToChatAll(sprintf("Smallest team players: %f", smallest_team_player_count));
840 entity switchable_bot = NULL;
841 int teams = BITS(NUM_TEAMS);
844 int largest_team_index = TeamBalance_GetLargestTeamIndex(balance,
846 if (smallest_team_index == largest_team_index)
848 TeamBalance_Destroy(balance);
851 entity largest_team = TeamBalance_GetTeamFromIndex(balance,
853 int largest_team_player_count = TeamBalanceTeam_GetNumberOfPlayers(
855 if (largest_team_player_count - smallest_team_player_count < 2)
857 TeamBalance_Destroy(balance);
860 //PrintToChatAll(sprintf("Largest team: %f", largest_team_index));
861 //PrintToChatAll(sprintf("Largest team players: %f", largest_team_player_count));
862 switchable_bot = TeamBalance_GetPlayerForTeamSwitch(largest_team_index,
863 smallest_team_index, true);
864 if (switchable_bot != NULL)
868 teams &= ~Team_IndexToBit(largest_team_index);
870 TeamBalance_Destroy(balance);
871 if (switchable_bot == NULL)
873 //PrintToChatAll("No bot found after searching through all the teams");
876 SetPlayerTeam(switchable_bot, smallest_team_index, TEAM_CHANGE_AUTO);
879 int TeamBalance_GetLargestTeamIndex(entity balance, int teams)
881 int largest_team_index = 0;
882 int largest_team_player_count = 0;
883 for (int i = 1; i <= NUM_TEAMS; ++i)
885 if (!(Team_IndexToBit(i) & teams))
889 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
890 if (!TeamBalanceTeam_IsAllowed(team_))
894 int player_count = TeamBalanceTeam_GetNumberOfPlayers(team_);
895 if (largest_team_index == 0)
897 largest_team_index = i;
898 largest_team_player_count = player_count;
900 else if (player_count > largest_team_player_count)
902 largest_team_index = i;
903 largest_team_player_count = player_count;
906 return largest_team_index;
909 entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index,
910 int destination_team_index, bool is_bot)
912 if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, source_team_index,
913 destination_team_index, is_bot))
915 return M_ARGV(3, entity);
917 entity lowest_player = NULL;
918 float lowest_score = FLOAT_MAX;
919 FOREACH_CLIENT(Entity_GetTeamIndex(it) == source_team_index,
921 if (IS_BOT_CLIENT(it) != is_bot)
925 float temp_score = PlayerScore_Get(it, SP_SCORE);
926 if (temp_score >= lowest_score)
930 //PrintToChatAll(sprintf(
931 // "Found %s with lowest score, checking allowed teams", it.netname));
932 entity balance = TeamBalance_CheckAllowedTeams(it);
933 if (TeamBalance_IsTeamAllowed(balance, source_team_index))
935 //PrintToChatAll("Allowed");
937 lowest_score = temp_score;
941 //PrintToChatAll("Not allowed");
943 TeamBalance_Destroy(balance);
945 return lowest_player;
948 void LogTeamChange(float player_id, float team_number, int type)
950 if (!autocvar_sv_eventlog)
958 GameLogEcho(sprintf(":team:%f:%f:%f", player_id, team_number, type));
961 void KillPlayerForTeamChange(entity player)
967 if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
971 Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
972 player.origin, '0 0 0');
975 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
977 return balance.m_team_balance_team[index - 1].m_num_players !=
981 void TeamBalance_BanTeamsExcept(entity balance, int index)
983 for (int i = 1; i <= NUM_TEAMS; ++i)
987 balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
992 entity TeamBalance_GetTeamFromIndex(entity balance, int index)
994 if (!Team_IsValidIndex(index))
996 LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
998 return balance.m_team_balance_team[index - 1];
1001 entity TeamBalance_GetTeam(entity balance, int team_num)
1003 return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
1006 bool TeamBalanceTeam_IsAllowed(entity team_ent)
1008 return team_ent.m_num_players != TEAM_NOT_ALLOWED;
1011 int TeamBalanceTeam_GetNumberOfPlayers(entity team_ent)
1013 return team_ent.m_num_players;
1016 int TeamBalanceTeam_GetNumberOfBots(entity team_ent)
1018 return team_ent.m_num_bots;
1021 int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
1022 entity player, bool use_score)
1024 if (team_a == team_b)
1026 return TEAMS_COMPARE_EQUAL;
1028 if (!TeamBalanceTeam_IsAllowed(team_a) ||
1029 !TeamBalanceTeam_IsAllowed(team_b))
1031 return TEAMS_COMPARE_INVALID;
1033 int num_players_team_a = team_a.m_num_players;
1034 int num_players_team_b = team_b.m_num_players;
1035 if (IS_REAL_CLIENT(player) && bots_would_leave)
1037 num_players_team_a -= team_a.m_num_bots;
1038 num_players_team_b -= team_b.m_num_bots;
1040 if (num_players_team_a < num_players_team_b)
1042 return TEAMS_COMPARE_LESS;
1044 if (num_players_team_a > num_players_team_b)
1046 return TEAMS_COMPARE_GREATER;
1050 return TEAMS_COMPARE_EQUAL;
1052 if (team_a.m_team_score < team_b.m_team_score)
1054 return TEAMS_COMPARE_LESS;
1056 if (team_a.m_team_score > team_b.m_team_score)
1058 return TEAMS_COMPARE_GREATER;
1060 return TEAMS_COMPARE_EQUAL;
1063 // Called when the player connects or when they change their color with "color"
1065 void SV_ChangeTeam(entity this, float _color)
1067 //PrintToChatAll(sprintf("SV_ChangeTeam: %s, %f", this.netname, _color));
1069 // in normal deathmatch we can just apply the color and we're done
1071 SetPlayerColors(this, _color);
1073 if(!IS_CLIENT(this))
1075 //PrintToChatAll("Not a client yet");
1076 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
1077 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING, this.netname);
1084 int source_color, destination_color;
1085 int source_team_index, destination_team_index;
1087 source_color = this.clientcolors & 0x0F;
1088 destination_color = _color & 0x0F;
1090 source_team_index = Team_TeamToIndex(source_color + 1);
1091 destination_team_index = Team_TeamToIndex(destination_color + 1);
1093 if (destination_team_index == -1)
1098 entity balance = TeamBalance_CheckAllowedTeams(this);
1100 if (destination_team_index == 1 && !TeamBalance_IsTeamAllowedInternal(
1103 destination_team_index = 4;
1105 if (destination_team_index == 4 && !TeamBalance_IsTeamAllowedInternal(
1108 destination_team_index = 3;
1110 if (destination_team_index == 3 && !TeamBalance_IsTeamAllowedInternal(
1113 destination_team_index = 2;
1115 if (destination_team_index == 2 && !TeamBalance_IsTeamAllowedInternal(
1118 destination_team_index = 1;
1121 // not changing teams
1122 if (source_color == destination_color)
1124 SetPlayerTeam(this, destination_team_index, TEAM_CHANGE_MANUAL);
1125 TeamBalance_Destroy(balance);
1129 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && CS(this).wasplayer)) {
1130 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
1131 return; // changing teams is not allowed
1134 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
1135 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
1137 TeamBalance_GetTeamCounts(balance, this);
1138 if ((Team_IndexToBit(destination_team_index) &
1139 TeamBalance_FindBestTeams(balance, this, false)) == 0)
1141 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
1142 TeamBalance_Destroy(balance);
1146 TeamBalance_Destroy(balance);
1147 SetPlayerTeam(this, destination_team_index, TEAM_CHANGE_MANUAL);