/* Plugin generated by ds811888 */

#include <amxmodx>
#include <amxmisc>
#include <fun>
#include <cstrike>
#include <fakemeta>
#include <biohazard>

#if !defined _biohazard_included 
        #assert Biohazard functions file required! 
#endif

#define OFFSET_NVGOGGLES    129
#define HAS_NVGS (1<<0)
#define USES_NVGS (1<<8)
#define PA_LOW  25.0
#define PA_HIGH 50.0

new antidote_enabled, antidote_cost, antidote_hudmessage, antidotegun_enabled,
antidotegun_cost

new gmsgNVGToggle, g_msgMoney
new g_hasadgun[33]

public plugin_init()
{
	/* Plugin */
	register_plugin("Biohazard Zombie Antidote & Antidote Gun", "1.5", "ds811888")

	g_msgMoney = get_user_msgid("Money")
	gmsgNVGToggle = get_user_msgid("NVGToggle")
    
	/* Commands */
	register_clcmd("say /buy_antidote", "infect_antidote")
	register_clcmd("say buy_antidote", "infect_antidote")
	register_clcmd("say /buy_antidotegun", "infect_antidotegun")
	register_clcmd("say buy_antidotegun", "infect_antidotegun")

	/* Admin Commands */
	register_concmd("amx_antidote", "cmd_give_antidote", ADMIN_BAN, "<name>")
	register_concmd("amx_antidotegun", "cmd_give_antidotegun", ADMIN_BAN, "<name>")

	/* Events */
	register_event("Damage", "event_damage", "b", "2>0")
	register_event("HLTV","event_newround", "a","1=0", "2=0")

	/* Pcvars */
	antidote_enabled     	= register_cvar("bio_antidote_enabled",     	"1" )
	antidote_cost         	= register_cvar("bio_antidote_cost",     	"5000")
	antidote_hudmessage    	= register_cvar("bio_antidote_hudmessage",	"1")
	antidotegun_enabled 	= register_cvar("bio_antidotegun_enabled",	"1")
	antidotegun_cost 	= register_cvar("bio_antidotegun_cost",		"8000")

	/* Language */
	register_dictionary("bio_antidote.txt")
}

public event_damage(id) 
{
	if(get_pcvar_num(antidotegun_enabled) <= 0 || !is_user_alive(id) || is_user_bot(id)) 
		return PLUGIN_CONTINUE;

	new iWeapID, attacker = get_user_attacker(id, iWeapID);

	if(!is_user_connected(attacker))
		return PLUGIN_CONTINUE;

	if(!is_user_connected(id))
		return PLUGIN_CONTINUE;

	/* if user have antidote.. */
	if(iWeapID == CSW_AWP) 
	{
		if (g_hasadgun[id])
		{
			set_user_human(id)
		}
	}
	return PLUGIN_CONTINUE;
}

public event_newround() 
{
	/* Remove user Antidote Gun*/
	for(new i = 0; i < 32; i++)
	{
		g_hasadgun[i] = false
	}
}

public client_connect(id)
{
	g_hasadgun[id] = false
}

/* Admin Commands: amx_antidote */
public cmd_give_antidote(id,level,cid)
{
	/* Check user admin */
	if (!cmd_access(id,level,cid,2))
		return PLUGIN_HANDLED;

        /* Check user alive */
        if(!is_user_alive(id))
	    return PLUGIN_HANDLED

        /* Check user zombie */
        if(!is_user_zombie(id))
            return PLUGIN_HANDLED;

	new arg[32];
	read_argv(1,arg,31);
	
	new player = cmd_target(id,arg,7);
	if (!player) 
		return PLUGIN_HANDLED;
	
	new name[32];
	get_user_name(player,name,31);

	/* Give a Antidote */
	set_user_human(player)
	
	return PLUGIN_HANDLED
}

/* Admin Commands: amx_antidotegun */
public cmd_give_antidotegun(id,level,cid)
{
	/* Check user admin */
	if (!cmd_access(id,level,cid,2))
		return PLUGIN_HANDLED;

        /* Check user alive */
        if(!is_user_alive(id))
		return PLUGIN_HANDLED

	new arg[32];
	read_argv(1,arg,31);
	
	new player = cmd_target(id,arg,7);
	if (!player) 
		return PLUGIN_HANDLED;
	
	new name[32];
	get_user_name(player,name,31);

	/* Give a Antidote Gun */
	g_hasadgun[player] = true
	give_item(player, "weapon_awp")
	
	return PLUGIN_HANDLED
}

