// Credits to RedSprend / Rick for weapon base
// Thanks to Nevermore2790 and Mikk155 for helping

/*namespace CustomHornet
{*/
const int HORNET_TYPE_PRI		= 0;
const int HORNET_TYPE_ALT		= 1;
const float HORNET_PRI_SPEED	= 700.0f;
const float HORNET_ALT_SPEED	= 900.0f;
const float HORNET_BUZZ_VOLUME	= 0.8f;

const int HORNET_DAMAGE			= ( g_EngineFuncs.CVarGetFloat( "sk_hornet_pdmg" ) * 1 );
const int POISON_TICKS = 2;
const float POISON_DAMAGE_PER_SECOND = 0.0f; // idk why but this applies a second poison to entitiy

const string HORNET_MODEL	= "models/sc_insecure/hornet_poison.mdl";

class CHornet : ScriptBaseMonsterEntity
{
	private float m_flStopAttack;
	private int m_iHornetType;
	private float m_flFlySpeed;
	
	private int m_iHornetTrail;
	private int m_iHornetPuff;
	
	// don't let hornets gib, ever.
	int TakeDamage( entvars_t@ pevInflictor, entvars_t@ pevAttacker, float flDamage, int bitsDamageType)
	{
		// filter these bits a little.
		bitsDamageType &= ~ ( DMG_ALWAYSGIB );
		bitsDamageType |= DMG_NEVERGIB;

		return BaseClass.TakeDamage(pevInflictor, pevAttacker, flDamage, bitsDamageType);
	}

	void Spawn( void )
	{
		Precache();

		pev.movetype	= MOVETYPE_FLY;
		pev.solid		= SOLID_BBOX;
		pev.takedamage 	= DAMAGE_YES;
		pev.flags		|= FL_MONSTER;
		pev.health		= 1;// weak!

		// hornets don't live as long in multiplayer
		m_flStopAttack = g_Engine.time + 5.0;

		self.m_flFieldOfView = 0.9; // +- 25 degrees

		if ( Math.RandomLong( 1, 5 ) <= 2 )
		{
			m_iHornetType = HORNET_TYPE_PRI;
			m_flFlySpeed = HORNET_PRI_SPEED;
		}
		else
		{
			m_iHornetType = HORNET_TYPE_ALT;
			m_flFlySpeed = HORNET_ALT_SPEED;
		}

		g_EntityFuncs.SetModel( self, HORNET_MODEL );
		g_EntityFuncs.SetSize( self.pev, Vector( -4, -4, -4 ), Vector( 4, 4, 4 ) );

		SetTouch( TouchFunction( DieTouch ) );
		SetThink( ThinkFunction( StartTrack ) );

		edict_t@ pSoundEnt = pev.owner;
		if ( pSoundEnt is null )
			@pSoundEnt = self.edict();

		pev.dmg = HORNET_DAMAGE;
		
		pev.nextthink = g_Engine.time + 0.1;
		self.ResetSequenceInfo( );
	}

	void Precache( void )
	{
		g_Game.PrecacheModel( HORNET_MODEL );

		g_SoundSystem.PrecacheSound( "insecure/phornet/ph_buzz1.wav" );
		g_SoundSystem.PrecacheSound( "insecure/phornet/ph_buzz2.wav" );
		g_SoundSystem.PrecacheSound( "insecure/phornet/ph_buzz3.wav" );

		g_SoundSystem.PrecacheSound( "insecure/phornet/ph_fire1.wav" );
		g_SoundSystem.PrecacheSound( "insecure/phornet/ph_fire2.wav" );
		g_SoundSystem.PrecacheSound( "insecure/phornet/ph_fire3.wav" );

		g_SoundSystem.PrecacheSound( "insecure/phornet/ph_hornethit1.wav" );
		g_SoundSystem.PrecacheSound( "insecure/phornet/ph_hornethit2.wav" );
		g_SoundSystem.PrecacheSound( "insecure/phornet/ph_hornethit3.wav" );

		m_iHornetPuff = g_Game.PrecacheModel( "sprites/insecure/muz9.spr" );
		m_iHornetTrail = g_Game.PrecacheModel( "sprites/laserbeam.spr" );
	}	

