#include <amxmodx>
#include <engine>
#include <fakemeta>
#include <fakemeta_util>
#include <hamsandwich>
#include <cstrike>
#include <byu_money>
#include <fun>
#include <zombieplague>
#include <18_ds>

#define PLUGIN "Compound Bow"
#define VERSION "2.0"
#define AUTHOR "m4m3ts"

#define CSW_CBOW CSW_GALIL
#define weapon_cbow "weapon_galil"
#define old_event "events/galil.sc"
#define old_w_model "models/w_galil.mdl"
#define WEAPON_SECRETCODE 19498


#define DEFAULT_AMMO 60
#define RELOAD_TIME 0.435
#define STAB_TIME 1.3
#define DAMAGE 85
#define DAMAGE2 525
#define SYSTEM_CLASSNAME "compound_arrow"
#define SYSTEM_CLASSNAME2 "compound_arrow2"
#define KNIFE_KNOCK 25
#define WEAPON_ANIMEXT "slingshot"

#define Get_Ent_Data(%1,%2) get_pdata_int(%1,%2,4)
#define Set_Ent_Data(%1,%2,%3) set_pdata_int(%1,%2,%3,4)

const PDATA_SAFE = 2
const OFFSET_LINUX_WEAPONS = 4
const OFFSET_WEAPONOWNER = 41
const m_flNextAttack = 83
const m_szAnimExtention = 492

new const v_model[] = "models/m4m3ts/v_cbow.mdl"
new const p_model[] = "models/m4m3ts/p_bow_new.mdl"
new const w_model[] = "models/m4m3ts/w_bow_new.mdl"
new const ARROW_MODEL[] = "models/m4m3ts/arrow.mdl"
new const bow_hit[] = "weapons/bow_hit.wav"
new const bow_wall[] = "weapons/bow_wall.wav"
new const weapon_sound[7][] = 
{
	"weapons/bow_shoot1.wav",
	"weapons/bow_charge_finish1.wav",
	"weapons/bow_charge_shoot1_empty.wav",
	"weapons/bow_charge_shoot2.wav",
	"weapons/bow_charge_start1.wav",
	"weapons/bow_charge_start2.wav",
	"weapons/bow_draw.wav"
}


new const WeaponResource[5][] = 
{
	"sprites/weapon_cbow.txt",
	"sprites/640hud2_2.spr",
	"sprites/640hud10_2.spr",
	"sprites/640hud98.spr",
	"sprites/640hud12_2.spr"
}

enum
{
	ANIM_IDLE = 0,
	ANIM_IDLE_EMPTY,
	ANIM_SHOOT,
	ANIM_SHOOT_EMPTY,
	ANIM_DRAW,
	ANIM_DRAW_EMPTY,
	ANIM_CHARGE_START,
	ANIM_CHARGE_FINISH,
	ANIM_CHARGE_IDLE1,
	ANIM_CHARGE_IDLE2,
	ANIM_CHARGE_SHOOT1,
	ANIM_CHARGE_SHOOT1_EMPTY,
	ANIM_CHARGE_SHOOT2,
	ANIM_CHARGE_SHOOT2_EMPTY
}

new bool:charge_fail_shoot, g_MsgDeathMsg, g_endround

new g_had_cbow[33], g_cbow_ammo[33], g_Shoot_Count[33], charge_idle1_masuk[33], charge_ready[33], charge_fail[33], secondary_attack[33], g_temp_attack[33], Float:TargetOriginX[3]
new g_old_weapon[33], g_smokepuff_id, m_iBlood[2], Float:g_can_laser[33], Float:cl_pushangle[33][3]
new sTrail

const PRIMARY_WEAPONS_BIT_SUM = 
(1<<CSW_SCOUT)|(1<<CSW_XM1014)|(1<<CSW_MAC10)|(1<<CSW_AUG)|(1<<CSW_UMP45)|(1<<CSW_SG550)|(1<<CSW_GALIL)|(1<<CSW_FAMAS)|(1<<CSW_AWP)|(1<<
CSW_MP5NAVY)|(1<<CSW_M249)|(1<<CSW_M3)|(1<<CSW_M4A1)|(1<<CSW_TMP)|(1<<CSW_G3SG1)|(1<<CSW_SG552)|(1<<CSW_AK47)|(1<<CSW_P90)

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_forward(FM_CmdStart, "fw_CmdStart")
	register_forward(FM_SetModel, "fw_SetModel")
	register_think(SYSTEM_CLASSNAME, "fw_Think")
	register_think(SYSTEM_CLASSNAME2, "fw_Think")
	register_touch(SYSTEM_CLASSNAME, "*", "fw_touch")
	register_touch(SYSTEM_CLASSNAME2, "*", "fw_touch2")
	register_forward(FM_PlayerPreThink, "fw_PlayerPreThink")
	register_forward(FM_UpdateClientData, "fw_UpdateClientData_Post", 1)
	register_forward(FM_TraceLine,"fw_traceline",1)
	RegisterHam(Ham_Weapon_PrimaryAttack, weapon_cbow, "fw_bow_PrimaryAttack")
	RegisterHam(Ham_Weapon_PrimaryAttack, weapon_cbow, "fw_bow_PrimaryAttack_Post", 1)
	RegisterHam(Ham_Spawn, "player", "Player_Spawn", 1)
	RegisterHam(Ham_Weapon_WeaponIdle, weapon_cbow, "fw_cbowidleanim", 1)
	RegisterHam(Ham_Killed, "player", "fw_PlayerKilled")
	RegisterHam(Ham_Item_AddToPlayer, weapon_cbow, "fw_AddToPlayer_Post", 1)
	RegisterHam(Ham_TraceAttack, "player", "fw_TraceAttack")
	RegisterHam(Ham_TraceAttack, "worldspawn", "fw_TraceAttack", 1)
	RegisterHam(Ham_TraceAttack, "func_breakable", "fw_TraceAttack", 1)
	RegisterHam(Ham_TraceAttack, "func_wall", "fw_TraceAttack", 1)
	RegisterHam(Ham_TraceAttack, "func_door", "fw_TraceAttack", 1)
	RegisterHam(Ham_TraceAttack, "func_door_rotating", "fw_TraceAttack", 1)
	RegisterHam(Ham_TraceAttack, "func_plat", "fw_TraceAttack", 1)
	RegisterHam(Ham_TraceAttack, "func_rotating", "fw_TraceAttack", 1)
	register_message(get_user_msgid("DeathMsg"), "message_DeathMsg")
	register_event("CurWeapon", "Event_CurWeapon", "be", "1=1")
	
	register_clcmd("weapon_cbow", "hook_weapon")
	
	g_MsgDeathMsg = get_user_msgid("DeathMsg")
	
	g_endround = 1
}


