/*
	v1.0 - При низком онлайне отбирает AWP [хоть купленное, хоть взято с VIP/Admin оружейки]
	v2.0 - Добавлены проверки на: ботов, hltv, игроков сидящих в спекте.
	v3.0 - Добавлена проверка на покупку AWP [Спасибо: http://c-s.net.ua/forum/topic71418.html]
	v3.0 - Добавлен квар awp_players_low
	v4.0 - Добавлен лимит + квары awp_limit, awp_limit_max и awp_limit_immun
*/
#include < amxmodx >
#include < cstrike >
#include < hamsandwich >
#include < engine >
#include < fakemeta_util >

#define FLAG ADMIN_KICK

new T, CT
new bool: useawp[32]

public plugin_init()
{
	register_plugin("AWPoff", "4.0", "PAffAEJIkA :3");
	
	register_logevent("round_restart", 2, "1&Restart_Round_");
	
	register_menucmd(register_menuid("BuyRifle", true), (1<<4|1<<5), "OldMenusHook");
	register_clcmd("menuselect", "ClCmdMenuSelect");
	register_clcmd("awp", "cmdawp");
	register_clcmd("magnum", "cmdawp");
	register_clcmd("drop", "ClCmdDrop");
	
	RegisterHam(Ham_Touch, "weapon_awp", "fwdTouch");
	
	register_event("CurWeapon", "curwp", "b");
	register_event("WeapPickup", "wp", "b");
	
	register_event("DeathMsg", "Death", "a");
	
	register_cvar( "awp_players_low", "6" );
	register_cvar( "awp_limit", "1" );
	register_cvar( "awp_limit_max", "2" );
	register_cvar( "awp_limit_immun", "1" );
	
	new mapname[32]; get_mapname(mapname, charsmax(mapname));	
	new maps[][] = {"awp_", "aim_"};
	for(new i; i < sizeof maps; i++)
	{
		if(containi(mapname, maps[i]) != -1)
		{
			pause("ad");
			return;
		}
	}
}

public client_connect(id)
{
	useawp[id] = false
}

public client_disconnect(id)
{
	if(useawp[id] == true)
	{
		useawp[id] = false
		if(cs_get_user_team(id) == CS_TEAM_T)
			T--;
		else if(cs_get_user_team(id) == CS_TEAM_CT)
			CT--;
	}
}

public round_restart()
{
	for(new id = 1; id <= get_maxplayers(); id++)
	{
		if(useawp[id] == true)
		{
			useawp[id] = false
		}
	}
	T = 0
	CT = 0
}

public Death()
	deathawp(read_data(2))
	

deathawp(id)
{
	if(get_cvar_num("awp_limit_immun") == 1 && get_user_flags(id) & FLAG)			return 0;
	
	if(is_user_hltv(id) || is_user_bot(id))			return 0;
	
	if(useawp[id] == true)
	{
		useawp[id] = false
		if(cs_get_user_team(id) == CS_TEAM_T)
			T--;
		else if(cs_get_user_team(id) == CS_TEAM_CT)
			CT--;
	}
	return PLUGIN_CONTINUE;
}

public cmdawp(id)
{	
	if(is_user_hltv(id) || is_user_bot(id))			return 0;
	
	if(get_playersnum() < get_cvar_num("awp_players_low"))
	{
		if(!(cs_get_user_team(id) == CS_TEAM_SPECTATOR))
		{
			client_print(id, print_center, "Kevesebb mint 6-n vannak a szerveren, Tilos az AWP!")
			return PLUGIN_HANDLED;
		}
	}	
	else if(get_cvar_num("awp_limit") == 1)
	{
		if(get_cvar_num("awp_limit_immun") == 1 && get_user_flags(id) & FLAG)			return 0;
		
		if(useawp[id] == true)			return 0;
			
		if(cs_get_user_money(id) >= 4750)
		{
			if(cs_get_user_team(id) == CS_TEAM_T)
			{
				if(T < get_cvar_num("awp_limit_max"))
				{
					T++;		
					useawp[id] = true
				}
				else
				{
					client_print(id, print_center, "Elerte a max AWP -s jatekosok szamat!")
					return PLUGIN_HANDLED;
				}
			}
			else if(cs_get_user_team(id) == CS_TEAM_CT)
			{
				if(CT < get_cvar_num("awp_limit_max"))
				{
					CT++;		
					useawp[id] = true
				}
				else
				{
					client_print(id, print_center, "Elerte a max AWP -s jatekosok szamat!")
					return PLUGIN_HANDLED;
				}
			}
		}
	}
	return PLUGIN_CONTINUE;
}

public OldMenusHook(id, iKey)
{
	if(is_user_hltv(id) || is_user_bot(id))			return 0;
	
	if(get_playersnum() < get_cvar_num("awp_players_low"))
	{
		return CheckKeys(id, iKey);
	}
	else if(get_cvar_num("awp_limit") == 1)
	{
		if(get_cvar_num("awp_limit_immun") == 1 && get_user_flags(id) & FLAG)			return 0;
		
		if(cs_get_user_team(id) == CS_TEAM_T)
		{
			if(T >= get_cvar_num("awp_limit_max"))
				return CheckKeys(id, iKey);
		}
		else if(cs_get_user_team(id) == CS_TEAM_CT)
		{
			if(CT >= get_cvar_num("awp_limit_max"))
				return CheckKeys(id, iKey);
		}
	}
	return PLUGIN_CONTINUE;
}

