From: Lyberta Date: Sun, 29 Jul 2018 21:05:22 +0000 (+0300) Subject: Teamplay: Second pass of autobalance. X-Git-Tag: xonotic-v0.8.5~1899^2~11 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=eb13e9f317dc515b6b5a579010f2952e7261d82d;p=xonotic%2Fxonotic-data.pk3dir.git Teamplay: Second pass of autobalance. --- 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.