/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <codmod>
#include <fakemeta>
#include <fakemeta_stocks>
#include <cstrike>
#include <engine>

new const perk_name[] = "AWP Scope";
new const perk_desc[] = "Posiadasz lunete, namierzasz bronia niczym AWP";
new bool: ma_perk[33], change_zoom[33], gUSPorM4[33], gGLorFMS[33];

public plugin_init() 
{
	register_plugin(perk_name, "1.0", "Hleb");
	register_forward(FM_CmdStart, "fwdCmdStart", 0)
	cod_register_perk(perk_name, perk_desc);
	register_event("CurWeapon", "eventCurWeaponActive","be","1=1");
}
public cod_perk_enabled(id)
{
	ma_perk[id] = true;
	cs_set_user_zoom(id, CS_SET_NO_ZOOM, 1)
}	
public cod_perk_disabled(id)
{
	ma_perk[id] = false;
	cs_set_user_zoom(id, CS_SET_NO_ZOOM, 1)
}
public client_disconnect(id)
{
	ma_perk[id] = false;
}	
public fwdCmdStart(id, ucHandle)
{
	static button, wp
	
	button = get_uc(ucHandle, UC_Buttons);
	wp = get_user_weapon(id);
	
	if(ma_perk[id] && button & IN_ATTACK2)
	{
		change_zoom[id] = true;
		if(wp == CSW_M4A1 || wp == CSW_USP || wp == CSW_FAMAS || wp == CSW_GLOCK18 || wp == CSW_AUG || wp == CSW_SG552)
			set_uc(ucHandle, UC_Buttons, button &= ~IN_ATTACK2)
		if(wp == CSW_AWP || wp == CSW_SCOUT || wp == CSW_SG550 || wp == CSW_G3SG1 || wp == CSW_KNIFE || wp == CSW_C4)
			return FMRES_IGNORED;
		if(!(pev( id, pev_oldbuttons ) & IN_ATTACK2 ))	
			SetZoom(id);
	}
	if(button & IN_RELOAD)
		cs_set_user_zoom(id, CS_SET_NO_ZOOM, 1);
	return FMRES_IGNORED;
}
public SetZoom(id)
{
	if(cs_get_user_zoom(id) == CS_SET_NO_ZOOM && change_zoom[id])
	{
		cs_set_user_zoom(id, CS_SET_FIRST_ZOOM, 1);
		emit_sound( id, CHAN_ITEM, "weapons/zoom.wav", 0.20, 2.40, 0, 100 )
		change_zoom[id] = false;
	}
	else if(cs_get_user_zoom(id) == CS_SET_FIRST_ZOOM && change_zoom[id])
	{
		cs_set_user_zoom(id, CS_SET_SECOND_ZOOM, 1);
		emit_sound( id, CHAN_ITEM, "weapons/zoom.wav", 0.20, 2.40, 0, 100 )
		change_zoom[id] = false;
	}
	else if(cs_get_user_zoom(id) == CS_SET_SECOND_ZOOM && change_zoom[id])
	{
		cs_set_user_zoom(id, CS_SET_NO_ZOOM, 1);
		emit_sound( id, CHAN_ITEM, "weapons/zoom.wav", 0.20, 2.40, 0, 100 )
		change_zoom[id] = false;
	}
}
public eventCurWeaponActive(id)
{
	if(ma_perk[id])
	{
		new weapon=read_data(2);
		new bool:s_USP=(weapon==CSW_USP);
		new bool:s_M4=(weapon==CSW_M4A1);
		new bool:b_FAMAS=(weapon==CSW_FAMAS);
		new bool:b_GLOCK=(weapon==CSW_GLOCK18);
		gUSPorM4[id]=(s_M4 || s_USP);
		gGLorFMS[id]=(b_GLOCK || b_FAMAS);
		if(gUSPorM4[id])
		{
			new szClass[33];
			if(s_USP)
				copy(szClass, 32, "weapon_usp");
			else if(s_M4)
				copy(szClass, 32, "weapon_m4a1");
			else return;
			new ent=get_user_weapon_ent(id, szClass);
			if(ent && cs_get_weapon_silen(ent) == 1)
				cs_set_weapon_silen(ent,0,0);
		}
		if(gGLorFMS[id])
		{
			new szClass[33];
			if(b_GLOCK)
				copy(szClass, 32, "weapon_glock18");
			else if (b_FAMAS)
				copy(szClass, 32, "weapon_famas");
			else return;
			new ent=get_user_weapon_ent(id, szClass);
			if(ent && cs_get_weapon_burst(ent) == 1)
				cs_set_weapon_burst(ent, 0);
		}
		
	}
}
stock Float:fm_entity_range(ent1, ent2) {
	new Float:origin1[3], Float:origin2[3]
	pev(ent1, pev_origin, origin1)
	pev(ent2, pev_origin, origin2)

	return get_distance_f(origin1, origin2)
}
stock get_user_weapon_ent(id, const wname[]){
	new ent=EF_FindEntityByString(-1, "classname",wname); 
	while(ent){
		if(fm_entity_range(id, ent)<10.0){
			return ent;
		}
		ent=EF_FindEntityByString(ent, "classname",wname); 
	}
	return 0;
}

		
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1045\\ f0\\ fs16 \n\\ par }
*/
