/* 
                  
                  #@#######HH$                                                        
                             @X                                                                 
                             H@#,                                                                                             
               #######X$-.      $M:                                         
                        MHX$-    M%@:                                        
                          MHXM    M%M                                       
                           .;@X#   #X-                                       
                            %@%#   MM                                       
                 HX$H$X$M@@H#M#H%M =##                                       
              MX@#@HHHHH#@$$XM$X#%+###                                       
            X@##.     #$H#M@X%#$MMMM#               H#@M%                    
           @#H      HXHH$   #MMM####M%             ;M#.#@@$                  
         ,H        XHX;       XM#MMMHM             H#M   ###                 
        H#        .@          ####@#MM-        .MM/M#M    .@H$.              
                  H#        $$#MMH@##H        %$M#X##        :H@             
                 $#        $XH#H###### ;@H#HH  H# #H##         M             
                 H#       XH@+###@##;H=X=$=@@M#MM#@#M###:      #H            
                ##       M#  #@MX#%:+//;:::=H M####MM@$@MM##    ##           
                #       H#  #%MM#X;=:;;:;//::M###$$HHHHX@H@M##  ,@           
               H       H$  M$#M###MH;=;;;-:/+M#@@HHMHHHHXMHHM#%  #M          
             #M:      #X  MM. #MM#MMX=;;/;=:M###@H#HHHHHM@HHH###  @=         
             #       MM  #@    M###MH;//+/;%M#M#M@@HMH#HH#@HHM##, #@         
                  @H#: M$M      #M#MM+++%++#M###MMMMMM@M@#HH@M###  M:        
                      XXM        ####M$$$$%M########MMMMMMHMMM###. ##        
                     XH#         :M##M@HH%$##M###################   #:       
                    ,@$            M#MH@#@MMM #############H#####   .H@      
                   #@               #@,   XM     ,#########H###.      ##:    
                  #@                                       ##                
                 -#                                        #H                
                 M#                                        #                
                ##                 -== Tuty ==-             #                
               M#                                           -H               
            /@##                                             #@.             
                                                              ##
*/


/* Credits
* -------------
* 
*	() VEN 
*	() KleeneX
*	() Alka
*/



#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <engine>

#define ADMIN_CMD ADMIN_BAN


new const PLUGIN[] 	= "Deagle Nail Launcher"
new const VERSION[] 	= "2.3"
new const AUTH[] 	= "tuty"

new nail_MODEL[64] 	= "models/nail.mdl"
new nail_SOUND[65]	= "misc/spike1.wav"


new Float:was_nail[33];

new trail, explode;
new g_toggle_enable, g_toggle_speed, g_toggle_delay, g_toggle_trail_width;
new g_toggle_admin_only, g_toggle_damageradius, g_toggle_nail_damage;
new gMsgID, gDmsgID, g_toggle_colormode;