public plugin_precache()
{
	precache_model(v_model)
	precache_model(p_model)
	precache_model(w_model)
	precache_model(ARROW_MODEL)
	precache_sound(bow_hit)
	precache_sound(bow_wall)
	
	for(new i = 0; i < sizeof(weapon_sound); i++) 
		precache_sound(weapon_sound[i])
		
	precache_generic(WeaponResource[0])
	for(new i = 1; i < sizeof(WeaponResource); i++)
		precache_model(WeaponResource[i])
	
	g_smokepuff_id = engfunc(EngFunc_PrecacheModel, "sprites/wall_puff1.spr")
	m_iBlood[0] = precache_model("sprites/blood.spr")
	m_iBlood[1] = precache_model("sprites/bloodspray.spr")	
	sTrail = precache_model("sprites/laserbeam.spr")
}

public zp_user_infected_post(id)
{
	remove_cbow(id)
}

public plugin_natives()
{
	register_native("get_cbow", "native_get_cbow", 1)
	register_native("refill_cbow", "native_refill_cbow", 1)
}

public native_get_cbow(id)
{
	get_cbow(id)
}

public native_refill_cbow(id)
{
	refill_cbow(id)
}

new g_register
public client_putinserver(id)
{
	if(!g_register && is_user_bot(id))
	{
		g_register = 0
		set_task(0.1, "do_register", id, _, _, "b")
	}
}

public do_register(id)
{
	RegisterHamFromEntity(Ham_TraceAttack, id, "fw_TraceAttack")
	RegisterHamFromEntity(Ham_TraceAttack, id, "fw_TraceAttack", 1)
}

public zp_round_started() g_endround = 0
public zp_round_ended() g_endround = 1

public Player_Spawn(id)
{
	remove_cbow(id)
}

public fw_PlayerKilled(id)
{
	remove_cbow(id)
}

public hook_weapon(id)
{
	engclient_cmd(id, weapon_cbow)
	return
}

public get_cbow(id)
{
	if(!is_user_alive(id))
		return
	charge_ready[id] = 0
	drop_weapons(id, 1)
	g_had_cbow[id] = 1
	g_cbow_ammo[id] = DEFAULT_AMMO
	
	give_item(id, weapon_cbow)
	update_ammo(id)
	
	static weapon_ent; weapon_ent = fm_find_ent_by_owner(-1, weapon_cbow, id)
	if(pev_valid(weapon_ent)) cs_set_weapon_ammo(weapon_ent, 1)
}

public remove_cbow(id)
{
	g_had_cbow[id] = 0
	g_cbow_ammo[id] = 0
	charge_ready[id] = 0
	secondary_attack[id] = false
	charge_fail_shoot = false
	charge_fail[id] = false
	charge_idle1_masuk[id] = false
}

public refill_cbow(id)
{	
	if(g_had_cbow[id]) g_cbow_ammo[id] = 75
	
	if(get_user_weapon(id) == CSW_CBOW && g_had_cbow[id]) update_ammo(id)
}
	
public fw_UpdateClientData_Post(id, sendweapons, cd_handle)
{
	if(!is_user_alive(id) || !is_user_connected(id))
		return FMRES_IGNORED	
	if(get_user_weapon(id) == CSW_CBOW && g_had_cbow[id])
		set_cd(cd_handle, CD_flNextAttack, get_gametime() + 0.001) 
	
	return FMRES_HANDLED
}

public Event_CurWeapon(id)
{
	if(!is_user_alive(id))
		return
		
	if(get_user_weapon(id) == CSW_CBOW && g_had_cbow[id])
	{
		set_pev(id, pev_viewmodel2, v_model)
		set_pev(id, pev_weaponmodel2, p_model)
		set_pdata_string(id, m_szAnimExtention * 4, WEAPON_ANIMEXT, -1 , 20)
		remove_task(id)
		secondary_attack[id] = false
		charge_fail_shoot = false
		charge_fail[id] = false
		charge_idle1_masuk[id] = false
		charge_ready[id] = 0
		if(g_old_weapon[id] != CSW_CBOW && g_cbow_ammo[id] >= 1) set_weapon_anim(id, ANIM_DRAW)
		if(g_old_weapon[id] != CSW_CBOW && g_cbow_ammo[id] == 0) set_weapon_anim(id, ANIM_DRAW_EMPTY)
		update_ammo(id)
	}
	
	g_old_weapon[id] = get_user_weapon(id)
}

