// Insecure Explosive Houndeyes.
// Ported to Angelscript by Nevermore2790.

namespace SVEN_HOUNDEYE_EXPLOSIVE
{
	// Sprites
	const string HOUND_EXP_SHOCKWAVE				= "sprites/white.spr";
	const string HOUND_EXP_FIREBALL					= "sprites/zerogxplode.spr";
	const string HOUND_EXP_FIREBALL_WATER			= "sprites/WXplo1.spr";
	// Models
	const string HOUND_EXP_MODEL					= "models/sc_insecure/houndeye_explosive.mdl";
	const string HOUND_EXP_MODEL_CLASSIC			= "models/sc_insecure/hlclassic/houndeye_explosive.mdl";
	
	// Monster information
	const float HOUND_EXP_HEALTH					= g_EngineFuncs.CVarGetFloat( "sk_houndeye_health" ) * 0.2;
	const float HOUND_EXP_DAMAGE					= g_EngineFuncs.CVarGetFloat( "sk_houndeye_dmg_blast" ) * 3;
	const float HOUND_EXP_RADIUS					= 300;
	
	const int HOUND_EXP_MAX_ATTACK_RADIUS 			= 384;
	// Marphy Fact Files Fix - Fix various instances of Houndeye not correctly blinking/closing eyes
	const int HOUND_EXP_EYE_FRAMES 					= 3;	// how many different switchable maps for the eye
	const int HOUND_EXP_SOUND_STARTLE_VOLUME		= 128;	 // how loud a sound has to be to badly scare a sleeping houndeye
	
	const int ACTIVITY_NOT_AVAILABLE = -1;
	
	const int TASKSTATUS_RUNNING 	= 1; // Running task & movement
	const int TASKSTATUS_COMPLETE 	= 4; // Completed, get next task
	
	// Monster Spawnflags
	const int SF_MONSTER_FALL_TO_GROUND = int(0x80000000);

	// Monster Hint types
	const int HINT_NONE = 0;
	const int HINT_WORLD_MACHINERY = 4;
	const int HINT_WORLD_BLINKING_LIGHT = 8;
	const int HINT_WORLD_HUMAN_BLOOD = 10;
	const int HINT_WORLD_ALIEN_BLOOD = 11;
	
	//=========================================================
	// monster-specific tasks
	//=========================================================
	enum TASK_HOUND_EXP
	{
		TASK_HOUND_EXP_CLOSE_EYE = LAST_COMMON_TASK + 1,
		TASK_HOUND_EXP_OPEN_EYE,
		TASK_HOUND_EXP_THREAT_DISPLAY,
		TASK_HOUND_EXP_FALL_ASLEEP,
		TASK_HOUND_EXP_WAKE_UP,
		TASK_HOUND_EXP_HOP_BACK
	};
	
	//=========================================================
	// monster-specific schedule types
	//=========================================================
	enum SCHED_HOUND_EXP
	{
		SCHED_HOUND_EXP_AGITATED = LAST_COMMON_SCHEDULE + 1,
		SCHED_HOUND_EXP_HOP_RETREAT,
		SCHED_HOUND_EXP_FAIL
	};
	
	//=========================================================
	// Monster's Anim Events Go Here
	//=========================================================
	const int HOUND_EXP_AE_WARN = ( 1 );
	const int HOUND_EXP_AE_STARTATTACK = ( 2 );
	const int HOUND_EXP_AE_THUMP = ( 3 );
	const int HOUND_EXP_AE_ANGERSOUND1 = ( 4 );
	const int HOUND_EXP_AE_ANGERSOUND2 = ( 5 );
	const int HOUND_EXP_AE_HOPBACK = ( 6 );
	const int HOUND_EXP_AE_CLOSE_EYE = ( 7 );

	void PrecacheSoundArray(const array<string>& in arr)
	{
		for ( uint i = 0; i < arr.length(); i++ )
			g_SoundSystem.PrecacheSound( arr[i] );
	}

	string RandomSoundFromArray(const array<string>& in arr)
	{
		return arr[Math.RandomLong( 0, arr.length() - 1 )];
	}
	
	const array<string> pAttackSounds =
	{
		"houndeye/he_attack1.wav",
		"houndeye/he_attack2.wav",
		"houndeye/he_attack3.wav"
	};

	const array<string> pAlertSounds =
	{
		"houndeye/he_alert1.wav",
		"houndeye/he_alert2.wav",
		"houndeye/he_alert3.wav"
	};
	
	const array<string> pPainSounds =
	{
		"houndeye/he_pain1.wav",
		"houndeye/he_pain2.wav",
		"houndeye/he_pain3.wav",
		"houndeye/he_pain4.wav",
		"houndeye/he_pain5.wav"
	};

	const array<string> pDeathSounds =
	{
		"houndeye/he_die1.wav",
		"houndeye/he_die2.wav",
		"houndeye/he_die3.wav"
	};

	const array<string> pIdleSounds =
	{
		"houndeye/he_idle1.wav",
		"houndeye/he_idle2.wav",
		"houndeye/he_idle3.wav",
		"houndeye/he_idle4.wav"
	};
	
	const array<string> pSonicWaveSounds =
	{
		"houndeye/he_blast1.wav",
		"houndeye/he_blast2.wav",
		"houndeye/he_blast3.wav"
	};
	
	const array<string> pHuntSounds =
	{
		"houndeye/he_hunt1.wav",
		"houndeye/he_hunt2.wav",
		"houndeye/he_hunt3.wav",
		"houndeye/he_hunt4.wav"
	};
	
	const array<string> pExplodeSounds =
	{
		"weapons/explode3.wav",
		"weapons/explode4.wav",
		"weapons/explode5.wav"
	};

