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 .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 .string m_team_name; // z411 team name
37 .float m_team_score; ///< The score of the team.
38 .int m_num_players; ///< Number of players (both humans and bots) in a team.
39 .int m_num_bots; ///< Number of bots in a team.
40 .int m_num_players_alive; ///< Number of alive players in a team.
41 .int m_num_control_points; ///< Number of control points owned by a team.
43 string autocvar_g_forced_team_red;
44 string autocvar_g_forced_team_blue;
45 string autocvar_g_forced_team_yellow;
46 string autocvar_g_forced_team_pink;
48 entity g_team_entities[NUM_TEAMS]; ///< Holds global team entities.
50 STATIC_INIT(g_team_entities)
52 for (int i = 0; i < NUM_TEAMS; ++i)
54 g_team_entities[i] = spawn();
58 entity Team_GetTeamFromIndex(int index)
60 if (!Team_IsValidIndex(index))
62 LOG_FATALF("Team_GetTeamFromIndex: Index is invalid: %f", index);
64 return g_team_entities[index - 1];
67 entity Team_GetTeam(int team_num)
69 if (!Team_IsValidTeam(team_num))
71 LOG_FATALF("Team_GetTeam: Value is invalid: %f", team_num);
73 return g_team_entities[Team_TeamToIndex(team_num) - 1];
76 string Team_GetTeamName(entity team_ent)
78 return team_ent.m_team_name;
81 void Team_SetTeamName(entity team_ent, string name)
83 team_ent.m_team_name = name;
86 float Team_GetTeamScore(entity team_ent)
88 return team_ent.m_team_score;
91 void Team_SetTeamScore(entity team_ent, float score)
93 team_ent.m_team_score = score;
96 int Team_GetNumberOfAlivePlayers(entity team_ent)
98 return team_ent.m_num_players_alive;
101 void Team_SetNumberOfAlivePlayers(entity team_ent, int number)
103 team_ent.m_num_players_alive = number;
106 int Team_GetNumberOfPlayers(entity team_ent)
108 return team_ent.m_num_players;
111 void Team_SetNumberOfPlayers(entity team_ent, int number)
113 team_ent.m_num_players = number;
116 int Team_GetNumberOfAliveTeams()
119 for (int i = 0; i < NUM_TEAMS; ++i)
121 if (g_team_entities[i].m_num_players_alive > 0)
129 int Team_GetNumberOfControlPoints(entity team_ent)
131 return team_ent.m_num_control_points;
134 void Team_SetNumberOfControlPoints(entity team_ent, int number)
136 team_ent.m_num_control_points = number;
139 int Team_GetNumberOfTeamsWithControlPoints()
142 for (int i = 0; i < NUM_TEAMS; ++i)
144 if (g_team_entities[i].m_num_control_points > 0)
152 void setcolor(entity this, int clr)
155 this.clientcolors = clr;
156 this.team = (clr & 15) + 1;
158 builtin_setcolor(this, clr);
162 bool Entity_HasValidTeam(entity this)
164 return Team_IsValidTeam(this.team);
167 int Entity_GetTeamIndex(entity this)
169 return Team_TeamToIndex(this.team);
172 entity Entity_GetTeam(entity this)
174 int index = Entity_GetTeamIndex(this);
175 if (!Team_IsValidIndex(index))
179 return Team_GetTeamFromIndex(index);
182 void SetPlayerColors(entity player, float _color)
184 float pants = _color & 0x0F;
185 float shirt = _color & 0xF0;
188 setcolor(player, 16 * pants + pants);
192 setcolor(player, shirt + pants);
196 bool Player_SetTeamIndex(entity player, int index)
198 int new_team = Team_IndexToTeam(index);
199 if (player.team == new_team)
203 // This is important when players join the game and one of their
204 // color matches the team color while other doesn't. For example
205 // [BOT]Lion: color 0 4.
206 SetPlayerColors(player, new_team - 1);
210 int old_index = Team_TeamToIndex(player.team);
211 if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, old_index, index) == true)
213 // Mutator has blocked team change.
222 SetPlayerColors(player, new_team - 1);
224 MUTATOR_CALLHOOK(Player_ChangedTeam, player, old_index, index);
228 bool SetPlayerTeam(entity player, int team_index, int type)
230 int old_team_index = Entity_GetTeamIndex(player);
231 if (!Player_SetTeamIndex(player, team_index))
235 LogTeamChange(player.playerid, player.team, type);
236 if (team_index != old_team_index)
238 PlayerScore_Clear(player);
239 if (team_index != -1)
241 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(
242 player.team, INFO_JOIN_PLAY_TEAM), player.netname);
246 if (!CS(player).just_joined)
248 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE,
252 KillPlayerForTeamChange(player);
253 if (!IS_BOT_CLIENT(player))
255 TeamBalance_AutoBalanceBots();
258 else if (team_index == -1)
260 if (!CS(player).just_joined && player.frags != FRAGS_SPECTATOR)
261 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE, player.netname);
266 void Player_SetTeamIndexChecked(entity player, int team_index)
272 if (!Team_IsValidIndex(team_index))
276 if ((autocvar_g_campaign) || (autocvar_g_changeteam_banned &&
277 CS(player).wasplayer))
279 Send_Notification(NOTIF_ONE, player, MSG_INFO,
280 INFO_TEAMCHANGE_NOTALLOWED);
283 entity balance = TeamBalance_CheckAllowedTeams(player);
284 if (team_index == 1 && !TeamBalance_IsTeamAllowedInternal(balance, 1))
288 if (team_index == 4 && !TeamBalance_IsTeamAllowedInternal(balance, 4))
292 if (team_index == 3 && !TeamBalance_IsTeamAllowedInternal(balance, 3))
296 if (team_index == 2 && !TeamBalance_IsTeamAllowedInternal(balance, 2))
300 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
301 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
303 TeamBalance_GetTeamCounts(balance, player);
304 if ((Team_IndexToBit(team_index) & TeamBalance_FindBestTeams(balance,
305 player, false)) == 0)
307 Send_Notification(NOTIF_ONE, player, MSG_INFO,
308 INFO_TEAMCHANGE_LARGERTEAM);
309 TeamBalance_Destroy(balance);
313 TeamBalance_Destroy(balance);
314 SetPlayerTeam(player, team_index, TEAM_CHANGE_MANUAL);
317 bool MoveToTeam(entity client, int team_index, int type)
319 //PrintToChatAll(sprintf("MoveToTeam: %s, %f", client.netname, team_index));
320 int lockteams_backup = lockteams; // backup any team lock
321 lockteams = 0; // disable locked teams
322 if (!SetPlayerTeam(client, team_index, type))
324 lockteams = lockteams_backup; // restore the team lock
327 lockteams = lockteams_backup; // restore the team lock
331 bool Player_HasRealForcedTeam(entity player)
333 return player.team_forced > TEAM_FORCE_DEFAULT;
336 int Player_GetForcedTeamIndex(entity player)
338 return player.team_forced;
341 void Player_SetForcedTeamIndex(entity player, int team_index)
345 case TEAM_FORCE_SPECTATOR:
346 case TEAM_FORCE_DEFAULT:
348 player.team_forced = team_index;
353 if (!Team_IsValidIndex(team_index))
355 LOG_FATAL("Player_SetForcedTeamIndex: Invalid team index.");
359 player.team_forced = team_index;
366 void Player_DetermineForcedTeam(entity player)
368 if (autocvar_g_campaign)
370 if (IS_REAL_CLIENT(player)) // only players, not bots
372 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
374 player.team_forced = autocvar_g_campaign_forceteam;
378 player.team_forced = TEAM_FORCE_DEFAULT;
382 else if (PlayerInList(player, autocvar_g_forced_team_red))
384 player.team_forced = 1;
386 else if (PlayerInList(player, autocvar_g_forced_team_blue))
388 player.team_forced = 2;
390 else if (PlayerInList(player, autocvar_g_forced_team_yellow))
392 player.team_forced = 3;
394 else if (PlayerInList(player, autocvar_g_forced_team_pink))
396 player.team_forced = 4;
400 switch (autocvar_g_forced_team_otherwise)
404 player.team_forced = 1;
409 player.team_forced = 2;
414 player.team_forced = 3;
419 player.team_forced = 4;
425 player.team_forced = TEAM_FORCE_SPECTATOR;
430 player.team_forced = TEAM_FORCE_DEFAULT;
435 if (!teamplay && Player_HasRealForcedTeam(player))
437 player.team_forced = TEAM_FORCE_DEFAULT;
441 void TeamBalance_JoinBestTeam(entity player)
443 //PrintToChatAll(sprintf("TeamBalance_JoinBestTeam: %s", player.netname));
448 if (player.bot_forced_team)
452 entity balance = TeamBalance_CheckAllowedTeams(player);
453 if (Player_HasRealForcedTeam(player))
455 int forced_team_index = player.team_forced;
456 bool is_team_allowed = TeamBalance_IsTeamAllowedInternal(balance,
458 TeamBalance_Destroy(balance);
459 if (!is_team_allowed)
463 if (!SetPlayerTeam(player, forced_team_index, TEAM_CHANGE_AUTO))
469 int best_team_index = TeamBalance_FindBestTeam(balance, player, true);
470 TeamBalance_Destroy(balance);
471 if (!SetPlayerTeam(player, best_team_index, TEAM_CHANGE_AUTO))
477 entity TeamBalance_CheckAllowedTeams(entity for_whom)
479 entity balance = spawn();
480 for (int i = 0; i < NUM_TEAMS; ++i)
482 entity team_ent = balance.m_team_balance_team[i] = spawn();
483 team_ent.m_team_score = g_team_entities[i].m_team_score;
484 team_ent.m_num_players = TEAM_NOT_ALLOWED;
485 team_ent.m_num_bots = 0;
487 setthink(balance, TeamBalance_Destroy);
488 balance.nextthink = time;
491 string teament_name = string_null;
492 bool mutator_returnvalue = MUTATOR_CALLHOOK(TeamBalance_CheckAllowedTeams,
493 teams_mask, teament_name, for_whom);
494 teams_mask = M_ARGV(0, float);
495 teament_name = M_ARGV(1, string);
496 if (mutator_returnvalue)
498 for (int i = 0; i < NUM_TEAMS; ++i)
500 if (teams_mask & BIT(i))
502 balance.m_team_balance_team[i].m_num_players = 0;
509 entity head = find(NULL, classname, teament_name);
512 if (Team_IsValidTeam(head.team))
514 TeamBalance_GetTeam(balance, head.team).m_num_players = 0;
516 head = find(head, classname, teament_name);
520 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
521 if (autocvar_bot_vs_human && AvailableTeams() == 2 && for_whom)
523 if (autocvar_bot_vs_human > 0)
525 // find last team available
526 if (IS_BOT_CLIENT(for_whom))
528 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
530 TeamBalance_BanTeamsExcept(balance, 4);
532 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
534 TeamBalance_BanTeamsExcept(balance, 3);
538 TeamBalance_BanTeamsExcept(balance, 2);
540 // no further cases, we know at least 2 teams exist
544 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
546 TeamBalance_BanTeamsExcept(balance, 1);
548 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
550 TeamBalance_BanTeamsExcept(balance, 2);
554 TeamBalance_BanTeamsExcept(balance, 3);
556 // no further cases, bots have one of the teams
561 // find first team available
562 if (IS_BOT_CLIENT(for_whom))
564 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
566 TeamBalance_BanTeamsExcept(balance, 1);
568 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
570 TeamBalance_BanTeamsExcept(balance, 2);
574 TeamBalance_BanTeamsExcept(balance, 3);
576 // no further cases, we know at least 2 teams exist
580 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
582 TeamBalance_BanTeamsExcept(balance, 4);
584 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
586 TeamBalance_BanTeamsExcept(balance, 3);
590 TeamBalance_BanTeamsExcept(balance, 2);
592 // no further cases, bots have one of the teams
599 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
603 // if player has a forced team, ONLY allow that one
604 for (int i = 1; i <= NUM_TEAMS; ++i)
606 if (for_whom.team_forced == i &&
607 TeamBalance_IsTeamAllowedInternal(balance, i))
609 TeamBalance_BanTeamsExcept(balance, i);
613 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
617 void TeamBalance_Destroy(entity balance)
623 for (int i = 0; i < NUM_TEAMS; ++i)
625 delete(balance.(m_team_balance_team[i]));
630 int TeamBalance_GetAllowedTeams(entity balance)
634 LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
636 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
638 LOG_FATAL("TeamBalance_GetAllowedTeams: "
639 "Team balance entity is not initialized.");
642 for (int i = 1; i <= NUM_TEAMS; ++i)
644 if (TeamBalance_IsTeamAllowedInternal(balance, i))
646 result |= Team_IndexToBit(i);
652 bool TeamBalance_IsTeamAllowed(entity balance, int index)
656 LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
658 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
660 LOG_FATAL("TeamBalance_IsTeamAllowed: "
661 "Team balance entity is not initialized.");
663 if (!Team_IsValidIndex(index))
665 LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
668 return TeamBalance_IsTeamAllowedInternal(balance, index);
671 void TeamBalance_GetTeamCounts(entity balance, entity ignore)
675 LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
677 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
679 LOG_FATAL("TeamBalance_GetTeamCounts: "
680 "Team balance entity is not initialized.");
682 if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
684 // Mutator has overriden the configuration.
685 for (int i = 1; i <= NUM_TEAMS; ++i)
687 entity team_ent = TeamBalance_GetTeamFromIndex(balance, i);
688 if (TeamBalanceTeam_IsAllowed(team_ent))
690 MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, i, ignore);
691 team_ent.m_num_players = M_ARGV(2, float);
692 team_ent.m_num_bots = M_ARGV(3, float);
698 // Manually count all players.
706 // TODO: Reconsider when the player is truly on the team.
707 if (IS_CLIENT(it) || (it.caplayer))
711 else if (Player_HasRealForcedTeam(it))
713 // Do we really need this? Probably not.
714 team_num = Team_IndexToTeam(it.team_forced); // reserve the spot
720 if (!Team_IsValidTeam(team_num))
724 entity team_ent = TeamBalance_GetTeam(balance, team_num);
725 if (!TeamBalanceTeam_IsAllowed(team_ent))
729 ++team_ent.m_num_players;
730 if (IS_BOT_CLIENT(it))
732 ++team_ent.m_num_bots;
737 // if the player who has a forced team has not joined yet, reserve the spot
738 if (autocvar_g_campaign)
740 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
742 entity team_ent = TeamBalance_GetTeamFromIndex(balance,
743 autocvar_g_campaign_forceteam);
744 if (team_ent.m_num_players == team_ent.m_num_bots)
746 ++team_ent.m_num_players;
750 balance.m_team_balance_state = TEAM_BALANCE_TEAM_COUNTS_FILLED;
753 int TeamBalance_GetNumberOfPlayers(entity balance, int index)
757 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
758 "Team balance entity is NULL.");
760 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
762 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
763 "TeamBalance_GetTeamCounts has not been called.");
765 if (!Team_IsValidIndex(index))
767 LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
770 return balance.m_team_balance_team[index - 1].m_num_players;
773 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
777 LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
779 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
781 LOG_FATAL("TeamBalance_FindBestTeam: "
782 "Team balance entity is not initialized.");
784 // count how many players are in each team
787 TeamBalance_GetTeamCounts(balance, player);
791 TeamBalance_GetTeamCounts(balance, NULL);
793 int team_bits = TeamBalance_FindBestTeams(balance, player, true);
796 LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
797 MapInfo_Type_ToString(MapInfo_CurrentGametype()));
799 RandomSelection_Init();
800 for (int i = 1; i <= NUM_TEAMS; ++i)
802 if (team_bits & Team_IndexToBit(i))
804 RandomSelection_AddFloat(i, 1, 1);
807 return RandomSelection_chosen_float;
810 int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
814 LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
816 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
818 LOG_FATAL("TeamBalance_FindBestTeams: "
819 "TeamBalance_GetTeamCounts has not been called.");
821 if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
823 return M_ARGV(1, float);
826 int previous_team = 0;
827 for (int i = 1; i <= NUM_TEAMS; ++i)
829 if (!TeamBalance_IsTeamAllowedInternal(balance, i))
833 if (previous_team == 0)
835 team_bits = Team_IndexToBit(i);
839 int compare = TeamBalance_CompareTeams(balance, i, previous_team,
841 if (compare == TEAMS_COMPARE_LESS)
843 team_bits = Team_IndexToBit(i);
847 if (compare == TEAMS_COMPARE_EQUAL)
849 team_bits |= Team_IndexToBit(i);
856 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
857 entity player, bool use_score)
861 LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
863 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
865 LOG_FATAL("TeamBalance_CompareTeams: "
866 "TeamBalance_GetTeamCounts has not been called.");
868 if (!Team_IsValidIndex(team_index_a))
870 LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
873 if (!Team_IsValidIndex(team_index_b))
875 LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
878 if (team_index_a == team_index_b)
880 return TEAMS_COMPARE_EQUAL;
882 entity team_a = TeamBalance_GetTeamFromIndex(balance, team_index_a);
883 entity team_b = TeamBalance_GetTeamFromIndex(balance, team_index_b);
884 return TeamBalance_CompareTeamsInternal(team_a, team_b, player, use_score);
887 void TeamBalance_AutoBalanceBots()
889 // checks disabled because we always want auto-balanced bots
890 //if (!(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance))
893 entity balance = TeamBalance_CheckAllowedTeams(NULL);
894 TeamBalance_GetTeamCounts(balance, NULL);
895 int smallest_team_index = 0;
896 int smallest_team_player_count = 0;
897 for (int i = 1; i <= NUM_TEAMS; ++i)
899 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
900 if (!TeamBalanceTeam_IsAllowed(team_))
904 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
905 if (smallest_team_index == 0)
907 smallest_team_index = i;
908 smallest_team_player_count = playercount;
910 else if (playercount < smallest_team_player_count)
912 smallest_team_index = i;
913 smallest_team_player_count = playercount;
916 //PrintToChatAll(sprintf("Smallest team: %f", smallest_team_index));
917 //PrintToChatAll(sprintf("Smallest team players: %f", smallest_team_player_count));
918 entity switchable_bot = NULL;
919 int teams = BITS(NUM_TEAMS);
922 int largest_team_index = TeamBalance_GetLargestTeamIndex(balance,
924 if (smallest_team_index == largest_team_index)
926 TeamBalance_Destroy(balance);
929 entity largest_team = TeamBalance_GetTeamFromIndex(balance,
931 int largest_team_player_count = TeamBalanceTeam_GetNumberOfPlayers(
933 if (largest_team_player_count - smallest_team_player_count < 2)
935 TeamBalance_Destroy(balance);
938 //PrintToChatAll(sprintf("Largest team: %f", largest_team_index));
939 //PrintToChatAll(sprintf("Largest team players: %f", largest_team_player_count));
940 switchable_bot = TeamBalance_GetPlayerForTeamSwitch(largest_team_index,
941 smallest_team_index, true);
942 if (switchable_bot != NULL)
946 teams &= ~Team_IndexToBit(largest_team_index);
948 TeamBalance_Destroy(balance);
949 if (switchable_bot == NULL)
951 //PrintToChatAll("No bot found after searching through all the teams");
954 SetPlayerTeam(switchable_bot, smallest_team_index, TEAM_CHANGE_AUTO);
957 int TeamBalance_GetLargestTeamIndex(entity balance, int teams)
959 int largest_team_index = 0;
960 int largest_team_player_count = 0;
961 for (int i = 1; i <= NUM_TEAMS; ++i)
963 if (!(Team_IndexToBit(i) & teams))
967 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
968 if (!TeamBalanceTeam_IsAllowed(team_))
972 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
973 if (largest_team_index == 0)
975 largest_team_index = i;
976 largest_team_player_count = playercount;
978 else if (playercount > largest_team_player_count)
980 largest_team_index = i;
981 largest_team_player_count = playercount;
984 return largest_team_index;
987 entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index,
988 int destination_team_index, bool is_bot)
990 if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, source_team_index,
991 destination_team_index, is_bot))
993 return M_ARGV(3, entity);
995 entity lowest_player = NULL;
996 float lowest_score = FLOAT_MAX;
997 FOREACH_CLIENT(Entity_GetTeamIndex(it) == source_team_index,
999 if (IS_BOT_CLIENT(it) != is_bot)
1003 float temp_score = PlayerScore_Get(it, SP_SCORE);
1004 if (temp_score >= lowest_score)
1008 //PrintToChatAll(sprintf(
1009 // "Found %s with lowest score, checking allowed teams", it.netname));
1010 entity balance = TeamBalance_CheckAllowedTeams(it);
1011 if (TeamBalance_IsTeamAllowed(balance, source_team_index))
1013 //PrintToChatAll("Allowed");
1015 lowest_score = temp_score;
1019 //PrintToChatAll("Not allowed");
1021 TeamBalance_Destroy(balance);
1023 return lowest_player;
1026 void LogTeamChange(float player_id, float team_number, int type)
1028 if (!autocvar_sv_eventlog)
1036 GameLogEcho(sprintf(":team:%d:%d:%d", player_id, team_number, type));
1039 void KillPlayerForTeamChange(entity player)
1041 if (IS_DEAD(player))
1045 if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
1049 Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
1050 player.origin, '0 0 0');
1053 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
1055 return balance.m_team_balance_team[index - 1].m_num_players !=
1059 void TeamBalance_BanTeamsExcept(entity balance, int index)
1061 for (int i = 1; i <= NUM_TEAMS; ++i)
1065 balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
1070 entity TeamBalance_GetTeamFromIndex(entity balance, int index)
1072 if (!Team_IsValidIndex(index))
1074 LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
1076 return balance.m_team_balance_team[index - 1];
1079 entity TeamBalance_GetTeam(entity balance, int team_num)
1081 return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
1084 bool TeamBalanceTeam_IsAllowed(entity team_ent)
1086 return team_ent.m_num_players != TEAM_NOT_ALLOWED;
1089 int TeamBalanceTeam_GetNumberOfPlayers(entity team_ent)
1091 return team_ent.m_num_players;
1094 int TeamBalanceTeam_GetNumberOfBots(entity team_ent)
1096 return team_ent.m_num_bots;
1099 int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
1100 entity player, bool use_score)
1102 if (team_a == team_b)
1104 return TEAMS_COMPARE_EQUAL;
1106 if (!TeamBalanceTeam_IsAllowed(team_a) ||
1107 !TeamBalanceTeam_IsAllowed(team_b))
1109 return TEAMS_COMPARE_INVALID;
1111 int num_players_team_a = team_a.m_num_players;
1112 int num_players_team_b = team_b.m_num_players;
1113 if (IS_REAL_CLIENT(player) && bots_would_leave)
1115 num_players_team_a -= team_a.m_num_bots;
1116 num_players_team_b -= team_b.m_num_bots;
1118 if (num_players_team_a < num_players_team_b)
1120 return TEAMS_COMPARE_LESS;
1122 if (num_players_team_a > num_players_team_b)
1124 return TEAMS_COMPARE_GREATER;
1128 return TEAMS_COMPARE_EQUAL;
1130 if (team_a.m_team_score < team_b.m_team_score)
1132 return TEAMS_COMPARE_LESS;
1134 if (team_a.m_team_score > team_b.m_team_score)
1136 return TEAMS_COMPARE_GREATER;
1138 return TEAMS_COMPARE_EQUAL;
1141 void SV_ChangeTeam(entity player, int new_color)
1145 SetPlayerColors(player, new_color);
1147 // TODO: Should we really bother with this?
1148 if(!IS_CLIENT(player))
1150 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
1151 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING,
1159 Player_SetTeamIndexChecked(player, Team_TeamToIndex((new_color & 0x0F) +