3 * =============================================================================
5 * Copyright (C) 2004-2008 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)
94 pContext->LocalToString(params[1], &netname);
96 edict_t *pEdict = engine->CreateFakeClient(netname);
98 /* :TODO: does the engine fire forwards for us and whatnot? no idea... */
105 return IndexOfEdict(pEdict);
108 static cell_t SetFakeClientConVar(IPluginContext *pContext, const cell_t *params)
110 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
114 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
117 if (!pPlayer->IsConnected())
119 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
122 if (!pPlayer->IsFakeClient())
124 return pContext->ThrowNativeError("Client %d is not a fake client", params[1]);
129 pContext->LocalToString(params[2], &cvar);
130 pContext->LocalToString(params[3], &value);
132 engine->SetFakeClientConVarValue(pPlayer->GetEdict(), cvar, value);
137 static cell_t GetGameDescription(IPluginContext *pContext, const cell_t *params)
139 const char *description;
143 description = gamedll->GetGameDescription();
145 description = SERVER_CALL(GetGameDescription)();
150 pContext->StringToLocalUTF8(params[1], params[2], description, &numBytes);
155 static cell_t GetGameFolderName(IPluginContext *pContext, const cell_t *params)
157 const char *name = g_SourceMod.GetGameFolderName();
160 pContext->StringToLocalUTF8(params[1], params[2], name, &numBytes);
165 /* Useless comment to bump the build */
166 static cell_t GetCurrentMap(IPluginContext *pContext, const cell_t *params)
169 pContext->StringToLocalUTF8(params[1], params[2], STRING(gpGlobals->mapname), &bytes);
173 static cell_t PrecacheModel(IPluginContext *pContext, const cell_t *params)
176 pContext->LocalToString(params[1], &model);
178 return engine->PrecacheModel(model, params[2] ? true : false);
181 static cell_t PrecacheSentenceFile(IPluginContext *pContext, const cell_t *params)
184 pContext->LocalToString(params[1], &sentencefile);
186 return engine->PrecacheSentenceFile(sentencefile, params[2] ? true : false);
189 static cell_t PrecacheDecal(IPluginContext *pContext, const cell_t *params)
192 pContext->LocalToString(params[1], &decal);
194 return engine->PrecacheDecal(decal, params[2] ? true : false);
197 static cell_t PrecacheGeneric(IPluginContext *pContext, const cell_t *params)
200 pContext->LocalToString(params[1], &generic);
202 return engine->PrecacheGeneric(generic, params[2] ? true : false);
205 static cell_t IsModelPrecached(IPluginContext *pContext, const cell_t *params)
208 pContext->LocalToString(params[1], &model);
210 return engine->IsModelPrecached(model) ? 1 : 0;
213 static cell_t IsDecalPrecached(IPluginContext *pContext, const cell_t *params)
216 pContext->LocalToString(params[1], &decal);
218 return engine->IsDecalPrecached(decal) ? 1 : 0;
221 static cell_t IsGenericPrecached(IPluginContext *pContext, const cell_t *params)
224 pContext->LocalToString(params[1], &generic);
226 return engine->IsGenericPrecached(generic) ? 1 : 0;
229 static cell_t PrecacheSound(IPluginContext *pContext, const cell_t *params)
232 pContext->LocalToString(params[1], &sample);
234 return enginesound->PrecacheSound(sample, params[2] ? true : false) ? 1 : 0;
237 static cell_t IsSoundPrecached(IPluginContext *pContext, const cell_t *params)
240 pContext->LocalToString(params[1], &sample);
242 return enginesound->IsSoundPrecached(sample) ? 1 : 0;
245 static cell_t smn_CreateDialog(IPluginContext *pContext, const cell_t *params)
249 Handle_t hndl = static_cast<Handle_t>(params[2]);
250 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
254 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
257 if (!pPlayer->IsInGame())
259 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
262 pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
263 if (herr != HandleError_None)
265 return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
268 serverpluginhelpers->CreateMessage(pPlayer->GetEdict(),
269 static_cast<DIALOG_TYPE>(params[3]),
276 static cell_t PrintToChat(IPluginContext *pContext, const cell_t *params)
278 int client = params[1];
279 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
283 return pContext->ThrowNativeError("Client index %d is invalid", client);
286 if (!pPlayer->IsInGame())
288 return pContext->ThrowNativeError("Client %d is not in game", client);
291 g_SourceMod.SetGlobalTarget(client);
294 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
296 /* Check for an error before printing to the client */
297 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
302 if (!g_HL2.TextMsg(client, HUD_PRINTTALK, buffer))
304 return pContext->ThrowNativeError("Could not send a usermessage");
310 static cell_t PrintCenterText(IPluginContext *pContext, const cell_t *params)
312 int client = params[1];
313 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
317 return pContext->ThrowNativeError("Client index %d is invalid", client);
320 if (!pPlayer->IsInGame())
322 return pContext->ThrowNativeError("Client %d is not in game", client);
325 g_SourceMod.SetGlobalTarget(client);
328 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
330 /* Check for an error before printing to the client */
331 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
336 if (!g_HL2.TextMsg(client, HUD_PRINTCENTER, buffer))
338 return pContext->ThrowNativeError("Could not send a usermessage");
344 static cell_t PrintHintText(IPluginContext *pContext, const cell_t *params)
346 int client = params[1];
347 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
351 return pContext->ThrowNativeError("Client index %d is invalid", client);
354 if (!pPlayer->IsInGame())
356 return pContext->ThrowNativeError("Client %d is not in game", client);
359 g_SourceMod.SetGlobalTarget(client);
362 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
364 /* Check for an error before printing to the client */
365 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
370 if (!g_HL2.HintTextMsg(client, buffer))
372 return pContext->ThrowNativeError("Could not send a usermessage");
378 static cell_t ShowVGUIPanel(IPluginContext *pContext, const cell_t *params)
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);
389 return pContext->ThrowNativeError("Client index %d is invalid", client);
392 if (!pPlayer->IsInGame())
394 return pContext->ThrowNativeError("Client %d is not in game", client);
397 if (hndl != BAD_HANDLE)
399 pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
400 if (herr != HandleError_None)
402 return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
406 pContext->LocalToString(params[2], &name);
408 if (!g_HL2.ShowVGUIMenu(client, name, pKV, (params[4]) ? true : false))
410 return pContext->ThrowNativeError("Could not send a usermessage");
416 static cell_t smn_IsPlayerAlive(IPluginContext *pContext, const cell_t *params)
418 CPlayer *player = g_Players.GetPlayerByIndex(params[1]);
421 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
423 else if (!player->IsInGame())
425 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
428 unsigned int state = player->GetLifeState();
430 if (state == PLAYER_LIFE_UNKNOWN)
432 return pContext->ThrowNativeError("\"IsPlayerAlive\" not supported by this mod");
434 else if (state == PLAYER_LIFE_ALIVE)
444 static cell_t GuessSDKVersion(IPluginContext *pContext, const cell_t *params)
446 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
447 int version = g_SMAPI->GetSourceEngineBuild();
451 case SOURCE_ENGINE_ORIGINAL:
453 case SOURCE_ENGINE_EPISODEONE:
456 # if defined METAMOD_PLAPI_VERSION
458 case SOURCE_ENGINE_DARKMESSIAH:
460 case SOURCE_ENGINE_ORANGEBOX:
462 case SOURCE_ENGINE_ORANGEBOXVALVE:
464 case SOURCE_ENGINE_LEFT4DEAD:
466 case SOURCE_ENGINE_LEFT4DEAD2:
471 if (g_HL2.IsOriginalEngine())
484 static cell_t IndexToReference(IPluginContext *pContext, const cell_t *params)
486 if (params[1] >= NUM_ENT_ENTRIES || params[1] < 0)
488 return pContext->ThrowNativeError("Invalid entity index %i", params[1]);
491 return g_HL2.IndexToReference(params[1]);
494 static cell_t ReferenceToIndex(IPluginContext *pContext, const cell_t *params)
496 return g_HL2.ReferenceToIndex(params[1]);
499 static cell_t ReferenceToBCompatRef(IPluginContext *pContext, const cell_t *params)
501 return g_HL2.ReferenceToBCompatRef(params[1]);
504 REGISTER_NATIVES(halflifeNatives)
506 {"CreateFakeClient", CreateFakeClient},
507 {"GetCurrentMap", GetCurrentMap},
508 {"GetEngineTime", GetEngineTime},
509 {"GetGameDescription", GetGameDescription},
510 {"GetGameFolderName", GetGameFolderName},
511 {"GetGameTime", GetGameTime},
512 {"GetRandomFloat", GetRandomFloat},
513 {"GetRandomInt", GetRandomInt},
514 {"IsDedicatedServer", IsDedicatedServer},
515 {"IsMapValid", IsMapValid},
516 {"SetFakeClientConVar", SetFakeClientConVar},
517 {"SetRandomSeed", SetRandomSeed},
518 {"PrecacheModel", PrecacheModel},
519 {"PrecacheSentenceFile", PrecacheSentenceFile},
520 {"PrecacheDecal", PrecacheDecal},
521 {"PrecacheGeneric", PrecacheGeneric},
522 {"IsModelPrecached", IsModelPrecached},
523 {"IsDecalPrecached", IsDecalPrecached},
524 {"IsGenericPrecached", IsGenericPrecached},
525 {"PrecacheSound", PrecacheSound},
526 {"IsSoundPrecached", IsSoundPrecached},
527 {"CreateDialog", smn_CreateDialog},
528 {"PrintToChat", PrintToChat},
529 {"PrintCenterText", PrintCenterText},
530 {"PrintHintText", PrintHintText},
531 {"ShowVGUIPanel", ShowVGUIPanel},
532 {"IsPlayerAlive", smn_IsPlayerAlive},
533 {"GuessSDKVersion", GuessSDKVersion},
534 {"EntIndexToEntRef", IndexToReference},
535 {"EntRefToEntIndex", ReferenceToIndex},
536 {"MakeCompatEntRef", ReferenceToBCompatRef},