#include <amxmodx>
#include <engine>
#include <fakemeta>
#include <basebuilder>
#include <fun>
#include <hamsandwich>
#include <fakemeta_util>
#define is_user_frozen(%1)   bFrozen[%1]

#define PLUGIN "[BB] Zombie Class: Freez"
#define VERSION "1.1"
#define AUTHOR "Argon"

new const zclass_name[] = { "Freezer" }
new const zclass_info[] = { "\r[Freez Human] \y[Mouse2]" }
new const zclass_model[] = { "bb_zombie_freezer" }
new const zclass_clawmodel[] = { "bb_basebulder/bb_vv_freezer" }
const zclass_health = 1000
const zclass_speed = 240
const Float:zclass_gravity = 1.0
const Float:zclass_knockback = 0.25

new beamSpr, freezer_spr
new const freezer_sound[] = "basebulder/freezer.wav"

//Cvars
new pcvar_distance
new cvar_cooldown // Cooldown when dropped the weapon of human
new pcvar_freeze
new g_freezer

new g_maxplayers
new is_cooldown[33] = 0
new bool:g_cd[33]
new cvar_nemesis
new Countdown[33] 
 
new bool:bFrozen[33]
new Float:iAngles[33][3]
const iVictim = 2

public plugin_precache()
{
	g_freezer = zp_register_zombie_class(zclass_name, zclass_info, zclass_model, zclass_clawmodel, zclass_health, zclass_speed, zclass_gravity, zclass_knockback)
	beamSpr = precache_model("sprites/lgtning.spr")
	freezer_spr = precache_model("sprites/freezer_exp.spr")
	precache_sound(freezer_sound)
}

public plugin_init() 
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	pcvar_distance = register_cvar ( "zp_deimos_distancex", "1000" )
	cvar_cooldown = register_cvar("zp_deimos_shot_cooldownx","20.0")
	pcvar_freeze = register_cvar( "zp_frost_freeze_time", "5.0" )
	cvar_nemesis = register_cvar("zp_deimos_nemesisx", "1")
	g_maxplayers = get_maxplayers()
	register_logevent("roundStart", 2, "1=Round_Start")
	register_forward(FM_CmdStart, "fwd_cmd_start")
	RegisterHam(Ham_Spawn,"player","fw_PlayerSpawn_Post",1)
	RegisterHam(Ham_Spawn, "player", "ham_PlayerSpawn_Post", 1)
	register_event( "DeathMsg" , "eventDeathMsg" , "a" );	
	register_logevent( "eventRoundEnd" , 2 , "1=Round_End" );	
	
}

public fw_PlayerSpawn_Post(id)
{
	// Not alive...
	if(!is_user_alive(id))
		return HAM_IGNORED
	if(cvar_nemesis == 1)
	{
		if(bb_get_user_zombie_class(id) == g_freezer && bb_is_user_zombie(id))
		{
			g_cd[id] = true
		}
		} else {
		if(bb_get_user_zombie_class(id) == g_freezer && bb_is_user_zombie(id))
		{
			g_cd[id] = true
		}	
	}
	
	return HAM_IGNORED
}  

public fwd_cmd_start(id, uc_handle, seed) 
{
	if(cvar_nemesis == 1)
	{
		if (!is_user_alive(id) || !bb_is_user_zombie(id) || g_cd[id] )
			return FMRES_IGNORED
		} else {
		if (!is_user_alive(id) || !bb_is_user_zombie(id) || g_cd[id] )
			return FMRES_IGNORED
	}
	
	if (bb_get_user_zombie_class(id) != g_freezer)
		return FMRES_IGNORED
	
	static buttons
	buttons = get_uc(uc_handle, UC_Buttons)
	
	if(buttons & IN_ATTACK2) {
		cmd_freez(id)
		g_cd[id] = true
	}
	
	buttons &= ~IN_ATTACK2
	set_uc(uc_handle, UC_Buttons, buttons)
	
	return FMRES_HANDLED
}

