// Author: KernCore
// Modified by: GeckonCZ

#include "baseweapon"

namespace WeaponMp5sil
{

const string WEAPON_NAME			= "weapon_mp5sil";
const string AMMO_DROP_NAME			= "ammo_mp5sil";
const string AMMO_TYPE				= "9mm";

const int MP5SIL_MAX_CARRY    	= 250;
const int MP5SIL_DEFAULT_GIVE 	= 60;
const int MP5SIL_MAX_CLIP     	= 30;
const int MP5SIL_WEIGHT       	= 15;

enum OPBTMP5SILENCEDAnimation_e
{
	MP5SILENCED_LONGIDLE = 0,
	MP5SILENCED_IDLE,
	MP5SILENCED_RELOAD,
	MP5SILENCED_RELOAD_EMPTY,
	MP5SILENCED_DRAW,
	MP5SILENCED_SHOOT1,
	MP5SILENCED_SHOOT2,
	MP5SILENCED_SHOOT3
};

class CMp5sil : CBaseCustomWeapon
{
	string M3_W_MODEL = "models/scmod/weapons/opbt/mp5/w_9mmar.mdl";
	string M3_V_MODEL = "models/scmod/weapons/opbt/mp5/v_greasegun.mdl";
	string M3_P_MODEL = "models/scmod/weapons/opbt/mp5/p_9mmar.mdl";

	string M3_S_FIRE1 = "weapons/opbt/hks1_sl.wav";

	int m_iShell;
	
	void Spawn()
	{
		Precache();
		g_EntityFuncs.SetModel( self, M3_W_MODEL );
		
		self.m_iDefaultAmmo = MP5SIL_DEFAULT_GIVE;
		
		BaseClass.Spawn();
	}

	void Precache()
	{
		self.PrecacheCustomModels();
		g_Game.PrecacheModel( M3_W_MODEL );
		g_Game.PrecacheModel( M3_V_MODEL );
		g_Game.PrecacheModel( M3_P_MODEL );

		m_iShell = g_Game.PrecacheModel( "models/shell.mdl" );

		g_SoundSystem.PrecacheSound( M3_S_FIRE1 );
		g_SoundSystem.PrecacheSound( "weapons/opbt/mp5_clipinr.wav" );
		g_SoundSystem.PrecacheSound( "weapons/opbt/mp5_clipin.wav" );
		g_SoundSystem.PrecacheSound( "weapons/opbt/mp5_clipout.wav" );
		
		g_Game.PrecacheOther( AMMO_DROP_NAME );
	}

	bool GetItemInfo( ItemInfo& out info )
	{
		info.iMaxAmmo1	= MP5SIL_MAX_CARRY;
		info.iAmmo1Drop	= MP5SIL_MAX_CLIP;
		info.iMaxAmmo2	= -1;
		info.iAmmo2Drop	= -1;
		info.iMaxClip	= MP5SIL_MAX_CLIP;
		info.iSlot		= 2;
		info.iPosition	= 4;
		info.iFlags		= 0;
		info.iWeight	= MP5SIL_WEIGHT;
		
		return true;
	}

	bool AddToPlayer( CBasePlayer@ pPlayer )
	{
		if ( !BaseClass.AddToPlayer( pPlayer ) )
			return false;

		@m_pPlayer = pPlayer;
		
		NetworkMessage hunger6( MSG_ONE, NetworkMessages::WeapPickup, pPlayer.edict() );
		hunger6.WriteLong( self.m_iId );
		hunger6.End();
		
		return true;
	}

	bool PlayEmptySound()
	{
		if( self.m_bPlayEmptySound )
		{
			self.m_bPlayEmptySound = false;
			
			g_SoundSystem.EmitSoundDyn( m_pPlayer.edict(), CHAN_AUTO, "weapons/357_cock1.wav", 0.8, ATTN_NORM, 0, PITCH_NORM );
		}
		return false;
	}
	
	float WeaponTimeBase()
	{
		return g_Engine.time;
	}
	
	bool Deploy()
	{
		bool fResult = self.DefaultDeploy ( self.GetV_Model( M3_V_MODEL ), self.GetP_Model( M3_P_MODEL ), MP5SILENCED_DRAW, "mp5" );
		
		float deployTime = 0.67;
		self.m_flTimeWeaponIdle = self.m_flNextPrimaryAttack = self.m_flNextSecondaryAttack = g_Engine.time + deployTime;
		
		return fResult;
	}

	void Holster( int skipLocal = 0 ) 
	{
		self.m_fInReload = false;
		BaseClass.Holster( skipLocal );
	}

