/**
 * vim: set ts=4 :
 * =============================================================================
 * Source SDK Hooks Extension
 * Copyright (C) 2010-2012 Nicholas Hastings
 * Copyright (C) 2009-2010 Erik Minekus
 * =============================================================================
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, version 3.0, as published by the
 * Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * As a special exception, AlliedModders LLC gives you permission to link the
 * code of this program (as well as its derivative works) to "Half-Life 2," the
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
 * by the Valve Corporation.  You must obey the GNU General Public License in
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
 * this exception to all derivative works.  AlliedModders LLC defines further
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
 * or <http://www.sourcemod.net/license.php>.
 *
 * Version: $Id$
 */

#include "extension.h"
#include "natives.h"
#include <compat_wrappers.h>

#if defined SH_DECL_MANUALEXTERN1
SH_DECL_MANUALEXTERN1(OnTakeDamage, int, CTakeDamageInfoHack &);
SH_DECL_MANUALEXTERN3_void(Weapon_Drop, CBaseCombatWeapon *, const Vector *, const Vector *);
#endif

cell_t Native_Hook(IPluginContext *pContext, const cell_t *params)
{
	int entity = (int)params[1];
	SDKHookType type = (SDKHookType)params[2];
	IPluginFunction *callback = pContext->GetFunctionById(params[3]);
	HookReturn ret = g_Interface.Hook(entity, type, callback);
	switch (ret)
	{
		case HookRet_InvalidEntity:
			pContext->ThrowNativeError("Entity %d is invalid", entity);
			break;
		case HookRet_InvalidHookType:
			pContext->ThrowNativeError("Invalid hook type specified");
			break;
		case HookRet_NotSupported:
			pContext->ThrowNativeError("Hook type not supported on this game");
			break;
		case HookRet_BadEntForHookType:
		{
			const char * pClassname = gamehelpers->GetEntityClassname(PEntityOfEntIndex(gamehelpers->ReferenceToIndex(params[1])));
			if (!pClassname)
			{
				pContext->ThrowNativeError("Hook type not valid for this type of entity (%i).", entity);
			}
			else
			{
				pContext->ThrowNativeError("Hook type not valid for this type of entity (%s)", pClassname);
			}
			
			break;
		}
	}

	return 0;
}

cell_t Native_HookEx(IPluginContext *pContext, const cell_t *params)
{
	int entity = (int)params[1];
	SDKHookType type = (SDKHookType)params[2];
	IPluginFunction *callback = pContext->GetFunctionById(params[3]);
	HookReturn ret = g_Interface.Hook(entity, type, callback);
	if (ret == HookRet_Successful)
		return true;

	return false;
}

cell_t Native_Unhook(IPluginContext *pContext, const cell_t *params)
{
	int entity = (int)params[1];
	SDKHookType type = (SDKHookType)params[2];
	IPluginFunction *callback = pContext->GetFunctionById(params[3]);

	for(int i = g_HookList.Count() - 1; i >= 0; i--)
	{
		if(g_HookList[i].entity == entity && g_HookList[i].type == type && g_HookList[i].callback == callback)
			g_Interface.Unhook(i);
	}

	return 0;
}