	// hornets will never get mad at each other, no matter who the owner is.
	int IRelationship( CBaseEntity@ pTarget )
{
    if ( pTarget is null )
        return R_NO;

    if ( pTarget.edict() is pev.owner )
        return R_NO;

    if ( pTarget.IsPlayer() )
        return R_NO;

    if ( pTarget.pev.modelindex == pev.modelindex )
        return R_NO;

    if ( pTarget.pev.takedamage == DAMAGE_NO )
        return R_NO;
		
	if ( pTarget.pev.classname == "hornet"
	|| pTarget.pev.classname == "func_breakable"
	|| pTarget.Classify() == CLASS_NONE
	|| pTarget.pev.classname == "monster_sentry"
	|| pTarget.pev.classname == "monster_turret"
	|| pTarget.pev.classname == "monster_tripmine"
	|| pTarget.pev.classname == "monster_snark"
	|| pTarget.pev.classname == "monster_miniturret"
	|| pTarget.pev.classname == "monster_barnacle"
	|| pTarget.pev.classname == "monster_cockroach" )
        return R_NO;

    return R_HT;
}

	// ID's Hornet as their owner
	int Classify( void )
	{
		return CLASS_PLAYER_BIOWEAPON;
	}
	
	CBaseEntity@ FindClosestEnemy( float fRadius )
	{
		CBaseEntity@ ent = null;
		CBaseEntity@ enemy = null;
		float iNearest = fRadius;

		do
		{
			@ent = g_EntityFuncs.FindEntityInSphere( ent, self.pev.origin,
				fRadius, "*", "classname" ); 

			if ( ent is null || !ent.IsAlive() )
				continue;

			if ( ent.pev.classname == "squadmaker" )
				continue;

			if ( ent.entindex() == self.entindex() )
				continue;

			if ( ent.edict() is pev.owner )
				continue;

			int rel = IRelationship(ent);
			if ( rel == R_AL || rel == R_NO )
				continue;

			float iDist = ( ent.pev.origin - self.pev.origin ).Length();
			if ( iDist < iNearest )
			{
				iNearest = iDist;
				@enemy = ent;
			}
		}
		while ( ent !is null );

		return enemy;
	}

	// StartTrack - starts a hornet out tracking its target
	void StartTrack( void )
	{
		IgniteTrail();

		SetTouch( TouchFunction( TrackTouch ) );
		SetThink( ThinkFunction( TrackTarget ) );

		pev.nextthink = g_Engine.time + 0.1;
	}

	void StopTrack( void )
	{
		self.SUB_Remove();
	}

	// StartDart - starts a hornet out just flying straight.
	void StartDart ( void )
	{
		IgniteTrail();

		SetTouch( TouchFunction( DartTouch ) );
		SetThink( ThinkFunction( StopTrack ) );

		pev.nextthink = g_Engine.time + 4;
	}

	void IgniteTrail( void )
	{
		// trail
		NetworkMessage message( MSG_BROADCAST, NetworkMessages::SVC_TEMPENTITY );
			message.WriteByte( TE_BEAMFOLLOW );
			message.WriteShort( self.entindex() );
			message.WriteShort( m_iHornetTrail );
			message.WriteByte( 10 );
			message.WriteByte( 2 );

			switch ( m_iHornetType )
			{
			case HORNET_TYPE_PRI:
				message.WriteByte( 0 );	// r, was 179
				message.WriteByte( 200 );	// g, was 39
				message.WriteByte( 0 );	// b, was 14
				break;
			case HORNET_TYPE_ALT:
				message.WriteByte( 10 );	// r, was 255	
				message.WriteByte( 200 );	// g, was 128
				message.WriteByte( 0 );		// b, was 0
				break;
			}

			message.WriteByte( 128 );
		message.End();
	}

