core/smn_halflife.cpp
author Scott Ehlert <ds@alliedmods.net>
Tue Jul 27 17:32:32 2010 -0500 (2010-07-27)
changeset 3099 1b7bf8509625
parent 2889 b03b766cc1ab
child 3180 61db674548aa
permissions -rw-r--r--
Added support for Alien Swarm (bug 4530, r=dvander).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2010 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "sm_globals.h"
    33 #include "sourcemod.h"
    34 #include "sourcemm_api.h"
    35 #include "HandleSys.h"
    36 #include "PlayerManager.h"
    37 #include "HalfLife2.h"
    38 
    39 static cell_t SetRandomSeed(IPluginContext *pContext, const cell_t *params)
    40 {
    41 	engrandom->SetSeed(params[1]);
    42 
    43 	return 1;
    44 }
    45 
    46 static cell_t GetRandomFloat(IPluginContext *pContext, const cell_t *params)
    47 {
    48 	float fMin = sp_ctof(params[1]);
    49 	float fMax = sp_ctof(params[2]);
    50 
    51 	float fRandom = engrandom->RandomFloat(fMin, fMax);
    52 
    53 	return sp_ftoc(fRandom);
    54 }
    55 
    56 static cell_t GetRandomInt(IPluginContext *pContext, const cell_t *params)
    57 {
    58 	return engrandom->RandomInt(params[1], params[2]);
    59 }
    60 
    61 static cell_t IsMapValid(IPluginContext *pContext, const cell_t *params)
    62 {
    63 	char *map;
    64 	pContext->LocalToString(params[1], &map);
    65 
    66 	return engine->IsMapValid(map);
    67 }
    68 
    69 static cell_t IsDedicatedServer(IPluginContext *pContext, const cell_t *params)
    70 {
    71 	return engine->IsDedicatedServer();
    72 }
    73 
    74 static cell_t GetEngineTime(IPluginContext *pContext, const cell_t *params)
    75 {
    76 #if SOURCE_ENGINE >= SE_LEFT4DEAD2
    77 	float fTime = Plat_FloatTime();
    78 #else
    79 	float fTime = engine->Time();
    80 #endif
    81 	
    82 	return sp_ftoc(fTime);
    83 }
    84 
    85 static cell_t GetGameTime(IPluginContext *pContext, const cell_t *params)
    86 {
    87 	return sp_ftoc(gpGlobals->curtime);
    88 }
    89 
    90 static cell_t CreateFakeClient(IPluginContext *pContext, const cell_t *params)
    91 {
    92 	char *netname;
    93 
    94 	pContext->LocalToString(params[1], &netname);
    95 
    96 	edict_t *pEdict = engine->CreateFakeClient(netname);
    97 
    98 	/* :TODO: does the engine fire forwards for us and whatnot? no idea... */
    99 
   100 	if (!pEdict)
   101 	{
   102 		return 0;
   103 	}
   104 
   105 	return IndexOfEdict(pEdict);
   106 }
   107 
   108 static cell_t SetFakeClientConVar(IPluginContext *pContext, const cell_t *params)
   109 {
   110 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
   111 
   112 	if (!pPlayer)
   113 	{
   114 		return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
   115 	}
   116 
   117 	if (!pPlayer->IsConnected())
   118 	{
   119 		return pContext->ThrowNativeError("Client %d is not connected", params[1]);
   120 	}
   121 
   122 	if (!pPlayer->IsFakeClient())
   123 	{
   124 		return pContext->ThrowNativeError("Client %d is not a fake client", params[1]);
   125 	}
   126 
   127 	char *cvar, *value;
   128 
   129 	pContext->LocalToString(params[2], &cvar);
   130 	pContext->LocalToString(params[3], &value);
   131 
   132 	engine->SetFakeClientConVarValue(pPlayer->GetEdict(), cvar, value);
   133 
   134 	return 1;
   135 }
   136 
   137 static cell_t GetGameDescription(IPluginContext *pContext, const cell_t *params)
   138 {
   139 	const char *description;
   140 
   141 	if (params[3])
   142 	{
   143 		description = gamedll->GetGameDescription();
   144 	} else {
   145 		description = SERVER_CALL(GetGameDescription)();
   146 	}
   147 
   148 	size_t numBytes;
   149 	
   150 	pContext->StringToLocalUTF8(params[1], params[2], description, &numBytes);
   151 
   152 	return numBytes;
   153 }
   154 
   155 static cell_t GetGameFolderName(IPluginContext *pContext, const cell_t *params)
   156 {
   157 	const char *name = g_SourceMod.GetGameFolderName();
   158 	size_t numBytes;
   159 
   160 	pContext->StringToLocalUTF8(params[1], params[2], name, &numBytes);
   161 
   162 	return numBytes;
   163 }
   164 
   165 /* Useless comment to bump the build */
   166 static cell_t GetCurrentMap(IPluginContext *pContext, const cell_t *params)
   167 {
   168 	size_t bytes;
   169 	pContext->StringToLocalUTF8(params[1], params[2], STRING(gpGlobals->mapname), &bytes);
   170 	return bytes;
   171 }
   172 
   173 static cell_t PrecacheModel(IPluginContext *pContext, const cell_t *params)
   174 {
   175 	char *model;
   176 	pContext->LocalToString(params[1], &model);
   177 
   178 	return engine->PrecacheModel(model, params[2] ? true : false);
   179 }
   180 
   181 static cell_t PrecacheSentenceFile(IPluginContext *pContext, const cell_t *params)
   182 {
   183 	char *sentencefile;
   184 	pContext->LocalToString(params[1], &sentencefile);
   185 
   186 	return engine->PrecacheSentenceFile(sentencefile, params[2] ? true : false);
   187 }
   188 
   189 static cell_t PrecacheDecal(IPluginContext *pContext, const cell_t *params)
   190 {
   191 	char *decal;
   192 	pContext->LocalToString(params[1], &decal);
   193 
   194 	return engine->PrecacheDecal(decal, params[2] ? true : false);
   195 }
   196 
   197 static cell_t PrecacheGeneric(IPluginContext *pContext, const cell_t *params)
   198 {
   199 	char *generic;
   200 	pContext->LocalToString(params[1], &generic);
   201 
   202 	return engine->PrecacheGeneric(generic, params[2] ? true : false);
   203 }
   204 
   205 static cell_t IsModelPrecached(IPluginContext *pContext, const cell_t *params)
   206 {
   207 	char *model;
   208 	pContext->LocalToString(params[1], &model);
   209 
   210 	return engine->IsModelPrecached(model) ? 1 : 0;
   211 }
   212 
   213 static cell_t IsDecalPrecached(IPluginContext *pContext, const cell_t *params)
   214 {
   215 	char *decal;
   216 	pContext->LocalToString(params[1], &decal);
   217 
   218 	return engine->IsDecalPrecached(decal) ? 1 : 0;
   219 }
   220 
   221 static cell_t IsGenericPrecached(IPluginContext *pContext, const cell_t *params)
   222 {
   223 	char *generic;
   224 	pContext->LocalToString(params[1], &generic);
   225 
   226 	return engine->IsGenericPrecached(generic) ? 1 : 0;
   227 }
   228 
   229 static cell_t PrecacheSound(IPluginContext *pContext, const cell_t *params)
   230 {
   231 	char *sample;
   232 	pContext->LocalToString(params[1], &sample);
   233 
   234 	return enginesound->PrecacheSound(sample, params[2] ? true : false) ? 1 : 0;
   235 }
   236 
   237 static cell_t IsSoundPrecached(IPluginContext *pContext, const cell_t *params)
   238 {
   239 	char *sample;
   240 	pContext->LocalToString(params[1], &sample);
   241 
   242 	return enginesound->IsSoundPrecached(sample) ? 1 : 0;
   243 }
   244 
   245 static cell_t smn_CreateDialog(IPluginContext *pContext, const cell_t *params)
   246 {
   247 	KeyValues *pKV;
   248 	HandleError herr;
   249 	Handle_t hndl = static_cast<Handle_t>(params[2]);
   250 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
   251 
   252 	if (!pPlayer)
   253 	{
   254 		return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
   255 	}
   256 
   257 	if (!pPlayer->IsInGame())
   258 	{
   259 		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
   260 	}
   261 
   262 	pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
   263 	if (herr != HandleError_None)
   264 	{
   265 		return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   266 	}
   267 
   268 	serverpluginhelpers->CreateMessage(pPlayer->GetEdict(), 
   269 		static_cast<DIALOG_TYPE>(params[3]), 
   270 		pKV, 
   271 		vsp_interface);
   272 
   273 	return 1;
   274 }
   275 
   276 static cell_t PrintToChat(IPluginContext *pContext, const cell_t *params)
   277 {
   278 	int client = params[1];
   279 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   280 
   281 	if (!pPlayer)
   282 	{
   283 		return pContext->ThrowNativeError("Client index %d is invalid", client);
   284 	}
   285 
   286 	if (!pPlayer->IsInGame())
   287 	{
   288 		return pContext->ThrowNativeError("Client %d is not in game", client);
   289 	}
   290 
   291 	g_SourceMod.SetGlobalTarget(client);
   292 
   293 	char buffer[192];
   294 	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
   295 
   296 	/* Check for an error before printing to the client */
   297 	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
   298 	{
   299 		return 0;
   300 	}
   301 
   302 	if (!g_HL2.TextMsg(client, HUD_PRINTTALK, buffer))
   303 	{
   304 		return pContext->ThrowNativeError("Could not send a usermessage");
   305 	}
   306 
   307 	return 1;
   308 }
   309 
   310 static cell_t PrintCenterText(IPluginContext *pContext, const cell_t *params)
   311 {
   312 	int client = params[1];
   313 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   314 
   315 	if (!pPlayer)
   316 	{
   317 		return pContext->ThrowNativeError("Client index %d is invalid", client);
   318 	}
   319 
   320 	if (!pPlayer->IsInGame())
   321 	{
   322 		return pContext->ThrowNativeError("Client %d is not in game", client);
   323 	}
   324 
   325 	g_SourceMod.SetGlobalTarget(client);
   326 
   327 	char buffer[192];
   328 	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
   329 
   330 	/* Check for an error before printing to the client */
   331 	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
   332 	{
   333 		return 0;
   334 	}
   335 
   336 	if (!g_HL2.TextMsg(client, HUD_PRINTCENTER, buffer))
   337 	{
   338 		return pContext->ThrowNativeError("Could not send a usermessage");
   339 	}
   340 
   341 	return 1;
   342 }
   343 
   344 static cell_t PrintHintText(IPluginContext *pContext, const cell_t *params)
   345 {
   346 	int client = params[1];
   347 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   348 
   349 	if (!pPlayer)
   350 	{
   351 		return pContext->ThrowNativeError("Client index %d is invalid", client);
   352 	}
   353 
   354 	if (!pPlayer->IsInGame())
   355 	{
   356 		return pContext->ThrowNativeError("Client %d is not in game", client);
   357 	}
   358 
   359 	g_SourceMod.SetGlobalTarget(client);
   360 
   361 	char buffer[192];
   362 	g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
   363 
   364 	/* Check for an error before printing to the client */
   365 	if (pContext->GetLastNativeError() != SP_ERROR_NONE)
   366 	{
   367 		return 0;
   368 	}
   369 
   370 	if (!g_HL2.HintTextMsg(client, buffer))
   371 	{
   372 		return pContext->ThrowNativeError("Could not send a usermessage");
   373 	}
   374 
   375 	return 1;
   376 }
   377 
   378 static cell_t ShowVGUIPanel(IPluginContext *pContext, const cell_t *params)
   379 {
   380 	HandleError herr;
   381 	char *name;
   382 	KeyValues *pKV = NULL;
   383 	int client = params[1];
   384 	Handle_t hndl = static_cast<Handle_t>(params[3]);
   385 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   386 
   387 	if (!pPlayer)
   388 	{
   389 		return pContext->ThrowNativeError("Client index %d is invalid", client);
   390 	}
   391 
   392 	if (!pPlayer->IsInGame())
   393 	{
   394 		return pContext->ThrowNativeError("Client %d is not in game", client);
   395 	}
   396 
   397 	if (hndl != BAD_HANDLE)
   398 	{
   399 		pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
   400 		if (herr != HandleError_None)
   401 		{
   402 			return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   403 		}
   404 	}
   405 
   406 	pContext->LocalToString(params[2], &name);
   407 
   408 	if (!g_HL2.ShowVGUIMenu(client, name, pKV, (params[4]) ? true : false))
   409 	{
   410 		return pContext->ThrowNativeError("Could not send a usermessage");
   411 	}
   412 
   413 	return 1;
   414 }
   415 
   416 static cell_t smn_IsPlayerAlive(IPluginContext *pContext, const cell_t *params)
   417 {
   418 	CPlayer *player = g_Players.GetPlayerByIndex(params[1]);
   419 	if (player == NULL)
   420 	{
   421 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
   422 	}
   423 	else if (!player->IsInGame())
   424 	{
   425 		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
   426 	}
   427 
   428 	unsigned int state = player->GetLifeState();
   429 
   430 	if (state == PLAYER_LIFE_UNKNOWN)
   431 	{
   432 		return pContext->ThrowNativeError("\"IsPlayerAlive\" not supported by this mod");
   433 	}
   434 	else if (state == PLAYER_LIFE_ALIVE)
   435 	{
   436 		return 1;
   437 	}
   438 	else
   439 	{
   440 		return 0;
   441 	}
   442 }
   443 
   444 static cell_t GuessSDKVersion(IPluginContext *pContext, const cell_t *params)
   445 {
   446 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
   447 	int version = g_SMAPI->GetSourceEngineBuild();
   448 
   449 	switch (version)
   450 	{
   451 	case SOURCE_ENGINE_ORIGINAL:
   452 		return 10;
   453 	case SOURCE_ENGINE_EPISODEONE:
   454 		return 20;
   455 
   456 # if defined METAMOD_PLAPI_VERSION
   457 	/* Newer games. */
   458 	case SOURCE_ENGINE_DARKMESSIAH:
   459 		return 15;
   460 	case SOURCE_ENGINE_ORANGEBOX:
   461 		return 30;
   462 	case SOURCE_ENGINE_ORANGEBOXVALVE:
   463 		return 35;
   464 	case SOURCE_ENGINE_LEFT4DEAD:
   465 		return 40;
   466 	case SOURCE_ENGINE_LEFT4DEAD2:
   467 		return 50;
   468 	case SOURCE_ENGINE_ALIENSWARM:
   469 		return 60;
   470 # endif
   471 	}
   472 #else
   473 	if (g_HL2.IsOriginalEngine())
   474 	{
   475 		return 10;
   476 	}
   477 	else
   478 	{
   479 		return 20;
   480 	}
   481 #endif
   482 
   483 	return 0;
   484 }
   485 
   486 static cell_t IndexToReference(IPluginContext *pContext, const cell_t *params)
   487 {
   488 	if (params[1] >= NUM_ENT_ENTRIES || params[1] < 0)
   489 	{
   490 		return pContext->ThrowNativeError("Invalid entity index %i", params[1]);
   491 	}
   492 
   493 	return g_HL2.IndexToReference(params[1]);
   494 }
   495 
   496 static cell_t ReferenceToIndex(IPluginContext *pContext, const cell_t *params)
   497 {
   498 	return g_HL2.ReferenceToIndex(params[1]);
   499 }
   500 
   501 static cell_t ReferenceToBCompatRef(IPluginContext *pContext, const cell_t *params)
   502 {
   503 	return g_HL2.ReferenceToBCompatRef(params[1]);
   504 }
   505 
   506 REGISTER_NATIVES(halflifeNatives)
   507 {
   508 	{"CreateFakeClient",		CreateFakeClient},
   509 	{"GetCurrentMap",			GetCurrentMap},
   510 	{"GetEngineTime",			GetEngineTime},
   511 	{"GetGameDescription",		GetGameDescription},
   512 	{"GetGameFolderName",		GetGameFolderName},
   513 	{"GetGameTime",				GetGameTime},
   514 	{"GetRandomFloat",			GetRandomFloat},
   515 	{"GetRandomInt",			GetRandomInt},
   516 	{"IsDedicatedServer",		IsDedicatedServer},
   517 	{"IsMapValid",				IsMapValid},
   518 	{"SetFakeClientConVar",		SetFakeClientConVar},
   519 	{"SetRandomSeed",			SetRandomSeed},
   520 	{"PrecacheModel",			PrecacheModel},
   521 	{"PrecacheSentenceFile",	PrecacheSentenceFile},
   522 	{"PrecacheDecal",			PrecacheDecal},
   523 	{"PrecacheGeneric",			PrecacheGeneric},
   524 	{"IsModelPrecached",		IsModelPrecached},
   525 	{"IsDecalPrecached",		IsDecalPrecached},
   526 	{"IsGenericPrecached",		IsGenericPrecached},
   527 	{"PrecacheSound",			PrecacheSound},
   528 	{"IsSoundPrecached",		IsSoundPrecached},
   529 	{"CreateDialog",			smn_CreateDialog},
   530 	{"PrintToChat",				PrintToChat},
   531 	{"PrintCenterText",			PrintCenterText},
   532 	{"PrintHintText",			PrintHintText},
   533 	{"ShowVGUIPanel",			ShowVGUIPanel},
   534 	{"IsPlayerAlive",			smn_IsPlayerAlive},
   535 	{"GuessSDKVersion",			GuessSDKVersion},
   536 	{"EntIndexToEntRef",		IndexToReference},
   537 	{"EntRefToEntIndex",		ReferenceToIndex},
   538 	{"MakeCompatEntRef",		ReferenceToBCompatRef},
   539 	{NULL,						NULL},
   540 };