public fw_cbowidleanim(Weapon)
{
	new id = get_pdata_cbase(Weapon, 41, 4)

	if(!is_user_alive(id) || zp_get_user_zombie(id) || !g_had_cbow[id] || get_user_weapon(id) != CSW_CBOW)
		return HAM_IGNORED;

	if(g_cbow_ammo[id] >= 1) 
		return HAM_SUPERCEDE;
	
	if(g_cbow_ammo[id] == 0 && get_pdata_float(Weapon, 48, 4) <= 0.25) 
	{
		set_weapon_anim(id, ANIM_IDLE_EMPTY)
		set_pdata_float(Weapon, 48, 20.0, 4)
		return HAM_SUPERCEDE;
	}

	return HAM_IGNORED;
}

public fw_CmdStart(id, uc_handle, seed)
{
	if(!is_user_alive(id) || !is_user_connected(id))
		return
	if(get_user_weapon(id) != CSW_CBOW || !g_had_cbow[id])
		return
	
	static ent; ent = fm_get_user_weapon_entity(id, CSW_CBOW)
	if(!pev_valid(ent))
		return
	if(get_pdata_float(ent, 46, OFFSET_LINUX_WEAPONS) > 0.0 || get_pdata_float(ent, 47, OFFSET_LINUX_WEAPONS) > 0.0) 
		return
	
	static CurButton
	CurButton = get_uc(uc_handle, UC_Buttons)
	
	if(CurButton & IN_ATTACK)
	{
		CurButton &= ~IN_ATTACK
		set_uc(uc_handle, UC_Buttons, CurButton)
		
		if(g_cbow_ammo[id] == 0)
			return
		if(get_pdata_float(id, 83, 5) <= 0.0)
		{
			g_cbow_ammo[id]--
			g_Shoot_Count[id] = 0
			update_ammo(id)
			FireArrow(id)
			if(g_cbow_ammo[id] > 0) set_weapon_anim(id, ANIM_SHOOT)
			else set_weapon_anim(id, ANIM_SHOOT_EMPTY)
			emit_sound(id, CHAN_WEAPON, weapon_sound[0], 1.0, ATTN_NORM, 0, PITCH_NORM)
			set_player_nextattack(id, CSW_CBOW, RELOAD_TIME)
		}
	}
	else if(CurButton & IN_ATTACK2 && get_pdata_float(id, 83, 5) <= 0.0 && g_cbow_ammo[id] > 0)
	{
		charge_fail_shoot = false
		charge_fail[id] = false
		charge_idle1_masuk[id] = false
		
		secondary_attack[id] = true
		set_task(0.6, "fail_shot", id)
		set_weapon_anim(id, ANIM_CHARGE_START)
		set_weapons_timeidle(id, CSW_CBOW, 0.0)
		set_player_nextattackx(id, 256.0)
	}
	else if((pev(id, pev_oldbuttons) & IN_ATTACK2) && !(CurButton & IN_ATTACK2))
	{
		if(g_cbow_ammo[id] == 0)
			return
			
		if(charge_ready[id])
		{
			charge_fail_shoot = false
			charge_fail[id] = false
			charge_idle1_masuk[id] = false
			ExecuteHamB(Ham_Weapon_PrimaryAttack, ent)
			entity_set_int(id, EV_INT_sequence, 85)
			set_pev(id, pev_framerate, 0.55)
			g_cbow_ammo[id]--
			g_Shoot_Count[id] = 0
			update_ammo(id)
			charge_ready[id] = 0
			remove_task(id)
			set_weapons_timeidle(id, CSW_CBOW, STAB_TIME)
			set_player_nextattackx(id, STAB_TIME)
		}
		else
		{
			charge_fail[id] = true
			charge_fail_shoot = true
		}
	}
}

public fw_PlayerPreThink(id)
{
	if(get_user_weapon(id) != CSW_CBOW || !g_had_cbow[id])
		return

	if(charge_fail_shoot && charge_idle1_masuk[id])
	{
		shuting_fail(id)
	}
}

public fail_shot(id)
{
	if(get_user_weapon(id) != CSW_CBOW || !g_had_cbow[id])
		return
		
	if(charge_fail[id])
	{
		shuting_fail(id)
	}
	else
	{
		charge_idle1(id)
		set_task(0.6, "charge_finish1", id)
		set_task(0.9, "charge_idle2", id)
	}
}

public shuting_fail(id)
{
	if(get_user_weapon(id) != CSW_CBOW || !g_had_cbow[id])
		return
		
	if(g_cbow_ammo[id] == 0)
		return
		
	charge_fail_shoot = false
	charge_fail[id] = false
	charge_idle1_masuk[id] = false
	remove_task(id)
	charge_ready[id] = 0
	FireArrow(id)
	g_cbow_ammo[id]--
	g_Shoot_Count[id] = 0
	update_ammo(id)
	secondary_attack[id] = false
	set_weapons_timeidle(id, CSW_CBOW, STAB_TIME)
	set_player_nextattackx(id, STAB_TIME)
	if(g_cbow_ammo[id] > 0)
	{	set_weapon_anim(id, ANIM_CHARGE_SHOOT2)
		emit_sound(id, CHAN_WEAPON, weapon_sound[3], 1.0, ATTN_NORM, 0, PITCH_NORM)
	}
	else
	{
		set_weapon_anim(id, ANIM_CHARGE_SHOOT2_EMPTY)
		emit_sound(id, CHAN_WEAPON, weapon_sound[2], 1.0, ATTN_NORM, 0, PITCH_NORM)
	}
}

