plugins/include/clients.inc
author Matt Woodrow <pred@alliedmods.net>
Fri Aug 08 00:17:37 2008 +0000 (2008-08-08)
changeset 2224 676c3b826f13
parent 2162 59c5dc056c69
child 2289 44045ead61cd
permissions -rw-r--r--
Initial Import of beta structs extension.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     5  * =============================================================================
     6  *
     7  * This file is part of the SourceMod/SourcePawn SDK.
     8  *
     9  * This program is free software; you can redistribute it and/or modify it under
    10  * the terms of the GNU General Public License, version 3.0, as published by the
    11  * Free Software Foundation.
    12  * 
    13  * This program is distributed in the hope that it will be useful, but WITHOUT
    14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    16  * details.
    17  *
    18  * You should have received a copy of the GNU General Public License along with
    19  * this program.  If not, see <http://www.gnu.org/licenses/>.
    20  *
    21  * As a special exception, AlliedModders LLC gives you permission to link the
    22  * code of this program (as well as its derivative works) to "Half-Life 2," the
    23  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    24  * by the Valve Corporation.  You must obey the GNU General Public License in
    25  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    26  * this exception to all derivative works.  AlliedModders LLC defines further
    27  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    28  * or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #if defined _clients_included
    34  #endinput
    35 #endif
    36 #define _clients_included
    37 
    38 /**
    39  * Network flow directions.
    40  */
    41 enum NetFlow
    42 {
    43 	NetFlow_Outgoing = 0,	/**< Outgoing traffic */
    44 	NetFlow_Incoming,		/**< Incoming traffic */
    45 	NetFlow_Both,			/**< Both values added together */
    46 };
    47 
    48 #define MAXPLAYERS		64	/**< Maximum number of players that can be in server */
    49 #define MAX_NAME_LENGTH 32	/**< Maximum buffer required to store a client name */
    50 
    51 /**
    52  * Called on client connection.
    53  *
    54  * @param client		Client index.
    55  * @param rejectmsg		Buffer to store the rejection message when the connection is refused.
    56  * @param maxlen		Maximum number of characters for rejection buffer.
    57  * @return				True to validate client's connection, false to refuse it.
    58  */
    59 forward bool:OnClientConnect(client, String:rejectmsg[], maxlen);
    60 
    61 /**
    62  * Called when a client is entering the game.
    63  *
    64  * Whether a client has a steamid is undefined until OnClientAuthorized
    65  * is called, which may occur either before or after OnClientPutInServer.
    66  * Similarly, use OnClientPostAdminCheck() if you need to verify whether 
    67  * connecting players are admins.
    68  *
    69  * GetClientCount() will include clients as they are passed through this 
    70  * function, as clients are already in game at this point.
    71  *
    72  * @param client		Client index.
    73  * @noreturn
    74  */
    75 forward OnClientPutInServer(client);
    76 
    77 /**
    78  * Called when a client is disconnecting from the server.
    79  *
    80  * @param client		Client index.
    81  * @noreturn
    82  */
    83 forward OnClientDisconnect(client);
    84 
    85 /**
    86  * Called when a client is disconnected from the server.
    87  *
    88  * @param client		Client index.
    89  * @noreturn
    90  */
    91 forward OnClientDisconnect_Post(client);
    92 
    93 /**
    94  * Called when a client is sending a command.
    95  *
    96  * @param client		Client index.
    97  * @param args			Number of arguments.
    98  * @noreturn
    99  */
   100 forward Action:OnClientCommand(client, args);
   101 
   102 /**
   103  * Called whenever the client's settings are changed.
   104  *
   105  * @param client		Client index.
   106  * @noreturn
   107  */
   108 forward OnClientSettingsChanged(client);
   109 
   110 /**
   111  * Called when a client receives a Steam ID.  The state of a client's 
   112  * authorization via Steam or as an admin is not guaranteed here.  
   113  * Use OnClientAuthorized() or OnClientPostAdminCheck() for those, 
   114  * respectively.
   115  *
   116  * This is called by bots, but the ID will be "BOT".
   117  *
   118  * @param client		Client index.
   119  * @param auth			Client auth string.
   120  * @noreturn
   121  */
   122 forward OnClientAuthorized(client, const String:auth[]);
   123 
   124 /**
   125  * Called once a client is authorized and fully in-game, but 
   126  * before admin checks are done.  This can be used to override 
   127  * the default admin checks for a client.  You should only use 
   128  * this for overriding; use OnClientPostAdminCheck() instead 
   129  * if you want notification.
   130  *
   131  * Note: If handled/blocked, PostAdminCheck must be signalled 
   132  * manually via NotifyPostAdminCheck().
   133  *
   134  * This callback is gauranteed to occur on all clients, and always 
   135  * after each OnClientPutInServer() call.
   136  *
   137  * @param client		Client index.
   138  * @return				Plugin_Handled to block admin checks.
   139  */
   140 forward Action:OnClientPreAdminCheck(client);
   141 
   142 /**
   143  * Called directly before OnClientPostAdminCheck() as a method to 
   144  * alter administrative permissions before plugins perform final 
   145  * post-connect operations.  
   146  *
   147  * In general, do not use this function unless you are specifically 
   148  * attempting to change access permissions.  Use OnClientPostAdminCheck() 
   149  * instead if you simply want to perform post-connect authorization 
   150  * routines.
   151  *
   152  * See OnClientPostAdminCheck() for more information.
   153  *
   154  * @param client		Client index.
   155  * @noreturn
   156  */
   157 forward OnClientPostAdminFilter(client);
   158 
   159 /**
   160  * Called once a client is authorized and fully in-game, and 
   161  * after all post-connection authorizations have been performed.  
   162  *
   163  * This callback is gauranteed to occur on all clients, and always 
   164  * after each OnClientPutInServer() call.
   165  *
   166  * @param client		Client index.
   167  * @noreturn
   168  */
   169 forward OnClientPostAdminCheck(client);
   170 
   171 /**
   172  * Returns the maximum number of clients allowed on the server.  This may 
   173  * return 0 if called before OnMapStart(), and thus should not be called 
   174  * in OnPluginStart().  
   175  *
   176  * @return				Maximum number of clients allowed.
   177  */
   178 native GetMaxClients();
   179 
   180 /**
   181  * Returns the client count put in the server.
   182  *
   183  * @param inGameOnly	If false connecting players are also counted.
   184  * @return				Client count in the server.
   185  */
   186 native GetClientCount(bool:inGameOnly=true);
   187 
   188 /**
   189  * Returns the client's name.
   190  *
   191  * @param client		Player index.
   192  * @param name			Buffer to store the client's name.
   193  * @param maxlen		Maximum length of string buffer (includes NULL terminator).
   194  * @return				True on success, false otherwise.
   195  * @error				If the client is not connected an error will be thrown.
   196  */
   197 native bool:GetClientName(client, String:name[], maxlen);
   198 
   199 /**
   200  * Retrieves a client's IP address.
   201  *
   202  * @param client		Player index.
   203  * @param name			Buffer to store the client's ip address.
   204  * @param maxlen		Maximum length of string buffer (includes NULL terminator).
   205  * @param remport		Remove client's port from the ip string (true by default).
   206  * @return				True on success, false otherwise.
   207  * @error				If the client is not connected or the index is invalid.
   208  */
   209 native bool:GetClientIP(client, String:ip[], maxlen, bool:remport=true);
   210 
   211 /**
   212  * Retrieves a client's authentication string (SteamID).
   213  *
   214  * @param client		Player index.
   215  * @param auth			Buffer to store the client's auth string.
   216  * @param maxlen		Maximum length of string buffer (includes NULL terminator).
   217  * @return				True on success, false otherwise.
   218  * @error				If the client is not connected or the index is invalid.
   219  */
   220 native bool:GetClientAuthString(client, String:auth[], maxlen);
   221 
   222 /**
   223  * Retrieves a client's user id, which is an index incremented for every client
   224  * that joins the server.
   225  *
   226  * @param client		Player index.
   227  * @return				User id of the client.
   228  * @error 				If the client is not connected or the index is invalid.
   229  */
   230 native GetClientUserId(client);
   231 
   232 /**
   233  * Returns if a certain player is connected.
   234  *
   235  * @param client		Player index.
   236  * @return				True if player is connected to the server, false otherwise.
   237  */
   238 native bool:IsClientConnected(client);
   239 
   240 /**
   241  * Returns if a certain player has entered the game.
   242  *
   243  * @param client		Player index (index does not have to be connected).
   244  * @return				True if player has entered the game, false otherwise.
   245  * @error				Invalid client index.
   246  */
   247 native bool:IsClientInGame(client);
   248 
   249 /**
   250  * Returns if a client is in the "kick queue" (i.e. the client will be kicked 
   251  * shortly and thus they should not appear as valid).
   252  *
   253  * @param client		Player index (must be connected).
   254  * @return				True if in the kick queue, false otherwise.
   255  * @error				Invalid client index.
   256  */
   257 native bool:IsClientInKickQueue(client);
   258 
   259 /**
   260  * Backwards compatibility stock - use IsClientInGame
   261  * @deprecated			Renamed to IsClientInGame
   262  */
   263 #pragma deprecated Use IsClientInGame() instead
   264 stock bool:IsPlayerInGame(client)
   265 {
   266 	return IsClientInGame(client);
   267 }
   268 
   269 /**
   270  * Returns if a certain player has been authenticated.
   271  *
   272  * @param client		Player index.
   273  * @return				True if player has been authenticated, false otherwise.
   274  */
   275 native bool:IsClientAuthorized(client);
   276 
   277 /**
   278  * Returns if a certain player is a fake client.
   279  *
   280  * @param client		Player index.
   281  * @return				True if player is a fake client, false otherwise.
   282  */
   283 native bool:IsFakeClient(client);
   284 
   285 /**
   286  * Returns if a certain player is an observer/spectator.
   287  *
   288  * @param client		Player index.
   289  * @return				True if player is an obverser, false otherwise.
   290  */
   291 native bool:IsClientObserver(client);
   292 
   293 /**
   294  * Returns if the client is alive or dead.
   295  *
   296  * Note: This function was originally in SDKTools and was moved to core.
   297  *
   298  * @param client		Player's index.
   299  * @return				True if the client is alive, false otherwise.
   300  * @error				Invalid client index, client not in game, or no mod support.
   301  */
   302 native bool:IsPlayerAlive(client);
   303 
   304 /**
   305  * Retrieves values from client replicated keys.
   306  *
   307  * @param client		Player's index.
   308  * @param key			Key string.
   309  * @param value			Buffer to store value.
   310  * @param maxlen		Maximum length of valve (UTF-8 safe).
   311  * @return 				True on success, false otherwise.
   312  * @error				Invalid client index, or client not connected.
   313  */
   314 native bool:GetClientInfo(client, const String:key[], String:value[], maxlen);
   315 
   316 /**
   317  * Retrieves a client's team index.
   318  *
   319  * @param client		Player's index.
   320  * @return				Team index the client is on (mod specific).
   321  * @error				Invalid client index, client not in game, or no mod support.
   322  */
   323 native GetClientTeam(client);
   324  
   325 /**
   326  * Sets a client's AdminId.  
   327  *
   328  * @param client		Player's index.
   329  * @param id			AdminId to set.  INVALID_ADMIN_ID removes admin permissions.
   330  * @param temp			True if the id should be freed on disconnect.
   331  * @noreturn
   332  * @error				Invalid client index, client not connected, or bogus AdminId.
   333  */
   334 native SetUserAdmin(client, AdminId:id, bool:temp=false);
   335 
   336 /**
   337  * Retrieves a client's AdminId.
   338  *
   339  * @param client		Player's index.
   340  * @return				AdminId of the client, or INVALID_ADMIN_ID if none.
   341  * @error				Invalid client index, or client not connected.
   342  */
   343 native AdminId:GetUserAdmin(client);
   344 
   345 /**
   346  * Sets access flags on a client.  If the client is not an admin,
   347  * a temporary, anonymous AdminId is given.
   348  *
   349  * @param client		Player's index.
   350  * @param ...			Flags to set on the client.
   351  * @noreturn
   352  * @error				Invalid client index, or client not connected.
   353  */
   354 native AddUserFlags(client, AdminFlag:...);
   355 
   356 /**
   357  * Removes flags from a client.  If the client is not an admin,
   358  * this has no effect.
   359  *
   360  * @param client		Player's index.
   361  * @param ...			Flags to remove from the client.
   362  * @noreturn
   363  * @error				Invalid client index, or client not connected.
   364  */
   365 native RemoveUserFlags(client, AdminFlag:...);
   366 
   367 /** 
   368  * Sets access flags on a client using bits instead of flags.  If the
   369  * client is not an admin, and flags not 0, a temporary, anonymous AdminId is given.
   370  *
   371  * @param client		Player's index.
   372  * @param flags			Bitstring of flags to set on client.
   373  * @noreturn
   374  */
   375 native SetUserFlagBits(client, flags);
   376 
   377 /**
   378  * Returns client access flags.  If the client is not an admin,
   379  * the result is always 0.
   380  * 
   381  * @param client		Player's index.
   382  * @return				Flags
   383  * @error				Invalid client index, or client not connected.
   384  */
   385 native GetUserFlagBits(client);
   386 
   387 /**
   388  * Returns whether a user can target another user.
   389  * This is a helper function for CanAdminTarget.
   390  *
   391  * @param client		Player's index.
   392  * @param target		Target player's index.
   393  * @return				True if target is targettable by the player, false otherwise.
   394  * @error				Invalid or unconnected player indexers.
   395  */
   396 native bool:CanUserTarget(client, target);
   397 
   398 /**
   399  * Runs through the Core-defined admin authorization checks on a player.
   400  * Has no effect if the player is already an admin.
   401  *
   402  * Note: This function is based on the internal cache only.
   403  *
   404  * @param client		Client index.
   405  * @return				True if access was changed, false if it did not.
   406  * @error				Invalid client index or client not in-game AND authorized.
   407  */
   408 native bool:RunAdminCacheChecks(client);
   409 
   410 /**
   411  * Signals that a player has completed post-connection admin checks.
   412  * Has no effect if the player has already had this event signalled.
   413  *
   414  * Note: This must be sent even if no admin id was assigned.
   415  *
   416  * @param client		Client index.
   417  * @noreturn
   418  * @error				Invalid client index or client not in-game AND authorized.
   419  */
   420 native NotifyPostAdminCheck(client);
   421 
   422 /** 
   423  * Creates a fake client.
   424  *
   425  * @param name			Name to use.
   426  * @return				Client index on success, 0 otherwise.
   427  */
   428 native CreateFakeClient(const String:name[]);
   429 
   430 /**
   431  * Sets a convar value on a fake client.
   432  *
   433  * @param client		Client index.
   434  * @param cvar			ConVar name.
   435  * @param value			ConVar value.
   436  * @noreturn
   437  * @error				Invalid client index, client not connected,
   438  *						or client not a fake client.
   439  */
   440 native SetFakeClientConVar(client, const String:cvar[], const String:value[]);
   441 
   442 /**
   443  * Returns the client's health.
   444  *
   445  * @param client		Player's index.
   446  * @return				Health value.
   447  * @error				Invalid client index, client not in game, or no mod support.
   448  */
   449 native GetClientHealth(client);
   450 
   451 /**
   452  * Returns the client's model name.
   453  *
   454  * @param client		Player's index.
   455  * @param model			Buffer to store the client's model name.
   456  * @param maxlen		Maximum length of string buffer (includes NULL terminator).
   457  * @noreturn
   458  * @error				Invalid client index, client not in game, or no mod support.
   459  */
   460 native GetClientModel(client, String:model[], maxlen);
   461 
   462 /**
   463  * Returns the client's weapon name.
   464  *
   465  * @param client		Player's index.
   466  * @param weapon		Buffer to store the client's weapon name.
   467  * @param maxlen		Maximum length of string buffer (includes NULL terminator).
   468  * @noreturn
   469  * @error				Invalid client index, client not in game, or no mod support.
   470  */
   471 native GetClientWeapon(client, String:weapon[], maxlen);
   472 
   473 /**
   474  * Returns the client's max size vector.
   475  *
   476  * @param client		Player's index.
   477  * @param vec			Destination vector to store the client's max size.
   478  * @noreturn
   479  * @error				Invalid client index, client not in game, or no mod support.
   480  */
   481 native GetClientMaxs(client, Float:vec[3]);
   482 
   483 /**
   484  * Returns the client's min size vector.
   485  *
   486  * @param client		Player's index.
   487  * @param vec			Destination vector to store the client's min size.
   488  * @noreturn
   489  * @error				Invalid client index, client not in game, or no mod support.
   490  */
   491 native GetClientMins(client, Float:vec[3]);
   492 
   493 /**
   494  * Returns the client's position angle.
   495  *
   496  * @param client		Player's index.
   497  * @param ang			Destination vector to store the client's position angle.
   498  * @noreturn
   499  * @error				Invalid client index, client not in game, or no mod support.
   500  */
   501 native GetClientAbsAngles(client, Float:ang[3]);
   502 
   503 /**
   504  * Returns the client's origin vector.
   505  *
   506  * @param client		Player's index.
   507  * @param vec			Destination vector to store the client's origin vector.
   508  * @noreturn
   509  * @error				Invalid client index, client not in game, or no mod support.
   510  */
   511 native GetClientAbsOrigin(client, Float:vec[3]);
   512 
   513 /**
   514  * Returns the client's armor.
   515  *
   516  * @param client		Player's index.
   517  * @return				Armor value.
   518  * @error				Invalid client index, client not in game, or no mod support.
   519  */
   520 native GetClientArmor(client);
   521 
   522 /**
   523  * Returns the client's death count.
   524  *
   525  * @param client		Player's index.
   526  * @return				Death count.
   527  * @error				Invalid client index, client not in game, or no mod support.
   528  */
   529 native GetClientDeaths(client);
   530 
   531 /**
   532  * Returns the client's frag count.
   533  *
   534  * @param client		Player's index.
   535  * @return				Frag count.
   536  * @error				Invalid client index, client not in game, or no mod support.
   537  */
   538 native GetClientFrags(client);
   539 
   540 /**
   541  * Returns the client's send data rate in bytes/sec.
   542  *
   543  * @param client		Player's index.
   544  * @return				Data rate.
   545  * @error				Invalid client index, client not in game, or fake client.
   546  */
   547 native GetClientDataRate(client);
   548 
   549 /**
   550  * Returns if a client is timing out
   551  *
   552  * @param client		Player's index.
   553  * @return				True if client is timing out, false otherwise.
   554  * @error				Invalid client index, client not in game, or fake client.
   555  */
   556 native bool:IsClientTimingOut(client);
   557 
   558 /**
   559  * Returns the client's connection time in seconds.
   560  *
   561  * @param client		Player's index.
   562  * @return				Connection time.
   563  * @error				Invalid client index, client not in game, or fake client.
   564  */
   565 native Float:GetClientTime(client);
   566 
   567 /**
   568  * Returns the client's current latency (RTT), more accurate than GetAvgLatency but jittering.
   569  *
   570  * @param client		Player's index.
   571  * @param flow			Traffic flowing direction.
   572  * @return				Latency.
   573  * @error				Invalid client index, client not in game, or fake client.
   574  */
   575 native Float:GetClientLatency(client, NetFlow:flow);
   576 
   577 /**
   578  * Returns the client's average packet latency in seconds.
   579  *
   580  * @param client		Player's index.
   581  * @param flow			Traffic flowing direction.
   582  * @return				Average latency.
   583  * @error				Invalid client index, client not in game, or fake client.
   584  */
   585 native Float:GetClientAvgLatency(client, NetFlow:flow);
   586 
   587 /**
   588  * Returns the client's average packet loss, values go from 0 to 1 (for percentages).
   589  *
   590  * @param client		Player's index.
   591  * @param flow			Traffic flowing direction.
   592  * @return				Average packet loss.
   593  * @error				Invalid client index, client not in game, or fake client.
   594  */
   595 native Float:GetClientAvgLoss(client, NetFlow:flow);
   596 
   597 /**
   598  * Returns the client's average packet choke, values go from 0 to 1 (for percentages).
   599  *
   600  * @param client		Player's index.
   601  * @param flow			Traffic flowing direction.
   602  * @return				Average packet choke.
   603  * @error				Invalid client index, client not in game, or fake client.
   604  */
   605 native Float:GetClientAvgChoke(client, NetFlow:flow);
   606 
   607 /**
   608  * Returns the client's data flow in bytes/sec.
   609  *
   610  * @param client		Player's index.
   611  * @param flow			Traffic flowing direction.
   612  * @return				Data flow.
   613  * @error				Invalid client index, client not in game, or fake client.
   614  */
   615 native Float:GetClientAvgData(client, NetFlow:flow);
   616 
   617 /**
   618  * Returns the client's average packet frequency in packets/sec.
   619  *
   620  * @param client		Player's index.
   621  * @param flow			Traffic flowing direction.
   622  * @return				Packet frequency.
   623  * @error				Invalid client index, client not in game, or fake client.
   624  */
   625 native Float:GetClientAvgPackets(client, NetFlow:flow);
   626 
   627 /**
   628  * Translates an userid index to the real player index.
   629  *
   630  * @param userid		Userid value.
   631  * @return				Client value.
   632  * @error				Returns 0 if invalid userid.
   633  */
   634 native GetClientOfUserId(userid);
   635 
   636 /**
   637  * Disconnects a client from the server as soon as the next frame starts.
   638  *
   639  * Note: Originally, KickClient() was immediate.  The delay was introduced 
   640  * because despite warnings, plugins were using it in ways that would crash. 
   641  * The new safe version can break cases that rely on immediate disconnects, 
   642  * but ensures that plugins do not accidentally cause crashes.
   643  *
   644  * If you need immediate disconnects, use KickClientEx().
   645  *
   646  * Note: IsClientInKickQueue() will return true before the kick occurs.
   647  *
   648  * @param client		Client index.
   649  * @param format		Optional formatting rules for disconnect reason.
   650  *                      Note that a period is automatically appended to the string by the engine.
   651  * @param ...			Variable number of format parameters.
   652  * @noreturn
   653  * @error				Invalid client index, or client not connected.
   654  */
   655 native KickClient(client, const String:format[]="", any:...);
   656 
   657 /**
   658  * Immediately disconnects a client from the server.
   659  *
   660  * Kicking clients from certain events or callbacks may cause crashes.  If in 
   661  * doubt, create a short (0.1 second) timer to kick the client in the next 
   662  * available frame.
   663  *
   664  * @param client		Client index.
   665  * @param format		Optional formatting rules for disconnect reason.
   666  *                      Note that a period is automatically appended to the string by the engine.
   667  * @param ...			Variable number of format parameters.
   668  * @noreturn
   669  * @error				Invalid client index, or client not connected.
   670  */
   671 native KickClientEx(client, const String:format[]="", any:...);
   672 
   673 /**
   674  * Changes a client's team through the mod's generic team changing function.
   675  * On CS:S, this will kill the player.
   676  *
   677  * @param client		Client index.
   678  * @param team			Mod-specific team index.
   679  * @noreturn
   680  * @error				Invalid client index, client not connected, or lack of 
   681  *						mod support.
   682  */
   683 native ChangeClientTeam(client, team);
   684