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 Indicates that the player is not allowed to join a team.
19 const int TEAM_NOT_ALLOWED = -1;
21 .float m_team_score; ///< The score of the team.
22 .int m_num_players; ///< Number of players (both humans and bots) in a team.
23 .int m_num_bots; ///< Number of bots in a team.
24 .entity m_lowest_human; ///< Human with the lowest score in a team.
25 .entity m_lowest_bot; ///< Bot with the lowest score in a team.
27 entity g_team_entities[4]; ///< Holds global team entities.
29 STATIC_INIT(g_team_entities)
31 g_team_entities[0] = spawn();
32 g_team_entities[1] = spawn();
33 g_team_entities[2] = spawn();
34 g_team_entities[3] = spawn();
37 entity Team_GetTeamFromIndex(int index)
39 if (!Team_IsValidIndex(index))
41 LOG_FATALF("Team_GetTeamFromIndex: Index is invalid: %f", index);
43 return g_team_entities[index - 1];
46 entity Team_GetTeam(int team_num)
48 if (!Team_IsValidTeam(team_num))
50 LOG_FATALF("Team_GetTeam: Value is invalid: %f", team_num);
52 return g_team_entities[Team_TeamToNumber(team_num) - 1];
55 float Team_GetTeamScore(entity team_)
57 return team_.m_team_score;
60 void Team_SetTeamScore(entity team_, float score)
62 team_.m_team_score = score;
65 void CheckAllowedTeams(entity for_whom)
67 for (int i = 0; i < 4; ++i)
69 g_team_entities[i].m_num_players = TEAM_NOT_ALLOWED;
70 g_team_entities[i].m_num_bots = 0;
71 g_team_entities[i].m_lowest_human = NULL;
72 g_team_entities[i].m_lowest_bot = NULL;
76 string teament_name = string_null;
77 bool mutator_returnvalue = MUTATOR_CALLHOOK(CheckAllowedTeams, teams_mask,
78 teament_name, for_whom);
79 teams_mask = M_ARGV(0, float);
80 teament_name = M_ARGV(1, string);
81 if (mutator_returnvalue)
83 for (int i = 0; i < 4; ++i)
85 if (teams_mask & BIT(i))
87 g_team_entities[i].m_num_players = 0;
92 // find out what teams are allowed if necessary
95 entity head = find(NULL, classname, teament_name);
98 if (Team_IsValidTeam(head.team))
100 Team_GetTeam(head.team).m_num_players = 0;
102 head = find(head, classname, teament_name);
106 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
107 if (AvailableTeams() == 2)
108 if (autocvar_bot_vs_human && for_whom)
110 if (autocvar_bot_vs_human > 0)
112 // find last team available
113 if (IS_BOT_CLIENT(for_whom))
115 if (Team_IsAllowed(g_team_entities[3]))
117 g_team_entities[2].m_num_players = TEAM_NOT_ALLOWED;
118 g_team_entities[1].m_num_players = TEAM_NOT_ALLOWED;
119 g_team_entities[0].m_num_players = TEAM_NOT_ALLOWED;
121 else if (Team_IsAllowed(g_team_entities[2]))
123 g_team_entities[3].m_num_players = TEAM_NOT_ALLOWED;
124 g_team_entities[1].m_num_players = TEAM_NOT_ALLOWED;
125 g_team_entities[0].m_num_players = TEAM_NOT_ALLOWED;
129 g_team_entities[3].m_num_players = TEAM_NOT_ALLOWED;
130 g_team_entities[2].m_num_players = TEAM_NOT_ALLOWED;
131 g_team_entities[0].m_num_players = TEAM_NOT_ALLOWED;
133 // no further cases, we know at least 2 teams exist
137 if (Team_IsAllowed(g_team_entities[0]))
139 g_team_entities[1].m_num_players = TEAM_NOT_ALLOWED;
140 g_team_entities[2].m_num_players = TEAM_NOT_ALLOWED;
141 g_team_entities[3].m_num_players = TEAM_NOT_ALLOWED;
143 else if (Team_IsAllowed(g_team_entities[1]))
145 g_team_entities[0].m_num_players = TEAM_NOT_ALLOWED;
146 g_team_entities[2].m_num_players = TEAM_NOT_ALLOWED;
147 g_team_entities[3].m_num_players = TEAM_NOT_ALLOWED;
151 g_team_entities[0].m_num_players = TEAM_NOT_ALLOWED;
152 g_team_entities[1].m_num_players = TEAM_NOT_ALLOWED;
153 g_team_entities[3].m_num_players = TEAM_NOT_ALLOWED;
155 // no further cases, bots have one of the teams
160 // find first team available
161 if (IS_BOT_CLIENT(for_whom))
163 if (Team_IsAllowed(g_team_entities[0]))
165 g_team_entities[1].m_num_players = TEAM_NOT_ALLOWED;
166 g_team_entities[2].m_num_players = TEAM_NOT_ALLOWED;
167 g_team_entities[3].m_num_players = TEAM_NOT_ALLOWED;
169 else if (Team_IsAllowed(g_team_entities[1]))
171 g_team_entities[0].m_num_players = TEAM_NOT_ALLOWED;
172 g_team_entities[2].m_num_players = TEAM_NOT_ALLOWED;
173 g_team_entities[3].m_num_players = TEAM_NOT_ALLOWED;
177 g_team_entities[0].m_num_players = TEAM_NOT_ALLOWED;
178 g_team_entities[1].m_num_players = TEAM_NOT_ALLOWED;
179 g_team_entities[3].m_num_players = TEAM_NOT_ALLOWED;
181 // no further cases, we know at least 2 teams exist
185 if (Team_IsAllowed(g_team_entities[3]))
187 g_team_entities[2].m_num_players = TEAM_NOT_ALLOWED;
188 g_team_entities[1].m_num_players = TEAM_NOT_ALLOWED;
189 g_team_entities[0].m_num_players = TEAM_NOT_ALLOWED;
191 else if (Team_IsAllowed(g_team_entities[2]))
193 g_team_entities[3].m_num_players = TEAM_NOT_ALLOWED;
194 g_team_entities[1].m_num_players = TEAM_NOT_ALLOWED;
195 g_team_entities[0].m_num_players = TEAM_NOT_ALLOWED;
199 g_team_entities[3].m_num_players = TEAM_NOT_ALLOWED;
200 g_team_entities[2].m_num_players = TEAM_NOT_ALLOWED;
201 g_team_entities[0].m_num_players = TEAM_NOT_ALLOWED;
203 // no further cases, bots have one of the teams
213 // if player has a forced team, ONLY allow that one
214 if (for_whom.team_forced == NUM_TEAM_1 && Team_IsAllowed(
217 g_team_entities[1].m_num_players = TEAM_NOT_ALLOWED;
218 g_team_entities[2].m_num_players = TEAM_NOT_ALLOWED;
219 g_team_entities[3].m_num_players = TEAM_NOT_ALLOWED;
221 else if (for_whom.team_forced == NUM_TEAM_2 && Team_IsAllowed(
224 g_team_entities[0].m_num_players = TEAM_NOT_ALLOWED;
225 g_team_entities[2].m_num_players = TEAM_NOT_ALLOWED;
226 g_team_entities[3].m_num_players = TEAM_NOT_ALLOWED;
228 else if (for_whom.team_forced == NUM_TEAM_3 && Team_IsAllowed(
231 g_team_entities[0].m_num_players = TEAM_NOT_ALLOWED;
232 g_team_entities[1].m_num_players = TEAM_NOT_ALLOWED;
233 g_team_entities[3].m_num_players = TEAM_NOT_ALLOWED;
235 else if (for_whom.team_forced == NUM_TEAM_4 && Team_IsAllowed(
238 g_team_entities[0].m_num_players = TEAM_NOT_ALLOWED;
239 g_team_entities[1].m_num_players = TEAM_NOT_ALLOWED;
240 g_team_entities[2].m_num_players = TEAM_NOT_ALLOWED;
244 int GetAllowedTeams()
247 for (int i = 0; i < 4; ++i)
249 if (Team_IsAllowed(g_team_entities[i]))
257 bool Team_IsAllowed(entity team_)
259 return team_.m_num_players != TEAM_NOT_ALLOWED;
262 void GetTeamCounts(entity ignore)
264 if (MUTATOR_CALLHOOK(GetTeamCounts) == true)
266 // Mutator has overriden the configuration.
267 for (int i = 0; i < 4; ++i)
269 entity team_ = g_team_entities[i];
270 if (Team_IsAllowed(team_))
272 MUTATOR_CALLHOOK(GetTeamCount, Team_NumberToTeam(i + 1), ignore,
273 team_.m_num_players, team_.m_num_bots, team_.m_lowest_human,
275 team_.m_num_players = M_ARGV(2, float);
276 team_.m_num_bots = M_ARGV(3, float);
277 team_.m_lowest_human = M_ARGV(4, entity);
278 team_.m_lowest_bot = M_ARGV(5, entity);
284 // Manually count all players.
292 if (IS_PLAYER(it) || it.caplayer)
296 else if (it.team_forced > 0)
298 team_num = it.team_forced; // reserve the spot
304 if (!Team_IsValidTeam(team_num))
308 entity team_ = Team_GetTeam(team_num);
309 if (!Team_IsAllowed(team_))
313 ++team_.m_num_players;
314 if (IS_BOT_CLIENT(it))
318 float temp_score = PlayerScore_Get(it, SP_SCORE);
319 if (!IS_BOT_CLIENT(it))
321 if (team_.m_lowest_human == NULL)
323 team_.m_lowest_human = it;
326 if (temp_score < PlayerScore_Get(team_.m_lowest_human,
329 team_.m_lowest_human = it;
333 if (team_.m_lowest_bot == NULL)
335 team_.m_lowest_bot = it;
338 if (temp_score < PlayerScore_Get(team_.m_lowest_bot, SP_SCORE))
340 team_.m_lowest_bot = it;
345 // if the player who has a forced team has not joined yet, reserve the spot
346 if (autocvar_g_campaign)
348 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
350 entity team_ = Team_GetTeamFromIndex(autocvar_g_campaign_forceteam);
351 if (team_.m_num_players == team_.m_num_bots)
353 ++team_.m_num_players;
359 int Team_GetNumberOfPlayers(entity team_)
361 return team_.m_num_players;
364 int Team_GetNumberOfBots(entity team_)
366 return team_.m_num_bots;
369 entity Team_GetLowestHuman(entity team_)
371 return team_.m_lowest_human;
374 entity Team_GetLowestBot(entity team_)
376 return team_.m_lowest_bot;
379 void TeamchangeFrags(entity e)
381 PlayerScore_Clear(e);
384 void LogTeamchange(float player_id, float team_number, float type)
386 if(!autocvar_sv_eventlog)
392 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
395 void default_delayedinit(entity this)
397 if(!scores_initialized)
398 ScoreRules_generic();
401 void InitGameplayMode()
405 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
407 // assign reflectively to avoid "assignment to world" warning
408 int done = 0; for (int i = 0, n = numentityfields(); i < n; ++i) {
409 string k = entityfieldname(i); vector v = (k == "mins") ? mi_min : (k == "maxs") ? mi_max : '0 0 0';
411 putentityfieldstring(i, world, sprintf("%v", v));
412 if (++done == 2) break;
415 // currently, NetRadiant's limit is 131072 qu for each side
416 // distance from one corner of a 131072qu cube to the opposite corner is approx. 227023 qu
417 // set the distance according to map size but don't go over the limit to avoid issues with float precision
418 // in case somebody makes extremely large maps
419 max_shot_distance = min(230000, vlen(world.maxs - world.mins));
421 MapInfo_LoadMapSettings(mapname);
422 GameRules_teams(false);
424 if (!cvar_value_issafe(world.fog))
426 LOG_INFO("The current map contains a potentially harmful fog setting, ignored");
427 world.fog = string_null;
429 if(MapInfo_Map_fog != "")
430 if(MapInfo_Map_fog == "none")
431 world.fog = string_null;
433 world.fog = strzone(MapInfo_Map_fog);
434 clientstuff = strzone(MapInfo_Map_clientstuff);
436 MapInfo_ClearTemps();
438 gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
440 cache_mutatormsg = strzone("");
441 cache_lastmutatormsg = strzone("");
443 InitializeEntity(NULL, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
446 string GetClientVersionMessage(entity this)
448 if (CS(this).version_mismatch) {
449 if(CS(this).version < autocvar_gameversion) {
450 return strcat("This is Xonotic ", autocvar_g_xonoticversion,
451 "\n^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8");
453 return strcat("This is Xonotic ", autocvar_g_xonoticversion,
454 "\n^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8");
457 return strcat("Welcome to Xonotic ", autocvar_g_xonoticversion);
461 string getwelcomemessage(entity this)
463 MUTATOR_CALLHOOK(BuildMutatorsPrettyString, "");
464 string modifications = M_ARGV(0, string);
468 if(g_weaponarena_random)
469 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
471 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
473 else if(cvar("g_balance_blaster_weaponstartoverride") == 0)
474 modifications = strcat(modifications, ", No start weapons");
475 if(cvar("sv_gravity") < stof(cvar_defstring("sv_gravity")))
476 modifications = strcat(modifications, ", Low gravity");
477 if(g_weapon_stay && !g_cts)
478 modifications = strcat(modifications, ", Weapons stay");
480 modifications = strcat(modifications, ", Jet pack");
481 if(autocvar_g_powerups == 0)
482 modifications = strcat(modifications, ", No powerups");
483 if(autocvar_g_powerups > 0)
484 modifications = strcat(modifications, ", Powerups");
485 modifications = substring(modifications, 2, strlen(modifications) - 2);
487 string versionmessage = GetClientVersionMessage(this);
488 string s = strcat(versionmessage, "^8\n^8\nmatch type is ^1", gamemode_name, "^8\n");
490 if(modifications != "")
491 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
493 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
495 if(cache_lastmutatormsg)
496 strunzone(cache_lastmutatormsg);
498 strunzone(cache_mutatormsg);
499 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
500 cache_mutatormsg = strzone(cache_lastmutatormsg);
503 if (cache_mutatormsg != "") {
504 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
507 string mutator_msg = "";
508 MUTATOR_CALLHOOK(BuildGameplayTipsString, mutator_msg);
509 mutator_msg = M_ARGV(0, string);
511 s = strcat(s, mutator_msg); // trust that the mutator will do proper formatting
513 string motd = autocvar_sv_motd;
515 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
520 void setcolor(entity this, int clr)
523 this.clientcolors = clr;
524 this.team = (clr & 15) + 1;
526 builtin_setcolor(this, clr);
530 void SetPlayerColors(entity player, float _color)
532 float pants = _color & 0x0F;
533 float shirt = _color & 0xF0;
536 setcolor(player, 16 * pants + pants);
540 setcolor(player, shirt + pants);
544 void KillPlayerForTeamChange(entity player)
550 if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
554 Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
555 player.origin, '0 0 0');
558 bool SetPlayerTeamSimple(entity player, int team_num)
560 if (player.team == team_num)
562 // This is important when players join the game and one of their color
563 // matches the team color while other doesn't. For example [BOT]Lion.
564 SetPlayerColors(player, team_num - 1);
567 if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, Team_TeamToNumber(
568 player.team), Team_TeamToNumber(team_num)) == true)
570 // Mutator has blocked team change.
573 int old_team_num = player.team;
574 SetPlayerColors(player, team_num - 1);
575 MUTATOR_CALLHOOK(Player_ChangedTeam, player, old_team_num, player.team);
579 bool SetPlayerTeam(entity player, int destination_team_index,
580 int source_team_index, bool no_print)
582 int team_num = Team_NumberToTeam(destination_team_index);
583 if (!SetPlayerTeamSimple(player, team_num))
587 LogTeamchange(player.playerid, player.team, 3); // log manual team join
592 bprint(playername(player, false), "^7 has changed from ",
593 Team_NumberToColoredFullName(source_team_index), "^7 to ",
594 Team_NumberToColoredFullName(destination_team_index), "\n");
598 bool IsTeamSmallerThanTeam(int team_index_a, int team_index_b, entity player,
601 if (!Team_IsValidIndex(team_index_a))
603 LOG_FATALF("IsTeamSmallerThanTeam: team_index_a is invalid: %f",
606 if (!Team_IsValidIndex(team_index_b))
608 LOG_FATALF("IsTeamSmallerThanTeam: team_index_b is invalid: %f",
611 if (team_index_a == team_index_b)
615 entity team_a = Team_GetTeamFromIndex(team_index_a);
616 entity team_b = Team_GetTeamFromIndex(team_index_b);
617 if (!Team_IsAllowed(team_a) || !Team_IsAllowed(team_b))
621 int num_players_team_a = team_a.m_num_players;
622 int num_players_team_b = team_b.m_num_players;
623 if (IS_REAL_CLIENT(player) && bots_would_leave)
625 num_players_team_a -= team_a.m_num_bots;
626 num_players_team_b -= team_b.m_num_bots;
630 return num_players_team_a < num_players_team_b;
632 if (num_players_team_a < num_players_team_b)
636 if (num_players_team_a > num_players_team_b)
640 return team_a.m_team_score < team_b.m_team_score;
643 bool IsTeamEqualToTeam(int team_index_a, int team_index_b, entity player,
646 if (!Team_IsValidIndex(team_index_a))
648 LOG_FATALF("IsTeamEqualToTeam: team_index_a is invalid: %f",
651 if (!Team_IsValidIndex(team_index_b))
653 LOG_FATALF("IsTeamEqualToTeam: team_index_b is invalid: %f",
656 if (team_index_a == team_index_b)
660 entity team_a = Team_GetTeamFromIndex(team_index_a);
661 entity team_b = Team_GetTeamFromIndex(team_index_b);
662 if (!Team_IsAllowed(team_a) || !Team_IsAllowed(team_b))
666 int num_players_team_a = team_a.m_num_players;
667 int num_players_team_b = team_b.m_num_players;
668 if (IS_REAL_CLIENT(player) && bots_would_leave)
670 num_players_team_a -= team_a.m_num_bots;
671 num_players_team_b -= team_b.m_num_bots;
675 return num_players_team_a == num_players_team_b;
677 if (num_players_team_a != num_players_team_b)
681 return team_a.m_team_score == team_b.m_team_score;
684 int FindBestTeams(entity player, bool use_score)
686 if (MUTATOR_CALLHOOK(FindBestTeams, player) == true)
688 return M_ARGV(1, float);
691 int previous_team = 0;
692 if (Team_IsAllowed(g_team_entities[0]))
697 if (Team_IsAllowed(g_team_entities[1]))
699 if (previous_team == 0)
704 else if (IsTeamSmallerThanTeam(2, previous_team, player, use_score))
709 else if (IsTeamEqualToTeam(2, previous_team, player, use_score))
715 if (Team_IsAllowed(g_team_entities[2]))
717 if (previous_team == 0)
722 else if (IsTeamSmallerThanTeam(3, previous_team, player, use_score))
727 else if (IsTeamEqualToTeam(3, previous_team, player, use_score))
733 if (Team_IsAllowed(g_team_entities[3]))
735 if (previous_team == 0)
739 else if (IsTeamSmallerThanTeam(4, previous_team, player, use_score))
743 else if (IsTeamEqualToTeam(4, previous_team, player, use_score))
751 int FindBestTeam(entity player, float ignore_player)
753 // count how many players are in each team
756 GetTeamCounts(player);
762 int team_bits = FindBestTeams(player, true);
765 LOG_FATALF("FindBestTeam: No teams available for %s\n",
766 MapInfo_Type_ToString(MapInfo_CurrentGametype()));
768 RandomSelection_Init();
769 if ((team_bits & BIT(0)) != 0)
771 RandomSelection_AddFloat(1, 1, 1);
773 if ((team_bits & BIT(1)) != 0)
775 RandomSelection_AddFloat(2, 1, 1);
777 if ((team_bits & BIT(2)) != 0)
779 RandomSelection_AddFloat(3, 1, 1);
781 if ((team_bits & BIT(3)) != 0)
783 RandomSelection_AddFloat(4, 1, 1);
785 return RandomSelection_chosen_float;
788 void JoinBestTeam(entity this, bool force_best_team)
790 // don't join a team if we're not playing a team game
796 // find out what teams are available
797 CheckAllowedTeams(this);
799 // if we don't care what team they end up on, put them on whatever team they entered as.
800 // if they're not on a valid team, then let other code put them on the smallest team
801 if (!force_best_team)
803 int selected_team_num = -1;
804 for (int i = 0; i < 4; ++i)
806 if (Team_IsAllowed(g_team_entities[i]) && (this.team ==
807 Team_NumberToTeam(i)))
809 selected_team_num = this.team;
814 if (Team_IsValidTeam(selected_team_num))
816 SetPlayerTeamSimple(this, selected_team_num);
817 LogTeamchange(this.playerid, this.team, 99);
821 // otherwise end up on the smallest team (handled below)
822 if (this.bot_forced_team)
826 int best_team_index = FindBestTeam(this, true);
827 int best_team_num = Team_NumberToTeam(best_team_index);
828 int old_team_index = Team_TeamToNumber(this.team);
829 TeamchangeFrags(this);
830 SetPlayerTeamSimple(this, best_team_num);
831 LogTeamchange(this.playerid, this.team, 2); // log auto join
832 if ((old_team_index != -1) && !IS_BOT_CLIENT(this))
834 AutoBalanceBots(old_team_index, best_team_index);
836 KillPlayerForTeamChange(this);
839 void SV_ChangeTeam(entity this, float _color)
841 int source_color, destination_color;
842 int source_team_index, destination_team_index;
844 // in normal deathmatch we can just apply the color and we're done
846 SetPlayerColors(this, _color);
850 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
851 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING, this.netname);
858 source_color = this.clientcolors & 0x0F;
859 destination_color = _color & 0x0F;
861 source_team_index = Team_TeamToNumber(source_color + 1);
862 destination_team_index = Team_TeamToNumber(destination_color + 1);
864 if (destination_team_index == -1)
869 CheckAllowedTeams(this);
871 if (destination_team_index == 1 && !Team_IsAllowed(g_team_entities[0])) destination_team_index = 4;
872 if (destination_team_index == 4 && !Team_IsAllowed(g_team_entities[3])) destination_team_index = 3;
873 if (destination_team_index == 3 && !Team_IsAllowed(g_team_entities[2])) destination_team_index = 2;
874 if (destination_team_index == 2 && !Team_IsAllowed(g_team_entities[1])) destination_team_index = 1;
876 // not changing teams
877 if (source_color == destination_color)
879 SetPlayerTeam(this, destination_team_index, source_team_index, true);
883 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && CS(this).wasplayer)) {
884 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
885 return; // changing teams is not allowed
888 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
889 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
892 if ((BIT(destination_team_index - 1) & FindBestTeams(this, false)) == 0)
894 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
898 if (IS_PLAYER(this) && source_team_index != destination_team_index)
900 // reduce frags during a team change
901 TeamchangeFrags(this);
903 if (!SetPlayerTeam(this, destination_team_index, source_team_index,
908 AutoBalanceBots(source_team_index, destination_team_index);
909 if (!IS_PLAYER(this) || (source_team_index == destination_team_index))
913 KillPlayerForTeamChange(this);
916 void AutoBalanceBots(int source_team_index, int destination_team_index)
918 if (!Team_IsValidIndex(source_team_index))
920 LOG_WARNF("AutoBalanceBots: Source team index is invalid: %f",
924 if (!Team_IsValidIndex(destination_team_index))
926 LOG_WARNF("AutoBalanceBots: Destination team index is invalid: %f",
927 destination_team_index);
930 if (!autocvar_g_balance_teams ||
931 !autocvar_g_balance_teams_prevent_imbalance)
935 entity source_team = Team_GetTeamFromIndex(source_team_index);
936 if (!Team_IsAllowed(source_team))
940 entity destination_team = Team_GetTeamFromIndex(destination_team_index);
941 if ((destination_team.m_num_players <= source_team.m_num_players) ||
942 (destination_team.m_lowest_bot == NULL))
946 SetPlayerTeamSimple(destination_team.m_lowest_bot,
947 Team_NumberToTeam(source_team_index));
948 KillPlayerForTeamChange(destination_team.m_lowest_bot);