6 #include "scores_rules.qh"
10 #include "command/vote.qh"
12 #include "mutators/_mod.qh"
14 #include "../common/deathtypes/all.qh"
15 #include "../common/gamemodes/_mod.qh"
16 #include "../common/teams.qh"
18 /// \brief Describes a state of team balance entity.
21 TEAM_BALANCE_UNINITIALIZED, ///< The team balance has not been initialized.
22 /// \brief TeamBalance_CheckAllowedTeams has been called.
23 TEAM_BALANCE_TEAMS_CHECKED,
24 /// \brief TeamBalance_GetTeamCounts has been called.
25 TEAM_BALANCE_TEAM_COUNTS_FILLED
28 /// \brief Indicates that the player is not allowed to join a team.
29 const int TEAM_NOT_ALLOWED = -1;
31 .int m_team_balance_state; ///< Holds the state of the team balance entity.
32 .entity m_team_balance_team[NUM_TEAMS]; ///< ???
34 .float m_team_score; ///< The score of the team.
35 .int m_num_players; ///< Number of players (both humans and bots) in a team.
36 .int m_num_bots; ///< Number of bots in a team.
37 .int m_num_players_alive; ///< Number of alive players in a team.
38 .entity m_lowest_human; ///< Human with the lowest score in a team.
39 .entity m_lowest_bot; ///< Bot with the lowest score in a team.
41 entity g_team_entities[NUM_TEAMS]; ///< Holds global team entities.
43 STATIC_INIT(g_team_entities)
45 for (int i = 0; i < NUM_TEAMS; ++i)
47 g_team_entities[i] = spawn();
51 entity Team_GetTeamFromIndex(int index)
53 if (!Team_IsValidIndex(index))
55 LOG_FATALF("Team_GetTeamFromIndex: Index is invalid: %f", index);
57 return g_team_entities[index - 1];
60 entity Team_GetTeam(int team_num)
62 if (!Team_IsValidTeam(team_num))
64 LOG_FATALF("Team_GetTeam: Value is invalid: %f", team_num);
66 return g_team_entities[Team_TeamToIndex(team_num) - 1];
69 float Team_GetTeamScore(entity team_)
71 return team_.m_team_score;
74 void Team_SetTeamScore(entity team_, float score)
76 team_.m_team_score = score;
79 int Team_GetNumberOfAlivePlayers(entity team_)
81 return team_.m_num_players_alive;
84 void Team_SetNumberOfAlivePlayers(entity team_, int number)
86 team_.m_num_players_alive = number;
89 int Team_GetNumberOfAliveTeams()
92 for (int i = 0; i < NUM_TEAMS; ++i)
94 if (g_team_entities[i].m_num_players_alive > 0)
102 void TeamchangeFrags(entity e)
104 PlayerScore_Clear(e);
107 void LogTeamchange(float player_id, float team_number, float type)
109 if(!autocvar_sv_eventlog)
115 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
118 void default_delayedinit(entity this)
120 if(!scores_initialized)
121 ScoreRules_generic();
124 void InitGameplayMode()
128 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
130 // assign reflectively to avoid "assignment to world" warning
131 int done = 0; for (int i = 0, n = numentityfields(); i < n; ++i) {
132 string k = entityfieldname(i); vector v = (k == "mins") ? mi_min : (k == "maxs") ? mi_max : '0 0 0';
134 putentityfieldstring(i, world, sprintf("%v", v));
135 if (++done == 2) break;
138 // currently, NetRadiant's limit is 131072 qu for each side
139 // distance from one corner of a 131072qu cube to the opposite corner is approx. 227023 qu
140 // set the distance according to map size but don't go over the limit to avoid issues with float precision
141 // in case somebody makes extremely large maps
142 max_shot_distance = min(230000, vlen(world.maxs - world.mins));
144 MapInfo_LoadMapSettings(mapname);
145 GameRules_teams(false);
147 if (!cvar_value_issafe(world.fog))
149 LOG_INFO("The current map contains a potentially harmful fog setting, ignored");
150 world.fog = string_null;
152 if(MapInfo_Map_fog != "")
153 if(MapInfo_Map_fog == "none")
154 world.fog = string_null;
156 world.fog = strzone(MapInfo_Map_fog);
157 clientstuff = strzone(MapInfo_Map_clientstuff);
159 MapInfo_ClearTemps();
161 gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
163 cache_mutatormsg = strzone("");
164 cache_lastmutatormsg = strzone("");
166 InitializeEntity(NULL, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
169 string GetClientVersionMessage(entity this)
171 if (CS(this).version_mismatch) {
172 if(CS(this).version < autocvar_gameversion) {
173 return strcat("This is Xonotic ", autocvar_g_xonoticversion,
174 "\n^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8");
176 return strcat("This is Xonotic ", autocvar_g_xonoticversion,
177 "\n^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8");
180 return strcat("Welcome to Xonotic ", autocvar_g_xonoticversion);
184 string getwelcomemessage(entity this)
186 MUTATOR_CALLHOOK(BuildMutatorsPrettyString, "");
187 string modifications = M_ARGV(0, string);
191 if(g_weaponarena_random)
192 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
194 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
196 else if(cvar("g_balance_blaster_weaponstartoverride") == 0)
197 modifications = strcat(modifications, ", No start weapons");
198 if(cvar("sv_gravity") < stof(cvar_defstring("sv_gravity")))
199 modifications = strcat(modifications, ", Low gravity");
200 if(g_weapon_stay && !g_cts)
201 modifications = strcat(modifications, ", Weapons stay");
203 modifications = strcat(modifications, ", Jet pack");
204 if(autocvar_g_powerups == 0)
205 modifications = strcat(modifications, ", No powerups");
206 if(autocvar_g_powerups > 0)
207 modifications = strcat(modifications, ", Powerups");
208 modifications = substring(modifications, 2, strlen(modifications) - 2);
210 string versionmessage = GetClientVersionMessage(this);
211 string s = strcat(versionmessage, "^8\n^8\nmatch type is ^1", gamemode_name, "^8\n");
213 if(modifications != "")
214 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
216 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
218 if(cache_lastmutatormsg)
219 strunzone(cache_lastmutatormsg);
221 strunzone(cache_mutatormsg);
222 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
223 cache_mutatormsg = strzone(cache_lastmutatormsg);
226 if (cache_mutatormsg != "") {
227 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
230 string mutator_msg = "";
231 MUTATOR_CALLHOOK(BuildGameplayTipsString, mutator_msg);
232 mutator_msg = M_ARGV(0, string);
234 s = strcat(s, mutator_msg); // trust that the mutator will do proper formatting
236 string motd = autocvar_sv_motd;
238 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
243 void setcolor(entity this, int clr)
246 this.clientcolors = clr;
247 this.team = (clr & 15) + 1;
249 builtin_setcolor(this, clr);
253 bool Entity_HasValidTeam(entity this)
255 return Team_IsValidTeam(this.team);
258 int Entity_GetTeamIndex(entity this)
260 return Team_TeamToIndex(this.team);
263 entity Entity_GetTeam(entity this)
265 int index = Entity_GetTeamIndex(this);
266 if (!Team_IsValidIndex(index))
270 return Team_GetTeamFromIndex(index);
273 void SetPlayerColors(entity player, float _color)
275 float pants = _color & 0x0F;
276 float shirt = _color & 0xF0;
279 setcolor(player, 16 * pants + pants);
283 setcolor(player, shirt + pants);
287 bool Player_SetTeamIndex(entity player, int index)
289 int new_team = Team_IndexToTeam(index);
290 if (player.team == new_team)
292 // This is important when players join the game and one of their color
293 // matches the team color while other doesn't. For example [BOT]Lion.
294 SetPlayerColors(player, new_team - 1);
297 int old_index = Team_TeamToIndex(player.team);
298 if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, old_index, index) == true)
300 // Mutator has blocked team change.
303 SetPlayerColors(player, new_team - 1);
304 MUTATOR_CALLHOOK(Player_ChangedTeam, player, old_index, index);
308 bool SetPlayerTeam(entity player, int destination_team_index,
309 int source_team_index, bool no_print)
311 if (!Player_SetTeamIndex(player, destination_team_index))
315 LogTeamchange(player.playerid, player.team, 3); // log manual team join
320 bprint(playername(player, false), "^7 has changed from ",
321 Team_IndexToColoredFullName(source_team_index), "^7 to ",
322 Team_IndexToColoredFullName(destination_team_index), "\n");
326 void KillPlayerForTeamChange(entity player)
332 if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
336 Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
337 player.origin, '0 0 0');
340 entity TeamBalance_CheckAllowedTeams(entity for_whom)
342 entity balance = spawn();
343 for (int i = 0; i < NUM_TEAMS; ++i)
345 entity team_ = balance.m_team_balance_team[i] = spawn();
346 team_.m_team_score = g_team_entities[i].m_team_score;
347 team_.m_num_players = TEAM_NOT_ALLOWED;
348 team_.m_num_bots = 0;
349 team_.m_lowest_human = NULL;
350 team_.m_lowest_bot = NULL;
354 string teament_name = string_null;
355 bool mutator_returnvalue = MUTATOR_CALLHOOK(TeamBalance_CheckAllowedTeams,
356 teams_mask, teament_name, for_whom);
357 teams_mask = M_ARGV(0, float);
358 teament_name = M_ARGV(1, string);
359 if (mutator_returnvalue)
361 for (int i = 0; i < NUM_TEAMS; ++i)
363 if (teams_mask & BIT(i))
365 balance.m_team_balance_team[i].m_num_players = 0;
372 entity head = find(NULL, classname, teament_name);
375 if (Team_IsValidTeam(head.team))
377 TeamBalance_GetTeam(balance, head.team).m_num_players = 0;
379 head = find(head, classname, teament_name);
383 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
384 if (AvailableTeams() == 2)
385 if (autocvar_bot_vs_human && for_whom)
387 if (autocvar_bot_vs_human > 0)
389 // find last team available
390 if (IS_BOT_CLIENT(for_whom))
392 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
394 TeamBalance_BanTeamsExcept(balance, 4);
396 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
398 TeamBalance_BanTeamsExcept(balance, 3);
402 TeamBalance_BanTeamsExcept(balance, 2);
404 // no further cases, we know at least 2 teams exist
408 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
410 TeamBalance_BanTeamsExcept(balance, 1);
412 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
414 TeamBalance_BanTeamsExcept(balance, 2);
418 TeamBalance_BanTeamsExcept(balance, 3);
420 // no further cases, bots have one of the teams
425 // find first team available
426 if (IS_BOT_CLIENT(for_whom))
428 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
430 TeamBalance_BanTeamsExcept(balance, 1);
432 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
434 TeamBalance_BanTeamsExcept(balance, 2);
438 TeamBalance_BanTeamsExcept(balance, 3);
440 // no further cases, we know at least 2 teams exist
444 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
446 TeamBalance_BanTeamsExcept(balance, 4);
448 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
450 TeamBalance_BanTeamsExcept(balance, 3);
454 TeamBalance_BanTeamsExcept(balance, 2);
456 // no further cases, bots have one of the teams
463 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
467 // if player has a forced team, ONLY allow that one
468 for (int i = 1; i <= NUM_TEAMS; ++i)
470 if (for_whom.team_forced == Team_IndexToTeam(i) &&
471 TeamBalance_IsTeamAllowedInternal(balance, i))
473 TeamBalance_BanTeamsExcept(balance, i);
477 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
481 void TeamBalance_Destroy(entity balance)
487 for (int i = 0; i < NUM_TEAMS; ++i)
489 remove(balance.(m_team_balance_team[i]));
494 int TeamBalance_GetAllowedTeams(entity balance)
498 LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
500 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
502 LOG_FATAL("TeamBalance_GetAllowedTeams: "
503 "Team balance entity is not initialized.");
506 for (int i = 1; i <= NUM_TEAMS; ++i)
508 if (TeamBalance_IsTeamAllowedInternal(balance, i))
510 result |= Team_IndexToBit(i);
516 bool TeamBalance_IsTeamAllowed(entity balance, int index)
520 LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
522 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
524 LOG_FATAL("TeamBalance_IsTeamAllowed: "
525 "Team balance entity is not initialized.");
527 if (!Team_IsValidIndex(index))
529 LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
532 return TeamBalance_IsTeamAllowedInternal(balance, index);
535 void TeamBalance_GetTeamCounts(entity balance, entity ignore)
539 LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
541 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
543 LOG_FATAL("TeamBalance_GetTeamCounts: "
544 "Team balance entity is not initialized.");
546 if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
548 // Mutator has overriden the configuration.
549 for (int i = 1; i <= NUM_TEAMS; ++i)
551 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
552 if (TeamBalanceTeam_IsAllowed(team_))
554 MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, i, ignore,
555 team_.m_num_players, team_.m_num_bots, team_.m_lowest_human,
557 team_.m_num_players = M_ARGV(2, float);
558 team_.m_num_bots = M_ARGV(3, float);
559 team_.m_lowest_human = M_ARGV(4, entity);
560 team_.m_lowest_bot = M_ARGV(5, entity);
566 // Manually count all players.
574 if (IS_PLAYER(it) || it.caplayer)
578 else if (it.team_forced > 0)
580 team_num = it.team_forced; // reserve the spot
586 if (!Team_IsValidTeam(team_num))
590 entity team_ = TeamBalance_GetTeam(balance, team_num);
591 if (!TeamBalanceTeam_IsAllowed(team_))
595 ++team_.m_num_players;
596 if (IS_BOT_CLIENT(it))
600 float temp_score = PlayerScore_Get(it, SP_SCORE);
601 if (!IS_BOT_CLIENT(it))
603 if (team_.m_lowest_human == NULL)
605 team_.m_lowest_human = it;
608 if (temp_score < PlayerScore_Get(team_.m_lowest_human,
611 team_.m_lowest_human = it;
615 if (team_.m_lowest_bot == NULL)
617 team_.m_lowest_bot = it;
620 if (temp_score < PlayerScore_Get(team_.m_lowest_bot, SP_SCORE))
622 team_.m_lowest_bot = it;
627 // if the player who has a forced team has not joined yet, reserve the spot
628 if (autocvar_g_campaign)
630 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
632 entity team_ = TeamBalance_GetTeamFromIndex(balance,
633 autocvar_g_campaign_forceteam);
634 if (team_.m_num_players == team_.m_num_bots)
636 ++team_.m_num_players;
640 balance.m_team_balance_state = TEAM_BALANCE_TEAM_COUNTS_FILLED;
643 int TeamBalance_GetNumberOfPlayers(entity balance, int index)
647 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
648 "Team balance entity is NULL.");
650 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
652 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
653 "TeamBalance_GetTeamCounts has not been called.");
655 if (!Team_IsValidIndex(index))
657 LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
660 return balance.m_team_balance_team[index - 1].m_num_players;
663 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
667 LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
669 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
671 LOG_FATAL("TeamBalance_FindBestTeam: "
672 "Team balance entity is not initialized.");
674 // count how many players are in each team
677 TeamBalance_GetTeamCounts(balance, player);
681 TeamBalance_GetTeamCounts(balance, NULL);
683 int team_bits = TeamBalance_FindBestTeams(balance, player, true);
686 LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
687 MapInfo_Type_ToString(MapInfo_CurrentGametype()));
689 RandomSelection_Init();
690 for (int i = 1; i <= NUM_TEAMS; ++i)
692 if (team_bits & Team_IndexToBit(i))
694 RandomSelection_AddFloat(i, 1, 1);
697 return RandomSelection_chosen_float;
700 int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
704 LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
706 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
708 LOG_FATAL("TeamBalance_FindBestTeams: "
709 "TeamBalance_GetTeamCounts has not been called.");
711 if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
713 return M_ARGV(1, float);
716 int previous_team = 0;
717 for (int i = 1; i <= NUM_TEAMS; ++i)
719 if (!TeamBalance_IsTeamAllowedInternal(balance, i))
723 if (previous_team == 0)
725 team_bits = Team_IndexToBit(i);
729 int compare = TeamBalance_CompareTeams(balance, i, previous_team,
731 if (compare == TEAMS_COMPARE_LESS)
733 team_bits = Team_IndexToBit(i);
737 if (compare == TEAMS_COMPARE_EQUAL)
739 team_bits |= Team_IndexToBit(i);
746 void TeamBalance_JoinBestTeam(entity this, bool force_best_team)
748 // don't join a team if we're not playing a team game
754 // find out what teams are available
755 entity balance = TeamBalance_CheckAllowedTeams(this);
757 // if we don't care what team they end up on, put them on whatever team they entered as.
758 // if they're not on a valid team, then let other code put them on the smallest team
759 if (!force_best_team)
761 int selected_team_index = -1;
762 for (int i = 1; i <= NUM_TEAMS; ++i)
764 if (TeamBalance_IsTeamAllowedInternal(balance, i) &&
765 (Team_TeamToIndex(this.team) == i))
767 selected_team_index = i;
772 if (Team_IsValidIndex(selected_team_index))
774 Player_SetTeamIndex(this, selected_team_index);
775 LogTeamchange(this.playerid, this.team, 99);
776 TeamBalance_Destroy(balance);
780 // otherwise end up on the smallest team (handled below)
781 if (this.bot_forced_team)
783 TeamBalance_Destroy(balance);
786 int best_team_index = TeamBalance_FindBestTeam(balance, this, true);
787 int old_team_index = Team_TeamToIndex(this.team);
788 TeamchangeFrags(this);
789 Player_SetTeamIndex(this, best_team_index);
790 LogTeamchange(this.playerid, this.team, 2); // log auto join
791 if ((old_team_index != -1) && !IS_BOT_CLIENT(this))
793 TeamBalance_AutoBalanceBots(balance, old_team_index, best_team_index);
795 KillPlayerForTeamChange(this);
796 TeamBalance_Destroy(balance);
799 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
800 entity player, bool use_score)
804 LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
806 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
808 LOG_FATAL("TeamBalance_CompareTeams: "
809 "TeamBalance_GetTeamCounts has not been called.");
811 if (!Team_IsValidIndex(team_index_a))
813 LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
816 if (!Team_IsValidIndex(team_index_b))
818 LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
821 if (team_index_a == team_index_b)
823 return TEAMS_COMPARE_EQUAL;
825 entity team_a = TeamBalance_GetTeamFromIndex(balance, team_index_a);
826 entity team_b = TeamBalance_GetTeamFromIndex(balance, team_index_b);
827 return TeamBalance_CompareTeamsInternal(team_a, team_b, player, use_score);
830 void TeamBalance_AutoBalanceBots(entity balance, int source_team_index,
831 int destination_team_index)
835 LOG_FATAL("TeamBalance_AutoBalanceBots: Team balance entity is NULL.");
837 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
839 LOG_FATAL("TeamBalance_AutoBalanceBots: "
840 "TeamBalance_GetTeamCounts has not been called.");
842 if (!Team_IsValidIndex(source_team_index))
844 LOG_WARNF("AutoBalanceBots: Source team index is invalid: %f",
848 if (!Team_IsValidIndex(destination_team_index))
850 LOG_WARNF("AutoBalanceBots: Destination team index is invalid: %f",
851 destination_team_index);
854 if (!autocvar_g_balance_teams ||
855 !autocvar_g_balance_teams_prevent_imbalance)
859 entity source_team = TeamBalance_GetTeamFromIndex(balance,
861 if (!TeamBalanceTeam_IsAllowed(source_team))
865 entity destination_team = TeamBalance_GetTeamFromIndex(balance,
866 destination_team_index);
867 if ((destination_team.m_num_players <= source_team.m_num_players) ||
868 (destination_team.m_lowest_bot == NULL))
872 Player_SetTeamIndex(destination_team.m_lowest_bot, source_team_index);
873 KillPlayerForTeamChange(destination_team.m_lowest_bot);
876 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
878 return balance.m_team_balance_team[index - 1].m_num_players !=
882 void TeamBalance_BanTeamsExcept(entity balance, int index)
884 for (int i = 1; i <= NUM_TEAMS; ++i)
888 balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
893 entity TeamBalance_GetTeamFromIndex(entity balance, int index)
895 if (!Team_IsValidIndex(index))
897 LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
899 return balance.m_team_balance_team[index - 1];
902 entity TeamBalance_GetTeam(entity balance, int team_num)
904 return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
907 bool TeamBalanceTeam_IsAllowed(entity team_)
909 return team_.m_num_players != TEAM_NOT_ALLOWED;
912 int TeamBalanceTeam_GetNumberOfPlayers(entity team_)
914 return team_.m_num_players;
917 int TeamBalanceTeam_GetNumberOfBots(entity team_)
919 return team_.m_num_bots;
922 entity TeamBalanceTeam_GetLowestHuman(entity team_)
924 return team_.m_lowest_human;
927 entity TeamBalanceTeam_GetLowestBot(entity team_)
929 return team_.m_lowest_bot;
932 int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
933 entity player, bool use_score)
935 if (team_a == team_b)
937 return TEAMS_COMPARE_EQUAL;
939 if (!TeamBalanceTeam_IsAllowed(team_a) ||
940 !TeamBalanceTeam_IsAllowed(team_b))
942 return TEAMS_COMPARE_INVALID;
944 int num_players_team_a = team_a.m_num_players;
945 int num_players_team_b = team_b.m_num_players;
946 if (IS_REAL_CLIENT(player) && bots_would_leave)
948 num_players_team_a -= team_a.m_num_bots;
949 num_players_team_b -= team_b.m_num_bots;
951 if (num_players_team_a < num_players_team_b)
953 return TEAMS_COMPARE_LESS;
955 if (num_players_team_a > num_players_team_b)
957 return TEAMS_COMPARE_GREATER;
961 return TEAMS_COMPARE_EQUAL;
963 if (team_a.m_team_score < team_b.m_team_score)
965 return TEAMS_COMPARE_LESS;
967 if (team_a.m_team_score > team_b.m_team_score)
969 return TEAMS_COMPARE_GREATER;
971 return TEAMS_COMPARE_EQUAL;
974 void SV_ChangeTeam(entity this, float _color)
976 int source_color, destination_color;
977 int source_team_index, destination_team_index;
979 // in normal deathmatch we can just apply the color and we're done
981 SetPlayerColors(this, _color);
985 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
986 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING, this.netname);
993 source_color = this.clientcolors & 0x0F;
994 destination_color = _color & 0x0F;
996 source_team_index = Team_TeamToIndex(source_color + 1);
997 destination_team_index = Team_TeamToIndex(destination_color + 1);
999 if (destination_team_index == -1)
1004 entity balance = TeamBalance_CheckAllowedTeams(this);
1006 if (destination_team_index == 1 && !TeamBalance_IsTeamAllowedInternal(
1009 destination_team_index = 4;
1011 if (destination_team_index == 4 && !TeamBalance_IsTeamAllowedInternal(
1014 destination_team_index = 3;
1016 if (destination_team_index == 3 && !TeamBalance_IsTeamAllowedInternal(
1019 destination_team_index = 2;
1021 if (destination_team_index == 2 && !TeamBalance_IsTeamAllowedInternal(
1024 destination_team_index = 1;
1027 // not changing teams
1028 if (source_color == destination_color)
1030 SetPlayerTeam(this, destination_team_index, source_team_index, true);
1031 TeamBalance_Destroy(balance);
1035 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && CS(this).wasplayer)) {
1036 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
1037 return; // changing teams is not allowed
1040 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
1041 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
1043 TeamBalance_GetTeamCounts(balance, this);
1044 if ((Team_IndexToBit(destination_team_index) &
1045 TeamBalance_FindBestTeams(balance, this, false)) == 0)
1047 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
1048 TeamBalance_Destroy(balance);
1052 if (IS_PLAYER(this) && source_team_index != destination_team_index)
1054 // reduce frags during a team change
1055 TeamchangeFrags(this);
1057 if (!SetPlayerTeam(this, destination_team_index, source_team_index,
1060 TeamBalance_Destroy(balance);
1063 TeamBalance_AutoBalanceBots(balance, source_team_index,
1064 destination_team_index);
1065 if (!IS_PLAYER(this) || (source_team_index == destination_team_index))
1067 TeamBalance_Destroy(balance);
1070 KillPlayerForTeamChange(this);
1071 TeamBalance_Destroy(balance);