Updated cstrike gamedata.
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 GetGameTickCount(IPluginContext *pContext, const cell_t *params)
92 return gpGlobals->tickcount;
95 static cell_t CreateFakeClient(IPluginContext *pContext, const cell_t *params)
97 if (!g_SourceMod.IsMapRunning())
99 return pContext->ThrowNativeError("Cannot create fakeclient when no map is active");
104 pContext->LocalToString(params[1], &netname);
106 edict_t *pEdict = engine->CreateFakeClient(netname);
108 /* :TODO: does the engine fire forwards for us and whatnot? no idea... */
115 return IndexOfEdict(pEdict);
118 static cell_t SetFakeClientConVar(IPluginContext *pContext, const cell_t *params)
120 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
124 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
127 if (!pPlayer->IsConnected())
129 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
132 if (!pPlayer->IsFakeClient())
134 return pContext->ThrowNativeError("Client %d is not a fake client", params[1]);
139 pContext->LocalToString(params[2], &cvar);
140 pContext->LocalToString(params[3], &value);
142 engine->SetFakeClientConVarValue(pPlayer->GetEdict(), cvar, value);
147 static cell_t GetGameDescription(IPluginContext *pContext, const cell_t *params)
149 const char *description;
153 description = gamedll->GetGameDescription();
155 description = SERVER_CALL(GetGameDescription)();
160 pContext->StringToLocalUTF8(params[1], params[2], description, &numBytes);
165 static cell_t GetGameFolderName(IPluginContext *pContext, const cell_t *params)
167 const char *name = g_SourceMod.GetGameFolderName();
170 pContext->StringToLocalUTF8(params[1], params[2], name, &numBytes);
175 /* Useless comment to bump the build */
176 static cell_t GetCurrentMap(IPluginContext *pContext, const cell_t *params)
179 pContext->StringToLocalUTF8(params[1], params[2], STRING(gpGlobals->mapname), &bytes);
183 static cell_t PrecacheModel(IPluginContext *pContext, const cell_t *params)
186 pContext->LocalToString(params[1], &model);
188 return engine->PrecacheModel(model, params[2] ? true : false);
191 static cell_t PrecacheSentenceFile(IPluginContext *pContext, const cell_t *params)
194 pContext->LocalToString(params[1], &sentencefile);
196 return engine->PrecacheSentenceFile(sentencefile, params[2] ? true : false);
199 static cell_t PrecacheDecal(IPluginContext *pContext, const cell_t *params)
202 pContext->LocalToString(params[1], &decal);
204 return engine->PrecacheDecal(decal, params[2] ? true : false);
207 static cell_t PrecacheGeneric(IPluginContext *pContext, const cell_t *params)
210 pContext->LocalToString(params[1], &generic);
212 return engine->PrecacheGeneric(generic, params[2] ? true : false);
215 static cell_t IsModelPrecached(IPluginContext *pContext, const cell_t *params)
218 pContext->LocalToString(params[1], &model);
220 return engine->IsModelPrecached(model) ? 1 : 0;
223 static cell_t IsDecalPrecached(IPluginContext *pContext, const cell_t *params)
226 pContext->LocalToString(params[1], &decal);
228 return engine->IsDecalPrecached(decal) ? 1 : 0;
231 static cell_t IsGenericPrecached(IPluginContext *pContext, const cell_t *params)
234 pContext->LocalToString(params[1], &generic);
236 return engine->IsGenericPrecached(generic) ? 1 : 0;
239 static cell_t PrecacheSound(IPluginContext *pContext, const cell_t *params)
242 pContext->LocalToString(params[1], &sample);
244 return enginesound->PrecacheSound(sample, params[2] ? true : false) ? 1 : 0;
247 static cell_t IsSoundPrecached(IPluginContext *pContext, const cell_t *params)
250 pContext->LocalToString(params[1], &sample);
252 return enginesound->IsSoundPrecached(sample) ? 1 : 0;
255 static cell_t smn_CreateDialog(IPluginContext *pContext, const cell_t *params)
259 Handle_t hndl = static_cast<Handle_t>(params[2]);
260 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
264 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
267 if (!pPlayer->IsInGame())
269 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
272 pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
273 if (herr != HandleError_None)
275 return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
278 serverpluginhelpers->CreateMessage(pPlayer->GetEdict(),
279 static_cast<DIALOG_TYPE>(params[3]),
286 static cell_t PrintToChat(IPluginContext *pContext, const cell_t *params)
288 int client = params[1];
289 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
293 return pContext->ThrowNativeError("Client index %d is invalid", client);
296 if (!pPlayer->IsInGame())
298 return pContext->ThrowNativeError("Client %d is not in game", client);
301 g_SourceMod.SetGlobalTarget(client);
304 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
306 /* Check for an error before printing to the client */
307 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
312 if (!g_HL2.TextMsg(client, HUD_PRINTTALK, buffer))
314 return pContext->ThrowNativeError("Could not send a usermessage");
320 static cell_t PrintCenterText(IPluginContext *pContext, const cell_t *params)
322 int client = params[1];
323 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
327 return pContext->ThrowNativeError("Client index %d is invalid", client);
330 if (!pPlayer->IsInGame())
332 return pContext->ThrowNativeError("Client %d is not in game", client);
335 g_SourceMod.SetGlobalTarget(client);
338 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
340 /* Check for an error before printing to the client */
341 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
346 if (!g_HL2.TextMsg(client, HUD_PRINTCENTER, buffer))
348 return pContext->ThrowNativeError("Could not send a usermessage");
354 static cell_t PrintHintText(IPluginContext *pContext, const cell_t *params)
356 int client = params[1];
357 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
361 return pContext->ThrowNativeError("Client index %d is invalid", client);
364 if (!pPlayer->IsInGame())
366 return pContext->ThrowNativeError("Client %d is not in game", client);
369 g_SourceMod.SetGlobalTarget(client);
372 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
374 /* Check for an error before printing to the client */
375 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
380 if (!g_HL2.HintTextMsg(client, buffer))
382 return pContext->ThrowNativeError("Could not send a usermessage");
388 static cell_t ShowVGUIPanel(IPluginContext *pContext, const cell_t *params)
392 KeyValues *pKV = NULL;
393 int client = params[1];
394 Handle_t hndl = static_cast<Handle_t>(params[3]);
395 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
399 return pContext->ThrowNativeError("Client index %d is invalid", client);
402 if (!pPlayer->IsInGame())
404 return pContext->ThrowNativeError("Client %d is not in game", client);
407 if (hndl != BAD_HANDLE)
409 pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
410 if (herr != HandleError_None)
412 return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
416 pContext->LocalToString(params[2], &name);
418 if (!g_HL2.ShowVGUIMenu(client, name, pKV, (params[4]) ? true : false))
420 return pContext->ThrowNativeError("Could not send a usermessage");
426 static cell_t smn_IsPlayerAlive(IPluginContext *pContext, const cell_t *params)
428 CPlayer *player = g_Players.GetPlayerByIndex(params[1]);
431 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
433 else if (!player->IsInGame())
435 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
438 unsigned int state = player->GetLifeState();
440 if (state == PLAYER_LIFE_UNKNOWN)
442 return pContext->ThrowNativeError("\"IsPlayerAlive\" not supported by this mod");
444 else if (state == PLAYER_LIFE_ALIVE)
454 static cell_t GuessSDKVersion(IPluginContext *pContext, const cell_t *params)
456 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
457 int version = g_SMAPI->GetSourceEngineBuild();
461 case SOURCE_ENGINE_ORIGINAL:
463 case SOURCE_ENGINE_EPISODEONE:
466 # if defined METAMOD_PLAPI_VERSION
468 case SOURCE_ENGINE_DARKMESSIAH:
470 case SOURCE_ENGINE_ORANGEBOX:
472 case SOURCE_ENGINE_BLOODYGOODTIME:
474 case SOURCE_ENGINE_EYE:
476 case SOURCE_ENGINE_CSS:
478 case SOURCE_ENGINE_ORANGEBOXVALVE:
480 case SOURCE_ENGINE_LEFT4DEAD:
482 case SOURCE_ENGINE_LEFT4DEAD2:
484 case SOURCE_ENGINE_ALIENSWARM:
486 case SOURCE_ENGINE_PORTAL2:
488 case SOURCE_ENGINE_CSGO:
493 if (g_HL2.IsOriginalEngine())
506 static cell_t IndexToReference(IPluginContext *pContext, const cell_t *params)
508 if (params[1] >= NUM_ENT_ENTRIES || params[1] < 0)
510 return pContext->ThrowNativeError("Invalid entity index %i", params[1]);
513 return g_HL2.IndexToReference(params[1]);
516 static cell_t ReferenceToIndex(IPluginContext *pContext, const cell_t *params)
518 return g_HL2.ReferenceToIndex(params[1]);
521 static cell_t ReferenceToBCompatRef(IPluginContext *pContext, const cell_t *params)
523 return g_HL2.ReferenceToBCompatRef(params[1]);
526 REGISTER_NATIVES(halflifeNatives)
528 {"CreateFakeClient", CreateFakeClient},
529 {"GetCurrentMap", GetCurrentMap},
530 {"GetEngineTime", GetEngineTime},
531 {"GetGameDescription", GetGameDescription},
532 {"GetGameFolderName", GetGameFolderName},
533 {"GetGameTime", GetGameTime},
534 {"GetGameTickCount", GetGameTickCount},
535 {"GetRandomFloat", GetRandomFloat},
536 {"GetRandomInt", GetRandomInt},
537 {"IsDedicatedServer", IsDedicatedServer},
538 {"IsMapValid", IsMapValid},
539 {"SetFakeClientConVar", SetFakeClientConVar},
540 {"SetRandomSeed", SetRandomSeed},
541 {"PrecacheModel", PrecacheModel},
542 {"PrecacheSentenceFile", PrecacheSentenceFile},
543 {"PrecacheDecal", PrecacheDecal},
544 {"PrecacheGeneric", PrecacheGeneric},
545 {"IsModelPrecached", IsModelPrecached},
546 {"IsDecalPrecached", IsDecalPrecached},
547 {"IsGenericPrecached", IsGenericPrecached},
548 {"PrecacheSound", PrecacheSound},
549 {"IsSoundPrecached", IsSoundPrecached},
550 {"CreateDialog", smn_CreateDialog},
551 {"PrintToChat", PrintToChat},
552 {"PrintCenterText", PrintCenterText},
553 {"PrintHintText", PrintHintText},
554 {"ShowVGUIPanel", ShowVGUIPanel},
555 {"IsPlayerAlive", smn_IsPlayerAlive},
556 {"GuessSDKVersion", GuessSDKVersion},
557 {"EntIndexToEntRef", IndexToReference},
558 {"EntRefToEntIndex", ReferenceToIndex},
559 {"MakeCompatEntRef", ReferenceToBCompatRef},