	// Hornet is flying, gently tracking target
	void TrackTarget( void )
	{
		Vector	vecFlightDir;
		Vector	vecDirToEnemy;
		float	flDelta;

		self.StudioFrameAdvance();

		if (g_Engine.time > m_flStopAttack)
		{
			SetTouch( null );
			SetThink( ThinkFunction( StopTrack ) );
			pev.nextthink = g_Engine.time + 0.1;
			return;
		}

		if( !self.m_hEnemy.IsValid() || !self.m_hEnemy.GetEntity().IsAlive() )
		{
			self.m_hEnemy = FindClosestEnemy( 512 );
		}

		if ( self.m_hEnemy.IsValid() && self.FVisible( self.m_hEnemy.GetEntity(), true ))
		{
			self.m_vecEnemyLKP = self.m_hEnemy.GetEntity().BodyTarget( pev.origin );
		}
		else
		{
			self.m_vecEnemyLKP = self.m_vecEnemyLKP + pev.velocity * m_flFlySpeed * 0.1;
		}

		vecDirToEnemy = ( self.m_vecEnemyLKP - pev.origin ).Normalize();

		if (pev.velocity.Length() < 0.1)
			vecFlightDir = vecDirToEnemy;
		else 
			vecFlightDir = pev.velocity.Normalize();

		// measure how far the turn is, the wider the turn, the slow we'll go this time.
		flDelta = DotProduct ( vecFlightDir, vecDirToEnemy );

		if ( flDelta < 0.5 )
		{
			// hafta turn wide again. play sound
			switch ( Math.RandomLong( 0, 2 ) )
			{
			case 0:	g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, "insecure/phornet/ph_buzz1.wav", HORNET_BUZZ_VOLUME, ATTN_NORM);	break;
			case 1:	g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, "insecure/phornet/ph_buzz1.wav", HORNET_BUZZ_VOLUME, ATTN_NORM);	break;
			case 2:	g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, "insecure/phornet/ph_buzz1.wav", HORNET_BUZZ_VOLUME, ATTN_NORM);	break;
			}
		}

		if ( flDelta <= 0 && m_iHornetType == HORNET_TYPE_PRI )
		{
			// no flying backwards, but we don't want to invert this, cause we'd go fast when we have to turn REAL far.
			flDelta = 0.25;
		}

		pev.velocity = ( vecFlightDir + vecDirToEnemy).Normalize();

		if ( pev.owner !is null && (pev.owner.vars.flags & FL_MONSTER) != 0 )
		{
			// random pattern applies to hornets fired by monsters
			pev.velocity.x += Math.RandomFloat( -0.08, 0.08 );// scramble the flight dir a bit.
			pev.velocity.y += Math.RandomFloat( -0.08, 0.08 );
			pev.velocity.z += Math.RandomFloat( -0.08, 0.08 );
		}
		
		switch ( m_iHornetType )
		{
			case HORNET_TYPE_PRI:
				pev.velocity = pev.velocity * ( m_flFlySpeed * flDelta );// scale the dir by the ( speed * width of turn )
				pev.nextthink = g_Engine.time + Math.RandomFloat( 0.1, 0.3 );
				break;
			case HORNET_TYPE_ALT:
				pev.velocity = pev.velocity * m_flFlySpeed;// do not have to slow down to turn.
				pev.nextthink = g_Engine.time + 0.1;// fixed think time
				break;
		}

		pev.angles = Math.VecToAngles (pev.velocity);

		pev.solid = SOLID_BBOX;
	}

	// Tracking Hornet hit something
	void TrackTouch( CBaseEntity@ pOther )
	{
		if ( pOther.edict() is pev.owner || pOther.pev.modelindex == pev.modelindex || pOther.pev.takedamage == DAMAGE_NO )
		{
			// bumped into the guy that shot it.
			pev.solid = SOLID_NOT;
			return;
		}

		if ( IRelationship( pOther ) <= R_NO )
		{
			// hit something we don't want to hurt, so turn around.

			pev.velocity = pev.velocity.Normalize();

			pev.velocity.x *= -1;
			pev.velocity.y *= -1;

			pev.origin = pev.origin + pev.velocity * 4; // bounce the hornet off a bit.
			pev.velocity = pev.velocity * m_flFlySpeed;

			return;
		}

		DieTouch( pOther );
	}

	void DartTouch( CBaseEntity@ pOther )
	{
		DieTouch( pOther );
	}

	void StartPoison( CBaseEntity@ victim, CBaseEntity@ attacker )
	{
		if( victim is null )
			return;

		// game used to crash when poison applied to bullsquid and gargantua
		string cname = victim.GetClassname();
		//if( cname == "monster_bullchicken" || cname == "monster_gargantua" )
		//	return;

		// dont apply if no damagisti
		if( victim.pev.takedamage == DAMAGE_NO )
			return;

		CustomKeyvalues@ ckv = victim.GetCustomKeyvalues();
		int ticks = ckv.GetKeyvalue("$i_poison").GetInteger();

		// reset poison timer if already poisoned
		if( ticks > 0 )
		{
			ckv.SetKeyvalue( "$i_poison", Math.min( ticks + 1, POISON_TICKS ) );
			return;
		}

		// start loop if not poisoned
		ckv.SetKeyvalue( "$i_poison", POISON_TICKS );

		g_Scheduler.SetTimeout( this, "ApplyPoison", 2.0f, EHandle(victim), EHandle(attacker) );
	}

	void ApplyPoison( EHandle hVictim, EHandle hAttacker )
	{
		if( !hVictim.IsValid() )
			return;

		CBaseEntity@ victim = hVictim.GetEntity();
		if( victim is null )
			return;

		// game crashes when poison applies to bullsquid and gargantua
		string cname = victim.GetClassname();
		if( cname == "monster_bullchicken" || cname == "monster_gargantua" )
			return;

		if( victim.pev.takedamage == DAMAGE_NO )
			return;

		CustomKeyvalues@ ckv = victim.GetCustomKeyvalues();
		int ticks = ckv.GetKeyvalue("$i_poison").GetInteger();

		if( ticks <= 0 )
			return;

		entvars_t@ pevAttacker = null;
			if( hAttacker.IsValid() )
			{
				CBaseEntity@ attackerEnt = hAttacker.GetEntity();
				if( attackerEnt !is null )
					@pevAttacker = attackerEnt.pev;
			}

		if( pevAttacker is null )
		@pevAttacker = victim.pev;
		victim.TakeDamage( pevAttacker, pevAttacker, POISON_DAMAGE_PER_SECOND, DMG_POISON );
		ckv.SetKeyvalue( "$i_poison", ticks - 1 );
		if( ticks - 1 > 0 )
		{
			g_Scheduler.SetTimeout( this, "ApplyPoison", 2.0f, hVictim, hAttacker );
		}
	}

	void DieTouch( CBaseEntity@ pOther )
	{
		if ( pOther !is null && pOther.pev.takedamage != 0.0f )
		{
			// muzzle sprite when hornet hits target
			Vector vecPos = pev.origin;
			NetworkMessage	message( MSG_PVS, NetworkMessages::SVC_TEMPENTITY, vecPos );
			message.WriteByte( TE_SPRITE );
			message.WriteCoord( vecPos.x );
			message.WriteCoord( vecPos.y );
			message.WriteCoord( vecPos.z );
			message.WriteShort( m_iHornetPuff );
			message.WriteByte( 5 );		// size (x10)
			message.WriteByte( 200 );  	// brightness
			message.End();
			
			// buzz when you plug someone
			switch (Math.RandomLong(0,2))
			{
				case 0:	g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, "insecure/phornet/ph_hornethit1.wav", 1, ATTN_NORM);	break;
				case 1:	g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, "insecure/phornet/ph_hornethit2.wav", 1, ATTN_NORM);	break;
				case 2:	g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, "insecure/phornet/ph_hornethit3.wav", 1, ATTN_NORM);	break;
			}

			// do the damage		
			
			if( pOther.GetClassname() == "monster_houndeye_explosive" )
			{
				pOther.TakeDamage( pev, pev.owner.vars, pev.dmg, DMG_BULLET );	// calcute damage again just to instakill them
			}
			
			pOther.TakeDamage( pev, pev.owner.vars, pev.dmg, DMG_BULLET );
			if( IRelationship( pOther ) > R_NO )
			{
    		StartPoison( pOther, self );
			}
			
			// give points to player so TEXHABP!!! can get more money $$$$$$
			
            CBaseEntity@ pOwnerEnt = g_EntityFuncs.Instance( pev.owner );
            CBasePlayer@ pPlayer = cast<CBasePlayer@>( pOwnerEnt );
            
            if ( pOwnerEnt !is null && pOther.IsMonster() && IRelationship( pOther ) >= R_DL )
            {
                if ( pPlayer !is null && pOther.GetClassname() != "monster_gargantua" )
                {
                    pPlayer.pev.frags += .2; // give points
                }
            }
            else if ( pOwnerEnt !is null && pOther.IsMonster() && IRelationship( pOther ) == R_AL )
            {
                if ( pPlayer !is null )
                {
                    pPlayer.pev.frags -= .2; // lose points
                }
            }
		}

		// GeckoN: modelindex is const, let's apply EF_NODRAW instead
		//pev.modelindex = 0;// so will disappear for the 0.1 secs we wait until NEXTTHINK gets rid
		pev.effects |= EF_NODRAW;
		pev.solid = SOLID_NOT;

		SetThink ( ThinkFunction( StopTrack ) );
		pev.nextthink = g_Engine.time + 1;// stick around long enough for the sound to finish!
	}

}

/*void Register()
{
	g_CustomEntityFuncs.RegisterCustomEntity( "CustomHornet::CHornet", "customhornet" );
}

}*/ // end of namespace