#pragma once
+int autocvar_teamplay_mode;
+
+bool autocvar_g_changeteam_banned;
+bool autocvar_teamplay_lockonrestart;
+
+bool autocvar_g_balance_teams;
+bool autocvar_g_balance_teams_prevent_imbalance;
+
+string autocvar_g_forced_team_otherwise;
+
bool lockteams;
+.int team_forced; // can be a team number to force a team, or 0 for default action, or -1 for forced spectator
+
// ========================== Global teams API ================================
+void Team_InitTeams();
+
/// \brief Returns the global team entity at the given index.
/// \param[in] index Index of the team.
/// \return Global team entity at the given index.
/// \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 number of control points owned by a team.
+/// \brief Returns the winner team.
+/// \param[in] min_owned_items Minimum number of items the winner team must have.
+/// \return Winner team or 0 if 2 or more teams own items or -1 if no team own any items.
+int Team_GetWinnerTeam_WithOwnedItems(int min_owned_items);
+
+/// \brief Returns the number of items owned by a team.
/// \param[in] team_ent Team entity.
-/// \return Number of control points owned by a team.
-int Team_GetNumberOfControlPoints(entity team_ent);
+/// \return Number of items owned by a team.
+int Team_GetNumberOfOwnedItems(entity team_ent);
-/// \brief Sets the number of control points owned by a team.
+/// \brief Sets the number of items owned by a team.
/// \param[in,out] team_ent Team entity.
-/// \param[in] number Number of control points to set.
-void Team_SetNumberOfControlPoints(entity team_ent, int number);
+/// \param[in] number Number of items to set.
+void Team_SetNumberOfOwnedItems(entity team_ent, int number);
-/// \brief Returns the number of teams that own control points.
-/// \return Number of teams that own control points.
-int Team_GetNumberOfTeamsWithControlPoints();
+/// \brief Returns the number of teams that own items.
+/// \return Number of teams that own items.
+int Team_GetNumberOfTeamsWithOwnedItems();
// ======================= Entity specific API ================================
/// \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);
+/// \brief Sets the team of the player with all sanity checks.
+/// \param[in,out] player Player to adjust.
+/// \param[in] team_index Index of the team to set.
+void Player_SetTeamIndexChecked(entity player, int team_index);
+
/// \brief Moves player to the specified team.
/// \param[in,out] client Client to move.
/// \param[in] team_index Index of the team.
/// \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.
// ========================= 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.
/// function.
int TeamBalance_FindBestTeams(entity balance, entity player, bool use_score);
-void TeamBalance_JoinBestTeam(entity this);
-
/// \brief Describes the result of comparing teams.
enum
{
/// \brief Switches a bot from one team to another if teams are not balanced.
void TeamBalance_AutoBalanceBots();
+/// \brief Returns the index of the team with most players that is contained in
+/// the given bitmask of teams.
+/// \param[in] balance Team balance entity.
+/// \param[in] teams Bitmask of teams to search in.
+/// \return Index of the team with most players.
+int TeamBalance_GetLargestTeamIndex(entity balance, int teams);
+
/// \brief Returns the player who is the most suitable for switching between
/// the given teams.
/// \param[in] source_team_index Index of the team to search in.
// ============================ 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.
/// function.
int TeamBalance_CompareTeamsInternal(entity team_a, entity team_index_b,
entity player, bool use_score);
+
+/// \brief Called when the player changes color with the "color" command.
+/// Note that the "color" command is always called early on player connection
+/// \param[in,out] player Player that requested a new color.
+/// \param[in] new_color Requested color.
+void SV_ChangeTeam(entity player, int new_color);