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