namespace cnpc_agrunt
{

const uint g_MaxAmmoPri		= 999;
const string sWeaponName	= "weapon_agrunt";
const float CD_HORNET		= 1.9;
const float CD_MELEE		= 1.0;

const float DMG_MELEE		= 50.0;
const float MELEE_RANGE		= 110.0;

const float HORNET_REFIRE	= 0.2;
const float CNPC_IDLESOUND	= 10.0;

const array<string> pSqknestSounds = 
{
	"debris/flesh1.wav",
	"debris/flesh2.wav",
	"debris/flesh3.wav",
	"debris/flesh4.wav",
	"debris/flesh5.wav"
};

const array<string> pAttackHitSounds = 
{
	"zombie/claw_strike1.wav",
	"zombie/claw_strike2.wav",
	"zombie/claw_strike3.wav"
};

const array<string> pAttackMissSounds = 
{
	"zombie/claw_miss1.wav",
	"zombie/claw_miss2.wav"
};

const array<string> pPainSounds = 
{
	"agrunt/ag_pain1.wav",
	"agrunt/ag_pain2.wav",
	"agrunt/ag_pain3.wav",
	"agrunt/ag_pain4.wav",
	"agrunt/ag_pain5.wav"
};

const array<string> pDieSounds = 
{
	"agrunt/ag_die1.wav",
	"agrunt/ag_die4.wav",
	"agrunt/ag_die5.wav"
};

const array<string> pIdleSounds = 
{
	"agrunt/ag_idle1.wav",
	"agrunt/ag_idle2.wav",
	"agrunt/ag_idle3.wav",
	"agrunt/ag_idle4.wav"
};

const array<string> pAttackSounds = 
{
	"agrunt/ag_attack1.wav",
	"agrunt/ag_attack2.wav",
	"agrunt/ag_attack3.wav"
};

enum states_e
{
	STATE_IDLE = 0,
	STATE_WALK,
	STATE_RUN,
	STATE_DEATH,
	STATE_ATTACK_MELEE,
	STATE_ATTACK_HORNET
};

class weapon_agrunt : CBaseDriveWeapon
{
	float m_flRechargeTime;
	float m_flNextIdleSound;
	float m_fNextRegenTick;
	float m_fNextHealthCheck;
	float m_flNextShortBurst1;
	float m_flNextShortBurst2;
	private bool m_bShotBlocked;
	private int m_iLastWord;
	private int m_iShootStage;
	private int m_iShootStage2;
	private int m_iState;
	private int m_iAutoDeploy;
	private int m_iAgruntMuzzleFlash;
	private int m_iRandomAttack;
	private float m_flStopHornetAttack;
	private float m_flStopSnarkThrow;
	private float m_flNextSpeakTime;
	private float m_flNextWordTime;

	void Spawn()
	{
		Precache();

		self.m_iDefaultAmmo = 999;

		g_EntityFuncs.SetModel( self, "models/cnpc/agrunt.mdl" );
		g_EntityFuncs.DispatchKeyValue( self.edict(), "renderamt", 128 );
		g_EntityFuncs.DispatchKeyValue( self.edict(), "rendermode", 2 );
		g_EntityFuncs.DispatchKeyValue( self.edict(), "renderfx", 15 );

		self.pev.sequence = self.LookupSequence("idle1");
		self.pev.frame = 0;
		self.ResetSequenceInfo();

		m_iState = STATE_IDLE;
		m_iRandomAttack = 0;
		m_iAutoDeploy = 1;
		m_iLastWord = 0;
		m_iShootStage = 0;
		m_iShootStage2 = 0;
		m_flNextShortBurst1 = 0;
		m_flNextShortBurst2 = 0;
		m_flNextSpeakTime = m_flNextWordTime = g_Engine.time + 10 + Math.RandomFloat(0, 10);
		m_flNextIdleSound = g_Engine.time + CNPC_IDLESOUND;

		self.FallInit();
	}

	void Precache()
	{
		self.PrecacheCustomModels();
		g_Game.PrecacheGeneric( "important_files_to_install.zip" );
		//g_Game.PrecacheGeneric( "sprites/controlnpc/weapon_agrunt.txt" );
		g_Game.PrecacheModel( "sprites/controlnpc/crosshairs.spr" );
		g_Game.PrecacheModel( "models/cnpc/agrunt.mdl" );
		m_iAgruntMuzzleFlash = g_Game.PrecacheModel( "sprites/muz4.spr" );

		for( uint i = 0; i < pAttackHitSounds.length(); i++ )
			g_SoundSystem.PrecacheSound( pSqknestSounds[i] );
			g_SoundSystem.PrecacheSound( "debris/bustflesh1.wav" );
			g_SoundSystem.PrecacheSound( "debris/bustflesh2.wav" );
			g_SoundSystem.PrecacheSound( "debris/flesh6.wav" );
			g_SoundSystem.PrecacheSound( "debris/flesh7.wav" );

		for( uint i = 0; i < pAttackHitSounds.length(); i++ )
			g_SoundSystem.PrecacheSound( pAttackHitSounds[i] );

		for( uint i = 0; i < pAttackMissSounds.length(); i++ )
			g_SoundSystem.PrecacheSound( pAttackMissSounds[i] );

		for( uint i = 0; i < pPainSounds.length(); i++ )
			g_SoundSystem.PrecacheSound( pPainSounds[i] );

		for( uint i = 0; i < pDieSounds.length(); i++ )
			g_SoundSystem.PrecacheSound( pDieSounds[i] );

		for( uint i = 0; i < pDieSounds.length(); i++ )
			g_SoundSystem.PrecacheSound( pIdleSounds[i] );
			g_SoundSystem.PrecacheSound( "agrunt/ag_idle4.wav" );

		for( uint i = 0; i < pDieSounds.length(); i++ )
			g_SoundSystem.PrecacheSound( pAttackSounds[i] );
	}

