]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Simplify and share some duplicated code
authorterencehill <piuntn@gmail.com>
Fri, 31 Dec 2021 00:21:11 +0000 (01:21 +0100)
committerterencehill <piuntn@gmail.com>
Fri, 31 Dec 2021 00:21:11 +0000 (01:21 +0100)
qcsrc/common/gamemodes/gamemode/clanarena/sv_clanarena.qc
qcsrc/common/gamemodes/gamemode/domination/sv_domination.qc
qcsrc/common/gamemodes/gamemode/freezetag/sv_freezetag.qc
qcsrc/common/gamemodes/gamemode/onslaught/sv_onslaught.qc
qcsrc/server/teamplay.qc
qcsrc/server/teamplay.qh

index d40c04c0b658a63087c801a92d607bf20dd9493d..3d0c292617f9596d0c0ce3c68d9cf374e23de8e5 100644 (file)
@@ -33,42 +33,13 @@ void CA_count_alive_players()
        });
        FOREACH_CLIENT(IS_REAL_CLIENT(it),
        {
-               STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(
-                       1));
-               STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(
-                       Team_GetTeamFromIndex(2));
-               STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(
-                       Team_GetTeamFromIndex(3));
-               STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(
-                       Team_GetTeamFromIndex(4));
+               STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1));
+               STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(2));
+               STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(3));
+               STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(4));
        });
 }
 
-int CA_GetWinnerTeam()
-{
-       int winner_team = 0;
-       if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1)) >= 1)
-       {
-               winner_team = NUM_TEAM_1;
-       }
-       for (int i = 2; i <= NUM_TEAMS; ++i)
-       {
-               if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) >= 1)
-               {
-                       if (winner_team != 0)
-                       {
-                               return 0;
-                       }
-                       winner_team = Team_IndexToTeam(i);
-               }
-       }
-       if (winner_team)
-       {
-               return winner_team;
-       }
-       return -1; // no player left
-}
-
 void nades_Clear(entity player);
 
 float CA_CheckWinner()
@@ -86,12 +57,10 @@ float CA_CheckWinner()
        }
 
        CA_count_alive_players();
-       if (Team_GetNumberOfAliveTeams() > 1)
-       {
+       int winner_team = Team_GetWinnerAliveTeam();
+       if (!winner_team)
                return 0;
-       }
 
-       int winner_team = CA_GetWinnerTeam();
        if(winner_team > 0)
        {
                Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
index ca782cedbb87f04f3ee45fa1f6ad5eddb7519b80..ac8e48856ec58c3524994e430e09e6955e56effb 100644 (file)
@@ -327,33 +327,6 @@ void Domination_count_controlpoints()
        });
 }
 
-int Domination_GetWinnerTeam()
-{
-       int winner_team = 0;
-       if (Team_GetNumberOfControlPoints(Team_GetTeamFromIndex(1)) ==
-               total_control_points)
-       {
-               winner_team = NUM_TEAM_1;
-       }
-       for (int i = 2; i <= NUM_TEAMS; ++i)
-       {
-               if (Team_GetNumberOfControlPoints(Team_GetTeamFromIndex(i)) ==
-                       total_control_points)
-               {
-                       if (winner_team != 0)
-                       {
-                               return 0;
-                       }
-                       winner_team = Team_IndexToTeam(i);
-               }
-       }
-       if (winner_team)
-       {
-               return winner_team;
-       }
-       return -1; // no control points left?
-}
-
 bool Domination_CheckWinner()
 {
        if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0)
@@ -367,11 +340,9 @@ bool Domination_CheckWinner()
        }
 
        Domination_count_controlpoints();
-
-       float winner_team = Domination_GetWinnerTeam();
-
-       if(winner_team == -1)
-               return false;
+       int winner_team = Team_GetWinnerTeam_WIthControlPoints(total_control_points);
+       if (winner_team == -1)
+               return 0;
 
        if(winner_team > 0)
        {
@@ -379,11 +350,6 @@ bool Domination_CheckWinner()
                Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(winner_team, INFO_ROUND_TEAM_WIN));
                TeamScore_AddToTeam(winner_team, ST_DOM_CAPS, +1);
        }
-       else if(winner_team == -1)
-       {
-               Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_ROUND_TIED);
-               Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_ROUND_TIED);
-       }
 
        game_stopped = true;
        round_handler_Init(5, autocvar_g_domination_warmup, autocvar_g_domination_round_timelimit);
index 5712c82c980cab2cfa9345f72d284da0301fef6e..58fd90c897f15213ce6b686e9a5b402df00c19d6 100644 (file)
@@ -40,14 +40,10 @@ void freezetag_count_alive_players()
        });
        FOREACH_CLIENT(IS_REAL_CLIENT(it),
        {
-               STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(
-                       1));
-               STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(
-                       Team_GetTeamFromIndex(2));
-               STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(
-                       Team_GetTeamFromIndex(3));
-               STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(
-                       Team_GetTeamFromIndex(4));
+               STAT(REDALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1));
+               STAT(BLUEALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(2));
+               STAT(YELLOWALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(3));
+               STAT(PINKALIVE, it) = Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(4));
        });
 
        eliminatedPlayers.SendFlags |= 1;
@@ -87,31 +83,6 @@ bool freezetag_CheckTeams()
        return false;
 }
 
