core/smn_halflife.cpp
author Scott Ehlert <ds@alliedmods.net>
Sat Apr 21 17:03:28 2007 +0000 (2007-04-21)
changeset 702 ff520b71249c
parent 682 a70055d6d3b3
child 721 4cc4debb70c1
permissions -rw-r--r--
Wow, how did I forget this - exported IMemoryUtils interface
     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 GetCurrentMap(IPluginContext *pContext, const cell_t *params)
   144 {
   145 	size_t bytes;
   146 	pContext->StringToLocalUTF8(params[1], params[2], STRING(gpGlobals->mapname), &bytes);
   147 	return bytes;
   148 }
   149 
   150 static cell_t PrecacheModel(IPluginContext *pContext, const cell_t *params)
   151 {
   152 	char *model;
   153 	pContext->LocalToString(params[1], &model);
   154 
   155 	return engine->PrecacheModel(model, params[2] ? true : false);
   156 }
   157 
   158 static cell_t PrecacheSentenceFile(IPluginContext *pContext, const cell_t *params)
   159 {
   160 	char *sentencefile;
   161 	pContext->LocalToString(params[1], &sentencefile);
   162 
   163 	return engine->PrecacheSentenceFile(sentencefile, params[2] ? true : false);
   164 }
   165 
   166 static cell_t PrecacheDecal(IPluginContext *pContext, const cell_t *params)
   167 {
   168 	char *decal;
   169 	pContext->LocalToString(params[1], &decal);
   170 
   171 	return engine->PrecacheDecal(decal, params[2] ? true : false);
   172 }
   173 
   174 static cell_t PrecacheGeneric(IPluginContext *pContext, const cell_t *params)
   175 {
   176 	char *generic;
   177 	pContext->LocalToString(params[1], &generic);
   178 
   179 	return engine->PrecacheGeneric(generic, params[2] ? true : false);
   180 }
   181 
   182 static cell_t IsModelPrecached(IPluginContext *pContext, const cell_t *params)
   183 {
   184 	char *model;
   185 	pContext->LocalToString(params[1], &model);
   186 
   187 	return engine->IsModelPrecached(model) ? 1 : 0;
   188 }
   189 
   190 static cell_t IsDecalPrecached(IPluginContext *pContext, const cell_t *params)
   191 {
   192 	char *decal;
   193 	pContext->LocalToString(params[1], &decal);
   194 
   195 	return engine->IsDecalPrecached(decal) ? 1 : 0;
   196 }
   197 
   198 static cell_t IsGenericPrecached(IPluginContext *pContext, const cell_t *params)
   199 {
   200 	char *generic;
   201 	pContext->LocalToString(params[1], &generic);
   202 
   203 	return engine->IsGenericPrecached(generic) ? 1 : 0;
   204 }
   205 
   206 static cell_t PrecacheSound(IPluginContext *pContext, const cell_t *params)
   207 {
   208 	char *sample;
   209 	pContext->LocalToString(params[1], &sample);
   210 
   211 	return enginesound->PrecacheSound(sample, params[2] ? true : false) ? 1 : 0;
   212 }
   213 
   214 static cell_t IsSoundPrecached(IPluginContext *pContext, const cell_t *params)
   215 {
   216 	char *sample;
   217 	pContext->LocalToString(params[1], &sample);
   218 
   219 	return enginesound->IsSoundPrecached(sample) ? 1 : 0;
   220 }
   221 
   222 static cell_t FakeClientCommand(IPluginContext *pContext, const cell_t *params)
   223 {
   224 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
   225 
   226 	if (!pPlayer)
   227 	{
   228 		return pContext->ThrowNativeError("Player %d is not a valid player", params[1]);
   229 	}
   230 
   231 	if (!pPlayer->IsConnected())
   232 	{
   233 		return pContext->ThrowNativeError("Player %d is not connected", params[1]);
   234 	}
   235 
   236 	char buffer[256];
   237 	g_SourceMod.FormatString(buffer, sizeof(buffer)-1, pContext, params, 2);
   238 
   239 	serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), buffer);
   240 
   241 	return 1;
   242 }
   243 
   244 static cell_t smn_CreateDialog(IPluginContext *pContext, const cell_t *params)
   245 {
   246 	KeyValues *pKV;
   247 	HandleError herr;
   248 	Handle_t hndl = static_cast<Handle_t>(params[2]);
   249 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(params[1]);
   250 
   251 	if (!pPlayer)
   252 	{
   253 		return pContext->ThrowNativeError("Player %d is not a valid player", params[1]);
   254 	}
   255 
   256 	if (!pPlayer->IsConnected())
   257 	{
   258 		return pContext->ThrowNativeError("Player %d is not connected", params[1]);
   259 	}
   260 
   261 	pKV = g_SourceMod.ReadKeyValuesHandle(hndl, &herr, true);
   262 	if (herr != HandleError_None)
   263 	{
   264 		return pContext->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
   265 	}
   266 
   267 	serverpluginhelpers->CreateMessage(pPlayer->GetEdict(), static_cast<DIALOG_TYPE>(params[3]), pKV, g_VSP);
   268 
   269 	return 1;
   270 }
   271 
   272 static HalfLifeNatives s_HalfLifeNatives;
   273 
   274 REGISTER_NATIVES(halflifeNatives)
   275 {
   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},
   298 	{NULL,						NULL},
   299 };