	bool GetItemInfo( ItemInfo& out info )
	{
		info.iMaxAmmo1	= 999;
		info.iMaxClip		= WEAPON_NOCLIP;
		info.iSlot		= 1 - 1;
		info.iPosition		= 1 - 1;
		info.iFlags 			= ITEM_FLAG_SELECTONEMPTY; //to prevent monster from being despawned if out of ammo TODO
		info.iWeight			= 0; //-1 ??

		return true;
	}

	bool AddToPlayer( CBasePlayer@ pPlayer )
	{
		if( !BaseClass.AddToPlayer(pPlayer) )
			return false;

		@m_pPlayer = pPlayer;

		/*NetworkMessage m1( MSG_ONE, NetworkMessages::WeapPickup, pPlayer.edict() );
			m1.WriteLong( g_ItemRegistry.GetIdForName(sWeaponName) );
		m1.End();*/

		m_pPlayer.SwitchWeapon(self);

		return true;
	}

	bool Deploy()
	{
		spawn_driveent();
		self.m_flNextPrimaryAttack = self.m_flNextSecondaryAttack = g_Engine.time + 0.1;

		return self.DefaultDeploy( "", "", 0, "" );
	}

	bool CanHolster()
	{
		return false;
	}

	void DoIdleSound()
	{
		if( m_iState != STATE_IDLE and m_iState != STATE_WALK ) return;

		if( m_flNextIdleSound > g_Engine.time ) return;

		if( Math.RandomLong(0, 99) == 0 )
			IdleSound();
	}

	void Holster( int skipLocal = 0 )
	{
		if( m_pDriveEnt !is null )
			@m_pDriveEnt.pev.owner = null;

		ResetPlayer();

		BaseClass.Holster( skipLocal );
	}

	void PrimaryAttack()
	{
		TraceResult tr;
		Math.MakeVectors( m_pPlayer.pev.v_angle );
		g_Utility.TraceHull( m_pPlayer.pev.origin, m_pPlayer.pev.origin + g_Engine.v_forward * 38, dont_ignore_monsters, head_hull, m_pPlayer.edict(), tr );

		if( m_pDriveEnt !is null and m_iState == STATE_RUN )
		{
			self.m_flNextPrimaryAttack = g_Engine.time + 0.1;
		}
		
		if( m_pDriveEnt !is null and tr.flFraction != 1.0 and m_pPlayer.pev.velocity.Length2D() == 0.0 ) //and (m_pPlayer.pev.flags & FL_FAKECLIENT) != 0 )
		{
			g_EngineFuncs.ClientPrintf( m_pPlayer, print_center, "Can't shoot here.\n Something's in the way.\n" );
			self.m_flNextPrimaryAttack = g_Engine.time + 0.1;
			DoIdleAnimation();
			m_bShotBlocked = true;
			return;
		}
		else if( m_pDriveEnt !is null and tr.flFraction == 1.0 and m_pPlayer.pev.velocity.Length2D() == 0.0 ) //and (m_pPlayer.pev.flags & FL_FAKECLIENT) != 0 )
		{
			m_bShotBlocked = false;
		}

		if( m_pDriveEnt !is null )
		{
			if( m_iState != STATE_DEATH and !m_bShotBlocked )
			{
				switch( Math.RandomLong(1, 4) )
				{
					case 1:
					if( m_flNextShortBurst1 < g_Engine.time )
					{
						m_flNextShortBurst1 = g_Engine.time + 3.0;
						m_iShootStage = 0;
						m_iShootStage2 = 0;
						m_iState = STATE_ATTACK_HORNET;
						m_pPlayer.SetMaxSpeedOverride( 0 );
						m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("quickshoot");
						m_pDriveEnt.pev.frame = 0;
						m_pDriveEnt.ResetSequenceInfo();

						SetThink( ThinkFunction(this.HornetAttackThink) );
						pev.nextthink = g_Engine.time + 0.3;
						m_flStopHornetAttack = g_Engine.time + 0.6;

						self.m_flNextPrimaryAttack = g_Engine.time + 0.9;
						self.m_flNextSecondaryAttack = g_Engine.time + 0.25;
						self.m_flTimeWeaponIdle = g_Engine.time + 1.0;
					}
					break;
					case 2:
					if( m_flNextShortBurst2 < g_Engine.time )
					{
						m_flNextShortBurst2 = g_Engine.time + 3.0;
						m_iShootStage = 0;
						m_iShootStage2 = 0;
						m_iState = STATE_ATTACK_HORNET;
						m_pPlayer.SetMaxSpeedOverride( 0 );
						m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("quickshoot");
						m_pDriveEnt.pev.frame = 0;
						m_pDriveEnt.ResetSequenceInfo();

						SetThink( ThinkFunction(this.HornetAttackThink) );
						pev.nextthink = g_Engine.time + 0.3;
						m_flStopHornetAttack = g_Engine.time + 0.6;

						self.m_flNextPrimaryAttack = g_Engine.time + 0.9;
						self.m_flNextSecondaryAttack = g_Engine.time + 0.25;
						self.m_flTimeWeaponIdle = g_Engine.time + 1.0;
					}
					break;
					case 3:
						m_iShootStage = 0;
						m_iShootStage2 = 0;
						m_iState = STATE_ATTACK_HORNET;
						m_pPlayer.SetMaxSpeedOverride( 0 );
						m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("hornetattack");
						m_pDriveEnt.pev.frame = 0;
						m_pDriveEnt.ResetSequenceInfo();

						SetThink( ThinkFunction(this.HornetBurstThink) );
						pev.nextthink = g_Engine.time + 0.01;

						//SetThink( ThinkFunction(this.HornetAttackThink) );
						//pev.nextthink = g_Engine.time + 0.3;
						//m_flStopHornetAttack = g_Engine.time + 1.2;

						self.m_flNextPrimaryAttack = g_Engine.time + 1.5;
						self.m_flNextSecondaryAttack = g_Engine.time + 0.25;
						self.m_flTimeWeaponIdle = g_Engine.time + 1.5;
					break;
					case 4:
						m_iShootStage = 0;
						m_iShootStage2 = 0;
						m_iState = STATE_ATTACK_HORNET;
						m_pPlayer.SetMaxSpeedOverride( 0 );
						m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("longshoot");
						m_pDriveEnt.pev.frame = 0;
						m_pDriveEnt.ResetSequenceInfo();

						//SetThink( ThinkFunction(this.HornetLongBurstThink) );
						//pev.nextthink = g_Engine.time + 0.01;

						SetThink( ThinkFunction(this.HornetAttackThink) );
						pev.nextthink = g_Engine.time + 0.3;
						m_flStopHornetAttack = g_Engine.time + 1.6;

						self.m_flNextPrimaryAttack = g_Engine.time + CD_HORNET;
						self.m_flNextSecondaryAttack = g_Engine.time + 0.25;
						self.m_flTimeWeaponIdle = g_Engine.time + CD_HORNET;
					break;
				}
			}
		}
		else
		{
			spawn_driveent();
			self.m_flNextPrimaryAttack = self.m_flNextSecondaryAttack = self.m_flTimeWeaponIdle = g_Engine.time + 0.5;

			return;
		}
	}

