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