public charge_idle1(id)
{
	if(get_user_weapon(id) != CSW_CBOW || !g_had_cbow[id])
		return
		
	set_weapon_anim(id, ANIM_CHARGE_IDLE1)
	charge_idle1_masuk[id] = true
}

public charge_finish1(id)
{
	if(get_user_weapon(id) != CSW_CBOW || !g_had_cbow[id])
		return
		
	set_weapon_anim(id, ANIM_CHARGE_FINISH)
}
public charge_idle2(id)
{
	if(get_user_weapon(id) != CSW_CBOW || !g_had_cbow[id])
		return
		
	set_weapon_anim(id, ANIM_CHARGE_IDLE2)
	charge_ready[id] = 1
}

public FireArrow(id)
{
	static Float:StartOrigin[3], Float:TargetOrigin[3], Float:angles[3], Float:anglestrue[3]
	get_position(id, 2.0, 0.0, 0.0, StartOrigin)
	
	pev(id,pev_v_angle,angles)
	static Ent; Ent = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "info_target"))
	if(!pev_valid(Ent)) return
	anglestrue[0] = 360.0 - angles[0]
	anglestrue[1] = angles[1]
	anglestrue[2] = angles[2]
	
	// Set info for ent
	set_pev(Ent, pev_movetype, MOVETYPE_FLY)
	set_pev(Ent, pev_owner, id) // Better than pev_owner
	set_pev(Ent, pev_fuser1, get_gametime() + 4.0)
	set_pev(Ent, pev_nextthink, halflife_time() + 0.1)
	
	entity_set_string(Ent, EV_SZ_classname, SYSTEM_CLASSNAME)
	engfunc(EngFunc_SetModel, Ent, ARROW_MODEL)
	set_pev(Ent, pev_mins, Float:{-1.0, -1.0, -1.0})
	set_pev(Ent, pev_maxs, Float:{1.0, 1.0, 1.0})
	set_pev(Ent, pev_origin, StartOrigin)
	set_pev(Ent, pev_angles, anglestrue)
	set_pev(Ent, pev_gravity, 0.01)
	set_pev(Ent, pev_solid, SOLID_BBOX)
	set_pev(Ent, pev_frame, 0.0)
	
	static Float:Velocity[3]
	fm_get_aim_origin(id, TargetOrigin)
	get_speed_vector(StartOrigin, TargetOrigin, 3500.0, Velocity)
	set_pev(Ent, pev_velocity, Velocity)
		
	message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
	write_byte(TE_BEAMFOLLOW) // Temporary entity ID
	write_short(Ent) // Entity
	write_short(sTrail) // Sprite index
	write_byte(7) // Life
	write_byte(1) // Line width
	write_byte(255) // Red
	write_byte(255) // Green
	write_byte(255) // Blue
	write_byte(100) // Alpha
	message_end()
	
	create_fake_attack(id)
}

public fw_bow_PrimaryAttack(Weapon)
{
	new Player = get_pdata_cbase(Weapon, 41, 4)
	
	if (!g_had_cbow[Player] || !charge_ready[Player])
		return
	
	pev(Player,pev_punchangle,cl_pushangle[Player])
}

public fw_bow_PrimaryAttack_Post(Weapon)
{
	new Player = get_pdata_cbase(Weapon, 41, 4)
		
	if(!is_user_alive(Player) || !charge_ready[Player])
		return

	if(g_had_cbow[Player])
	{
		new Float:push[3]
		pev(Player,pev_punchangle,push)
		xs_vec_sub(push,cl_pushangle[Player],push)
		xs_vec_mul_scalar(push,0.65,push)
		xs_vec_add(push,cl_pushangle[Player],push)
		set_pev(Player,pev_punchangle,push)
		
		if(g_cbow_ammo[Player] > 0)
		{	set_weapon_anim(Player, ANIM_CHARGE_SHOOT1)
			emit_sound(Player, CHAN_WEAPON, weapon_sound[3], 1.0, ATTN_NORM, 0, PITCH_NORM)
		}
		else
		{
			set_weapon_anim(Player, ANIM_CHARGE_SHOOT1_EMPTY)
			emit_sound(Player, CHAN_WEAPON, weapon_sound[2], 1.0, ATTN_NORM, 0, PITCH_NORM)
		}
		
		create_laser(Player)
	}
}

public create_laser(id)
{
	static Float:StartOriginX[3]
	get_position(id, 40.0, 2.5, -4.0, StartOriginX)
	
	message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
	write_byte(TE_BEAMPOINTS)
	engfunc(EngFunc_WriteCoord, StartOriginX[0])
	engfunc(EngFunc_WriteCoord, StartOriginX[1])
	engfunc(EngFunc_WriteCoord, StartOriginX[2])
	engfunc(EngFunc_WriteCoord, TargetOriginX[0])
	engfunc(EngFunc_WriteCoord, TargetOriginX[1])
	engfunc(EngFunc_WriteCoord, TargetOriginX[2])
	write_short(sTrail)
	write_byte(0)
	write_byte(0)
	write_byte(18)
	write_byte(20)
	write_byte(0)
	write_byte(255)
	write_byte(80)
	write_byte(80)
	write_byte(170)
	write_byte(0)
	message_end()
}