cell_t Native_TakeDamage(IPluginContext *pContext, const cell_t *params)
{
// todo: fix code to not require this >.<
#if !defined SH_DECL_MANUALEXTERN1
	pContext->ThrowNativeError("SDKHooks_TakeDamage is not supported on this engine.");
#else
	CBaseEntity *pVictim = UTIL_GetCBaseEntity(params[1]);
	if (!pVictim)
		return pContext->ThrowNativeError("Invalid entity index %d for victim", params[1]);
	
	CBaseEntity *pInflictor = UTIL_GetCBaseEntity(params[2]);
	if (!pInflictor)
		return pContext->ThrowNativeError("Invalid entity index %d for inflictor", params[2]);

	CBaseEntity *pAttacker = NULL;
	if (params[3] != -1)
	{
		pAttacker = UTIL_GetCBaseEntity(params[3]);
		if (!pAttacker)
		{
			return pContext->ThrowNativeError("Invalid entity index %d for attackerr", params[3]);
		}
	}

	float flDamage = sp_ctof(params[4]);
	int iDamageType = params[5];

	CBaseEntity *pWeapon = NULL;
	if (params[6] != -1)
	{
		pAttacker = UTIL_GetCBaseEntity(params[6]);
		if (!pAttacker)
		{
			return pContext->ThrowNativeError("Invalid entity index %d for weapon", params[6]);
		}
	}

	Vector vecDamageForce = Vector(0.0f, 0.0f, 0.0f);
	cell_t *addr;
	int err;
	if ((err = pContext->LocalToPhysAddr(params[7], &addr)) != SP_ERROR_NONE)
	{
		return pContext->ThrowNativeError("Could not read damageForce vector");
	}

	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
	{
		vecDamageForce = Vector(
			sp_ctof(addr[0]),
			sp_ctof(addr[1]),
			sp_ctof(addr[2]));
	}

	Vector vecDamagePosition = vec3_origin;
	if ((err = pContext->LocalToPhysAddr(params[8], &addr)) != SP_ERROR_NONE)
	{
		return pContext->ThrowNativeError("Could not read damagePosition vector");
	}

	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
	{
		vecDamagePosition = Vector(
			sp_ctof(addr[0]),
			sp_ctof(addr[1]),
			sp_ctof(addr[2]));
	}

	CTakeDamageInfoHack info(pInflictor, pAttacker, flDamage, iDamageType, pWeapon, vecDamageForce, vecDamagePosition);
	SH_MCALL(pVictim, OnTakeDamage)((CTakeDamageInfoHack &)info);
#endif

	return 0;
}

cell_t Native_DropWeapon(IPluginContext *pContext, const cell_t *params)
{
// todo: fix code to not require this >.<
#if !defined SH_DECL_MANUALEXTERN1
	pContext->ThrowNativeError("SDKHooks_DropWeapon is not supported on this engine.");
#else
	CBaseEntity *pPlayer = UTIL_GetCBaseEntity(params[1], true);
	if (!pPlayer)
		return pContext->ThrowNativeError("Invalid client index %d", params[1]);
	
	CBaseEntity *pWeapon = UTIL_GetCBaseEntity(params[2]);
	if (!pWeapon)
		return pContext->ThrowNativeError("Invalid entity index %d for weapon", params[2]);

	sm_sendprop_info_t spi;
	IServerUnknown *pUnk = (IServerUnknown *)pWeapon;
	IServerNetworkable *pNet = pUnk->GetNetworkable();

	if (!UTIL_FindDataTable(pNet->GetServerClass()->m_pTable, "DT_BaseCombatWeapon", &spi, 0))
		return pContext->ThrowNativeError("Entity index %d is not a weapon", params[2]);

	if (!gamehelpers->FindSendPropInfo("CBaseCombatWeapon", "m_hOwnerEntity", &spi))
		return pContext->ThrowNativeError("Invalid entity index %d for weapon", params[2]);

	CBaseHandle &hndl = *(CBaseHandle *)((uint8_t *)pWeapon + spi.actual_offset);
	if (params[1] != hndl.GetEntryIndex())
		return pContext->ThrowNativeError("Weapon %d is not owned by client %d", params[2], params[1]);

	Vector vecTarget;
	cell_t *addr;
	int err;
	if ((err = pContext->LocalToPhysAddr(params[3], &addr)) != SP_ERROR_NONE)
	{
		return pContext->ThrowNativeError("Could not read vecTarget vector");
	}

	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
	{
		vecTarget = Vector(
			sp_ctof(addr[0]),
			sp_ctof(addr[1]),
			sp_ctof(addr[2]));
	}
	else
	{
		SH_MCALL(pPlayer, Weapon_Drop)((CBaseCombatWeapon *)pWeapon, NULL, NULL);
		return 0;
	}

	Vector vecVelocity;
	if ((err = pContext->LocalToPhysAddr(params[8], &addr)) != SP_ERROR_NONE)
	{
		return pContext->ThrowNativeError("Could not read vecVelocity vector");
	}

	if (addr != pContext->GetNullRef(SP_NULL_VECTOR))
	{
		vecVelocity = Vector(
			sp_ctof(addr[0]),
			sp_ctof(addr[1]),
			sp_ctof(addr[2]));
	}
	else
	{
		SH_MCALL(pPlayer, Weapon_Drop)((CBaseCombatWeapon *)pWeapon, &vecTarget, NULL);
		return 0;
	}

	SH_MCALL(pPlayer, Weapon_Drop)((CBaseCombatWeapon *)pWeapon, &vecTarget, &vecVelocity);
#endif

	return 0;
}
