//based on a plugin I had made for superhero mod(Squall) which in turn was based on Avalanche's i_aim_good plugin

#include <amxmodx>
#include <fakemeta>
#include <xs>
#include <zombieplague>

/*================================================================================
 [Plugin Customization]
=================================================================================*/

new const knifemodel[] = "models/v_glock18.mdl";
new const gunsound[] = "weapons/glock18-2.wav";
new const gItemName[] = "Infection Glock";
const gItemCost = 24

/*================================================================================
 [End Customization]
 ================================================================================*/

// GLOBAL VARIABLES
new oldmodel[33][64];
new gBullets[33];
new gItemid;

//pcvars
new pcvar_bullets;
/*============================================================================*/
public plugin_init()
{
	register_plugin("[ZP] Infection Glock", "0.1", "Random1");
	
	// cvars
	pcvar_bullets = register_cvar("zp_infgun_bullets", "7");
	
	// Register our item to zombie plague
	gItemid = zp_register_extra_item(gItemName, gItemCost, ZP_TEAM_ZOMBIE);	

	// Knife Model
	register_event( "CurWeapon", "weaponChange", "be", "1=1", "2=29" );
		
	//knife hack part
	register_forward(FM_TraceLine,"fw_traceline");
	register_forward(FM_EmitSound, "fw_emitsound")
}

public zp_extra_item_selected(player, itemid)
	if (itemid == gItemid) {
		gBullets[player] += get_pcvar_num(pcvar_bullets);
		weaponChange(player);
	}

//----------------------------------------------------------------------------------------------
public plugin_precache()	//we still precache the files in case the user decides to use a custom model and/or sound
{
	engfunc( EngFunc_PrecacheSound, gunsound );
	engfunc( EngFunc_PrecacheModel, knifemodel );
}
//----------------------------------------------------------------------------------------------
public weaponChange(id)
	if ( is_user_alive(id) && zp_get_user_zombie(id) && gBullets[id] > 0 )
	{
		pev(id, pev_viewmodel2, oldmodel[id], charsmax(oldmodel[]));
		set_pev(id, pev_viewmodel2, knifemodel);
	}
		
//----------------------------------------------------------------------------------------------
public fw_traceline(Float:v1[3],Float:v2[3],noMonsters,id)
{
	if( !is_user_alive(id) || !zp_get_user_zombie(id) || gBullets[id] <= 0 )
		return;

	static Float:flMyAim[3], ent, body;
	static weapid;
	weapid = get_user_weapon(id);
 	if(weapid != CSW_KNIFE)
		return;

	// get crosshair aim
	_get_aim_origin(id, flMyAim);

	// set crosshair aim
	set_tr(TR_vecEndPos,flMyAim);
	
	// get ent looking at
	get_user_aiming(id,ent,body);
	
	// if looking at something
	if(pev_valid(ent)) {
		set_tr(TR_flFraction,0.1); // 1.0 == no hit, < 1.0 == hit
		set_tr(TR_pHit,ent); // entity hit
		set_tr(TR_iHitgroup,body); // bodypart hit				
	}
}
//----------------------------------------------------------------------------------------------
public fw_emitsound(id, channel, const sample[], Float:volume, Float:attn, flags, pitch)
{
	if ( !is_user_alive(id) || !zp_get_user_zombie(id) || gBullets[id] <= 0 )
		return FMRES_IGNORED;
		
	if ( !equali(sample,"weapons/knife_deploy1.wav", 25) && equali(sample,"weapons/knife_", 14) )	//9 sounds dealing with knife, 1 of which not useful
	{
		gBullets[id]--;
		if ( 0 < gBullets[id] < 5 )
			client_print( id, print_center, "You Have %d Bullet%s Left", gBullets[id], gBullets[id] == 1 ? "" : "s" );
		
		engfunc(EngFunc_EmitSound, id, channel, gunsound, volume, attn, flags, pitch);
		if ( !gBullets[id] )	//force our knife model to revert to normal since we just ran out of bullets
			set_pev(id, pev_viewmodel2, oldmodel[id]);
			
		return FMRES_SUPERCEDE;
	}
	return FMRES_IGNORED;
}
//---------------------------------------------------------------------------------------
_get_aim_origin(index, Float:origin[3])
{
	static Float:start[3], Float:view_ofs[3];
	pev(index, pev_origin, start);
	pev(index, pev_view_ofs, view_ofs);
	xs_vec_add(start, view_ofs, start);

	static Float:dest[3];
	pev(index, pev_v_angle, dest);
	engfunc(EngFunc_MakeVectors, dest);
	global_get(glb_v_forward, dest);
	xs_vec_mul_scalar(dest, 9999.0, dest);
	xs_vec_add(start, dest, dest);

	engfunc(EngFunc_TraceLine, start, dest, 0, index, 0);
	get_tr2(0, TR_vecEndPos, origin);

	return 1;
}