3 * =============================================================================
5 * Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
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"
39 static cell_t SetRandomSeed(IPluginContext *pContext, const cell_t *params)
41 engrandom->SetSeed(params[1]);
46 static cell_t GetRandomFloat(IPluginContext *pContext, const cell_t *params)
48 float fMin = sp_ctof(params[1]);
49 float fMax = sp_ctof(params[2]);
51 float fRandom = engrandom->RandomFloat(fMin, fMax);
53 return sp_ftoc(fRandom);
56 static cell_t GetRandomInt(IPluginContext *pContext, const cell_t *params)
58 return engrandom->RandomInt(params[1], params[2]);
61 static cell_t IsMapValid(IPluginContext *pContext, const cell_t *params)
64 pContext->LocalToString(params[1], &map);
66 return engine->IsMapValid(map);
69 static cell_t IsDedicatedServer(IPluginContext *pContext, const cell_t *params)
71 return engine->IsDedicatedServer();
74 static cell_t GetEngineTime(IPluginContext *pContext, const cell_t *params)
76 #if SOURCE_ENGINE >= SE_LEFT4DEAD2
77 float fTime = Plat_FloatTime();
79 float fTime = engine->Time();
82 return sp_ftoc(fTime);
85 static cell_t GetGameTime(IPluginContext *pContext, const cell_t *params)
87 return sp_ftoc(gpGlobals->curtime);
90 static cell_t CreateFakeClient(IPluginContext *pContext, const cell_t *params)
92 if (!g_SourceMod.IsMapRunning())
94 return pContext->ThrowNativeError("Cannot create fakeclient when no map is active");
99 pContext->LocalToString(params[1], &netname);
101 edict_t *pEdict = engine->CreateFakeClient(netname);
103 /* :TODO: does the engine fire forwards for us and whatnot? no idea... */
110 return IndexOfEdict(pEdict);
113 static cell_t SetFakeClientConVar(IPluginContext *pContext, const cell_t *params)
115 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
119 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
122 if (!pPlayer->IsConnected())
124 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
127 if (!pPlayer->IsFakeClient())
129 return pContext->ThrowNativeError("Client %d is not a fake client", params[1]);
134 pContext->LocalToString(params[2], &cvar);
135 pContext->LocalToString(params[3], &value);
137 engine->SetFakeClientConVarValue(pPlayer->GetEdict(), cvar, value);
142 static cell_t GetGameDescription(IPluginContext *pContext, const cell_t *params)
144 const char *description;
148 description = gamedll->GetGameDescription();
150 description = SERVER_CALL(GetGameDescription)();
155 pContext->StringToLocalUTF8(params[1], params[2], description, &numBytes);
160 static cell_t GetGameFolderName(IPluginContext *pContext, const cell_t *params)
162 const char *name = g_SourceMod.GetGameFolderName();
165 pContext->StringToLocalUTF8(params[1], params[2], name, &numBytes);
170 /* Useless comment to bump the build */
171 static cell_t GetCurrentMap(IPluginContext *pContext, const cell_t *params)
174 pContext->StringToLocalUTF8(params[1], params[2], STRING(gpGlobals->mapname), &bytes);
178 static cell_t PrecacheModel(IPluginContext *pContext, const cell_t *params)
181 pContext->LocalToString(params[1], &model);
183 return engine->PrecacheModel(model, params[2] ? true : false);
186 static cell_t PrecacheSentenceFile(IPluginContext *pContext, const cell_t *params)
189 pContext->LocalToString(params[1], &sentencefile);
191 return engine->PrecacheSentenceFile(sentencefile, params[2] ? true : false);
194 static cell_t PrecacheDecal(IPluginContext *pContext, const cell_t *params)
197 pContext->LocalToString(params[1], &decal);
199 return engine->PrecacheDecal(decal, params[2] ? true : false);
202 static cell_t PrecacheGeneric(IPluginContext *pContext, const cell_t *params)
205 pContext->LocalToString(params[1], &generic);
207 return engine->PrecacheGeneric(generic, params[2] ? true : false);
210 static cell_t IsModelPrecached(IPluginContext *pContext, const cell_t *params)
213 pContext->LocalToString(params[1], &model);
215 return engine->IsModelPrecached(model) ? 1 : 0;
218 static cell_t IsDecalPrecached(IPluginContext *pContext, const cell_t *params)
221 pContext->LocalToString(params[1], &decal);
223 return engine->IsDecalPrecached(decal) ? 1 : 0;
226 static cell_t IsGenericPrecached(IPluginContext *pContext, const cell_t *params)
229 pContext->LocalToString(params[1], &generic);
231 return engine->IsGenericPrecached(generic) ? 1 : 0;
234 static cell_t PrecacheSound(IPluginContext *pContext, const cell_t *params)
237 pContext->LocalToString(params[1], &sample);
239 return enginesound->PrecacheSound(sample, params[2] ? true : false) ? 1 : 0;
242 static cell_t IsSoundPrecached(IPluginContext *pContext, const cell_t *params)
245 pContext->LocalToString(params[1], &sample);
247 return enginesound->IsSoundPrecached(sample) ? 1 : 0;
250 static cell_t smn_CreateDialog(IPluginContext *pContext, const cell_t *params)
254 Handle_t hndl = static_cast<Handle_t>(params[2]);
255 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
259 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
262 if (!pPlayer->IsInGame())
264 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
267 pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
268 if (herr != HandleError_None)
270 return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
273 serverpluginhelpers->CreateMessage(pPlayer->GetEdict(),
274 static_cast<DIALOG_TYPE>(params[3]),
281 static cell_t PrintToChat(IPluginContext *pContext, const cell_t *params)
283 int client = params[1];
284 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
288 return pContext->ThrowNativeError("Client index %d is invalid", client);
291 if (!pPlayer->IsInGame())
293 return pContext->ThrowNativeError("Client %d is not in game", client);
296 g_SourceMod.SetGlobalTarget(client);
299 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
301 /* Check for an error before printing to the client */
302 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
307 if (!g_HL2.TextMsg(client, HUD_PRINTTALK, buffer))
309 return pContext->ThrowNativeError("Could not send a usermessage");
315 static cell_t PrintCenterText(IPluginContext *pContext, const cell_t *params)
317 int client = params[1];
318 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
322 return pContext->ThrowNativeError("Client index %d is invalid", client);
325 if (!pPlayer->IsInGame())
327 return pContext->ThrowNativeError("Client %d is not in game", client);
330 g_SourceMod.SetGlobalTarget(client);
333 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
335 /* Check for an error before printing to the client */
336 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
341 if (!g_HL2.TextMsg(client, HUD_PRINTCENTER, buffer))
343 return pContext->ThrowNativeError("Could not send a usermessage");
349 static cell_t PrintHintText(IPluginContext *pContext, const cell_t *params)
351 int client = params[1];
352 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
356 return pContext->ThrowNativeError("Client index %d is invalid", client);
359 if (!pPlayer->IsInGame())
361 return pContext->ThrowNativeError("Client %d is not in game", client);
364 g_SourceMod.SetGlobalTarget(client);
367 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
369 /* Check for an error before printing to the client */
370 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
375 if (!g_HL2.HintTextMsg(client, buffer))
377 return pContext->ThrowNativeError("Could not send a usermessage");
383 static cell_t ShowVGUIPanel(IPluginContext *pContext, const cell_t *params)
387 KeyValues *pKV = NULL;
388 int client = params[1];
389 Handle_t hndl = static_cast<Handle_t>(params[3]);
390 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
394 return pContext->ThrowNativeError("Client index %d is invalid", client);
397 if (!pPlayer->IsInGame())
399 return pContext->ThrowNativeError("Client %d is not in game", client);
402 if (hndl != BAD_HANDLE)
404 pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
405 if (herr != HandleError_None)
407 return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
411 pContext->LocalToString(params[2], &name);
413 if (!g_HL2.ShowVGUIMenu(client, name, pKV, (params[4]) ? true : false))
415 return pContext->ThrowNativeError("Could not send a usermessage");
421 static cell_t smn_IsPlayerAlive(IPluginContext *pContext, const cell_t *params)
423 CPlayer *player = g_Players.GetPlayerByIndex(params[1]);
426 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
428 else if (!player->IsInGame())
430 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
433 unsigned int state = player->GetLifeState();
435 if (state == PLAYER_LIFE_UNKNOWN)
437 return pContext->ThrowNativeError("\"IsPlayerAlive\" not supported by this mod");
439 else if (state == PLAYER_LIFE_ALIVE)
449 static cell_t GuessSDKVersion(IPluginContext *pContext, const cell_t *params)
451 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
452 int version = g_SMAPI->GetSourceEngineBuild();
456 case SOURCE_ENGINE_ORIGINAL:
458 case SOURCE_ENGINE_EPISODEONE:
461 # if defined METAMOD_PLAPI_VERSION
463 case SOURCE_ENGINE_DARKMESSIAH:
465 case SOURCE_ENGINE_ORANGEBOX:
467 case SOURCE_ENGINE_BLOODYGOODTIME:
469 case SOURCE_ENGINE_EYE:
471 case SOURCE_ENGINE_ORANGEBOXVALVE:
473 case SOURCE_ENGINE_LEFT4DEAD:
475 case SOURCE_ENGINE_LEFT4DEAD2:
477 case SOURCE_ENGINE_ALIENSWARM:
482 if (g_HL2.IsOriginalEngine())
495 static cell_t IndexToReference(IPluginContext *pContext, const cell_t *params)
497 if (params[1] >= NUM_ENT_ENTRIES || params[1] < 0)
499 return pContext->ThrowNativeError("Invalid entity index %i", params[1]);
502 return g_HL2.IndexToReference(params[1]);
505 static cell_t ReferenceToIndex(IPluginContext *pContext, const cell_t *params)
507 return g_HL2.ReferenceToIndex(params[1]);
510 static cell_t ReferenceToBCompatRef(IPluginContext *pContext, const cell_t *params)
512 return g_HL2.ReferenceToBCompatRef(params[1]);
515 REGISTER_NATIVES(halflifeNatives)
517 {"CreateFakeClient", CreateFakeClient},
518 {"GetCurrentMap", GetCurrentMap},
519 {"GetEngineTime", GetEngineTime},
520 {"GetGameDescription", GetGameDescription},
521 {"GetGameFolderName", GetGameFolderName},
522 {"GetGameTime", GetGameTime},
523 {"GetRandomFloat", GetRandomFloat},
524 {"GetRandomInt", GetRandomInt},
525 {"IsDedicatedServer", IsDedicatedServer},
526 {"IsMapValid", IsMapValid},
527 {"SetFakeClientConVar", SetFakeClientConVar},
528 {"SetRandomSeed", SetRandomSeed},
529 {"PrecacheModel", PrecacheModel},
530 {"PrecacheSentenceFile", PrecacheSentenceFile},
531 {"PrecacheDecal", PrecacheDecal},
532 {"PrecacheGeneric", PrecacheGeneric},
533 {"IsModelPrecached", IsModelPrecached},
534 {"IsDecalPrecached", IsDecalPrecached},
535 {"IsGenericPrecached", IsGenericPrecached},
536 {"PrecacheSound", PrecacheSound},
537 {"IsSoundPrecached", IsSoundPrecached},
538 {"CreateDialog", smn_CreateDialog},
539 {"PrintToChat", PrintToChat},
540 {"PrintCenterText", PrintCenterText},
541 {"PrintHintText", PrintHintText},
542 {"ShowVGUIPanel", ShowVGUIPanel},
543 {"IsPlayerAlive", smn_IsPlayerAlive},
544 {"GuessSDKVersion", GuessSDKVersion},
545 {"EntIndexToEntRef", IndexToReference},
546 {"EntRefToEntIndex", ReferenceToIndex},
547 {"MakeCompatEntRef", ReferenceToBCompatRef},