/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <fakemeta>
#include <zombieplague>
#include <fun>
#include <hamsandwich>
#include <engine>
#define PLUGIN "M24-Grenade"
#define VERSION "1.0"
#define AUTHOR "Barney/Faktor"

new const NADE_TYPE_M24 = 100500
new const Float:RADIUS = 240.0
new const item_cost = 1
new const item_name[] = "M24 Grenade"
new const v_model_m24[] = "models/zombie_plague/v_m24grenade.mdl"
new const p_model_m24[] = "models/zombie_plague/p_m24grenade.mdl"
new const w_model_m24[] = "models/zombie_plague/w_m24grenade.mdl"
new const g_soundexpl[] = "weapons/explode3.wav"

new item_id,decal
new has_bomb[33]
new spriteexpl,gmsgWeaponList
public plugin_init() 
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	RegisterHam( Ham_Item_AddToPlayer, "weapon_hegrenade", "fw_AddToPlayer" )
	RegisterHam(Ham_Think, "grenade", "fw_ThinkGrenade")
	RegisterHam(Ham_Killed, "player", "fw_PlayerKilled")
	register_message(get_user_msgid("DeathMsg"), "message_deathmsg")
	register_forward(FM_SetModel, "fw_SetModel")	
	decal = engfunc(EngFunc_DecalIndex,"{scorch3")
	gmsgWeaponList = get_user_msgid("WeaponList")
	register_message(get_user_msgid("CurWeapon"), "message_cur_weapon")
	item_id = zp_register_extra_item(item_name,item_cost,ZP_TEAM_HUMAN)
}

public plugin_precache()
{
	engfunc(EngFunc_PrecacheModel, w_model_m24)
	engfunc(EngFunc_PrecacheModel, v_model_m24)
	engfunc(EngFunc_PrecacheModel, p_model_m24)
	engfunc(EngFunc_PrecacheSound, g_soundexpl)
	spriteexpl = precache_model("sprites/zerogxplode.spr")
	precache_generic( "sprites/weapon_m24grenade.txt" )
	precache_generic( "sprites/640hud32.spr" )
	precache_generic( "sprites/640hud33.spr" )

	register_clcmd("weapon_m24grenade", "Hook_Select")
}

public Hook_Select(id)
{
    engclient_cmd(id, "weapon_hegrenade")
    return PLUGIN_HANDLED
}

public fw_AddToPlayer( iEnt, Player )
{
    if( pev_valid( iEnt ) && is_user_connected( Player ) )
    {
	if( has_bomb[ Player ] ){
	Sprite(Player)
	}
	else if(!has_bomb[Player]){
	Sprite1(Player)
	}
    }
} 

public Sprite(id)
{
  
    message_begin(MSG_ONE, gmsgWeaponList, {0,0,0}, id)
    write_string("weapon_m24grenade")
    write_byte(12)
    write_byte(1)
    write_byte(-1)
    write_byte(-1)
    write_byte(3)
    write_byte(1)
    write_byte(4)
    write_byte(24)
    message_end()

}
public Sprite1(id)
{
    
    message_begin(MSG_ONE, gmsgWeaponList, {0,0,0}, id)
    write_string("weapon_hegrenade")
    write_byte(12)
    write_byte(1)
    write_byte(-1)
    write_byte(-1)
    write_byte(3)
    write_byte(1)
    write_byte(4)
    write_byte(24)
    message_end()

}

public client_disconnect(id)
{
	has_bomb[id] = 0
}

public zp_extra_item_selected(player, itemid)
{
	if(itemid == item_id)
	{
		has_bomb[player] = 1	
		give_item(player,"weapon_hegrenade")
		Sprite(player)
	}
	
}

public fw_PlayerKilled(victim, attacker, shouldgib)
{
	has_bomb[victim] = 0	
}

public fw_ThinkGrenade(entity)
{	
	if(!pev_valid(entity))
		return HAM_IGNORED
		
	static Float:dmgtime	
	pev(entity, pev_dmgtime, dmgtime)
	
	if (dmgtime > get_gametime())
		return HAM_IGNORED	
	
	if(pev(entity, pev_flTimeStepSound) == NADE_TYPE_M24)
		heart_explode(entity)
	
	return HAM_SUPERCEDE
}

