/* AMX Mod script.
*
* AWSM Limit 0.21
*  by JustinHoMi
*
* Allows no more than 2 awsms per team
*

Cvars:

awsm_ctrl "1"   1/0: on/off
awsm_ctrl_loadout "2"   num of awsm per team


*
*/

#include <amxmod>

new plist[32] = { 0, ... }	// 0 = no awsm; 1 = carrying awsm
new awsm_count[3]			// 1 = T; 2 = CT

/* handles restricting the menu */
public menu_awsm(id,key) {
	if(get_cvar_num("awsm_ctrl")==1)
	{
	new team = get_user_team(id)
	if(awsm_count[team] >= get_cvar_num("awsm_ctrl_loadout"))
	{
		engclient_cmd(id,"menuselect","10")
		client_print(id,print_center,"Request Denied.  Your team has reached it's AWP Limit.")
		return PLUGIN_HANDLED
	}
	}
	return PLUGIN_CONTINUE
}

/* handles when a player drops their weapon */
public handle_drop(id) {
	if(get_cvar_num("awsm_ctrl")==1)
	{
	if (plist[id] == 1)
	{
		new tmp1,tmp2
		new curweapon = get_user_weapon(id,tmp1,tmp2)

		if (curweapon == 18)
		{
			plist[id] = 0
			new team = get_user_team(id)
			awsm_count[team]--
		}
	}
	}
	return PLUGIN_CONTINUE
}

/* handles when a player picks up an awsm */
public handle_pickup(id) {
	if(get_cvar_num("awsm_ctrl")==1)
	{
	new team = get_user_team(id)
	if (awsm_count[team] >= get_cvar_num("awsm_ctrl_loadout"))
	{
		new id_s[3]
		num_to_str(id,id_s,3)
		set_task(0.3,"delayed_drop",0,id_s,3)
	}
	else
	{
		plist[id] = 1
		awsm_count[team]++
	}
	}
}

/* delays the drop slightly to give the server time to register that they picked up an awsm */
public delayed_drop(id_s[])
{
	new idx = str_to_num(id_s)
	client_print(idx,print_center,"Request Denied.  Your team has reached it's AWP Limit.")
	client_cmd(idx,"drop")
/*
	new Model[51]
	new Maxplayers = get_maxplayers()
	new gunbox = find_ent_by_class(Maxplayers + 1, "weaponbox")
	entity_get_string(gunbox, EV_SZ_model, Model, 50)
	
	if(equali(model, "models/w_deagle.mdl")  || (model, "models/w_awp.mdl"))
	        call_think(gunbox)

*/
}

/* removes awsm when player dies */
public handle_death() {
	if(get_cvar_num("awsm_ctrl")==1)
	{
	new idx = read_data(2)

	if(plist[idx] == 1)
	{
		new team = get_user_team(idx)
		awsm_count[team]--

		plist[idx] = 0
	}
	}
}

/* clear vars when player connects */
public client_connect(id) {
	plist[id] = 0 // just to make sure
}

/* clear vars when player disconnects */
public client_disconnect(id) {
	if (plist[id] == 1)
	{
		new team = get_user_team(id)
		awsm_count[team]--
	}
	plist[id] = 0
}

public plugin_init(){
    register_plugin("AWSM Control","0.21","JustinHoMi")
    register_menucmd(-31,1<<5,"menu_awsm")
    register_clcmd("drop","handle_drop")
    register_event("WeapPickup","handle_pickup","b","1=18")
    register_event("DeathMsg","handle_death","a")
    register_cvar("awsm_ctrl_loadout", "1")          //def=2 [2 AWSMs per team]
    register_cvar("awsm_ctrl", "0")                  //def=1 [on]

    return PLUGIN_CONTINUE
}