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.
51 if (g_team_entities[0])
53 for (int i = 0; i < NUM_TEAMS; ++i)
55 g_team_entities[i] = new_pure(team_entity);
59 entity Team_GetTeamFromIndex(int index)
61 if (!Team_IsValidIndex(index))
63 LOG_FATALF("Team_GetTeamFromIndex: Index is invalid: %f", index);
65 return g_team_entities[index - 1];
68 entity Team_GetTeam(int team_num)
70 if (!Team_IsValidTeam(team_num))
72 LOG_FATALF("Team_GetTeam: Value is invalid: %f", team_num);
74 return g_team_entities[Team_TeamToIndex(team_num) - 1];
77 float Team_GetTeamScore(entity team_ent)
79 return team_ent.m_team_score;
82 void Team_SetTeamScore(entity team_ent, float score)
84 team_ent.m_team_score = score;
87 int Team_GetNumberOfAlivePlayers(entity team_ent)
89 return team_ent.m_num_players_alive;
92 void Team_SetNumberOfAlivePlayers(entity team_ent, int number)
94 team_ent.m_num_players_alive = number;
97 int Team_GetNumberOfAliveTeams()
100 for (int i = 0; i < NUM_TEAMS; ++i)
102 if (g_team_entities[i].m_num_players_alive > 0)
110 int Team_GetNumberOfControlPoints(entity team_ent)
112 return team_ent.m_num_control_points;
115 void Team_SetNumberOfControlPoints(entity team_ent, int number)
117 team_ent.m_num_control_points = number;
120 int Team_GetNumberOfTeamsWithControlPoints()
123 for (int i = 0; i < NUM_TEAMS; ++i)
125 if (g_team_entities[i].m_num_control_points > 0)
133 void setcolor(entity this, int clr)
136 this.clientcolors = clr;
137 this.team = (clr & 15) + 1;
139 builtin_setcolor(this, clr);
143 bool Entity_HasValidTeam(entity this)
145 return Team_IsValidTeam(this.team);
148 int Entity_GetTeamIndex(entity this)
150 return Team_TeamToIndex(this.team);
153 entity Entity_GetTeam(entity this)
155 int index = Entity_GetTeamIndex(this);
156 if (!Team_IsValidIndex(index))
160 return Team_GetTeamFromIndex(index);
163 void SetPlayerColors(entity player, float _color)
165 float pants = _color & 0x0F;
166 float shirt = _color & 0xF0;
169 setcolor(player, 16 * pants + pants);
173 setcolor(player, shirt + pants);
177 bool Player_SetTeamIndex(entity player, int index)
179 int new_team = Team_IndexToTeam(index);
180 if (player.team == new_team)
184 // This is important when players join the game and one of their
185 // color matches the team color while other doesn't. For example
186 // [BOT]Lion: color 0 4.
187 SetPlayerColors(player, new_team - 1);
191 int old_index = Team_TeamToIndex(player.team);
192 if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, old_index, index) == true)
194 // Mutator has blocked team change.
203 SetPlayerColors(player, new_team - 1);
205 MUTATOR_CALLHOOK(Player_ChangedTeam, player, old_index, index);
209 bool SetPlayerTeam(entity player, int team_index, int type)
211 int old_team_index = Entity_GetTeamIndex(player);
212 if (!Player_SetTeamIndex(player, team_index))
216 LogTeamChange(player.playerid, player.team, type);
217 if (team_index != old_team_index)
219 PlayerScore_Clear(player);
220 if (team_index != -1)
222 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(
223 player.team, INFO_JOIN_PLAY_TEAM), player.netname);
227 if (!CS(player).just_joined)
229 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE,
233 KillPlayerForTeamChange(player);
234 if (!IS_BOT_CLIENT(player))
236 TeamBalance_AutoBalanceBots();
239 else if (team_index == -1)
241 if (!CS(player).just_joined && player.frags != FRAGS_SPECTATOR)
242 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE, player.netname);
247 void Player_SetTeamIndexChecked(entity player, int team_index)
253 if (!Team_IsValidIndex(team_index))
257 if ((autocvar_g_campaign) || (autocvar_g_changeteam_banned &&
258 CS(player).wasplayer))
260 Send_Notification(NOTIF_ONE, player, MSG_INFO,
261 INFO_TEAMCHANGE_NOTALLOWED);
264 entity balance = TeamBalance_CheckAllowedTeams(player);
265 if (team_index == 1 && !TeamBalance_IsTeamAllowedInternal(balance, 1))
269 if (team_index == 4 && !TeamBalance_IsTeamAllowedInternal(balance, 4))
273 if (team_index == 3 && !TeamBalance_IsTeamAllowedInternal(balance, 3))
277 if (team_index == 2 && !TeamBalance_IsTeamAllowedInternal(balance, 2))
281 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
282 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
284 TeamBalance_GetTeamCounts(balance, player);
285 if ((Team_IndexToBit(team_index) & TeamBalance_FindBestTeams(balance,
286 player, false)) == 0)
288 Send_Notification(NOTIF_ONE, player, MSG_INFO,
289 INFO_TEAMCHANGE_LARGERTEAM);
290 TeamBalance_Destroy(balance);
294 TeamBalance_Destroy(balance);
295 SetPlayerTeam(player, team_index, TEAM_CHANGE_MANUAL);
298 bool MoveToTeam(entity client, int team_index, int type)
300 //PrintToChatAll(sprintf("MoveToTeam: %s, %f", client.netname, team_index));
301 int lockteams_backup = lockteams; // backup any team lock
302 lockteams = 0; // disable locked teams
303 if (!SetPlayerTeam(client, team_index, type))
305 lockteams = lockteams_backup; // restore the team lock
308 lockteams = lockteams_backup; // restore the team lock
312 bool Player_HasRealForcedTeam(entity player)
314 return player.team_forced > TEAM_FORCE_DEFAULT;
317 int Player_GetForcedTeamIndex(entity player)
319 return player.team_forced;
322 void Player_SetForcedTeamIndex(entity player, int team_index)
326 case TEAM_FORCE_SPECTATOR:
327 case TEAM_FORCE_DEFAULT:
329 player.team_forced = team_index;
334 if (!Team_IsValidIndex(team_index))
336 LOG_FATAL("Player_SetForcedTeamIndex: Invalid team index.");
340 player.team_forced = team_index;
347 void Player_DetermineForcedTeam(entity player)
349 if (autocvar_g_campaign)
351 if (IS_REAL_CLIENT(player)) // only players, not bots
353 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
355 player.team_forced = autocvar_g_campaign_forceteam;
359 player.team_forced = TEAM_FORCE_DEFAULT;
363 else if (PlayerInList(player, autocvar_g_forced_team_red))
365 player.team_forced = 1;
367 else if (PlayerInList(player, autocvar_g_forced_team_blue))
369 player.team_forced = 2;
371 else if (PlayerInList(player, autocvar_g_forced_team_yellow))
373 player.team_forced = 3;
375 else if (PlayerInList(player, autocvar_g_forced_team_pink))
377 player.team_forced = 4;
381 switch (autocvar_g_forced_team_otherwise)
385 player.team_forced = 1;
390 player.team_forced = 2;
395 player.team_forced = 3;
400 player.team_forced = 4;
406 player.team_forced = TEAM_FORCE_SPECTATOR;
411 player.team_forced = TEAM_FORCE_DEFAULT;
416 if (!teamplay && Player_HasRealForcedTeam(player))
418 player.team_forced = TEAM_FORCE_DEFAULT;
422 void TeamBalance_JoinBestTeam(entity player)
424 //PrintToChatAll(sprintf("TeamBalance_JoinBestTeam: %s", player.netname));
429 if (player.bot_forced_team)
433 entity balance = TeamBalance_CheckAllowedTeams(player);
434 if (Player_HasRealForcedTeam(player))
436 int forced_team_index = player.team_forced;
437 bool is_team_allowed = TeamBalance_IsTeamAllowedInternal(balance,
439 TeamBalance_Destroy(balance);
440 if (!is_team_allowed)
444 if (!SetPlayerTeam(player, forced_team_index, TEAM_CHANGE_AUTO))
450 int best_team_index = TeamBalance_FindBestTeam(balance, player, true);
451 TeamBalance_Destroy(balance);
452 if (!SetPlayerTeam(player, best_team_index, TEAM_CHANGE_AUTO))
458 entity TeamBalance_CheckAllowedTeams(entity for_whom)
460 entity balance = spawn();
461 for (int i = 0; i < NUM_TEAMS; ++i)
463 entity team_ent = balance.m_team_balance_team[i] = spawn();
464 team_ent.m_team_score = g_team_entities[i].m_team_score;
465 team_ent.m_num_players = TEAM_NOT_ALLOWED;
466 team_ent.m_num_bots = 0;
468 setthink(balance, TeamBalance_Destroy);
469 balance.nextthink = time;
472 string teament_name = string_null;
473 bool mutator_returnvalue = MUTATOR_CALLHOOK(TeamBalance_CheckAllowedTeams,
474 teams_mask, teament_name, for_whom);
475 teams_mask = M_ARGV(0, float);
476 teament_name = M_ARGV(1, string);
477 if (mutator_returnvalue)
479 for (int i = 0; i < NUM_TEAMS; ++i)
481 if (teams_mask & BIT(i))
483 balance.m_team_balance_team[i].m_num_players = 0;
490 entity head = find(NULL, classname, teament_name);
493 if (Team_IsValidTeam(head.team))
495 TeamBalance_GetTeam(balance, head.team).m_num_players = 0;
497 head = find(head, classname, teament_name);
501 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
502 if (autocvar_bot_vs_human && AvailableTeams() == 2 && for_whom)
504 if (autocvar_bot_vs_human > 0)
506 // find last team available
507 if (IS_BOT_CLIENT(for_whom))
509 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
511 TeamBalance_BanTeamsExcept(balance, 4);
513 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
515 TeamBalance_BanTeamsExcept(balance, 3);
519 TeamBalance_BanTeamsExcept(balance, 2);
521 // no further cases, we know at least 2 teams exist
525 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
527 TeamBalance_BanTeamsExcept(balance, 1);
529 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
531 TeamBalance_BanTeamsExcept(balance, 2);
535 TeamBalance_BanTeamsExcept(balance, 3);
537 // no further cases, bots have one of the teams
542 // find first team available
543 if (IS_BOT_CLIENT(for_whom))
545 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
547 TeamBalance_BanTeamsExcept(balance, 1);
549 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
551 TeamBalance_BanTeamsExcept(balance, 2);
555 TeamBalance_BanTeamsExcept(balance, 3);
557 // no further cases, we know at least 2 teams exist
561 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
563 TeamBalance_BanTeamsExcept(balance, 4);
565 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
567 TeamBalance_BanTeamsExcept(balance, 3);
571 TeamBalance_BanTeamsExcept(balance, 2);
573 // no further cases, bots have one of the teams
580 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
584 // if player has a forced team, ONLY allow that one
585 for (int i = 1; i <= NUM_TEAMS; ++i)
587 if (for_whom.team_forced == i &&
588 TeamBalance_IsTeamAllowedInternal(balance, i))
590 TeamBalance_BanTeamsExcept(balance, i);
594 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
598 void TeamBalance_Destroy(entity balance)
604 for (int i = 0; i < NUM_TEAMS; ++i)
606 delete(balance.(m_team_balance_team[i]));
611 int TeamBalance_GetAllowedTeams(entity balance)
615 LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
617 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
619 LOG_FATAL("TeamBalance_GetAllowedTeams: "
620 "Team balance entity is not initialized.");
623 for (int i = 1; i <= NUM_TEAMS; ++i)
625 if (TeamBalance_IsTeamAllowedInternal(balance, i))
627 result |= Team_IndexToBit(i);
633 bool TeamBalance_IsTeamAllowed(entity balance, int index)
637 LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
639 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
641 LOG_FATAL("TeamBalance_IsTeamAllowed: "
642 "Team balance entity is not initialized.");
644 if (!Team_IsValidIndex(index))
646 LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
649 return TeamBalance_IsTeamAllowedInternal(balance, index);
652 void TeamBalance_GetTeamCounts(entity balance, entity ignore)
656 LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
658 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
660 LOG_FATAL("TeamBalance_GetTeamCounts: "
661 "Team balance entity is not initialized.");
663 if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
665 // Mutator has overriden the configuration.
666 for (int i = 1; i <= NUM_TEAMS; ++i)
668 entity team_ent = TeamBalance_GetTeamFromIndex(balance, i);
669 if (TeamBalanceTeam_IsAllowed(team_ent))
671 MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, i, ignore);
672 team_ent.m_num_players = M_ARGV(2, float);
673 team_ent.m_num_bots = M_ARGV(3, float);
679 // Manually count all players.
687 // TODO: Reconsider when the player is truly on the team.
688 if (IS_CLIENT(it) || (it.caplayer))
692 else if (Player_HasRealForcedTeam(it))
694 // Do we really need this? Probably not.
695 team_num = Team_IndexToTeam(it.team_forced); // reserve the spot
701 if (!Team_IsValidTeam(team_num))
705 entity team_ent = TeamBalance_GetTeam(balance, team_num);
706 if (!TeamBalanceTeam_IsAllowed(team_ent))
710 ++team_ent.m_num_players;
711 if (IS_BOT_CLIENT(it))
713 ++team_ent.m_num_bots;
718 // if the player who has a forced team has not joined yet, reserve the spot
719 if (autocvar_g_campaign)
721 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
723 entity team_ent = TeamBalance_GetTeamFromIndex(balance,
724 autocvar_g_campaign_forceteam);
725 if (team_ent.m_num_players == team_ent.m_num_bots)
727 ++team_ent.m_num_players;
731 balance.m_team_balance_state = TEAM_BALANCE_TEAM_COUNTS_FILLED;
734 int TeamBalance_GetNumberOfPlayers(entity balance, int index)
738 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
739 "Team balance entity is NULL.");
741 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
743 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
744 "TeamBalance_GetTeamCounts has not been called.");
746 if (!Team_IsValidIndex(index))
748 LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
751 return balance.m_team_balance_team[index - 1].m_num_players;
754 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
758 LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
760 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
762 LOG_FATAL("TeamBalance_FindBestTeam: "
763 "Team balance entity is not initialized.");
765 // count how many players are in each team
768 TeamBalance_GetTeamCounts(balance, player);
772 TeamBalance_GetTeamCounts(balance, NULL);
774 int team_bits = TeamBalance_FindBestTeams(balance, player, true);
777 LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
778 MapInfo_Type_ToString(MapInfo_CurrentGametype()));
780 RandomSelection_Init();
781 for (int i = 1; i <= NUM_TEAMS; ++i)
783 if (team_bits & Team_IndexToBit(i))
785 RandomSelection_AddFloat(i, 1, 1);
788 return RandomSelection_chosen_float;
791 int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
795 LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
797 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
799 LOG_FATAL("TeamBalance_FindBestTeams: "
800 "TeamBalance_GetTeamCounts has not been called.");
802 if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
804 return M_ARGV(1, float);
807 int previous_team = 0;
808 for (int i = 1; i <= NUM_TEAMS; ++i)
810 if (!TeamBalance_IsTeamAllowedInternal(balance, i))
814 if (previous_team == 0)
816 team_bits = Team_IndexToBit(i);
820 int compare = TeamBalance_CompareTeams(balance, i, previous_team,
822 if (compare == TEAMS_COMPARE_LESS)
824 team_bits = Team_IndexToBit(i);
828 if (compare == TEAMS_COMPARE_EQUAL)
830 team_bits |= Team_IndexToBit(i);
837 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
838 entity player, bool use_score)
842 LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
844 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
846 LOG_FATAL("TeamBalance_CompareTeams: "
847 "TeamBalance_GetTeamCounts has not been called.");
849 if (!Team_IsValidIndex(team_index_a))
851 LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
854 if (!Team_IsValidIndex(team_index_b))
856 LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
859 if (team_index_a == team_index_b)
861 return TEAMS_COMPARE_EQUAL;
863 entity team_a = TeamBalance_GetTeamFromIndex(balance, team_index_a);
864 entity team_b = TeamBalance_GetTeamFromIndex(balance, team_index_b);
865 return TeamBalance_CompareTeamsInternal(team_a, team_b, player, use_score);
868 void TeamBalance_AutoBalanceBots()
870 // checks disabled because we always want auto-balanced bots
871 //if (!(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance))
874 entity balance = TeamBalance_CheckAllowedTeams(NULL);
875 TeamBalance_GetTeamCounts(balance, NULL);
876 int smallest_team_index = 0;
877 int smallest_team_player_count = 0;
878 for (int i = 1; i <= NUM_TEAMS; ++i)
880 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
881 if (!TeamBalanceTeam_IsAllowed(team_))
885 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
886 if (smallest_team_index == 0)
888 smallest_team_index = i;
889 smallest_team_player_count = playercount;
891 else if (playercount < smallest_team_player_count)
893 smallest_team_index = i;
894 smallest_team_player_count = playercount;
897 //PrintToChatAll(sprintf("Smallest team: %f", smallest_team_index));
898 //PrintToChatAll(sprintf("Smallest team players: %f", smallest_team_player_count));
899 entity switchable_bot = NULL;
900 int teams = BITS(NUM_TEAMS);
903 int largest_team_index = TeamBalance_GetLargestTeamIndex(balance,
905 if (smallest_team_index == largest_team_index)
907 TeamBalance_Destroy(balance);
910 entity largest_team = TeamBalance_GetTeamFromIndex(balance,
912 int largest_team_player_count = TeamBalanceTeam_GetNumberOfPlayers(
914 if (largest_team_player_count - smallest_team_player_count < 2)
916 TeamBalance_Destroy(balance);
919 //PrintToChatAll(sprintf("Largest team: %f", largest_team_index));
920 //PrintToChatAll(sprintf("Largest team players: %f", largest_team_player_count));
921 switchable_bot = TeamBalance_GetPlayerForTeamSwitch(largest_team_index,
922 smallest_team_index, true);
923 if (switchable_bot != NULL)
927 teams &= ~Team_IndexToBit(largest_team_index);
929 TeamBalance_Destroy(balance);
930 if (switchable_bot == NULL)
932 //PrintToChatAll("No bot found after searching through all the teams");
935 SetPlayerTeam(switchable_bot, smallest_team_index, TEAM_CHANGE_AUTO);
938 int TeamBalance_GetLargestTeamIndex(entity balance, int teams)
940 int largest_team_index = 0;
941 int largest_team_player_count = 0;
942 for (int i = 1; i <= NUM_TEAMS; ++i)
944 if (!(Team_IndexToBit(i) & teams))
948 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
949 if (!TeamBalanceTeam_IsAllowed(team_))
953 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
954 if (largest_team_index == 0)
956 largest_team_index = i;
957 largest_team_player_count = playercount;
959 else if (playercount > largest_team_player_count)
961 largest_team_index = i;
962 largest_team_player_count = playercount;
965 return largest_team_index;
968 entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index,
969 int destination_team_index, bool is_bot)
971 if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, source_team_index,
972 destination_team_index, is_bot))
974 return M_ARGV(3, entity);
976 entity lowest_player = NULL;
977 float lowest_score = FLOAT_MAX;
978 FOREACH_CLIENT(Entity_GetTeamIndex(it) == source_team_index,
980 if (IS_BOT_CLIENT(it) != is_bot)
984 float temp_score = PlayerScore_Get(it, SP_SCORE);
985 if (temp_score >= lowest_score)
989 //PrintToChatAll(sprintf(
990 // "Found %s with lowest score, checking allowed teams", it.netname));
991 entity balance = TeamBalance_CheckAllowedTeams(it);
992 if (TeamBalance_IsTeamAllowed(balance, source_team_index))
994 //PrintToChatAll("Allowed");
996 lowest_score = temp_score;
1000 //PrintToChatAll("Not allowed");
1002 TeamBalance_Destroy(balance);
1004 return lowest_player;
1007 void LogTeamChange(float player_id, float team_number, int type)
1009 if (!autocvar_sv_eventlog)
1017 GameLogEcho(sprintf(":team:%d:%d:%d", player_id, team_number, type));
1020 void KillPlayerForTeamChange(entity player)
1022 if (IS_DEAD(player))
1026 if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
1030 Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
1031 player.origin, '0 0 0');
1034 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
1036 return balance.m_team_balance_team[index - 1].m_num_players !=
1040 void TeamBalance_BanTeamsExcept(entity balance, int index)
1042 for (int i = 1; i <= NUM_TEAMS; ++i)
1046 balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
1051 entity TeamBalance_GetTeamFromIndex(entity balance, int index)
1053 if (!Team_IsValidIndex(index))
1055 LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
1057 return balance.m_team_balance_team[index - 1];
1060 entity TeamBalance_GetTeam(entity balance, int team_num)
1062 return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
1065 bool TeamBalanceTeam_IsAllowed(entity team_ent)
1067 return team_ent.m_num_players != TEAM_NOT_ALLOWED;
1070 int TeamBalanceTeam_GetNumberOfPlayers(entity team_ent)
1072 return team_ent.m_num_players;
1075 int TeamBalanceTeam_GetNumberOfBots(entity team_ent)
1077 return team_ent.m_num_bots;
1080 int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
1081 entity player, bool use_score)
1083 if (team_a == team_b)
1085 return TEAMS_COMPARE_EQUAL;
1087 if (!TeamBalanceTeam_IsAllowed(team_a) ||
1088 !TeamBalanceTeam_IsAllowed(team_b))
1090 return TEAMS_COMPARE_INVALID;
1092 int num_players_team_a = team_a.m_num_players;
1093 int num_players_team_b = team_b.m_num_players;
1094 if (IS_REAL_CLIENT(player) && bots_would_leave)
1096 num_players_team_a -= team_a.m_num_bots;
1097 num_players_team_b -= team_b.m_num_bots;
1099 if (num_players_team_a < num_players_team_b)
1101 return TEAMS_COMPARE_LESS;
1103 if (num_players_team_a > num_players_team_b)
1105 return TEAMS_COMPARE_GREATER;
1109 return TEAMS_COMPARE_EQUAL;
1111 if (team_a.m_team_score < team_b.m_team_score)
1113 return TEAMS_COMPARE_LESS;
1115 if (team_a.m_team_score > team_b.m_team_score)
1117 return TEAMS_COMPARE_GREATER;
1119 return TEAMS_COMPARE_EQUAL;
1122 void SV_ChangeTeam(entity player, int new_color)
1126 SetPlayerColors(player, new_color);
1128 if(!IS_CLIENT(player))
1136 Player_SetTeamIndexChecked(player, Team_TeamToIndex((new_color & 0x0F) + 1));