public fw_Think(ent)
{
	if(!pev_valid(ent)) 
		return
	
	static Float:fFrame; pev(ent, pev_frame, fFrame)
	
	fFrame += 1.5
	fFrame = floatmin(21.0, fFrame)
	
	set_pev(ent, pev_frame, fFrame)
	set_pev(ent, pev_nextthink, get_gametime() + 0.05)
	
	// time remove
	static Float:fTimeRemove, Float:Amount
	pev(ent, pev_fuser1, fTimeRemove)
	pev(ent, pev_renderamt, Amount)
	
	if(get_gametime() >= fTimeRemove) 
	{
		remove_entity(ent)
	}
}

public fw_touch(Ent, Id)
{
	// If ent is valid
	if(!pev_valid(Ent))
		return
	static Owner; Owner = pev(Ent, pev_owner)
	if(pev(Ent, pev_movetype) == MOVETYPE_NONE)
		return
	static classnameptd[32]
	pev(Id, pev_classname, classnameptd, 31)
	if (equali(classnameptd, "func_breakable")) ExecuteHamB( Ham_TakeDamage, Id, 0, 0, 60.0, DMG_GENERIC )
	Damage_cbow(Ent, Id)
	
	// Get it's origin
	new Float:originF[3]
	pev(Ent, pev_origin, originF)
	// Alive...
	
	if(is_user_alive(Id) && zp_get_user_zombie(Id))
	{
		remove_entity(Ent)
		create_blood(originF)
		create_blood(originF)
	}
	
	else if(is_user_alive(Id) && !zp_get_user_zombie(Id))
	{
		remove_entity(Ent)
		emit_sound(Id, CHAN_VOICE, bow_hit, 1.0, ATTN_NORM, 0, PITCH_NORM)
	}
	
	else
	{
		make_bullet(Owner, originF)
		fake_smokes(Owner, originF)
		engfunc(EngFunc_EmitSound, Ent, CHAN_WEAPON, bow_wall, 1.0, ATTN_STATIC, 0, PITCH_NORM)
		set_pev(Ent, pev_movetype, MOVETYPE_NONE)
		set_pev(Ent, pev_solid, SOLID_NOT)
	}
}

public Damage_cbow(Ent, Id)
{
	static Owner; Owner = pev(Ent, pev_owner)
	static Attacker; 
	if(!is_user_alive(Owner)) 
	{
		Attacker = 0
		return
	} else Attacker = Owner
	
	if(g_endround)
		return

	new bool:bIsHeadShot; // never make that one static
	new Float:flAdjustedDamage, bool:death

	switch( Get_MissileWeaponHitGroup(Ent) ) 
	{
		case HIT_GENERIC: flAdjustedDamage = DAMAGE * 1.0 
		case HIT_CHEST: flAdjustedDamage = DAMAGE * 1.2
		case HIT_STOMACH: flAdjustedDamage = DAMAGE * 1.1 
		case HIT_LEFTLEG, HIT_RIGHTLEG: flAdjustedDamage = DAMAGE * 1.0
		case HIT_LEFTARM, HIT_RIGHTARM: flAdjustedDamage = DAMAGE * 1.0
		case HIT_HEAD:
		{
			flAdjustedDamage = DAMAGE * 3.0
			bIsHeadShot = true;
		}
	}
	if(pev(Id, pev_health) <= flAdjustedDamage) death = true 
	
	if(is_user_alive(Id))
	{
		if( bIsHeadShot && death)
		{
			zp_cs_set_user_money(Attacker, zp_cs_get_user_money(Attacker) + 500)

			emessage_begin(MSG_BROADCAST, g_MsgDeathMsg)
			ewrite_byte(Attacker)
			ewrite_byte(Id)
			ewrite_byte(1)
			ewrite_string("Compound Bow")
			emessage_end()
			
			set_msg_block(g_MsgDeathMsg, BLOCK_SET)
			user_silentkill(Id)
			set_msg_block(g_MsgDeathMsg, BLOCK_NOT)
			
			new kfrags = get_user_frags( Owner );
			set_user_frags( Owner, kfrags+1 );
			new vfrags = get_user_frags( Id );
			set_user_frags( Id, vfrags+1 );
			
			death = false			
		}
		else ExecuteHamB(Ham_TakeDamage, Id, Ent, Attacker, flAdjustedDamage, DMG_BULLET)
	}
}

public fw_SetModel(entity, model[])
{
	if(!pev_valid(entity))
		return FMRES_IGNORED
	
	static Classname[64]
	pev(entity, pev_classname, Classname, sizeof(Classname))
	
	if(!equal(Classname, "weaponbox"))
		return FMRES_IGNORED
	
	static id
	id = pev(entity, pev_owner)
	
	if(equal(model, old_w_model))
	{
		static weapon
		weapon = fm_get_user_weapon_entity(entity, CSW_CBOW)
		
		if(!pev_valid(weapon))
			return FMRES_IGNORED
		
		if(g_had_cbow[id])
		{
			set_pev(weapon, pev_impulse, WEAPON_SECRETCODE)
			set_pev(weapon, pev_iuser4, g_cbow_ammo[id])
			engfunc(EngFunc_SetModel, entity, w_model)
			
			g_had_cbow[id] = 0
			g_cbow_ammo[id] = 0
			
			return FMRES_SUPERCEDE
		}
	}

	return FMRES_IGNORED;
}

public fw_AddToPlayer_Post(ent, id)
{
	if(pev(ent, pev_impulse) == WEAPON_SECRETCODE)
	{
		g_had_cbow[id] = 1
		g_cbow_ammo[id] = pev(ent, pev_iuser4)
		
		set_pev(ent, pev_impulse, 0)
	}			
	
	message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("WeaponList"), _, id)
	write_string((g_had_cbow[id] == 1 ? "weapon_cbow" : "weapon_galil"))
	write_byte(1)
	write_byte(90)
	write_byte(-1)
	write_byte(-1)
	write_byte(0)
	write_byte(17)
	write_byte(CSW_CBOW)
	write_byte(0)
	message_end()
}

