/*	+---------------- 
	|	Fixes :
	|
	|	   1. Added Icons in Curweapon
	|	   2. Hooked the Reload event with Ham_Weapon_Reload
	|	   3. Added more sounds ( male & female )
		   4. Added a menu to choose the character ( Male,Female or Random )
		   5. Added /character , /reload commands to change the players Character
		   6. Fixed Radio Commands ( emit_sounds ) "Only Teammates can hear the Radio command.
		   7. Added Random character to player after joining the Server ( as default )
		   8. Fixed Flashing user icon in Radar.
		   9. Added on/off cvar ( amx_reload_sounds 1/0)
		  10. Fixed the Icon colors.
		
		    * Green = When user has enough Clip & Ammo
		    * Yello = When user has less Clip
		    * Red = when user has no Ammo & clip
		    
		   11. Added Own Include file
		   12. Added Reloadbar
		   13. Added more Cvars to turning the functions on/off					|
		   14. Removed Include file								|	
		   15. Changed sounds directory to sounds/radio/					|
													|
											----------------+
*/
/*	+---------------- 
	|	L4D RELOADING SOUNDS2.9
	|
	|	   Credits :
	|	   
	|	   ConnorMcLeod ( see:leksys)
		   
		   THE IDEA:
		   
		   Give more support to the user which needs backup while Reloading the weapon.
		   
		   DESCRIPTION :
		  
		   This plugin playes Reloadingsounds while user reloads his weapon and adds more effects
		   in Gameplay. 
		   
			1. Reload-sounds while reloading
			2. ReloadBar while reloading
			3. User can choose between Male and Female character
			4. Weapon icons will show users weapon status
			5. Makes the usersicon in Radar flashing.					|
			6. Sends a Chat message to all teammates.					|	
			7. Supports Location of player in Counter-Strike:Condition Zero			|
													|
											----------------+
*/
/*	Copyright of Reload Status Bar by ConnorMcLeod :


	Copyright © 2009, ConnorMcLeod

	Reload Status Bar is free software;
	you can redistribute it and/or modify it under the terms of the
	GNU General Public License as published by the Free Software Foundation.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Reload Status Bar; if not, write to the
	Free Software Foundation, Inc., 59 Temple Place - Suite 330,
	Boston, MA 02111-1307, USA.
*/


#include <amxmodx>
#include <cstrike>
#include <fakemeta>
#include <hamsandwich>


#define MAX_PLAYERS	32

#define HUD_PRINTRADIO	5

#define XTRA_OFS_WEAPON	4
#define m_pPlayer	41
#define m_fInReload	54

#define XTRA_OFS_PLAYER	5
#define m_flNextAttack	83

new weapons[][]=
{   
	"weapon_p228", 
	"weapon_scout", 
	"weapon_xm1014", 
	"weapon_mac10", 
	"weapon_aug", 
	"weapon_elite", 
	"weapon_fiveseven", 
	"weapon_ump45",
	"weapon_sg550", 
	"weapon_galil", 
	"weapon_famas", 
	"weapon_usp", 
	"weapon_glock18", 
	"weapon_awp",
	"weapon_mp5navy", 
	"weapon_m249",
	"weapon_m3", 
	"weapon_m4a1", 
	"weapon_tmp", 
	"weapon_g3sg1", 
	"weapon_deagle", 
	"weapon_sg552", 
	"weapon_ak47", 
	"weapon_p90"
}

new g_iBarTime2,g_iSendAudio, g_iTextMsg
const NOCLIP_WPN_BS = ((1<<2)|(1<<CSW_HEGRENADE)|(1<<CSW_SMOKEGRENADE)|(1<<CSW_FLASHBANG)|(1<<CSW_KNIFE)|(1<<CSW_C4))
const SHOTGUNS_BS = ((1<<CSW_M3)|(1<<CSW_XM1014))
new g_pCvarRadioDelay,reload_bar,audio_cvar,iconstatus
new Float:g_flNextRadioGameTime[MAX_PLAYERS+1]
new g_szLocation[MAX_PLAYERS+1][32]
new bool:g_bLocation
static sprite[33][16]
new users_character[33]
new ask_character,text_messages,show_icons

