#include <sourcemod>
#include <cstrike>
public void OnPluginStart()
{
int aPlayers[MAXPLAYERS], count;
get_players(aPlayers, count);
// or
get_playersnum_ex(GetPlayers_ExcludeBots);
// or
get_playersnum();
}
enum GetPlayersFlags (<<=1) {
GetPlayers_None = 0,
GetPlayers_ExcludeDead = 1,
GetPlayers_ExcludeAlive,
GetPlayers_ExcludeBots,
GetPlayers_ExcludeHuman,
GetPlayers_MatchTeam,
GetPlayers_MatchNameSubstring,
GetPlayers_CaseInsensitive,
GetPlayers_ExcludeHLTV,
GetPlayers_IncludeConnecting
}
/**
* Returns the number of clients on the server.
*
* @param flag Count clients still in the connecting process if nonzero
*
* @return Number of clients on the server
*/
stock int get_playersnum(int flag = 0) {
int num = 0;
for (int i = 1; i <= MaxClients; i++) {
if (IsClientConnected(i) || (flag && IsClientInGame(i)) {
num++;
}
}
return num;
}
/**
* Stores a filtered list of client indexes to an array.
*
* @note Example retrieving all alive CTs:
* get_players_ex(players, num, GetPlayers_ExcludeDead | GetPlayers_MatchTeam, "CT")
*
* @param players Array to store indexes to
* @param num Variable to store number of indexes to
* @param flags Optional filtering flags (enum GetPlayersFlags); valid flags are:
* GetPlayers_None - No filter (Default)
* GetPlayers_ExcludeDead - do not include dead clients
* GetPlayers_ExcludeAlive - do not include alive clients
* GetPlayers_ExcludeBots - do not include bots
* GetPlayers_ExcludeHuman - do not include human clients
* GetPlayers_MatchTeam - match with team
* GetPlayers_MatchNameSubstring - match with part of name
* GetPlayers_CaseInsensitive - match case insensitive
* GetPlayers_ExcludeHLTV - do not include SourceTV proxies
* GetPlayers_IncludeConnecting - include connecting clients
* @param team String to match against if the "MatchTeam" or "GetPlayers_MatchNameSubstring" flag is specified
*
* @noreturn
*/
stock void get_players(int players[MAXPLAYERS] = {}, int &num, GetPlayersFlags flags = GetPlayers_None, const char[] buffer = "") {
int team = CS_TEAM_NONE;
char name[MAX_NAME_LENGTH];
if (flags & GetPlayers_MatchTeam) {
team = TeamFromName(buffer, !(flags & GetPlayers_CaseInsensitive));
}
for (int i = 1; i <= MaxClients; i++) {
if (IsClientConnected(i) || ((flags & GetPlayers_IncludeConnecting) && IsClientInGame(i))) {
if (IsPlayerAlive(i) ? (flags & GetPlayers_ExcludeAlive) : (flags & GetPlayers_ExcludeDead))
continue;
if (IsFakeClient(i) ? (flags & GetPlayers_ExcludeBots) : (flags & GetPlayers_ExcludeHuman))
continue;
if ((flags & GetPlayers_MatchTeam) && GetClientTeam(i) != team)
continue;
if ((flags & GetPlayers_ExcludeHLTV) && IsClientSourceTV(i))
continue;
if (flags & GetPlayers_MatchNameSubstring) {
GetClientName(i, name, sizeof name);
if (!StrContains(buffer, name, !(flags & GetPlayers_CaseInsensitive)))
continue;
}
players[num++] = i;
}
}
}
/**
* Returns the number of clients on the server that match the specified flags.
*
* @note Example retrieving all alive CTs:
* new AliveCt = get_playersnum_ex(GetPlayers_ExcludeDead | GetPlayers_MatchTeam, "CT")
*
* @param flags Optional filtering flags (enum GetPlayersFlags); valid flags are:
* GetPlayers_None - No filter (Default)
* GetPlayers_ExcludeDead - do not include dead clients
* GetPlayers_ExcludeAlive - do not include alive clients
* GetPlayers_ExcludeBots - do not include bots
* GetPlayers_ExcludeHuman - do not include human clients
* GetPlayers_MatchTeam - match with team
* GetPlayers_MatchNameSubstring - match with part of name
* GetPlayers_CaseInsensitive - match case insensitive
* GetPlayers_ExcludeHLTV - do not include SourceTV proxies
* GetPlayers_IncludeConnecting - include connecting clients
* @param team String to match against if the GetPlayers_MatchTeam or GetPlayers_MatchNameSubstring flag is specified
*
* @return Number of clients on the server that match the specified flags
*/
stock int get_playersnum_ex(GetPlayersFlags flags = GetPlayers_None, const char[] buffer = "") {
int players[MAXPLAYERS], num;
get_players_ex(_, num, flags, buffer);
return num;
}
stock int TeamFromName(const char[] team, bool caseSensitive = true) {
if(strcmp(team, "TERRORIST", caseSensitive) == 0)
return CS_TEAM_T;
if(strcmp(team, "СT", caseSensitive) == 0)
return CS_TEAM_CT;
if(strcmp(team, "SPECTATOR", caseSensitive) == 0)
return CS_TEAM_SPECTATOR;
return CS_TEAM_NONE;
}