public update_ammo(id)
{
	if(!is_user_alive(id))
		return
	
	static weapon_ent; weapon_ent = fm_find_ent_by_owner(-1, weapon_cbow, id)
	if(pev_valid(weapon_ent)) cs_set_weapon_ammo(weapon_ent, 1)	
	
	cs_set_user_bpammo(id, CSW_CBOW, 0)
	
	engfunc(EngFunc_MessageBegin, MSG_ONE_UNRELIABLE, get_user_msgid("CurWeapon"), {0, 0, 0}, id)
	write_byte(1)
	write_byte(CSW_CBOW)
	write_byte(-1)
	message_end()
	
	message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("AmmoX"), _, id)
	write_byte(1)
	write_byte(g_cbow_ammo[id])
	message_end()
}

public fw_TraceAttack(ent, attacker, Float:Damage, Float:fDir[3], ptr, iDamageType)
{
	if(!is_user_alive(attacker))
		return HAM_IGNORED	
	if(get_user_weapon(attacker) != CSW_CBOW || !g_had_cbow[attacker])
		return HAM_IGNORED
	if(g_temp_attack[attacker])
		return HAM_IGNORED
	
	static Float:flEnd[3], Float:myOrigin[3]
	
	pev(attacker, pev_origin, myOrigin)
	SetHamParamFloat(3, float(DAMAGE2))
	get_tr2(ptr, TR_vecEndPos, flEnd)
	TargetOriginX = flEnd

	if(get_gametime() - 0.1 > g_can_laser[attacker])
	{
		if(!is_user_alive(ent))
		{
			make_bullet(attacker, flEnd)
			fake_smokes(attacker, flEnd)
		}
		
		g_can_laser[attacker] = get_gametime()
	}
			
	if(!is_user_alive(ent))
		return HAM_IGNORED
		
	if(zp_get_user_zombie(ent)) hook_ent2(ent, myOrigin)

	return HAM_HANDLED
}

public fw_traceline(Float:v1[3],Float:v2[3],noMonsters,id,ptr)
{
	if(!is_user_alive(id))
		return HAM_IGNORED	
	if(get_user_weapon(id) != CSW_CBOW || !g_had_cbow[id])
		return HAM_IGNORED
	if(g_temp_attack[id])
		return HAM_IGNORED

	// get crosshair aim
	static Float:aim[3];
	get_aim(id,v1,aim);
	
	// do another trace to this spot
	new trace = create_tr2();
	engfunc(EngFunc_TraceLine,v1,aim,noMonsters,id,trace);
	
	// copy ints
	set_tr2(ptr,TR_AllSolid,get_tr2(trace,TR_AllSolid));
	set_tr2(ptr,TR_StartSolid,get_tr2(trace,TR_StartSolid));
	set_tr2(ptr,TR_InOpen,get_tr2(trace,TR_InOpen));
	set_tr2(ptr,TR_InWater,get_tr2(trace,TR_InWater));
	set_tr2(ptr,TR_pHit,get_tr2(trace,TR_pHit));
	set_tr2(ptr,TR_iHitgroup,get_tr2(trace,TR_iHitgroup));

	// copy floats
	get_tr2(trace,TR_flFraction,aim[0]);
	set_tr2(ptr,TR_flFraction,aim[0]);
	get_tr2(trace,TR_flPlaneDist,aim[0]);
	set_tr2(ptr,TR_flPlaneDist,aim[0]);
	
	// copy vecs
	get_tr2(trace,TR_vecEndPos,aim);
	set_tr2(ptr,TR_vecEndPos,aim);
	get_tr2(trace,TR_vecPlaneNormal,aim);
	set_tr2(ptr,TR_vecPlaneNormal,aim);

	// get rid of new trace
	free_tr2(trace);

	return FMRES_IGNORED;
}

public message_DeathMsg(msg_id, msg_dest, id)
{
	static szTruncatedWeapon[33], iAttacker, iVictim
	
	get_msg_arg_string(4, szTruncatedWeapon, charsmax(szTruncatedWeapon))
	
	iAttacker = get_msg_arg_int(1)
	iVictim = get_msg_arg_int(2)
	
	if(!is_user_connected(iAttacker) || iAttacker == iVictim)
		return PLUGIN_CONTINUE
	
	if(equal(szTruncatedWeapon, "galil") && get_user_weapon(iAttacker) == CSW_CBOW)
	{
		if(g_had_cbow[iAttacker])
			set_msg_arg_string(4, "Compound Bow")
	}
	return PLUGIN_CONTINUE
}

public create_fake_attack(id)
{
	if(!is_user_alive(id))
		return
	
	static ent
	ent = fm_get_user_weapon_entity(id, CSW_CBOW)
	
	if(pev_valid(ent)) 
	{
		g_temp_attack[id] = 1
		ExecuteHamB(Ham_Weapon_PrimaryAttack, ent)	
		g_temp_attack[id] = 0
	}
}

get_aim(id,Float:source[3],Float:ret[3])
{
	static Float:vAngle[3], Float:pAngle[3], Float:dir[3], Float:temp[3];

	// get aiming direction from forward global based on view angle and punch angle
	pev(id,pev_v_angle,vAngle);
	pev(id,pev_punchangle,pAngle);
	xs_vec_add(vAngle,pAngle,temp);
	engfunc(EngFunc_MakeVectors,temp);
	global_get(glb_v_forward,dir);
	
	/* vecEnd = vecSrc + vecDir * flDistance; */
	xs_vec_mul_scalar(dir,8192.0,temp);
	xs_vec_add(source,temp,ret);
}

