#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <engine>
#include <fakemeta>
#include <fun>
#include <hamsandwich>

new const PLUGIN[] = "Weapon awp xmas CSO"
new const VERSION[] = "1.0"
new const AUTHOR[] = "Mapaky9"

new const P_MODEL[] = "models/mapaky9/p_m24.mdl"
new const V_MODEL[] = "models/mapaky9/v_m24.mdl"
new const W_MODEL[] = "models/mapaky9/w_m24.mdl"

new cvar_enabled, cvar_cost, cvar_dmgmultiplier

#define is_valid_player(%1) (1 <= %1 <= 32)

new g_hasgun[33]
new g_Firegun[33]
new g_canShoot[33]
new Float:g_last_shot_time[33]
new g_restarted

#define TASK_HUDAMMO	    1337
#define ID_HUDAMMO (taskid - TASK_HUDAMMO)

public plugin_precache()
{
	precache_model(P_MODEL)
	precache_model(V_MODEL)
	precache_model(W_MODEL)

	precache_sound("mapaky9/aw50_clipin.wav")
	precache_sound("mapaky9/aw50_clipout.wav")
//	precache_sound("mapaky9/m24_draw.wav")
	precache_sound("mapaky9/aw50_foley1.wav")
	precache_sound("mapaky9/aw50_foley2.wav")
}

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	register_cvar("wpn_trg_version", VERSION, FCVAR_SERVER)
	
	register_clcmd("buy_m24", "CmdBuyIt")
	
	register_event("CurWeapon", "Event_CurrentWeapon", "be", "1=1")
	register_event("HLTV", "Event_NewRound", "a", "1=0", "2=0");
	register_event("TextMsg", "Event_GameRestart", "a", "2=#Game_Commencing", "2=#Game_will_restart_in");
	
	register_concmd("amx_m24", "cmd_givetrg", ADMIN_BAN, "<name>")

	register_forward(FM_SetModel, "fw_SetModel")
	RegisterHam(Ham_Killed, "player", "fw_PlayerKilled")
	RegisterHam(Ham_TakeDamage, "player", "fw_TakeDamage")
	register_forward(FM_Touch, "fw_Touch")
	register_forward(FM_CmdStart, "fw_CmdStart")
	register_forward(FM_PlayerPostThink, "fw_PlayerPostThink")
	register_forward(FM_UpdateClientData, "fw_UpdateClientData_Post", 1)
	
	cvar_enabled 		= register_cvar("amx_wpn_m24", 	"1")
	cvar_cost 		= register_cvar("amx_wpn_m24_cost", "0")
	cvar_dmgmultiplier	= register_cvar("amx_wpn_m24_dmg", "150")
}

public fw_TakeDamage(victim, inflictor, attacker, Float:damage)
{
	if(is_valid_player(attacker) && get_user_weapon(attacker) == CSW_SCOUT && g_hasgun[attacker] )
	{
		SetHamParamFloat(4, get_pcvar_float(cvar_dmgmultiplier))
	}
}

public client_connect(id)
{
	g_hasgun[id] = false
	g_canShoot[id] = false
	g_last_shot_time[id] = 0.0
}

public cmd_givetrg(id,level,cid)
{
	if (!cmd_access(id,level,cid,2))
		return PLUGIN_HANDLED;
	new arg[32];
	read_argv(1,arg,31);
	
	new player = cmd_target(id,arg,7);
	if (!player) 
		return PLUGIN_HANDLED;
	
	new name[32];
	get_user_name(player,name,31);
	
	give_item(player, "weapon_scout")
	cs_set_user_bpammo(id , CSW_SCOUT , 90)
	g_hasgun[player] = true
	
	return PLUGIN_HANDLED
}

public CmdBuyIt(id)
{
	if (get_pcvar_num(cvar_enabled) == 0)
		return PLUGIN_CONTINUE
	
	if (!is_user_alive(id))
		return PLUGIN_CONTINUE
		
/*	if (!cs_get_user_buyzone(id))
	{
		client_print(id, print_center, "You are not in buyzone")
		return PLUGIN_CONTINUE
	}
	
	if (g_hasgun[id] || user_has_weapon(id, CSW_SCOUT))
	{
		client_print(id, print_center, "You already own this weapon")
		return PLUGIN_CONTINUE
	}
	
	if (cs_get_user_vip(id))
	{
		client_print(id, print_center, "You are VIP.You can't buy this weapon")
		return PLUGIN_CONTINUE
	}
	
	if (cs_get_user_money(id) < get_pcvar_num(cvar_cost))
	{
		client_print(id, print_center, "You don't have enough money")
		return PLUGIN_CONTINUE
	}
*/	
	give_item(id, "weapon_awp")
	cs_set_user_money(id, cs_get_user_money(id) - get_pcvar_num(cvar_cost))
	g_hasgun[id] = true
	
	return PLUGIN_CONTINUE
}

