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