/*================================================================================
	-------------------------------------------
	-*- [ZP] Zombie Class: Supplied Damage | by Re.Act!ve -*-
	-------------------------------------------

	~~~~~~~~~~~~~~~
	- Description -
	~~~~~~~~~~~~~~~	
	New class zombie for zombie plague. He had is Supplied Damage ability: supplied full damage to health in time ability

	~~~~~~~~~~~~~~~
	- Credits -
	~~~~~~~~~~~~~~~
	Re.Act!ve 			This Creator of zombie class plugin

	~~~~~~~~~~~~~~~
	- Changelog -
	~~~~~~~~~~~~~~~
	v. 1.0 - First Plugin Released
	v. 1.1 - Fixed Bugs ability
================================================================================*/
#include <amxmodx>
#include <fakemeta_util>
#include <zombieplague>

#define PLUGIN "[ZP] Zombie Class: Supplied Damage"
#define VERSION "1.1"
#define AUTHOR "Re.Act!ve"
#pragma tabsize 0

new const zclass_name[ ] = "Проклятый"
new const zclass_info[ ] = "Поглащает Урон"
new const zclass_model[ ] = "reindeer"
new const zclass_clawmodel[ ] = "v_reindeer.mdl"
const zclass_health = 850
const zclass_speed = 260
const Float:zclass_gravity = 0.9
const Float:zclass_knockback = 0.7
const Float:sup_time = 2.8			// Time of action ability
const Float:sup_cdown = 20.0			// CoolDown Ability
new sound_ability_start[] = { "zombie_plague/spells/sup_start.wav" }
new sound_ability_end[] = { "zombie_plague/spells/sup_end.wav" }
new SayText
new g_zclass_sup, sup_glow, sup_health_max
new bool: g_continue[33], g_ability[32], g_CoolDown[33]
new g_timer[33], g_timer1[33], g_timer0[33]
new sup_multiple = 2

public plugin_init( )
{
	sup_glow = 			register_cvar("zp_sup_aura", "1")			// Aura  in ability time < 0 | 1 >
	sup_health_max = 	register_cvar("zp_sup_health_max", "0") // Not to exceed a maximum of lives < 0 | 1 >
	register_plugin(PLUGIN, VERSION, AUTHOR)
	register_clcmd( "ability2", "use_cmd" )	
	register_event("Damage", "fw_TakeDamage", "b", "2!0", "3=0", "4!0")
	SayText = get_user_msgid("SayText")	
}

public plugin_precache( )
{
    	g_zclass_sup = zp_register_zombie_class( zclass_name, zclass_info, zclass_model, zclass_clawmodel, zclass_health, zclass_speed, zclass_gravity, zclass_knockback )
	precache_sound(sound_ability_start)
	precache_sound(sound_ability_end)
}

public zp_user_infected_post( player, infector )
{
    if( zp_get_user_zombie_class( player ) == g_zclass_sup )
    {
	g_ability[player] = false;
	g_CoolDown[player] = false;
   	ChatColor(player, "!g[ZP] !yPress !gF1 !y to use ability. Cooldown !g%.f !ysec.", sup_cdown)
	client_cmd(player, "bind F1 ^"ability2^"")
    }
}

public use_cmd( id )
{
	if( zp_get_user_zombie(id) && (zp_get_user_zombie_class(id) == g_zclass_sup) && !zp_get_user_nemesis(id) && is_user_alive(id)) 
	{
		if(g_CoolDown[id] || g_ability[id])
		{
			if(g_timer0[id] != 0)
			ChatColor(id, "!g[ZP] !yCoolDown is !g%i!y sec. ", g_timer0[id])
		}
		else
		{
			g_ability[id] = true;
			g_continue[id] = true;
			emit_sound(id, CHAN_STREAM, sound_ability_start, 1.0, ATTN_NORM, 0, PITCH_NORM)
			g_timer[id] = floatround(sup_time) * 10;
			g_timer1[id] = floatround(sup_time) * 10;
		     	set_task(0.1, "start_ability", id, _, _, "a",g_timer[id])
		}
	}
}

public fw_TakeDamage()
{
	new damage = read_data(2)
	new victim = read_data(0)
	if ((zp_get_user_zombie_class(victim) == g_zclass_sup) && zp_get_user_zombie(victim) && g_continue[victim] )
	{
		new x_hp = get_user_health(victim);
		new max_health = zp_get_zombie_maxhealth(victim);
		if(get_pcvar_num(sup_health_max) && (x_hp <= max_health) )		
			fm_set_user_health(victim , x_hp + damage * sup_multiple)
		else 
		{
			if(get_pcvar_num(sup_health_max) == 0)
			fm_set_user_health(victim, x_hp + damage * sup_multiple)
		}
	}	
}

public start_ability(player)
{
	if ((zp_get_user_zombie_class(player) == g_zclass_sup) && zp_get_user_zombie(player) && !zp_get_user_nemesis(player) && !g_CoolDown[ player ] && g_continue[player] )
	{
		if(g_timer[player] == g_timer1[player])
		set_task( sup_time, "ability_end", player )
		g_timer[player] = g_timer[player] - 1;
		if( (g_timer[player] > 1) && get_pcvar_num(sup_glow) )
		fm_set_rendering( player, kRenderFxGlowShell, 60, 200, 0, kRenderNormal, 16 ) 
	}
}
public ability_end( id )
{
	if( is_user_alive(id) && zp_get_user_zombie(id) )
	{
		g_continue[id] = false;
		g_CoolDown[id] = true;
		g_timer0[id] = floatround(sup_cdown);
		fm_set_rendering( id, 0, 0, 0, 0, kRenderNormal, 25 ) 
		emit_sound(id, CHAN_STREAM, sound_ability_end, 1.0, ATTN_NORM, 0, PITCH_NORM)
		set_task( sup_cdown + 1.0 , "reset_cooldown", id )
		set_task(1.0, "ShowHUD", id, _, _, "a",g_timer0[id])
	}
}

public ShowHUD(id)
{
	if(is_user_alive(id))
	{
		if(g_timer0[id] > 0)
		g_timer0[id] = g_timer0[id] - 1;
		set_hudmessage(200, 0, 0, 0.75, 0.92, 0, 1.0, 1.1, 0.0, 0.0, -1)
		show_hudmessage(id, "Cooldown Ability: %d",g_timer0[id])
	}
	else
	remove_task(id)
}

public reset_cooldown( player )
{
	g_ability[player] = false;
    	g_CoolDown[ player ] = false;
    	ChatColor( player, "!g[ZP] !yCooldown finished. !gSupplied Damage!y is ready." )
} 

stock ChatColor(const id, const input[], any:...)
{
	new count = 1, players[32]
	static msg[191]
	vformat(msg, 190, input, 3)
	
	replace_all(msg, 190, "!g", "^4") // Green Color
	replace_all(msg, 190, "!y", "^1") // Default Color
	replace_all(msg, 190, "!t", "^3") // Team Color
	replace_all(msg, 190, "!t2", "^0") // Team2 Color
	
	if (id) players[0] = id; else get_players(players, count, "ch")
	{
		for (new i = 0; i < count; i++)
		{
			if (is_user_connected(players[i]))
			{
				message_begin(MSG_ONE_UNRELIABLE, SayText, _, players[i])
				write_byte(players[i]);
				write_string(msg);
				message_end();
			}
		}
	}
}