plugins/include/halflife.inc
author Nicholas Hastings <skamonkey@gmail.com>
Mon Feb 08 19:57:20 2010 -0800 (2010-02-08)
changeset 2914 bf5f9aa53abe
parent 2894 8b7300d96395
child 2920 1d5645ab7ae0
permissions -rw-r--r--
Fixed FindEntityByClassname crash in Synergy (bug 4235, r=dvander).
     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 _halflife_included
    34  #endinput
    35 #endif
    36 #define _halflife_included
    37 
    38 #define SOURCE_SDK_UNKNOWN			0		/**< Could not determine the engine version */
    39 #define SOURCE_SDK_ORIGINAL			10		/**< Original Source engine (still used by "The Ship") */
    40 #define SOURCE_SDK_DARKMESSIAH		15		/**< Modified version of original engine used by Dark Messiah (no SDK) */
    41 #define SOURCE_SDK_EPISODE1			20		/**< SDK+Engine released after Episode 1 */
    42 #define SOURCE_SDK_EPISODE2			30		/**< SDK+Engine released after Episode 2/Orange Box */
    43 #define SOURCE_SDK_EPISODE2VALVE	35		/**< SDK+Engine released after Episode 2/Orange Box */
    44 #define SOURCE_SDK_LEFT4DEAD		40		/**< Engine released after Left 4 Dead (no SDK yet) */
    45 #define SOURCE_SDK_LEFT4DEAD2		50		/**< Engine released after Left 4 Dead 2 (no SDK yet) */
    46 
    47 
    48 #define MOTDPANEL_TYPE_TEXT		0	/**< Treat msg as plain text */
    49 #define MOTDPANEL_TYPE_INDEX	1	/**< Msg is auto determined by the engine */
    50 #define MOTDPANEL_TYPE_URL		2	/**< Treat msg as an URL link */
    51 #define MOTDPANEL_TYPE_FILE		3	/**< Treat msg as a filename to be openned */
    52 
    53 enum DialogType
    54 {
    55 	DialogType_Msg = 0,		/**< just an on screen message */
    56 	DialogType_Menu, 		/**< an options menu */
    57 	DialogType_Text, 		/**< a richtext dialog */
    58 	DialogType_Entry, 		/**< an entry box */
    59 	DialogType_AskConnect	/**< ask the client to connect to a specified IP */
    60 };
    61 
    62 #define INVALID_ENT_REFERENCE 0xFFFFFFFF
    63 
    64 /**
    65  * Logs a generic message to the HL2 logs.
    66  *
    67  * @param format		String format.
    68  * @param ...			Format arguments.
    69  * @noreturn
    70  */
    71 native LogToGame(const String:format[], any:...);
    72 
    73 /**
    74  * Sets the seed value for the global Half-Life 2 Random Stream.
    75  *
    76  * @param seed			Seed value.
    77  * @noreturn	
    78  */
    79 native SetRandomSeed(seed);
    80 
    81 /**
    82  * Returns a random floating point number from the Half-Life 2 Random Stream.
    83  *
    84  * @param fMin			Minimum random bound.
    85  * @param fMax			Maximum random bound.
    86  * @return				A random number between (inclusive) fMin and fMax.
    87  */
    88 native Float:GetRandomFloat(Float:fMin=0.0, Float:fMax=1.0);
    89 
    90 /**
    91  * Returns a random number from the Half-Life 2 Random Stream.
    92  *
    93  * @param nmin			Minimum random bound.
    94  * @param nmax			Maximum random bound.
    95  * @return				A random number between (inclusive) nmin and nmax.
    96  */
    97 native GetRandomInt(nmin, nmax);
    98 
    99 /**
   100  * Returns whether a map is valid or not.
   101  * 
   102  * @param 				Map name, excluding .bsp extension.
   103  * @return				True if valid, false otherwise.
   104  */
   105 native bool:IsMapValid(const String:map[]);
   106 
   107 /**
   108  * Returns whether the server is dedicated.
   109  *
   110  * @return				True if dedicated, false otherwise.
   111  */
   112 native bool:IsDedicatedServer();
   113 
   114 /**
   115  * Returns a high-precision time value for profiling the engine.
   116  *
   117  * @return				A floating point time value.
   118  */
   119 native Float:GetEngineTime();
   120 
   121 /** 
   122  * Returns the game time based on the game tick.
   123  *
   124  * @return				Game tick time.
   125  */
   126 native Float:GetGameTime();
   127 
   128 /**
   129  * Returns the game description from the mod.
   130  *
   131  * @param buffer		Buffer to store the description.
   132  * @param maxlength		Maximum size of the buffer.
   133  * @param original		If true, retrieves the original game description,
   134  *						ignoring any potential hooks from plugins.
   135  * @return				Number of bytes written to the buffer (UTF-8 safe).
   136  */
   137 native GetGameDescription(String:buffer[], maxlength, bool:original=false);
   138 
   139 /**
   140  * Returns the name of the game's directory.
   141  *
   142  * @param buffer		Buffer to store the directory name.
   143  * @param maxlength		Maximum size of the buffer.
   144  *
   145  * return				Number of bytes written to the buffer (UTF-8 safe).
   146  */
   147 native GetGameFolderName(String:buffer[], maxlength);
   148 
   149 /**
   150  * Returns the current map name.
   151  *
   152  * @param buffer		Buffer to store map name.
   153  * @param maxlength		Maximum length of buffer.
   154  * @return				Number of bytes written (UTF-8 safe).
   155  */
   156 native GetCurrentMap(String:buffer[], maxlength);
   157 
   158 /**
   159  * Precaches a given model.
   160  *
   161  * @param model			Name of the model to precache.
   162  * @param preload		If preload is true the file will be precached before level startup.
   163  * @return				Returns the model index, 0 for error.
   164  */
   165 native PrecacheModel(const String:model[], bool:preload=false);
   166 
   167 /**
   168  * Precaches a given sentence file.
   169  *
   170  * @param file			Name of the sentence file to precache.
   171  * @param preload		If preload is true the file will be precached before level startup.
   172  * @return				Returns a sentence file index.
   173  */
   174 native PrecacheSentenceFile(const String:file[], bool:preload=false);
   175 
   176 /**
   177  * Precaches a given decal.
   178  *
   179  * @param decal			Name of the decal to precache.
   180  * @param preload		If preload is true the file will be precached before level startup.
   181  * @return				Returns a decal index.
   182  */
   183 native PrecacheDecal(const String:decal[], bool:preload=false);
   184 
   185 /**
   186  * Precaches a given generic file.
   187  *
   188  * @param generic		Name of the generic file to precache.
   189  * @param preload		If preload is true the file will be precached before level startup.
   190  * @return				Returns a generic file index.
   191  */
   192 native PrecacheGeneric(const String:generic[], bool:preload=false);
   193 
   194 /**
   195  * Returns if a given model is precached.
   196  *
   197  * @param model			Name of the model to check.
   198  * @return				True if precached, false otherwise.
   199  */
   200 native bool:IsModelPrecached(const String:model[]);
   201 
   202 /**
   203  * Returns if a given decal is precached.
   204  *
   205  * @param decal			Name of the decal to check.
   206  * @return				True if precached, false otherwise.
   207  */
   208 native bool:IsDecalPrecached(const String:decal[]);
   209 
   210 /**
   211  * Returns if a given generic file is precached.
   212  *
   213  * @param decal			Name of the generic file to check.
   214  * @return				True if precached, false otherwise.
   215  */
   216 native bool:IsGenericPrecached(const String:generic[]);
   217 
   218 /**
   219  * Precaches a given sound.
   220  *
   221  * @param sound			Name of the sound to precache.
   222  * @param preload		If preload is true the file will be precached before level startup.
   223  * @return				True if successfully precached, false otherwise.
   224  */
   225 native bool:PrecacheSound(const String:sound[], bool:preload=false);
   226 
   227 /**
   228  * Returns if a given sound is precached.
   229  *
   230  * @param sound			Name of the sound to check.
   231  * @return				True if precached, false otherwise.
   232  */
   233 native bool:IsSoundPrecached(const String:sound[]);
   234 
   235 /**
   236  * Creates different types of ingame messages.
   237  *
   238  * @param client		Index of the client.
   239  * @param kv			KeyValues handle to set the menu keys and options. (Check iserverplugin.h for more information).
   240  * @param type			Message type to display ingame.
   241  * @noreturn
   242  * @error				Invalid client index, or client not connected.
   243  */
   244 native CreateDialog(client, Handle:kv, DialogType:type);
   245 
   246 /**
   247  * Guesses the SDK version a mod was compiled against.  If nothing 
   248  * specific is known about the game, the engine version is used instead.
   249  *
   250  * The return values are guaranteed to increase chronologically (that is, 
   251  * a later release will have a higher value).
   252  *
   253  * @return				SOURCE_SDK version code.
   254  */
   255 native GuessSDKVersion();
   256 
   257 /**
   258  * Prints a message to a specific client in the chat area.
   259  *
   260  * @param client		Client index.
   261  * @param format		Formatting rules.
   262  * @param ...			Variable number of format parameters.
   263  * @noreturn
   264  * @error				If the client is not connected an error will be thrown.
   265  */
   266 native PrintToChat(client, const String:format[], any:...);
   267 
   268 /**
   269  * Prints a message to all clients in the chat area.
   270  *
   271  * @param format		Formatting rules.
   272  * @param ...			Variable number of format parameters.
   273  * @noreturn
   274  */
   275 stock PrintToChatAll(const String:format[], any:...)
   276 {
   277 	new maxClients = GetMaxClients();
   278 	decl String:buffer[192];
   279 	
   280 	for (new i = 1; i <= maxClients; i++)
   281 	{
   282 		if (IsClientInGame(i))
   283 		{
   284 			SetGlobalTransTarget(i);
   285 			VFormat(buffer, sizeof(buffer), format, 2);
   286 			PrintToChat(i, "%s", buffer);
   287 		}
   288 	}
   289 }
   290 
   291 /**
   292  * Prints a message to a specific client in the center of the screen.
   293  *
   294  * @param client		Client index.
   295  * @param format		Formatting rules.
   296  * @param ...			Variable number of format parameters.
   297  * @noreturn
   298  * @error				If the client is not connected an error will be thrown.
   299  */
   300 native PrintCenterText(client, const String:format[], any:...);
   301 
   302 /**
   303  * Prints a message to all clients in the center of the screen.
   304  *
   305  * @param format		Formatting rules.
   306  * @param ...			Variable number of format parameters.
   307  * @noreturn
   308  */
   309 stock PrintCenterTextAll(const String:format[], any:...)
   310 {
   311 	new maxClients = GetMaxClients();
   312 	decl String:buffer[192];
   313 
   314 	for (new i = 1; i <= maxClients; i++)
   315 	{
   316 		if (IsClientInGame(i))
   317 		{
   318 			SetGlobalTransTarget(i);
   319 			VFormat(buffer, sizeof(buffer), format, 2);
   320 			PrintCenterText(i, "%s", buffer);
   321 		}
   322 	}
   323 }
   324 
   325 /**
   326  * Prints a message to a specific client with a hint box.
   327  *
   328  * @param client		Client index.
   329  * @param format		Formatting rules.
   330  * @param ...			Variable number of format parameters.
   331  * @noreturn
   332  * @error				If the client is not connected an error will be thrown.
   333  */
   334 native PrintHintText(client, const String:format[], any:...);
   335 
   336 /**
   337  * Prints a message to all clients with a hint box.
   338  *
   339  * @param format		Formatting rules.
   340  * @param ...			Variable number of format parameters.
   341  * @noreturn
   342  */
   343 stock PrintHintTextToAll(const String:format[], any:...)
   344 {
   345 	new maxClients = GetMaxClients();
   346 	decl String:buffer[192];
   347 	
   348 	for (new i = 1; i <= maxClients; i++)
   349 	{
   350 		if (IsClientInGame(i))
   351 		{
   352 			SetGlobalTransTarget(i);
   353 			VFormat(buffer, sizeof(buffer), format, 2);
   354 			PrintHintText(i, "%s", buffer);
   355 		}
   356 	}
   357 }
   358 
   359 /**
   360  * Shows a VGUI panel to a specific client.
   361  *
   362  * @param client		Client index.
   363  * @param name			Panel type name (Check viewport_panel_names.h to see a list of 
   364  *						some panel names).
   365  * @param Kv			KeyValues handle with all the data for the panel setup (Depends 
   366  *						on the panel type and may be unused).
   367  * @param show			True to show the panel, or false to remove it from the client screen.
   368  * @noreturn
   369  * @error				If the client is not connected an error will be thrown.
   370  */
   371 native ShowVGUIPanel(client, const String:name[], Handle:Kv=INVALID_HANDLE, bool:show=true);
   372 
   373 /**
   374  * Creates a HUD synchronization object.  This object is used to automatically assign and 
   375  * re-use channels for a set of messages.  
   376  *
   377  * The HUD has a hardcoded number of channels (usually 6) for displaying 
   378  * text.  You can use any channel for any area of the screen.  Text on 
   379  * different channels can overlap, but text on the same channel will 
   380  * erase the old text first.  This overlapping and overwriting gets problematic.
   381  *
   382  * A HUD synchronization object automatically selects channels for you based on 
   383  * the following heuristics:
   384  *  - If channel X was last used by the object, and hasn't been modified again, 
   385  *    channel X gets re-used.
   386  *  - Otherwise, a new channel is chosen based on the least-recently-used channel.
   387  *
   388  * This ensures that if you display text on a sync object, that the previous text 
   389  * displayed on it will always be cleared first.  This is because your new text 
   390  * will either overwrite the old text on the same channel, or because another 
   391  * channel has already erased your text.
   392  *
   393  * Note that messages can still overlap if they are on different synchronization 
   394  * objects, or they are displayed to manual channels.
   395  *
   396  * These are particularly useful for displaying repeating or refreshing HUD text, in 
   397  * addition to displaying multiple message sets in one area of the screen (for example, 
   398  * center-say messages that may pop up randomly that you don't want to overlap each 
   399  * other).
   400  *
   401  * @return				New HUD synchronization object.
   402  *						The Handle can be closed with CloseHandle().
   403  *						If HUD text is not supported on this mod, then 
   404  *						INVALID_HANDLE is returned.
   405  */
   406 native Handle:CreateHudSynchronizer();
   407 
   408 /**
   409  * Sets the HUD parameters for drawing text.  These parameters are stored 
   410  * globally, although nothing other than this function and SetHudTextParamsEx 
   411  * modify them.
   412  *
   413  * You must call this function before drawing text.  If you are drawing 
   414  * text to multiple clients, you can set the parameters once, since 
   415  * they won't be modified.  However, as soon as you pass control back 
   416  * to other plugins, you must reset the parameters next time you draw.
   417  *
   418  * @param x				x coordinate, from 0 to 1.  -1.0 is the center.
   419  * @param y				y coordinate, from 0 to 1.  -1.0 is the center.
   420  * @param holdTime		Number of seconds to hold the text.
   421  * @param r				Red color value.
   422  * @param g				Green color value.
   423  * @param b				Blue color value.
   424  * @param a				Alpha transparency value.
   425  * @param effect 		0/1 causes the text to fade in and fade out.
   426  *						2 causes the text to flash[?].
   427  * @param fxTime		Duration of chosen effect (may not apply to all effects).
   428  * @param fadeIn		Number of seconds to spend fading in.
   429  * @param fadeOut		Number of seconds to spend fading out.
   430  * @noreturn
   431  */
   432 native SetHudTextParams(Float:x, Float:y, Float:holdTime, r, g, b, a, effect = 0,
   433 						Float:fxTime=6.0, Float:fadeIn=0.1, Float:fadeOut=0.2);
   434 						
   435 /**
   436  * Sets the HUD parameters for drawing text.  These parameters are stored 
   437  * globally, although nothing other than this function and SetHudTextParams
   438  * modify them.
   439  *
   440  * This is the same as SetHudTextParams(), except it lets you set the alternate 
   441  * color for when effects require it.  
   442  *
   443  * @param x				x coordinate, from 0 to 1.  -1.0 is the center.
   444  * @param y				y coordinate, from 0 to 1.  -1.0 is the center.
   445  * @param holdTime		Number of seconds to hold the text.
   446  * @param color1		First color set, array values being [red, green, blue, alpha]
   447  * @param color2		Second color set, array values being [red, green, blue, alpha]
   448  * @param effect 		0/1 causes the text to fade in and fade out.
   449  *						2 causes the text to flash[?].
   450  * @param fxTime		Duration of chosen effect (may not apply to all effects).
   451  * @param fadeIn		Number of seconds to spend fading in.
   452  * @param fadeOut		Number of seconds to spend fading out.
   453  * @noreturn
   454  */
   455 native SetHudTextParamsEx(Float:x, Float:y, Float:holdTime, color1[4], 
   456 						  color2[4]={255,255,255,0}, effect = 0, Float:fxTime=6.0, 
   457 						  Float:fadeIn=0.1, Float:fadeOut=0.2);
   458 
   459 /**
   460  * Shows a synchronized HUD message to a client.  
   461  *
   462  * As of this writing, only TF, HL2MP, and SourceForts support HUD Text.
   463  *
   464  * @param client		Client index to send the message to.
   465  * @param sync			Synchronization object.
   466  * @param message		Message text or formatting rules.
   467  * @param ...			Message formatting parameters.
   468  * @return				-1 on failure, anything else on success.
   469  *						This function fails if the mod does not support it.
   470  * @error				Client not in-game, or sync object not valid.
   471  */
   472 native ShowSyncHudText(client, Handle:sync, const String:message[], any:...);
   473 
   474 /**
   475  * Clears the text on a synchronized HUD channel. 
   476  *
   477  * This is not the same as sending "" because it guarantees that it won't 
   478  * overwrite text on another channel.  For example, consider the scenario: 
   479  *
   480  * 1. Your synchronized message goes to channel 3.
   481  * 2. Someone else's non-synchronized message goes to channel 3.
   482  *
   483  * If you were to simply send "" on your synchronized message, 
   484  * then someone else's text could be overwritten.
   485  *
   486  * @param client		Client index to send the message to.
   487  * @param sync			Synchronization object.
   488  * @noreturn
   489  * @error				Client not in-game, or sync object not valid.
   490  */
   491 native ClearSyncHud(client, Handle:sync);
   492 
   493 /**
   494  * Shows a HUD message to a client on the given channel.
   495  *
   496  * As of this writing, only TF, HL2MP, and SourceForts support HUD Text.
   497  * 
   498  * @param client		Client index to send the message to.
   499  * @param channel		A channel number.
   500  *						If -1, then a channel will automatically be selected 
   501  *						based on the least-recently-used channel.  If the 
   502  *						channel is any other number, it will be modulo'd with 
   503  *						the channel count to get a final channel number.
   504  * @param message		Message text or formatting rules.
   505  * @param ...			Message formatting parameters.
   506  * @return				-1 on failure (lack of mod support).
   507  *						Any other return value is the channel number that was 
   508  *						used to render the text.
   509  */
   510 native ShowHudText(client, channel, const String:message[], any:...);
   511 
   512 /**
   513  * Shows a MOTD panel to a specific client.
   514  *
   515  * @param client		Client index.
   516  * @param title			Title of the panel (printed on the top border of the window).
   517  * @param msg			Contents of the panel, it can be treated as an url, filename or plain text
   518  *				 		depending on the type parameter (WARNING: msg has to be 192 bytes maximum!)
   519  * @param type			Determines the way to treat the message body of the panel.
   520  * @noreturn
   521  * @error				If the client is not connected an error will be thrown.
   522  */
   523 stock ShowMOTDPanel(client, const String:title[], const String:msg[], type=MOTDPANEL_TYPE_INDEX)
   524 {
   525 	decl String:num[3];
   526 	new Handle:Kv = CreateKeyValues("data");
   527 	IntToString(type, num, sizeof(num));
   528 
   529 	KvSetString(Kv, "title", title);
   530 	KvSetString(Kv, "type", num);
   531 	KvSetString(Kv, "msg", msg);
   532 	ShowVGUIPanel(client, "info", Kv);
   533 	CloseHandle(Kv);
   534 }
   535 
   536 /**
   537  * Displays a panel asking the client to connect to a specified IP.
   538  *
   539  * @param client		Client index.
   540  * @param time			Duration to hold the panel on the client's screen.
   541  * @param ip			Destionation IP.
   542  * @noreturn
   543  */
   544 stock DisplayAskConnectBox(client, Float:time, const String:ip[])
   545 {
   546 	new Handle:Kv = CreateKeyValues("data");
   547 	KvSetFloat(Kv, "time", time);
   548 	KvSetString(Kv, "title", ip);
   549 	CreateDialog(client, Kv, DialogType_AskConnect);
   550 	CloseHandle(Kv);
   551 }
   552 
   553 /**
   554  * Converts an entity index into a serial encoded entity reference.
   555  *
   556  * @param entity		Entity index.
   557  * @return				Entity reference.
   558  */
   559 native EntIndexToEntRef(entity);
   560 
   561 /**
   562  * Retrieves the entity index from a reference.
   563  *
   564  * @param ref			Entity reference.
   565  * @return				Entity index.
   566  */
   567 native EntRefToEntIndex(ref);
   568 
   569 /**
   570  * Converts a reference into a backwards compatible version.
   571  *
   572  * @param ref			Entity reference.
   573  * @return				Bcompat reference.
   574  */
   575 native MakeCompatEntRef(ref);
   576