From df74e5b1580fe375617b0340d2e71b017a8999c1 Mon Sep 17 00:00:00 2001 From: Lyberta Date: Sun, 11 Mar 2018 12:43:09 +0300 Subject: [PATCH] Removed global alive variables. --- qcsrc/server/mutators/gamemode.qh | 1 - qcsrc/server/mutators/mutator/gamemode_ca.qc | 89 ++++++++++-------- .../mutators/mutator/gamemode_freezetag.qc | 91 +++++++++++-------- qcsrc/server/teamplay.qc | 39 ++++++++ qcsrc/server/teamplay.qh | 26 +++++- 5 files changed, 168 insertions(+), 78 deletions(-) diff --git a/qcsrc/server/mutators/gamemode.qh b/qcsrc/server/mutators/gamemode.qh index b0f42f59e..f0a2497ec 100644 --- a/qcsrc/server/mutators/gamemode.qh +++ b/qcsrc/server/mutators/gamemode.qh @@ -105,4 +105,3 @@ .float lastground; float total_players; -float redalive, bluealive, yellowalive, pinkalive; diff --git a/qcsrc/server/mutators/mutator/gamemode_ca.qc b/qcsrc/server/mutators/mutator/gamemode_ca.qc index 077e277a6..a35962e4e 100644 --- a/qcsrc/server/mutators/mutator/gamemode_ca.qc +++ b/qcsrc/server/mutators/mutator/gamemode_ca.qc @@ -5,53 +5,64 @@ bool autocvar_g_ca_spectate_enemies; void CA_count_alive_players() { - total_players = redalive = bluealive = yellowalive = pinkalive = 0; - FOREACH_CLIENT(IS_PLAYER(it), { - switch(it.team) + total_players = 0; + for (int i = 1; i <= NUM_TEAMS; ++i) + { + Team_SetNumberOfAlivePlayers(Team_GetTeamFromIndex(i), 0); + } + FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it), + { + ++total_players; + if (IS_DEAD(it)) { - case NUM_TEAM_1: ++total_players; if(!IS_DEAD(it)) ++redalive; break; - case NUM_TEAM_2: ++total_players; if(!IS_DEAD(it)) ++bluealive; break; - case NUM_TEAM_3: ++total_players; if(!IS_DEAD(it)) ++yellowalive; break; - case NUM_TEAM_4: ++total_players; if(!IS_DEAD(it)) ++pinkalive; break; + continue; } + entity team_ = Entity_GetTeam(it); + int num_alive = Team_GetNumberOfAlivePlayers(team_); + ++num_alive; + Team_SetNumberOfAlivePlayers(team_, num_alive); }); - FOREACH_CLIENT(IS_REAL_CLIENT(it), { - STAT(REDALIVE, it) = redalive; - STAT(BLUEALIVE, it) = bluealive; - STAT(YELLOWALIVE, it) = yellowalive; - STAT(PINKALIVE, it) = pinkalive; + 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)); }); } -float CA_GetWinnerTeam() +int CA_GetWinnerTeam() { - float winner_team = 0; - if(redalive >= 1) - winner_team = NUM_TEAM_1; - if(bluealive >= 1) + int winner_team = 0; + if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1)) >= 1) { - if(winner_team) return 0; - winner_team = NUM_TEAM_2; + winner_team = NUM_TEAM_1; } - if(yellowalive >= 1) + for (int i = 2; i <= NUM_TEAMS; ++i) { - if(winner_team) return 0; - winner_team = NUM_TEAM_3; + if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) >= 1) + { + if (winner_team != 0) + { + return 0; + } + winner_team = Team_IndexToTeam(i); + } } - if(pinkalive >= 1) + if (winner_team) { - if(winner_team) return 0; - winner_team = NUM_TEAM_4; - } - if(winner_team) return winner_team; + } return -1; // no player left } void nades_Clear(entity player); -#define CA_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0)) -#define CA_ALIVE_TEAMS_OK() (CA_ALIVE_TEAMS() == NumTeams(ca_teams)) +#define CA_ALIVE_TEAMS_OK() (Team_GetNumberOfAliveTeams() == NumTeams(ca_teams)) float CA_CheckWinner() { if(round_handler_GetEndTime() > 0 && round_handler_GetEndTime() - time <= 0) @@ -67,8 +78,10 @@ float CA_CheckWinner() } CA_count_alive_players(); - if(CA_ALIVE_TEAMS() > 1) + if (Team_GetNumberOfAliveTeams() > 1) + { return 0; + } int winner_team = CA_GetWinnerTeam(); if(winner_team > 0) @@ -117,14 +130,14 @@ bool CA_CheckTeams() return false; } int missing_teams_mask = 0; - if(ca_teams & BIT(0)) - missing_teams_mask += (!redalive) * 1; - if(ca_teams & BIT(1)) - missing_teams_mask += (!bluealive) * 2; - if(ca_teams & BIT(2)) - missing_teams_mask += (!yellowalive) * 4; - if(ca_teams & BIT(3)) - missing_teams_mask += (!pinkalive) * 8; + for (int i = 1; i <= NUM_TEAMS; ++i) + { + if ((ca_teams & Team_IndexToBit(i)) && + (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0)) + { + missing_teams_mask |= Team_IndexToBit(i); + } + } if(prev_missing_teams_mask != missing_teams_mask) { Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask); diff --git a/qcsrc/server/mutators/mutator/gamemode_freezetag.qc b/qcsrc/server/mutators/mutator/gamemode_freezetag.qc index 0ba3d04f1..049bf6375 100644 --- a/qcsrc/server/mutators/mutator/gamemode_freezetag.qc +++ b/qcsrc/server/mutators/mutator/gamemode_freezetag.qc @@ -9,27 +9,40 @@ float autocvar_g_freezetag_warmup; void freezetag_count_alive_players() { - total_players = redalive = bluealive = yellowalive = pinkalive = 0; - FOREACH_CLIENT(IS_PLAYER(it), { - switch(it.team) + total_players = 0; + for (int i = 1; i <= NUM_TEAMS; ++i) + { + Team_SetNumberOfAlivePlayers(Team_GetTeamFromIndex(i), 0); + } + FOREACH_CLIENT(IS_PLAYER(it) && Entity_HasValidTeam(it), + { + ++total_players; + if ((GetResourceAmount(it, RESOURCE_HEALTH) < 1) || + (STAT(FROZEN, it) == 1)) { - case NUM_TEAM_1: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++redalive; break; - case NUM_TEAM_2: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++bluealive; break; - case NUM_TEAM_3: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++yellowalive; break; - case NUM_TEAM_4: ++total_players; if(it.health >= 1 && STAT(FROZEN, it) != 1) ++pinkalive; break; + continue; } + entity team_ = Entity_GetTeam(it); + int num_alive = Team_GetNumberOfAlivePlayers(team_); + ++num_alive; + Team_SetNumberOfAlivePlayers(team_, num_alive); }); - FOREACH_CLIENT(IS_REAL_CLIENT(it), { - STAT(REDALIVE, it) = redalive; - STAT(BLUEALIVE, it) = bluealive; - STAT(YELLOWALIVE, it) = yellowalive; - STAT(PINKALIVE, it) = pinkalive; + 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)); }); eliminatedPlayers.SendFlags |= 1; } -#define FREEZETAG_ALIVE_TEAMS() ((redalive > 0) + (bluealive > 0) + (yellowalive > 0) + (pinkalive > 0)) -#define FREEZETAG_ALIVE_TEAMS_OK() (FREEZETAG_ALIVE_TEAMS() == NumTeams(freezetag_teams)) + +#define FREEZETAG_ALIVE_TEAMS_OK() (Team_GetNumberOfAliveTeams() == NumTeams(freezetag_teams)) float freezetag_CheckTeams() { @@ -49,14 +62,14 @@ float freezetag_CheckTeams() return 0; } int missing_teams_mask = 0; - if(freezetag_teams & BIT(0)) - missing_teams_mask += (!redalive) * 1; - if(freezetag_teams & BIT(1)) - missing_teams_mask += (!bluealive) * 2; - if(freezetag_teams & BIT(2)) - missing_teams_mask += (!yellowalive) * 4; - if(freezetag_teams & BIT(3)) - missing_teams_mask += (!pinkalive) * 8; + for (int i = 1; i <= NUM_TEAMS; ++i) + { + if ((freezetag_teams & Team_IndexToBit(i)) && + (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) == 0)) + { + missing_teams_mask |= Team_IndexToBit(i); + } + } if(prev_missing_teams_mask != missing_teams_mask) { Send_Notification(NOTIF_ALL, NULL, MSG_CENTER, CENTER_MISSING_TEAMS, missing_teams_mask); @@ -65,28 +78,28 @@ float freezetag_CheckTeams() return 0; } -float freezetag_getWinnerTeam() +int freezetag_getWinnerTeam() { - float winner_team = 0; - if(redalive >= 1) - winner_team = NUM_TEAM_1; - if(bluealive >= 1) + int winner_team = 0; + if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(1)) >= 1) { - if(winner_team) return 0; - winner_team = NUM_TEAM_2; + winner_team = NUM_TEAM_1; } - if(yellowalive >= 1) + for (int i = 2; i <= NUM_TEAMS; ++i) { - if(winner_team) return 0; - winner_team = NUM_TEAM_3; + if (Team_GetNumberOfAlivePlayers(Team_GetTeamFromIndex(i)) >= 1) + { + if (winner_team != 0) + { + return 0; + } + winner_team = Team_IndexToTeam(i); + } } - if(pinkalive >= 1) + if (winner_team) { - if(winner_team) return 0; - winner_team = NUM_TEAM_4; - } - if(winner_team) return winner_team; + } return -1; // no player left } @@ -108,8 +121,10 @@ float freezetag_CheckWinner() return 1; } - if(FREEZETAG_ALIVE_TEAMS() > 1) + if (Team_GetNumberOfAliveTeams() > 1) + { return 0; + } int winner_team = freezetag_getWinnerTeam(); if(winner_team > 0) diff --git a/qcsrc/server/teamplay.qc b/qcsrc/server/teamplay.qc index e2b16cab9..325252bcd 100644 --- a/qcsrc/server/teamplay.qc +++ b/qcsrc/server/teamplay.qc @@ -34,6 +34,7 @@ const int TEAM_NOT_ALLOWED = -1; .float m_team_score; ///< The score of the team. .int m_num_players; ///< Number of players (both humans and bots) in a team. .int m_num_bots; ///< Number of bots in a team. +.int m_num_players_alive; ///< Number of alive players in a team. .entity m_lowest_human; ///< Human with the lowest score in a team. .entity m_lowest_bot; ///< Bot with the lowest score in a team. @@ -75,6 +76,29 @@ void Team_SetTeamScore(entity team_, float score) team_.m_team_score = score; } +int Team_GetNumberOfAlivePlayers(entity team_) +{ + return team_.m_num_players_alive; +} + +void Team_SetNumberOfAlivePlayers(entity team_, int number) +{ + team_.m_num_players_alive = number; +} + +int Team_GetNumberOfAliveTeams() +{ + int result = 0; + for (int i = 0; i < NUM_TEAMS; ++i) + { + if (g_team_entities[i].m_num_players_alive > 0) + { + ++result; + } + } + return result; +} + void TeamchangeFrags(entity e) { PlayerScore_Clear(e); @@ -226,11 +250,26 @@ void setcolor(entity this, int clr) #endif } +bool Entity_HasValidTeam(entity this) +{ + return Team_IsValidTeam(this.team); +} + int Entity_GetTeamIndex(entity this) { return Team_TeamToIndex(this.team); } +entity Entity_GetTeam(entity this) +{ + int index = Entity_GetTeamIndex(this); + if (!Team_IsValidIndex(index)) + { + return NULL; + } + return Team_GetTeamFromIndex(index); +} + void SetPlayerColors(entity player, float _color) { float pants = _color & 0x0F; diff --git a/qcsrc/server/teamplay.qh b/qcsrc/server/teamplay.qh index 5d2c75dc7..a12e26b6f 100644 --- a/qcsrc/server/teamplay.qh +++ b/qcsrc/server/teamplay.qh @@ -24,6 +24,20 @@ float Team_GetTeamScore(entity team_); /// \param[in] score Score to set. void Team_SetTeamScore(entity team_, float score); +/// \brief Returns the number of alive players in a team. +/// \param[in] team_ Team entity. +/// \return Number of alive players in a team. +int Team_GetNumberOfAlivePlayers(entity team_); + +/// \brief Sets the number of alive players in a team. +/// \param[in,out] team_ Team entity. +/// \param[in] number Number of players to set. +void Team_SetNumberOfAlivePlayers(entity team_, int number); + +/// \brief Returns the number of alive teams. +/// \return Number of alive teams. +int Team_GetNumberOfAliveTeams(); + int redowned, blueowned, yellowowned, pinkowned; void TeamchangeFrags(entity e); @@ -40,11 +54,21 @@ string getwelcomemessage(entity this); void setcolor(entity this, int clr); -/// \brief Returns the team index of the entity. +/// \brief Returns whether the given entity belongs to a valid team. +/// \param[in] this Entity to check. +/// \return True if entity belongs to a valid team, false otherwise. +bool Entity_HasValidTeam(entity this); + +/// \brief Returns the team index of the given entity. /// \param[in] this Entity to check. /// \return Team index of the entity. int Entity_GetTeamIndex(entity this); +/// \brief Returns the team entity of the given entity. +/// \param[in] this Entity to check. +/// \return Team entity of the given entity. +entity Entity_GetTeam(entity this); + void SetPlayerColors(entity player, float _color); /// \brief Sets the team of the player using its index. -- 2.39.2