	void PrimaryAttack()
	{
		CBasePlayer@ pPlayer = m_pPlayer;
		
		if( pPlayer.pev.waterlevel == WATERLEVEL_HEAD || self.m_iClip <= 0 )
		{
			self.PlayEmptySound();
			self.m_flNextPrimaryAttack = WeaponTimeBase() + 0.15f;
			return;
		}
		
		self.m_flNextPrimaryAttack = self.m_flNextSecondaryAttack = WeaponTimeBase() + 0.09;
		
		pPlayer.m_iWeaponVolume = NORMAL_GUN_VOLUME;
		pPlayer.m_iWeaponFlash = BRIGHT_GUN_FLASH;
		
		--self.m_iClip;
		
		pPlayer.pev.effects |= EF_MUZZLEFLASH;
		pPlayer.SetAnimation( PLAYER_ATTACK1 );
		
		switch ( g_PlayerFuncs.SharedRandomLong( pPlayer.random_seed, 0, 2 ) )
		{
			case 0: 
			self.SendWeaponAnim( MP5SILENCED_SHOOT1, 0, 0 );
			pPlayer.pev.punchangle.y += Math.RandomFloat( -0.4, 0.2 );
			break;

			case 1:
			self.SendWeaponAnim( MP5SILENCED_SHOOT2, 0, 0 );
			pPlayer.pev.punchangle.y += -0.3;
			break;
			case 2:
			self.SendWeaponAnim( MP5SILENCED_SHOOT3, 0, 0 );
			pPlayer.pev.punchangle.y += 0.3;
			break;
		}
		
		g_SoundSystem.EmitSoundDyn( pPlayer.edict(), CHAN_WEAPON, M3_S_FIRE1, Math.RandomFloat( 0.95, 1.0 ), ATTN_NORM, 0, 93 + Math.RandomLong( 0, 0xf ) );
		
		Vector vecSrc	 = pPlayer.GetGunPosition();
		Vector vecAiming = pPlayer.GetAutoaimVector( AUTOAIM_5DEGREES );

		if( self.m_iClip == 0 && pPlayer.m_rgAmmo( self.m_iPrimaryAmmoType ) <= 0 )
			pPlayer.SetSuitUpdate( "!HEV_AMO0", false, 0 );

		//self.m_flNextPrimaryAttack = self.m_flNextPrimaryAttack + 0.15f;
		if( self.m_flNextPrimaryAttack < WeaponTimeBase() )
			self.m_flNextPrimaryAttack = WeaponTimeBase() + 0.15f;

		self.m_flTimeWeaponIdle = WeaponTimeBase() + Math.RandomFloat( 10, 15 );

		pPlayer.pev.punchangle.x -= 0.6;

		pPlayer.FireBullets( 1, vecSrc, vecAiming, VECTOR_CONE_3DEGREES, 8192, BULLET_PLAYER_MP5 );
		
		TraceResult tr;
		
		float x, y;
		
		g_Utility.GetCircularGaussianSpread( x, y );

		Vector vecDir = vecAiming + x * VECTOR_CONE_3DEGREES.x * g_Engine.v_right + y * VECTOR_CONE_3DEGREES.y * g_Engine.v_up;

		Vector vecEnd	= vecSrc + vecDir * 4096;

		g_Utility.TraceLine( vecSrc, vecEnd, dont_ignore_monsters, pPlayer.edict(), tr );

		Vector vecShellVelocity, vecShellOrigin;
		
		GetDefaultShellInfo( pPlayer, vecShellVelocity, vecShellOrigin, 25, 7, -7 );
		
		vecShellVelocity.y *= 1;
		
		g_EntityFuncs.EjectBrass( vecShellOrigin, vecShellVelocity, pPlayer.pev.angles[ 1 ], m_iShell, TE_BOUNCE_SHELL );
		
		if( tr.flFraction < 1.0 )
		{
			if( tr.pHit !is null )
			{
				CBaseEntity@ pHit = g_EntityFuncs.Instance( tr.pHit );
				
				if( pHit is null || pHit.IsBSPModel() == true )
				{
					g_WeaponFuncs.DecalGunshot( tr, BULLET_PLAYER_MP5 );
				}
			}
		}
	}

	void Reload()
	{
		if( self.m_iClip < MP5SIL_MAX_CLIP )
			BaseClass.Reload();

		self.m_iClip == 0 ? self.DefaultReload( MP5SIL_MAX_CLIP, MP5SILENCED_RELOAD_EMPTY, 2.18, 0 ) : self.DefaultReload( MP5SIL_MAX_CLIP, MP5SILENCED_RELOAD, 1.60, 0 );
	}
	
	void WeaponIdle()
	{
		self.ResetEmptySound();

		m_pPlayer.GetAutoaimVector( AUTOAIM_10DEGREES );
		
		if( self.m_flTimeWeaponIdle > WeaponTimeBase() )
			return;
		
		int iAnim;
		switch( g_PlayerFuncs.SharedRandomLong( m_pPlayer.random_seed,  0, 1 ) )
		{
			case 0:	iAnim = MP5SILENCED_LONGIDLE;
			break;
			
			case 1: iAnim = MP5SILENCED_IDLE;
			break;
		}

		self.SendWeaponAnim( iAnim, 0, 0 );

		self.m_flTimeWeaponIdle = WeaponTimeBase() + g_PlayerFuncs.SharedRandomFloat( m_pPlayer.random_seed, 5, 7 );
	}
}

class CMp5silMagazine : CBaseCustomAmmo
{
	string MP5SIL_MAG_MODEL = "models/w_mp5_clip.mdl";
	
	CMp5silMagazine()
	{
		m_strModel = MP5SIL_MAG_MODEL;
		m_strName = AMMO_TYPE;
		m_iAmount = MP5SIL_MAX_CLIP;
		m_iMax = MP5SIL_MAX_CARRY;
	}
}

void Register()
{
	g_CustomEntityFuncs.RegisterCustomEntity( "WeaponMp5sil::CMp5silMagazine", AMMO_DROP_NAME );
	g_CustomEntityFuncs.RegisterCustomEntity( "WeaponMp5sil::CMp5sil", WEAPON_NAME );
	g_ItemRegistry.RegisterWeapon( WEAPON_NAME, "opbt/weapons", AMMO_TYPE, "", AMMO_DROP_NAME, "" );
}

} // end of namespace
