#include <amxmodx>
#include <hamsandwich>
#include <fakemeta>
#include <xs>
#tryinclude <biohazard>

#if !defined _biohazard_included
        #assert Biohazard functions file required!
#endif

enum WEAPONS
{
	P228 	  = 1,
	SCOUT	  = 3,
	XM1014	  = 5,
	MAC10	  = 7,
	AUG	  = 8,
	ELITE	  = 10,
	FIVESEVEN = 11,
	UMP45	  = 12,
	SG550	  = 13,
	GALIL	  = 14,
	FAMAS	  = 15,
	USP	  = 16,
	GLOCK18	  = 17,
	AWP	  = 18,
	MP5NAVY	  = 19,
	M249	  = 20,
	M3	  = 21,
	M4A1	  = 22,
	TMP	  = 23,
	G3SG1	  = 24,
	DEAGLE	  = 26,
	SG552	  = 27,
	AK47	  = 28,
	KNIFE	  = 29,
	P90	  = 30
}

new const NAME[][] = 
{
	"",// null
	"bh_wkb_p228",
	"",// null
	"bh_wkb_scout",
	"", // hegrenade
	"bh_wkb_xm1014",
	"",// c4
	"bh_wkb_mac10",
	"bh_wkb_aug",
	"",// smokegrenade
	"bh_wkb_elite",
	"bh_wkb_fiveseven",
	"bh_wkb_ump45",
	"bh_wkb_sg550",
	"bh_wkb_galil",
	"bh_wkb_famas",
	"bh_wkb_usp",
	"bh_wkb_glock18",
	"bh_wkb_awp",
	"bh_wkb_mp5navy",
	"bh_wkb_m249",
	"bh_wkb_m3",
	"bh_wkb_m4a1",
	"bh_wkb_tmp",
	"bh_wkb_g3sg1",
	"",// flashbang
	"bh_wkb_deagle",
	"bh_wkb_sg552",
	"bh_wkb_ak47",
	"bh_wkb_knife",
	"bh_wkb_p90"
}

new const POWER[][] =
{
	"",  	// NULL
	"5.0",  // CSW_P228
	"",  	// NULL
	"23.0", // CSW_SCOUT
	"",  	// CSW_HEGRENADE
	"7.5", // CSW_XM1014
	"",     // CSW_C4
	"4.0",  // CSW_MAC10
	"7.0",  // CSW_AUG
	"",     // CSW_SMOKEGRENADE
	"4.0",  // CSW_ELITE
	"5.5",  // CSW_FIVESEVEN
	"4.5",  // CSW_UMP45
	"7.5",  // CSW_SG550
	"6.5",  // CSW_GALIL
	"3.5",  // CSW_FAMAS
	"4.5",  // CSW_USP
	"2.0",  // CSW_GLOCK18
	"25.0", // CSW_AWP
	"4.0",  // CSW_MP5NAVY
	"8.0",  // CSW_M249
	"15.0", // CSW_M3
	"7.5",  // CSW_M4A1
	"4.0",  // CSW_TMP
	"8.0",  // CSW_G3SG1
	"",  	// CSW_FLASHBANG
	"6.0",  // CSW_DEAGLE
	"7.0",  // CSW_SG552
	"8.0",  // CSW_AK47
	"1.0",  // CSW_KNIFE
	"5.5"   // CSW_P90
}

new cvar_knockback, cvar_power[WEAPONS], cvar_knockback_hp[2]
public plugin_init()
{
	register_plugin("knockback", "0.3", "cheap_suit")
	is_biomod_active() ? plugin_init2() : pause("ad")
}

public plugin_init2()
{
	cvar_knockback = register_cvar("bh_knockback", "1")
	cvar_knockback_hp[0] = get_cvar_pointer("bh_zombie_kb_hp")

	for(new i = 0; i < sizeof NAME; i++) if(strlen(NAME[i]) > 0)
		cvar_power[WEAPONS:i] = register_cvar(NAME[i], POWER[i])
		
	RegisterHam(Ham_TraceAttack, "player", "bacon_traceattack")
}

public plugin_cfg()
{
	cvar_knockback_hp[1] = get_pcvar_num(cvar_knockback_hp[0])
	set_pcvar_num(cvar_knockback_hp[0], 0)
}

public plugin_end()
	set_pcvar_num(cvar_knockback_hp[0], cvar_knockback_hp[1])

public bacon_traceattack(victim, attacker, Float:damage, Float:direction[3], tracehandle, damagetype)
{
	if(!is_user_zombie(victim)  || !get_pcvar_num(cvar_knockback)
	|| !(damagetype & DMG_BULLET) || !is_user_connected(attacker))
		return HAM_IGNORED
	
	static Float:velocity[2][3] 
	pev(victim, pev_velocity, velocity[0])
	xs_vec_copy(velocity[0], velocity[1])
	
	xs_vec_mul_scalar(direction, damage, direction)
	xs_vec_mul_scalar(direction, get_pcvar_float(cvar_power[WEAPONS:get_user_weapon(attacker)]), direction)
	xs_vec_add(direction, velocity[0], velocity[0])
	velocity[0][2] = velocity[1][2]
	set_pev(victim, pev_velocity, velocity[0])
	
	return HAM_IGNORED
}