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 GetCurrentMap(IPluginContext *pContext, const cell_t *params)
146 pContext->StringToLocalUTF8(params[1], params[2], STRING(gpGlobals->mapname), &bytes);
150 static cell_t PrecacheModel(IPluginContext *pContext, const cell_t *params)
153 pContext->LocalToString(params[1], &model);
155 return engine->PrecacheModel(model, params[2] ? true : false);
158 static cell_t PrecacheSentenceFile(IPluginContext *pContext, const cell_t *params)
161 pContext->LocalToString(params[1], &sentencefile);
163 return engine->PrecacheSentenceFile(sentencefile, params[2] ? true : false);
166 static cell_t PrecacheDecal(IPluginContext *pContext, const cell_t *params)
169 pContext->LocalToString(params[1], &decal);
171 return engine->PrecacheDecal(decal, params[2] ? true : false);
174 static cell_t PrecacheGeneric(IPluginContext *pContext, const cell_t *params)
177 pContext->LocalToString(params[1], &generic);
179 return engine->PrecacheGeneric(generic, params[2] ? true : false);
182 static cell_t IsModelPrecached(IPluginContext *pContext, const cell_t *params)
185 pContext->LocalToString(params[1], &model);
187 return engine->IsModelPrecached(model) ? 1 : 0;
190 static cell_t IsDecalPrecached(IPluginContext *pContext, const cell_t *params)
193 pContext->LocalToString(params[1], &decal);
195 return engine->IsDecalPrecached(decal) ? 1 : 0;
198 static cell_t IsGenericPrecached(IPluginContext *pContext, const cell_t *params)
201 pContext->LocalToString(params[1], &generic);
203 return engine->IsGenericPrecached(generic) ? 1 : 0;
206 static cell_t PrecacheSound(IPluginContext *pContext, const cell_t *params)
209 pContext->LocalToString(params[1], &sample);
211 return enginesound->PrecacheSound(sample, params[2] ? true : false) ? 1 : 0;
214 static cell_t IsSoundPrecached(IPluginContext *pContext, const cell_t *params)
217 pContext->LocalToString(params[1], &sample);
219 return enginesound->IsSoundPrecached(sample) ? 1 : 0;
222 static cell_t FakeClientCommand(IPluginContext *pContext, const cell_t *params)
224 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
228 return pContext->ThrowNativeError("Player %d is not a valid player", params[1]);
231 if (!pPlayer->IsConnected())
233 return pContext->ThrowNativeError("Player %d is not connected", params[1]);
237 g_SourceMod.FormatString(buffer, sizeof(buffer)-1, pContext, params, 2);
239 serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), buffer);
244 static cell_t smn_CreateDialog(IPluginContext *pContext, const cell_t *params)
248 Handle_t hndl = static_cast<Handle_t>(params[2]);
249 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
253 return pContext->ThrowNativeError("Player %d is not a valid player", params[1]);
256 if (!pPlayer->IsConnected())
258 return pContext->ThrowNativeError("Player %d is not connected", params[1]);
261 pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
262 if (herr != HandleError_None)
264 return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
267 serverpluginhelpers->CreateMessage(pPlayer->GetEdict(), static_cast<DIALOG_TYPE>(params[3]), pKV, g_VSP);
272 static HalfLifeNatives s_HalfLifeNatives;
274 REGISTER_NATIVES(halflifeNatives)
276 {"CreateFakeClient", CreateFakeClient},
277 {"GetCurrentMap", GetCurrentMap},
278 {"GetEngineTime", GetEngineTime},
279 {"GetGameDescription", GetGameDescription},
280 {"GetGameTime", GetGameTime},
281 {"GetRandomFloat", GetRandomFloat},
282 {"GetRandomInt", GetRandomInt},
283 {"IsDedicatedServer", IsDedicatedServer},
284 {"IsMapValid", IsMapValid},
285 {"SetFakeClientConVar", SetFakeClientConVar},
286 {"SetRandomSeed", SetRandomSeed},
287 {"PrecacheModel", PrecacheModel},
288 {"PrecacheSentenceFile", PrecacheSentenceFile},
289 {"PrecacheDecal", PrecacheDecal},
290 {"PrecacheGeneric", PrecacheGeneric},
291 {"IsModelPrecached", IsModelPrecached},
292 {"IsDecalPrecached", IsDecalPrecached},
293 {"IsGenericPrecached", IsGenericPrecached},
294 {"PrecacheSound", PrecacheSound},
295 {"IsSoundPrecached", IsSoundPrecached},
296 {"FakeClientCommand", FakeClientCommand},
297 {"CreateDialog", smn_CreateDialog},