From 8dc41732ce8f482ba23e1e2c4f91a734d2297c61 Mon Sep 17 00:00:00 2001 From: Lyberta Date: Thu, 8 Mar 2018 19:27:50 +0300 Subject: [PATCH] Better team balance function names. --- qcsrc/server/bot/default/bot.qc | 2 +- qcsrc/server/client.qc | 8 +- qcsrc/server/command/cmd.qc | 2 +- qcsrc/server/command/sv_cmd.qc | 2 +- qcsrc/server/teamplay.qc | 184 ++++++++++++++++---------------- qcsrc/server/teamplay.qh | 38 ++++--- 6 files changed, 120 insertions(+), 116 deletions(-) diff --git a/qcsrc/server/bot/default/bot.qc b/qcsrc/server/bot/default/bot.qc index 2f579aaf8..ec6ee35d3 100644 --- a/qcsrc/server/bot/default/bot.qc +++ b/qcsrc/server/bot/default/bot.qc @@ -438,7 +438,7 @@ void bot_clientconnect(entity this) else if(this.bot_forced_team==4) this.team = NUM_TEAM_4; else - JoinBestTeam(this, true); + JoinBestTeamForBalance(this, true); havocbot_setupbot(this); } diff --git a/qcsrc/server/client.qc b/qcsrc/server/client.qc index 48ea4c261..4109818ab 100644 --- a/qcsrc/server/client.qc +++ b/qcsrc/server/client.qc @@ -516,7 +516,7 @@ void PutPlayerInServer(entity this) accuracy_resend(this); if (this.team < 0) - JoinBestTeam(this, true); + JoinBestTeamForBalance(this, true); entity spot = SelectSpawnPoint(this, false); if (!spot) { @@ -903,7 +903,7 @@ void ClientKill_Now_TeamChange(entity this) { if(this.killindicator_teamchange == -1) { - JoinBestTeam( this, true ); + JoinBestTeamForBalance(this, true); } else if(this.killindicator_teamchange == -2) { @@ -1216,7 +1216,7 @@ void ClientConnect(entity this) int playerid_save = this.playerid; this.playerid = 0; // silent - JoinBestTeam(this, false); // if the team number is valid, keep it + JoinBestTeamForBalance(this, false); // if the team number is valid, keep it this.playerid = playerid_save; if (autocvar_sv_spectate || autocvar_g_campaign || this.team_forced < 0) { @@ -2006,7 +2006,7 @@ void Join(entity this) if(!this.team_selected) if(autocvar_g_campaign || autocvar_g_balance_teams) - JoinBestTeam(this, true); + JoinBestTeamForBalance(this, true); if(autocvar_g_campaign) campaign_bots_may_start = true; diff --git a/qcsrc/server/command/cmd.qc b/qcsrc/server/command/cmd.qc index 088925f10..2314f7103 100644 --- a/qcsrc/server/command/cmd.qc +++ b/qcsrc/server/command/cmd.qc @@ -396,7 +396,7 @@ void ClientCommand_selectteam(entity caller, float request, float argc) { CheckAllowedTeams(caller); GetTeamCounts(caller); - if ((BIT(Team_TeamToNumber(selection) - 1) & FindBestTeams(caller, false)) == 0) + if ((BIT(Team_TeamToNumber(selection) - 1) & FindBestTeamsForBalance(caller, false)) == 0) { Send_Notification(NOTIF_ONE, caller, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM); return; diff --git a/qcsrc/server/command/sv_cmd.qc b/qcsrc/server/command/sv_cmd.qc index 0e2d92557..d7ede6af4 100644 --- a/qcsrc/server/command/sv_cmd.qc +++ b/qcsrc/server/command/sv_cmd.qc @@ -1073,7 +1073,7 @@ void GameCommand_moveplayer(float request, float argc) else if (team_id == 0) // auto team { CheckAllowedTeams(client); - team_id = Team_NumberToTeam(FindBestTeam(client, false)); + team_id = Team_NumberToTeam(FindBestTeamForBalance(client, false)); } else { diff --git a/qcsrc/server/teamplay.qc b/qcsrc/server/teamplay.qc index 8b92f86b2..aa2a44b29 100644 --- a/qcsrc/server/teamplay.qc +++ b/qcsrc/server/teamplay.qc @@ -595,93 +595,7 @@ bool SetPlayerTeam(entity player, int destination_team_index, return true; } -bool IsTeamSmallerThanTeam(int team_index_a, int team_index_b, entity player, - bool use_score) -{ - if (!Team_IsValidIndex(team_index_a)) - { - LOG_FATALF("IsTeamSmallerThanTeam: team_index_a is invalid: %f", - team_index_a); - } - if (!Team_IsValidIndex(team_index_b)) - { - LOG_FATALF("IsTeamSmallerThanTeam: team_index_b is invalid: %f", - team_index_b); - } - if (team_index_a == team_index_b) - { - return false; - } - entity team_a = Team_GetTeamFromIndex(team_index_a); - entity team_b = Team_GetTeamFromIndex(team_index_b); - if (!Team_IsAllowed(team_a) || !Team_IsAllowed(team_b)) - { - return false; - } - int num_players_team_a = team_a.m_num_players; - int num_players_team_b = team_b.m_num_players; - if (IS_REAL_CLIENT(player) && bots_would_leave) - { - num_players_team_a -= team_a.m_num_bots; - num_players_team_b -= team_b.m_num_bots; - } - if (!use_score) - { - return num_players_team_a < num_players_team_b; - } - if (num_players_team_a < num_players_team_b) - { - return true; - } - if (num_players_team_a > num_players_team_b) - { - return false; - } - return team_a.m_team_score < team_b.m_team_score; -} - -bool IsTeamEqualToTeam(int team_index_a, int team_index_b, entity player, - bool use_score) -{ - if (!Team_IsValidIndex(team_index_a)) - { - LOG_FATALF("IsTeamEqualToTeam: team_index_a is invalid: %f", - team_index_a); - } - if (!Team_IsValidIndex(team_index_b)) - { - LOG_FATALF("IsTeamEqualToTeam: team_index_b is invalid: %f", - team_index_b); - } - if (team_index_a == team_index_b) - { - return true; - } - entity team_a = Team_GetTeamFromIndex(team_index_a); - entity team_b = Team_GetTeamFromIndex(team_index_b); - if (!Team_IsAllowed(team_a) || !Team_IsAllowed(team_b)) - { - return false; - } - int num_players_team_a = team_a.m_num_players; - int num_players_team_b = team_b.m_num_players; - if (IS_REAL_CLIENT(player) && bots_would_leave) - { - num_players_team_a -= team_a.m_num_bots; - num_players_team_b -= team_b.m_num_bots; - } - if (!use_score) - { - return num_players_team_a == num_players_team_b; - } - if (num_players_team_a != num_players_team_b) - { - return false; - } - return team_a.m_team_score == team_b.m_team_score; -} - -int FindBestTeams(entity player, bool use_score) +int FindBestTeamsForBalance(entity player, bool use_score) { if (MUTATOR_CALLHOOK(FindBestTeams, player) == true) { @@ -748,7 +662,7 @@ int FindBestTeams(entity player, bool use_score) return team_bits; } -int FindBestTeam(entity player, float ignore_player) +int FindBestTeamForBalance(entity player, float ignore_player) { // count how many players are in each team if (ignore_player) @@ -759,7 +673,7 @@ int FindBestTeam(entity player, float ignore_player) { GetTeamCounts(NULL); } - int team_bits = FindBestTeams(player, true); + int team_bits = FindBestTeamsForBalance(player, true); if (team_bits == 0) { LOG_FATALF("FindBestTeam: No teams available for %s\n", @@ -785,7 +699,7 @@ int FindBestTeam(entity player, float ignore_player) return RandomSelection_chosen_float; } -void JoinBestTeam(entity this, bool force_best_team) +void JoinBestTeamForBalance(entity this, bool force_best_team) { // don't join a team if we're not playing a team game if (!teamplay) @@ -823,7 +737,7 @@ void JoinBestTeam(entity this, bool force_best_team) { return; } - int best_team_index = FindBestTeam(this, true); + int best_team_index = FindBestTeamForBalance(this, true); int best_team_num = Team_NumberToTeam(best_team_index); int old_team_index = Team_TeamToNumber(this.team); TeamchangeFrags(this); @@ -836,6 +750,92 @@ void JoinBestTeam(entity this, bool force_best_team) KillPlayerForTeamChange(this); } +bool IsTeamSmallerThanTeam(int team_index_a, int team_index_b, entity player, + bool use_score) +{ + if (!Team_IsValidIndex(team_index_a)) + { + LOG_FATALF("IsTeamSmallerThanTeam: team_index_a is invalid: %f", + team_index_a); + } + if (!Team_IsValidIndex(team_index_b)) + { + LOG_FATALF("IsTeamSmallerThanTeam: team_index_b is invalid: %f", + team_index_b); + } + if (team_index_a == team_index_b) + { + return false; + } + entity team_a = Team_GetTeamFromIndex(team_index_a); + entity team_b = Team_GetTeamFromIndex(team_index_b); + if (!Team_IsAllowed(team_a) || !Team_IsAllowed(team_b)) + { + return false; + } + int num_players_team_a = team_a.m_num_players; + int num_players_team_b = team_b.m_num_players; + if (IS_REAL_CLIENT(player) && bots_would_leave) + { + num_players_team_a -= team_a.m_num_bots; + num_players_team_b -= team_b.m_num_bots; + } + if (!use_score) + { + return num_players_team_a < num_players_team_b; + } + if (num_players_team_a < num_players_team_b) + { + return true; + } + if (num_players_team_a > num_players_team_b) + { + return false; + } + return team_a.m_team_score < team_b.m_team_score; +} + +bool IsTeamEqualToTeam(int team_index_a, int team_index_b, entity player, + bool use_score) +{ + if (!Team_IsValidIndex(team_index_a)) + { + LOG_FATALF("IsTeamEqualToTeam: team_index_a is invalid: %f", + team_index_a); + } + if (!Team_IsValidIndex(team_index_b)) + { + LOG_FATALF("IsTeamEqualToTeam: team_index_b is invalid: %f", + team_index_b); + } + if (team_index_a == team_index_b) + { + return true; + } + entity team_a = Team_GetTeamFromIndex(team_index_a); + entity team_b = Team_GetTeamFromIndex(team_index_b); + if (!Team_IsAllowed(team_a) || !Team_IsAllowed(team_b)) + { + return false; + } + int num_players_team_a = team_a.m_num_players; + int num_players_team_b = team_b.m_num_players; + if (IS_REAL_CLIENT(player) && bots_would_leave) + { + num_players_team_a -= team_a.m_num_bots; + num_players_team_b -= team_b.m_num_bots; + } + if (!use_score) + { + return num_players_team_a == num_players_team_b; + } + if (num_players_team_a != num_players_team_b) + { + return false; + } + return team_a.m_team_score == team_b.m_team_score; +} + void SV_ChangeTeam(entity this, float _color) { int source_color, destination_color; @@ -889,7 +889,7 @@ void SV_ChangeTeam(entity this, float _color) if (autocvar_g_balance_teams && autocvar_g_balance_teams_prevent_imbalance) { GetTeamCounts(this); - if ((BIT(destination_team_index - 1) & FindBestTeams(this, false)) == 0) + if ((BIT(destination_team_index - 1) & FindBestTeamsForBalance(this, false)) == 0) { Send_Notification(NOTIF_ONE, this, MSG_INFO, INFO_TEAMCHANGE_LARGERTEAM); return; diff --git a/qcsrc/server/teamplay.qh b/qcsrc/server/teamplay.qh index 85dca1ac4..71b73b240 100644 --- a/qcsrc/server/teamplay.qh +++ b/qcsrc/server/teamplay.qh @@ -118,6 +118,27 @@ bool SetPlayerTeamSimple(entity player, int team_num); bool SetPlayerTeam(entity player, int destination_team_index, int source_team_index, bool no_print); +/// \brief Returns the bitmask of the teams that will make the game most +/// balanced if the player joins any of them. +/// \param[in] player Player to check. +/// \param[in] use_score Whether to take into account team scores. +/// \return Bitmask of the teams that will make the game most balanced if the +/// player joins any of them. +/// \note You need to call CheckAllowedTeams and GetTeamCounts before calling +/// this function. +int FindBestTeamsForBalance(entity player, bool use_score); + +/// \brief Finds the team that will make the game most balanced if the player +/// joins it. +/// \param[in] player Player to check. +/// \param[in] ignore_player ??? +/// \return Index of the team that will make the game most balanced if the +/// player joins it. If there are several equally good teams available, the +/// function will pick a random one. +int FindBestTeamForBalance(entity player, float ignore_player); + +void JoinBestTeamForBalance(entity this, bool force_best_team); + /// \brief Returns whether one team is smaller than the other. /// \param[in] team_index_a Index of the first team. /// \param[in] team_index_b Index of the second team. @@ -140,23 +161,6 @@ bool IsTeamSmallerThanTeam(int team_index_a, int team_index_b, entity player, bool IsTeamEqualToTeam(int team_index_a, int team_index_b, entity player, bool use_score); -/// \brief Returns the bitmask of the best teams for the player to join. -/// \param[in] player Player to check. -/// \param[in] use_score Whether to take into account team scores. -/// \return Bitmask of the best teams for the player to join. -/// \note You need to call CheckAllowedTeams and GetTeamCounts before calling -/// this function. -int FindBestTeams(entity player, bool use_score); - -/// \brief Finds the best team for the player. -/// \param[in] player Player to check. -/// \param[in] ignore_player ??? -/// \return Index of the best team for the player. If there are several equally -/// good teams available, the function will pick a random one. -int FindBestTeam(entity player, float ignore_player); - -void JoinBestTeam(entity this, bool force_best_team); - /// \brief Auto balances bots in teams after the player has changed team. /// \param[in] source_team_index Previous index of the team of the player. /// \param[in] destination_team_index Current index of the team of the player. -- 2.39.2