-int freezetag_getWinnerTeam()
-{
-       int winner_team = 0;
-       if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1)) >= 1)
-       {
-               winner_team = NUM_TEAM_1;
-       }
-       for (int i = 2; i <= NUM_TEAMS; ++i)
-       {
-               if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) >= 1)
-               {
-                       if (winner_team != 0)
-                       {
-                               return 0;
-                       }
-                       winner_team = Team_IndexToTeam(i);
-               }
-       }
-       if (winner_team)
-       {
-               return winner_team;
-       }
-       return -1; // no player left
-}
-
 void nades_Clear(entity);
 void nades_GiveBonus(entity player, float score);
 
@@ -131,12 +102,10 @@ bool freezetag_CheckWinner()
                return true;
        }
 
-       if (Team_GetNumberOfAliveTeams() > 1)
-       {
+       int winner_team = Team_GetWinnerAliveTeam();
+       if (!winner_team)
                return false;
-       }
 
-       int winner_team = freezetag_getWinnerTeam();
        if(winner_team > 0)
        {
                Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, APP_TEAM_NUM(winner_team, CENTER_ROUND_TEAM_WIN));
index db39a8a39513a3d80c97816ad3a2bf08d3c1cb94..58fc8a3662888e664b22dd63591654bd73bb352a 100644 (file)
@@ -1142,31 +1142,6 @@ void Onslaught_count_generators()
        }
 }
 
-int Onslaught_GetWinnerTeam()
-{
-       int winner_team = 0;
-       if (Team_GetNumberOfControlPoints(Team_GetTeamFromIndex(1)) >= 1)
-       {
-               winner_team = NUM_TEAM_1;
-       }
-       for (int i = 2; i <= NUM_TEAMS; ++i)
-       {
-               if (Team_GetNumberOfControlPoints(Team_GetTeamFromIndex(i)) >= 1)
-               {
-                       if (winner_team != 0)
-                       {
-                               return 0;
-                       }
-                       winner_team = Team_IndexToTeam(i);
-               }
-       }
-       if (winner_team)
-       {
-               return winner_team;
-       }
-       return -1; // no generators left?
-}
-
 void nades_Clear(entity e);
 
 bool Onslaught_CheckWinner()
@@ -1213,13 +1188,9 @@ bool Onslaught_CheckWinner()
        else { wpforenemy_announced = false; ons_stalemate = false; }
 
        Onslaught_count_generators();
-
-       if (Team_GetNumberOfTeamsWithControlPoints() > 1)
-       {
+       int winner_team = Team_GetWinnerTeam_WIthControlPoints(1); // actually generators
+       if (!winner_team)
                return 0;
-       }
-
-       int winner_team = Onslaught_GetWinnerTeam();
 
        if(winner_team > 0)
        {
index c26a0b16550ac51062a40c742468d5f93f214076..b1edf75e40de5831c9dcb3fdeccba5079447bb71 100644 (file)
@@ -92,6 +92,21 @@ void Team_SetNumberOfAlivePlayers(entity team_ent, int number)
        team_ent.m_num_players_alive = number;
 }
 
+int Team_GetWinnerAliveTeam()
+{
+       int winner = 0;
+       for (int i = 0; i < NUM_TEAMS; ++i)
+       {
+               if (g_team_entities[i].m_num_players_alive > 0)
+               {
+                       if (winner)
+                               return 0;
+                       winner = Team_IndexToTeam(i + 1);
+               }
+       }
+       return (winner ? winner : -1);
+}
+
 int Team_GetNumberOfAliveTeams()
 {
        int result = 0;
@@ -105,6 +120,21 @@ int Team_GetNumberOfAliveTeams()
        return result;
 }
 
+int Team_GetWinnerTeam_WIthControlPoints(int min_control_points)
+{
+       int winner = 0;
+       for (int i = 0; i < NUM_TEAMS; ++i)
+       {
+               if (g_team_entities[i].m_num_control_points >= min_control_points)
+               {
+                       if (winner)
+                               return 0;
+                       winner = Team_IndexToTeam(i + 1);
+               }
+       }
+       return (winner ? winner : -1);
+}
+
 int Team_GetNumberOfControlPoints(entity team_ent)
 {
        return team_ent.m_num_control_points;
index 06787c6ffa06c72ec593b4429560949c99e74996..c7b2ac8d09988c1d140979a17bdae24d60c38d99 100644 (file)
@@ -51,10 +51,19 @@ int Team_GetNumberOfAlivePlayers(entity team_ent);
 /// \param[in] number Number of players to set.
 void Team_SetNumberOfAlivePlayers(entity team_ent, int number);
 
+/// \brief Returns the winner team.
+/// \return Winner team or 0 if 2 or more teams have alive players or -1 if no team has any alive players.
+int Team_GetWinnerAliveTeam();
+
 /// \brief Returns the number of alive teams.
 /// \return Number of alive teams.
 int Team_GetNumberOfAliveTeams();
 
+/// \brief Returns the winner team.
+/// \param[in] min_control_points Minimum number of control points the winner team must have.
+/// \return Winner team or 0 if 2 or more teams have control points or -1 if no team has any control points.
+int Team_GetWinnerTeam_WIthControlPoints(int min_control_points);
+
 /// \brief Returns the number of control points owned by a team.
 /// \param[in] team_ent Team entity.
 /// \return Number of control points owned by a team.