	class CHoundeyeExplosive : ScriptBaseMonsterEntity
	{
		int m_iSoundPitch, m_iExplosionTexture, m_iExplosionWaterTexture, m_iShockwaveTexture;
		
		bool m_fDontBlink;		// don't try to open/close eye if this bit is set!
		Vector m_vecPackCenter; // the center of the pack. The leader maintains this by averaging the origins of all pack members.
		
		CHoundeyeExplosive()
		{
			@this.m_Schedules = @monster_houndeye_explosive_schedules;
		}
	
		//=========================================================
		// Spawn
		//=========================================================
		void Spawn()
		{
			Precache();

			// Added Classic Mode Support.
			if ( !self.SetupModel() )
			{
				if ( string( self.pev.model ).IsEmpty() )
				{
					if ( g_ClassicMode.IsEnabled() )
						g_EntityFuncs.SetModel( self, HOUND_EXP_MODEL_CLASSIC );
					else
						g_EntityFuncs.SetModel( self, HOUND_EXP_MODEL );
				}
			}
			
			g_EntityFuncs.SetSize( self.pev, Vector(-16, -16, 0), Vector(16, 16, 36) );

			self.pev.solid = SOLID_SLIDEBOX;
			self.pev.movetype = MOVETYPE_STEP;
			
			if ( self.m_bloodColor == 0 )	// Unlike bleeding no blood. They bleed now.
				self.m_bloodColor = BLOOD_COLOR_GREEN;
			
			self.pev.effects = 0;
			
			if ( self.pev.health == 0.0f )
				self.pev.health = HOUND_EXP_HEALTH;
			
			self.pev.yaw_speed = 5;	   //!!! should we put this in the monster's changeanim function since turn rates may vary with state/anim?
			
			self.m_flFieldOfView = 0.5; // indicates the width of this monster's forward view cone ( as a dotproduct result )
			self.m_MonsterState = MONSTERSTATE_NONE;
			m_fDontBlink = false;
			self.m_afCapability = bits_CAP_DOORS_GROUP;
			
			m_iSoundPitch = 120;

			if ( self.IsPlayerAlly() )
			{ 
				SetUse( UseFunction( this.FollowerUse ) );
				self.CanPlayerFollow();
			}
			
			self.MonsterInit();
			
			if ( string( self.m_FormattedName ).IsEmpty() )
				self.m_FormattedName = "Explosive Houndeye";
		}

		//=========================================================
		// Precache - precaches all resources this monster needs
		//=========================================================
		void Precache()
		{
			BaseClass.Precache();
			
			m_iExplosionTexture = g_Game.PrecacheModel( HOUND_EXP_FIREBALL );
			m_iExplosionWaterTexture = g_Game.PrecacheModel( HOUND_EXP_FIREBALL_WATER );
			m_iShockwaveTexture = g_Game.PrecacheModel( HOUND_EXP_SHOCKWAVE );
			
			// Should precache both Classic and Default model.
			if ( string( self.pev.model ).IsEmpty() )
			{
				g_Game.PrecacheModel( HOUND_EXP_MODEL_CLASSIC );
				g_Game.PrecacheModel( HOUND_EXP_MODEL );
			}

			PrecacheSoundArray( pAttackSounds );
			PrecacheSoundArray( pAlertSounds );
			PrecacheSoundArray( pPainSounds );
			PrecacheSoundArray( pDeathSounds );
			PrecacheSoundArray( pIdleSounds );
			PrecacheSoundArray( pSonicWaveSounds );
			PrecacheSoundArray( pHuntSounds );
			PrecacheSoundArray( pExplodeSounds );
		}

		void FollowerUse( CBaseEntity@ pActivator, CBaseEntity@ pCaller, USE_TYPE useType, float flValue )
		{
			self.FollowerPlayerUse( pActivator, pCaller, useType, flValue );
			
			CBaseEntity@ pTarget = self.m_hTargetEnt;
			
			if ( pTarget is pActivator )
				FollowSound();
			else
				StopFollowSound();
		}
		
		int Classify()
		{
			// player ally override
			if ( self.IsPlayerAlly() )
				return CLASS_PLAYER_ALLY;
				
			if ( self.m_fOverrideClass )
				return self.m_iClassSelection;
			
			return CLASS_ALIEN_MONSTER;
		}
		
		int IRelationship(CBaseEntity@ pTarget)
		{
			// Don't hate on the controller exterminator, yet.
			if ( pTarget.pev.classname == "monster_controller_exterminator" )
			{
				return R_AL;
			}
			
			return self.IRelationship( pTarget );
		}

		int ObjectCaps()
		{
			if ( self.IsPlayerAlly() )
				return FCAP_IMPULSE_USE;
			else
				return BaseClass.ObjectCaps();
		}
		
		//=========================================================
		//  FValidateHintType
		//=========================================================
		bool FValidateHintType(int16 sHint)
		{
			int i;
			
			const array<int16> sHoundHints =
			{
				HINT_WORLD_MACHINERY,
				HINT_WORLD_BLINKING_LIGHT,
				HINT_WORLD_HUMAN_BLOOD,
				HINT_WORLD_ALIEN_BLOOD
			};
			
			for ( i = 0; i < sHoundHints.length(); i++ )
			{
				if ( sHoundHints[i] == sHint )
					return true;
			}

			return false;
		}

		//=========================================================
		// CheckRangeAttack1 - overridden for houndeyes so that they
		// try to get within half of their max attack radius before
		// attacking, so as to increase their chances of doing damage.
		//=========================================================
		bool CheckRangeAttack1(float flDot, float flDist)
		{
			if ( flDist <= ( HOUND_EXP_MAX_ATTACK_RADIUS * 0.5 ) && flDot >= 0.3 )
			{
				return true;
			}
			
			return false;
		}

