#include <amxmodx>
#include <amxmisc>
#include <engine>
#include <cstrike>
#include <fakemeta>
#include <fakemeta_util>
#include <hamsandwich>
#include <zombieplague>

#define PLUGIN "[ZP] Addon: Bonus Box"
#define VERSION "1.1"
#define AUTHOR "DOK_BATCOH"

#define ENT_CLASS_MONEY    "bonus_box_money"
#define ENT_CLASS_AMMO     "bonus_box_ammo"
#define ENT_CLASS_MEDKIT   "bonus_medkit"
#define ENT_CLASS_KEY      "bonus_key"
#define ENT_CLASS_ARMOR    "bonus_armor"

#define GIVE_MONEY_MIN 2000          
#define GIVE_MONEY_MAX 3000          

#define GIVE_HEALTH 50
#define GIVE_ARMOR  25

#define COLOR_MONEY_R 34.0
#define COLOR_MONEY_G 139.0
#define COLOR_MONEY_B 34.0

#define COLOR_AMMO_R 255.0
#define COLOR_AMMO_G 255.0
#define COLOR_AMMO_B 0.0

#define COLOR_MEDKIT_R 255.0
#define COLOR_MEDKIT_G 250.0
#define COLOR_MEDKIT_B 250.0

#define COLOR_KEY_R 0.0
#define COLOR_KEY_G 0.0
#define COLOR_KEY_B 250.0

#define COLOR_ARMOR_R 0.0
#define COLOR_ARMOR_G 0.0
#define COLOR_ARMOR_B 250.0

#define MODEL_MONEY   "models/w_9mmar.mdl"
#define MODEL_AMMO    "models/w_chainammo.mdl"
#define MODEL_MEDKIT  "models/w_medkit.mdl"
#define MODEL_KEY     "models/w_security.mdl"
#define MODEL_ARMOR   "models/w_battery.mdl"

#define SOUND_BUY_AMMO   "items/9mmclip1.wav"
#define SOUND_BUY_HEALTH "items/smallmedkit1.wav"

enum
{
        ACTION_MONEY = 0,
        ACTION_AMMO,
        ACTION_MEDKIT,
        ACTION_KEY,
        ACTION_ARMOR
}

new const MAXBPAMMO[] = { -1, 52, -1, 90, 1, 32, 1, 100, 90, 1, 120, 100, 100, 90, 90, 90, 100, 120,
			30, 120, 200, 32, 90, 120, 90, 2, 35, 90, 90, -1, 100 }

new const AMMOTYPE[][] = { "", "357sig", "", "762nato", "", "buckshot", "", "45acp", "556nato", "", "9mm", "57mm", "45acp",
			"556nato", "556nato", "556nato", "45acp", "9mm", "338magnum", "9mm", "556natobox", "buckshot",
			"556nato", "9mm", "762nato", "", "50ae", "556nato", "762nato", "", "57mm" }

new g_key[33]

public plugin_init()
{
       register_plugin(PLUGIN, VERSION, AUTHOR)

       register_forward(FM_Touch,"fwd_Touch_Items")

       RegisterHam(Ham_Killed, "player", "fw_PlayerKilled")

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

       register_dictionary("z_box.txt")
}

public plugin_precache()
{
       precache_model(MODEL_MONEY)
       precache_model(MODEL_AMMO)
       precache_model(MODEL_MEDKIT)
       precache_model(MODEL_KEY)
       precache_model(MODEL_ARMOR)

       precache_sound(SOUND_BUY_AMMO)
       precache_sound(SOUND_BUY_HEALTH)	
}

public plugin_natives()
{
       register_native("get_user_key", "native_get_user_key", 1)
       register_native("set_user_key", "native_set_user_key", 1)
}