/* Commands: say /buy_antidote */
public infect_antidote(id)
{    
    if(get_pcvar_num(antidote_enabled))
    {
        new money = cs_get_user_money(id) 
        new price_antidote = get_pcvar_num(antidote_cost)

        new ts[32], tsnum
        new maxplayers = get_maxplayers()
        new CsTeams:team
        
        for (new i=1; i<=maxplayers; i++)
        {
            if (!is_user_connected(i) || !is_user_alive(i))
            {
                continue
            }
            team = cs_get_user_team(i)
            
            if (team == CS_TEAM_T)
            {
                ts[tsnum++] = i
            }
        }

        /* Check user last zombie */
        if (tsnum == 1)
        {
            client_print(id, print_chat, "[BIO] %L", LANG_PLAYER, "LAST_ZOMBIE")
            return PLUGIN_HANDLED
        }

        /* Check user alive */
        if(!is_user_alive(id))
            return PLUGIN_HANDLED

        /* Check user zombie */
        if(!is_user_zombie(id))
            return PLUGIN_HANDLED
    
        /* Check user money */
        if(money <= price_antidote)
        {
            client_print(id, print_center, "[BIO] %L", LANG_PLAYER, "NOT_ENOUGH_MONEY" , price_antidote)
        }
        else
        {
            /* -user money to buy a zombie antidote */
            message_begin(MSG_ONE, g_msgMoney, _, id)
            write_long(money-price_antidote) // amount
            write_byte(1) // flash
            message_end()
            
            cs_set_user_money(id, money - price_antidote) 

            /* Client Print */
            client_print(id, print_center, "[BIO] %L", LANG_PLAYER, "USED_ANTIDOTE")

            /* Set user to survivor */
            set_user_human(id)

            /* HudMessage */
            if(get_pcvar_num(antidote_hudmessage))
            {
                set_hudmessage(0, 0, 255, -1.0, 0.53)
                show_hudmessage(id, "%L", LANG_PLAYER, "AGAIN_SURVIVOR")
            }
        }
    }
    return PLUGIN_HANDLED
}

/* Commands: say /buy_antidotegun */
public infect_antidotegun(id)
{    
    if(get_pcvar_num(antidotegun_enabled))
    {
        new money = cs_get_user_money(id) 
        new price_antidotegun = get_pcvar_num(antidotegun_cost)

        /* Check user alive */
        if(!is_user_alive(id))
            return PLUGIN_HANDLED

        /* Check user money */
        if(money <= price_antidotegun)
        {
            client_print(id, print_center, "[BIO] %L", LANG_PLAYER, "NOT_ENOUGH_MONEY_GUN" , price_antidotegun)
        }
        else
        {
            /* -user money to buy a zombie antidote gun */
            cs_set_user_money(id, money - price_antidotegun) 

            /* Client Print */
            client_print(id, print_center, "[BIO] %L", LANG_PLAYER, "BOUGHT_ANTIDOTEGUN")

	    /* Give user antidote gun */
	    g_hasadgun[id] = true
	    give_item(id, "weapon_awp")
        }
    }
    return PLUGIN_HANDLED
}

/* Set user to survivor */
stock set_user_human(id)
{
    cure_user(id)

    /* Remove user Nvgs */
    Remove_User_Nvgs(id)

    /* Set user health to 100 */
    set_user_health(id, 100)
    
    /* Set user to CT TEAM */
    cs_set_user_team(id, CS_TEAM_CT)

    /* reset user model */
    cs_reset_user_model(id)

    /* give user weapon */
    give_item(id, "weapon_m4a1")
    give_item(id, "weapon_deagle")

    /* set user ammo */
    cs_set_user_bpammo(id, CSW_M4A1, 90)
    cs_set_user_bpammo(id, CSW_DEAGLE, 35)
}

/* ConnorMcLeod by BeasT */
Remove_User_Nvgs(id)
{
    new iNvgs = get_pdata_int(id, OFFSET_NVGOGGLES, 5)
    if( !iNvgs )
    {
        return
    }
    if( iNvgs & USES_NVGS )
    {
        emit_sound(id, CHAN_ITEM, "items/nvg_off.wav", VOL_NORM, ATTN_NORM, 0, PITCH_NORM)

        emessage_begin(MSG_ONE, gmsgNVGToggle, _, id)
        ewrite_byte(0)
        emessage_end()
    }
    set_pdata_int(id, OFFSET_NVGOGGLES, 0, 5)
}  