]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Teamplay: Second pass of autobalance.
authorLyberta <lyberta@lyberta.net>
Sun, 29 Jul 2018 21:05:22 +0000 (00:05 +0300)
committerLyberta <lyberta@lyberta.net>
Sun, 29 Jul 2018 21:05:22 +0000 (00:05 +0300)
qcsrc/server/teamplay.qc
qcsrc/server/teamplay.qh

index 357685887e14dc236e9a6e46dd6bbd1e39fa1ea7..cfd15f24807476ccdcede9326b98930f7733b13a 100644 (file)
@@ -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 !=
index 05578be7aaf0ad7efd4dc8c5805217b52d496c1a..404d728a250f8393dfe769534afaf53da13b5355 100644 (file)
@@ -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.