	void SecondaryAttack()
	{
		if( m_iState == STATE_RUN )
		{
			//self.m_flNextSecondaryAttack = g_Engine.time + 0.1;
			return;
		}

		if( m_pDriveEnt !is null and m_iState != STATE_DEATH )
		{
			m_iState = STATE_ATTACK_MELEE;
			m_pPlayer.SetMaxSpeedOverride( 0 );

			m_iRandomAttack = Math.RandomLong(2, 3);
			PlayMeleeAttackSound();

			switch( m_iRandomAttack )
			{
				case 2:	m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("mattack2"); pev.nextthink = g_Engine.time + 0.5; break;
				case 3:	m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("mattack3"); pev.nextthink = g_Engine.time + 0.4; break;
			}

			m_pDriveEnt.pev.frame = 0;
			m_pDriveEnt.ResetSequenceInfo();

			SetThink( ThinkFunction(this.MeleeAttackThink) );
		}

		self.m_flNextPrimaryAttack = g_Engine.time + 1.0;
		self.m_flNextSecondaryAttack = g_Engine.time + CD_MELEE;
		self.m_flTimeWeaponIdle = g_Engine.time + CD_MELEE;
	}

	void TertiaryAttack()
	{
		Vector vecOrigin, vecOrigin2, vecOrigin3, vecOrigin4, vecOrigin5; 
		//m_pDriveEnt.GetAttachment( 0, vecOrigin, void );

		entvars_t@ pevOwner;
		if( self.pev.owner !is null )
			@pevOwner = @self.pev.owner.vars;
		else
			@pevOwner = self.pev;

		vecOrigin = m_pPlayer.pev.origin + g_Engine.v_right * -24;

		if( m_iState == STATE_RUN )
		{
			//self.m_flNextSecondaryAttack = g_Engine.time + 0.1;
			return;
		}

		if( m_pDriveEnt !is null and m_iState != STATE_DEATH )
		{
			m_iState = STATE_ATTACK_MELEE;
			m_pPlayer.SetMaxSpeedOverride( 0 );

			m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("mattack3");
			m_pDriveEnt.pev.frame = 0;
			m_pDriveEnt.ResetSequenceInfo();

			SetThink( ThinkFunction(this.ThrowSnarkThink) );
			pev.nextthink = g_Engine.time + 0.1;
			m_flStopSnarkThrow = g_Engine.time + 0.75;

			//CBaseEntity@ pSqueak = g_EntityFuncs.Create( "monster_hlsnark", vecOrigin, m_pPlayer.pev.v_angle, false, m_pPlayer.edict() );
			//pSqueak.pev.velocity = g_Engine.v_forward * 200 + g_Engine.v_up * 300;
			//CBaseEntity@ pOwner = g_EntityFuncs.Instance( pevOwner );
			//@pSqueak.pev.owner = @pOwner.edict();
			//pSqueak.SetPlayerAlly( !self.IsPlayerAlly() );
		}

		m_flNextIdleSound = g_Engine.time + 4.0;
		m_fNextRegenTick = g_Engine.time + 6.0;
		self.m_flNextPrimaryAttack = g_Engine.time + 1.0;
		self.m_flNextSecondaryAttack = g_Engine.time + CD_MELEE;
		self.m_flNextTertiaryAttack = g_Engine.time + 10.0;
		self.m_flTimeWeaponIdle = g_Engine.time + CD_MELEE;
	}

	void Reload()
	{
	}

	void WeaponIdle()
	{
		if( self.m_flTimeWeaponIdle > g_Engine.time )
			return;

		DoIdleAnimation();

		self.m_flTimeWeaponIdle = g_Engine.time + 1.0;
	}

