#include <amxmodx>
#include <fakemeta>
#include <zombieplague>

new g_Status, g_Time, g_Amount, g_ZombiePlague, g_Nemesis, g_FirstZombie, g_LastZombie, g_heal

new const health_sound[] = "zb/zombi_heal.wav"

public plugin_init()
{
	register_plugin("ZP: Regeneration", "1.3 Fix bug", "hleV")

	g_Status = register_cvar("zp_regeneration", "1")				//on/off
	g_Time = register_cvar("zp_regen_time", "1")				//vpema perena
	g_Amount = register_cvar("zp_regen_amount", "10")			//ckolko hp gavat npu perene
	g_Nemesis = register_cvar("zp_regen_nemesis", "1") 		//perenutca lu nemesis
	g_FirstZombie = register_cvar("zp_regen_firstzombie", "1") 	//perenutca lu 1 zombu
	g_LastZombie = register_cvar("zp_regen_lastzombie", "1") 	//perenutca lu last zombu

	register_event("Damage", "SetRegeneration", "be", "2>0")

	g_ZombiePlague = get_cvar_pointer("zp_on")
}

public plugin_precache() 
{ 
	engfunc(EngFunc_PrecacheSound, health_sound)
    	g_heal = precache_model("sprites/cso_heal.spr") 
} 

public SetRegeneration(Client)
{
	if(!g_ZombiePlague || !get_pcvar_num(g_Status) || !is_user_connected(Client) || !is_user_alive(Client) || !zp_get_user_zombie(Client))
		return

	new Health = get_user_health(Client)

	if(Health < 1)
		return

	if(!get_pcvar_num(g_Nemesis) && zp_get_user_nemesis(Client))
		return

	if(!get_pcvar_num(g_FirstZombie) && zp_get_user_first_zombie(Client))
		return

	if(!get_pcvar_num(g_LastZombie) && zp_get_user_last_zombie(Client))
		return

	if(get_user_health(Client) < zp_get_zombie_maxhealth(Client))
		set_task(get_pcvar_float(g_Time), "Regenerate", Client, _, _, "b")
}

public Regenerate(Client)
{
	if(!g_ZombiePlague || !get_pcvar_num(g_Status) || !is_user_connected(Client) || !is_user_alive(Client))
		return

	new Health = pev(Client, pev_health)

	if(Health < 1)
		return

	new NeededHealth = zp_get_zombie_maxhealth(Client) - Health

	if(NeededHealth <= get_pcvar_num(g_Amount))
	{
		set_pev(Client, pev_health, Health + float(NeededHealth))
		remove_task(Client)

		return
	}
	set_pev(Client, pev_health, Health + get_pcvar_float(g_Amount))

        engfunc(EngFunc_EmitSound, Client, CHAN_BODY, health_sound, 1.0, ATTN_NORM, 0, PITCH_NORM)

    	new origin[3] 
    	get_user_origin(Client,origin) 

    	message_begin(MSG_BROADCAST,SVC_TEMPENTITY) 
    	write_byte(TE_SPRITE) 
    	write_coord(origin[0]) 
    	write_coord(origin[1]) 
    	write_coord(origin[2]+=30) 
    	write_short(g_heal) 
    	write_byte(8) 
    	write_byte(255) 
    	message_end() 
}