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 .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
184 // [BOT]Lion: color 0 4.
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 if (!CS(player).just_joined)
227 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE,
231 KillPlayerForTeamChange(player);
232 if (!IS_BOT_CLIENT(player))
234 TeamBalance_AutoBalanceBots();
237 else if (team_index == -1)
239 if (!CS(player).just_joined && player.frags != FRAGS_SPECTATOR)
240 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE, player.netname);
245 void Player_SetTeamIndexChecked(entity player, int team_index)
251 if (!Team_IsValidIndex(team_index))
255 if ((autocvar_g_campaign) || (autocvar_g_changeteam_banned &&
256 CS(player).wasplayer))
258 Send_Notification(NOTIF_ONE, player, MSG_INFO,
259 INFO_TEAMCHANGE_NOTALLOWED);
262 entity balance = TeamBalance_CheckAllowedTeams(player);
263 if (team_index == 1 && !TeamBalance_IsTeamAllowedInternal(balance, 1))
267 if (team_index == 4 && !TeamBalance_IsTeamAllowedInternal(balance, 4))
271 if (team_index == 3 && !TeamBalance_IsTeamAllowedInternal(balance, 3))
275 if (team_index == 2 && !TeamBalance_IsTeamAllowedInternal(balance, 2))
279 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
280 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
282 TeamBalance_GetTeamCounts(balance, player);
283 if ((Team_IndexToBit(team_index) & TeamBalance_FindBestTeams(balance,
284 player, false)) == 0)
286 Send_Notification(NOTIF_ONE, player, MSG_INFO,
287 INFO_TEAMCHANGE_LARGERTEAM);
288 TeamBalance_Destroy(balance);
292 TeamBalance_Destroy(balance);
293 SetPlayerTeam(player, team_index, TEAM_CHANGE_MANUAL);
296 bool MoveToTeam(entity client, int team_index, int type)
298 //PrintToChatAll(sprintf("MoveToTeam: %s, %f", client.netname, team_index));
299 int lockteams_backup = lockteams; // backup any team lock
300 lockteams = 0; // disable locked teams
301 if (!SetPlayerTeam(client, team_index, type))
303 lockteams = lockteams_backup; // restore the team lock
306 lockteams = lockteams_backup; // restore the team lock
310 bool Player_HasRealForcedTeam(entity player)
312 return player.team_forced > TEAM_FORCE_DEFAULT;
315 int Player_GetForcedTeamIndex(entity player)
317 return player.team_forced;
320 void Player_SetForcedTeamIndex(entity player, int team_index)
324 case TEAM_FORCE_SPECTATOR:
325 case TEAM_FORCE_DEFAULT:
327 player.team_forced = team_index;
332 if (!Team_IsValidIndex(team_index))
334 LOG_FATAL("Player_SetForcedTeamIndex: Invalid team index.");
338 player.team_forced = team_index;
345 void Player_DetermineForcedTeam(entity player)
347 if (autocvar_g_campaign)
349 if (IS_REAL_CLIENT(player)) // only players, not bots
351 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
353 player.team_forced = autocvar_g_campaign_forceteam;
357 player.team_forced = TEAM_FORCE_DEFAULT;
361 else if (PlayerInList(player, autocvar_g_forced_team_red))
363 player.team_forced = 1;
365 else if (PlayerInList(player, autocvar_g_forced_team_blue))
367 player.team_forced = 2;
369 else if (PlayerInList(player, autocvar_g_forced_team_yellow))
371 player.team_forced = 3;
373 else if (PlayerInList(player, autocvar_g_forced_team_pink))
375 player.team_forced = 4;
379 switch (autocvar_g_forced_team_otherwise)
383 player.team_forced = 1;
388 player.team_forced = 2;
393 player.team_forced = 3;
398 player.team_forced = 4;
404 player.team_forced = TEAM_FORCE_SPECTATOR;
409 player.team_forced = TEAM_FORCE_DEFAULT;
414 if (!teamplay && Player_HasRealForcedTeam(player))
416 player.team_forced = TEAM_FORCE_DEFAULT;
420 void TeamBalance_JoinBestTeam(entity player)
422 //PrintToChatAll(sprintf("TeamBalance_JoinBestTeam: %s", player.netname));
427 if (player.bot_forced_team)
431 entity balance = TeamBalance_CheckAllowedTeams(player);
432 if (Player_HasRealForcedTeam(player))
434 int forced_team_index = player.team_forced;
435 bool is_team_allowed = TeamBalance_IsTeamAllowedInternal(balance,
437 TeamBalance_Destroy(balance);
438 if (!is_team_allowed)
442 if (!SetPlayerTeam(player, forced_team_index, TEAM_CHANGE_AUTO))
448 int best_team_index = TeamBalance_FindBestTeam(balance, player, true);
449 TeamBalance_Destroy(balance);
450 if (!SetPlayerTeam(player, best_team_index, TEAM_CHANGE_AUTO))
456 entity TeamBalance_CheckAllowedTeams(entity for_whom)
458 entity balance = spawn();
459 for (int i = 0; i < NUM_TEAMS; ++i)
461 entity team_ent = balance.m_team_balance_team[i] = spawn();
462 team_ent.m_team_score = g_team_entities[i].m_team_score;
463 team_ent.m_num_players = TEAM_NOT_ALLOWED;
464 team_ent.m_num_bots = 0;
466 setthink(balance, TeamBalance_Destroy);
467 balance.nextthink = time;
470 string teament_name = string_null;
471 bool mutator_returnvalue = MUTATOR_CALLHOOK(TeamBalance_CheckAllowedTeams,
472 teams_mask, teament_name, for_whom);
473 teams_mask = M_ARGV(0, float);
474 teament_name = M_ARGV(1, string);
475 if (mutator_returnvalue)
477 for (int i = 0; i < NUM_TEAMS; ++i)
479 if (teams_mask & BIT(i))
481 balance.m_team_balance_team[i].m_num_players = 0;
488 entity head = find(NULL, classname, teament_name);
491 if (Team_IsValidTeam(head.team))
493 TeamBalance_GetTeam(balance, head.team).m_num_players = 0;
495 head = find(head, classname, teament_name);
499 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
500 if (autocvar_bot_vs_human && AvailableTeams() == 2 && for_whom)
502 if (autocvar_bot_vs_human > 0)
504 // find last team available
505 if (IS_BOT_CLIENT(for_whom))
507 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
509 TeamBalance_BanTeamsExcept(balance, 4);
511 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
513 TeamBalance_BanTeamsExcept(balance, 3);
517 TeamBalance_BanTeamsExcept(balance, 2);
519 // no further cases, we know at least 2 teams exist
523 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
525 TeamBalance_BanTeamsExcept(balance, 1);
527 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
529 TeamBalance_BanTeamsExcept(balance, 2);
533 TeamBalance_BanTeamsExcept(balance, 3);
535 // no further cases, bots have one of the teams
540 // find first team available
541 if (IS_BOT_CLIENT(for_whom))
543 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
545 TeamBalance_BanTeamsExcept(balance, 1);
547 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
549 TeamBalance_BanTeamsExcept(balance, 2);
553 TeamBalance_BanTeamsExcept(balance, 3);
555 // no further cases, we know at least 2 teams exist
559 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
561 TeamBalance_BanTeamsExcept(balance, 4);
563 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
565 TeamBalance_BanTeamsExcept(balance, 3);
569 TeamBalance_BanTeamsExcept(balance, 2);
571 // no further cases, bots have one of the teams
578 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
582 // if player has a forced team, ONLY allow that one
583 for (int i = 1; i <= NUM_TEAMS; ++i)
585 if (for_whom.team_forced == i &&
586 TeamBalance_IsTeamAllowedInternal(balance, i))
588 TeamBalance_BanTeamsExcept(balance, i);
592 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
596 void TeamBalance_Destroy(entity balance)
602 for (int i = 0; i < NUM_TEAMS; ++i)
604 delete(balance.(m_team_balance_team[i]));
609 int TeamBalance_GetAllowedTeams(entity balance)
613 LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
615 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
617 LOG_FATAL("TeamBalance_GetAllowedTeams: "
618 "Team balance entity is not initialized.");
621 for (int i = 1; i <= NUM_TEAMS; ++i)
623 if (TeamBalance_IsTeamAllowedInternal(balance, i))
625 result |= Team_IndexToBit(i);
631 bool TeamBalance_IsTeamAllowed(entity balance, int index)
635 LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
637 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
639 LOG_FATAL("TeamBalance_IsTeamAllowed: "
640 "Team balance entity is not initialized.");
642 if (!Team_IsValidIndex(index))
644 LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
647 return TeamBalance_IsTeamAllowedInternal(balance, index);
650 void TeamBalance_GetTeamCounts(entity balance, entity ignore)
654 LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
656 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
658 LOG_FATAL("TeamBalance_GetTeamCounts: "
659 "Team balance entity is not initialized.");
661 if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
663 // Mutator has overriden the configuration.
664 for (int i = 1; i <= NUM_TEAMS; ++i)
666 entity team_ent = TeamBalance_GetTeamFromIndex(balance, i);
667 if (TeamBalanceTeam_IsAllowed(team_ent))
669 MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, i, ignore);
670 team_ent.m_num_players = M_ARGV(2, float);
671 team_ent.m_num_bots = M_ARGV(3, float);
677 // Manually count all players.
685 // TODO: Reconsider when the player is truly on the team.
686 if (IS_CLIENT(it) || (it.caplayer))
690 else if (Player_HasRealForcedTeam(it))
692 // Do we really need this? Probably not.
693 team_num = Team_IndexToTeam(it.team_forced); // reserve the spot
699 if (!Team_IsValidTeam(team_num))
703 entity team_ent = TeamBalance_GetTeam(balance, team_num);
704 if (!TeamBalanceTeam_IsAllowed(team_ent))
708 ++team_ent.m_num_players;
709 if (IS_BOT_CLIENT(it))
711 ++team_ent.m_num_bots;
716 // if the player who has a forced team has not joined yet, reserve the spot
717 if (autocvar_g_campaign)
719 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
721 entity team_ent = TeamBalance_GetTeamFromIndex(balance,
722 autocvar_g_campaign_forceteam);
723 if (team_ent.m_num_players == team_ent.m_num_bots)
725 ++team_ent.m_num_players;
729 balance.m_team_balance_state = TEAM_BALANCE_TEAM_COUNTS_FILLED;
732 int TeamBalance_GetNumberOfPlayers(entity balance, int index)
736 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
737 "Team balance entity is NULL.");
739 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
741 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
742 "TeamBalance_GetTeamCounts has not been called.");
744 if (!Team_IsValidIndex(index))
746 LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
749 return balance.m_team_balance_team[index - 1].m_num_players;
752 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
756 LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
758 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
760 LOG_FATAL("TeamBalance_FindBestTeam: "
761 "Team balance entity is not initialized.");
763 // count how many players are in each team
766 TeamBalance_GetTeamCounts(balance, player);
770 TeamBalance_GetTeamCounts(balance, NULL);
772 int team_bits = TeamBalance_FindBestTeams(balance, player, true);
775 LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
776 MapInfo_Type_ToString(MapInfo_CurrentGametype()));
778 RandomSelection_Init();
779 for (int i = 1; i <= NUM_TEAMS; ++i)
781 if (team_bits & Team_IndexToBit(i))
783 RandomSelection_AddFloat(i, 1, 1);
786 return RandomSelection_chosen_float;
789 int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
793 LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
795 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
797 LOG_FATAL("TeamBalance_FindBestTeams: "
798 "TeamBalance_GetTeamCounts has not been called.");
800 if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
802 return M_ARGV(1, float);
805 int previous_team = 0;
806 for (int i = 1; i <= NUM_TEAMS; ++i)
808 if (!TeamBalance_IsTeamAllowedInternal(balance, i))
812 if (previous_team == 0)
814 team_bits = Team_IndexToBit(i);
818 int compare = TeamBalance_CompareTeams(balance, i, previous_team,
820 if (compare == TEAMS_COMPARE_LESS)
822 team_bits = Team_IndexToBit(i);
826 if (compare == TEAMS_COMPARE_EQUAL)
828 team_bits |= Team_IndexToBit(i);
835 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
836 entity player, bool use_score)
840 LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
842 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
844 LOG_FATAL("TeamBalance_CompareTeams: "
845 "TeamBalance_GetTeamCounts has not been called.");
847 if (!Team_IsValidIndex(team_index_a))
849 LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
852 if (!Team_IsValidIndex(team_index_b))
854 LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
857 if (team_index_a == team_index_b)
859 return TEAMS_COMPARE_EQUAL;
861 entity team_a = TeamBalance_GetTeamFromIndex(balance, team_index_a);
862 entity team_b = TeamBalance_GetTeamFromIndex(balance, team_index_b);
863 return TeamBalance_CompareTeamsInternal(team_a, team_b, player, use_score);
866 void TeamBalance_AutoBalanceBots()
868 // checks disabled because we always want auto-balanced bots
869 //if (!(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance))
872 entity balance = TeamBalance_CheckAllowedTeams(NULL);
873 TeamBalance_GetTeamCounts(balance, NULL);
874 int smallest_team_index = 0;
875 int smallest_team_player_count = 0;
876 for (int i = 1; i <= NUM_TEAMS; ++i)
878 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
879 if (!TeamBalanceTeam_IsAllowed(team_))
883 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
884 if (smallest_team_index == 0)
886 smallest_team_index = i;
887 smallest_team_player_count = playercount;
889 else if (playercount < smallest_team_player_count)
891 smallest_team_index = i;
892 smallest_team_player_count = playercount;
895 //PrintToChatAll(sprintf("Smallest team: %f", smallest_team_index));
896 //PrintToChatAll(sprintf("Smallest team players: %f", smallest_team_player_count));
897 entity switchable_bot = NULL;
898 int teams = BITS(NUM_TEAMS);
901 int largest_team_index = TeamBalance_GetLargestTeamIndex(balance,
903 if (smallest_team_index == largest_team_index)
905 TeamBalance_Destroy(balance);
908 entity largest_team = TeamBalance_GetTeamFromIndex(balance,
910 int largest_team_player_count = TeamBalanceTeam_GetNumberOfPlayers(
912 if (largest_team_player_count - smallest_team_player_count < 2)
914 TeamBalance_Destroy(balance);
917 //PrintToChatAll(sprintf("Largest team: %f", largest_team_index));
918 //PrintToChatAll(sprintf("Largest team players: %f", largest_team_player_count));
919 switchable_bot = TeamBalance_GetPlayerForTeamSwitch(largest_team_index,
920 smallest_team_index, true);
921 if (switchable_bot != NULL)
925 teams &= ~Team_IndexToBit(largest_team_index);
927 TeamBalance_Destroy(balance);
928 if (switchable_bot == NULL)
930 //PrintToChatAll("No bot found after searching through all the teams");
933 SetPlayerTeam(switchable_bot, smallest_team_index, TEAM_CHANGE_AUTO);
936 int TeamBalance_GetLargestTeamIndex(entity balance, int teams)
938 int largest_team_index = 0;
939 int largest_team_player_count = 0;
940 for (int i = 1; i <= NUM_TEAMS; ++i)
942 if (!(Team_IndexToBit(i) & teams))
946 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
947 if (!TeamBalanceTeam_IsAllowed(team_))
951 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
952 if (largest_team_index == 0)
954 largest_team_index = i;
955 largest_team_player_count = playercount;
957 else if (playercount > largest_team_player_count)
959 largest_team_index = i;
960 largest_team_player_count = playercount;
963 return largest_team_index;
966 entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index,
967 int destination_team_index, bool is_bot)
969 if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, source_team_index,
970 destination_team_index, is_bot))
972 return M_ARGV(3, entity);
974 entity lowest_player = NULL;
975 float lowest_score = FLOAT_MAX;
976 FOREACH_CLIENT(Entity_GetTeamIndex(it) == source_team_index,
978 if (IS_BOT_CLIENT(it) != is_bot)
982 float temp_score = PlayerScore_Get(it, SP_SCORE);
983 if (temp_score >= lowest_score)
987 //PrintToChatAll(sprintf(
988 // "Found %s with lowest score, checking allowed teams", it.netname));
989 entity balance = TeamBalance_CheckAllowedTeams(it);
990 if (TeamBalance_IsTeamAllowed(balance, source_team_index))
992 //PrintToChatAll("Allowed");
994 lowest_score = temp_score;
998 //PrintToChatAll("Not allowed");
1000 TeamBalance_Destroy(balance);
1002 return lowest_player;
1005 void LogTeamChange(float player_id, float team_number, int type)
1007 if (!autocvar_sv_eventlog)
1015 GameLogEcho(sprintf(":team:%d:%d:%d", player_id, team_number, type));
1018 void KillPlayerForTeamChange(entity player)
1020 if (IS_DEAD(player))
1024 if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
1028 Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
1029 player.origin, '0 0 0');
1032 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
1034 return balance.m_team_balance_team[index - 1].m_num_players !=
1038 void TeamBalance_BanTeamsExcept(entity balance, int index)
1040 for (int i = 1; i <= NUM_TEAMS; ++i)
1044 balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
1049 entity TeamBalance_GetTeamFromIndex(entity balance, int index)
1051 if (!Team_IsValidIndex(index))
1053 LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
1055 return balance.m_team_balance_team[index - 1];
1058 entity TeamBalance_GetTeam(entity balance, int team_num)
1060 return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
1063 bool TeamBalanceTeam_IsAllowed(entity team_ent)
1065 return team_ent.m_num_players != TEAM_NOT_ALLOWED;
1068 int TeamBalanceTeam_GetNumberOfPlayers(entity team_ent)
1070 return team_ent.m_num_players;
1073 int TeamBalanceTeam_GetNumberOfBots(entity team_ent)
1075 return team_ent.m_num_bots;
1078 int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
1079 entity player, bool use_score)
1081 if (team_a == team_b)
1083 return TEAMS_COMPARE_EQUAL;
1085 if (!TeamBalanceTeam_IsAllowed(team_a) ||
1086 !TeamBalanceTeam_IsAllowed(team_b))
1088 return TEAMS_COMPARE_INVALID;
1090 int num_players_team_a = team_a.m_num_players;
1091 int num_players_team_b = team_b.m_num_players;
1092 if (IS_REAL_CLIENT(player) && bots_would_leave)
1094 num_players_team_a -= team_a.m_num_bots;
1095 num_players_team_b -= team_b.m_num_bots;
1097 if (num_players_team_a < num_players_team_b)
1099 return TEAMS_COMPARE_LESS;
1101 if (num_players_team_a > num_players_team_b)
1103 return TEAMS_COMPARE_GREATER;
1107 return TEAMS_COMPARE_EQUAL;
1109 if (team_a.m_team_score < team_b.m_team_score)
1111 return TEAMS_COMPARE_LESS;
1113 if (team_a.m_team_score > team_b.m_team_score)
1115 return TEAMS_COMPARE_GREATER;
1117 return TEAMS_COMPARE_EQUAL;
1120 void SV_ChangeTeam(entity player, int new_color)
1124 SetPlayerColors(player, new_color);
1126 // TODO: Should we really bother with this?
1127 if(!IS_CLIENT(player))
1129 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
1130 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING,
1138 Player_SetTeamIndexChecked(player, Team_TeamToIndex((new_color & 0x0F) +