	void ItemPreFrame()
	{
		if( m_pDriveEnt !is null )
		{
			m_pPlayer.pev.friction = 2;

			DoIdleSound();
			CheckMaxHealth();

			//if( m_pPlayer.pev.button & (IN_BACK|IN_MOVELEFT|IN_MOVERIGHT) != 0 )
				//m_pPlayer.SetMaxSpeedOverride( 0 );
			//else if( m_pPlayer.pev.button & IN_FORWARD != 0 )
			if ( m_pPlayer.pev.button & (IN_FORWARD|IN_BACK|IN_MOVELEFT|IN_MOVERIGHT) != 0 )
			{
				m_pPlayer.SetMaxSpeedOverride( 270 );
				DoMovementAnimation();
				self.m_flTimeWeaponIdle = g_Engine.time + 0.1;
			}
			else if( (m_pPlayer.pev.flags & FL_FAKECLIENT) != 0 and m_pPlayer.pev.velocity.Length2D() > 0.0 and m_iState != STATE_ATTACK_HORNET and m_iState != STATE_ATTACK_MELEE )
			{
				m_pPlayer.SetMaxSpeedOverride( 270 );
				DoMovementAnimation();
				self.m_flTimeWeaponIdle = g_Engine.time + 0.1;
			}

			if( m_pPlayer.pev.velocity.Length() <= 0 and m_iState != STATE_ATTACK_MELEE and m_iState != STATE_ATTACK_HORNET )
				DoIdleAnimation();

			if( m_iState == STATE_ATTACK_HORNET )
			{
				Vector angDir = m_pPlayer.pev.v_angle;

				if( angDir.x > 180 )
					angDir.x = angDir.x - 360;

				m_pDriveEnt.SetBlending( 0, -angDir.x );

				m_pPlayer.SetMaxSpeedOverride( 0 );
				m_pPlayer.SetViewMode( ViewMode_ThirdPerson );
			}

			if( m_iState == STATE_IDLE )
			{
				RegenerateHealth();
				m_pPlayer.pev.view_ofs = Vector( 0, 0, 50 );
				m_pPlayer.SetViewMode( ViewMode_ThirdPerson );
			}

			if( m_iState == STATE_WALK )
			{
				m_pPlayer.pev.view_ofs = Vector( 0, 0, 50 );
				m_pPlayer.SetViewMode( ViewMode_ThirdPerson );
			}

			if( m_iState == STATE_RUN )
			{
				m_pPlayer.pev.view_ofs = Vector( 0, 0, 50 );
				m_pPlayer.SetViewMode( ViewMode_ThirdPerson );
			}
		}
	}

	void HornetBurstThink()
	{
		if( m_pPlayer is null or m_pDriveEnt is null or m_iState != STATE_ATTACK_HORNET or m_bShotBlocked )
		{
			SetThink( null );
			m_iShootStage = 0;
			return;
		}

		if( m_iState == STATE_ATTACK_HORNET and m_pDriveEnt.pev.sequence == m_pDriveEnt.LookupSequence("hornetattack") )
		{
			switch( GetFrame(16) )
			{
			    case 3: { if( m_iShootStage == 0 ) { ShootHornet(); m_iShootStage++; } break; }
			    case 5: { if( m_iShootStage == 1 ) { ShootHornet(); m_iShootStage++; } break; }
			    case 7: { if( m_iShootStage == 2 ) { ShootHornet(); m_iShootStage++; } break; }
			    case 8: { if( m_iShootStage == 3 ) { ShootHornet(); m_iShootStage++; } break; }
			    case 11: { if( m_iShootStage == 4 ) { ShootHornet(); m_iShootStage++; } break; }
			}

			pev.nextthink = g_Engine.time;
		}
	}
	
	void HornetLongBurstThink()
	{
		if( m_pPlayer is null or m_pDriveEnt is null or m_iState != STATE_ATTACK_HORNET )
		{
			SetThink( null );
			m_iShootStage2 = 0;
			return;
		}

		if( m_iState == STATE_ATTACK_HORNET and m_pDriveEnt.pev.sequence == m_pDriveEnt.LookupSequence("longshoot") )
		{
			switch( GetFrame(21) )
			{
			    case 3: { if( m_iShootStage2 == 0 ) { ShootHornet(); m_iShootStage2++; } break; }
			    case 5: { if( m_iShootStage2 == 1 ) { ShootHornet(); m_iShootStage2++; } break; }
			    case 7: { if( m_iShootStage2 == 2 ) { ShootHornet(); m_iShootStage2++; } break; }
			    case 9: { if( m_iShootStage2 == 3 ) { ShootHornet(); m_iShootStage2++; } break; }
			    case 11: { if( m_iShootStage2 == 4 ) { ShootHornet(); m_iShootStage2++; } break; }
			    case 13: { if( m_iShootStage2 == 5 ) { ShootHornet(); m_iShootStage2++; } break; }
			    case 15: { if( m_iShootStage2 == 6 ) { ShootHornet(); m_iShootStage2++; } break; }
			}
			
			pev.nextthink = g_Engine.time;
		}
	}

	void RegenerateHealth ()
	{
		if( m_pDriveEnt !is null )
		{
			if( m_fNextRegenTick < g_Engine.time and m_pPlayer.pev.health < 300 )
			{
				m_pPlayer.pev.health += 20;
				m_fNextRegenTick = g_Engine.time + 1.0;
			}
		}
	}

	void CheckMaxHealth ()
	{
		if( m_pDriveEnt !is null )
		{
			if( m_fNextHealthCheck < g_Engine.time and m_pPlayer.pev.health > 300 and (m_pPlayer.pev.flags & FL_FAKECLIENT) == 0 )
			{
				m_pPlayer.pev.health = 300;
				m_fNextHealthCheck = g_Engine.time + 0.1;
			}
		}
	}

