SourceMod MSVC project files updated, windows tested and loading, gamedata fix.
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 float fTime = Plat_FloatTime();
78 return sp_ftoc(fTime);
81 static cell_t GetGameTime(IPluginContext *pContext, const cell_t *params)
83 return sp_ftoc(gpGlobals->curtime);
86 static cell_t CreateFakeClient(IPluginContext *pContext, const cell_t *params)
90 pContext->LocalToString(params[1], &netname);
92 edict_t *pEdict = engine->CreateFakeClient(netname);
94 /* :TODO: does the engine fire forwards for us and whatnot? no idea... */
101 return IndexOfEdict(pEdict);
104 static cell_t SetFakeClientConVar(IPluginContext *pContext, const cell_t *params)
106 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
110 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
113 if (!pPlayer->IsConnected())
115 return pContext->ThrowNativeError("Client %d is not connected", params[1]);
118 if (!pPlayer->IsFakeClient())
120 return pContext->ThrowNativeError("Client %d is not a fake client", params[1]);
125 pContext->LocalToString(params[2], &cvar);
126 pContext->LocalToString(params[3], &value);
128 engine->SetFakeClientConVarValue(pPlayer->GetEdict(), cvar, value);
133 static cell_t GetGameDescription(IPluginContext *pContext, const cell_t *params)
135 const char *description;
139 description = gamedll->GetGameDescription();
141 description = SERVER_CALL(GetGameDescription)();
146 pContext->StringToLocalUTF8(params[1], params[2], description, &numBytes);
151 static cell_t GetGameFolderName(IPluginContext *pContext, const cell_t *params)
153 const char *name = g_SourceMod.GetGameFolderName();
156 pContext->StringToLocalUTF8(params[1], params[2], name, &numBytes);
161 /* Useless comment to bump the build */
162 static cell_t GetCurrentMap(IPluginContext *pContext, const cell_t *params)
165 pContext->StringToLocalUTF8(params[1], params[2], STRING(gpGlobals->mapname), &bytes);
169 static cell_t PrecacheModel(IPluginContext *pContext, const cell_t *params)
172 pContext->LocalToString(params[1], &model);
174 return engine->PrecacheModel(model, params[2] ? true : false);
177 static cell_t PrecacheSentenceFile(IPluginContext *pContext, const cell_t *params)
180 pContext->LocalToString(params[1], &sentencefile);
182 return engine->PrecacheSentenceFile(sentencefile, params[2] ? true : false);
185 static cell_t PrecacheDecal(IPluginContext *pContext, const cell_t *params)
188 pContext->LocalToString(params[1], &decal);
190 return engine->PrecacheDecal(decal, params[2] ? true : false);
193 static cell_t PrecacheGeneric(IPluginContext *pContext, const cell_t *params)
196 pContext->LocalToString(params[1], &generic);
198 return engine->PrecacheGeneric(generic, params[2] ? true : false);
201 static cell_t IsModelPrecached(IPluginContext *pContext, const cell_t *params)
204 pContext->LocalToString(params[1], &model);
206 return engine->IsModelPrecached(model) ? 1 : 0;
209 static cell_t IsDecalPrecached(IPluginContext *pContext, const cell_t *params)
212 pContext->LocalToString(params[1], &decal);
214 return engine->IsDecalPrecached(decal) ? 1 : 0;
217 static cell_t IsGenericPrecached(IPluginContext *pContext, const cell_t *params)
220 pContext->LocalToString(params[1], &generic);
222 return engine->IsGenericPrecached(generic) ? 1 : 0;
225 static cell_t PrecacheSound(IPluginContext *pContext, const cell_t *params)
228 pContext->LocalToString(params[1], &sample);
230 return enginesound->PrecacheSound(sample, params[2] ? true : false) ? 1 : 0;
233 static cell_t IsSoundPrecached(IPluginContext *pContext, const cell_t *params)
236 pContext->LocalToString(params[1], &sample);
238 return enginesound->IsSoundPrecached(sample) ? 1 : 0;
241 static cell_t smn_CreateDialog(IPluginContext *pContext, const cell_t *params)
245 Handle_t hndl = static_cast<Handle_t>(params[2]);
246 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
250 return pContext->ThrowNativeError("Client index %d is invalid", params[1]);
253 if (!pPlayer->IsInGame())
255 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
258 pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
259 if (herr != HandleError_None)
261 return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
264 serverpluginhelpers->CreateMessage(pPlayer->GetEdict(),
265 static_cast<DIALOG_TYPE>(params[3]),
272 static cell_t PrintToChat(IPluginContext *pContext, const cell_t *params)
274 int client = params[1];
275 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
279 return pContext->ThrowNativeError("Client index %d is invalid", client);
282 if (!pPlayer->IsInGame())
284 return pContext->ThrowNativeError("Client %d is not in game", client);
287 g_SourceMod.SetGlobalTarget(client);
290 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
292 /* Check for an error before printing to the client */
293 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
298 if (!g_HL2.TextMsg(client, HUD_PRINTTALK, buffer))
300 return pContext->ThrowNativeError("Could not send a usermessage");
306 static cell_t PrintCenterText(IPluginContext *pContext, const cell_t *params)
308 int client = params[1];
309 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
313 return pContext->ThrowNativeError("Client index %d is invalid", client);
316 if (!pPlayer->IsInGame())
318 return pContext->ThrowNativeError("Client %d is not in game", client);
321 g_SourceMod.SetGlobalTarget(client);
324 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
326 /* Check for an error before printing to the client */
327 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
332 if (!g_HL2.TextMsg(client, HUD_PRINTCENTER, buffer))
334 return pContext->ThrowNativeError("Could not send a usermessage");
340 static cell_t PrintHintText(IPluginContext *pContext, const cell_t *params)
342 int client = params[1];
343 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
347 return pContext->ThrowNativeError("Client index %d is invalid", client);
350 if (!pPlayer->IsInGame())
352 return pContext->ThrowNativeError("Client %d is not in game", client);
355 g_SourceMod.SetGlobalTarget(client);
358 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
360 /* Check for an error before printing to the client */
361 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
366 if (!g_HL2.HintTextMsg(client, buffer))
368 return pContext->ThrowNativeError("Could not send a usermessage");
374 static cell_t ShowVGUIPanel(IPluginContext *pContext, const cell_t *params)
378 KeyValues *pKV = NULL;
379 int client = params[1];
380 Handle_t hndl = static_cast<Handle_t>(params[3]);
381 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
385 return pContext->ThrowNativeError("Client index %d is invalid", client);
388 if (!pPlayer->IsInGame())
390 return pContext->ThrowNativeError("Client %d is not in game", client);
393 if (hndl != BAD_HANDLE)
395 pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
396 if (herr != HandleError_None)
398 return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
402 pContext->LocalToString(params[2], &name);
404 if (!g_HL2.ShowVGUIMenu(client, name, pKV, (params[4]) ? true : false))
406 return pContext->ThrowNativeError("Could not send a usermessage");
412 static cell_t smn_IsPlayerAlive(IPluginContext *pContext, const cell_t *params)
414 CPlayer *player = g_Players.GetPlayerByIndex(params[1]);
417 return pContext->ThrowNativeError("Invalid client index %d", params[1]);
419 else if (!player->IsInGame())
421 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
424 unsigned int state = player->GetLifeState();
426 if (state == PLAYER_LIFE_UNKNOWN)
428 return pContext->ThrowNativeError("\"IsPlayerAlive\" not supported by this mod");
430 else if (state == PLAYER_LIFE_ALIVE)
440 static cell_t GuessSDKVersion(IPluginContext *pContext, const cell_t *params)
442 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
443 int version = g_SMAPI->GetSourceEngineBuild();
447 case SOURCE_ENGINE_ORIGINAL:
449 case SOURCE_ENGINE_EPISODEONE:
452 # if defined METAMOD_PLAPI_VERISON
454 case SOURCE_ENGINE_DARKMESSIAH:
456 case SOURCE_ENGINE_ORANGEBOX:
458 case SOURCE_ENGINE_LEFT4DEAD:
463 if (g_HL2.IsOriginalEngine())
476 static cell_t IndexToReference(IPluginContext *pContext, const cell_t *params)
478 if (params[1] >= NUM_ENT_ENTRIES || params[1] < 0)
480 return pContext->ThrowNativeError("Invalid entity index %i", params[1]);
483 return g_HL2.IndexToReference(params[1]);
486 static cell_t ReferenceToIndex(IPluginContext *pContext, const cell_t *params)
488 return g_HL2.ReferenceToIndex(params[1]);
491 static cell_t ReferenceToBCompatRef(IPluginContext *pContext, const cell_t *params)
493 return g_HL2.ReferenceToBCompatRef(params[1]);
496 REGISTER_NATIVES(halflifeNatives)
498 {"CreateFakeClient", CreateFakeClient},
499 {"GetCurrentMap", GetCurrentMap},
500 {"GetEngineTime", GetEngineTime},
501 {"GetGameDescription", GetGameDescription},
502 {"GetGameFolderName", GetGameFolderName},
503 {"GetGameTime", GetGameTime},
504 {"GetRandomFloat", GetRandomFloat},
505 {"GetRandomInt", GetRandomInt},
506 {"IsDedicatedServer", IsDedicatedServer},
507 {"IsMapValid", IsMapValid},
508 {"SetFakeClientConVar", SetFakeClientConVar},
509 {"SetRandomSeed", SetRandomSeed},
510 {"PrecacheModel", PrecacheModel},
511 {"PrecacheSentenceFile", PrecacheSentenceFile},
512 {"PrecacheDecal", PrecacheDecal},
513 {"PrecacheGeneric", PrecacheGeneric},
514 {"IsModelPrecached", IsModelPrecached},
515 {"IsDecalPrecached", IsDecalPrecached},
516 {"IsGenericPrecached", IsGenericPrecached},
517 {"PrecacheSound", PrecacheSound},
518 {"IsSoundPrecached", IsSoundPrecached},
519 {"CreateDialog", smn_CreateDialog},
520 {"PrintToChat", PrintToChat},
521 {"PrintCenterText", PrintCenterText},
522 {"PrintHintText", PrintHintText},
523 {"ShowVGUIPanel", ShowVGUIPanel},
524 {"IsPlayerAlive", smn_IsPlayerAlive},
525 {"GuessSDKVersion", GuessSDKVersion},
526 {"EntIndexToEntRef", IndexToReference},
527 {"EntRefToEntIndex", ReferenceToIndex},
528 {"MakeCompatEntRef", ReferenceToBCompatRef},