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