#include <amxmodx>
#include <hamsandwich>
#include <engine>
#include <zombieplague>

const kill_count = 7
const nemesis_health = 10000

new g_count_kill
new g_maxplayers
new g_target_player

public plugin_init()
{
       register_plugin("[Z][ZP] Happy Zombie : Spawn Nemesis","1.0","batcon")

       RegisterHam(Ham_Killed, "player", "Ham_Killed_Post", true)

       register_event("HLTV", "event_round_start", "a", "1=0", "2=0")

       g_maxplayers = get_maxplayers()
}

public Ham_Killed_Post(victim, attacker, shouldgib)
{
       if(zp_is_plague_round())
       return HAM_IGNORED

       if(!zp_get_user_zombie(victim))
       return HAM_IGNORED

       if(zp_get_user_last_zombie(victim))
       return HAM_IGNORED

       g_count_kill++

       if(g_count_kill == kill_count)
       {
            g_target_player = random_player(random_num(1, alive_player()))

            zp_make_user_nemesis(g_target_player)

            set_health(g_target_player, float(nemesis_health))
       
            g_count_kill = 0
       }  

       return HAM_IGNORED
}

public event_round_start()
{
       g_count_kill = 0
}

stock random_player(target_index)
{
      new iAlive, id
	
      for(id = 1; id <= g_maxplayers; id++)
      {
         if(is_user_alive(id) && zp_get_user_zombie(id) && !zp_get_user_nemesis(id))
         iAlive++
		
         if(iAlive == target_index)
         return id
      }
	
      return -1
}

stock alive_player()
{
      new iAlive, id
	
      for(id = 1; id <= g_maxplayers; id++)
      {
          if(is_user_alive(id) && zp_get_user_zombie(id) && !zp_get_user_nemesis(id))
          iAlive++
      }
	
      return iAlive
}

stock set_health(index, Float:health)
{
      entity_set_float(index, EV_FL_health, health)
}