public plugin_init() {
	
	
	
	register_plugin(PLUGIN, VERSION, AUTH);
	
	g_toggle_enable = register_cvar("nail_enable", "1");// 1-enabled/2-disabled
	g_toggle_admin_only= register_cvar("nail_admin_only", "0");// set this to 1 from ADMINONLY
	g_toggle_speed = register_cvar("nail_speed","600");// nail speed
	g_toggle_delay = register_cvar("nail_delay","3.0"); // nail delay
	g_toggle_damageradius = register_cvar("nail_damage_radius", "500");// nail explode radius damage
	g_toggle_nail_damage = register_cvar("nail_damage", "200");// nail damage when hit someone
	g_toggle_trail_width = register_cvar("nail_trail_width", "5");// trail width
	g_toggle_colormode = register_cvar("nail_trail_colormode", "1");// 0-grey and 1- team color (default: 1 - team color)
	
	gMsgID = get_user_msgid("Damage");
	gDmsgID = get_user_msgid("DeathMsg");
	
	
	
}
public server_frame() {
	
	
	
	if(get_pcvar_num(g_toggle_enable) == 0)
		return PLUGIN_HANDLED;
		

	for(new id = 1; id < 33; id++) {
		
		if(is_user_alive(id)) {
			
			
			check_nails(id);

		}
		
	}
	return PLUGIN_CONTINUE;
	
	
}
public plugin_precache()
{
	
	
	explode 	= precache_model("sprites/zerogxplode.spr");
	trail 		= precache_model("sprites/smoke.spr");
	
	precache_model(nail_MODEL);
	precache_sound(nail_SOUND);

	
	
}
public check_nails(id) {
	
	
	if(get_pcvar_num(g_toggle_enable) == 0)
	{
		
		new name[32];
		get_user_name(id, name, 31);
		
		client_print(id, print_chat, "[DNL] Sorry %s, but the plugin is OFF!", name);
		
		return PLUGIN_HANDLED;
		
	}
		
	new clip,ammo;
	new wpnid = get_user_weapon(id,clip,ammo);
	new button = pev(id,pev_button);
	
	
	if ( wpnid == CSW_DEAGLE) {
		
		
		if(button & IN_ATTACK2) {
			
				
			launch_nail(id);
			
		}
		
	}
	return PLUGIN_CONTINUE;
	
	
}
public launch_nail(id)
{
	
	
	if(get_pcvar_num(g_toggle_enable) == 0)
	{
		
		new name[32];
		get_user_name(id, name, 31);
		
		client_print(id, print_chat, "[DNL] Sorry %s, but the plugin is OFF!", name);
		
		return PLUGIN_HANDLED;
		
	}
	
	
	if(get_pcvar_num(g_toggle_admin_only) == 1) {
		
		if(!(get_user_flags(id) & ADMIN_CMD)) {
			
			new name[32];
			get_user_name(id, name, 31);
			
			client_print(id, print_chat, "[DNL] Sorry %s, the pugin has been set just 4 admins!", name);
			log_amx("[DNL] The plugin has been set just 4 admins!");
			
			return PLUGIN_HANDLED;
		}
	}
	
	new Float:nexTime = get_gametime();
	
	if (was_nail[id] > nexTime) {
		
		
		return PLUGIN_CONTINUE;
		
	}
	else
	{
		
		new nail = create_entity("info_target");
		
		if(nail == 0) 
			return PLUGIN_CONTINUE;
		
		entity_set_string(nail, EV_SZ_classname, "deagle_nail");
		entity_set_model(nail, nail_MODEL);
		entity_set_size(nail, Float:{0.0, 0.0, 0.0}, Float:{0.0, 0.0, 0.0});
		entity_set_int(nail, EV_INT_movetype, MOVETYPE_FLY);
		entity_set_int(nail, EV_INT_solid, SOLID_BBOX);
		
		new Float:vSrc[3];
		entity_get_vector(id, EV_VEC_origin, vSrc);
		
		new Float:Aim[3],Float:origin[3];
		VelocityByAim(id, 64, Aim);
		entity_get_vector(id,EV_VEC_origin,origin);
		
		vSrc[0] += Aim[0];
		vSrc[1] += Aim[1];

		entity_set_origin(nail, vSrc);
		
		new Float:velocity[3], Float:angles[3];
		VelocityByAim(id, get_pcvar_num(g_toggle_speed), velocity);
		
		entity_set_vector(nail, EV_VEC_velocity, velocity);
		vector_to_angle(velocity, angles);
		entity_set_vector(nail, EV_VEC_angles, angles);
		entity_set_edict(nail,EV_ENT_owner,id);
		entity_set_float(nail, EV_FL_takedamage, 1.0);
		
		message_begin(MSG_BROADCAST, SVC_TEMPENTITY);
		write_byte(TE_BEAMFOLLOW);
		write_short(nail);
		write_short(trail);
		write_byte(25);
		write_byte(get_pcvar_num(g_toggle_trail_width));
		if(get_pcvar_num(g_toggle_colormode))
		{
			
			if(get_user_team(id) == 1)
			{
				
				write_byte(255);
				write_byte(0);
				write_byte(0);
			}
			else if(get_user_team(id) == 2)
			{
				
				write_byte(0);
				write_byte(0);
				write_byte(255);
				
			}
			else 
			{
				
				write_byte(211);
				write_byte(211);
				write_byte(211);
				
			}
		}
		else 
		{
				
			write_byte(211);
			write_byte(211);
			write_byte(211);
		}
				
		write_byte(255);
		message_end();

		emit_sound(nail, CHAN_WEAPON, nail_SOUND, 1.0, ATTN_NORM, 0, PITCH_NORM);
		was_nail[id] = nexTime + get_pcvar_float(g_toggle_delay);
		
	
	}
	
	return PLUGIN_CONTINUE;


}
public pfn_touch(ptr, ptd) {
	
	
	if(get_pcvar_num(g_toggle_enable) == 0)
		return PLUGIN_HANDLED;
	
	
	if(is_valid_ent(ptr)) {
		
		
		new classname[32];
		entity_get_string(ptr,EV_SZ_classname,classname,31);
		
		if(equal(classname, "deagle_nail")) {
			
			
			new Float:fOrigin[3];
			new iOrigin[3];
			entity_get_vector(ptr, EV_VEC_origin, fOrigin);
			FVecIVec(fOrigin,iOrigin);
			
			nail_radius_damage(ptr);
				
			message_begin(MSG_BROADCAST,SVC_TEMPENTITY,iOrigin);
			write_byte(TE_EXPLOSION);
			write_coord(iOrigin[0]);
			write_coord(iOrigin[1]);
			write_coord(iOrigin[2]);
			write_short(explode);
			write_byte(30);
			write_byte(15);
			write_byte(0);
			message_end();
				
			
			if(is_valid_ent(ptd)) {
				
				
				new classname2[32];
				entity_get_string(ptd,EV_SZ_classname,classname2,31);
				
				if(equal(classname2,"func_breakable")) // this is 4 breakable doors or objects and you can break it;)
					force_use(ptr,ptd);
					
			}
			
			remove_entity(ptr);
		}
	}
	
	
	return PLUGIN_CONTINUE;
	
}
stock nail_radius_damage(entity) {
	
	
	new id = entity_get_edict(entity,EV_ENT_owner);
	
	for(new i = 1; i < 33; i++) {
		
		if(is_user_alive(i)) {
			
			new dist = floatround(entity_range(entity,i));
			
			if(dist <= get_pcvar_num(g_toggle_damageradius)) {
				
				new hp = get_user_health(i);
				new Float:damage = get_pcvar_float(g_toggle_nail_damage)-(get_pcvar_float(g_toggle_nail_damage) / get_pcvar_float(g_toggle_damageradius))*float(dist)
				
				new Origin[3];
				get_user_origin(i,Origin);
				
				if(!get_cvar_num("mp_friendlyfire")) {
					
					if(get_user_team(id) != get_user_team(i)) {
						
						if(hp > damage)
						{
							
							nail_take_damage(i,floatround(damage),Origin,DMG_BLAST);
							
						}
						else
						{
							
							nail_kill(id,i,"** Deagle_Nail **",0);
							
						}
					}
				}
				else
				{
					if(hp > damage)
					{
						
						nail_take_damage(i,floatround(damage),Origin,DMG_BLAST);
						
					}
					else
					{
						
						nail_kill(id,i,"** Deagle_Nail **",0);
						
					}
					
				}
			}
		}
	}
}
stock nail_take_damage(victim,damage,origin[3],bit)
{

	
	message_begin(MSG_ONE, gMsgID,{0,0,0},victim);
	write_byte(21);
	write_byte(20);
	write_long(bit);
	write_coord(origin[0]);
	write_coord(origin[1]);
	write_coord(origin[2]);
	message_end();
	
	fm_set_user_health(victim,get_user_health(victim)-damage);
	
	
}
stock nail_kill(killer, victim, weapon[],headshot)
{
	
	
	set_msg_block(gDmsgID , BLOCK_SET);
	user_kill(victim,1);
	set_msg_block(gDmsgID, BLOCK_NOT);
	
	message_begin(MSG_ALL, gDmsgID, {0,0,0}, 0);
	write_byte(killer);
	write_byte(victim);
	write_byte(headshot);
	write_string(weapon);
	message_end();
	
	if(get_user_team(killer)!= get_user_team(victim))
	{
		
		fm_set_user_frags(killer,get_user_frags(killer) +1);
		
	}
	if(get_user_team(killer) == get_user_team(victim))
	{
		
		fm_set_user_frags(killer,get_user_frags(killer) -1);
		
	}

 	return PLUGIN_CONTINUE;
	
}
stock fm_set_user_frags(index, frags)
{
	
	set_pev(index, pev_frags, float(frags));

	return 1;

}
stock fm_set_user_health(index, health)
{

	
	health > 0 ? set_pev(index, pev_health, float(health)) : dllfunc(DLLFunc_ClientKill, index);

	return 1;

}
