]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/server/teamplay.qc
Teamplay: Third pass at autobalance.
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / teamplay.qc
index cfd15f24807476ccdcede9326b98930f7733b13a..4a4305bac4ec9559f92a302efc0465ebdac440be 100644 (file)
@@ -838,8 +838,6 @@ void TeamBalance_AutoBalanceBots()
        TeamBalance_GetTeamCounts(balance, NULL);
        int smallest_team_index = 0;
        int smallest_team_player_count = 0;
        TeamBalance_GetTeamCounts(balance, NULL);
        int smallest_team_index = 0;
        int smallest_team_player_count = 0;
-       int largest_team_index = 0;
-       int largest_team_player_count = 0;
        for (int i = 1; i <= NUM_TEAMS; ++i)
        {
                entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
        for (int i = 1; i <= NUM_TEAMS; ++i)
        {
                entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
@@ -858,35 +856,43 @@ void TeamBalance_AutoBalanceBots()
                        smallest_team_index = i;
                        smallest_team_player_count = player_count;
                }
                        smallest_team_index = i;
                        smallest_team_player_count = player_count;
                }
-               if (largest_team_index == 0)
+       }
+       //PrintToChatAll(sprintf("Smallest team: %f", smallest_team_index));
+       //PrintToChatAll(sprintf("Smallest team players: %f", smallest_team_player_count));
+       entity lowest_bot = NULL;
+       int teams = BITS(NUM_TEAMS);
+       while (teams != 0)
+       {
+               int largest_team_index = TeamBalance_GetLargestTeamIndex(balance,
+                       teams);
+               if (smallest_team_index == largest_team_index)
                {
                {
-                       largest_team_index = i;
-                       largest_team_player_count = player_count;
+                       TeamBalance_Destroy(balance);
+                       return;
                }
                }
-               else if (player_count > largest_team_player_count)
+               entity largest_team = TeamBalance_GetTeamFromIndex(balance,
+                       largest_team_index);
+               int largest_team_player_count = TeamBalanceTeam_GetNumberOfPlayers(
+                       largest_team);
+               if (largest_team_player_count - smallest_team_player_count < 2)
                {
                {
-                       largest_team_index = i;
-                       largest_team_player_count = player_count;
+                       TeamBalance_Destroy(balance);
+                       return;
                }
                }
+               //PrintToChatAll(sprintf("Largest team: %f", largest_team_index));
+               //PrintToChatAll(sprintf("Largest team players: %f", largest_team_player_count));
+               lowest_bot = TeamBalance_GetPlayerForTeamSwitch(largest_team_index,
+                       smallest_team_index, true);
+               if (lowest_bot != NULL)
+               {
+                       break;
+               }
+               teams &= ~Team_IndexToBit(largest_team_index);
        }
        TeamBalance_Destroy(balance);
        }
        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));
-       //PrintToChatAll(sprintf("Largest team players: %f", largest_team_player_count));
-       if (smallest_team_index == largest_team_index)
-       {
-               return;
-       }
-       if (largest_team_player_count - smallest_team_player_count < 2)
-       {
-               return;
-       }
-       entity lowest_bot = TeamBalance_GetPlayerForTeamSwitch(largest_team_index,
-               smallest_team_index, true);
        if (lowest_bot == NULL)
        {
        if (lowest_bot == NULL)
        {
-               //PrintToChatAll("No bot found");
+               //PrintToChatAll("No bot found after searching through all the teams");
                return;
        }
        if (!Player_SetTeamIndex(lowest_bot, smallest_team_index))
                return;
        }
        if (!Player_SetTeamIndex(lowest_bot, smallest_team_index))
@@ -896,6 +902,36 @@ void TeamBalance_AutoBalanceBots()
        KillPlayerForTeamChange(lowest_bot);
 }
 
        KillPlayerForTeamChange(lowest_bot);
 }
 
+int TeamBalance_GetLargestTeamIndex(entity balance, int teams)
+{
+       int largest_team_index = 0;
+       int largest_team_player_count = 0;
+       for (int i = 1; i <= NUM_TEAMS; ++i)
+       {
+               if (!(Team_IndexToBit(i) & teams))
+               {
+                       continue;
+               }
+               entity team_ = TeamBalance_GetTeamFromIndex(balance, i);
+               if (!TeamBalanceTeam_IsAllowed(team_))
+               {
+                       continue;
+               }
+               int player_count = TeamBalanceTeam_GetNumberOfPlayers(team_);
+               if (largest_team_index == 0)
+               {
+                       largest_team_index = i;
+                       largest_team_player_count = player_count;
+               }
+               else if (player_count > largest_team_player_count)
+               {
+                       largest_team_index = i;
+                       largest_team_player_count = player_count;
+               }
+       }
+       return largest_team_index;
+}
+
 entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index,
        int destination_team_index, bool is_bot)
 {
 entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index,
        int destination_team_index, bool is_bot)
 {