From: terencehill Date: Fri, 31 Dec 2021 00:21:11 +0000 (+0100) Subject: Simplify and share some duplicated code X-Git-Tag: xonotic-v0.8.5~257 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=5237b383b83b86b0e5044ff910e752116bb52498;p=xonotic%2Fxonotic-data.pk3dir.git Simplify and share some duplicated code --- diff --git a/qcsrc/common/gamemodes/gamemode/clanarena/sv_clanarena.qc b/qcsrc/common/gamemodes/gamemode/clanarena/sv_clanarena.qc index d40c04c0b..3d0c29261 100644 --- a/qcsrc/common/gamemodes/gamemode/clanarena/sv_clanarena.qc +++ b/qcsrc/common/gamemodes/gamemode/clanarena/sv_clanarena.qc @@ -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)); diff --git a/qcsrc/common/gamemodes/gamemode/domination/sv_domination.qc b/qcsrc/common/gamemodes/gamemode/domination/sv_domination.qc index ca782cedb..ac8e48856 100644 --- a/qcsrc/common/gamemodes/gamemode/domination/sv_domination.qc +++ b/qcsrc/common/gamemodes/gamemode/domination/sv_domination.qc @@ -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); diff --git a/qcsrc/common/gamemodes/gamemode/freezetag/sv_freezetag.qc b/qcsrc/common/gamemodes/gamemode/freezetag/sv_freezetag.qc index 5712c82c9..58fd90c89 100644 --- a/qcsrc/common/gamemodes/gamemode/freezetag/sv_freezetag.qc +++ b/qcsrc/common/gamemodes/gamemode/freezetag/sv_freezetag.qc @@ -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)); diff --git a/qcsrc/common/gamemodes/gamemode/onslaught/sv_onslaught.qc b/qcsrc/common/gamemodes/gamemode/onslaught/sv_onslaught.qc index db39a8a39..58fc8a366 100644 --- a/qcsrc/common/gamemodes/gamemode/onslaught/sv_onslaught.qc +++ b/qcsrc/common/gamemodes/gamemode/onslaught/sv_onslaught.qc @@ -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) { diff --git a/qcsrc/server/teamplay.qc b/qcsrc/server/teamplay.qc index c26a0b165..b1edf75e4 100644 --- a/qcsrc/server/teamplay.qc +++ b/qcsrc/server/teamplay.qc @@ -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; diff --git a/qcsrc/server/teamplay.qh b/qcsrc/server/teamplay.qh index 06787c6ff..c7b2ac8d0 100644 --- a/qcsrc/server/teamplay.qh +++ b/qcsrc/server/teamplay.qh @@ -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.