	void IdleSound()
	{
		if( m_pDriveEnt is null ) return;
		if( m_flNextSpeakTime > g_Engine.time ) return;

		if( m_flNextWordTime < g_Engine.time )
		{
			int num = -1;

			do
			{
				num = Math.RandomLong( 0,3 );
			} 
			
			while( num == m_iLastWord );

			m_iLastWord = num;

			g_SoundSystem.EmitSound( m_pDriveEnt.edict(), CHAN_VOICE, pIdleSounds[num], VOL_NORM, ATTN_NORM );

			if( Math.RandomLong(1, 10) <= 1 )
				m_flNextWordTime = m_flNextSpeakTime = g_Engine.time + ( 10 + Math.RandomFloat(0.0, 10.0) );
			else
				m_flNextWordTime = g_Engine.time + Math.RandomFloat( 0.5, 1.0 );
		}
	}

	void PlayMeleeAttackSound ()
	{
		if( m_pDriveEnt !is null )
		{
			switch( Math.RandomLong(0, 2) )
			{
				case 0: g_SoundSystem.EmitSound( m_pDriveEnt.edict(), CHAN_VOICE, "agrunt/ag_attack1.wav", VOL_NORM, ATTN_NORM ); break;
				case 1: g_SoundSystem.EmitSound( m_pDriveEnt.edict(), CHAN_VOICE, "agrunt/ag_attack2.wav", VOL_NORM, ATTN_NORM ); break;
				case 2: g_SoundSystem.EmitSound( m_pDriveEnt.edict(), CHAN_VOICE, "agrunt/ag_attack3.wav", VOL_NORM, ATTN_NORM ); break;
			}
		}
	}

	void HornetAttackThink()
	{
		if( m_pPlayer is null or m_pDriveEnt is null or m_iState != STATE_ATTACK_HORNET or m_flStopHornetAttack < g_Engine.time or m_bShotBlocked )
		{
			SetThink( null );
			return;
		}

		m_pPlayer.SetMaxSpeedOverride( 0 );

		ShootHornet();
		pev.nextthink = g_Engine.time + HORNET_REFIRE;
	}

	void ThrowSnarkThink()
	{
		if( m_pPlayer is null or m_pDriveEnt is null or m_iState != STATE_ATTACK_MELEE or m_flStopSnarkThrow < g_Engine.time )
		{
			SetThink( null );
			return;
		}

		m_pPlayer.SetMaxSpeedOverride( 0 );

		ThrowSnark();
		pev.nextthink = g_Engine.time + 0.15;
	}

	void ShootHornet()
	{
		if( m_pDriveEnt !is null and m_pPlayer.IsAlive() and !m_bShotBlocked )
		{
			Vector vecAngle, vecOrigin, vecMuzzle;
			vecAngle = m_pPlayer.pev.v_angle;

			if( vecAngle.x < -54.5 ) vecAngle.x = -54.5;
			if( vecAngle.x > 32.0 ) vecAngle.x = 32.0;

			Math.MakeVectors( vecAngle );
			m_pDriveEnt.GetAttachment( 0, vecOrigin, void );

			m_pPlayer.SetMaxSpeedOverride( 0 );

			vecMuzzle = vecOrigin + g_Engine.v_forward * 32;

			m_pDriveEnt.pev.effects |= EF_MUZZLEFLASH;

			NetworkMessage m1( MSG_PVS, NetworkMessages::SVC_TEMPENTITY, vecMuzzle );
				m1.WriteByte( TE_SPRITE );
				m1.WriteCoord( vecMuzzle.x );
				m1.WriteCoord( vecMuzzle.y );
				m1.WriteCoord( vecMuzzle.z );
				m1.WriteShort( m_iAgruntMuzzleFlash );
				m1.WriteByte( 6 ); // size * 10
				m1.WriteByte( 128 ); // brightness
			m1.End();

			CBaseEntity@ pHornet = g_EntityFuncs.Create( "hivehornet", vecMuzzle, m_pPlayer.pev.v_angle, false, m_pPlayer.edict() );
			pHornet.pev.velocity = g_Engine.v_forward * 300;
			pHornet.pev.angles = Math.VecToAngles( pHornet.pev.velocity );

			switch( Math.RandomLong(0, 2) )
			{
				case 0: g_SoundSystem.EmitSound( m_pDriveEnt.edict(), CHAN_WEAPON, "agrunt/ag_fire1.wav", VOL_NORM, ATTN_NORM ); break;
				case 1: g_SoundSystem.EmitSound( m_pDriveEnt.edict(), CHAN_WEAPON, "agrunt/ag_fire2.wav", VOL_NORM, ATTN_NORM ); break;
				case 2: g_SoundSystem.EmitSound( m_pDriveEnt.edict(), CHAN_WEAPON, "agrunt/ag_fire3.wav", VOL_NORM, ATTN_NORM ); break;
			}

			m_flNextIdleSound = g_Engine.time + 4.0;
			m_fNextRegenTick = g_Engine.time + 6.0;
		}
	}

	void ThrowSnark()
	{
		if( m_pDriveEnt !is null and m_pPlayer.IsAlive() )
		{
			entvars_t@ pevOwner;
			if( self.pev.owner !is null )
				@pevOwner = @self.pev.owner.vars;
			else
				@pevOwner = self.pev;

			g_EngineFuncs.MakeVectors( m_pPlayer.pev.v_angle );
			Vector vecGrenadeOrigin = m_pPlayer.pev.origin + m_pPlayer.pev.view_ofs + g_Engine.v_forward * 1; //= m_pPlayer.GetGunPosition() + g_Engine.v_forward * 1.0;

			CBaseEntity@ pSqueak = g_EntityFuncs.Create( "monster_hlsnark", vecGrenadeOrigin, m_pPlayer.pev.v_angle, false, m_pPlayer.edict() );
			pSqueak.pev.velocity = g_Engine.v_forward * 300 + g_Engine.v_up * 300;
			pSqueak.pev.angles = Math.VecToAngles( pSqueak.pev.velocity );

			m_flNextIdleSound = g_Engine.time + 4.0;
			m_fNextRegenTick = g_Engine.time + 6.0;
		}
	}

