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 .int m_num_control_points; ///< Number of control points owned by a team.
39 .entity m_lowest_human; ///< Human with the lowest score in a team.
40 .entity m_lowest_bot; ///< Bot with the lowest score in a team.
42 entity g_team_entities[NUM_TEAMS]; ///< Holds global team entities.
44 STATIC_INIT(g_team_entities)
46 for (int i = 0; i < NUM_TEAMS; ++i)
48 g_team_entities[i] = spawn();
52 entity Team_GetTeamFromIndex(int index)
54 if (!Team_IsValidIndex(index))
56 LOG_FATALF("Team_GetTeamFromIndex: Index is invalid: %f", index);
58 return g_team_entities[index - 1];
61 entity Team_GetTeam(int team_num)
63 if (!Team_IsValidTeam(team_num))
65 LOG_FATALF("Team_GetTeam: Value is invalid: %f", team_num);
67 return g_team_entities[Team_TeamToIndex(team_num) - 1];
70 float Team_GetTeamScore(entity team_)
72 return team_.m_team_score;
75 void Team_SetTeamScore(entity team_, float score)
77 team_.m_team_score = score;
80 int Team_GetNumberOfAlivePlayers(entity team_)
82 return team_.m_num_players_alive;
85 void Team_SetNumberOfAlivePlayers(entity team_, int number)
87 team_.m_num_players_alive = number;
90 int Team_GetNumberOfAliveTeams()
93 for (int i = 0; i < NUM_TEAMS; ++i)
95 if (g_team_entities[i].m_num_players_alive > 0)
103 int Team_GetNumberOfControlPoints(entity team_)
105 return team_.m_num_control_points;
108 void Team_SetNumberOfControlPoints(entity team_, int number)
110 team_.m_num_control_points = number;
113 int Team_GetNumberOfTeamsWithControlPoints()
116 for (int i = 0; i < NUM_TEAMS; ++i)
118 if (g_team_entities[i].m_num_control_points > 0)
126 void TeamchangeFrags(entity e)
128 PlayerScore_Clear(e);
131 void LogTeamchange(float player_id, float team_number, float type)
133 if(!autocvar_sv_eventlog)
139 GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type)));
142 void default_delayedinit(entity this)
144 if(!scores_initialized)
145 ScoreRules_generic();
148 void InitGameplayMode()
152 // find out good world mins/maxs bounds, either the static bounds found by looking for solid, or the mapinfo specified bounds
154 // assign reflectively to avoid "assignment to world" warning
155 int done = 0; for (int i = 0, n = numentityfields(); i < n; ++i) {
156 string k = entityfieldname(i); vector v = (k == "mins") ? mi_min : (k == "maxs") ? mi_max : '0 0 0';
158 putentityfieldstring(i, world, sprintf("%v", v));
159 if (++done == 2) break;
162 // currently, NetRadiant's limit is 131072 qu for each side
163 // distance from one corner of a 131072qu cube to the opposite corner is approx. 227023 qu
164 // set the distance according to map size but don't go over the limit to avoid issues with float precision
165 // in case somebody makes extremely large maps
166 max_shot_distance = min(230000, vlen(world.maxs - world.mins));
168 MapInfo_LoadMapSettings(mapname);
169 GameRules_teams(false);
171 if (!cvar_value_issafe(world.fog))
173 LOG_INFO("The current map contains a potentially harmful fog setting, ignored");
174 world.fog = string_null;
176 if(MapInfo_Map_fog != "")
177 if(MapInfo_Map_fog == "none")
178 world.fog = string_null;
180 world.fog = strzone(MapInfo_Map_fog);
181 clientstuff = strzone(MapInfo_Map_clientstuff);
183 MapInfo_ClearTemps();
185 gamemode_name = MapInfo_Type_ToText(MapInfo_LoadedGametype);
187 cache_mutatormsg = strzone("");
188 cache_lastmutatormsg = strzone("");
190 InitializeEntity(NULL, default_delayedinit, INITPRIO_GAMETYPE_FALLBACK);
193 string GetClientVersionMessage(entity this)
195 if (CS(this).version_mismatch) {
196 if(CS(this).version < autocvar_gameversion) {
197 return strcat("This is Xonotic ", autocvar_g_xonoticversion,
198 "\n^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8");
200 return strcat("This is Xonotic ", autocvar_g_xonoticversion,
201 "\n^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8");
204 return strcat("Welcome to Xonotic ", autocvar_g_xonoticversion);
208 string getwelcomemessage(entity this)
210 MUTATOR_CALLHOOK(BuildMutatorsPrettyString, "");
211 string modifications = M_ARGV(0, string);
215 if(g_weaponarena_random)
216 modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
218 modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
220 else if(cvar("g_balance_blaster_weaponstartoverride") == 0)
221 modifications = strcat(modifications, ", No start weapons");
222 if(cvar("sv_gravity") < stof(cvar_defstring("sv_gravity")))
223 modifications = strcat(modifications, ", Low gravity");
224 if(g_weapon_stay && !g_cts)
225 modifications = strcat(modifications, ", Weapons stay");
227 modifications = strcat(modifications, ", Jet pack");
228 if(autocvar_g_powerups == 0)
229 modifications = strcat(modifications, ", No powerups");
230 if(autocvar_g_powerups > 0)
231 modifications = strcat(modifications, ", Powerups");
232 modifications = substring(modifications, 2, strlen(modifications) - 2);
234 string versionmessage = GetClientVersionMessage(this);
235 string s = strcat(versionmessage, "^8\n^8\nmatch type is ^1", gamemode_name, "^8\n");
237 if(modifications != "")
238 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
240 if(cache_lastmutatormsg != autocvar_g_mutatormsg)
242 if(cache_lastmutatormsg)
243 strunzone(cache_lastmutatormsg);
245 strunzone(cache_mutatormsg);
246 cache_lastmutatormsg = strzone(autocvar_g_mutatormsg);
247 cache_mutatormsg = strzone(cache_lastmutatormsg);
250 if (cache_mutatormsg != "") {
251 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
254 string mutator_msg = "";
255 MUTATOR_CALLHOOK(BuildGameplayTipsString, mutator_msg);
256 mutator_msg = M_ARGV(0, string);
258 s = strcat(s, mutator_msg); // trust that the mutator will do proper formatting
260 string motd = autocvar_sv_motd;
262 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
267 void setcolor(entity this, int clr)
270 this.clientcolors = clr;
271 this.team = (clr & 15) + 1;
273 builtin_setcolor(this, clr);
277 bool Entity_HasValidTeam(entity this)
279 return Team_IsValidTeam(this.team);
282 int Entity_GetTeamIndex(entity this)
284 return Team_TeamToIndex(this.team);
287 entity Entity_GetTeam(entity this)
289 int index = Entity_GetTeamIndex(this);
290 if (!Team_IsValidIndex(index))
294 return Team_GetTeamFromIndex(index);
297 void SetPlayerColors(entity player, float _color)
299 float pants = _color & 0x0F;
300 float shirt = _color & 0xF0;
303 setcolor(player, 16 * pants + pants);
307 setcolor(player, shirt + pants);
311 bool Player_SetTeamIndex(entity player, int index)
313 int new_team = Team_IndexToTeam(index);
314 if (player.team == new_team)
316 // This is important when players join the game and one of their color
317 // matches the team color while other doesn't. For example [BOT]Lion.
318 SetPlayerColors(player, new_team - 1);
321 int old_index = Team_TeamToIndex(player.team);
322 if (MUTATOR_CALLHOOK(Player_ChangeTeam, player, old_index, index) == true)
324 // Mutator has blocked team change.
327 SetPlayerColors(player, new_team - 1);
328 MUTATOR_CALLHOOK(Player_ChangedTeam, player, old_index, index);
332 bool SetPlayerTeam(entity player, int destination_team_index,
333 int source_team_index, bool no_print)
335 if (!Player_SetTeamIndex(player, destination_team_index))
339 LogTeamchange(player.playerid, player.team, 3); // log manual team join
344 bprint(playername(player, false), "^7 has changed from ",
345 Team_IndexToColoredFullName(source_team_index), "^7 to ",
346 Team_IndexToColoredFullName(destination_team_index), "\n");
350 void KillPlayerForTeamChange(entity player)
356 if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true)
360 Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP,
361 player.origin, '0 0 0');
364 entity TeamBalance_CheckAllowedTeams(entity for_whom)
366 entity balance = spawn();
367 for (int i = 0; i < NUM_TEAMS; ++i)
369 entity team_ = balance.m_team_balance_team[i] = spawn();
370 team_.m_team_score = g_team_entities[i].m_team_score;
371 team_.m_num_players = TEAM_NOT_ALLOWED;
372 team_.m_num_bots = 0;
373 team_.m_lowest_human = NULL;
374 team_.m_lowest_bot = NULL;
378 string teament_name = string_null;
379 bool mutator_returnvalue = MUTATOR_CALLHOOK(TeamBalance_CheckAllowedTeams,
380 teams_mask, teament_name, for_whom);
381 teams_mask = M_ARGV(0, float);
382 teament_name = M_ARGV(1, string);
383 if (mutator_returnvalue)
385 for (int i = 0; i < NUM_TEAMS; ++i)
387 if (teams_mask & BIT(i))
389 balance.m_team_balance_team[i].m_num_players = 0;
396 entity head = find(NULL, classname, teament_name);
399 if (Team_IsValidTeam(head.team))
401 TeamBalance_GetTeam(balance, head.team).m_num_players = 0;
403 head = find(head, classname, teament_name);
407 // TODO: Balance quantity of bots across > 2 teams when bot_vs_human is set (and remove next line)
408 if (AvailableTeams() == 2)
409 if (autocvar_bot_vs_human && for_whom)
411 if (autocvar_bot_vs_human > 0)
413 // find last team available
414 if (IS_BOT_CLIENT(for_whom))
416 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
418 TeamBalance_BanTeamsExcept(balance, 4);
420 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
422 TeamBalance_BanTeamsExcept(balance, 3);
426 TeamBalance_BanTeamsExcept(balance, 2);
428 // no further cases, we know at least 2 teams exist
432 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
434 TeamBalance_BanTeamsExcept(balance, 1);
436 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
438 TeamBalance_BanTeamsExcept(balance, 2);
442 TeamBalance_BanTeamsExcept(balance, 3);
444 // no further cases, bots have one of the teams
449 // find first team available
450 if (IS_BOT_CLIENT(for_whom))
452 if (TeamBalance_IsTeamAllowedInternal(balance, 1))
454 TeamBalance_BanTeamsExcept(balance, 1);
456 else if (TeamBalance_IsTeamAllowedInternal(balance, 2))
458 TeamBalance_BanTeamsExcept(balance, 2);
462 TeamBalance_BanTeamsExcept(balance, 3);
464 // no further cases, we know at least 2 teams exist
468 if (TeamBalance_IsTeamAllowedInternal(balance, 4))
470 TeamBalance_BanTeamsExcept(balance, 4);
472 else if (TeamBalance_IsTeamAllowedInternal(balance, 3))
474 TeamBalance_BanTeamsExcept(balance, 3);
478 TeamBalance_BanTeamsExcept(balance, 2);
480 // no further cases, bots have one of the teams
487 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
491 // if player has a forced team, ONLY allow that one
492 for (int i = 1; i <= NUM_TEAMS; ++i)
494 if (for_whom.team_forced == Team_IndexToTeam(i) &&
495 TeamBalance_IsTeamAllowedInternal(balance, i))
497 TeamBalance_BanTeamsExcept(balance, i);
501 balance.m_team_balance_state = TEAM_BALANCE_TEAMS_CHECKED;
505 void TeamBalance_Destroy(entity balance)
511 for (int i = 0; i < NUM_TEAMS; ++i)
513 remove(balance.(m_team_balance_team[i]));
518 int TeamBalance_GetAllowedTeams(entity balance)
522 LOG_FATAL("TeamBalance_GetAllowedTeams: Team balance entity is NULL.");
524 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
526 LOG_FATAL("TeamBalance_GetAllowedTeams: "
527 "Team balance entity is not initialized.");
530 for (int i = 1; i <= NUM_TEAMS; ++i)
532 if (TeamBalance_IsTeamAllowedInternal(balance, i))
534 result |= Team_IndexToBit(i);
540 bool TeamBalance_IsTeamAllowed(entity balance, int index)
544 LOG_FATAL("TeamBalance_IsTeamAllowed: Team balance entity is NULL.");
546 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
548 LOG_FATAL("TeamBalance_IsTeamAllowed: "
549 "Team balance entity is not initialized.");
551 if (!Team_IsValidIndex(index))
553 LOG_FATALF("TeamBalance_IsTeamAllowed: Team index is invalid: %f",
556 return TeamBalance_IsTeamAllowedInternal(balance, index);
559 void TeamBalance_GetTeamCounts(entity balance, entity ignore)
563 LOG_FATAL("TeamBalance_GetTeamCounts: Team balance entity is NULL.");
565 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
567 LOG_FATAL("TeamBalance_GetTeamCounts: "
568 "Team balance entity is not initialized.");
570 if (MUTATOR_CALLHOOK(TeamBalance_GetTeamCounts) == true)
572 // Mutator has overriden the configuration.
573 for (int i = 1; i <= NUM_TEAMS; ++i)
575 entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
576 if (TeamBalanceTeam_IsAllowed(team_))
578 MUTATOR_CALLHOOK(TeamBalance_GetTeamCount, i, ignore,
579 team_.m_num_players, team_.m_num_bots, team_.m_lowest_human,
581 team_.m_num_players = M_ARGV(2, float);
582 team_.m_num_bots = M_ARGV(3, float);
583 team_.m_lowest_human = M_ARGV(4, entity);
584 team_.m_lowest_bot = M_ARGV(5, entity);
590 // Manually count all players.
598 if (IS_PLAYER(it) || it.caplayer)
602 else if (it.team_forced > 0)
604 team_num = it.team_forced; // reserve the spot
610 if (!Team_IsValidTeam(team_num))
614 entity team_ = TeamBalance_GetTeam(balance, team_num);
615 if (!TeamBalanceTeam_IsAllowed(team_))
619 ++team_.m_num_players;
620 if (IS_BOT_CLIENT(it))
624 float temp_score = PlayerScore_Get(it, SP_SCORE);
625 if (!IS_BOT_CLIENT(it))
627 if (team_.m_lowest_human == NULL)
629 team_.m_lowest_human = it;
632 if (temp_score < PlayerScore_Get(team_.m_lowest_human,
635 team_.m_lowest_human = it;
639 if (team_.m_lowest_bot == NULL)
641 team_.m_lowest_bot = it;
644 if (temp_score < PlayerScore_Get(team_.m_lowest_bot, SP_SCORE))
646 team_.m_lowest_bot = it;
651 // if the player who has a forced team has not joined yet, reserve the spot
652 if (autocvar_g_campaign)
654 if (Team_IsValidIndex(autocvar_g_campaign_forceteam))
656 entity team_ = TeamBalance_GetTeamFromIndex(balance,
657 autocvar_g_campaign_forceteam);
658 if (team_.m_num_players == team_.m_num_bots)
660 ++team_.m_num_players;
664 balance.m_team_balance_state = TEAM_BALANCE_TEAM_COUNTS_FILLED;
667 int TeamBalance_GetNumberOfPlayers(entity balance, int index)
671 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
672 "Team balance entity is NULL.");
674 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
676 LOG_FATAL("TeamBalance_GetNumberOfPlayers: "
677 "TeamBalance_GetTeamCounts has not been called.");
679 if (!Team_IsValidIndex(index))
681 LOG_FATALF("TeamBalance_GetNumberOfPlayers: Team index is invalid: %f",
684 return balance.m_team_balance_team[index - 1].m_num_players;
687 int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player)
691 LOG_FATAL("TeamBalance_FindBestTeam: Team balance entity is NULL.");
693 if (balance.m_team_balance_state == TEAM_BALANCE_UNINITIALIZED)
695 LOG_FATAL("TeamBalance_FindBestTeam: "
696 "Team balance entity is not initialized.");
698 // count how many players are in each team
701 TeamBalance_GetTeamCounts(balance, player);
705 TeamBalance_GetTeamCounts(balance, NULL);
707 int team_bits = TeamBalance_FindBestTeams(balance, player, true);
710 LOG_FATALF("TeamBalance_FindBestTeam: No teams available for %s\n",
711 MapInfo_Type_ToString(MapInfo_CurrentGametype()));
713 RandomSelection_Init();
714 for (int i = 1; i <= NUM_TEAMS; ++i)
716 if (team_bits & Team_IndexToBit(i))
718 RandomSelection_AddFloat(i, 1, 1);
721 return RandomSelection_chosen_float;
724 int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score)
728 LOG_FATAL("TeamBalance_FindBestTeams: Team balance entity is NULL.");
730 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
732 LOG_FATAL("TeamBalance_FindBestTeams: "
733 "TeamBalance_GetTeamCounts has not been called.");
735 if (MUTATOR_CALLHOOK(TeamBalance_FindBestTeams, player) == true)
737 return M_ARGV(1, float);
740 int previous_team = 0;
741 for (int i = 1; i <= NUM_TEAMS; ++i)
743 if (!TeamBalance_IsTeamAllowedInternal(balance, i))
747 if (previous_team == 0)
749 team_bits = Team_IndexToBit(i);
753 int compare = TeamBalance_CompareTeams(balance, i, previous_team,
755 if (compare == TEAMS_COMPARE_LESS)
757 team_bits = Team_IndexToBit(i);
761 if (compare == TEAMS_COMPARE_EQUAL)
763 team_bits |= Team_IndexToBit(i);
770 void TeamBalance_JoinBestTeam(entity this, bool force_best_team)
772 // don't join a team if we're not playing a team game
778 // find out what teams are available
779 entity balance = TeamBalance_CheckAllowedTeams(this);
781 // if we don't care what team they end up on, put them on whatever team they entered as.
782 // if they're not on a valid team, then let other code put them on the smallest team
783 if (!force_best_team)
785 int selected_team_index = -1;
786 for (int i = 1; i <= NUM_TEAMS; ++i)
788 if (TeamBalance_IsTeamAllowedInternal(balance, i) &&
789 (Team_TeamToIndex(this.team) == i))
791 selected_team_index = i;
796 if (Team_IsValidIndex(selected_team_index))
798 Player_SetTeamIndex(this, selected_team_index);
799 LogTeamchange(this.playerid, this.team, 99);
800 TeamBalance_Destroy(balance);
804 // otherwise end up on the smallest team (handled below)
805 if (this.bot_forced_team)
807 TeamBalance_Destroy(balance);
810 int best_team_index = TeamBalance_FindBestTeam(balance, this, true);
811 int old_team_index = Team_TeamToIndex(this.team);
812 TeamchangeFrags(this);
813 Player_SetTeamIndex(this, best_team_index);
814 LogTeamchange(this.playerid, this.team, 2); // log auto join
815 if ((old_team_index != -1) && !IS_BOT_CLIENT(this))
817 TeamBalance_AutoBalanceBots(balance, old_team_index, best_team_index);
819 KillPlayerForTeamChange(this);
820 TeamBalance_Destroy(balance);
823 int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b,
824 entity player, bool use_score)
828 LOG_FATAL("TeamBalance_CompareTeams: Team balance entity is NULL.");
830 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
832 LOG_FATAL("TeamBalance_CompareTeams: "
833 "TeamBalance_GetTeamCounts has not been called.");
835 if (!Team_IsValidIndex(team_index_a))
837 LOG_FATALF("TeamBalance_CompareTeams: team_index_a is invalid: %f",
840 if (!Team_IsValidIndex(team_index_b))
842 LOG_FATALF("TeamBalance_CompareTeams: team_index_b is invalid: %f",
845 if (team_index_a == team_index_b)
847 return TEAMS_COMPARE_EQUAL;
849 entity team_a = TeamBalance_GetTeamFromIndex(balance, team_index_a);
850 entity team_b = TeamBalance_GetTeamFromIndex(balance, team_index_b);
851 return TeamBalance_CompareTeamsInternal(team_a, team_b, player, use_score);
854 void TeamBalance_AutoBalanceBots(entity balance, int source_team_index,
855 int destination_team_index)
859 LOG_FATAL("TeamBalance_AutoBalanceBots: Team balance entity is NULL.");
861 if (balance.m_team_balance_state != TEAM_BALANCE_TEAM_COUNTS_FILLED)
863 LOG_FATAL("TeamBalance_AutoBalanceBots: "
864 "TeamBalance_GetTeamCounts has not been called.");
866 if (!Team_IsValidIndex(source_team_index))
868 LOG_WARNF("AutoBalanceBots: Source team index is invalid: %f",
872 if (!Team_IsValidIndex(destination_team_index))
874 LOG_WARNF("AutoBalanceBots: Destination team index is invalid: %f",
875 destination_team_index);
878 if (!autocvar_g_balance_teams ||
879 !autocvar_g_balance_teams_prevent_imbalance)
883 entity source_team = TeamBalance_GetTeamFromIndex(balance,
885 if (!TeamBalanceTeam_IsAllowed(source_team))
889 entity destination_team = TeamBalance_GetTeamFromIndex(balance,
890 destination_team_index);
891 if ((destination_team.m_num_players <= source_team.m_num_players) ||
892 (destination_team.m_lowest_bot == NULL))
896 Player_SetTeamIndex(destination_team.m_lowest_bot, source_team_index);
897 KillPlayerForTeamChange(destination_team.m_lowest_bot);
900 bool TeamBalance_IsTeamAllowedInternal(entity balance, int index)
902 return balance.m_team_balance_team[index - 1].m_num_players !=
906 void TeamBalance_BanTeamsExcept(entity balance, int index)
908 for (int i = 1; i <= NUM_TEAMS; ++i)
912 balance.m_team_balance_team[i - 1].m_num_players = TEAM_NOT_ALLOWED;
917 entity TeamBalance_GetTeamFromIndex(entity balance, int index)
919 if (!Team_IsValidIndex(index))
921 LOG_FATALF("TeamBalance_GetTeamFromIndex: Index is invalid: %f", index);
923 return balance.m_team_balance_team[index - 1];
926 entity TeamBalance_GetTeam(entity balance, int team_num)
928 return TeamBalance_GetTeamFromIndex(balance, Team_TeamToIndex(team_num));
931 bool TeamBalanceTeam_IsAllowed(entity team_)
933 return team_.m_num_players != TEAM_NOT_ALLOWED;
936 int TeamBalanceTeam_GetNumberOfPlayers(entity team_)
938 return team_.m_num_players;
941 int TeamBalanceTeam_GetNumberOfBots(entity team_)
943 return team_.m_num_bots;
946 entity TeamBalanceTeam_GetLowestHuman(entity team_)
948 return team_.m_lowest_human;
951 entity TeamBalanceTeam_GetLowestBot(entity team_)
953 return team_.m_lowest_bot;
956 int TeamBalance_CompareTeamsInternal(entity team_a, entity team_b,
957 entity player, bool use_score)
959 if (team_a == team_b)
961 return TEAMS_COMPARE_EQUAL;
963 if (!TeamBalanceTeam_IsAllowed(team_a) ||
964 !TeamBalanceTeam_IsAllowed(team_b))
966 return TEAMS_COMPARE_INVALID;
968 int num_players_team_a = team_a.m_num_players;
969 int num_players_team_b = team_b.m_num_players;
970 if (IS_REAL_CLIENT(player) && bots_would_leave)
972 num_players_team_a -= team_a.m_num_bots;
973 num_players_team_b -= team_b.m_num_bots;
975 if (num_players_team_a < num_players_team_b)
977 return TEAMS_COMPARE_LESS;
979 if (num_players_team_a > num_players_team_b)
981 return TEAMS_COMPARE_GREATER;
985 return TEAMS_COMPARE_EQUAL;
987 if (team_a.m_team_score < team_b.m_team_score)
989 return TEAMS_COMPARE_LESS;
991 if (team_a.m_team_score > team_b.m_team_score)
993 return TEAMS_COMPARE_GREATER;
995 return TEAMS_COMPARE_EQUAL;
998 void SV_ChangeTeam(entity this, float _color)
1000 int source_color, destination_color;
1001 int source_team_index, destination_team_index;
1003 // in normal deathmatch we can just apply the color and we're done
1005 SetPlayerColors(this, _color);
1007 if(!IS_CLIENT(this))
1009 // since this is an engine function, and gamecode doesn't have any calls earlier than this, do the connecting message here
1010 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_CONNECTING, this.netname);
1017 source_color = this.clientcolors & 0x0F;
1018 destination_color = _color & 0x0F;
1020 source_team_index = Team_TeamToIndex(source_color + 1);
1021 destination_team_index = Team_TeamToIndex(destination_color + 1);
1023 if (destination_team_index == -1)
1028 entity balance = TeamBalance_CheckAllowedTeams(this);
1030 if (destination_team_index == 1 && !TeamBalance_IsTeamAllowedInternal(
1033 destination_team_index = 4;
1035 if (destination_team_index == 4 && !TeamBalance_IsTeamAllowedInternal(
1038 destination_team_index = 3;
1040 if (destination_team_index == 3 && !TeamBalance_IsTeamAllowedInternal(
1043 destination_team_index = 2;
1045 if (destination_team_index == 2 && !TeamBalance_IsTeamAllowedInternal(
1048 destination_team_index = 1;
1051 // not changing teams
1052 if (source_color == destination_color)
1054 SetPlayerTeam(this, destination_team_index, source_team_index, true);
1055 TeamBalance_Destroy(balance);
1059 if((autocvar_g_campaign) || (autocvar_g_changeteam_banned && CS(this).wasplayer)) {
1060 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_NOTALLOWED);
1061 return; // changing teams is not allowed
1064 // autocvar_g_balance_teams_prevent_imbalance only makes sense if autocvar_g_balance_teams is on, as it makes the team selection dialog pointless
1065 if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance)
1067 TeamBalance_GetTeamCounts(balance, this);
1068 if ((Team_IndexToBit(destination_team_index) &
1069 TeamBalance_FindBestTeams(balance, this, false)) == 0)
1071 Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM);
1072 TeamBalance_Destroy(balance);
1076 if (IS_PLAYER(this) && source_team_index != destination_team_index)
1078 // reduce frags during a team change
1079 TeamchangeFrags(this);
1081 if (!SetPlayerTeam(this, destination_team_index, source_team_index,
1084 TeamBalance_Destroy(balance);
1087 TeamBalance_AutoBalanceBots(balance, source_team_index,
1088 destination_team_index);
1089 if (!IS_PLAYER(this) || (source_team_index == destination_team_index))
1091 TeamBalance_Destroy(balance);
1094 KillPlayerForTeamChange(this);
1095 TeamBalance_Destroy(balance);