new male_sounds[][] =
{
	"radio/reloadm_01.wav",
	"radio/reloadm_02.wav",
	"radio/reloadm_03.wav",
	"radio/reloadm_04.wav",
	"radio/reloadm_05.wav",
	"radio/reloadm_06.wav",
	"radio/reloadm_07.wav",
	"radio/reloadm_08.wav",
	"radio/reloadm_09.wav"
}
new female_sounds[][] =
{
	"radio/reloadfm_01.wav",
	"radio/reloadfm_02.wav",
	"radio/reloadfm_03.wav",
	"radio/reloadfm_04.wav"
}
public plugin_init()
{
	register_plugin("L4D Reloading Sounds", "2.9", "One/Connor")
	register_cvar("Reload_Sounds_L4D", "2.9", FCVAR_SERVER)
	/* Delay between reloads*/
	g_pCvarRadioDelay = register_cvar("reload_radio_delay", "5")
	
	/* Reload Hook event*/
	for(new i = 0 ; i< sizeof weapons; i++)
	{
			RegisterHam(Ham_Weapon_Reload, weapons[i], "Weapon_Reload", 1)
			RegisterHam(Ham_Item_Holster, weapons[i], "Item_Holster")

	}
	register_event("CurWeapon", "ev_CheckWeapon", "be", "1=1")
	register_clcmd( "say","Say_Command")
	register_clcmd( "say_team","Say_Command")
	register_event("ResetHUD","resethud","be")
	new szModName[7]
	get_modname(szModName, charsmax(szModName))
	if(equal(szModName, "czero"))
	{
		register_event("Location", "Event_Location", "be")
	}
	reload_bar = register_cvar("amx_reload_bar","1")		//Enables Reloadbar
	audio_cvar = register_cvar("amx_reload_sound","1")		//Enables Reloadsounds
	g_iBarTime2 = get_user_msgid("BarTime2")
	g_iSendAudio = get_user_msgid("SendAudio")
	g_iTextMsg = get_user_msgid("TextMsg")
	iconstatus = get_user_msgid("StatusIcon")
	ask_character = register_cvar("amx_character_menu","1")		//Enables the Charactermenu
	text_messages = register_cvar("amx_radiomessages","1")		//Enables the Radio texts
	show_icons = register_cvar("amx_show_icons","1")		//Enables the Icons
}
public ev_CheckWeapon(id)
{
	if(get_pcvar_num(show_icons))
		return PLUGIN_HANDLED
		
	if(!is_user_alive(id))
	{
		message_begin(MSG_ONE,iconstatus,{0,0,0},id)
		write_byte(0)
		write_string(sprite[id])
		message_end()
		return PLUGIN_HANDLED
	}
/* 
			+-------------------------------
			|	Remove icon to add new one    |
					      ----------------+
*/
	
	message_begin(MSG_ONE,iconstatus,{0,0,0},id)
	write_byte(0)
	write_string(sprite[id])
	message_end()
		
	new  clip, ammo
	static  iwpn
	iwpn = get_user_weapon(id, clip, ammo)

	
	switch(iwpn) 
	{
		case CSW_P228:         sprite[id]="d_p228"
		case CSW_SCOUT:         sprite[id]="d_scout"
		case CSW_HEGRENADE:         sprite[id]="d_grenade"
		case CSW_XM1014:         sprite[id]="d_xm1014"
		case CSW_MAC10:         sprite[id]="d_mac10"
		case CSW_AUG:         sprite[id]="d_aug"
		case CSW_SMOKEGRENADE:     sprite[id]="d_flashbang"
		case CSW_ELITE:         sprite[id]="d_elite"
		case CSW_FIVESEVEN:         sprite[id]="d_fiveseven"
		case CSW_UMP45:         sprite[id]="d_ump45"
		case CSW_SG550:         sprite[id]="d_sg550"
		case CSW_GALIL:         sprite[id]="d_galil"
		case CSW_FAMAS:         sprite[id]="d_famas"
		case CSW_USP:         sprite[id]="d_usp"
		case CSW_MP5NAVY:         sprite[id]="d_mp5navy"
		case CSW_M249:         sprite[id]="d_m249"
		case CSW_M3:             sprite[id]="d_m3"
		case CSW_M4A1:         sprite[id]="d_m4a1"
		case CSW_TMP:         sprite[id]="d_tmp"
		case CSW_G3SG1:         sprite[id]="d_g3sg1"
		case CSW_FLASHBANG:         sprite[id]="d_flashbang"
		case CSW_DEAGLE:         sprite[id]="d_deagle"
		case CSW_SG552:         sprite[id]="d_sg552"
		case CSW_AK47:         sprite[id]="d_ak47"
		case CSW_KNIFE:         sprite[id]="d_knife"
		case CSW_P90:         sprite[id]="d_p90"
		case CSW_GLOCK18:         sprite[id]="d_glock18"
		case CSW_AWP:         sprite[id]="d_awp"
	}   
/* 
			+-------------------------------
			|	 check the muni to add a color to the icon
			|	if user has 0 muni, set the color on RED
				if user has ammo 0, set the color YELLOW						|
				else just add the GREEN ICON			|
								----------------+
*/
	new icon_color[3]
	
	if ((ammo == 0 && clip == 0))
	{
		icon_color = {255, 0, 0}		//RED
	}
	else if ( ammo==0 && iwpn!=CSW_KNIFE)
	{
		icon_color = {255, 160, 0}	//YELLOW
	}
	else 
	{
		icon_color = {0, 160, 0}		//GREEN
	}
	message_begin(MSG_ONE,iconstatus,{0,0,0},id)
	write_byte(1)
	write_string(sprite[id])
	write_byte(icon_color[0])
	write_byte(icon_color[1])
	write_byte(icon_color[2])
	message_end()
	return PLUGIN_HANDLED
}
public Say_Command(id)
{
	if(!get_pcvar_num(ask_character))
		return PLUGIN_HANDLED
		
	static say_args[64]
	read_args(say_args, charsmax(say_args))
	remove_quotes(say_args)
	
	if(say_args[0] != '/')
		return PLUGIN_CONTINUE
		
	if(equali(say_args, "/character") || equali(say_args, "/reload"))
	{
		show_character_menu(id)
	}
	return PLUGIN_CONTINUE
}
public client_putinserver(id)
{
	users_character[id] = -1		//Random
	return PLUGIN_HANDLED
}
public resethud(id)
{
	if(!get_pcvar_num(ask_character))
		return PLUGIN_HANDLED
		
	if(users_character[id] == -1)
	{
		set_task(5.0,"show_character_menu",id)
	}
	return PLUGIN_HANDLED
}
public show_character_menu(id)
{
	new menu = menu_create("\rChoose your Character:", "character_handle")
	menu_additem(menu, "\wMale", "1", 0)
	menu_additem(menu, "\wFemale", "2", 0)
	menu_additem(menu, "\wRandom","3",0)
	menu_setprop(menu, MPROP_EXIT, MEXIT_ALL)
	menu_display(id, menu, 0)
	return PLUGIN_HANDLED

}
public character_handle(id,menu,item)
{
	if( item == MENU_EXIT )
	{
		menu_destroy(menu)
		return PLUGIN_HANDLED
	}
	new data[6], iName[64]
	new access, callback
	menu_item_getinfo(menu, item, access, data,5, iName, 63, callback)
	
	new key = str_to_num(data)
	switch(key)
	{
		case 1:
		{
			users_character[id] = 1		//Male
		}
		case 2:
		{
			users_character[id] = 2		//Female
		}
		case 3:
		{
			users_character[id] = 0		//Random
		}
	}
	
	menu_destroy(menu)
	return PLUGIN_HANDLED
}
public plugin_precache()
{
	for( new i; i < sizeof male_sounds; i++ )
	{
		precache_sound(male_sounds[i])
	}
	for(new i;i < sizeof female_sounds;i++)
	{
		precache_sound(female_sounds[i])
	}
}
public Event_Location( id )
{
	g_bLocation = true

	if( read_data(1) == id )
	{
		read_data(2, g_szLocation[id], charsmax(g_szLocation[]))
	}
}
Reload_Inform( id )
{
	new Float:flGameTime = get_gametime()
	if( flGameTime > g_flNextRadioGameTime[id] )
	{
		static szGameRadio[] = "#Game_radio"
		static szGameRadioLoc[] = "#Game_radio_location"

		g_flNextRadioGameTime[id] + get_pcvar_float(g_pCvarRadioDelay)

		new iPlayers[MAX_PLAYERS], iNum, iPlayer
		new CsTeams:iTeam = cs_get_user_team(id)

		new male = random_num( 0, sizeof male_sounds-1 )
		new female = random_num(0,sizeof female_sounds-1)
		new random = random_num(0,1)


		new szId[4]
		num_to_str(id, szId, charsmax(szId))

		new szName[32]
		get_user_name(id, szName, charsmax(szName))

		new szLocation[32]
		szLocation = g_szLocation[id]

		get_players(iPlayers, iNum, "ch")

		for(new i; i<iNum; i++)
		{
			iPlayer = iPlayers[i]
			if( iPlayer == id || cs_get_user_team(iPlayer) == iTeam )
			{
				if(get_pcvar_num(audio_cvar))
				{
					if(users_character[id] == 1)
					{
						Send_SendAudio(id, iPlayer, male_sounds[male])
					}
					else if(users_character[id] == 2)
					{
						Send_SendAudio(id, iPlayer, female_sounds[female])
					}
					else if(users_character[id] == 0 || users_character[id] == -1)
					{
						if(random)
						{
							Send_SendAudio(id, iPlayer, male_sounds[male])
						}
						else
						{
							Send_SendAudio(id, iPlayer, female_sounds[female])
						}
					}
				}
				if(get_pcvar_num(text_messages))
				{
					if( g_bLocation )
					{
						Util_ClientPrint(iPlayer, HUD_PRINTRADIO, szId, szGameRadioLoc, szName, szLocation[1], "Reloading !");
					}
					else
					{
						Util_ClientPrint(iPlayer, HUD_PRINTRADIO, szId, szGameRadio, szName, "Reloading !");
					}
				}
			}
		}
	}
}
public Weapon_Reload( iEnt )
{
	if( get_pdata_int(iEnt, m_fInReload, XTRA_OFS_WEAPON) )
	{
		new id = get_pdata_cbase(iEnt, m_pPlayer, XTRA_OFS_WEAPON)
		new Float:flNextAttack = get_pdata_float(id, m_flNextAttack, XTRA_OFS_PLAYER)
		new iSeconds = floatround(flNextAttack, floatround_ceil)
		if(get_pcvar_num(reload_bar))
		{
			Make_BarTime2(id, iSeconds, 100 - floatround( (flNextAttack/iSeconds) * 100 ))
		}
		Reload_Inform(id)
	}
}
public Item_Holster( iEnt )
{
	if( get_pdata_int(iEnt, m_fInReload, 4) )
	{
		Make_BarTime2(get_pdata_cbase(iEnt, m_pPlayer, 4), 0, 0)
	}
}
/* Include Parts */
/**
 * Make the BarMessage
 */