	void MeleeAttackThink()
	{
		m_flNextIdleSound = g_Engine.time + 4.0;
		m_fNextRegenTick = g_Engine.time + 6.0;

		if( m_pPlayer is null or m_pDriveEnt is null )
		{
			SetThink( null );
			return;
		}

		if( m_iState == STATE_RUN or m_iState == STATE_WALK )
		{
			return;
		}

		CBaseEntity@ pHurt = CheckTraceHullAttack( MELEE_RANGE, DMG_MELEE, DMG_ENERGYBEAM | DMG_NEVERGIB );
		
		if( pHurt !is null )
		{
			pHurt.pev.punchangle.y = (m_iRandomAttack == 3) ? -25.0 : 25.0;
			pHurt.pev.punchangle.x = 8.0;

			if( (pHurt.pev.flags & (FL_CLIENT)) == 1 )
				pHurt.pev.velocity = pHurt.pev.velocity - g_Engine.v_right * ((m_iRandomAttack == 3) ? 250.0 : -250.0);

			g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_WEAPON, pAttackHitSounds[Math.RandomLong(0,(pAttackHitSounds.length() - 1))], VOL_NORM, ATTN_NORM, 0, 100 + Math.RandomLong(-5, 5) );

			Vector vecArmPos, vecArmAng;
			m_pDriveEnt.GetAttachment( 0, vecArmPos, vecArmAng );
			g_WeaponFuncs.SpawnBlood( vecArmPos, pHurt.BloodColor(), 25 );
		}
		else
			g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_WEAPON, pAttackMissSounds[Math.RandomLong(0,(pAttackMissSounds.length() - 1))], VOL_NORM, ATTN_NORM, 0, 100 + Math.RandomLong(-5, 5) );