public fwd_Touch_Items(id, ent)
{
	if (!is_user_alive(id) || !pev_valid(ent) || zp_get_user_zombie(id) || zp_get_user_survivor(id) || zp_get_user_nemesis(id))
		return FMRES_IGNORED
	
        new classname[32]
	
	pev(ent, pev_classname, classname, 31)
	if (!equal(classname, ENT_CLASS_AMMO) && !equal(classname, ENT_CLASS_MONEY) && !equal(classname, ENT_CLASS_MEDKIT) && !equal(classname, ENT_CLASS_KEY) && !equal(classname, ENT_CLASS_ARMOR))
                return FMRES_IGNORED

        if (equal(classname, ENT_CLASS_MONEY))
        {

        give_money(id)

        set_pev(ent, pev_effects, EF_NODRAW)
	set_pev(ent, pev_solid, SOLID_NOT)

        }
        else if (equal(classname, ENT_CLASS_AMMO))
        {

        give_ammo(id)

        set_pev(ent, pev_effects, EF_NODRAW)
	set_pev(ent, pev_solid, SOLID_NOT)

        }	
        else if(equal(classname, ENT_CLASS_MEDKIT))
        {

        give_medkit(id)

        set_pev(ent, pev_effects, EF_NODRAW)
	set_pev(ent, pev_solid, SOLID_NOT)

        }
        else if(equal(classname, ENT_CLASS_KEY) && g_key[id] == 0)
        {

        give_key(id)

        set_pev(ent, pev_effects, EF_NODRAW)
	set_pev(ent, pev_solid, SOLID_NOT)

        }
        else if(equal(classname, ENT_CLASS_ARMOR))
        {

        give_armor(id)

        set_pev(ent, pev_effects, EF_NODRAW)
	set_pev(ent, pev_solid, SOLID_NOT)

        }

	return FMRES_IGNORED
}

public fw_PlayerKilled(victim, attacker, shouldgib)
{
        if(zp_is_survivor_round() || !zp_get_user_zombie(victim) || zp_get_user_survivor(victim) || zp_get_user_nemesis(victim))
        return HAM_IGNORED

	if (!is_user_connected(attacker) || !is_user_connected(victim) || attacker == victim || !attacker)
		return HAM_IGNORED
	
        new origin[3]
	get_user_origin(victim, origin, 0)

	new random = random_num(0, 14)
	
	if (random == 0 || random == 1)
	{
		ent(origin, ACTION_MONEY)
	}
        else if(random == 2 || random == 3)
	{	
		ent(origin, ACTION_AMMO)
	}
        else if(random == 4 || random == 5)
	{
		ent(origin, ACTION_MEDKIT)
	}
        else if(random == 6 || random == 7)
	{	
		ent(origin, ACTION_KEY)
	}
        else if(random == 8 || random == 9)
	{	
		ent(origin, ACTION_ARMOR)
	}
	
	return HAM_IGNORED
}

public zp_user_infected_post(id)
{
       g_key[id] = 0
}

