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