		SetThink( null );
	}

	void DoMovementAnimation()
	{
		float flMinWalkVelocity = -150.0;
		float flMaxWalkVelocity = 150.0;
		m_bShotBlocked = false;

		if( (m_pPlayer.pev.button & IN_USE) != 0 or IsBetween(m_pPlayer.pev.velocity.Length(), flMinWalkVelocity, flMaxWalkVelocity) )
		{
			if( m_pDriveEnt.pev.sequence != m_pDriveEnt.LookupSequence("walk") )
			{
				m_iState = STATE_WALK;
				m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("walk");
				m_pDriveEnt.pev.frame = 0;
				m_pDriveEnt.ResetSequenceInfo();
			}
			else
			{
				if( GetFrame(20) == 9 and m_pPlayer.pev.flags & FL_ONGROUND != 0 )
				{
					switch( Math.RandomLong(0, 1) )
					{
						case 0:	g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "player/pl_ladder2.wav", VOL_NORM, ATTN_NORM, 0, 70 ); break;
						case 1:	g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "player/pl_ladder4.wav", VOL_NORM, ATTN_NORM, 0, 70 ); break;
					}
				}
				else if( GetFrame(20) == 19 and m_pPlayer.pev.flags & FL_ONGROUND != 0 )
				{
					switch( Math.RandomLong(0, 1) )
					{
						case 0:	g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "player/pl_ladder1.wav", VOL_NORM, ATTN_NORM, 0, 70 ); break;
						case 1:	g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "player/pl_ladder3.wav", VOL_NORM, ATTN_NORM, 0, 70 ); break;
					}
				}
			}

			m_fNextRegenTick = g_Engine.time + 6.0;
			
			if( (m_pPlayer.pev.flags & FL_FAKECLIENT) != 0 )
			{
				//nada
			}
			else
			{
				self.m_flNextPrimaryAttack = g_Engine.time + 0.25;
				self.m_flNextSecondaryAttack = g_Engine.time + 0.15;
			}
		}
		else if( (m_pPlayer.pev.flags & FL_FAKECLIENT) != 0 and m_pPlayer.pev.velocity.Length2D() > 0.0 )
		{
			if( m_pDriveEnt.pev.sequence != m_pDriveEnt.LookupSequence("run") )
			{
				m_iState = STATE_RUN;
				m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("run");
				m_pDriveEnt.pev.frame = 0;
				m_pDriveEnt.ResetSequenceInfo();
			}
			else
			{
				/*if( GetFrame(26) == 10 )
				{
					g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_VOICE, "agrunt/ag_alert1.wav", 1.0, ATTN_NORM, 0, 80 );
				}*/
				if( GetFrame(26) == 11 and m_pPlayer.pev.flags & FL_ONGROUND != 0 )
				{
					switch( Math.RandomLong(0, 1) )
					{
						case 0:	g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "player/pl_ladder2.wav", VOL_NORM, ATTN_NORM, 0, 70 ); break;
						case 1:	g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "player/pl_ladder4.wav", VOL_NORM, ATTN_NORM, 0, 70 ); break;
					}
				}
				/*else if( GetFrame(26) == 22 )
				{
					g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_VOICE, "agrunt/ag_alert1.wav", 1.0, ATTN_NORM, 0, PITCH_NORM );
				}*/
				else if( GetFrame(26) == 23 and m_pPlayer.pev.flags & FL_ONGROUND != 0 )
				{
					switch( Math.RandomLong(0, 1) )
					{
						case 0:	g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "player/pl_ladder1.wav", VOL_NORM, ATTN_NORM, 0, 70 ); break;
						case 1:	g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "player/pl_ladder3.wav", VOL_NORM, ATTN_NORM, 0, 70 ); break;
					}
				}

				m_fNextRegenTick = g_Engine.time + 6.0;
				m_pPlayer.pev.flTimeStepSound = 9999; //prevents normal footsteps from playing, kinda? :hehe:
			}
		}
		else
		{
			if( m_pDriveEnt.pev.sequence != m_pDriveEnt.LookupSequence("run") )
			{
				m_iState = STATE_RUN;
				m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("run");
				m_pDriveEnt.pev.frame = 0;
				m_pDriveEnt.ResetSequenceInfo();
			}
			else
			{
				/*if( GetFrame(26) == 10 )
				{
					g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_VOICE, "agrunt/ag_alert5.wav", 1.0, ATTN_NORM, 0, 70 );
				}*/
				if( GetFrame(26) == 11 )
				{
					switch( Math.RandomLong(0, 1) )
					{
						case 0:	g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "player/pl_ladder2.wav", VOL_NORM, ATTN_NORM, 0, 70 ); break;
						case 1:	g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "player/pl_ladder4.wav", VOL_NORM, ATTN_NORM, 0, 70 ); break;
					}
				}
				/*else if( GetFrame(26) == 22 )
				{
					g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_VOICE, "agrunt/ag_alert5.wav", 1.0, ATTN_NORM, 0, PITCH_NORM );
				}*/
				else if( GetFrame(26) == 23 )
				{
					switch( Math.RandomLong(0, 1) )
					{
						case 0:	g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "player/pl_ladder1.wav", VOL_NORM, ATTN_NORM, 0, 70 ); break;
						case 1:	g_SoundSystem.EmitSoundDyn( m_pDriveEnt.edict(), CHAN_BODY, "player/pl_ladder3.wav", VOL_NORM, ATTN_NORM, 0, 70 ); break;
					}
				}

				m_fNextRegenTick = g_Engine.time + 6.0;
				self.m_flNextPrimaryAttack = g_Engine.time + 0.25;
				self.m_flNextSecondaryAttack = g_Engine.time + 0.15;
				m_pPlayer.pev.flTimeStepSound = 9999; //prevents normal footsteps from playing, kinda? :hehe:
			}
		}
	}

	void DoIdleAnimation()
	{
		if( m_pDriveEnt is null ) return;
		//if( m_iState == STATE_ATTACK_HORNET and m_pDriveEnt.pev.sequence == m_pDriveEnt.LookupSequence("quickshoot") and !m_pDriveEnt.m_fSequenceFinished ) return;
		//if( m_iState == STATE_ATTACK_HORNET and m_pDriveEnt.pev.sequence == m_pDriveEnt.LookupSequence("hornetattack") and !m_pDriveEnt.m_fSequenceFinished ) return;
		//if( m_iState == STATE_ATTACK_HORNET and m_pDriveEnt.pev.sequence == m_pDriveEnt.LookupSequence("longshoot") and !m_pDriveEnt.m_fSequenceFinished ) return;
		//if( m_iState == STATE_ATTACK_MELEE and m_pDriveEnt.pev.sequence == m_pDriveEnt.LookupSequence("mattack2") and !m_pDriveEnt.m_fSequenceFinished ) return;
		//if( m_iState == STATE_ATTACK_MELEE and m_pDriveEnt.pev.sequence == m_pDriveEnt.LookupSequence("mattack3") and !m_pDriveEnt.m_fSequenceFinished ) return;

		if( m_iState != STATE_IDLE )
		{
			m_pPlayer.SetMaxSpeedOverride( 270 );
			m_iState = STATE_IDLE;
			m_pDriveEnt.pev.sequence = m_pDriveEnt.LookupSequence("idle1");
			m_pDriveEnt.pev.frame = 0;
			m_pDriveEnt.ResetSequenceInfo();
			m_iShootStage = 0;
			m_iShootStage2 = 0;
			m_bShotBlocked = false;
		}
	}

	void spawn_driveent()
	{
		if( !m_pPlayer.pev.FlagBitSet(FL_ONGROUND) )
		{
			g_EngineFuncs.ClientPrintf( m_pPlayer, print_center, "Unable to deploy\nwhile in the air" );
			return;
		}

		@m_pDriveEnt = cast<CBaseAnimating@>( g_EntityFuncs.Create("cnpc_agrunt", m_pPlayer.pev.origin, m_pPlayer.pev.angles, true, m_pPlayer.edict()) );

		m_pDriveEnt.pev.set_controller( 0,  127 );

		g_EntityFuncs.DispatchSpawn( m_pDriveEnt.edict() );

		m_pPlayer.pev.effects |= EF_NODRAW;
		m_pPlayer.pev.flags &= ~FL_NOTARGET;
		m_pPlayer.pev.iuser3 = 1;
		m_pPlayer.pev.fuser4 = 1; //disable jump
		m_pPlayer.m_bloodColor = BLOOD_COLOR_YELLOW;
		self.m_bExclusiveHold = true;

		m_pPlayer.SetViewMode( ViewMode_ThirdPerson );

		NetworkMessage m1( MSG_ONE, NetworkMessages::SVC_STUFFTEXT, m_pPlayer.edict() );
			m1.WriteString( "cam_idealdist 128\n" );
		m1.End();

		CustomKeyvalues@ pCustom = m_pPlayer.GetCustomKeyvalues();
		pCustom.SetKeyvalue( CNPC::sCNPCKV, CNPC::CNPC_AGRUNT );
		//g_EntityFuncs.DispatchKeyValue(m_pPlayer.edict(), "classify", 4);
		g_EntityFuncs.DispatchKeyValue(m_pPlayer.edict(), "max_health", 300);
		g_EntityFuncs.DispatchKeyValue(m_pPlayer.edict(), "health", 300);
		m_pPlayer.pev.view_ofs = Vector( 0, 0, 50 );

		/*if( (m_pPlayer.pev.flags & FL_FAKECLIENT) != 0 )
		{
			m_pPlayer.pev.netname = "Alien Grunt";
		}*/
	}

	void ResetPlayer()
	{
		m_pPlayer.pev.iuser3 = 0;
		m_pPlayer.pev.fuser4 = 0; //enable jump
		m_pPlayer.SetMaxSpeedOverride( -1 );

		CustomKeyvalues@ pCustom = m_pPlayer.GetCustomKeyvalues();
		pCustom.SetKeyvalue( CNPC::sCNPCKV, 0 );
		g_EntityFuncs.DispatchKeyValue(m_pPlayer.edict(), "max_health", 100);
	}
}

