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);
213 if (!Player_SetTeamIndex(player, team_index))
216 LogTeamChange(player.playerid, player.team, type);
218 if (team_index != old_team_index)
220 KillPlayerForTeamChange(player);
221 PlayerScore_Clear(player);
222 CS(player).parm_idlesince = time;
224 if (!IS_BOT_CLIENT(player))
225 TeamBalance_AutoBalanceBots();
227 if (team_index != -1)
228 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(player.team, INFO_JOIN_PLAY_TEAM), player.netname);
231 if (team_index == -1)
233 if (autocvar_sv_maxidle_playertospectator > 0 && CS(player).idlekick_lasttimeleft)
235 // this done here so it happens even when manually speccing during the countdown
236 Kill_Notification(NOTIF_ONE_ONLY, player, MSG_CENTER, CPID_IDLING);
237 CS(player).idlekick_lasttimeleft = 0;
239 else if (!CS(player).just_joined && player.frags != FRAGS_SPECTATOR)
241 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_SPECTATE, player.netname);
248 void Player_SetTeamIndexChecked(entity player, int team_index)
254 if (!Team_IsValidIndex(team_index))
258 if ((autocvar_g_campaign) || (autocvar_g_changeteam_banned &&
259 CS(player).wasplayer))
261 Send_Notification(NOTIF_ONE, player, MSG_INFO,
262 INFO_TEAMCHANGE_NOTALLOWED);
265 entity balance = TeamBalance_CheckAllowedTeams(player);
266 if (team_index == 1 && !TeamBalance_IsTeamAllowedInternal(balance, 1))
270 if (team_index == 4 && !TeamBalance_IsTeamAllowedInternal(balance, 4))
274 if (team_index == 3 && !TeamBalance_IsTeamAllowedInternal(balance, 3))
278 if (team_index == 2 && !TeamBalance_IsTeamAllowedInternal(balance, 2))
282 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
283 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
285 TeamBalance_GetTeamCounts(balance, player);
286 if ((Team_IndexToBit(team_index) & TeamBalance_FindBestTeams(balance,
287 player, false)) == 0)
289 Send_Notification(NOTIF_ONE, player, MSG_INFO,
290 INFO_TEAMCHANGE_LARGERTEAM);
291 TeamBalance_Destroy(balance);
295 TeamBalance_Destroy(balance);
296 SetPlayerTeam(player, team_index, TEAM_CHANGE_MANUAL);
299 bool MoveToTeam(entity client, int team_index, int type)
301 //PrintToChatAll(sprintf("MoveToTeam: %s, %f", client.netname, team_index));
302 int lockteams_backup = lockteams; // backup any team lock
303 lockteams = 0; // disable locked teams
304 if (!SetPlayerTeam(client, team_index, type))
306 lockteams = lockteams_backup; // restore the team lock
309 lockteams = lockteams_backup; // restore the team lock
313 bool Player_HasRealForcedTeam(entity player)
315 return player.team_forced > TEAM_FORCE_DEFAULT;
318 int Player_GetForcedTeamIndex(entity player)
320 return player.team_forced;
323 void Player_SetForcedTeamIndex(entity player, int team_index)
327 case TEAM_FORCE_SPECTATOR:
328 case TEAM_FORCE_DEFAULT:
330 player.team_forced = team_index;
335 if (!Team_IsValidIndex(team_index))
337 LOG_FATAL("Player_SetForcedTeamIndex: Invalid team index.");
341 player.team_forced = team_index;
348 void Player_DetermineForcedTeam(entity player)
350 if (autocvar_g_campaign)
352 if (IS_REAL_CLIENT(player)) // only players, not bots
354 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
356 player.team_forced = autocvar_g_campaign_forceteam;
360 player.team_forced = TEAM_FORCE_DEFAULT;
364 else if (PlayerInList(player, autocvar_g_forced_team_red))
366 player.team_forced = 1;
368 else if (PlayerInList(player, autocvar_g_forced_team_blue))
370 player.team_forced = 2;
372 else if (PlayerInList(player, autocvar_g_forced_team_yellow))
374 player.team_forced = 3;
376 else if (PlayerInList(player, autocvar_g_forced_team_pink))
378 player.team_forced = 4;
382 switch (autocvar_g_forced_team_otherwise)
386 player.team_forced = 1;
391 player.team_forced = 2;
396 player.team_forced = 3;
401 player.team_forced = 4;
407 player.team_forced = TEAM_FORCE_SPECTATOR;
412 player.team_forced = TEAM_FORCE_DEFAULT;
417 if (!teamplay && Player_HasRealForcedTeam(player))
419 player.team_forced = TEAM_FORCE_DEFAULT;
423 void TeamBalance_JoinBestTeam(entity player)
425 //PrintToChatAll(sprintf("TeamBalance_JoinBestTeam: %s", player.netname));
430 if (player.bot_forced_team)
434 entity balance = TeamBalance_CheckAllowedTeams(player);
435 if (Player_HasRealForcedTeam(player))
437 int forced_team_index = player.team_forced;
438 bool is_team_allowed = TeamBalance_IsTeamAllowedInternal(balance,
440 TeamBalance_Destroy(balance);
441 if (!is_team_allowed)
445 if (!SetPlayerTeam(player, forced_team_index, TEAM_CHANGE_AUTO))
451 int best_team_index = TeamBalance_FindBestTeam(balance, player, true);
452 TeamBalance_Destroy(balance);
453 if (!SetPlayerTeam(player, best_team_index, TEAM_CHANGE_AUTO))
459 entity TeamBalance_CheckAllowedTeams(entity for_whom)
461 entity balance = spawn();
462 for (int i = 0; i < NUM_TEAMS; ++i)
464 entity team_ent = balance.m_team_balance_team[i] = spawn();
465 team_ent.m_team_score = g_team_entities[i].m_team_score;
466 team_ent.m_num_players = TEAM_NOT_ALLOWED;
467 team_ent.m_num_bots = 0;
469 setthink(balance, TeamBalance_Destroy);
470 balance.nextthink = time;
473 string teament_name = string_null;
474 bool mutator_returnvalue = MUTATOR_CALLHOOK(TeamBalance_CheckAllowedTeams,
475 teams_mask, teament_name, for_whom);
476 teams_mask = M_ARGV(0, float);
477 teament_name = M_ARGV(1, string);
478 if (mutator_returnvalue)
480 for (int i = 0; i < NUM_TEAMS; ++i)
482 if (teams_mask & BIT(i))
484 balance.m_team_balance_team[i].m_num_players = 0;
491 entity head = find(NULL, classname, teament_name);
494 if (Team_IsValidTeam(head.team))
496 TeamBalance_GetTeam(balance, head.team).m_num_players = 0;
498 head = find(head, classname, teament_name);
502 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
503 if (autocvar_bot_vs_human && AvailableTeams() == 2 && for_whom)
505 if (autocvar_bot_vs_human > 0)
507 // find last team available
508 if (IS_BOT_CLIENT(for_whom))
510 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
512 TeamBalance_BanTeamsExcept(balance, 4);
514 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
516 TeamBalance_BanTeamsExcept(balance, 3);
520 TeamBalance_BanTeamsExcept(balance, 2);
522 // no further cases, we know at least 2 teams exist
526 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
528 TeamBalance_BanTeamsExcept(balance, 1);
530 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
532 TeamBalance_BanTeamsExcept(balance, 2);
536 TeamBalance_BanTeamsExcept(balance, 3);
538 // no further cases, bots have one of the teams
543 // find first team available
544 if (IS_BOT_CLIENT(for_whom))
546 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
548 TeamBalance_BanTeamsExcept(balance, 1);
550 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
552 TeamBalance_BanTeamsExcept(balance, 2);
556 TeamBalance_BanTeamsExcept(balance, 3);
558 // no further cases, we know at least 2 teams exist
562 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
564 TeamBalance_BanTeamsExcept(balance, 4);
566 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
568 TeamBalance_BanTeamsExcept(balance, 3);
572 TeamBalance_BanTeamsExcept(balance, 2);
574 // no further cases, bots have one of the teams
581 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
585 // if player has a forced team, ONLY allow that one
586 for (int i = 1; i <= NUM_TEAMS; ++i)
588 if (for_whom.team_forced == i &&
589 TeamBalance_IsTeamAllowedInternal(balance, i))
591 TeamBalance_BanTeamsExcept(balance, i);
595 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
599 void TeamBalance_Destroy(entity balance)
605 for (int i = 0; i < NUM_TEAMS; ++i)
607 delete(balance.(m_team_balance_team[i]));
612 int TeamBalance_GetAllowedTeams(entity balance)
616 LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
618 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
620 LOG_FATAL("TeamBalance_GetAllowedTeams: "
621 "Team balance entity is not initialized.");
624 for (int i = 1; i <= NUM_TEAMS; ++i)
626 if (TeamBalance_IsTeamAllowedInternal(balance, i))
628 result |= Team_IndexToBit(i);
634 bool TeamBalance_IsTeamAllowed(entity balance, int index)
638 LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
640 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
642 LOG_FATAL("TeamBalance_IsTeamAllowed: "
643 "Team balance entity is not initialized.");
645 if (!Team_IsValidIndex(index))
647 LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
650 return TeamBalance_IsTeamAllowedInternal(balance, index);
653 void TeamBalance_GetTeamCounts(entity balance, entity ignore)
657 LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
659 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
661 LOG_FATAL("TeamBalance_GetTeamCounts: "
662 "Team balance entity is not initialized.");
664 if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
666 // Mutator has overriden the configuration.
667 for (int i = 1; i <= NUM_TEAMS; ++i)
669 entity team_ent = TeamBalance_GetTeamFromIndex(balance, i);
670 if (TeamBalanceTeam_IsAllowed(team_ent))
672 MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, i, ignore);
673 team_ent.m_num_players = M_ARGV(2, float);
674 team_ent.m_num_bots = M_ARGV(3, float);
680 // Manually count all players.
688 // TODO: Reconsider when the player is truly on the team.
689 if (IS_CLIENT(it) || (it.caplayer))
693 else if (Player_HasRealForcedTeam(it))
695 // Do we really need this? Probably not.
696 team_num = Team_IndexToTeam(it.team_forced); // reserve the spot
702 if (!Team_IsValidTeam(team_num))
706 entity team_ent = TeamBalance_GetTeam(balance, team_num);
707 if (!TeamBalanceTeam_IsAllowed(team_ent))
711 ++team_ent.m_num_players;
712 if (IS_BOT_CLIENT(it))
714 ++team_ent.m_num_bots;
719 // if the player who has a forced team has not joined yet, reserve the spot
720 if (autocvar_g_campaign)
722 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
724 entity team_ent = TeamBalance_GetTeamFromIndex(balance,
725 autocvar_g_campaign_forceteam);
726 if (team_ent.m_num_players == team_ent.m_num_bots)
728 ++team_ent.m_num_players;
732 balance.m_team_balance_state = TEAM_BALANCE_TEAM_COUNTS_FILLED;
735 int TeamBalance_GetNumberOfPlayers(entity balance, int index)
739 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
740 "Team balance entity is NULL.");
742 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
744 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
745 "TeamBalance_GetTeamCounts has not been called.");
747 if (!Team_IsValidIndex(index))
749 LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
752 return balance.m_team_balance_team[index - 1].m_num_players;
755 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
759 LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
761 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
763 LOG_FATAL("TeamBalance_FindBestTeam: "
764 "Team balance entity is not initialized.");
766 // count how many players are in each team
769 TeamBalance_GetTeamCounts(balance, player);
773 TeamBalance_GetTeamCounts(balance, NULL);
775 int team_bits = TeamBalance_FindBestTeams(balance, player, true);
778 LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
779 MapInfo_Type_ToString(MapInfo_CurrentGametype()));
781 RandomSelection_Init();
782 for (int i = 1; i <= NUM_TEAMS; ++i)
784 if (team_bits & Team_IndexToBit(i))
786 RandomSelection_AddFloat(i, 1, 1);
789 return RandomSelection_chosen_float;
792 int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
796 LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
798 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
800 LOG_FATAL("TeamBalance_FindBestTeams: "
801 "TeamBalance_GetTeamCounts has not been called.");
803 if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
805 return M_ARGV(1, float);
808 int previous_team = 0;
809 for (int i = 1; i <= NUM_TEAMS; ++i)
811 if (!TeamBalance_IsTeamAllowedInternal(balance, i))
815 if (previous_team == 0)
817 team_bits = Team_IndexToBit(i);
821 int compare = TeamBalance_CompareTeams(balance, i, previous_team,
823 if (compare == TEAMS_COMPARE_LESS)
825 team_bits = Team_IndexToBit(i);
829 if (compare == TEAMS_COMPARE_EQUAL)
831 team_bits |= Team_IndexToBit(i);
838 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
839 entity player, bool use_score)
843 LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
845 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
847 LOG_FATAL("TeamBalance_CompareTeams: "
848 "TeamBalance_GetTeamCounts has not been called.");
850 if (!Team_IsValidIndex(team_index_a))
852 LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
855 if (!Team_IsValidIndex(team_index_b))
857 LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
860 if (team_index_a == team_index_b)
862 return TEAMS_COMPARE_EQUAL;
864 entity team_a = TeamBalance_GetTeamFromIndex(balance, team_index_a);
865 entity team_b = TeamBalance_GetTeamFromIndex(balance, team_index_b);
866 return TeamBalance_CompareTeamsInternal(team_a, team_b, player, use_score);
869 void TeamBalance_AutoBalanceBots()
871 // checks disabled because we always want auto-balanced bots
872 //if (!(autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance))
875 entity balance = TeamBalance_CheckAllowedTeams(NULL);
876 TeamBalance_GetTeamCounts(balance, NULL);
877 int smallest_team_index = 0;
878 int smallest_team_player_count = 0;
879 for (int i = 1; i <= NUM_TEAMS; ++i)
881 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
882 if (!TeamBalanceTeam_IsAllowed(team_))
886 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
887 if (smallest_team_index == 0)
889 smallest_team_index = i;
890 smallest_team_player_count = playercount;
892 else if (playercount < smallest_team_player_count)
894 smallest_team_index = i;
895 smallest_team_player_count = playercount;
898 //PrintToChatAll(sprintf("Smallest team: %f", smallest_team_index));
899 //PrintToChatAll(sprintf("Smallest team players: %f", smallest_team_player_count));
900 entity switchable_bot = NULL;
901 int teams = BITS(NUM_TEAMS);
904 int largest_team_index = TeamBalance_GetLargestTeamIndex(balance,
906 if (smallest_team_index == largest_team_index)
908 TeamBalance_Destroy(balance);
911 entity largest_team = TeamBalance_GetTeamFromIndex(balance,
913 int largest_team_player_count = TeamBalanceTeam_GetNumberOfPlayers(
915 if (largest_team_player_count - smallest_team_player_count < 2)
917 TeamBalance_Destroy(balance);
920 //PrintToChatAll(sprintf("Largest team: %f", largest_team_index));
921 //PrintToChatAll(sprintf("Largest team players: %f", largest_team_player_count));
922 switchable_bot = TeamBalance_GetPlayerForTeamSwitch(largest_team_index,
923 smallest_team_index, true);
924 if (switchable_bot != NULL)
928 teams &= ~Team_IndexToBit(largest_team_index);
930 TeamBalance_Destroy(balance);
931 if (switchable_bot == NULL)
933 //PrintToChatAll("No bot found after searching through all the teams");
936 SetPlayerTeam(switchable_bot, smallest_team_index, TEAM_CHANGE_AUTO);
939 int TeamBalance_GetLargestTeamIndex(entity balance, int teams)
941 int largest_team_index = 0;
942 int largest_team_player_count = 0;
943 for (int i = 1; i <= NUM_TEAMS; ++i)
945 if (!(Team_IndexToBit(i) & teams))
949 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
950 if (!TeamBalanceTeam_IsAllowed(team_))
954 int playercount = TeamBalanceTeam_GetNumberOfPlayers(team_);
955 if (largest_team_index == 0)
957 largest_team_index = i;
958 largest_team_player_count = playercount;
960 else if (playercount > largest_team_player_count)
962 largest_team_index = i;
963 largest_team_player_count = playercount;
966 return largest_team_index;
969 entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index,
970 int destination_team_index, bool is_bot)
972 if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, source_team_index,
973 destination_team_index, is_bot))
975 return M_ARGV(3, entity);
977 entity lowest_player = NULL;
978 float lowest_score = FLOAT_MAX;
979 FOREACH_CLIENT(Entity_GetTeamIndex(it) == source_team_index,
981 if (IS_BOT_CLIENT(it) != is_bot)
985 float temp_score = PlayerScore_Get(it, SP_SCORE);
986 if (temp_score >= lowest_score)
990 //PrintToChatAll(sprintf(
991 // "Found %s with lowest score, checking allowed teams", it.netname));
992 entity balance = TeamBalance_CheckAllowedTeams(it);
993 if (TeamBalance_IsTeamAllowed(balance, source_team_index))
995 //PrintToChatAll("Allowed");
997 lowest_score = temp_score;
1001 //PrintToChatAll("Not allowed");
1003 TeamBalance_Destroy(balance);
1005 return lowest_player;
1008 void LogTeamChange(float player_id, float team_number, int type)
1010 if (!autocvar_sv_eventlog)
1018 GameLogEcho(sprintf(":team:%d:%d:%d", player_id, team_number, type));
1021 void KillPlayerForTeamChange(entity player)
1023 if (IS_DEAD(player))
1027 if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
1031 Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
1032 player.origin, '0 0 0');
1035 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
1037 return balance.m_team_balance_team[index - 1].m_num_players !=
1041 void TeamBalance_BanTeamsExcept(entity balance, int index)
1043 for (int i = 1; i <= NUM_TEAMS; ++i)
1047 balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
1052 entity TeamBalance_GetTeamFromIndex(entity balance, int index)
1054 if (!Team_IsValidIndex(index))
1056 LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
1058 return balance.m_team_balance_team[index - 1];
1061 entity TeamBalance_GetTeam(entity balance, int team_num)
1063 return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
1066 bool TeamBalanceTeam_IsAllowed(entity team_ent)
1068 return team_ent.m_num_players != TEAM_NOT_ALLOWED;
1071 int TeamBalanceTeam_GetNumberOfPlayers(entity team_ent)
1073 return team_ent.m_num_players;
1076 int TeamBalanceTeam_GetNumberOfBots(entity team_ent)
1078 return team_ent.m_num_bots;
1081 int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
1082 entity player, bool use_score)
1084 if (team_a == team_b)
1086 return TEAMS_COMPARE_EQUAL;
1088 if (!TeamBalanceTeam_IsAllowed(team_a) ||
1089 !TeamBalanceTeam_IsAllowed(team_b))
1091 return TEAMS_COMPARE_INVALID;
1093 int num_players_team_a = team_a.m_num_players;
1094 int num_players_team_b = team_b.m_num_players;
1095 if (IS_REAL_CLIENT(player) && bots_would_leave)
1097 num_players_team_a -= team_a.m_num_bots;
1098 num_players_team_b -= team_b.m_num_bots;
1100 if (num_players_team_a < num_players_team_b)
1102 return TEAMS_COMPARE_LESS;
1104 if (num_players_team_a > num_players_team_b)
1106 return TEAMS_COMPARE_GREATER;
1110 return TEAMS_COMPARE_EQUAL;
1112 if (team_a.m_team_score < team_b.m_team_score)
1114 return TEAMS_COMPARE_LESS;
1116 if (team_a.m_team_score > team_b.m_team_score)
1118 return TEAMS_COMPARE_GREATER;
1120 return TEAMS_COMPARE_EQUAL;
1123 void SV_ChangeTeam(entity player, int new_color)
1127 SetPlayerColors(player, new_color);
1129 if(!IS_CLIENT(player))
1137 Player_SetTeamIndexChecked(player, Team_TeamToIndex((new_color & 0x0F) + 1));