3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #include "sm_globals.h"
16 #include "sourcemod.h"
17 #include "sourcemm_api.h"
18 #include "PlayerManager.h"
19 #include "HandleSys.h"
21 IServerPluginCallbacks *g_VSP = NULL;
23 class HalfLifeNatives : public SMGlobalClass
25 public: //SMGlobalClass
26 void OnSourceModVSPReceived(IServerPluginCallbacks *iface)
32 static cell_t SetRandomSeed(IPluginContext *pContext, const cell_t *params)
34 engrandom->SetSeed(params[1]);
39 static cell_t GetRandomFloat(IPluginContext *pContext, const cell_t *params)
41 float fMin = sp_ctof(params[1]);
42 float fMax = sp_ctof(params[2]);
44 float fRandom = engrandom->RandomFloat(fMin, fMax);
46 return sp_ftoc(fRandom);
49 static cell_t GetRandomInt(IPluginContext *pContext, const cell_t *params)
51 return engrandom->RandomInt(params[1], params[2]);
54 static cell_t IsMapValid(IPluginContext *pContext, const cell_t *params)
57 pContext->LocalToString(params[1], &map);
59 return engine->IsMapValid(map);
62 static cell_t IsDedicatedServer(IPluginContext *pContext, const cell_t *params)
64 return engine->IsDedicatedServer();
67 static cell_t GetEngineTime(IPluginContext *pContext, const cell_t *params)
69 float fTime = engine->Time();
70 return sp_ftoc(fTime);
73 static cell_t GetGameTime(IPluginContext *pContext, const cell_t *params)
75 return sp_ftoc(gpGlobals->curtime);
78 static cell_t CreateFakeClient(IPluginContext *pContext, const cell_t *params)
82 pContext->LocalToString(params[1], &netname);
84 edict_t *pEdict = engine->CreateFakeClient(netname);
86 /* :TODO: does the engine fire forwards for us and whatnot? no idea... */
93 return engine->IndexOfEdict(pEdict);
96 static cell_t SetFakeClientConVar(IPluginContext *pContext, const cell_t *params)
98 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
102 return pContext->ThrowNativeError("Player %d is not a valid player", params[1]);
105 if (!pPlayer->IsConnected())
107 return pContext->ThrowNativeError("Player %d is not connected", params[1]);
110 if (!pPlayer->IsFakeClient())
112 return pContext->ThrowNativeError("Player %d is not a fake client", params[1]);
117 pContext->LocalToString(params[2], &cvar);
118 pContext->LocalToString(params[3], &value);
120 engine->SetFakeClientConVarValue(pPlayer->GetEdict(), cvar, value);
125 static cell_t GetGameDescription(IPluginContext *pContext, const cell_t *params)
127 const char *description;
131 description = gamedll->GetGameDescription();
133 description = SERVER_CALL(GetGameDescription)();
138 pContext->StringToLocalUTF8(params[1], params[2], description, &numBytes);
143 static cell_t GetGameFolderName(IPluginContext *pContext, const cell_t *params)
145 const char *name = g_SourceMod.GetModFolderName();
148 pContext->StringToLocalUTF8(params[1], params[2], name, &numBytes);
153 static cell_t GetCurrentMap(IPluginContext *pContext, const cell_t *params)
156 pContext->StringToLocalUTF8(params[1], params[2], STRING(gpGlobals->mapname), &bytes);
160 static cell_t PrecacheModel(IPluginContext *pContext, const cell_t *params)
163 pContext->LocalToString(params[1], &model);
165 return engine->PrecacheModel(model, params[2] ? true : false);
168 static cell_t PrecacheSentenceFile(IPluginContext *pContext, const cell_t *params)
171 pContext->LocalToString(params[1], &sentencefile);
173 return engine->PrecacheSentenceFile(sentencefile, params[2] ? true : false);
176 static cell_t PrecacheDecal(IPluginContext *pContext, const cell_t *params)
179 pContext->LocalToString(params[1], &decal);
181 return engine->PrecacheDecal(decal, params[2] ? true : false);
184 static cell_t PrecacheGeneric(IPluginContext *pContext, const cell_t *params)
187 pContext->LocalToString(params[1], &generic);
189 return engine->PrecacheGeneric(generic, params[2] ? true : false);
192 static cell_t IsModelPrecached(IPluginContext *pContext, const cell_t *params)
195 pContext->LocalToString(params[1], &model);
197 return engine->IsModelPrecached(model) ? 1 : 0;
200 static cell_t IsDecalPrecached(IPluginContext *pContext, const cell_t *params)
203 pContext->LocalToString(params[1], &decal);
205 return engine->IsDecalPrecached(decal) ? 1 : 0;
208 static cell_t IsGenericPrecached(IPluginContext *pContext, const cell_t *params)
211 pContext->LocalToString(params[1], &generic);
213 return engine->IsGenericPrecached(generic) ? 1 : 0;
216 static cell_t PrecacheSound(IPluginContext *pContext, const cell_t *params)
219 pContext->LocalToString(params[1], &sample);
221 return enginesound->PrecacheSound(sample, params[2] ? true : false) ? 1 : 0;
224 static cell_t IsSoundPrecached(IPluginContext *pContext, const cell_t *params)
227 pContext->LocalToString(params[1], &sample);
229 return enginesound->IsSoundPrecached(sample) ? 1 : 0;
232 static cell_t FakeClientCommand(IPluginContext *pContext, const cell_t *params)
234 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
238 return pContext->ThrowNativeError("Player %d is not a valid player", params[1]);
241 if (!pPlayer->IsConnected())
243 return pContext->ThrowNativeError("Player %d is not connected", params[1]);
247 g_SourceMod.FormatString(buffer, sizeof(buffer)-1, pContext, params, 2);
249 serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), buffer);
254 static cell_t smn_CreateDialog(IPluginContext *pContext, const cell_t *params)
258 Handle_t hndl = static_cast<Handle_t>(params[2]);
259 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
263 return pContext->ThrowNativeError("Player %d is not a valid player", params[1]);
266 if (!pPlayer->IsConnected())
268 return pContext->ThrowNativeError("Player %d is not connected", params[1]);
271 pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
272 if (herr != HandleError_None)
274 return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
277 serverpluginhelpers->CreateMessage(pPlayer->GetEdict(), static_cast<DIALOG_TYPE>(params[3]), pKV, g_VSP);
282 static HalfLifeNatives s_HalfLifeNatives;
284 REGISTER_NATIVES(halflifeNatives)
286 {"CreateFakeClient", CreateFakeClient},
287 {"GetCurrentMap", GetCurrentMap},
288 {"GetEngineTime", GetEngineTime},
289 {"GetGameDescription", GetGameDescription},
290 {"GetGameFolderName", GetGameFolderName},
291 {"GetGameTime", GetGameTime},
292 {"GetRandomFloat", GetRandomFloat},
293 {"GetRandomInt", GetRandomInt},
294 {"IsDedicatedServer", IsDedicatedServer},
295 {"IsMapValid", IsMapValid},
296 {"SetFakeClientConVar", SetFakeClientConVar},
297 {"SetRandomSeed", SetRandomSeed},
298 {"PrecacheModel", PrecacheModel},
299 {"PrecacheSentenceFile", PrecacheSentenceFile},
300 {"PrecacheDecal", PrecacheDecal},
301 {"PrecacheGeneric", PrecacheGeneric},
302 {"IsModelPrecached", IsModelPrecached},
303 {"IsDecalPrecached", IsDecalPrecached},
304 {"IsGenericPrecached", IsGenericPrecached},
305 {"PrecacheSound", PrecacheSound},
306 {"IsSoundPrecached", IsSoundPrecached},
307 {"FakeClientCommand", FakeClientCommand},
308 {"CreateDialog", smn_CreateDialog},