		//=========================================================
		// SetYawSpeed - allows each sequence to have a different
		// turn rate associated with it.
		//=========================================================
		void SetYawSpeed()
		{
			int ys;

			ys = 90;

			switch ( self.m_Activity )
			{
				case ACT_CROUCHIDLE: //sleeping!
					ys = 0;
					break;
				case ACT_IDLE:
					ys = 60;
					break;
				case ACT_WALK:
					ys = 90;
					break;
				case ACT_RUN:
					ys = 90;
					break;
				case ACT_TURN_LEFT:
				case ACT_TURN_RIGHT:
					ys = 90;
					break;
			}

			self.pev.yaw_speed = ys * 2.5; // multiply as monsters in SC tend to have higher yaw speeds than in HL
		}
		
		//=========================================================
		// SetActivity
		//=========================================================
		void SetActivity( Activity NewActivity )
		{
			int iSequence;

			if ( NewActivity == self.m_Activity )
				return;

			if ( self.m_MonsterState == MONSTERSTATE_COMBAT && NewActivity == ACT_IDLE && Math.RandomLong( 0, 1 ) == 0 )
			{
				// play pissed idle.
				iSequence = self.LookupSequence("madidle");

				self.m_Activity = NewActivity; // Go ahead and set this so it doesn't keep trying when the anim is not present

				// In case someone calls this with something other than the ideal activity
				self.m_IdealActivity = self.m_Activity;

				// Set to the desired anim, or default anim if the desired is not present
				if ( iSequence > ACTIVITY_NOT_AVAILABLE )
				{
					self.pev.sequence = iSequence; // Set to the reset anim (if it's there)
					self.pev.frame = 0;			   // FIX: frame counter shouldn't be reset when its the same activity as before
					self.ResetSequenceInfo();
					SetYawSpeed();
				}
			}
			else
			{
				self.SetActivity( NewActivity );
			}
		}

		//=========================================================
		// HandleAnimEvent - catches the monster-specific messages
		// that occur when tagged animation frames are played.
		//=========================================================
		void HandleAnimEvent( MonsterEvent@ pEvent )
		{
			switch ( pEvent.event )
			{
				case HOUND_EXP_AE_WARN:
					// do stuff for this event.
					WarnSound();
					break;
				case HOUND_EXP_AE_STARTATTACK:
					WarmUpSound();
					break;
				case HOUND_EXP_AE_HOPBACK:
				{
					float flGravity = g_EngineFuncs.CVarGetFloat( "sv_gravity" );

					self.pev.flags &= ~FL_ONGROUND;

					self.pev.velocity = g_Engine.v_forward * -200;
					self.pev.velocity.z += ( 0.6 * flGravity ) * 0.5;

					break;
				}
				case HOUND_EXP_AE_THUMP:
					// emit the shockwaves
					ExplosiveAttack();
					break;
				case HOUND_EXP_AE_ANGERSOUND1:
					g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, pPainSounds[2], 1, ATTN_NORM, 0, m_iSoundPitch );
					break;
				case HOUND_EXP_AE_ANGERSOUND2:
					g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, pPainSounds[0], 1, ATTN_NORM, 0, m_iSoundPitch );
					break;
				case HOUND_EXP_AE_CLOSE_EYE:
					if ( !m_fDontBlink )
					{
						self.pev.skin = HOUND_EXP_EYE_FRAMES - 1;
					}
					
