From f3c106c6c892b55eaf4c8387437616be63139cb2 Mon Sep 17 00:00:00 2001 From: Lyberta Date: Tue, 31 Jul 2018 00:21:44 +0300 Subject: [PATCH] Teamplay: Polish. --- qcsrc/server/client.qc | 1 - qcsrc/server/teamplay.qc | 153 ++++++++++++++++++--------------------- qcsrc/server/teamplay.qh | 35 +++++---- 3 files changed, 89 insertions(+), 100 deletions(-) diff --git a/qcsrc/server/client.qc b/qcsrc/server/client.qc index e1b883b3b..a43534dc7 100644 --- a/qcsrc/server/client.qc +++ b/qcsrc/server/client.qc @@ -387,7 +387,6 @@ void PutObserverInServer(entity this) { SetPlayerTeam(this, -1, TEAM_CHANGE_SPECTATOR); this.frags = FRAGS_SPECTATOR; - PlayerScore_Clear(this); // clear scores when needed } } diff --git a/qcsrc/server/teamplay.qc b/qcsrc/server/teamplay.qc index 68f4d453f..588432996 100644 --- a/qcsrc/server/teamplay.qc +++ b/qcsrc/server/teamplay.qc @@ -211,9 +211,10 @@ bool SetPlayerTeam(entity player, int team_index, int type) { return false; } - LogTeamchange(player.playerid, player.team, type); + LogTeamChange(player.playerid, player.team, type); if (team_index != old_team_index) { + PlayerScore_Clear(player); if (team_index != -1) { Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM( @@ -225,6 +226,10 @@ bool SetPlayerTeam(entity player, int team_index, int type) player.netname); } KillPlayerForTeamChange(player); + if (!IS_BOT_CLIENT(player)) + { + TeamBalance_AutoBalanceBots(); + } } return true; } @@ -234,7 +239,6 @@ bool MoveToTeam(entity client, int team_index, int type) //PrintToChatAll(sprintf("MoveToTeam: %s, %f", client.netname, team_index)); int lockteams_backup = lockteams; // backup any team lock lockteams = 0; // disable locked teams - PlayerScore_Clear(client); if (!SetPlayerTeam(client, team_index, type)) { lockteams = lockteams_backup; // restore the team lock @@ -244,31 +248,6 @@ bool MoveToTeam(entity client, int team_index, int type) return true; } -void KillPlayerForTeamChange(entity player) -{ - if (IS_DEAD(player)) - { - return; - } - if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true) - { - return; - } - Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP, - player.origin, '0 0 0'); -} - -void LogTeamchange(float player_id, float team_number, int type) -{ - if(!autocvar_sv_eventlog) - return; - - if(player_id < 1) - return; - - GameLogEcho(strcat(":team:", ftos(player_id), ":", ftos(team_number), ":", ftos(type))); -} - bool Player_HasRealForcedTeam(entity player) { return player.team_forced > TEAM_FORCE_DEFAULT; @@ -379,6 +358,42 @@ void Player_DetermineForcedTeam(entity player) } } +void TeamBalance_JoinBestTeam(entity player) +{ + //PrintToChatAll(sprintf("TeamBalance_JoinBestTeam: %s", player.netname)); + if (!teamplay) + { + return; + } + if (player.bot_forced_team) + { + return; + } + entity balance = TeamBalance_CheckAllowedTeams(player); + if (Player_HasRealForcedTeam(player)) + { + int forced_team_index = player.team_forced; + bool is_team_allowed = TeamBalance_IsTeamAllowedInternal(balance, + forced_team_index); + TeamBalance_Destroy(balance); + if (!is_team_allowed) + { + return; + } + if (!SetPlayerTeam(player, forced_team_index, TEAM_CHANGE_AUTO)) + { + return; + } + return; + } + int best_team_index = TeamBalance_FindBestTeam(balance, player, true); + TeamBalance_Destroy(balance); + if (!SetPlayerTeam(player, best_team_index, TEAM_CHANGE_AUTO)) + { + return; + } +} + entity TeamBalance_CheckAllowedTeams(entity for_whom) { entity balance = spawn(); @@ -758,51 +773,6 @@ int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score) return team_bits; } -void TeamBalance_JoinBestTeam(entity this) -{ - //PrintToChatAll(sprintf("JoinBestTeam: %s", this.netname)); - if (!teamplay) - { - return; - } - if (this.bot_forced_team) - { - return; - } - entity balance = TeamBalance_CheckAllowedTeams(this); - if (Player_HasRealForcedTeam(this)) - { - int forced_team_index = this.team_forced; - bool is_team_allowed = TeamBalance_IsTeamAllowedInternal(balance, - forced_team_index); - TeamBalance_Destroy(balance); - if (!is_team_allowed) - { - return; - } - if (!SetPlayerTeam(this, forced_team_index, TEAM_CHANGE_AUTO)) - { - return; - } - if (!IS_BOT_CLIENT(this)) - { - TeamBalance_AutoBalanceBots(); - } - return; - } - int best_team_index = TeamBalance_FindBestTeam(balance, this, true); - TeamBalance_Destroy(balance); - PlayerScore_Clear(this); - if (!SetPlayerTeam(this, best_team_index, TEAM_CHANGE_AUTO)) - { - return; - } - if (!IS_BOT_CLIENT(this)) - { - TeamBalance_AutoBalanceBots(); - } -} - int TeamBalance_CompareTeams(entity balance, int team_index_a, int team_index_b, entity player, bool use_score) { @@ -975,6 +945,33 @@ entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index, return lowest_player; } +void LogTeamChange(float player_id, float team_number, int type) +{ + if (!autocvar_sv_eventlog) + { + return; + } + if (player_id < 1) + { + return; + } + GameLogEcho(sprintf(":team:%f:%f:%f", player_id, team_number, type)); +} + +void KillPlayerForTeamChange(entity player) +{ + if (IS_DEAD(player)) + { + return; + } + if (MUTATOR_CALLHOOK(Player_ChangeTeamKill, player) == true) + { + return; + } + Damage(player, player, player, 100000, DEATH_TEAMCHANGE.m_id, DMG_NOWEP, + player.origin, '0 0 0'); +} + bool TeamBalance_IsTeamAllowedInternal(entity balance, int index) { return balance.m_team_balance_team[index - 1].m_num_players != @@ -1126,7 +1123,6 @@ void SV_ChangeTeam(entity this, float _color) { SetPlayerTeam(this, destination_team_index, TEAM_CHANGE_MANUAL); TeamBalance_Destroy(balance); - TeamBalance_AutoBalanceBots(); // Hack return; } @@ -1148,14 +1144,5 @@ void SV_ChangeTeam(entity this, float _color) } } TeamBalance_Destroy(balance); - if (IS_PLAYER(this) && source_team_index != destination_team_index) - { - // reduce frags during a team change - PlayerScore_Clear(this); - } - if (!SetPlayerTeam(this, destination_team_index, TEAM_CHANGE_MANUAL)) - { - return; - } - TeamBalance_AutoBalanceBots(); + SetPlayerTeam(this, destination_team_index, TEAM_CHANGE_MANUAL); } diff --git a/qcsrc/server/teamplay.qh b/qcsrc/server/teamplay.qh index e265df49f..c03690949 100644 --- a/qcsrc/server/teamplay.qh +++ b/qcsrc/server/teamplay.qh @@ -83,10 +83,17 @@ void SetPlayerColors(entity player, float _color); /// \return True if team switch was successful, false otherwise. bool Player_SetTeamIndex(entity player, int index); +enum +{ + TEAM_CHANGE_AUTO = 2, ///< The team was selected by autobalance. + TEAM_CHANGE_MANUAL = 3, ///< Player has manually selected their team. + TEAM_CHANGE_SPECTATOR = 4 ///< Player is joining spectators. //TODO: Remove? +}; + /// \brief Sets the team of the player. /// \param[in,out] player Player to adjust. /// \param[in] team_index Index of the team to set. -/// \param[in] type ??? +/// \param[in] type Type of the team change. See TEAM_CHANGE constants. /// \return True if team switch was successful, false otherwise. bool SetPlayerTeam(entity player, int team_index, int type); @@ -97,19 +104,6 @@ bool SetPlayerTeam(entity player, int team_index, int type); /// \return True on success, false otherwise. bool MoveToTeam(entity client, int team_index, int type); -/// \brief Kills player as a result of team change. -/// \param[in,out] player Player to kill. -void KillPlayerForTeamChange(entity player); - -enum -{ - TEAM_CHANGE_AUTO = 2, - TEAM_CHANGE_MANUAL = 3, - TEAM_CHANGE_SPECTATOR = 4 -}; - -void LogTeamchange(float player_id, float team_number, int type); - enum { TEAM_FORCE_SPECTATOR = -1, ///< Force the player to spectator team. @@ -138,6 +132,11 @@ void Player_DetermineForcedTeam(entity player); // ========================= Team balance API ================================= +/// \brief Assigns the given player to a team that will make the game most +/// balanced. +/// \param[in,out] player Player to assign. +void TeamBalance_JoinBestTeam(entity player); + /// \brief Checks whether the player can join teams according to global /// configuration and mutator settings. /// \param[in] for_whom Player to check for. Pass NULL for global rules. @@ -200,8 +199,6 @@ int TeamBalance_FindBestTeam(entity balance, entity player, bool ignore_player); /// function. int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score); -void TeamBalance_JoinBestTeam(entity this); - /// \brief Describes the result of comparing teams. enum { @@ -245,6 +242,12 @@ entity TeamBalance_GetPlayerForTeamSwitch(int source_team_index, // ============================ Internal API ================================== +void LogTeamChange(float player_id, float team_number, int type); + +/// \brief Kills player as a result of team change. +/// \param[in,out] player Player to kill. +void KillPlayerForTeamChange(entity player); + /// \brief Returns whether the team change to the specified team is allowed. /// \param[in] balance Team balance entity. /// \param[in] index Index of the team. -- 2.39.2