public fw_SetModel(ent, const model[])
{
	new id = pev(ent, pev_owner)
	
	if(!pev_valid(ent) || !equal(model[9], "hegrenade.mdl") || !has_bomb[id] || zp_get_user_zombie(id))
		return FMRES_IGNORED
	static classname[32]; pev(ent, pev_classname, classname, 31)
	if(equal(classname, "grenade"))
	{
		set_pcvar_num(get_cvar_pointer("zp_fire_grenades"),0)
		engfunc(EngFunc_SetModel, ent, w_model_m24)
		has_bomb[id] = 0
		set_pev(ent, pev_flTimeStepSound, NADE_TYPE_M24)
		return FMRES_SUPERCEDE
	}
	return FMRES_IGNORED
	
}


public heart_explode(ent)
{
	static Float:flOrigin [ 3 ]
	pev ( ent, pev_origin, flOrigin )
	new id = pev(ent, pev_owner)
	engfunc(EngFunc_EmitSound, ent, CHAN_WEAPON,g_soundexpl, 1.0, ATTN_NORM, 0, PITCH_NORM)
	engfunc(EngFunc_MessageBegin, MSG_PVS, SVC_TEMPENTITY, flOrigin, 0)
	write_byte(TE_SPRITE)
	engfunc(EngFunc_WriteCoord, flOrigin[0]+random_float(-5.0, 5.0))
	engfunc(EngFunc_WriteCoord, flOrigin[1]+random_float(-5.0, 5.0))
	engfunc(EngFunc_WriteCoord, flOrigin[2]+50.0)
	write_short(spriteexpl)
	write_byte(30)
	write_byte(200)
	message_end()	
	engfunc(EngFunc_MessageBegin, MSG_PAS, SVC_TEMPENTITY, flOrigin, 0)
	write_byte(TE_WORLDDECAL)
	engfunc(EngFunc_WriteCoord, flOrigin[0])
	engfunc(EngFunc_WriteCoord, flOrigin[1])
	engfunc(EngFunc_WriteCoord, flOrigin[2])
	write_byte(decal)
	message_end()
	for ( new victim = 1; victim < get_maxplayers(); victim++ )
	{
		new Float:flVictimOrigin [ 3 ]
		pev ( victim, pev_origin, flVictimOrigin )
		new Float:flDistance = get_distance_f ( flOrigin, flVictimOrigin )
		if ( flDistance <= RADIUS && zp_get_user_zombie(victim))
		{
			if (!is_user_alive(victim))
				continue;
			ExecuteHamB(Ham_TakeDamage,victim,0,id,250.0,DMG_BULLET | DMG_BLAST)
		}
	}
	set_pcvar_num(get_cvar_pointer("zp_fire_grenades"),1)
	remove_entity(ent)
}

public message_deathmsg(msg_id, msg_dest, id)
{
	new weapon[33], attacker , victim
	
	get_msg_arg_string(4, weapon , 32)
	
	attacker = get_msg_arg_int(1)
	victim = get_msg_arg_int(2)
	
	if(!is_user_alive(attacker) || get_user_weapon(attacker) != CSW_HEGRENADE|| !has_bomb[attacker] || attacker == victim)
		return PLUGIN_CONTINUE


	message_begin(MSG_BROADCAST, msg_id)
	write_byte(attacker)
	write_byte(victim)
	write_byte(0)
	write_string("hegrenade")
	message_end()

	return PLUGIN_CONTINUE
}

public replace_models(id)
{
	if (!is_user_alive(id))
		return
	
	if(get_user_weapon(id) == CSW_HEGRENADE && has_bomb[id])
	{
		set_pev(id, pev_viewmodel2, v_model_m24)
		set_pev(id, pev_weaponmodel2, p_model_m24)
		
	}
}

public message_cur_weapon(msg_id, msg_dest, msg_entity)
{
	replace_models(msg_entity)
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1049\\ f0\\ fs16 \n\\ par }
*/
