extensions/sdktools/vnatives.cpp
author Matt Woodrow <pred@alliedmods.net>
Fri Jul 24 12:34:31 2009 +1200 (2009-07-24)
changeset 2746 8d68569b9648
parent 2744 28f6d1ffe3ec
child 2766 ad9d0794d4e7
permissions -rw-r--r--
Added support for logical (non-networked) entities using entity references - See http://wiki.alliedmods.net/Entity_References_%28SourceMod%29 (bug 2459, r=dvander)
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod SDKTools Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <stdlib.h>
    33 #include <sh_string.h>
    34 #include "extension.h"
    35 #include "vcallbuilder.h"
    36 #include "vnatives.h"
    37 #include "vhelpers.h"
    38 #include "vglobals.h"
    39 #include "CellRecipientFilter.h"
    40 #include <inetchannel.h>
    41 #include <iclient.h>
    42 #include "iserver.h"
    43 
    44 SourceHook::List<ValveCall *> g_RegCalls;
    45 SourceHook::List<ICallWrapper *> g_CallWraps;
    46 
    47 inline void InitPass(ValvePassInfo &info, ValveType vtype, PassType type, unsigned int flags, unsigned int decflags=0)
    48 {
    49 	info.decflags = decflags;
    50 	info.encflags = 0;
    51 	info.flags = flags;
    52 	info.type = type;
    53 	info.vtype = vtype;
    54 }
    55 
    56 #define START_CALL() \
    57 	unsigned char *vptr = pCall->stk_get();
    58 
    59 #define FINISH_CALL_SIMPLE(vret) \
    60 	pCall->call->Execute(vptr, vret); \
    61 	pCall->stk_put(vptr);
    62 
    63 #define ENCODE_VALVE_PARAM(num, which, vnum) \
    64 	if (EncodeValveParam(pContext, \
    65 			params[num], \
    66 			pCall, \
    67 			&pCall->which[vnum], \
    68 			vptr) \
    69 		== Data_Fail) \
    70 	{ \
    71 		return 0; \
    72 	}
    73 
    74 #define DECODE_VALVE_PARAM(num, which, vnum) \
    75 	if (DecodeValveParam(pContext, \
    76 			params[num], \
    77 			pCall, \
    78 			&pCall->which[vnum], \
    79 			vptr) \
    80 		== Data_Fail) \
    81 	{ \
    82 		return 0; \
    83 	}
    84 
    85 bool CreateBaseCall(const char *name,
    86 						  ValveCallType vcalltype,
    87 						  const ValvePassInfo *retinfo,
    88 						  const ValvePassInfo params[],
    89 						  unsigned int numParams,
    90 						  ValveCall **vaddr)
    91 {
    92 	int offset;
    93 	ValveCall *call;
    94 	if (g_pGameConf->GetOffset(name, &offset))
    95 	{
    96 		call = CreateValveVCall(offset, vcalltype, retinfo, params, numParams);
    97 		if (call)
    98 		{
    99 			g_RegCalls.push_back(call);
   100 		}
   101 		*vaddr = call;
   102 		return true;
   103 	} else {
   104 		void *addr = NULL;
   105 		if (g_pGameConf->GetMemSig(name, &addr) && addr != NULL)
   106 		{
   107 			call = CreateValveCall(addr, vcalltype, retinfo, params, numParams);
   108 			if (call)
   109 			{
   110 				g_RegCalls.push_back(call);
   111 			}
   112 			*vaddr = call;
   113 			return true;
   114 		}
   115 	}
   116 	return false;
   117 }
   118 
   119 static cell_t RemovePlayerItem(IPluginContext *pContext, const cell_t *params)
   120 {
   121 	static ValveCall *pCall = NULL;
   122 	if (!pCall)
   123 	{
   124 		ValvePassInfo pass[2];
   125 		InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
   126 		InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
   127 		if (!CreateBaseCall("RemovePlayerItem", ValveCall_Player, &pass[1], pass, 1, &pCall))
   128 		{
   129 			return pContext->ThrowNativeError("\"RemovePlayerItem\" not supported by this mod");
   130 		} else if (!pCall) {
   131 			return pContext->ThrowNativeError("\"RemovePlayerItem\" wrapper failed to initialize");
   132 		}
   133 	}
   134 
   135 	bool ret;
   136 	START_CALL();
   137 	DECODE_VALVE_PARAM(1, thisinfo, 0);
   138 	DECODE_VALVE_PARAM(2, vparams, 0);
   139 	FINISH_CALL_SIMPLE(&ret);
   140 	return ret ? 1 : 0;
   141 }
   142 
   143 static cell_t GiveNamedItem(IPluginContext *pContext, const cell_t *params)
   144 {
   145 	static ValveCall *pCall = NULL;
   146 	if (!pCall)
   147 	{
   148 		ValvePassInfo pass[3];
   149 		InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
   150 		InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
   151 		InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
   152 		if (!CreateBaseCall("GiveNamedItem", ValveCall_Player, &pass[2], pass, 2, &pCall))
   153 		{
   154 			return pContext->ThrowNativeError("\"GiveNamedItem\" not supported by this mod");
   155 		} else if (!pCall) {
   156 			return pContext->ThrowNativeError("\"GiveNamedItem\" wrapper failed to initialize");
   157 		}
   158 	}
   159 
   160 	CBaseEntity *pEntity = NULL;
   161 	START_CALL();
   162 	DECODE_VALVE_PARAM(1, thisinfo, 0);
   163 	DECODE_VALVE_PARAM(2, vparams, 0);
   164 	DECODE_VALVE_PARAM(3, vparams, 1);
   165 	FINISH_CALL_SIMPLE(&pEntity);
   166 
   167 	return gamehelpers->EntityToBCompatRef(pEntity);
   168 }
   169 
   170 static cell_t GetPlayerWeaponSlot(IPluginContext *pContext, const cell_t *params)
   171 {
   172 	static ValveCall *pCall = NULL;
   173 	if (!pCall)
   174 	{
   175 		ValvePassInfo pass[2];
   176 		InitPass(pass[0], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
   177 		InitPass(pass[1], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
   178 		if (!CreateBaseCall("Weapon_GetSlot", ValveCall_Player, &pass[1], pass, 1, &pCall))
   179 		{
   180 			return pContext->ThrowNativeError("\"Weapon_GetSlot\" not supported by this mod");
   181 		} else if (!pCall) {
   182 			return pContext->ThrowNativeError("\"Weapon_GetSlot\" wrapper failed to initialize");
   183 		}
   184 	}
   185 
   186 	CBaseEntity *pEntity;
   187 	START_CALL();
   188 	DECODE_VALVE_PARAM(1, thisinfo, 0);
   189 	DECODE_VALVE_PARAM(2, vparams, 0);
   190 	FINISH_CALL_SIMPLE(&pEntity);
   191 
   192 	return gamehelpers->EntityToBCompatRef(pEntity);
   193 }
   194 
   195 #if SOURCE_ENGINE != SE_DARKMESSIAH
   196 static cell_t IgniteEntity(IPluginContext *pContext, const cell_t *params)
   197 {
   198 	static ValveCall *pCall = NULL;
   199 	if (!pCall)
   200 	{
   201 		ValvePassInfo pass[4];
   202 		InitPass(pass[0], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
   203 		InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
   204 		InitPass(pass[2], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
   205 		InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
   206 		if (!CreateBaseCall("Ignite", ValveCall_Entity, NULL, pass, 4, &pCall))
   207 		{
   208 			return pContext->ThrowNativeError("\"Ignite\" not supported by this mod");
   209 		} else if (!pCall) {
   210 			return pContext->ThrowNativeError("\"Ignite\" wrapper failed to initialize");
   211 		}
   212 	}
   213 
   214 	START_CALL();
   215 	DECODE_VALVE_PARAM(1, thisinfo, 0);
   216 	DECODE_VALVE_PARAM(2, vparams, 0);
   217 	DECODE_VALVE_PARAM(3, vparams, 1);
   218 	DECODE_VALVE_PARAM(4, vparams, 2);
   219 	DECODE_VALVE_PARAM(5, vparams, 3);
   220 	FINISH_CALL_SIMPLE(NULL);
   221 
   222 	return 1;
   223 }
   224 #else
   225 /* Dark Messiah specific version */
   226 static cell_t IgniteEntity(IPluginContext *pContext, const cell_t *params)
   227 {
   228 	static ValveCall *pCall = NULL;
   229 	if (!pCall)
   230 	{
   231 		ValvePassInfo pass[6];
   232 		InitPass(pass[0], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
   233 		InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
   234 		InitPass(pass[2], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
   235 		InitPass(pass[3], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
   236 		InitPass(pass[4], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
   237 		InitPass(pass[5], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
   238 		if (!CreateBaseCall("Ignite", ValveCall_Entity, NULL, pass, 6, &pCall))
   239 		{
   240 			return pContext->ThrowNativeError("\"Ignite\" not supported by this mod");
   241 		} else if (!pCall) {
   242 			return pContext->ThrowNativeError("\"Ignite\" wrapper failed to initialize");
   243 		}
   244 	}
   245 
   246 	START_CALL();
   247 	DECODE_VALVE_PARAM(1, thisinfo, 0);
   248 	DECODE_VALVE_PARAM(2, vparams, 0);
   249 	DECODE_VALVE_PARAM(3, vparams, 1);
   250 	DECODE_VALVE_PARAM(4, vparams, 2);
   251 	DECODE_VALVE_PARAM(5, vparams, 3);
   252 	/* Not sure what these params do, but they appear to be the default values */
   253 	*(int *)(vptr + 14) = 3;
   254 	*(int *)(vptr + 18) = 0;
   255 	FINISH_CALL_SIMPLE(NULL);
   256 
   257 	return 1;
   258 }
   259 #endif
   260 
   261 static cell_t ExtinguishEntity(IPluginContext *pContext, const cell_t *params)
   262 {
   263 	static ValveCall *pCall = NULL;
   264 	if (!pCall)
   265 	{
   266 		if (!CreateBaseCall("Extinguish", ValveCall_Entity, NULL, NULL, 0, &pCall))
   267 		{
   268 			return pContext->ThrowNativeError("\"Extinguish\" not supported by this mod");
   269 		} else if (!pCall) {
   270 			return pContext->ThrowNativeError("\"Extinguish\" wrapper failed to initialize");
   271 		}
   272 	}
   273 
   274 	START_CALL();
   275 	DECODE_VALVE_PARAM(1, thisinfo, 0);
   276 	FINISH_CALL_SIMPLE(NULL);
   277 
   278 	return 1;
   279 }
   280 
   281 static cell_t TeleportEntity(IPluginContext *pContext, const cell_t *params)
   282 {
   283 	static ValveCall *pCall = NULL;
   284 	if (!pCall)
   285 	{
   286 		ValvePassInfo pass[3];
   287 		InitPass(pass[0], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
   288 		InitPass(pass[1], Valve_QAngle, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
   289 		InitPass(pass[2], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL);
   290 		if (!CreateBaseCall("Teleport", ValveCall_Entity, NULL, pass, 3, &pCall))
   291 		{
   292 			return pContext->ThrowNativeError("\"Teleport\" not supported by this mod");
   293 		} else if (!pCall) {
   294 			return pContext->ThrowNativeError("\"Teleport\" wrapper failed to initialize");
   295 		}
   296 	}
   297 
   298 	START_CALL();
   299 	DECODE_VALVE_PARAM(1, thisinfo, 0);
   300 	DECODE_VALVE_PARAM(2, vparams, 0);
   301 	DECODE_VALVE_PARAM(3, vparams, 1);
   302 	DECODE_VALVE_PARAM(4, vparams, 2);
   303 	FINISH_CALL_SIMPLE(NULL);
   304 
   305 	return 1;
   306 }
   307 
   308 #if SOURCE_ENGINE >= SE_ORANGEBOX
   309 /* :TODO: This is Team Fortress 2 specific */
   310 static cell_t ForcePlayerSuicide(IPluginContext *pContext, const cell_t *params)
   311 {
   312 	static ValveCall *pCall = NULL;
   313 	if (!pCall)
   314 	{
   315 		ValvePassInfo pass[2];
   316 		InitPass(pass[0], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
   317 		InitPass(pass[1], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
   318 		if (!CreateBaseCall("CommitSuicide", ValveCall_Player, NULL, pass, 2, &pCall))
   319 		{
   320 			return pContext->ThrowNativeError("\"CommitSuicide\" not supported by this mod");
   321 		}
   322 		else if (!pCall)
   323 		{
   324 			return pContext->ThrowNativeError("\"CommitSuicide\" wrapper failed to initialize");
   325 		}
   326 	}
   327 
   328 	START_CALL();
   329 	DECODE_VALVE_PARAM(1, thisinfo, 0);
   330 	*(bool *)(vptr + 4) = false;
   331 	*(bool *)(vptr + 5) = false;
   332 	FINISH_CALL_SIMPLE(NULL);
   333 
   334 	return 1;
   335 }
   336 #else
   337 static cell_t ForcePlayerSuicide(IPluginContext *pContext, const cell_t *params)
   338 {
   339 	static ValveCall *pCall = NULL;
   340 	if (!pCall)
   341 	{
   342 		if (!CreateBaseCall("CommitSuicide", ValveCall_Player, NULL, NULL, 0, &pCall))
   343 		{
   344 			return pContext->ThrowNativeError("\"CommitSuicide\" not supported by this mod");
   345 		} else if (!pCall) {
   346 			return pContext->ThrowNativeError("\"CommitSuicide\" wrapper failed to initialize");
   347 		}
   348 	}
   349 
   350 	START_CALL();
   351 	DECODE_VALVE_PARAM(1, thisinfo, 0);
   352 	FINISH_CALL_SIMPLE(NULL);
   353 
   354 	return 1;
   355 }
   356 #endif
   357 
   358 static cell_t SetClientViewEntity(IPluginContext *pContext, const cell_t *params)
   359 {
   360 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
   361 	if (player == NULL)
   362 	{
   363 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
   364 	}
   365 	if (!player->IsInGame())
   366 	{
   367 		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
   368 	}
   369 
   370 	edict_t *pEdict = PEntityOfEntIndex(gamehelpers->ReferenceToIndex(params[1]));
   371 	if (!pEdict || pEdict->IsFree())
   372 	{
   373 		return pContext->ThrowNativeError("Entity %d is not valid", params[2]);
   374 	}
   375 
   376 	engine->SetView(player->GetEdict(), pEdict);
   377 
   378 	return 1;
   379 }
   380 
   381 static SourceHook::String *g_lightstyle[MAX_LIGHTSTYLES] = {NULL};
   382 static cell_t SetLightStyle(IPluginContext *pContext, const cell_t *params)
   383 {
   384 	int style = params[1];
   385 	if (style >= MAX_LIGHTSTYLES)
   386 	{
   387 		return pContext->ThrowNativeError("Light style %d is invalid (range: 0-%d)", style, MAX_LIGHTSTYLES - 1);
   388 	}
   389 
   390 	if (g_lightstyle[style] == NULL)
   391 	{
   392 		/* We allocate and never free this because the Engine wants to hold onto it :\
   393 		 * in theory we could hook light style and know whether we're supposed to free 
   394 		 * this or not on shutdown, but for ~4K of memory MAX, it doesn't seem worth it yet.
   395 		 * So, it's a :TODO:!
   396 		 */
   397 		g_lightstyle[style] = new SourceHook::String();
   398 	}
   399 
   400 	char *str;
   401 	pContext->LocalToString(params[2], &str);
   402 
   403 	g_lightstyle[style]->assign(str);
   404 
   405 	engine->LightStyle(style, g_lightstyle[style]->c_str());
   406 
   407 	return 1;
   408 }
   409 
   410 static cell_t SlapPlayer(IPluginContext *pContext, const cell_t *params)
   411 {
   412 	static bool s_slap_supported = false;
   413 	static bool s_slap_setup = false;
   414 	static ICallWrapper *s_teleport = NULL;
   415 	static int s_health_offs = 0;
   416 	static int s_sound_count = 0;
   417 	static int s_frag_offs = 0;
   418 
   419 	if (!s_slap_setup)
   420 	{
   421 		int tries = 0;
   422 
   423 		s_slap_setup = true;
   424 
   425 		if (IsTeleportSupported())
   426 		{
   427 			tries++;
   428 		}
   429 		if (IsGetVelocitySupported())
   430 		{
   431 			tries++;
   432 		}
   433 
   434 		/* Setup health */
   435 		if (g_pGameConf->GetOffset("m_iHealth", &s_health_offs) && s_health_offs)
   436 		{
   437 			tries++;
   438 		}
   439 
   440 		if (tries == 3)
   441 		{
   442 			s_slap_supported = true;
   443 
   444 			const char *key;
   445 			if ((key = g_pGameConf->GetKeyValue("SlapSoundCount")) != NULL)
   446 			{
   447 				s_sound_count = atoi(key);
   448 			}
   449 		}
   450 	}
   451 
   452 	if (!s_slap_supported)
   453 	{
   454 		return pContext->ThrowNativeError("This function is not supported on this mod");
   455 	}
   456 
   457 	/* First check if the client is valid */
   458 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
   459 	if (!player)
   460 	{
   461 		return pContext->ThrowNativeError("Client %d is not valid", params[1]);
   462 	} else if (!player->IsInGame()) {
   463 		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
   464 	}
   465 
   466 	edict_t *pEdict = player->GetEdict();
   467 	CBaseEntity *pEntity = pEdict->GetUnknown()->GetBaseEntity();
   468 
   469 	/* See if we should be taking away health */
   470 	bool should_slay = false;
   471 	if (params[2])
   472 	{
   473 #if SOURCE_ENGINE != SE_DARKMESSIAH
   474 		int *health = (int *)((char *)pEntity + s_health_offs);
   475 
   476 		if (*health - params[2] <= 0)
   477 		{
   478 			*health = 1;
   479 			should_slay = true;
   480 		} else {
   481 			*health -= params[2];
   482 		}
   483 #else
   484 		float *health = (float *)((char *)pEntity + s_health_offs);
   485 
   486 		if (*health - (float)params[2] <= 0)
   487 		{
   488 			*health = 1.0f;
   489 			should_slay = true;
   490 		} else {
   491 			*health -= (float)params[2];
   492 		}
   493 #endif
   494 	}
   495 
   496 	/* Teleport in a random direction - thank you, Mani!*/
   497 	Vector velocity;
   498 	GetVelocity(pEntity, &velocity, NULL);
   499 	velocity.x += ((rand() % 180) + 50) * (((rand() % 2) == 1) ?  -1 : 1);
   500 	velocity.y += ((rand() % 180) + 50) * (((rand() % 2) == 1) ?  -1 : 1);
   501 	velocity.z += rand() % 200 + 100;
   502 	Teleport(pEntity, NULL, NULL, &velocity);
   503 
   504 	/* Play a random sound */
   505 	if (params[3] && s_sound_count > 0)
   506 	{
   507 		char name[48];
   508 		const char *sound_name;
   509 		cell_t player_list[256], total_players = 0;
   510 		int maxClients = playerhelpers->GetMaxClients();
   511 
   512 		int r = (rand() % s_sound_count) + 1;
   513 		snprintf(name, sizeof(name), "SlapSound%d", r);
   514 
   515 		if ((sound_name = g_pGameConf->GetKeyValue(name)) != NULL)
   516 		{
   517 			IGamePlayer *other;
   518 			for (int i=1; i<=maxClients; i++)
   519 			{
   520 				other = playerhelpers->GetGamePlayer(i);
   521 				if (other->IsInGame())
   522 				{
   523 					player_list[total_players++] = i;
   524 				}
   525 			}
   526 
   527 			const Vector & pos = pEdict->GetCollideable()->GetCollisionOrigin();
   528 			CellRecipientFilter rf;
   529 			rf.SetToReliable(true);
   530 			rf.Initialize(player_list, total_players);
   531 			engsound->EmitSound(rf, params[1], CHAN_AUTO, sound_name, VOL_NORM, ATTN_NORM, 0, PITCH_NORM, &pos);
   532 		}
   533 	}
   534 
   535 	if (!s_frag_offs)
   536 	{
   537 		const char *frag_prop = g_pGameConf->GetKeyValue("m_iFrags");
   538 		if (frag_prop)
   539 		{
   540 			datamap_t *pMap = gamehelpers->GetDataMap(pEntity);
   541 			typedescription_t *pType = gamehelpers->FindInDataMap(pMap, frag_prop);
   542 			if (pType != NULL)
   543 			{
   544 				s_frag_offs = pType->fieldOffset[TD_OFFSET_NORMAL];
   545 			}
   546 		}
   547 		if (!s_frag_offs)
   548 		{
   549 			s_frag_offs = -1;
   550 		}
   551 	}
   552 
   553 	int old_frags = 0;
   554 	if (s_frag_offs > 0)
   555 	{
   556 		old_frags = *(int *)((char *)pEntity + s_frag_offs);
   557 	}
   558 	
   559 	/* Force suicide */
   560 	if (should_slay)
   561 	{
   562 		pluginhelpers->ClientCommand(pEdict, "kill\n");
   563 	}
   564 
   565 	if (s_frag_offs > 0)
   566 	{
   567 		*(int *)((char *)pEntity + s_frag_offs) = old_frags;
   568 	}
   569 
   570 	return 1;
   571 }
   572 
   573 static cell_t GetClientEyePosition(IPluginContext *pContext, const cell_t *params)
   574 {
   575 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
   576 	if (player == NULL)
   577 	{
   578 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
   579 	}
   580 	if (!player->IsInGame())
   581 	{
   582 		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
   583 	}
   584 
   585 	Vector pos;
   586 	serverClients->ClientEarPosition(player->GetEdict(), &pos);
   587 
   588 	cell_t *addr;
   589 	pContext->LocalToPhysAddr(params[2], &addr);
   590 	addr[0] = sp_ftoc(pos.x);
   591 	addr[1] = sp_ftoc(pos.y);
   592 	addr[2] = sp_ftoc(pos.z);
   593 
   594 	return 1;
   595 }
   596 
   597 static cell_t GetClientEyeAngles(IPluginContext *pContext, const cell_t *params)
   598 {
   599 	IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(params[1]);
   600 
   601 	if (!pPlayer)
   602 	{
   603 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
   604 	}
   605 	else if (!pPlayer->IsInGame())
   606 	{
   607 		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
   608 	}
   609 
   610 	edict_t *pEdict = pPlayer->GetEdict();
   611 	CBaseEntity *pEntity = pEdict->GetUnknown() ? pEdict->GetUnknown()->GetBaseEntity() : NULL;
   612 
   613 	/* We always set the angles for backwards compatibility -- 
   614 	 * The original function had no return value.
   615 	 */
   616 	QAngle angles;
   617 	bool got_angles = false;
   618 
   619 	if (pEntity != NULL)
   620 	{
   621 		got_angles = GetEyeAngles(pEntity, &angles);
   622 	}
   623 	
   624 	cell_t *addr;
   625 	pContext->LocalToPhysAddr(params[2], &addr);
   626 	addr[0] = sp_ftoc(angles.x);
   627 	addr[1] = sp_ftoc(angles.y);
   628 	addr[2] = sp_ftoc(angles.z);
   629 
   630 	return got_angles ? 1 : 0;
   631 }
   632 
   633 static cell_t FindEntityByClassname(IPluginContext *pContext, const cell_t *params)
   634 {
   635 	static ValveCall *pCall = NULL;
   636 	if (!pCall)
   637 	{
   638 		ValvePassInfo pass[3];
   639 		InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL, VDECODE_FLAG_ALLOWNULL|VDECODE_FLAG_ALLOWWORLD);
   640 		InitPass(pass[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
   641 		InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
   642 		if (!CreateBaseCall("FindEntityByClassname", ValveCall_EntityList, &pass[2], pass, 2, &pCall))
   643 		{
   644 			return pContext->ThrowNativeError("\"FindEntityByClassname\" not supported by this mod");
   645 		} else if (!pCall) {
   646 			return pContext->ThrowNativeError("\"FindEntityByClassname\" wrapper failed to initialize");
   647 		}
   648 	}
   649 
   650 	CBaseEntity *pEntity;
   651 	START_CALL();
   652 	*(void **)vptr = g_EntList; 
   653 	DECODE_VALVE_PARAM(1, vparams, 0);
   654 	DECODE_VALVE_PARAM(2, vparams, 1);
   655 	FINISH_CALL_SIMPLE(&pEntity);
   656 
   657 	return gamehelpers->EntityToBCompatRef(pEntity);
   658 }
   659 
   660 #if SOURCE_ENGINE == SE_LEFT4DEAD
   661 static cell_t CreateEntityByName(IPluginContext *pContext, const cell_t *params)
   662 {
   663 	static ValveCall *pCall = NULL;
   664 	if (!pCall)
   665 	{
   666 		ValvePassInfo pass[4];
   667 		InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
   668 		InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
   669 		InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
   670 		InitPass(pass[3], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
   671 		if (!CreateBaseCall("CreateEntityByName", ValveCall_Static, &pass[3], pass, 3, &pCall))
   672 		{
   673 			return pContext->ThrowNativeError("\"CreateEntityByName\" not supported by this mod");
   674 		} else if (!pCall) {
   675 			return pContext->ThrowNativeError("\"CreateEntityByName\" wrapper failed to initialize");
   676 		}
   677 	}
   678 
   679 	CBaseEntity *pEntity = NULL;
   680 	START_CALL();
   681 	DECODE_VALVE_PARAM(1, vparams, 0);
   682 	DECODE_VALVE_PARAM(2, vparams, 1);
   683 	*(bool *)(vptr + 8) = true;
   684 	FINISH_CALL_SIMPLE(&pEntity);
   685 
   686 	return gamehelpers->EntityToBCompatRef(pEntity);
   687 }
   688 #else
   689 static cell_t CreateEntityByName(IPluginContext *pContext, const cell_t *params)
   690 {
   691 	static ValveCall *pCall = NULL;
   692 	if (!pCall)
   693 	{
   694 		ValvePassInfo pass[3];
   695 		InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
   696 		InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
   697 		InitPass(pass[2], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
   698 		if (!CreateBaseCall("CreateEntityByName", ValveCall_Static, &pass[2], pass, 2, &pCall))
   699 		{
   700 			return pContext->ThrowNativeError("\"CreateEntityByName\" not supported by this mod");
   701 		} else if (!pCall) {
   702 			return pContext->ThrowNativeError("\"CreateEntityByName\" wrapper failed to initialize");
   703 		}
   704 	}
   705 
   706 	CBaseEntity *pEntity = NULL;
   707 	START_CALL();
   708 	DECODE_VALVE_PARAM(1, vparams, 0);
   709 	DECODE_VALVE_PARAM(2, vparams, 1);
   710 	FINISH_CALL_SIMPLE(&pEntity);
   711 
   712 	return gamehelpers->EntityToBCompatRef(pEntity);
   713 }
   714 #endif
   715 
   716 static cell_t DispatchSpawn(IPluginContext *pContext, const cell_t *params)
   717 {
   718 	static ValveCall *pCall = NULL;
   719 	if (!pCall)
   720 	{
   721 		ValvePassInfo pass[2];
   722 		InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
   723 		InitPass(pass[1], Valve_POD, PassType_Basic, PASSFLAG_BYVAL);
   724 		if (!CreateBaseCall("DispatchSpawn", ValveCall_Static, &pass[1], pass, 1, &pCall))
   725 		{
   726 			return pContext->ThrowNativeError("\"DispatchSpawn\" not supported by this mod");
   727 		} else if (!pCall) {
   728 			return pContext->ThrowNativeError("\"DispatchSpawn\" wrapper failed to initialize");
   729 		}
   730 	}
   731 
   732 	int ret;
   733 	START_CALL();
   734 	DECODE_VALVE_PARAM(1, vparams, 0);
   735 	FINISH_CALL_SIMPLE(&ret);
   736 
   737 	return (ret == -1) ? 0 : 1;
   738 }
   739 
   740 static cell_t DispatchKeyValue(IPluginContext *pContext, const cell_t *params)
   741 {
   742 	static ValveCall *pCall = NULL;
   743 	if (!pCall)
   744 	{
   745 		ValvePassInfo pass[3];
   746 		InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
   747 		InitPass(pass[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
   748 		InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
   749 		if (!CreateBaseCall("DispatchKeyValue", ValveCall_Entity, &pass[2], pass, 2, &pCall))
   750 		{
   751 			return pContext->ThrowNativeError("\"DispatchKeyValue\" not supported by this mod");
   752 		} else if (!pCall) {
   753 			return pContext->ThrowNativeError("\"DispatchKeyValue\" wrapper failed to initialize");
   754 		}
   755 	}
   756 
   757 	bool ret;
   758 	START_CALL();
   759 	DECODE_VALVE_PARAM(1, thisinfo, 0);
   760 	DECODE_VALVE_PARAM(2, vparams, 0);
   761 	DECODE_VALVE_PARAM(3, vparams, 1);
   762 	FINISH_CALL_SIMPLE(&ret);
   763 
   764 	return ret ? 1 : 0;
   765 }
   766 
   767 static cell_t DispatchKeyValueFloat(IPluginContext *pContext, const cell_t *params)
   768 {
   769 	static ValveCall *pCall = NULL;
   770 	if (!pCall)
   771 	{
   772 		ValvePassInfo pass[3];
   773 		InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
   774 		InitPass(pass[1], Valve_Float, PassType_Float, PASSFLAG_BYVAL);
   775 		InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
   776 		if (!CreateBaseCall("DispatchKeyValueFloat", ValveCall_Entity, &pass[2], pass, 2, &pCall))
   777 		{
   778 			return pContext->ThrowNativeError("\"DispatchKeyValueFloat\" not supported by this mod");
   779 		} else if (!pCall) {
   780 			return pContext->ThrowNativeError("\"DispatchKeyValueFloat\" wrapper failed to initialize");
   781 		}
   782 	}
   783 
   784 	bool ret;
   785 	START_CALL();
   786 	DECODE_VALVE_PARAM(1, thisinfo, 0);
   787 	DECODE_VALVE_PARAM(2, vparams, 0);
   788 	DECODE_VALVE_PARAM(3, vparams, 1);
   789 	FINISH_CALL_SIMPLE(&ret);
   790 
   791 	return ret ? 1 : 0;
   792 }
   793 
   794 static cell_t DispatchKeyValueVector(IPluginContext *pContext, const cell_t *params)
   795 {
   796 	static ValveCall *pCall = NULL;
   797 	if (!pCall)
   798 	{
   799 		ValvePassInfo pass[3];
   800 		InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
   801 #if SOURCE_ENGINE >= SE_ORANGEBOX
   802 		InitPass(pass[1], Valve_Vector, PassType_Basic, PASSFLAG_BYVAL);
   803 #else
   804 		InitPass(pass[1], Valve_Vector, PassType_Object, PASSFLAG_BYVAL|PASSFLAG_OCTOR|PASSFLAG_OASSIGNOP);
   805 #endif
   806 		InitPass(pass[2], Valve_Bool, PassType_Basic, PASSFLAG_BYVAL);
   807 		if (!CreateBaseCall("DispatchKeyValueVector", ValveCall_Entity, &pass[2], pass, 2, &pCall))
   808 		{
   809 			return pContext->ThrowNativeError("\"DispatchKeyValueVector\" not supported by this mod");
   810 		} else if (!pCall) {
   811 			return pContext->ThrowNativeError("\"DispatchKeyValueVector\" wrapper failed to initialize");
   812 		}
   813 	}
   814 
   815 	bool ret;
   816 	START_CALL();
   817 	DECODE_VALVE_PARAM(1, thisinfo, 0);
   818 	DECODE_VALVE_PARAM(2, vparams, 0);
   819 	DECODE_VALVE_PARAM(3, vparams, 1);
   820 	FINISH_CALL_SIMPLE(&ret);
   821 
   822 	return ret ? 1 : 0;
   823 }
   824 
   825 static cell_t sm_GetClientAimTarget(IPluginContext *pContext, const cell_t *params)
   826 {
   827 	IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(params[1]);
   828 
   829 	if (!pPlayer)
   830 	{
   831 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
   832 	}
   833 	else if (!pPlayer->IsInGame())
   834 	{
   835 		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
   836 	}
   837 
   838 	return GetClientAimTarget(pPlayer->GetEdict(), params[2] ? true : false);
   839 }
   840 
   841 static cell_t sm_SetEntityModel(IPluginContext *pContext, const cell_t *params)
   842 {
   843 	static ValveCall *pCall = NULL;
   844 	if (!pCall)
   845 	{
   846 		ValvePassInfo pass[1];
   847 		InitPass(pass[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
   848 		if (!CreateBaseCall("SetEntityModel", ValveCall_Entity, NULL, pass, 1, &pCall))
   849 		{
   850 			return pContext->ThrowNativeError("\"SetEntityModel\" not supported by this mod");
   851 		} else if (!pCall) {
   852 			return pContext->ThrowNativeError("\"SetEntityModel\" wrapper failed to initialize");
   853 		}
   854 	}
   855 
   856 	START_CALL();
   857 	DECODE_VALVE_PARAM(1, thisinfo, 0);
   858 	DECODE_VALVE_PARAM(2, vparams, 0);
   859 	FINISH_CALL_SIMPLE(NULL);
   860 
   861 	return 1;
   862 }
   863 
   864 static cell_t GetPlayerDecalFile(IPluginContext *pContext, const cell_t *params)
   865 {
   866 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
   867 	if (player == NULL)
   868 	{
   869 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
   870 	}
   871 	if (!player->IsInGame())
   872 	{
   873 		return pContext->ThrowNativeError("Client %d is not in game", params[1]);
   874 	}
   875 
   876 	player_info_t info;
   877 	char *buffer;
   878 
   879 	if (!GetPlayerInfo(params[1], &info) || !info.customFiles[0])
   880 	{
   881 		return 0;
   882 	}
   883 
   884 	pContext->LocalToString(params[2], &buffer);
   885 	Q_binarytohex((byte *)&info.customFiles[0], sizeof(info.customFiles[0]), buffer, params[3]);
   886 
   887 	return 1;
   888 }
   889 
   890 static cell_t GetServerNetStats(IPluginContext *pContext, const cell_t *params)
   891 {
   892 	if (iserver == NULL)
   893 	{
   894 		return pContext->ThrowNativeError("IServer interface not supported, file a bug report.");
   895 	}
   896 
   897 	float in, out;
   898 	cell_t *pIn, *pOut;
   899 
   900 	pContext->LocalToPhysAddr(params[1], &pIn);
   901 	pContext->LocalToPhysAddr(params[2], &pOut);
   902 	iserver->GetNetStats(in, out);
   903 
   904 	*pIn = sp_ftoc(in);
   905 	*pOut = sp_ftoc(out);
   906 
   907 	return 1;
   908 }
   909 
   910 static cell_t WeaponEquip(IPluginContext *pContext, const cell_t *params)
   911 {
   912 	static ValveCall *pCall = NULL;
   913 	if (!pCall)
   914 	{
   915 		ValvePassInfo pass[1];
   916 		InitPass(pass[0], Valve_CBaseEntity, PassType_Basic, PASSFLAG_BYVAL);
   917 		if (!CreateBaseCall("WeaponEquip", ValveCall_Player, NULL, pass, 1, &pCall))
   918 		{
   919 			return pContext->ThrowNativeError("\"WeaponEquip\" not supported by this mod");
   920 		} else if (!pCall) {
   921 			return pContext->ThrowNativeError("\"WeaponEquip\" wrapper failed to initialize");
   922 		}
   923 	}
   924 
   925 	START_CALL();
   926 	DECODE_VALVE_PARAM(1, thisinfo, 0);
   927 	DECODE_VALVE_PARAM(2, vparams, 0);
   928 	FINISH_CALL_SIMPLE(NULL);
   929 
   930 	return 1;
   931 }
   932 
   933 static cell_t ActivateEntity(IPluginContext *pContext, const cell_t *params)
   934 {
   935 	static ValveCall *pCall = NULL;
   936 	if (!pCall)
   937 	{
   938 		if (!CreateBaseCall("Activate", ValveCall_Entity, NULL, NULL, 0, &pCall))
   939 		{
   940 			return pContext->ThrowNativeError("\"Activate\" not supported by this mod");
   941 		}
   942 		else if (!pCall)
   943 		{
   944 			return pContext->ThrowNativeError("\"Activate\" wrapper failed to initialize");
   945 		}
   946 	}
   947 
   948 	START_CALL();
   949 	DECODE_VALVE_PARAM(1, thisinfo, 0);
   950 	FINISH_CALL_SIMPLE(NULL);
   951 
   952 	return 1;
   953 }
   954 
   955 static cell_t SetClientInfo(IPluginContext *pContext, const cell_t *params)
   956 {
   957 	if (iserver == NULL)
   958 	{
   959 		return pContext->ThrowNativeError("IServer interface not supported, file a bug report.");
   960 	}
   961 
   962 	IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]);
   963 	IClient *pClient = iserver->GetClient(params[1] - 1);
   964 
   965 	if (player == NULL || pClient == NULL)
   966 	{
   967 		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
   968 	}
   969 	if (!player->IsConnected())
   970 	{
   971 		return pContext->ThrowNativeError("Client %d is not connected", params[1]);
   972 	}
   973 
   974 	static ValveCall *pCall = NULL;
   975 	if (!pCall)
   976 	{
   977 		ValvePassInfo params[2];
   978 		InitPass(params[0], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
   979 		InitPass(params[1], Valve_String, PassType_Basic, PASSFLAG_BYVAL);
   980 
   981 		if (!CreateBaseCall("SetUserCvar", ValveCall_Entity, NULL, params, 2, &pCall))
   982 		{
   983 			return pContext->ThrowNativeError("\"SetUserCvar\" not supported by this mod");
   984 		}
   985 		else if (!pCall)
   986 		{
   987 			return pContext->ThrowNativeError("\"SetUserCvar\" wrapper failed to initialize");
   988 		}
   989 	}
   990 
   991 /* TODO: Use UpdateUserSettings function for all engines */
   992 #if SOURCE_ENGINE == SE_DARKMESSIAH
   993 	static ValveCall *pUpdateSettings = NULL;
   994 	if (!pUpdateSettings)
   995 	{
   996 		if (!CreateBaseCall("UpdateUserSettings", ValveCall_Entity, NULL, NULL, 0, &pUpdateSettings))
   997 		{
   998 			return pContext->ThrowNativeError("\"SetUserCvar\" not supported by this mod");
   999 		}
  1000 		else if (!pUpdateSettings)
  1001 		{
  1002 			return pContext->ThrowNativeError("\"SetUserCvar\" wrapper failed to initialize");
  1003 		}
  1004 	}
  1005 #else
  1006 	static int changedOffset = -1;
  1007 
  1008 	if (changedOffset == -1)
  1009 	{	
  1010 		if (!g_pGameConf->GetOffset("InfoChanged", &changedOffset))
  1011 		{
  1012 			return pContext->ThrowNativeError("\"SetUserCvar\" not supported by this mod");
  1013 		}
  1014 	}
  1015 #endif
  1016 
  1017 	unsigned char *CGameClient = (unsigned char *)pClient - 4;
  1018 
  1019 	START_CALL();
  1020 	/* Not really a CBaseEntity* but this works */
  1021 	CBaseEntity **ebuf = (CBaseEntity **)vptr;
  1022 	*ebuf = (CBaseEntity *)CGameClient;
  1023 	DECODE_VALVE_PARAM(2, vparams, 0);
  1024 	DECODE_VALVE_PARAM(3, vparams, 1);
  1025 	FINISH_CALL_SIMPLE(NULL);
  1026 
  1027 #if SOURCE_ENGINE == SE_DARKMESSIAH
  1028 	unsigned char *args = pUpdateSettings->stk_get();
  1029 	*(void **)args = CGameClient;
  1030 	pUpdateSettings->call->Execute(args, NULL);
  1031 	pUpdateSettings->stk_put(args);
  1032 #else
  1033 	uint8_t* changed = (uint8_t *)(CGameClient + changedOffset);
  1034 	*changed = 1;
  1035 #endif
  1036 
  1037 	return 1;
  1038 }
  1039 
  1040 sp_nativeinfo_t g_Natives[] = 
  1041 {
  1042 	{"ExtinguishEntity",		ExtinguishEntity},
  1043 	{"ForcePlayerSuicide",		ForcePlayerSuicide},
  1044 	{"GivePlayerItem",			GiveNamedItem},
  1045 	{"GetPlayerWeaponSlot",		GetPlayerWeaponSlot},
  1046 	{"IgniteEntity",			IgniteEntity},
  1047 	{"RemovePlayerItem",		RemovePlayerItem},
  1048 	{"TeleportEntity",			TeleportEntity},
  1049 	{"SetClientViewEntity",		SetClientViewEntity},
  1050 	{"SetLightStyle",			SetLightStyle},
  1051 	{"SlapPlayer",				SlapPlayer},
  1052 	{"GetClientEyePosition",	GetClientEyePosition},
  1053 	{"GetClientEyeAngles",		GetClientEyeAngles},
  1054 	{"FindEntityByClassname",	FindEntityByClassname},
  1055 	{"CreateEntityByName",		CreateEntityByName},
  1056 	{"DispatchSpawn",			DispatchSpawn},
  1057 	{"DispatchKeyValue",		DispatchKeyValue},
  1058 	{"DispatchKeyValueFloat",	DispatchKeyValueFloat},
  1059 	{"DispatchKeyValueVector",	DispatchKeyValueVector},
  1060 	{"GetClientAimTarget",		sm_GetClientAimTarget},
  1061 	{"SetEntityModel",			sm_SetEntityModel},
  1062 	{"GetPlayerDecalFile",		GetPlayerDecalFile},
  1063 	{"GetServerNetStats",		GetServerNetStats},
  1064 	{"EquipPlayerWeapon",		WeaponEquip},
  1065 	{"ActivateEntity",			ActivateEntity},
  1066 	{"SetClientInfo",			SetClientInfo},
  1067 	{NULL,						NULL},
  1068 };