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