public ClCmdMenuSelect(id)
{
	if(!is_user_alive(id) || get_pdata_int(id, 205) != 6)
	return PLUGIN_CONTINUE;

	new sSlot[3]; 
	if(read_argv(1, sSlot, charsmax(sSlot)))
	return CheckKeys(id, str_to_num(sSlot));
	return PLUGIN_CONTINUE;	
}

CheckKeys(id, iKey)
{
	new team = get_user_team(id);
	if((team == 1 && iKey != 4) || (team == 2 && iKey != 5))
	return PLUGIN_CONTINUE;
	cmdawp(id);
	return PLUGIN_HANDLED;	
}

public curwp(id)
{	
	if(is_user_hltv(id) || is_user_bot(id))			return 0;
	
	if(get_playersnum() < get_cvar_num("awp_players_low"))
	{
		if(pev(id, pev_weapons) & (1 << CSW_AWP))
		{
			if(!(cs_get_user_team(id) == CS_TEAM_SPECTATOR))
			{
				client_print(id, print_center, "Kevesebb mint 6-n vannak a szerveren, Tilos az AWP!")
				set_task(0.1, "drop", id)
			}
		}
	}
	return PLUGIN_HANDLED
}

public wp(id)
{	
	if(is_user_hltv(id) || is_user_bot(id))			return 0;
	
	if(get_playersnum() < get_cvar_num("awp_players_low"))
	{
		if(pev(id, pev_weapons) & (1 << CSW_AWP))
		{
			if(!(cs_get_user_team(id) == CS_TEAM_SPECTATOR))
			{
				client_print(id, print_center, "Elerte a max AWP -s jatekosok szamat!")
				set_task(0.1, "drop", id)
			}
		}
	}
	else if(get_cvar_num("awp_limit") == 1)
	{	
		if(get_cvar_num("awp_limit_immun") == 1 && get_user_flags(id) & FLAG)			return 0;
		
		if(useawp[id] == true)			return 0;
		
		if(pev(id, pev_weapons) & (1 << CSW_AWP))
		{
			if(cs_get_user_team(id) == CS_TEAM_T)
			{
				if(T < get_cvar_num("awp_limit_max"))
				{
					T++;		
					useawp[id] = true
				}
				else
				{
					client_print(id, print_center, "Elerte a max AWP -s jatekosok szamat!")
					set_task(0.1, "drop", id)
				}
			}
			else if(cs_get_user_team(id) == CS_TEAM_CT)
			{
				if(CT < get_cvar_num("awp_limit_max"))
				{
					CT++;		
					useawp[id] = true
				}
				else
				{
					client_print(id, print_center, "Elerte a max AWP -s jatekosok szamat!")
					set_task(0.1, "drop", id)
				}
			}
		}
	}	
	return PLUGIN_HANDLED
}

public drop(id)
{
	fm_strip_user_gun(id, CSW_AWP)
}

public fwdTouch(ent, id)
{
	if(is_user_hltv(id) || is_user_bot(id))			return 0;
	
	if(get_playersnum() < get_cvar_num("awp_players_low"))
	{
		if(!(cs_get_user_team(id) == CS_TEAM_SPECTATOR))
		{
			client_print(id, print_center, "Kevesebb mint 6-n vannak a szerveren, Tilos az AWP!")
			return HAM_SUPERCEDE;
		}
	}
	else if(get_cvar_num("awp_limit") == 1)
	{
		if(get_cvar_num("awp_limit_immun") == 1 && get_user_flags(id) & FLAG)			return 0;
		
		if(cs_get_user_team(id) == CS_TEAM_T)
		{
			if(T >= get_cvar_num("awp_limit_max"))
			{
				client_print(id, print_center, "Elerte a max AWP -s jatekosok szamat!")
				return HAM_SUPERCEDE;
			}
		}
		else if(cs_get_user_team(id) == CS_TEAM_CT)
		{
			if(CT >= get_cvar_num("awp_limit_max"))
			{
				client_print(id, print_center, "Elerte a max AWP -s jatekosok szamat!")	
				return HAM_SUPERCEDE;
			}
		}
	}
	return PLUGIN_HANDLED
}

public ClCmdDrop(pPlayer)
{
	if(get_cvar_num("awp_limit_immun") == 1 && get_user_flags(pPlayer) & FLAG)			return 0;
	
	if(is_user_hltv(pPlayer) || is_user_bot(pPlayer))			return 0;
	
	if(read_argc() == 1)
	{
		new pEntity = get_pdata_cbase(pPlayer, 373);
		
		if(!is_valid_ent(pEntity))
			return PLUGIN_CONTINUE;
		
		if(cs_get_weapon_id(pEntity) == CSW_AWP)
		{
			if(cs_get_user_team(pPlayer) == CS_TEAM_T)
				T--;		
			else if(cs_get_user_team(pPlayer) == CS_TEAM_CT)
				CT--;	

			useawp[pPlayer] = false
		}
	}
	return PLUGIN_CONTINUE;
}