public Event_CurrentWeapon(id)
{
	new weaponID = read_data(2)
	
	if (weaponID == CSW_SCOUT)
	{
		if (g_hasgun[id])
		{
			entity_set_string(id, EV_SZ_viewmodel, V_MODEL)
			
			entity_set_string(id, EV_SZ_weaponmodel, P_MODEL)
		}
	}
}

public Event_NewRound(id)
{
	if (g_restarted)
	{
		for (new i = 1; i <= get_maxplayers(); i++)
		{
			g_hasgun[i] = false
			remove_task(i+TASK_HUDAMMO)
		}
		g_restarted = false
	}
}

public Event_GameRestart()
{
	g_restarted = true
}
				
public fw_SetModel(ent, const model[])
{
	if (!pev_valid(ent))	
		return FMRES_IGNORED
	
	if(!equali(model, "models/w_awp.mdl")) 
		return FMRES_IGNORED
	
	new owner = pev(ent, pev_owner)
	new classname[33]; pev(ent, pev_classname, classname, charsmax(classname))
	
	if(equal(classname, "weaponbox"))
	{
		if(g_hasgun[owner])
		{
			g_hasgun[owner] = false
			
			engfunc(EngFunc_SetModel, ent, W_MODEL)
			
			set_task(0.1, "touch_fix", owner)
			
			remove_task(owner+TASK_HUDAMMO)
			
			return FMRES_SUPERCEDE
		}
	}
	
	return FMRES_IGNORED

}

public fw_PlayerKilled(victim, attacker, shouldgib)
{
	if (g_hasgun[victim])		
	{
		g_hasgun[victim] = false
		g_canShoot[victim] = false
		g_Firegun[victim] = false
		
		remove_task(victim+TASK_HUDAMMO)
	}
}

public fw_Touch(ent, toucher)
{
	if(!pev_valid(ent) || !pev_valid(toucher))
		return FMRES_IGNORED;
	
	new model[33], toucherclass[33], entityclass[33]
	pev(ent, pev_model, model, charsmax(model))
	pev(ent, pev_classname, entityclass, charsmax(entityclass))
	pev(toucher, pev_classname, toucherclass, charsmax(toucherclass))
	
	if (!equali(toucherclass, "player") || !equali(entityclass, "weaponbox"))
		return FMRES_IGNORED
	
	if(equali(model, W_MODEL))
	{
		if(allowed_toucher(toucher))
		{
			g_hasgun[toucher] = true
			g_canShoot[toucher] = true
			g_Firegun[toucher] = false
		}
	}
	
	return FMRES_IGNORED;
}

public fw_CmdStart(id, uc_handle, seed) 
{
	if (!is_user_alive(id))
		return FMRES_IGNORED
		
	if (!g_hasgun[id])
		return FMRES_IGNORED
		
	new ammo, clip, weapon = get_user_weapon(id, clip, ammo)
	
	if (weapon != 5)
		return FMRES_IGNORED
		
	new buttons = get_uc(uc_handle, UC_Buttons)
	
	if(buttons & IN_ATTACK)
	{
		g_Firegun[id] = true
	
		buttons &= ~IN_ATTACK
		set_uc(uc_handle, UC_Buttons, buttons)
	}
	else 
		g_Firegun[id] = false	
		
	return FMRES_HANDLED
}

public fw_PlayerPostThink(id)
{
	if (!is_user_alive(id))
		return FMRES_IGNORED
		
	if (!g_hasgun[id])
		return FMRES_IGNORED
		
	new ammo, clip, weapon = get_user_weapon(id, clip, ammo)
	
	if (weapon != 5)
		return FMRES_IGNORED

	return FMRES_IGNORED
}

public fw_UpdateClientData_Post(id, sendweapons, cd_handle) 
{
	if (!is_user_alive(id))
		return FMRES_IGNORED
		
	if (!g_hasgun[id])
		return FMRES_IGNORED
	
	new ammo, clip, weapon = get_user_weapon(id, clip, ammo)
	
	if (weapon != 5)
		return FMRES_IGNORED
		
	set_cd(cd_handle, CD_ID, 0)
	return FMRES_HANDLED
}

public touch_fix(id)
{
	if (g_hasgun[id])
		g_hasgun[id] = false
}

stock allowed_toucher(player)
{
	if (g_hasgun[player])
		return false
	
	return true
}