					break;
				default:
					BaseClass.HandleAnimEvent( pEvent );
					break;
			}
		}

		void RunAI()
		{
			if ( ( self.m_MonsterState == MONSTERSTATE_IDLE ) && Math.RandomLong( 0, 50 ) == 0 )
				IdleSound();
				
			// first, do base class stuff
			BaseClass.RunAI();
		 
			// this isn't cheap, but this monster is not 
			// usual to be staying alive too much time. 
			// might be removed later.
			Vector vecSrc = self.pev.origin;
			
			NetworkMessage houndExpDynLightB(MSG_PVS, NetworkMessages::SVC_TEMPENTITY, vecSrc);
			houndExpDynLightB.WriteByte( TE_DLIGHT );
			houndExpDynLightB.WriteCoord( vecSrc.x );	// X
			houndExpDynLightB.WriteCoord( vecSrc.y );	// Y
			houndExpDynLightB.WriteCoord( vecSrc.z );	// Z
			houndExpDynLightB.WriteByte( 12 );		// radius * 0.1
			houndExpDynLightB.WriteByte( 224 );		// r
			houndExpDynLightB.WriteByte( 152 );		// g
			houndExpDynLightB.WriteByte( 96 );		// b
			houndExpDynLightB.WriteByte( 1.0 );		// time * 10
			houndExpDynLightB.WriteByte( 0.1 );		// decay * 0.1
			houndExpDynLightB.End();
		}

		//=========================================================
		// FollowSound
		//=========================================================
		void FollowSound()
		{
			g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, pIdleSounds[3], 1, ATTN_NORM, 0, m_iSoundPitch );
		}
		
		//=========================================================
		// StopFollowSound
		//=========================================================
		void StopFollowSound()
		{
			g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, pHuntSounds[3], 1, ATTN_NORM, 0, m_iSoundPitch );
		}
		
		//=========================================================
		// IdleSound
		//=========================================================
		void IdleSound()
		{
			switch ( Math.RandomLong(0, 2) )
			{
				case 0:
					g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, pIdleSounds[0], 1, ATTN_NORM, 0, m_iSoundPitch );
					break;
				case 1:
					g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, pIdleSounds[1], 1, ATTN_NORM, 0, m_iSoundPitch );
					break;
				case 2:
					g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, pIdleSounds[2], 1, ATTN_NORM, 0, m_iSoundPitch );
					break;
			}
		}

		//=========================================================
		// WarmUpSound
		//=========================================================
		void WarmUpSound()
		{
			switch ( Math.RandomLong(0, 1) )
			{
				case 0:
					g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_WEAPON, pAttackSounds[0], 0.7, ATTN_NORM, 0, m_iSoundPitch );
					break;
				case 1:
					g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_WEAPON, pAttackSounds[2], 0.7, ATTN_NORM, 0, m_iSoundPitch );
					break;
			}

			Vector vecSrc = self.pev.origin;
			
			float life;
			life = ( ( 255 - pev.frame ) / ( pev.framerate * self.m_flFrameRate) );
					
			if ( life < 0.1 )
				life = 0.1;
					
			NetworkMessage houndExpCharging( MSG_PAS, NetworkMessages::SVC_TEMPENTITY, vecSrc );
				houndExpCharging.WriteByte( TE_IMPLOSION );
				houndExpCharging.WriteCoord( vecSrc.x );
				houndExpCharging.WriteCoord( vecSrc.y );
				houndExpCharging.WriteCoord( vecSrc.z + 16 );
				houndExpCharging.WriteByte( int( 50 * life + 100 ) );	// radius
				houndExpCharging.WriteByte( int( pev.framerate / 25.0 ) );	// count
				houndExpCharging.WriteByte( int( life * 10 ) );	// life
			houndExpCharging.End();
					
			NetworkMessage houndExpDynLight(MSG_PVS, NetworkMessages::SVC_TEMPENTITY, vecSrc);
			houndExpDynLight.WriteByte( TE_DLIGHT );
			houndExpDynLight.WriteCoord( vecSrc.x );	// X
			houndExpDynLight.WriteCoord( vecSrc.y );	// Y
			houndExpDynLight.WriteCoord( vecSrc.z );	// Z
			houndExpDynLight.WriteByte( 12 );		// radius * 0.1
			houndExpDynLight.WriteByte( 224 );		// r
			houndExpDynLight.WriteByte( 152 );		// g
			houndExpDynLight.WriteByte( 96 );		// b
			houndExpDynLight.WriteByte( 20 / pev.framerate );		// time * 10
			houndExpDynLight.WriteByte( 0 );		// decay * 0.1
			houndExpDynLight.End();
		}
		
		//=========================================================
		// WarnSound
		//=========================================================
		void WarnSound()
		{
			switch ( Math.RandomLong(0, 2) )
			{
				case 0:
					g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, pHuntSounds[0], 1, ATTN_NORM, 0, m_iSoundPitch );
					break;
				case 1:
					g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, pHuntSounds[1], 1, ATTN_NORM, 0, m_iSoundPitch );
					break;
				case 2:
					g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, pHuntSounds[2], 1, ATTN_NORM, 0, m_iSoundPitch );
					break;
			}
		}

		//=========================================================
		// AlertSound
		//=========================================================
		void AlertSound()
		{
			switch ( Math.RandomLong(0, 2) )
			{
				case 0:
					g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, pAlertSounds[0], 1, ATTN_NORM, 0, m_iSoundPitch );
					break;
				case 1:
					g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, pAlertSounds[1], 1, ATTN_NORM, 0, m_iSoundPitch );
					break;
				case 2:
					g_SoundSystem.EmitSoundDyn( self.edict(), CHAN_VOICE, pAlertSounds[2], 1, ATTN_NORM, 0, m_iSoundPitch );	// ATTN_NONE
					break;
			}
		}

		//=========================================================
		// DeathSound
		//=========================================================
		void DeathSound()
		{
			return;
		}

		//=========================================================
		// PainSound
		//=========================================================
		void PainSound()
		{
			return;
		}

		//=========================================================
		// DeathEffect
		//=========================================================
		void DeathEffect()
		{
			// A slightly smaller explosion when killed.
			int iContents = g_EngineFuncs.PointContents( pev.origin );
			int iScale;

			iScale = 50;

			NetworkMessage houndExplode( MSG_PVS, NetworkMessages::SVC_TEMPENTITY, pev.origin );
				houndExplode.WriteByte(TE_EXPLOSION);
				houndExplode.WriteCoord( self.pev.origin.x );
				houndExplode.WriteCoord( self.pev.origin.y );
				houndExplode.WriteCoord( self.pev.origin.z );
				
				if ( iContents != CONTENTS_WATER )
					houndExplode.WriteShort( m_iExplosionTexture );
				else
					houndExplode.WriteShort( m_iExplosionWaterTexture );
				
				houndExplode.WriteByte( iScale );
				houndExplode.WriteByte( 15 );
				houndExplode.WriteByte( TE_EXPLFLAG_NOSOUND ); // No explosion sound, will be handled by a switch-case-default block below due to Stereo Sample issues.
			houndExplode.End();

			// blast circles
			NetworkMessage houndExplodeWave(MSG_PAS, NetworkMessages::SVC_TEMPENTITY, pev.origin);
				houndExplodeWave.WriteByte( TE_BEAMCYLINDER );
				houndExplodeWave.WriteCoord( self.pev.origin.x );
				houndExplodeWave.WriteCoord( self.pev.origin.y );
				houndExplodeWave.WriteCoord( self.pev.origin.z + 16 );
				houndExplodeWave.WriteCoord( self.pev.origin.x );
				houndExplodeWave.WriteCoord( self.pev.origin.y );
				houndExplodeWave.WriteCoord( self.pev.origin.z + int( ( HOUND_EXP_MAX_ATTACK_RADIUS / 0.2f ) * 1.1 ) ); 
				houndExplodeWave.WriteShort( m_iShockwaveTexture );
				houndExplodeWave.WriteByte( 0 ); // startframe
				houndExplodeWave.WriteByte( 0 ); // framerate
				houndExplodeWave.WriteByte( 2 ); // life
				houndExplodeWave.WriteByte( 64 );  // width
				houndExplodeWave.WriteByte( 0 );   // noise
				houndExplodeWave.WriteByte( 255 );
				houndExplodeWave.WriteByte( 255 );
				houndExplodeWave.WriteByte( 192 );
				houndExplodeWave.WriteByte( 255 ); 	//brightness
				houndExplodeWave.WriteByte( 0 );	// speed
			houndExplodeWave.End();
		
			NetworkMessage houndExplodeWaveB(MSG_PAS, NetworkMessages::SVC_TEMPENTITY, pev.origin);
				houndExplodeWaveB.WriteByte( TE_BEAMCYLINDER );
				houndExplodeWaveB.WriteCoord( self.pev.origin.x );
				houndExplodeWaveB.WriteCoord( self.pev.origin.y );
				houndExplodeWaveB.WriteCoord( self.pev.origin.z + 16 );
				houndExplodeWaveB.WriteCoord( self.pev.origin.x );
				houndExplodeWaveB.WriteCoord( self.pev.origin.y );
				houndExplodeWaveB.WriteCoord( self.pev.origin.z + int( ( ( HOUND_EXP_MAX_ATTACK_RADIUS / 2.0f ) / 0.2f ) * 1.1 ) ); 
				houndExplodeWaveB.WriteShort( m_iShockwaveTexture );
				houndExplodeWaveB.WriteByte( 0 ); // startframe
				houndExplodeWaveB.WriteByte( 0 ); // framerate
				houndExplodeWaveB.WriteByte( 2 ); // life
				houndExplodeWaveB.WriteByte( 32 );  // width
				houndExplodeWaveB.WriteByte( 0 );   // noise
				houndExplodeWaveB.WriteByte( 255 );
				houndExplodeWaveB.WriteByte( 255 );
				houndExplodeWaveB.WriteByte( 192 );
				houndExplodeWaveB.WriteByte( 255 ); 	//brightness
				houndExplodeWaveB.WriteByte( 0 );	// speed
			houndExplodeWaveB.End();
			
			switch ( Math.RandomLong( 0, 2 ) )
			{
				case 0: g_SoundSystem.PlaySound( null, CHAN_AUTO, pExplodeSounds[0], 1.0, 0.2, 0, PITCH_NORM, 0, false, pev.origin ); break;
				case 1: g_SoundSystem.PlaySound( null, CHAN_AUTO, pExplodeSounds[1], 1.0, 0.2, 0, PITCH_NORM, 0, false, pev.origin ); break;
				case 2: g_SoundSystem.PlaySound( null, CHAN_AUTO, pExplodeSounds[2], 1.0, 0.2, 0, PITCH_NORM, 0, false, pev.origin ); break;
			}
		
			GetSoundEntInstance().InsertSound( bits_SOUND_COMBAT, pev.origin, NORMAL_EXPLOSION_VOLUME, 3.0f, self ); 
			
			entvars_t@ pevOwner;

			if ( self.pev.owner !is null )
				@pevOwner = self.pev.owner.vars;
			else
				@pevOwner = null;

			g_WeaponFuncs.RadiusDamage( self.pev.origin, self.pev, pevOwner, HOUND_EXP_DAMAGE, HOUND_EXP_RADIUS, CLASS_NONE, DMG_BLAST | DMG_NEVERGIB );

			@pev.owner = null; // can't traceline attack owner if this is set
		}

		//=========================================================
		// ExplosiveAttack
		//=========================================================
		void ExplosiveAttack()
		{
			// A more bigger explosion if you let him attack.
			int iContents = g_EngineFuncs.PointContents( pev.origin );
			int iScale;

			iScale = 50;
			
			NetworkMessage houndExplode( MSG_PVS, NetworkMessages::SVC_TEMPENTITY, pev.origin );
				houndExplode.WriteByte(TE_EXPLOSION);
				houndExplode.WriteCoord( self.pev.origin.x );
				houndExplode.WriteCoord( self.pev.origin.y );
				houndExplode.WriteCoord( self.pev.origin.z );
				
				if ( iContents != CONTENTS_WATER )
					houndExplode.WriteShort( m_iExplosionTexture );
				else
					houndExplode.WriteShort( m_iExplosionWaterTexture );
				
				houndExplode.WriteByte( iScale );
				houndExplode.WriteByte( 15 );
				houndExplode.WriteByte( TE_EXPLFLAG_NOSOUND ); // No explosion sound, will be handled by a switch-case-default block below due to Stereo Sample issues.
			houndExplode.End();
			
			// blast circles
			NetworkMessage houndExplodeWave(MSG_PAS, NetworkMessages::SVC_TEMPENTITY, pev.origin);
				houndExplodeWave.WriteByte( TE_BEAMCYLINDER );
				houndExplodeWave.WriteCoord( self.pev.origin.x );
				houndExplodeWave.WriteCoord( self.pev.origin.y );
				houndExplodeWave.WriteCoord( self.pev.origin.z + 16 );
				houndExplodeWave.WriteCoord( self.pev.origin.x );
				houndExplodeWave.WriteCoord( self.pev.origin.y );
				houndExplodeWave.WriteCoord( self.pev.origin.z + int( ( HOUND_EXP_MAX_ATTACK_RADIUS / 0.2f ) * 1.25 ) ); 
				houndExplodeWave.WriteShort( m_iShockwaveTexture );
				houndExplodeWave.WriteByte( 0 ); // startframe
				houndExplodeWave.WriteByte( 0 ); // framerate
				houndExplodeWave.WriteByte( 2 ); // life
				houndExplodeWave.WriteByte( 64 );  // width
				houndExplodeWave.WriteByte( 0 );   // noise
				houndExplodeWave.WriteByte( 255 );
				houndExplodeWave.WriteByte( 255 );
				houndExplodeWave.WriteByte( 192 );
				houndExplodeWave.WriteByte( 255 ); 	//brightness
				houndExplodeWave.WriteByte( 0 );	// speed
			houndExplodeWave.End();
		
			NetworkMessage houndExplodeWaveB(MSG_PAS, NetworkMessages::SVC_TEMPENTITY, pev.origin);
				houndExplodeWaveB.WriteByte( TE_BEAMCYLINDER );
				houndExplodeWaveB.WriteCoord( self.pev.origin.x );
				houndExplodeWaveB.WriteCoord( self.pev.origin.y );
				houndExplodeWaveB.WriteCoord( self.pev.origin.z + 16 );
				houndExplodeWaveB.WriteCoord( self.pev.origin.x );
				houndExplodeWaveB.WriteCoord( self.pev.origin.y );
				houndExplodeWaveB.WriteCoord( self.pev.origin.z + int( ( ( HOUND_EXP_MAX_ATTACK_RADIUS / 2.0f ) / 0.2f ) * 1.25 ) ); 
				houndExplodeWaveB.WriteShort( m_iShockwaveTexture );
				houndExplodeWaveB.WriteByte( 0 ); // startframe
				houndExplodeWaveB.WriteByte( 0 ); // framerate
				houndExplodeWaveB.WriteByte( 2 ); // life
				houndExplodeWaveB.WriteByte( 32 );  // width
				houndExplodeWaveB.WriteByte( 0 );   // noise
				houndExplodeWaveB.WriteByte( 255 );
				houndExplodeWaveB.WriteByte( 255 );
				houndExplodeWaveB.WriteByte( 192 );
				houndExplodeWaveB.WriteByte( 255 ); 	//brightness
				houndExplodeWaveB.WriteByte( 0 );	// speed
			houndExplodeWaveB.End();
			
			switch ( Math.RandomLong( 0, 2 ) )
			{
				case 0: g_SoundSystem.PlaySound( null, CHAN_AUTO, pExplodeSounds[0], 1.0, 0.2, 0, PITCH_NORM, 0, false, pev.origin ); break;
				case 1: g_SoundSystem.PlaySound( null, CHAN_AUTO, pExplodeSounds[1], 1.0, 0.2, 0, PITCH_NORM, 0, false, pev.origin ); break;
				case 2: g_SoundSystem.PlaySound( null, CHAN_AUTO, pExplodeSounds[2], 1.0, 0.2, 0, PITCH_NORM, 0, false, pev.origin ); break;
			}
			
			GetSoundEntInstance().InsertSound( bits_SOUND_COMBAT, pev.origin, NORMAL_EXPLOSION_VOLUME, 3.0f, self ); 
			
			entvars_t@ pevOwner;

			if ( self.pev.owner !is null )
				@pevOwner = self.pev.owner.vars;
			else
				@pevOwner = null;
			
			g_WeaponFuncs.RadiusDamage( self.pev.origin, self.pev, pevOwner, HOUND_EXP_DAMAGE * 2, HOUND_EXP_RADIUS * 2, CLASS_NONE, DMG_BLAST | DMG_NEVERGIB );
			
			@pev.owner = null; // can't traceline attack owner if this is set
			
			g_EntityFuncs.Remove( self );
		}
		
		int TakeDamage(entvars_t@ pevInflictor, entvars_t@ pevAttacker, float flDamage, int bitsDamageType)
		{
			if ( pevAttacker is null )
				return 0;

			CBaseEntity@ pAttacker = g_EntityFuncs.Instance( pevAttacker );

			if ( self.CheckAttacker( pAttacker ) )
				return 0;
		
			return BaseClass.TakeDamage(pevInflictor, pevAttacker, flDamage, bitsDamageType);
		}
	
		float GetPointsForDamage( float flDamage )
		{
			return ( flDamage / self.pev.max_health ) * ( 1 );
		}
		
		void Killed(entvars_t@ pevAttacker, int iGib)
		{
			// When killed, make him non solid so the player doesn't step
			// in a invisible box.
			self.pev.solid = SOLID_NOT;
			self.pev.renderamt = 0;
			self.pev.rendermode = kRenderTransTexture;
			
			BaseClass.Killed( pevAttacker, GIB_ALWAYS );	// Setting to GIB_ALWAYS causes some problems.
			g_Utility.BloodDrips( self.GetOrigin(), g_vecZero, self.BloodColor(), 80 );
			
			DeathEffect();
			self.TaskComplete();
		}

		void ThinkRemove()
		{
			self.UpdateOnRemove();
			self.pev.flags |= FL_KILLME;
		}
	
		//=========================================================
		// start task
		//=========================================================
		void StartTask( Task@ pTask )
		{
			self.m_iTaskStatus = TASKSTATUS_RUNNING;

			switch( pTask.iTask )
			{
				case TASK_HOUND_EXP_OPEN_EYE:
				{
					m_fDontBlink = false; // turn blinking back on and that code will automatically open the eye
					self.m_iTaskStatus = TASKSTATUS_COMPLETE;
					break;
				}
				
				case TASK_HOUND_EXP_CLOSE_EYE:
				{
					pev.skin = 0;
					m_fDontBlink = true; // tell blink code to leave the eye alone.
					break;
				}
				
				case TASK_HOUND_EXP_THREAT_DISPLAY:
				{
					self.m_IdealActivity = ACT_IDLE_ANGRY;
					break;
				}
				
				case TASK_RANGE_ATTACK1:
				{
					self.m_IdealActivity = ACT_RANGE_ATTACK1;
					break;
				}
				
				case TASK_SPECIAL_ATTACK1:
				{
					self.m_IdealActivity = ACT_SPECIAL_ATTACK1;
					break;
				}
				
				case TASK_GUARD:
				{
					self.m_IdealActivity = ACT_GUARD;
					break;
				}
				
				case TASK_DIE:
				{
					self.m_flWaitFinished = g_Engine.time + 1.6f;
					DeathEffect();
					//[[fallthrough]];	//WTF is this?
				}
				
				default:
				{
					BaseClass.StartTask(pTask);
					break;
				}
			}
		}

		//=========================================================
		// RunTask
		//=========================================================
		void RunTask( Task@ pTask )
		{
			switch( pTask.iTask )
			{
				case TASK_HOUND_EXP_THREAT_DISPLAY:
				{
					self.MakeIdealYaw( self.m_vecEnemyLKP );
					self.ChangeYaw( pev.yaw_speed );

					if ( self.m_fSequenceFinished )
					{
						self.TaskComplete();
					}

					break;
				}
				
				case TASK_HOUND_EXP_CLOSE_EYE:
				{
					if ( self.pev.skin < HOUND_EXP_EYE_FRAMES - 1 )
					{
						self.pev.skin++;
					}
					
					break;
				}
				
				case TASK_HOUND_EXP_HOP_BACK:
				{
					if ( self.m_fSequenceFinished )
					{
						self.TaskComplete();
					}
					
					break;
				}
				
				case TASK_SPECIAL_ATTACK1:
				{
					self.pev.skin = Math.RandomLong( 0, HOUND_EXP_EYE_FRAMES - 1 );

					self.MakeIdealYaw( self.m_vecEnemyLKP );
					self.ChangeYaw( pev.yaw_speed );

					float life;
					life = ( ( 255 - pev.frame ) / ( pev.framerate * self.m_flFrameRate) );
					
					if ( life < 0.1 )
						life = 0.1;
					
					NetworkMessage houndExpCharging( MSG_PAS, NetworkMessages::SVC_TEMPENTITY, self.pev.origin );
						houndExpCharging.WriteByte( TE_IMPLOSION );
						houndExpCharging.WriteCoord( self.pev.origin.x );
						houndExpCharging.WriteCoord( self.pev.origin.y );
						houndExpCharging.WriteCoord( self.pev.origin.z + 16 );
						houndExpCharging.WriteByte( int( 50 * life + 100 ) );	// radius
						houndExpCharging.WriteByte( int( pev.framerate / 25.0 ) );	// count
						houndExpCharging.WriteByte( int( life * 10 ) );	// life
					houndExpCharging.End();

					if ( self.m_fSequenceFinished )
					{
						ExplosiveAttack();
						self.TaskComplete();
					}

					break;
				}
				
				case TASK_DIE:
				{
					if ( g_Engine.time > self.m_flWaitFinished )
					{
						SetThink( ThinkFunction( ThinkRemove ) );
						self.pev.nextthink = g_Engine.time + 0.15f;
					}
					
					break;
				}
				
				default:
				{
					BaseClass.RunTask(pTask);
					break;
				}
			}
		}
		
		//=========================================================
		// PrescheduleThink
		//=========================================================
		void PrescheduleThink()
		{
			// if the hound is mad and is running, make hunt noises.
			if ( self.m_MonsterState == MONSTERSTATE_COMBAT && self.m_Activity == ACT_RUN && Math.RandomLong(0, 1) < 0.2 )
			{
				WarnSound();
			}

			// at random, initiate a blink if not already blinking or sleeping
			if ( !m_fDontBlink )
			{
				if ( ( self.pev.skin == 0 ) && Math.RandomLong( 0, 0x7F ) == 0 )
				{ // start blinking!
					self.pev.skin = HOUND_EXP_EYE_FRAMES - 1;
				}
				else if ( self.pev.skin != 0 )
				{ // already blinking
					self.pev.skin--;
				}
			}
		}
		
		//=========================================================
		// GetScheduleOfType
		//=========================================================
		Schedule@ GetScheduleOfType(int Type)
		{
			switch ( Type )
			{
				case SCHED_RANGE_ATTACK1:
				{
					switch ( Math.RandomLong(0, 1) )
					{
						case 0: return slhoundeyeRangeAttack1; break;
						case 1: return slhoundeyeRangeAttack2; break;
					}
				}
				case SCHED_SPECIAL_ATTACK1:
				{
					return slhoundeyeSpecialAttack1;
				}
				case SCHED_GUARD:
				{
					return slhoundeyeGuardPack;
				}
				case SCHED_HOUND_EXP_AGITATED:
				{
					return slhoundeyeAgitated;
				}
				case SCHED_FAIL:
				{
					if ( self.m_MonsterState == MONSTERSTATE_COMBAT )
					{
						if ( !FNullEnt( g_EngineFuncs.FindClientInPVS( self.edict() ) ) )
						{
							// client in PVS
							return slhoundeyeCombatFailPVS;
						}
						else
						{
							// client has taken off!
							return slhoundeyeCombatFailNoPVS;
						}
					}
					else
					{
						return BaseClass.GetScheduleOfType(Type);
					}
				}
				default:
				{
					return BaseClass.GetScheduleOfType(Type);
				}
			}
		}
		
		//=========================================================
		// GetSchedule
		//=========================================================
		Schedule@ GetSchedule()
		{
			switch ( self.m_MonsterState )
			{
				case MONSTERSTATE_COMBAT:
				{
					// dead enemy
					if ( self.HasConditions( bits_COND_ENEMY_DEAD ) )
					{
						// call base class, all code to handle dead enemies is centralized there.
						return BaseClass.GetSchedule();
					}
					
					break;
				}
			}

			return BaseClass.GetSchedule();
		}
	}

	//=========================================================
	// AI Schedules Specific to this monster
	//=========================================================
	array<ScriptSchedule@>@ monster_houndeye_explosive_schedules;

	ScriptSchedule slhoundeyeGuardPack
	(
			bits_COND_SEE_HATE |
			bits_COND_LIGHT_DAMAGE |
			bits_COND_HEAVY_DAMAGE |
			bits_COND_PROVOKED |
			bits_COND_HEAR_SOUND,
			
			bits_SOUND_COMBAT |
			bits_SOUND_WORLD |
			bits_SOUND_MEAT |
			bits_SOUND_PLAYER,
			"GuardPack"
	);

	ScriptSchedule slhoundeyeRangeAttack1
	(
			bits_COND_LIGHT_DAMAGE |
			bits_COND_HEAVY_DAMAGE,
			0,
			"HoundRangeAttack1"
	);

	ScriptSchedule slhoundeyeRangeAttack2
	(
			bits_COND_LIGHT_DAMAGE |
			bits_COND_HEAVY_DAMAGE,
			0,
			"HoundRangeAttack2"
	);

	ScriptSchedule slhoundeyeSpecialAttack1
	(
			bits_COND_NEW_ENEMY |
			bits_COND_LIGHT_DAMAGE |
			bits_COND_HEAVY_DAMAGE |
			bits_COND_ENEMY_OCCLUDED,
			0,
			"Hound Special Attack1"
	);

	ScriptSchedule slhoundeyeAgitated
	(
			bits_COND_NEW_ENEMY |
			bits_COND_LIGHT_DAMAGE |
			bits_COND_HEAVY_DAMAGE,
			0,
			"Hound Agitated"
	);

	ScriptSchedule slhoundeyeCombatFailPVS
	(
			bits_COND_NEW_ENEMY |
			bits_COND_LIGHT_DAMAGE |
			bits_COND_HEAVY_DAMAGE,
			0,
			"HoundCombatFailPVS"
	);
	
	ScriptSchedule slhoundeyeCombatFailNoPVS
	(
			bits_COND_NEW_ENEMY |
			bits_COND_LIGHT_DAMAGE |
			bits_COND_HEAVY_DAMAGE,
			0,
			"HoundCombatFailNoPVS"
	);
	
	void InitSchedules()
	{
		// Marphy Fact Files Fix - Fix freeze stutter after leaderlook sequence
		slhoundeyeGuardPack.AddTask(ScriptTask(TASK_STOP_MOVING, 0));
		slhoundeyeGuardPack.AddTask(ScriptTask(TASK_PLAY_SEQUENCE, float(ACT_GUARD)));

		slhoundeyeRangeAttack1.AddTask(ScriptTask(TASK_STOP_MOVING, 0));
		slhoundeyeRangeAttack1.AddTask(ScriptTask(TASK_FACE_IDEAL, 0));
		slhoundeyeRangeAttack1.AddTask(ScriptTask(TASK_RANGE_ATTACK1, 0));
		slhoundeyeRangeAttack1.AddTask(ScriptTask(TASK_SET_SCHEDULE, float(SCHED_HOUND_EXP_AGITATED)));
		
		slhoundeyeRangeAttack2.AddTask(ScriptTask(TASK_STOP_MOVING, 0));
		slhoundeyeRangeAttack2.AddTask(ScriptTask(TASK_FACE_IDEAL, 0));
		slhoundeyeRangeAttack2.AddTask(ScriptTask(TASK_RANGE_ATTACK1, 0));
		
		slhoundeyeSpecialAttack1.AddTask(ScriptTask(TASK_STOP_MOVING, 0));
		slhoundeyeSpecialAttack1.AddTask(ScriptTask(TASK_FACE_IDEAL, 0));
		slhoundeyeSpecialAttack1.AddTask(ScriptTask(TASK_SPECIAL_ATTACK1, 0));
		slhoundeyeSpecialAttack1.AddTask(ScriptTask(TASK_PLAY_SEQUENCE, float(ACT_IDLE_ANGRY)));

		slhoundeyeAgitated.AddTask(ScriptTask(TASK_STOP_MOVING, 0));
		slhoundeyeAgitated.AddTask(ScriptTask(TASK_HOUND_EXP_THREAT_DISPLAY, 0));

		slhoundeyeCombatFailPVS.AddTask(ScriptTask(TASK_STOP_MOVING, 0));
		slhoundeyeCombatFailPVS.AddTask(ScriptTask(TASK_HOUND_EXP_THREAT_DISPLAY, 0));
		slhoundeyeCombatFailPVS.AddTask(ScriptTask(TASK_WAIT_FACE_ENEMY, 1));

		slhoundeyeCombatFailNoPVS.AddTask(ScriptTask(TASK_STOP_MOVING, 0));
		slhoundeyeCombatFailNoPVS.AddTask(ScriptTask(TASK_HOUND_EXP_THREAT_DISPLAY, 0));
		slhoundeyeCombatFailNoPVS.AddTask(ScriptTask(TASK_WAIT_FACE_ENEMY, 2));
		slhoundeyeCombatFailNoPVS.AddTask(ScriptTask(TASK_SET_ACTIVITY, float(ACT_IDLE)));
		slhoundeyeCombatFailNoPVS.AddTask(ScriptTask(TASK_WAIT_PVS, 0));

		array<ScriptSchedule@> scheds =
		{
			slhoundeyeGuardPack,
			slhoundeyeRangeAttack1,
			slhoundeyeRangeAttack2,
			slhoundeyeSpecialAttack1,
			slhoundeyeAgitated,
			slhoundeyeCombatFailPVS,
			slhoundeyeCombatFailNoPVS
		};
		
		@monster_houndeye_explosive_schedules = @scheds;
	}
	
	string GetMonsterName()
	{
		return "monster_houndeye_explosive";
	}
	
	void Register()
	{
		InitSchedules();
		g_CustomEntityFuncs.RegisterCustomEntity( "SVEN_HOUNDEYE_EXPLOSIVE::CHoundeyeExplosive", GetMonsterName() );
		g_Game.PrecacheOther( GetMonsterName() );
	}
} // End of Namespace