#include <amxmodx> 
#include <cstrike> 
#include <hamsandwich>      
#include <fakemeta_util>      
#include <fun> 
#include <hamsandwich>
#include <next21_DR_core>
#include <next21_DR_secondary>

#define PLUGIN "n21_Deathrun_Rambo"
#define VERSION "1.0"
#define AUTHOR "Psycrow"

#define TASK_SA 1337
#define get_gun_owner(%1)	get_pdata_cbase(%1, 41, 4)

#define GunVModel		"models/n21_deathrun/m249ep/v_m249ep.mdl"
#define GunPModel		"models/n21_deathrun/m249ep/p_m249ep.mdl"
#define GunWModel		"models/n21_deathrun/m249ep/w_m249ep.mdl"

new ModeId, in_fuckinrambo


public plugin_precache()
{
	precache_model(GunVModel)
	precache_model(GunPModel)
	precache_model(GunWModel)
}

public plugin_init() 
{ 
	register_plugin(PLUGIN, VERSION, AUTHOR) 
	
	ModeId = n21_dr_mode_registration("Рэмбо", "", 5, 0, 255, 0, 4)
	
	register_logevent("NewRound", 2, "1=Round_Start")
	
	RegisterHam(Ham_Item_Deploy, "weapon_m249", "CurWeapon", 1)
	RegisterHam(Ham_Use,"func_button","UseButton",0)
	RegisterHam(Ham_Spawn, "player", "hook_spawn_post", 1)
	RegisterHam(Ham_TakeDamage, "player", "ham_dmg")
	RegisterHam(Ham_Player_PreThink, "player", "Ham_PreThink_player")
}
	
public n21_dr_set_mode(id, mode)
	if(mode == ModeId)
	{
		SetRambo(id)
		for (new i=1; i<33; i++)
			if(is_user_alive(i) && cs_get_user_team(i) == CS_TEAM_CT)
			{
				ham_strip_weapon(i,"weapon_usp")
				fm_set_user_rendering(i,  kRenderFxGlowShell, 0, 0, 0, kRenderTransAlpha,0)
			}
		
	}
	
public n21_dr_new_terr(id, mode)
{
	if(mode == ModeId)
	{
		in_fuckinrambo = id
	
		fm_set_user_rendering(id)
		SetRambo(id)
	}
}

public NewRound()
	in_fuckinrambo = 0
		
public hook_spawn_post(id)
{
	if(!is_user_alive(id))
		return HAM_IGNORED
	
	if(ModeId != n21_dr_get_mode_id())
		return HAM_IGNORED
		
	if(cs_get_user_team(id) == CS_TEAM_CT)
		set_task(5.0,"SetInvis", id)
		
	return HAM_IGNORED
}

public Ham_PreThink_player(id)
{
	if(in_fuckinrambo != id)
		return HAM_IGNORED
		
	if(get_user_weapon(id) != CSW_M249)
		return HAM_IGNORED
	
	if((pev(id, pev_button) & IN_ATTACK2) && get_pdata_float(id, 83, 5) <= 0.0)
	{
		set_pdata_float(id, 83, 0.65, 5)
		
		new Float:aOrigin[3], Float: vOrigin[3], target, body
		pev(id, pev_origin, aOrigin)
		get_user_aiming(id, target, body)
		fm_get_aim_origin(id, vOrigin)
		
		if(get_distance_f(aOrigin, vOrigin) <= 40.0)
		{
			UTIL_PlayWeaponAnimation(id, 5)
			if(is_user_alive(target))
			{
				if(get_user_team(id) != get_user_team(target))
				{	
					new Float: fVelocity[3]
					get_speed_vector(aOrigin, vOrigin, 200.0, fVelocity)
					fVelocity[2] = 400.0
		
					set_pev(target, pev_velocity, fVelocity)
					ExecuteHam(Ham_TakeDamage, target, CSW_KNIFE, id, random_float(65.0, 70.0), DMG_ALWAYSGIB | DMG_BULLET)
				}
			}
			emit_sound(id, CHAN_WEAPON, "weapons/knife_hitwall1.wav", 1.0, ATTN_NORM, 0, PITCH_NORM)
		}
		else
		{
			emit_sound(id, CHAN_WEAPON, "weapons/knife_slash1.wav", 1.0, ATTN_NORM, 0, PITCH_NORM)
			UTIL_PlayWeaponAnimation(id, 6)
		}
	}
	
	return HAM_IGNORED
}

public CurWeapon(weapon)
{
	new id = get_gun_owner(weapon)
	
	if(in_fuckinrambo == id)
	{
		set_pev(id, pev_viewmodel2, GunVModel)
		set_pev(id, pev_weaponmodel2, GunPModel)
	}
}

public ham_dmg(Victim, Gun, Attacker, Float:damage)
{
	if(!is_user_connected(Victim))
		return HAM_IGNORED
		
	if(in_fuckinrambo != Victim)
		return HAM_IGNORED
		
	if(n21_in_duel())
		return HAM_IGNORED
	
	if(is_user_connected(Attacker))
	{
		if(get_user_weapon(Attacker) != CSW_KNIFE)
		{
			strip_user_weapons(Attacker)
			give_item(Attacker, "weapon_knife") 
		}
	}
	
	return HAM_IGNORED
}

public SetRambo(id)
{
	in_fuckinrambo = id
	ham_strip_weapon(id,"weapon_glock18")
	fm_set_user_health(id, 250) 
	give_item(id, "weapon_m249") 
	cs_set_user_bpammo(id, CSW_M249, 9000) 
}

public SetInvis(id)
{
	if(is_user_alive(id) && ModeId == n21_dr_get_mode_id())
	{
		ham_strip_weapon(id,"weapon_usp")
		fm_set_user_rendering(id,  kRenderFxGlowShell, 0, 0, 0, kRenderTransAlpha,0)
	}
}
	
public UseButton(ent, id, idactivator, use_type, Float:value)
{
	if(!is_user_connected(id))
		return HAM_SUPERCEDE  	
	
	if(in_fuckinrambo == id)
		return HAM_SUPERCEDE  
		
	return HAM_IGNORED   
}

stock ham_strip_weapon(id,weapon[])
{
	if(!equal(weapon,"weapon_",7)) return 0
 
	new wId = get_weaponid(weapon)
	if(!wId) return 0
 
	new wEnt
	while((wEnt = engfunc(EngFunc_FindEntityByString,wEnt,"classname",weapon)) && pev(wEnt,pev_owner) != id) {}
	if(!wEnt) return 0
 
	if(get_user_weapon(id) == wId) ExecuteHamB(Ham_Weapon_RetireWeapon,wEnt)
 
	if(!ExecuteHamB(Ham_RemovePlayerItem,id,wEnt)) return 0
	ExecuteHamB(Ham_Item_Kill,wEnt)
 
	set_pev(id,pev_weapons,pev(id,pev_weapons) & ~(1<<wId))
     
	return 1
}

stock UTIL_PlayWeaponAnimation(const Player, const Sequence)
{
	set_pev(Player, pev_weaponanim, Sequence)
   
	message_begin(MSG_ONE_UNRELIABLE, SVC_WEAPONANIM, .player = Player)
	write_byte(Sequence)
	write_byte(pev(Player, pev_body))
	message_end()
}

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]
	new Float: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
}
