From eb13e9f317dc515b6b5a579010f2952e7261d82d Mon Sep 17 00:00:00 2001 From: Lyberta Date: Mon, 30 Jul 2018 00:05:22 +0300 Subject: [PATCH] Teamplay: Second pass of autobalance. --- qcsrc/server/teamplay.qc | 82 ++++++++++++++++++++-------------------- qcsrc/server/teamplay.qh | 10 +++++ 2 files changed, 52 insertions(+), 40 deletions(-) diff --git a/qcsrc/server/teamplay.qc b/qcsrc/server/teamplay.qc index 357685887..cfd15f248 100644 --- a/qcsrc/server/teamplay.qc +++ b/qcsrc/server/teamplay.qc @@ -869,6 +869,7 @@ void TeamBalance_AutoBalanceBots() largest_team_player_count = player_count; } } + TeamBalance_Destroy(balance); //PrintToChatAll(sprintf("Smallest team: %f", smallest_team_index)); //PrintToChatAll(sprintf("Largest team: %f", largest_team_index)); //PrintToChatAll(sprintf("Smallest team players: %f", smallest_team_player_count)); @@ -881,46 +882,8 @@ void TeamBalance_AutoBalanceBots() { return; } - entity source_team = TeamBalance_GetTeamFromIndex(balance, - largest_team_index); - if (source_team.m_num_bots == 0) - { - TeamBalance_Destroy(balance); - return; - } - TeamBalance_Destroy(balance); - entity lowest_bot = NULL; - if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, largest_team_index, - smallest_team_index, true)) - { - lowest_bot = M_ARGV(3, entity); - } - else - { - float lowest_score = FLOAT_MAX; - FOREACH_CLIENT(IS_BOT_CLIENT(it) && (Entity_GetTeamIndex(it) == - largest_team_index), - { - float temp_score = PlayerScore_Get(it, SP_SCORE); - if (temp_score >= lowest_score) - { - continue; - } - //PrintToChatAll(sprintf("Found %s with lowest score, checking allowed teams", it.netname)); - balance = TeamBalance_CheckAllowedTeams(it); - if (TeamBalance_IsTeamAllowed(balance, smallest_team_index)) - { - //PrintToChatAll("Allowed"); - lowest_bot = it; - lowest_score = temp_score; - } - else - { - //PrintToChatAll("Not allowed"); - } - TeamBalance_Destroy(balance); - }); - } + entity lowest_bot = TeamBalance_GetPlayerForTeamSwitch(largest_team_index, + smallest_team_index, true); if (lowest_bot == NULL) { //PrintToChatAll("No bot found"); @@ -933,6 +896,45 @@ void TeamBalance_AutoBalanceBots() KillPlayerForTeamChange(lowest_bot); } +entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index, + int destination_team_index, bool is_bot) +{ + if (MUTATOR_CALLHOOK(TeamBalance_GetPlayerForTeamSwitch, source_team_index, + destination_team_index, is_bot)) + { + return M_ARGV(3, entity); + } + entity lowest_player = NULL; + float lowest_score = FLOAT_MAX; + FOREACH_CLIENT(Entity_GetTeamIndex(it) == source_team_index, + { + if (IS_BOT_CLIENT(it) != is_bot) + { + continue; + } + float temp_score = PlayerScore_Get(it, SP_SCORE); + if (temp_score >= lowest_score) + { + continue; + } + //PrintToChatAll(sprintf( + // "Found %s with lowest score, checking allowed teams", it.netname)); + entity balance = TeamBalance_CheckAllowedTeams(it); + if (TeamBalance_IsTeamAllowed(balance, source_team_index)) + { + //PrintToChatAll("Allowed"); + lowest_player = it; + lowest_score = temp_score; + } + else + { + //PrintToChatAll("Not allowed"); + } + TeamBalance_Destroy(balance); + }); + return lowest_player; +} + bool TeamBalance_IsTeamAllowedInternal(entity balance, int index) { return balance.m_team_balance_team[index - 1].m_num_players != diff --git a/qcsrc/server/teamplay.qh b/qcsrc/server/teamplay.qh index 05578be7a..404d728a2 100644 --- a/qcsrc/server/teamplay.qh +++ b/qcsrc/server/teamplay.qh @@ -226,6 +226,16 @@ int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b, /// \brief Switches a bot from one team to another if teams are not balanced. void TeamBalance_AutoBalanceBots(); +/// \brief Returns the player who is the most suitable for switching between +/// the given teams. +/// \param[in] source_team_index Index of the team to search in. +/// \param[in] destination_team_index Index of the team to switch to. +/// \param[in] is_bot True to search for bot, false for human. +/// \return Player who is the most suitable for switching between the given +/// teams or NULL if not found. +entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index, + int destination_team_index, bool is_bot); + // ============================ Internal API ================================== /// \brief Returns whether the team change to the specified team is allowed. -- 2.39.2