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