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 "HandleSys.h"
19 #include "PlayerManager.h"
20 #include "HalfLife2.h"
22 IServerPluginCallbacks *g_VSP = NULL;
24 class HalfLifeNatives : public SMGlobalClass
26 public: //SMGlobalClass
27 void OnSourceModVSPReceived(IServerPluginCallbacks *iface)
33 static cell_t SetRandomSeed(IPluginContext *pContext, const cell_t *params)
35 engrandom->SetSeed(params[1]);
40 static cell_t GetRandomFloat(IPluginContext *pContext, const cell_t *params)
42 float fMin = sp_ctof(params[1]);
43 float fMax = sp_ctof(params[2]);
45 float fRandom = engrandom->RandomFloat(fMin, fMax);
47 return sp_ftoc(fRandom);
50 static cell_t GetRandomInt(IPluginContext *pContext, const cell_t *params)
52 return engrandom->RandomInt(params[1], params[2]);
55 static cell_t IsMapValid(IPluginContext *pContext, const cell_t *params)
58 pContext->LocalToString(params[1], &map);
60 return engine->IsMapValid(map);
63 static cell_t IsDedicatedServer(IPluginContext *pContext, const cell_t *params)
65 return engine->IsDedicatedServer();
68 static cell_t GetEngineTime(IPluginContext *pContext, const cell_t *params)
70 float fTime = engine->Time();
71 return sp_ftoc(fTime);
74 static cell_t GetGameTime(IPluginContext *pContext, const cell_t *params)
76 return sp_ftoc(gpGlobals->curtime);
79 static cell_t CreateFakeClient(IPluginContext *pContext, const cell_t *params)
83 pContext->LocalToString(params[1], &netname);
85 edict_t *pEdict = engine->CreateFakeClient(netname);
87 /* :TODO: does the engine fire forwards for us and whatnot? no idea... */
94 return engine->IndexOfEdict(pEdict);
97 static cell_t SetFakeClientConVar(IPluginContext *pContext, const cell_t *params)
99 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
103 return pContext->ThrowNativeError("Player %d is not a valid player", params[1]);
106 if (!pPlayer->IsConnected())
108 return pContext->ThrowNativeError("Player %d is not connected", params[1]);
111 if (!pPlayer->IsFakeClient())
113 return pContext->ThrowNativeError("Player %d is not a fake client", params[1]);
118 pContext->LocalToString(params[2], &cvar);
119 pContext->LocalToString(params[3], &value);
121 engine->SetFakeClientConVarValue(pPlayer->GetEdict(), cvar, value);
126 static cell_t GetGameDescription(IPluginContext *pContext, const cell_t *params)
128 const char *description;
132 description = gamedll->GetGameDescription();
134 description = SERVER_CALL(GetGameDescription)();
139 pContext->StringToLocalUTF8(params[1], params[2], description, &numBytes);
144 static cell_t GetGameFolderName(IPluginContext *pContext, const cell_t *params)
146 const char *name = g_SourceMod.GetGameFolderName();
149 pContext->StringToLocalUTF8(params[1], params[2], name, &numBytes);
154 static cell_t GetCurrentMap(IPluginContext *pContext, const cell_t *params)
157 pContext->StringToLocalUTF8(params[1], params[2], STRING(gpGlobals->mapname), &bytes);
161 static cell_t PrecacheModel(IPluginContext *pContext, const cell_t *params)
164 pContext->LocalToString(params[1], &model);
166 return engine->PrecacheModel(model, params[2] ? true : false);
169 static cell_t PrecacheSentenceFile(IPluginContext *pContext, const cell_t *params)
172 pContext->LocalToString(params[1], &sentencefile);
174 return engine->PrecacheSentenceFile(sentencefile, params[2] ? true : false);
177 static cell_t PrecacheDecal(IPluginContext *pContext, const cell_t *params)
180 pContext->LocalToString(params[1], &decal);
182 return engine->PrecacheDecal(decal, params[2] ? true : false);
185 static cell_t PrecacheGeneric(IPluginContext *pContext, const cell_t *params)
188 pContext->LocalToString(params[1], &generic);
190 return engine->PrecacheGeneric(generic, params[2] ? true : false);
193 static cell_t IsModelPrecached(IPluginContext *pContext, const cell_t *params)
196 pContext->LocalToString(params[1], &model);
198 return engine->IsModelPrecached(model) ? 1 : 0;
201 static cell_t IsDecalPrecached(IPluginContext *pContext, const cell_t *params)
204 pContext->LocalToString(params[1], &decal);
206 return engine->IsDecalPrecached(decal) ? 1 : 0;
209 static cell_t IsGenericPrecached(IPluginContext *pContext, const cell_t *params)
212 pContext->LocalToString(params[1], &generic);
214 return engine->IsGenericPrecached(generic) ? 1 : 0;
217 static cell_t PrecacheSound(IPluginContext *pContext, const cell_t *params)
220 pContext->LocalToString(params[1], &sample);
222 return enginesound->PrecacheSound(sample, params[2] ? true : false) ? 1 : 0;
225 static cell_t IsSoundPrecached(IPluginContext *pContext, const cell_t *params)
228 pContext->LocalToString(params[1], &sample);
230 return enginesound->IsSoundPrecached(sample) ? 1 : 0;
233 static cell_t smn_CreateDialog(IPluginContext *pContext, const cell_t *params)
237 Handle_t hndl = static_cast<Handle_t>(params[2]);
238 CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
242 return pContext->ThrowNativeError("Client %d is not valid", params[1]);
245 if (!pPlayer->IsInGame())
247 return pContext->ThrowNativeError("Client %d is not in game", params[1]);
250 pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
251 if (herr != HandleError_None)
253 return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
256 serverpluginhelpers->CreateMessage(pPlayer->GetEdict(), static_cast<DIALOG_TYPE>(params[3]), pKV, g_VSP);
261 static cell_t PrintToChat(IPluginContext *pContext, const cell_t *params)
263 int client = params[1];
264 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
268 return pContext->ThrowNativeError("Client %d is not valid", client);
271 if (!pPlayer->IsInGame())
273 return pContext->ThrowNativeError("Client %d is not in game", client);
277 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
279 g_HL2.TextMsg(client, HUD_PRINTTALK, buffer);
284 static cell_t PrintCenterText(IPluginContext *pContext, const cell_t *params)
286 int client = params[1];
287 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
291 return pContext->ThrowNativeError("Client %d is not valid", client);
294 if (!pPlayer->IsInGame())
296 return pContext->ThrowNativeError("Client %d is not in game", client);
300 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
302 g_HL2.TextMsg(client, HUD_PRINTCENTER, buffer);
307 static HalfLifeNatives s_HalfLifeNatives;
309 REGISTER_NATIVES(halflifeNatives)
311 {"CreateFakeClient", CreateFakeClient},
312 {"GetCurrentMap", GetCurrentMap},
313 {"GetEngineTime", GetEngineTime},
314 {"GetGameDescription", GetGameDescription},
315 {"GetGameFolderName", GetGameFolderName},
316 {"GetGameTime", GetGameTime},
317 {"GetRandomFloat", GetRandomFloat},
318 {"GetRandomInt", GetRandomInt},
319 {"IsDedicatedServer", IsDedicatedServer},
320 {"IsMapValid", IsMapValid},
321 {"SetFakeClientConVar", SetFakeClientConVar},
322 {"SetRandomSeed", SetRandomSeed},
323 {"PrecacheModel", PrecacheModel},
324 {"PrecacheSentenceFile", PrecacheSentenceFile},
325 {"PrecacheDecal", PrecacheDecal},
326 {"PrecacheGeneric", PrecacheGeneric},
327 {"IsModelPrecached", IsModelPrecached},
328 {"IsDecalPrecached", IsDecalPrecached},
329 {"IsGenericPrecached", IsGenericPrecached},
330 {"PrecacheSound", PrecacheSound},
331 {"IsSoundPrecached", IsSoundPrecached},
332 {"CreateDialog", smn_CreateDialog},
333 {"PrintToChat", PrintToChat},
334 {"PrintCenterText", PrintCenterText},