class cnpc_agrunt : ScriptBaseAnimating//ScriptBaseMonsterEntity
{
	protected CBasePlayer@ m_pOwner
	{
		get { return cast<CBasePlayer@>( g_EntityFuncs.Instance(pev.owner) ); }
	}

	private float m_flNextOriginUpdate; //hopefully fixes hacky movement on other players

	void Spawn()
	{
		g_EntityFuncs.SetModel( self, "models/cnpc/agrunt.mdl" );
		g_EntityFuncs.SetSize( self.pev, Vector(-16, -16, 0), Vector(16, 16, 72) );
		g_EntityFuncs.SetOrigin( self, pev.origin );
		g_EngineFuncs.DropToFloor( self.edict() );

		pev.solid = SOLID_NOT;
		pev.movetype = MOVETYPE_NOCLIP;

		pev.sequence = self.LookupSequence("idle1");
		pev.frame = 0;
		self.ResetSequenceInfo();

		//pev.takedamage = DAMAGE_AIM;
		//self.m_bloodColor = BLOOD_COLOR_YELLOW;
		//self.m_MonsterState		= MONSTERSTATE_NONE;
		//self.m_afCapability			= 0;
		//self.MonsterInit();

		SetThink( ThinkFunction(this.DriveThink) );
		pev.nextthink = g_Engine.time;
	}

	void DriveThink()
	{
		if( m_pOwner is null or !m_pOwner.IsConnected() or m_pOwner.pev.deadflag != DEAD_NO )
		{
			pev.velocity = g_vecZero;
			SetThink( ThinkFunction(this.DieThink) );
			pev.nextthink = g_Engine.time;

			return;
		}

		if( m_flNextOriginUpdate < g_Engine.time )
		{
			Vector vecOrigin = m_pOwner.pev.origin;
			vecOrigin.z -= 32.0;
			g_EntityFuncs.SetOrigin( self, vecOrigin );
			m_flNextOriginUpdate = g_Engine.time + 0.1;
		}

		pev.velocity = m_pOwner.pev.velocity;

		pev.angles.x = 0;

		if( pev.velocity.Length2D() > 0.0 and (pev.sequence == self.LookupSequence("walk") or pev.sequence == self.LookupSequence("run")) )
			pev.angles.y = Math.VecToAngles( pev.velocity ).y;
		else
			pev.angles.y = m_pOwner.pev.angles.y;

		pev.angles.z = 0;

		self.StudioFrameAdvance();

		pev.nextthink = g_Engine.time + 0.01;
	}

	void DieThink()
	{
		array<string> arrsDeathAnims = 
		{
			"dieforward",
			"diebackward",
			"diegut",
			"diesimple"
		};

		pev.sequence = self.LookupSequence( arrsDeathAnims[Math.RandomLong(0, arrsDeathAnims.length()-1)] );
		pev.frame = 0;
		self.ResetSequenceInfo();

		g_EngineFuncs.DropToFloor( self.edict() );

		g_SoundSystem.EmitSound( self.edict(), CHAN_VOICE, pDieSounds[Math.RandomLong(0, pDieSounds.length()-1)], VOL_NORM, ATTN_NORM );

		SetThink( ThinkFunction(this.SUB_StartFadeOut) );
		pev.nextthink = g_Engine.time + 15.0;
	}

	void SUB_StartFadeOut()
	{
		if( pev.rendermode == kRenderNormal )
		{
			pev.renderamt = 255;
			pev.rendermode = kRenderTransTexture;
		}

		pev.solid = SOLID_NOT;
		pev.avelocity = g_vecZero;

		pev.nextthink = g_Engine.time + 0.1;
		SetThink( ThinkFunction(this.SUB_FadeOut) );
	}

	void SUB_FadeOut()
	{
		if( pev.renderamt > 7 )
		{
			pev.renderamt -= 7;
			pev.nextthink = g_Engine.time + 0.1;
		}
		else 
		{
			pev.renderamt = 0;
			pev.nextthink = g_Engine.time + 0.2;
			SetThink( ThinkFunction(this.SUB_Remove) );
		}
	}

	void SUB_Remove()
	{
		self.UpdateOnRemove();

		if( pev.health > 0 )
			pev.health = 0;

		NetworkMessage killbeam( MSG_BROADCAST, NetworkMessages::SVC_TEMPENTITY, null );
			killbeam.WriteByte(TE_KILLBEAM);
			killbeam.WriteShort(self.entindex());
		killbeam.End();

		g_EntityFuncs.Remove(self);
	}
}

void Register()
{
	g_CustomEntityFuncs.RegisterCustomEntity( "cnpc_agrunt::cnpc_agrunt", "cnpc_agrunt" );
	g_Game.PrecacheOther( "cnpc_agrunt" );

	g_CustomEntityFuncs.RegisterCustomEntity( "cnpc_agrunt::weapon_agrunt", sWeaponName );
	g_ItemRegistry.RegisterWeapon( sWeaponName, "controlnpc", "hornets" );
	g_Game.PrecacheOther( sWeaponName );
}

} //namespace cnpc_agrunt END

/* FIXME
Holding any button after attacking will cause the animation to freeze at the last frame until released
*/

/* TODO
Use turning animations 
*/
