core/smn_halflife.cpp
author David Anderson <dvander@alliedmods.net>
Sun May 06 06:15:48 2007 +0000 (2007-05-06)
changeset 743 a2804c2faba2
parent 729 110c14c99545
child 873 a2136b3be10d
permissions -rw-r--r--
added request amb251
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     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
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include "sm_globals.h"
    16 #include "sourcemod.h"
    17 #include "sourcemm_api.h"
    18 #include "PlayerManager.h"
    19 #include "HandleSys.h"
    20 
    21 IServerPluginCallbacks *g_VSP = NULL;
    22 
    23 class HalfLifeNatives : public SMGlobalClass
    24 {
    25 public: //SMGlobalClass
    26 	void OnSourceModVSPReceived(IServerPluginCallbacks *iface)
    27 	{
    28 		g_VSP = iface;
    29 	}
    30 };
    31 
    32 static cell_t SetRandomSeed(IPluginContext *pContext, const cell_t *params)
    33 {
    34 	engrandom->SetSeed(params[1]);
    35 
    36 	return 1;
    37 }
    38 
    39 static cell_t GetRandomFloat(IPluginContext *pContext, const cell_t *params)
    40 {
    41 	float fMin = sp_ctof(params[1]);
    42 	float fMax = sp_ctof(params[2]);
    43 
    44 	float fRandom = engrandom->RandomFloat(fMin, fMax);
    45 
    46 	return sp_ftoc(fRandom);
    47 }
    48 
    49 static cell_t GetRandomInt(IPluginContext *pContext, const cell_t *params)
    50 {
    51 	return engrandom->RandomInt(params[1], params[2]);
    52 }
    53 
    54 static cell_t IsMapValid(IPluginContext *pContext, const cell_t *params)
    55 {
    56 	char *map;
    57 	pContext->LocalToString(params[1], &map);
    58 
    59 	return engine->IsMapValid(map);
    60 }
    61 
    62 static cell_t IsDedicatedServer(IPluginContext *pContext, const cell_t *params)
    63 {
    64 	return engine->IsDedicatedServer();
    65 }
    66 
    67 static cell_t GetEngineTime(IPluginContext *pContext, const cell_t *params)
    68 {
    69 	float fTime = engine->Time();
    70 	return sp_ftoc(fTime);
    71 }
    72 
    73 static cell_t GetGameTime(IPluginContext *pContext, const cell_t *params)
    74 {
    75 	return sp_ftoc(gpGlobals->curtime);
    76 }
    77 
    78 static cell_t CreateFakeClient(IPluginContext *pContext, const cell_t *params)
    79 {
    80 	char *netname;
    81 
    82 	pContext->LocalToString(params[1], &netname);
    83 
    84 	edict_t *pEdict = engine->CreateFakeClient(netname);
    85 
    86 	/* :TODO: does the engine fire forwards for us and whatnot? no idea... */
    87 
    88 	if (!pEdict)
    89 	{
    90 		return 0;
    91 	}
    92 
    93 	return engine->IndexOfEdict(pEdict);
    94 }
    95 
    96 static cell_t SetFakeClientConVar(IPluginContext *pContext, const cell_t *params)
    97 {
    98 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
    99 
   100 	if (!pPlayer)
   101 	{
   102 		return pContext->ThrowNativeError("Player %d is not a valid player", params[1]);
   103 	}
   104 
   105 	if (!pPlayer->IsConnected())
   106 	{
   107 		return pContext->ThrowNativeError("Player %d is not connected", params[1]);
   108 	}
   109 
   110 	if (!pPlayer->IsFakeClient())
   111 	{
   112 		return pContext->ThrowNativeError("Player %d is not a fake client", params[1]);
   113 	}
   114 
   115 	char *cvar, *value;
   116 
   117 	pContext->LocalToString(params[2], &cvar);
   118 	pContext->LocalToString(params[3], &value);
   119 
   120 	engine->SetFakeClientConVarValue(pPlayer->GetEdict(), cvar, value);
   121 
   122 	return 1;
   123 }
   124 
   125 static cell_t GetGameDescription(IPluginContext *pContext, const cell_t *params)
   126 {
   127 	const char *description;
   128 
   129 	if (params[3])
   130 	{
   131 		description = gamedll->GetGameDescription();
   132 	} else {
   133 		description = SERVER_CALL(GetGameDescription)();
   134 	}
   135 
   136 	size_t numBytes;
   137 	
   138 	pContext->StringToLocalUTF8(params[1], params[2], description, &numBytes);
   139 
   140 	return numBytes;
   141 }
   142 
   143 static cell_t GetGameFolderName(IPluginContext *pContext, const cell_t *params)
   144 {
   145 	const char *name = g_SourceMod.GetGameFolderName();
   146 	size_t numBytes;
   147 
   148 	pContext->StringToLocalUTF8(params[1], params[2], name, &numBytes);
   149 
   150 	return numBytes;
   151 }
   152 
   153 static cell_t GetCurrentMap(IPluginContext *pContext, const cell_t *params)
   154 {
   155 	size_t bytes;
   156 	pContext->StringToLocalUTF8(params[1], params[2], STRING(gpGlobals->mapname), &bytes);
   157 	return bytes;
   158 }
   159 
   160 static cell_t PrecacheModel(IPluginContext *pContext, const cell_t *params)
   161 {
   162 	char *model;
   163 	pContext->LocalToString(params[1], &model);
   164 
   165 	return engine->PrecacheModel(model, params[2] ? true : false);
   166 }
   167 
   168 static cell_t PrecacheSentenceFile(IPluginContext *pContext, const cell_t *params)
   169 {
   170 	char *sentencefile;
   171 	pContext->LocalToString(params[1], &sentencefile);
   172 
   173 	return engine->PrecacheSentenceFile(sentencefile, params[2] ? true : false);
   174 }
   175 
   176 static cell_t PrecacheDecal(IPluginContext *pContext, const cell_t *params)
   177 {
   178 	char *decal;
   179 	pContext->LocalToString(params[1], &decal);
   180 
   181 	return engine->PrecacheDecal(decal, params[2] ? true : false);
   182 }
   183 
   184 static cell_t PrecacheGeneric(IPluginContext *pContext, const cell_t *params)
   185 {
   186 	char *generic;
   187 	pContext->LocalToString(params[1], &generic);
   188 
   189 	return engine->PrecacheGeneric(generic, params[2] ? true : false);
   190 }
   191 
   192 static cell_t IsModelPrecached(IPluginContext *pContext, const cell_t *params)
   193 {
   194 	char *model;
   195 	pContext->LocalToString(params[1], &model);
   196 
   197 	return engine->IsModelPrecached(model) ? 1 : 0;
   198 }
   199 
   200 static cell_t IsDecalPrecached(IPluginContext *pContext, const cell_t *params)
   201 {
   202 	char *decal;
   203 	pContext->LocalToString(params[1], &decal);
   204 
   205 	return engine->IsDecalPrecached(decal) ? 1 : 0;
   206 }
   207 
   208 static cell_t IsGenericPrecached(IPluginContext *pContext, const cell_t *params)
   209 {
   210 	char *generic;
   211 	pContext->LocalToString(params[1], &generic);
   212 
   213 	return engine->IsGenericPrecached(generic) ? 1 : 0;
   214 }
   215 
   216 static cell_t PrecacheSound(IPluginContext *pContext, const cell_t *params)
   217 {
   218 	char *sample;
   219 	pContext->LocalToString(params[1], &sample);
   220 
   221 	return enginesound->PrecacheSound(sample, params[2] ? true : false) ? 1 : 0;
   222 }
   223 
   224 static cell_t IsSoundPrecached(IPluginContext *pContext, const cell_t *params)
   225 {
   226 	char *sample;
   227 	pContext->LocalToString(params[1], &sample);
   228 
   229 	return enginesound->IsSoundPrecached(sample) ? 1 : 0;
   230 }
   231 
   232 static cell_t FakeClientCommand(IPluginContext *pContext, const cell_t *params)
   233 {
   234 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
   235 
   236 	if (!pPlayer)
   237 	{
   238 		return pContext->ThrowNativeError("Player %d is not a valid player", params[1]);
   239 	}
   240 
   241 	if (!pPlayer->IsConnected())
   242 	{
   243 		return pContext->ThrowNativeError("Player %d is not connected", params[1]);
   244 	}
   245 
   246 	char buffer[256];
   247 	g_SourceMod.FormatString(buffer, sizeof(buffer)-1, pContext, params, 2);
   248 
   249 	serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), buffer);
   250 
   251 	return 1;
   252 }
   253 
   254 static cell_t smn_CreateDialog(IPluginContext *pContext, const cell_t *params)
   255 {
   256 	KeyValues *pKV;
   257 	HandleError herr;
   258 	Handle_t hndl = static_cast<Handle_t>(params[2]);
   259 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
   260 
   261 	if (!pPlayer)
   262 	{
   263 		return pContext->ThrowNativeError("Player %d is not a valid player", params[1]);
   264 	}
   265 
   266 	if (!pPlayer->IsConnected())
   267 	{
   268 		return pContext->ThrowNativeError("Player %d is not connected", params[1]);
   269 	}
   270 
   271 	pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
   272 	if (herr != HandleError_None)
   273 	{
   274 		return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   275 	}
   276 
   277 	serverpluginhelpers->CreateMessage(pPlayer->GetEdict(), static_cast<DIALOG_TYPE>(params[3]), pKV, g_VSP);
   278 
   279 	return 1;
   280 }
   281 
   282 static HalfLifeNatives s_HalfLifeNatives;
   283 
   284 REGISTER_NATIVES(halflifeNatives)
   285 {
   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},
   309 	{NULL,						NULL},
   310 };