Make_BarTime2(const id, const iSeconds, const iPercent)
{
	message_begin(MSG_ONE_UNRELIABLE, g_iBarTime2, _, id)
	write_short(iSeconds)
	write_short(iPercent)
	message_end()
}
/**
 * Make the Radio commands which can only Hear from Teammates
 */
Send_SendAudio(const iSender, const iReceiver, const szSound[], const iPitch = 100)
{
	message_begin(MSG_ONE_UNRELIABLE, g_iSendAudio, .player=iReceiver)
	write_byte(iSender)
	write_string(szSound)
	write_short(iPitch)
	message_end()
}
/**
 * Make the Messages
 */
Util_ClientPrint(id, iMsgDest, szMessage[], szSubMessage1[] = "", szSubMessage2[] = "", szSubMessage3[] = "", szSubMessage4[] = "")
{
	message_begin(id ? MSG_ONE_UNRELIABLE : MSG_BROADCAST, g_iTextMsg, .player=id)
	{
		write_byte(iMsgDest)
		write_string(szMessage)
		if( szSubMessage1[0] )
		{
			write_string(szSubMessage1)
		}
		if( szSubMessage2[0] )
		{
			write_string(szSubMessage2)
		}
		if( szSubMessage3[0] )
		{
			write_string(szSubMessage3)
		}
		if( szSubMessage4[0] )
		{
			write_string(szSubMessage4)
		}
	}
	message_end()
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1031\\ f0\\ fs16 \n\\ par }
*/
