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 "PlayerManager.h"
36 #include "HalfLife2.h"
38 static cell_t SetRandomSeed(IPluginContext *pContext, const cell_t *params)
40 engrandom->SetSeed(params[1]);
45 static cell_t GetRandomFloat(IPluginContext *pContext, const cell_t *params)
47 float fMin = sp_ctof(params[1]);
48 float fMax = sp_ctof(params[2]);
50 float fRandom = engrandom->RandomFloat(fMin, fMax);
52 return sp_ftoc(fRandom);
55 static cell_t GetRandomInt(IPluginContext *pContext, const cell_t *params)
57 return engrandom->RandomInt(params[1], params[2]);
60 static cell_t IsMapValid(IPluginContext *pContext, const cell_t *params)
63 pContext->LocalToString(params[1], &map);
65 return g_HL2.IsMapValid(map);
68 static cell_t IsDedicatedServer(IPluginContext *pContext, const cell_t *params)
70 return engine->IsDedicatedServer();
73 static cell_t GetEngineTime(IPluginContext *pContext, const cell_t *params)
75 #if SOURCE_ENGINE >= SE_LEFT4DEAD2
76 float fTime = Plat_FloatTime();
78 float fTime = engine->Time();
81 return sp_ftoc(fTime);
84 static cell_t GetGameTime(IPluginContext *pContext, const cell_t *params)
86 return sp_ftoc(gpGlobals->curtime);
89 static cell_t GetGameTickCount(IPluginContext *pContext, const cell_t *params)
91 return gpGlobals->tickcount;
94 static cell_t CreateFakeClient(IPluginContext *pContext, const cell_t *params)
96 if (!g_SourceMod.IsMapRunning())
98 return pContext->ThrowNativeError("Cannot create fakeclient when no map is active");
103 pContext->LocalToString(params[1], &netname);
105 #if SOURCE_ENGINE == SE_DOTA
107 engine->CreateFakeClient(&index, netname);
116 edict_t *pEdict = engine->CreateFakeClient(netname);
118 /* :TODO: does the engine fire forwards for us and whatnot? no idea... */
125 return IndexOfEdict(pEdict);
129 static cell_t SetFakeClientConVar(IPluginContext *pContext, const cell_t *params)
131 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
135 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
138 if (!pPlayer->IsConnected())
140 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
143 if (!pPlayer->IsFakeClient())
145 return pContext->ThrowNativeError("Client %d is not a fake client", params[1]);
150 pContext->LocalToString(params[2], &cvar);
151 pContext->LocalToString(params[3], &value);
153 #if SOURCE_ENGINE == SE_DOTA
154 engine->SetFakeClientConVarValue(pPlayer->GetIndex(), cvar, value);
156 engine->SetFakeClientConVarValue(pPlayer->GetEdict(), cvar, value);
162 static cell_t GetGameDescription(IPluginContext *pContext, const cell_t *params)
164 const char *description;
168 description = gamedll->GetGameDescription();
170 description = SERVER_CALL(GetGameDescription)();
175 pContext->StringToLocalUTF8(params[1], params[2], description, &numBytes);
180 static cell_t GetGameFolderName(IPluginContext *pContext, const cell_t *params)
182 const char *name = g_SourceMod.GetGameFolderName();
185 pContext->StringToLocalUTF8(params[1], params[2], name, &numBytes);
190 /* Useless comment to bump the build */
191 static cell_t GetCurrentMap(IPluginContext *pContext, const cell_t *params)
194 pContext->StringToLocalUTF8(params[1], params[2], STRING(gpGlobals->mapname), &bytes);
198 static cell_t PrecacheModel(IPluginContext *pContext, const cell_t *params)
201 pContext->LocalToString(params[1], &model);
203 return engine->PrecacheModel(model, params[2] ? true : false);
206 static cell_t PrecacheSentenceFile(IPluginContext *pContext, const cell_t *params)
209 pContext->LocalToString(params[1], &sentencefile);
211 return engine->PrecacheSentenceFile(sentencefile, params[2] ? true : false);
214 static cell_t PrecacheDecal(IPluginContext *pContext, const cell_t *params)
217 pContext->LocalToString(params[1], &decal);
219 return engine->PrecacheDecal(decal, params[2] ? true : false);
222 static cell_t PrecacheGeneric(IPluginContext *pContext, const cell_t *params)
225 pContext->LocalToString(params[1], &generic);
227 return engine->PrecacheGeneric(generic, params[2] ? true : false);
230 static cell_t IsModelPrecached(IPluginContext *pContext, const cell_t *params)
233 pContext->LocalToString(params[1], &model);
235 return engine->IsModelPrecached(model) ? 1 : 0;
238 static cell_t IsDecalPrecached(IPluginContext *pContext, const cell_t *params)
241 pContext->LocalToString(params[1], &decal);
243 return engine->IsDecalPrecached(decal) ? 1 : 0;
246 static cell_t IsGenericPrecached(IPluginContext *pContext, const cell_t *params)
249 pContext->LocalToString(params[1], &generic);
251 return engine->IsGenericPrecached(generic) ? 1 : 0;
254 static cell_t PrecacheSound(IPluginContext *pContext, const cell_t *params)
257 pContext->LocalToString(params[1], &sample);
259 return enginesound->PrecacheSound(sample, params[2] ? true : false) ? 1 : 0;
262 static cell_t IsSoundPrecached(IPluginContext *pContext, const cell_t *params)
265 pContext->LocalToString(params[1], &sample);
267 return enginesound->IsSoundPrecached(sample) ? 1 : 0;
270 static cell_t smn_CreateDialog(IPluginContext *pContext, const cell_t *params)
272 #if SOURCE_ENGINE == SE_DOTA
273 return pContext->ThrowNativeError("CreateDialog is not supported on this game");
277 Handle_t hndl = static_cast<Handle_t>(params[2]);
278 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
282 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
285 if (!pPlayer->IsInGame())
287 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
290 pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
291 if (herr != HandleError_None)
293 return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
296 serverpluginhelpers->CreateMessage(pPlayer->GetEdict(),
297 static_cast<DIALOG_TYPE>(params[3]),
305 static cell_t PrintToChat(IPluginContext *pContext, const cell_t *params)
307 int client = params[1];
308 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
312 return pContext->ThrowNativeError("Client index %d is invalid", client);
315 if (!pPlayer->IsInGame())
317 return pContext->ThrowNativeError("Client %d is not in game", client);
320 g_SourceMod.SetGlobalTarget(client);
323 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
325 /* Check for an error before printing to the client */
326 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
331 if (!g_HL2.TextMsg(client, HUD_PRINTTALK, buffer))
333 return pContext->ThrowNativeError("Could not send a usermessage");
339 static cell_t PrintCenterText(IPluginContext *pContext, const cell_t *params)
341 int client = params[1];
342 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
346 return pContext->ThrowNativeError("Client index %d is invalid", client);
349 if (!pPlayer->IsInGame())
351 return pContext->ThrowNativeError("Client %d is not in game", client);
354 g_SourceMod.SetGlobalTarget(client);
357 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
359 /* Check for an error before printing to the client */
360 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
365 if (!g_HL2.TextMsg(client, HUD_PRINTCENTER, buffer))
367 return pContext->ThrowNativeError("Could not send a usermessage");
373 static cell_t PrintHintText(IPluginContext *pContext, const cell_t *params)
375 int client = params[1];
376 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
380 return pContext->ThrowNativeError("Client index %d is invalid", client);
383 if (!pPlayer->IsInGame())
385 return pContext->ThrowNativeError("Client %d is not in game", client);
388 g_SourceMod.SetGlobalTarget(client);
391 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
393 /* Check for an error before printing to the client */
394 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
399 if (!g_HL2.HintTextMsg(client, buffer))
401 return pContext->ThrowNativeError("Could not send a usermessage");
407 static cell_t ShowVGUIPanel(IPluginContext *pContext, const cell_t *params)
411 KeyValues *pKV = NULL;
412 int client = params[1];
413 Handle_t hndl = static_cast<Handle_t>(params[3]);
414 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
418 return pContext->ThrowNativeError("Client index %d is invalid", client);
421 if (!pPlayer->IsInGame())
423 return pContext->ThrowNativeError("Client %d is not in game", client);
426 if (hndl != BAD_HANDLE)
428 pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
429 if (herr != HandleError_None)
431 return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
435 pContext->LocalToString(params[2], &name);
437 if (!g_HL2.ShowVGUIMenu(client, name, pKV, (params[4]) ? true : false))
439 return pContext->ThrowNativeError("Could not send a usermessage");
445 static cell_t smn_IsPlayerAlive(IPluginContext *pContext, const cell_t *params)
447 CPlayer *player = g_Players.GetPlayerByIndex(params[1]);
450 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
452 else if (!player->IsInGame())
454 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
457 unsigned int state = player->GetLifeState();
459 if (state == PLAYER_LIFE_UNKNOWN)
461 return pContext->ThrowNativeError("\"IsPlayerAlive\" not supported by this mod");
463 else if (state == PLAYER_LIFE_ALIVE)
473 static cell_t GuessSDKVersion(IPluginContext *pContext, const cell_t *params)
475 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
476 int version = g_SMAPI->GetSourceEngineBuild();
480 case SOURCE_ENGINE_ORIGINAL:
482 case SOURCE_ENGINE_EPISODEONE:
485 # if defined METAMOD_PLAPI_VERSION
487 case SOURCE_ENGINE_DARKMESSIAH:
489 case SOURCE_ENGINE_ORANGEBOX:
491 case SOURCE_ENGINE_BLOODYGOODTIME:
493 case SOURCE_ENGINE_EYE:
495 case SOURCE_ENGINE_CSS:
497 case SOURCE_ENGINE_ORANGEBOXVALVE:
499 case SOURCE_ENGINE_LEFT4DEAD:
501 case SOURCE_ENGINE_LEFT4DEAD2:
503 case SOURCE_ENGINE_ALIENSWARM:
505 case SOURCE_ENGINE_PORTAL2:
507 case SOURCE_ENGINE_CSGO:
509 case SOURCE_ENGINE_DOTA:
514 if (g_HL2.IsOriginalEngine())
527 static cell_t IndexToReference(IPluginContext *pContext, const cell_t *params)
529 if (params[1] >= NUM_ENT_ENTRIES || params[1] < 0)
531 return pContext->ThrowNativeError("Invalid entity index %i", params[1]);
534 return g_HL2.IndexToReference(params[1]);
537 static cell_t ReferenceToIndex(IPluginContext *pContext, const cell_t *params)
539 return g_HL2.ReferenceToIndex(params[1]);
542 static cell_t ReferenceToBCompatRef(IPluginContext *pContext, const cell_t *params)
544 return g_HL2.ReferenceToBCompatRef(params[1]);
547 REGISTER_NATIVES(halflifeNatives)
549 {"CreateFakeClient", CreateFakeClient},
550 {"GetCurrentMap", GetCurrentMap},
551 {"GetEngineTime", GetEngineTime},
552 {"GetGameDescription", GetGameDescription},
553 {"GetGameFolderName", GetGameFolderName},
554 {"GetGameTime", GetGameTime},
555 {"GetGameTickCount", GetGameTickCount},
556 {"GetRandomFloat", GetRandomFloat},
557 {"GetRandomInt", GetRandomInt},
558 {"IsDedicatedServer", IsDedicatedServer},
559 {"IsMapValid", IsMapValid},
560 {"SetFakeClientConVar", SetFakeClientConVar},
561 {"SetRandomSeed", SetRandomSeed},
562 {"PrecacheModel", PrecacheModel},
563 {"PrecacheSentenceFile", PrecacheSentenceFile},
564 {"PrecacheDecal", PrecacheDecal},
565 {"PrecacheGeneric", PrecacheGeneric},
566 {"IsModelPrecached", IsModelPrecached},
567 {"IsDecalPrecached", IsDecalPrecached},
568 {"IsGenericPrecached", IsGenericPrecached},
569 {"PrecacheSound", PrecacheSound},
570 {"IsSoundPrecached", IsSoundPrecached},
571 {"CreateDialog", smn_CreateDialog},
572 {"PrintToChat", PrintToChat},
573 {"PrintCenterText", PrintCenterText},
574 {"PrintHintText", PrintHintText},
575 {"ShowVGUIPanel", ShowVGUIPanel},
576 {"IsPlayerAlive", smn_IsPlayerAlive},
577 {"GuessSDKVersion", GuessSDKVersion},
578 {"EntIndexToEntRef", IndexToReference},
579 {"EntRefToEntIndex", ReferenceToIndex},
580 {"MakeCompatEntRef", ReferenceToBCompatRef},