public ent(origin[3], activ_item)
{
        new Float:fOrigin[3]

	new ent = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "info_target"))
	
        switch(activ_item)
        {
              case ACTION_MONEY: 
              {

              engfunc(EngFunc_SetModel,ent, MODEL_MONEY) 
              set_pev(ent,pev_classname, ENT_CLASS_MONEY)
              set_pev(ent,pev_rendercolor,Float:{COLOR_MONEY_R,COLOR_MONEY_G,COLOR_MONEY_B})

              }
              case ACTION_AMMO: 
              {

              engfunc(EngFunc_SetModel,ent, MODEL_AMMO) 
              set_pev(ent,pev_classname, ENT_CLASS_AMMO)
              set_pev(ent,pev_rendercolor,Float:{COLOR_AMMO_R,COLOR_AMMO_G,COLOR_AMMO_B})

              }
              case ACTION_MEDKIT: 
              {

              engfunc(EngFunc_SetModel,ent, MODEL_MEDKIT) 
              set_pev(ent,pev_classname, ENT_CLASS_MEDKIT)
              set_pev(ent,pev_rendercolor,Float:{COLOR_MEDKIT_R,COLOR_MEDKIT_G,COLOR_MEDKIT_B})

              }
              case ACTION_KEY: 
              {

              engfunc(EngFunc_SetModel,ent, MODEL_KEY) 
              set_pev(ent,pev_classname, ENT_CLASS_KEY)
              set_pev(ent,pev_rendercolor,Float:{COLOR_KEY_R,COLOR_KEY_G,COLOR_KEY_B})

              }
              case ACTION_ARMOR: 
              {

              engfunc(EngFunc_SetModel,ent, MODEL_ARMOR) 
              set_pev(ent,pev_classname, ENT_CLASS_ARMOR)
              set_pev(ent,pev_rendercolor,Float:{COLOR_ARMOR_R,COLOR_ARMOR_G,COLOR_ARMOR_B})

              }
        }

        engfunc(EngFunc_SetSize, ent, { -2.0, -2.0, -2.0 }, { 2.0, 2.0, 2.0 })
        IVecFVec(origin, fOrigin)
        engfunc(EngFunc_SetOrigin, ent, fOrigin)

	set_pev(ent,pev_solid,SOLID_BBOX)
	set_pev(ent,pev_movetype,MOVETYPE_PUSHSTEP)

        set_pev(ent, pev_nextthink, get_gametime() + 0.01);
	
	set_pev(ent,pev_renderfx,kRenderFxGlowShell)

        engfunc(EngFunc_DropToFloor, ent)
}

public give_money(id)
{
        new money = random_num(GIVE_MONEY_MIN, GIVE_MONEY_MAX)

        client_print(id,print_center,"+ %d $", money)

	zp_set_user_ammo_packs(id, zp_get_user_ammo_packs(id) + money)
        emit_sound(id, CHAN_ITEM, SOUND_BUY_AMMO, 1.0, ATTN_NORM, 0, PITCH_NORM)
}

public give_ammo(id)
{
	new weapons[32], num_weapons, index, weaponid
	get_user_weapons(id, weapons, num_weapons)
	
	for (index = 0; index < num_weapons; index++)
	{
		weaponid = weapons[index]
		
		if (MAXBPAMMO[weaponid] > 2)
		{
			
		ExecuteHamB(Ham_GiveAmmo, id, MAXBPAMMO[weaponid], AMMOTYPE[weaponid], MAXBPAMMO[weaponid])
			
	        client_print(id,print_center,"%L", LANG_PLAYER, "GIVE_AMMO")

		emit_sound(id, CHAN_ITEM, SOUND_BUY_AMMO, 1.0, ATTN_NORM, 0, PITCH_NORM)
		}
	}	
	
	return PLUGIN_HANDLED
}

public give_medkit(id)
{
        fm_set_user_health(id, get_user_health(id) + GIVE_HEALTH)

        emit_sound(id, CHAN_ITEM, SOUND_BUY_HEALTH, 1.0, ATTN_NORM, 0, PITCH_NORM)
}

public give_key(id)
{
        client_print(id,print_center,"%L", LANG_PLAYER, "GIVE_KEY")

        g_key[id] = 1

        emit_sound(id, CHAN_ITEM, SOUND_BUY_AMMO, 1.0, ATTN_NORM, 0, PITCH_NORM)
}

public give_armor(id)
{
        client_print(id,print_center,"%L", LANG_PLAYER, "GIVE_ARMOR")

        fm_set_user_armor(id, get_user_armor(id) + GIVE_ARMOR)

        emit_sound(id, CHAN_ITEM, SOUND_BUY_AMMO, 1.0, ATTN_NORM, 0, PITCH_NORM)
}

public round_start()
{
	fm_remove_entity_name(ENT_CLASS_MONEY)
        fm_remove_entity_name(ENT_CLASS_AMMO)
        fm_remove_entity_name(ENT_CLASS_MEDKIT)
        fm_remove_entity_name(ENT_CLASS_KEY)
        fm_remove_entity_name(ENT_CLASS_ARMOR)
}

public native_get_user_key(id)
{
	
	return g_key[id];
}

public native_set_user_key(id, amount)
{
	g_key[id] = amount;
	return true;
}