stock make_bullet(id, Float:Origin[3])
{
	// Find target
	new decal = random_num(41, 45)
	const loop_time = 2
	
	static Body, Target
	get_user_aiming(id, Target, Body, 999999)
	
	if(is_user_connected(Target))
		return
	
	for(new i = 0; i < loop_time; i++)
	{
		// Put decal on "world" (a wall)
		message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
		write_byte(TE_WORLDDECAL)
		engfunc(EngFunc_WriteCoord, Origin[0])
		engfunc(EngFunc_WriteCoord, Origin[1])
		engfunc(EngFunc_WriteCoord, Origin[2])
		write_byte(decal)
		message_end()
	}
}

public fake_smoke(id, trace_result)
{
	static Float:vecSrc[3], Float:vecEnd[3], TE_FLAG
	
	get_weapon_attachment(id, vecSrc)
	global_get(glb_v_forward, vecEnd)
	
	xs_vec_mul_scalar(vecEnd, 8192.0, vecEnd)
	xs_vec_add(vecSrc, vecEnd, vecEnd)

	get_tr2(trace_result, TR_vecEndPos, vecSrc)
	get_tr2(trace_result, TR_vecPlaneNormal, vecEnd)
	
	xs_vec_mul_scalar(vecEnd, 2.5, vecEnd)
	xs_vec_add(vecSrc, vecEnd, vecEnd)
	
	TE_FLAG |= TE_EXPLFLAG_NODLIGHTS
	TE_FLAG |= TE_EXPLFLAG_NOSOUND
	TE_FLAG |= TE_EXPLFLAG_NOPARTICLES
	
	engfunc(EngFunc_MessageBegin, MSG_PAS, SVC_TEMPENTITY, vecEnd, 0)
	write_byte(TE_EXPLOSION)
	engfunc(EngFunc_WriteCoord, vecEnd[0])
	engfunc(EngFunc_WriteCoord, vecEnd[1])
	engfunc(EngFunc_WriteCoord, vecEnd[2] - 10.0)
	write_short(g_smokepuff_id)
	write_byte(2)
	write_byte(80)
	write_byte(TE_FLAG)
	message_end()	
}

public fake_smokes(id, Float:Origin[3])
{
	static TE_FLAG
	
	TE_FLAG |= TE_EXPLFLAG_NODLIGHTS
	TE_FLAG |= TE_EXPLFLAG_NOSOUND
	TE_FLAG |= TE_EXPLFLAG_NOPARTICLES
	
	engfunc(EngFunc_MessageBegin, MSG_PAS, SVC_TEMPENTITY, Origin, 0)
	write_byte(TE_EXPLOSION)
	engfunc(EngFunc_WriteCoord, Origin[0])
	engfunc(EngFunc_WriteCoord, Origin[1])
	engfunc(EngFunc_WriteCoord, Origin[2] - 10.0)
	write_short(g_smokepuff_id)
	write_byte(2)
	write_byte(80)
	write_byte(TE_FLAG)
	message_end()
}

stock set_weapon_anim(id, anim)
{
	if(!is_user_alive(id))
		return
	
	set_pev(id, pev_weaponanim, anim)
	
	message_begin(MSG_ONE_UNRELIABLE, SVC_WEAPONANIM, {0, 0, 0}, id)
	write_byte(anim)
	write_byte(pev(id, pev_body))
	message_end()
}


stock set_player_light(id, const LightStyle[])
{
	if(!is_user_alive(id))
		return
		
	message_begin(MSG_ONE_UNRELIABLE, SVC_LIGHTSTYLE, .player = id)
	write_byte(0)
	write_string(LightStyle)
	message_end()
}

stock get_weapon_attachment(id, Float:output[3], Float:fDis = 40.0)
{ 
	new Float:vfEnd[3], viEnd[3] 
	get_user_origin(id, viEnd, 3)  
	IVecFVec(viEnd, vfEnd) 
	
	new Float:fOrigin[3], Float:fAngle[3]
	
	pev(id, pev_origin, fOrigin) 
	pev(id, pev_view_ofs, fAngle)
	
	xs_vec_add(fOrigin, fAngle, fOrigin) 
	
	new Float:fAttack[3]
	
	xs_vec_sub(vfEnd, fOrigin, fAttack)
	xs_vec_sub(vfEnd, fOrigin, fAttack) 
	
	new Float:fRate
	
	fRate = fDis / vector_length(fAttack)
	xs_vec_mul_scalar(fAttack, fRate, fAttack)
	
	xs_vec_add(fOrigin, fAttack, output)
}

stock create_blood(const Float:origin[3])
{
	// Show some blood :)
	message_begin(MSG_BROADCAST, SVC_TEMPENTITY) 
	write_byte(TE_BLOODSPRITE)
	engfunc(EngFunc_WriteCoord, origin[0])
	engfunc(EngFunc_WriteCoord, origin[1])
	engfunc(EngFunc_WriteCoord, origin[2])
	write_short(m_iBlood[1])
	write_short(m_iBlood[0])
	write_byte(75)
	write_byte(8)
	message_end()
}

stock set_player_screenfade(pPlayer, sDuration = 0, sHoldTime = 0, sFlags = 0, r = 0, g = 0, b = 0, a = 0 )
{
	if(!is_user_connected(pPlayer))
		return
	
	message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("ScreenFade"), _, pPlayer)
	write_short(sDuration)
	write_short(sHoldTime)
	write_short(sFlags)
	write_byte(r)
	write_byte(g)
	write_byte(b)
	write_byte(a)
	message_end()
}