cmd_freez(id)
{
	new target, body
	static Float:start[3]
	static Float:aim[3]
	
	pev(id, pev_origin, start)
	fm_get_aim_origin(id, aim)
	
	start[2] += 16.0; // raise
	aim[2] += 16.0; // raise
	get_user_aiming ( id, target, body, pcvar_distance )
	
	if( is_user_alive( target ) && !bb_is_user_zombie( target ))
	{	
		message_begin(MSG_BROADCAST ,SVC_TEMPENTITY)
		write_byte(TE_EXPLOSION)
		engfunc(EngFunc_WriteCoord, aim[0])
		engfunc(EngFunc_WriteCoord, aim[1])
		engfunc(EngFunc_WriteCoord, aim[2])
		write_short(freezer_spr)
		write_byte(10)
		write_byte(30)
		write_byte(4)
		message_end()
				
		Freeze(target)		
		set_task( get_pcvar_float( pcvar_freeze ), "UnFreeze", target )
	}	
	
	message_begin(MSG_BROADCAST,SVC_TEMPENTITY)
	write_byte(0)
	engfunc(EngFunc_WriteCoord,start[0]);
	engfunc(EngFunc_WriteCoord,start[1]);
	engfunc(EngFunc_WriteCoord,start[2]);
	engfunc(EngFunc_WriteCoord,aim[0]);
	engfunc(EngFunc_WriteCoord,aim[1]);
	engfunc(EngFunc_WriteCoord,aim[2]);
	write_short(beamSpr); // sprite index
	write_byte(0); // start frame
	write_byte(10); // frame rate in 0.1's
	write_byte(10); // life in 0.1's
	write_byte(100); // line width in 0.1's
	write_byte(4); // noise amplititude in 0.01's
	write_byte(100); // red
	write_byte(149); // green
	write_byte(237); // blue
	write_byte(100); // brightness
	write_byte(50); // scroll speed in 0.1's
	message_end();	
	emit_sound(id, CHAN_WEAPON, freezer_sound, VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
	set_task( get_pcvar_float( cvar_cooldown ), "reset_cooldown2", id );
	set_task( 1.0, "CountDown1", id, _, _, "a",Countdown [ id ] );
	Countdown [ id ] = get_pcvar_num( cvar_cooldown )
}

public ham_PlayerSpawn_Post(id, infector)
{
	if (bb_get_user_zombie_class(id) == g_freezer)
	{
		is_cooldown[id] = 0
		g_cd[id] = false
		new text[100]
		format(text,99,"^x04[BB]^x01 Your ability is^x04 Freez Human^x01. Cooldown:^x04 %d ^x01seconds. Press ^x04[MOUSE2]^x01 to use this.",get_pcvar_num( cvar_cooldown ))
		message_begin(MSG_ONE,get_user_msgid("SayText"),{0,0,0},id) 
		write_byte(id) 
		write_string(text) 
		message_end()
	}
}
public reset_cooldown2(id)
{
		g_cd[id] = false	
		new text[100]
		format(text,99,"^x04[BB]^x01 Now You Can Use Your ability. Press ^x04[MOUSE2]")
		message_begin(MSG_ONE,get_user_msgid("SayText"),{0,0,0},id) 
		write_byte(id) 
		write_string(text) 
		message_end()
}

public zp_user_humanized_post(id)
{
	remove_task(id)
	is_cooldown[id] = 0
}

public roundStart()
{
	for (new i = 1; i <= g_maxplayers; i++)
	{
		Countdown [ i ] = get_pcvar_num( cvar_cooldown )
		is_cooldown[i] = 0
		remove_task(i)
	}
}

stock Freeze(id)
{ 
	new iFlags = pev( id , pev_flags );
	
	if( ~iFlags & FL_FROZEN ) 
	{ 
		set_pev( id , pev_flags , iFlags | FL_FROZEN );
		pev( id , pev_v_angle , iAngles[ id ] );
		
		set_user_rendering( id , kRenderFxGlowShell , 0 , 100 , 200 , kRenderNormal , 16 );		
	} 
	
	bFrozen[ id ] = true;	
	
}
public eventDeathMsg( )
{	
	UnFreeze( read_data( iVictim ) );
}

public eventRoundEnd( )
{
	new iPlayers[ 32 ] , iNum , i , id;
	get_players( iPlayers , iNum , "a" );
	
	for( i = 0; i < iNum; i++ )
	{
		id = iPlayers[ i ];
		
		if( is_user_frozen( id ) )
		{
			UnFreeze( id );
		}
	}
}
public UnFreeze(id)
{ 
	new frozenCount = 0;
	
	bFrozen[ id ] = false;
	
	new iFlags = pev( id , pev_flags ) ;
	
	if( iFlags & FL_FROZEN ) 
	{ 
		set_pev( id , pev_flags , iFlags & ~FL_FROZEN ) ;
		set_user_rendering( id , kRenderFxNone , 0 , 0 , 0 , kRenderNormal , 0 );
		
		new iPlayers[ 32 ] , iNum , i , tid;
		get_players( iPlayers , iNum , "a" );
		
		for( i = 0; i < iNum; i++ )
		{
			tid = iPlayers[ i ];
			
			if( is_user_frozen( tid ) )
			{
				frozenCount++;
			}
		}				
	} 
}
public CountDown1(id) { 
	

	if(is_valid_ent(id) && is_user_alive(id))
	{
		Countdown [ id ] = Countdown [ id ] - 1;
		set_hudmessage(0, 255, 0, 0.75, 0.92, 0, 1.0, 1.1, 0.0, 0.0, -1)
		show_hudmessage(id, "Freez Ability cooldown: %d",Countdown [ id ])
		
	}else{
		remove_task(id)
	}
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ fbidis\\ ansi\\ ansicpg1252\\ deff0{\\ fonttbl{\\ f0\\ fnil\\ fcharset0 Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ ltrpar\\ lang1049\\ f0\\ fs16 \n\\ par }
*/