stock hook_ent2(ent, Float:VicOrigin[3])
{
	static Float:flVelocity[3]
	static Float:EntOrigin[3]
	static Float:flNewSpeed
	static Float:flSpeed
	
	pev(ent, pev_origin, EntOrigin)
	
	flSpeed = 1500.0
				   
	flNewSpeed = flSpeed * ( 1.0 - ( 100.0 / 300.0 ) )
				   
	get_speed_vector ( VicOrigin, EntOrigin, flNewSpeed, flVelocity )
				   
	set_pev ( ent, pev_velocity,flVelocity )
}


stock drop_weapons(id, dropwhat)
{
	static weapons[32], num, i, weaponid
	num = 0
	get_user_weapons(id, weapons, num)
	 
	for (i = 0; i < num; i++)
	{
		weaponid = weapons[i]
		  
		if (dropwhat == 1 && ((1<<weaponid) & PRIMARY_WEAPONS_BIT_SUM))
		{
			static wname[32]
			get_weaponname(weaponid, wname, sizeof wname - 1)
			engclient_cmd(id, "drop", wname)
		}
	}
}

stock get_speed_vector(const Float:origin1[3],const Float:origin2[3],Float:speed, Float:new_velocity[3])
{
	new_velocity[0] = origin2[0] - origin1[0]
	new_velocity[1] = origin2[1] - origin1[1]
	new_velocity[2] = origin2[2] - origin1[2]
	static Float:num; num = floatsqroot(speed*speed / (new_velocity[0]*new_velocity[0] + new_velocity[1]*new_velocity[1] + new_velocity[2]*new_velocity[2]))
	new_velocity[0] *= num
	new_velocity[1] *= num
	new_velocity[2] *= num
	
	return 1;
}

stock Get_MissileWeaponHitGroup( iEnt )
{
	new Float:flStart[ 3 ], Float:flEnd[ 3 ];
	
	pev( iEnt, pev_origin, flStart );
	pev( iEnt, pev_velocity, flEnd );
	xs_vec_add( flStart, flEnd, flEnd );
	
	new ptr = create_tr2();
	engfunc( EngFunc_TraceLine, flStart, flEnd, 0, iEnt, ptr );
	
	new iHitGroup, Owner, nOhead, head
	Owner = pev(iEnt, pev_owner)
	nOhead = get_tr2( ptr, TR_iHitgroup )
	head = set_tr2( ptr, TR_iHitgroup, HIT_HEAD )
	
	iHitGroup = using_ds(Owner) ? head : nOhead
	free_tr2( ptr );
	
	return iHitGroup;
}

stock get_position(id,Float:forw, Float:right, Float:up, Float:vStart[])
{
	static Float:vOrigin[3], Float:vAngle[3], Float:vForward[3], Float:vRight[3], Float:vUp[3]
	
	pev(id, pev_origin, vOrigin)
	pev(id, pev_view_ofs, vUp) //for player
	xs_vec_add(vOrigin, vUp, vOrigin)
	pev(id, pev_v_angle, vAngle) // if normal entity ,use pev_angles
	
	angle_vector(vAngle, ANGLEVECTOR_FORWARD, vForward) //or use EngFunc_AngleVectors
	angle_vector(vAngle, ANGLEVECTOR_RIGHT, vRight)
	angle_vector(vAngle, ANGLEVECTOR_UP, vUp)
	
	vStart[0] = vOrigin[0] + vForward[0] * forw + vRight[0] * right + vUp[0] * up
	vStart[1] = vOrigin[1] + vForward[1] * forw + vRight[1] * right + vUp[1] * up
	vStart[2] = vOrigin[2] + vForward[2] * forw + vRight[2] * right + vUp[2] * up
}

stock set_weapons_timeidle(id, WeaponId ,Float:TimeIdle)
{
	if(!is_user_alive(id))
		return
		
	static entwpn; entwpn = fm_get_user_weapon_entity(id, WeaponId)
	if(!pev_valid(entwpn)) 
		return
		
	set_pdata_float(entwpn, 46, TimeIdle, OFFSET_LINUX_WEAPONS)
	set_pdata_float(entwpn, 47, TimeIdle, OFFSET_LINUX_WEAPONS)
	set_pdata_float(entwpn, 48, TimeIdle + 0.5, OFFSET_LINUX_WEAPONS)
}

stock set_player_nextattack(player, weapon_id, Float:NextTime)
{
	if(!is_user_alive(player))
		return
	
	const m_flNextPrimaryAttack = 46
	const m_flNextSecondaryAttack = 47
	const m_flTimeWeaponIdle = 48
	const m_flNextAttack = 83
	
	static weapon
	weapon = fm_get_user_weapon_entity(player, weapon_id)
	
	set_pdata_float(player, m_flNextAttack, NextTime, 5)
	if(pev_valid(weapon))
	{
		set_pdata_float(weapon, m_flNextPrimaryAttack , NextTime, 4)
		set_pdata_float(weapon, m_flNextSecondaryAttack, NextTime, 4)
		set_pdata_float(weapon, m_flTimeWeaponIdle, NextTime, 4)
	}
}

stock set_player_nextattackx(id, Float:nexttime)
{
	if(!is_user_alive(id))
		return
		
	set_pdata_float(id, m_flNextAttack, nexttime, 5)
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1033\\ f0\\ fs16 \n\\ par }
*/
