//=========== (C) Copyright 2000 Valve, L.L.C. All rights reserved. ===========
//
// The copyright to the contents herein is the property of Valve, L.L.C.
// The contents may be used and/or copied only with the written permission of
// Valve, L.L.C., or in accordance with the terms and conditions stipulated in
// the agreement/contract under which the contents have been supplied.
//
// Purpose: Bullseyes act as targets for other NPC's to attack and to trigger
//			events 
//
// $Workfile:     $
// $Date:         $
//
//-----------------------------------------------------------------------------
// $Log: $
//
// $NoKeywords: $
//=============================================================================

#include	"cbase.h"
#include	"AI_Default.h"
#include	"AI_Task.h"
#include	"AI_Schedule.h"
#include	"AI_Node.h"
#include	"AI_Hull.h"
#include	"AI_Hint.h"
#include	"AI_Navigator.h"
#include	"AI_Route.h"
#include	"AI_Squad.h"
#include	"AI_SquadSlot.h"
#include	"soundent.h"
#include	"game.h"
#include	"NPCEvent.h"
#include	"EntityList.h"
#include	"activitylist.h"
#include	"animation.h"
#include	"basecombatweapon.h"
#include	"IEffects.h"
#include	"vstdlib/random.h"
#include	"engine/IEngineSound.h"
#include	"ammodef.h"
#include    "te.h"
#include "hl1_ai_basenpc.h"

ConVar sk_agrunt_health( "sk_agrunt_health", "0" );
ConVar sk_agrunt_dmg_punch( "sk_agrunt_dmg_punch", "0" );

//=========================================================
// Monster's Anim Events Go Here
//=========================================================
#define		AGRUNT_AE_HORNET1	( 1 )
#define		AGRUNT_AE_HORNET2	( 2 )
#define		AGRUNT_AE_HORNET3	( 3 )
#define		AGRUNT_AE_HORNET4	( 4 )
#define		AGRUNT_AE_HORNET5	( 5 )
// some events are set up in the QC file that aren't recognized by the code yet.
#define		AGRUNT_AE_PUNCH		( 6 )
#define		AGRUNT_AE_BITE		( 7 )

#define		AGRUNT_AE_LEFT_FOOT	 ( 10 )
#define		AGRUNT_AE_RIGHT_FOOT ( 11 )

#define		AGRUNT_AE_LEFT_PUNCH ( 12 )
#define		AGRUNT_AE_RIGHT_PUNCH ( 13 )

#define		AGRUNT_MELEE_DIST	100

int iAgruntMuzzleFlash;
int ACT_THREAT_DISPLAY;

// -----------------------------------------------
//	> Squad slots
// -----------------------------------------------
enum AGruntSquadSlot_T
{	
	AGRUNT_SQUAD_SLOT_HORNET1 = LAST_SHARED_SQUADSLOT,
	AGRUNT_SQUAD_SLOT_HORNET2,
	AGRUNT_SQUAD_SLOT_CHASE,
};

enum
{
	SCHED_AGRUNT_FAIL = LAST_SHARED_SCHEDULE,
	SCHED_AGRUNT_COMBAT_FAIL,
	SCHED_AGRUNT_STANDOFF,
	SCHED_AGRUNT_SUPPRESS_HORNET,
	SCHED_AGRUNT_RANGE_ATTACK,
	SCHED_AGRUNT_HIDDEN_RANGE_ATTACK,
	SCHED_AGRUNT_TAKE_COVER_FROM_ENEMY,
	SCHED_AGRUNT_VICTORY_DANCE,
	SCHED_AGRUNT_THREAT_DISPLAY,
};

//=========================================================
// monster-specific tasks
//=========================================================
enum 
{
	TASK_AGRUNT_SETUP_HIDE_ATTACK = LAST_SHARED_TASK,
	TASK_AGRUNT_GET_PATH_TO_ENEMY_CORPSE,
};


class CNPC_AlienGrunt : public CHL1BaseNPC
{
	DECLARE_CLASS( CNPC_AlienGrunt, CHL1BaseNPC );
public:

	void Spawn( void );
	void Precache( void );

	static const char *pAttackHitSounds[];
	static const char *pAttackMissSounds[];
	static const char *pAttackSounds[];
	static const char *pDieSounds[];
	static const char *pPainSounds[];
	static const char *pIdleSounds[];
	static const char *pAlertSounds[];
	
	float	MaxYawSpeed( void );
	Class_T Classify ( void ){ return CLASS_ALIEN_MILITARY;	}
	int  GetSoundInterests ( void );
	void HandleAnimEvent( animevent_t *pEvent );

	void AlertSound( void );
	void DeathSound ( void );
	void PainSound ( void );
	void AttackSound ( void );

	bool ShouldSpeak( void );
	void PrescheduleThink ( void );
	
	bool FCanCheckAttacks ( void );

	int MeleeAttack1Conditions ( float flDot, float flDist );
	int RangeAttack1Conditions ( float flDot, float flDist );
	
	void StopTalking ( void );

	void StartTask( const Task_t *pTask );
	int TranslateSchedule( int scheduleType ); //GetScheduleOfType
	int SelectSchedule( void ); // GetSchedule

	void SetObjectCollisionBox( void )
	{
		SetAbsMins( GetAbsOrigin() + Vector( -32, -32, 0 ) );
		SetAbsMaxs( GetAbsOrigin() + Vector( 32, 32, 85 ) );
	}

	void	TraceAttack( const CTakeDamageInfo &info, const Vector &vecDir, trace_t *ptr );
	int		IRelationPriority( CBaseEntity *pTarget );
/*
	int IRelationship( CBaseEntity *pTarget );
*/
public:
	DECLARE_DATADESC();
	DEFINE_CUSTOM_AI;
	
	bool	m_fCanHornetAttack;
	float	m_flNextHornetAttackCheck;

	float	m_flNextPainTime;

	// three hacky fields for speech stuff. These don't really need to be saved.
	float	m_flNextSpeakTime;
	float	m_flNextWordTime;
	int		m_iLastWord;
	float   m_flDamageTime;
};

LINK_ENTITY_TO_CLASS( monster_alien_grunt, CNPC_AlienGrunt );

BEGIN_DATADESC( CNPC_AlienGrunt )
	DEFINE_FIELD( CNPC_AlienGrunt, m_fCanHornetAttack, FIELD_BOOLEAN ),
	DEFINE_FIELD( CNPC_AlienGrunt, m_flNextHornetAttackCheck, FIELD_TIME ),
	DEFINE_FIELD( CNPC_AlienGrunt, m_flNextPainTime, FIELD_TIME ),
	DEFINE_FIELD( CNPC_AlienGrunt, m_flNextSpeakTime, FIELD_TIME ),
	DEFINE_FIELD( CNPC_AlienGrunt, m_flNextWordTime, FIELD_TIME ),
	DEFINE_FIELD( CNPC_AlienGrunt, m_iLastWord, FIELD_INTEGER ),
END_DATADESC()

const char *CNPC_AlienGrunt::pAttackHitSounds[] = 
{
	"zombie/claw_strike1.wav",
	"zombie/claw_strike2.wav",
	"zombie/claw_strike3.wav",
};

const char *CNPC_AlienGrunt::pAttackMissSounds[] = 
{
	"zombie/claw_miss1.wav",
	"zombie/claw_miss2.wav",
};

const char *CNPC_AlienGrunt::pAttackSounds[] =
{
	"agrunt/ag_attack1.wav",
	"agrunt/ag_attack2.wav",
	"agrunt/ag_attack3.wav",
};

const char *CNPC_AlienGrunt::pDieSounds[] =
{
	"agrunt/ag_die1.wav",
	"agrunt/ag_die4.wav",
	"agrunt/ag_die5.wav",
};

const char *CNPC_AlienGrunt::pPainSounds[] =
{
	"agrunt/ag_pain1.wav",
	"agrunt/ag_pain2.wav",
	"agrunt/ag_pain3.wav",
	"agrunt/ag_pain4.wav",
	"agrunt/ag_pain5.wav",
};

const char *CNPC_AlienGrunt::pIdleSounds[] =
{
	"agrunt/ag_idle1.wav",
	"agrunt/ag_idle2.wav",
	"agrunt/ag_idle3.wav",
	"agrunt/ag_idle4.wav",
};

const char *CNPC_AlienGrunt::pAlertSounds[] =
{
	"agrunt/ag_alert1.wav",
	"agrunt/ag_alert3.wav",
	"agrunt/ag_alert4.wav",
	"agrunt/ag_alert5.wav",
};

int CNPC_AlienGrunt::IRelationPriority( CBaseEntity *pTarget )
{
	//I hate grunts more than anything.
	if ( pTarget->Classify() == CLASS_HUMAN_MILITARY )
	{
		if ( FClassnameIs( pTarget, "monster_human_grunt" ) )
		{
			 return ( BaseClass::IRelationPriority ( pTarget ) + 1 );
		}
	}

	return BaseClass::IRelationPriority( pTarget );
}

void CNPC_AlienGrunt::Spawn()
{
	Precache();

	SetModel( "models/agrunt.mdl");
	UTIL_SetSize( this, Vector( -32, -32, 0 ), Vector( 32, 32, 64 ) );

	SetSolid( SOLID_BBOX );
	AddSolidFlags( FSOLID_NOT_STANDABLE );
	SetMoveType( MOVETYPE_STEP );
	m_bloodColor		= BLOOD_COLOR_GREEN;
	m_fEffects			= 0;
	m_iHealth			= sk_agrunt_health.GetFloat();
	m_flFieldOfView		= 0.2;// indicates the width of this monster's forward view cone ( as a dotproduct result )
	m_NPCState			= NPC_STATE_NONE;

	CapabilitiesClear();
	CapabilitiesAdd ( bits_CAP_SQUAD | bits_CAP_MOVE_GROUND );

	CapabilitiesAdd(bits_CAP_INNATE_RANGE_ATTACK1 );

	// Innate range attack for kicking
	CapabilitiesAdd(bits_CAP_INNATE_MELEE_ATTACK1 );

	m_HackedGunPos		= Vector( 24, 64, 48 );

	m_flNextSpeakTime	= m_flNextWordTime = gpGlobals->curtime + 10 + random->RandomInt( 0, 10 );

	SetHullType(HULL_WIDE_HUMAN);
	SetHullSizeNormal();	

	SetRenderColor( 255, 255, 255, 255 );
	
	NPCInit();
	
	BaseClass::Spawn();
}

//=========================================================
// Precache - precaches all resources this monster needs
//=========================================================
void CNPC_AlienGrunt::Precache()
{
	int i;

	engine->PrecacheModel("models/agrunt.mdl");

	for ( i = 0; i < ARRAYSIZE( pAttackHitSounds ); i++ )
		enginesound->PrecacheSound((char *)pAttackHitSounds[i]);

	for ( i = 0; i < ARRAYSIZE( pAttackMissSounds ); i++ )
		enginesound->PrecacheSound((char *)pAttackMissSounds[i]);

	for ( i = 0; i < ARRAYSIZE( pIdleSounds ); i++ )
		enginesound->PrecacheSound((char *)pIdleSounds[i]);

	for ( i = 0; i < ARRAYSIZE( pDieSounds ); i++ )
		enginesound->PrecacheSound((char *)pDieSounds[i]);

	for ( i = 0; i < ARRAYSIZE( pPainSounds ); i++ )
		enginesound->PrecacheSound((char *)pPainSounds[i]);

	for ( i = 0; i < ARRAYSIZE( pAttackSounds ); i++ )
		enginesound->PrecacheSound((char *)pAttackSounds[i]);

	for ( i = 0; i < ARRAYSIZE( pAlertSounds ); i++ )
		enginesound->PrecacheSound((char *)pAlertSounds[i]);

	enginesound->PrecacheSound( "player/pl_ladder1.wav" );
	enginesound->PrecacheSound( "player/pl_ladder2.wav" );
	enginesound->PrecacheSound( "player/pl_ladder3.wav" );
	enginesound->PrecacheSound( "player/pl_ladder4.wav" );

	enginesound->PrecacheSound( "hassault/hw_shoot1.wav" );

	iAgruntMuzzleFlash = engine->PrecacheModel( "sprites/muz4.vmt" );

	UTIL_PrecacheOther( "hornet" );
}	

float CNPC_AlienGrunt::MaxYawSpeed( void )
{
	float ys;

	switch ( GetActivity() )
	{
	case ACT_TURN_LEFT:
	case ACT_TURN_RIGHT:
		ys = 110;
		break;
	default:
		ys = 100;
	}

	return ys;
}

int CNPC_AlienGrunt::GetSoundInterests ( void )
{
	return	SOUND_WORLD	|
			SOUND_COMBAT	|
			SOUND_PLAYER	|
			SOUND_DANGER;
}

//=========================================================
// HandleAnimEvent - catches the monster-specific messages
// that occur when tagged animation frames are played.
//
// Returns number of events handled, 0 if none.
//=========================================================
void CNPC_AlienGrunt::HandleAnimEvent( animevent_t *pEvent )
{
	switch( pEvent->event )
	{
	case AGRUNT_AE_HORNET1:
	case AGRUNT_AE_HORNET2:
	case AGRUNT_AE_HORNET3:
	case AGRUNT_AE_HORNET4:
	case AGRUNT_AE_HORNET5:
		{
			// m_vecEnemyLKP should be center of enemy body
			Vector vecArmPos;
			QAngle angArmDir;
			Vector vecDirToEnemy;
			QAngle angDir;

			if (HasCondition( COND_SEE_ENEMY))
			{
				Vector vecEnemyLKP = GetEnemy()->GetAbsOrigin();

				vecDirToEnemy = ( ( vecEnemyLKP ) - GetAbsOrigin() );
				VectorAngles( vecDirToEnemy, angDir );
				VectorNormalize( vecDirToEnemy );
			}
			else
			{
				angDir = GetAbsAngles();
				angDir.x = -angDir.x;

				Vector vForward;
				AngleVectors( angDir, &vForward );
				vecDirToEnemy = vForward;
			}

			m_fEffects = EF_MUZZLEFLASH;

			// make angles +-180
			if (angDir.x > 180)
			{
				angDir.x = angDir.x - 360;
			}

		//	SetBlending( 0, angDir.x );
			GetAttachment( "0", vecArmPos, angArmDir );

			vecArmPos = vecArmPos + vecDirToEnemy * 32;
		
			CPVSFilter filter( GetAbsOrigin() );
			te->Sprite( filter, 0.0,
				&vecArmPos, iAgruntMuzzleFlash, random->RandomFloat( 0.4, 0.8 ), 128 );

			CBaseEntity *pHornet = CBaseEntity::Create( "hornet", vecArmPos, QAngle( 0, 0, 0 ), this );

			Vector vForward;
			AngleVectors( angDir, &vForward );
	
			pHornet->SetAbsVelocity( vForward * 300 );
			pHornet->SetOwnerEntity( this );
			
			EmitSound( "Weapon_Hornetgun.Single" );

			CHL1BaseNPC *pHornetMonster = (CHL1BaseNPC *)pHornet->MyNPCPointer();

			if ( pHornetMonster )
			{
				pHornetMonster->SetEnemy( GetEnemy() );
			}
		}
		break;

	case AGRUNT_AE_LEFT_FOOT:
		// left foot
		{
			CPASAttenuationFilter filter2( this );
			switch ( random->RandomInt(0,1) )
			{
			case 0:	enginesound->EmitSound( filter2, entindex(), CHAN_BODY, "player/pl_ladder2.wav", 1, ATTN_NORM, 0, 70 );	break;
			case 1:	enginesound->EmitSound( filter2, entindex(), CHAN_BODY, "player/pl_ladder4.wav", 1, ATTN_NORM, 0, 70 );	break;
			}
		}
		break;
	case AGRUNT_AE_RIGHT_FOOT:
		// right foot
		{
			CPASAttenuationFilter filter3( this );
			switch ( random->RandomInt(0,1))
			{
			case 0:	enginesound->EmitSound( filter3, entindex(), CHAN_BODY, "player/pl_ladder1.wav", 1, ATTN_NORM, 0, 70 );	break;
			case 1:	enginesound->EmitSound( filter3, entindex(), CHAN_BODY, "player/pl_ladder3.wav", 1, ATTN_NORM, 0 ,70);	break;
			}
		}
		break;

	case AGRUNT_AE_LEFT_PUNCH:
		{
			Vector vecMins = GetHullMins();
			Vector vecMaxs = GetHullMaxs();
			vecMins.z = vecMins.x;
			vecMaxs.z = vecMaxs.x;

			CBaseEntity *pHurt = CheckTraceHullAttack( AGRUNT_MELEE_DIST, vecMins, vecMaxs, sk_agrunt_dmg_punch.GetFloat(), DMG_CLUB );
			CPASAttenuationFilter filter4( this );
			
			if ( pHurt )
			{
				if ( pHurt->GetFlags() & ( FL_NPC | FL_CLIENT ) )
					 pHurt->ViewPunch( QAngle( -25, 8, 0) );

				Vector vRight;
				AngleVectors( GetAbsAngles(), NULL, &vRight, NULL );

				// OK to use gpGlobals without calling MakeVectors, cause CheckTraceHullAttack called it above.
				if ( pHurt->IsPlayer() )
				{
					// this is a player. Knock him around.
					pHurt->SetAbsVelocity( pHurt->GetAbsVelocity() + vRight * 250 );
				}

				enginesound->EmitSound(filter4, entindex(), CHAN_WEAPON, pAttackHitSounds[ random->RandomInt(0,ARRAYSIZE(pAttackHitSounds)-1) ], 1.0, ATTN_NORM, 0, 100 + random->RandomInt(-5,5) );

				Vector vecArmPos;
				QAngle angArmAng;
				GetAttachment( 0, vecArmPos, angArmAng );
				SpawnBlood(vecArmPos, pHurt->BloodColor(), 25);// a little surface blood.
			}
			else
			{
				// Play a random attack miss sound
				enginesound->EmitSound(filter4, entindex(), CHAN_WEAPON, pAttackMissSounds[ random->RandomInt(0,ARRAYSIZE(pAttackMissSounds)-1) ], 1.0, ATTN_NORM, 0, 100 + random->RandomInt(-5,5) );
			}
		}
		break;

	case AGRUNT_AE_RIGHT_PUNCH:
		{
			Vector vecMins = GetHullMins();
			Vector vecMaxs = GetHullMaxs();
			vecMins.z = vecMins.x;
			vecMaxs.z = vecMaxs.x;

			CBaseEntity *pHurt = CheckTraceHullAttack( AGRUNT_MELEE_DIST, vecMins, vecMaxs, sk_agrunt_dmg_punch.GetFloat(), DMG_CLUB );
			CPASAttenuationFilter filter5( this );
				
			if ( pHurt )
			{
				if ( pHurt->GetFlags() & ( FL_NPC | FL_CLIENT ) )
					 pHurt->ViewPunch( QAngle( 25, 8, 0) );

				// OK to use gpGlobals without calling MakeVectors, cause CheckTraceHullAttack called it above.
				if ( pHurt->IsPlayer() )
				{
					// this is a player. Knock him around.
					Vector vRight;
					AngleVectors( GetAbsAngles(), NULL, &vRight, NULL );
					pHurt->SetAbsVelocity( pHurt->GetAbsVelocity() + vRight * -250 );
				}

				enginesound->EmitSound( filter5, entindex(), CHAN_WEAPON, pAttackHitSounds[ random->RandomInt(0,ARRAYSIZE(pAttackHitSounds)-1) ], 1.0, ATTN_NORM, 0, 100 + random->RandomInt(-5,5) );

				Vector vecArmPos;
				QAngle angArmAng;
				GetAttachment( 0, vecArmPos, angArmAng );
				SpawnBlood(vecArmPos, pHurt->BloodColor(), 25);// a little surface blood.
			}
			else
			{
				// Play a random attack miss sound
				enginesound->EmitSound( filter5, entindex(), CHAN_WEAPON, pAttackMissSounds[ random->RandomInt(0,ARRAYSIZE(pAttackMissSounds)-1) ], 1.0, ATTN_NORM, 0, 100 + random->RandomInt(-5,5) );
			}
		}
		break;

	default:
		BaseClass::HandleAnimEvent( pEvent );
		break;
	}
}


//=========================================================
// DieSound
//=========================================================
void CNPC_AlienGrunt::DeathSound ( void )
{
	StopTalking();

	CPASAttenuationFilter filter( this );
	enginesound->EmitSound( filter, entindex(), CHAN_VOICE, pDieSounds[ random->RandomInt(0,ARRAYSIZE(pDieSounds)-1)], 1.0, ATTN_NORM );
}

//=========================================================
// AlertSound
//=========================================================
void CNPC_AlienGrunt::AlertSound ( void )
{
	StopTalking();

	CPASAttenuationFilter filter( this );
	enginesound->EmitSound( filter, entindex(), CHAN_VOICE, pAlertSounds[random->RandomInt(0,ARRAYSIZE(pAlertSounds)-1)], 1.0, ATTN_NORM );
}

//=========================================================
// AttackSound
//=========================================================
void CNPC_AlienGrunt::AttackSound ( void )
{
	StopTalking();

	CPASAttenuationFilter filter( this );
	enginesound->EmitSound( filter, entindex(), CHAN_VOICE, pAttackSounds[random->RandomInt(0,ARRAYSIZE(pAttackSounds)-1)], 1.0, ATTN_NORM );
}

//=========================================================
// PainSound
//=========================================================
void CNPC_AlienGrunt::PainSound ( void )
{
	if ( m_flNextPainTime > gpGlobals->curtime )
	{
		return;
	}

	m_flNextPainTime = gpGlobals->curtime + 0.6;

	StopTalking();

	CPASAttenuationFilter filter( this );
	enginesound->EmitSound( filter, entindex(), CHAN_VOICE, pPainSounds[random->RandomInt(0,ARRAYSIZE(pPainSounds)-1)], 1.0, ATTN_NORM );
}

//=========================================================
// ShouldSpeak - Should this agrunt be talking?
//=========================================================
bool CNPC_AlienGrunt::ShouldSpeak( void )
{
	if ( m_flNextSpeakTime > gpGlobals->curtime )
	{
		// my time to talk is still in the future.
		return FALSE;
	}

	if ( m_spawnflags & SF_NPC_GAG )
	{
		if ( m_NPCState != NPC_STATE_COMBAT )
		{
			// if gagged, don't talk outside of combat.
			// if not going to talk because of this, put the talk time 
			// into the future a bit, so we don't talk immediately after 
			// going into combat
			m_flNextSpeakTime = gpGlobals->curtime + 3;
			return FALSE;
		}
	}

	return TRUE;
}

//=========================================================
// PrescheduleThink 
//=========================================================
void CNPC_AlienGrunt::PrescheduleThink ( void )
{
	BaseClass::PrescheduleThink();
	
	if ( ShouldSpeak() )
	{
		if ( m_flNextWordTime < gpGlobals->curtime )
		{
			int num = -1;

			do
			{
				num = random->RandomInt( 0,ARRAYSIZE(pIdleSounds)-1 );
			} while( num == m_iLastWord );

			m_iLastWord = num;

			// play a new sound
			CPASAttenuationFilter filter( this );
			enginesound->EmitSound( filter, entindex(), CHAN_VOICE, pIdleSounds[ num ], 1.0, ATTN_NORM );

			// is this word our last?
			if ( random->RandomInt( 1, 10 ) <= 1 )
			{
				// stop talking.
				StopTalking();
			}
			else
			{
				m_flNextWordTime = gpGlobals->curtime + random->RandomFloat( 0.5, 1 );
			}
		}
	}
}

//=========================================================
// FCanCheckAttacks - this is overridden for alien grunts
// because they can use their smart weapons against unseen
// enemies. Base class doesn't attack anyone it can't see.
//=========================================================
bool CNPC_AlienGrunt::FCanCheckAttacks ( void )
{
	if ( !HasCondition( COND_ENEMY_TOO_FAR ) )
		  return true;
	else
		  return false;
}

//=========================================================
// CheckMeleeAttack1 - alien grunts zap the crap out of 
// any enemy that gets too close. 
//=========================================================
int CNPC_AlienGrunt::MeleeAttack1Conditions ( float flDot, float flDist )
{
	if ( flDist > AGRUNT_MELEE_DIST )
		 return COND_NONE;

	if ( flDot < 0.6 )
		 return COND_NONE;

	if ( HasCondition ( COND_SEE_ENEMY ) && GetEnemy() != NULL )
		 return COND_CAN_MELEE_ATTACK1;

	return COND_NONE;
}

//=========================================================
// CheckRangeAttack1 
//
// !!!LATER - we may want to load balance this. Several
// tracelines are done, so we may not want to do this every
// server frame. Definitely not while firing. 
//=========================================================
int CNPC_AlienGrunt::RangeAttack1Conditions ( float flDot, float flDist )
{
	if ( gpGlobals->curtime < m_flNextHornetAttackCheck )
	{
		if ( HasCondition( COND_SEE_ENEMY ) )
		{
			if ( m_fCanHornetAttack == true )
			{
				 return COND_CAN_RANGE_ATTACK1;
			}
			else
			{
				return COND_NONE;
			}
		}
		else
			return COND_NONE;
	}

	if ( flDist < AGRUNT_MELEE_DIST )
		 return COND_NONE;

	if ( flDist > 1024 )
		 return COND_NONE;

	if ( flDot < 0.5 )
		 return COND_NONE;
	
	if ( HasCondition( COND_SEE_ENEMY ) )
	{
		trace_t tr;
		Vector	vecArmPos;
		QAngle	angArmDir;
		
		// verify that a shot fired from the gun will hit the enemy before the world.
		// !!!LATER - we may wish to do something different for projectile weapons as opposed to instant-hit
		GetAttachment( "0", vecArmPos, angArmDir );
		UTIL_TraceLine( vecArmPos, GetEnemy()->BodyTarget( vecArmPos ), MASK_SOLID, this, COLLISION_GROUP_NONE, &tr);
		
		if ( tr.fraction == 1.0 || tr.m_pEnt == GetEnemy() )
		{
			m_flNextHornetAttackCheck = gpGlobals->curtime + random->RandomFloat( 2, 5 );
			m_fCanHornetAttack = true;
			
			return COND_CAN_RANGE_ATTACK1;
		}
	}
	
	m_flNextHornetAttackCheck = gpGlobals->curtime + 0.2;// don't check for half second if this check wasn't successful
	m_fCanHornetAttack = false;
	return COND_NONE;
}

//=========================================================
// StopTalking - won't speak again for 10-20 seconds.
//=========================================================
void CNPC_AlienGrunt::StopTalking( void )
{
	m_flNextWordTime = m_flNextSpeakTime = gpGlobals->curtime + 10 + random->RandomInt(0, 10);
}


void CNPC_AlienGrunt::StartTask ( const Task_t *pTask )
{
	switch ( pTask->iTask )
	{
	case TASK_AGRUNT_GET_PATH_TO_ENEMY_CORPSE:
		{
			Vector forward;
			AngleVectors( GetAbsAngles(), &forward );
			Vector flEnemyLKP = GetEnemyLKP();

			GetNavigator()->SetGoal( flEnemyLKP - forward * 64, AIN_CLEAR_TARGET);

			if ( GetNavigator()->SetGoal( flEnemyLKP - forward * 64, AIN_CLEAR_TARGET) )
			{
				TaskComplete();
			}
			else
			{
				Msg ( "AGruntGetPathToEnemyCorpse failed!!\n" );
				TaskFail( FAIL_NO_ROUTE );
			}
		}
		break;

	case TASK_AGRUNT_SETUP_HIDE_ATTACK:
		// alien grunt shoots hornets back out into the open from a concealed location. 
		// try to find a spot to throw that gives the smart weapon a good chance of finding the enemy.
		// ideally, this spot is along a line that is perpendicular to a line drawn from the agrunt to the enemy.

		CHL1BaseNPC	*pEnemyMonsterPtr;

		pEnemyMonsterPtr = (CHL1BaseNPC *)GetEnemy()->MyNPCPointer();

		if ( pEnemyMonsterPtr )
		{
			Vector		vecCenter, vForward, vRight, vecEnemyLKP;
			QAngle		angTmp;
			trace_t		tr;
			BOOL		fSkip;

			fSkip = FALSE;
			vecCenter = WorldSpaceCenter();
			vecEnemyLKP = GetEnemyLKP();

			VectorAngles( vecEnemyLKP - GetAbsOrigin(), angTmp );
			SetAbsAngles( angTmp );
			AngleVectors( GetAbsAngles(), &vForward, &vRight, NULL );

			UTIL_TraceLine( WorldSpaceCenter() + vForward * 128, vecEnemyLKP, MASK_SOLID_BRUSHONLY, this, COLLISION_GROUP_NONE, &tr);
			if ( tr.fraction == 1.0 )
			{
				GetMotor()->SetIdealYawToTargetAndUpdate ( GetAbsOrigin() + vRight * 128 );
				fSkip = TRUE;
				TaskComplete();
			}
			
			if ( !fSkip )
			{
				UTIL_TraceLine( WorldSpaceCenter() - vForward * 128, vecEnemyLKP, MASK_SOLID_BRUSHONLY, this, COLLISION_GROUP_NONE, &tr);
				if ( tr.fraction == 1.0 )
				{
					GetMotor()->SetIdealYawToTargetAndUpdate ( GetAbsOrigin() - vRight * 128 );
					fSkip = TRUE;
					TaskComplete();
				}
			}
			
			if ( !fSkip )
			{
				UTIL_TraceLine( WorldSpaceCenter() + vForward * 256, vecEnemyLKP, MASK_SOLID_BRUSHONLY, this, COLLISION_GROUP_NONE, &tr);
				if ( tr.fraction == 1.0 )
				{
					GetMotor()->SetIdealYawToTargetAndUpdate ( GetAbsOrigin() + vRight * 256 );
					fSkip = TRUE;
					TaskComplete();
				}
			}
			
			if ( !fSkip )
			{
				UTIL_TraceLine( WorldSpaceCenter() - vForward * 256, vecEnemyLKP, MASK_SOLID_BRUSHONLY, this, COLLISION_GROUP_NONE, &tr);
				if ( tr.fraction == 1.0 )
				{
					GetMotor()->SetIdealYawToTargetAndUpdate ( GetAbsOrigin() - vRight * 256 );
					fSkip = TRUE;
					TaskComplete();
				}
			}
			
			if ( !fSkip )
			{
				TaskFail( FAIL_NO_COVER );
			}
		}
		else
		{
			Msg ( "AGRunt - no enemy monster ptr!!!\n" );
			TaskFail( FAIL_NO_ENEMY );
		}
		break;

	default:
		BaseClass::StartTask ( pTask );
		break;
	}
}

//=========================================================
// GetSchedule - Decides which type of schedule best suits
// the monster's current state and conditions. Then calls
// monster's member function to get a pointer to a schedule
// of the proper type.
//=========================================================
int CNPC_AlienGrunt::SelectSchedule( void )		
{
	if ( HasCondition( COND_HEAR_DANGER ) )
	{
		return SCHED_TAKE_COVER_FROM_BEST_SOUND;
	}

	switch	( m_NPCState )
	{
	case NPC_STATE_COMBAT:
		{
			// dead enemy
			if ( HasCondition( COND_ENEMY_DEAD ) )
			{
				// call base class, all code to handle dead enemies is centralized there.
				return BaseClass::SelectSchedule();
			}

			if ( HasCondition( COND_NEW_ENEMY) )
			{
				return SCHED_WAKE_ANGRY;
			}

			// zap player!
			if ( HasCondition ( COND_CAN_MELEE_ATTACK1 ) )
			{
				AttackSound();// this is a total hack. Should be parto f the schedule
				return SCHED_MELEE_ATTACK1;
			}

			if ( HasCondition ( COND_HEAVY_DAMAGE ) )
			{
				return SCHED_SMALL_FLINCH;
			}

			// can attack
			if ( HasCondition ( COND_CAN_RANGE_ATTACK1 ) && OccupyStrategySlotRange( AGRUNT_SQUAD_SLOT_HORNET1, AGRUNT_SQUAD_SLOT_HORNET2 ) )
			{
				return SCHED_RANGE_ATTACK1;
			}

			if ( OccupyStrategySlot ( AGRUNT_SQUAD_SLOT_CHASE ) )
			{
				return SCHED_CHASE_ENEMY;
			}

			return  SCHED_STANDOFF;
		}
	}

	return BaseClass::SelectSchedule();
}

int CNPC_AlienGrunt::TranslateSchedule( int scheduleType )
{
	switch	( scheduleType )
	{
	case SCHED_TAKE_COVER_FROM_ENEMY:
			return SCHED_AGRUNT_TAKE_COVER_FROM_ENEMY;
		break;
	
	/*case SCHED_RANGE_ATTACK1:
			if ( HasCondition( COND_SEE_ENEMY ) )
				return SCHED_AGRUNT_RANGE_ATTACK;
		//	else 
		//		return SCHED_AGRUNT_HIDDEN_RANGE_ATTACK;
		break;*/

	case SCHED_STANDOFF:
		return SCHED_AGRUNT_STANDOFF;
		break;

	case SCHED_VICTORY_DANCE:
		return SCHED_AGRUNT_VICTORY_DANCE;
		break;
	case SCHED_FAIL:
		// no fail schedule specified, so pick a good generic one.
		{
			if ( GetEnemy() != NULL )
			{
				// I have an enemy
				// !!!LATER - what if this enemy is really far away and i'm chasing him?
				// this schedule will make me stop, face his last known position for 2 
				// seconds, and then try to move again
				return SCHED_AGRUNT_COMBAT_FAIL;
			}

			return SCHED_AGRUNT_FAIL;
		}
		break;
	}
	
	return BaseClass::TranslateSchedule( scheduleType );
}

void CNPC_AlienGrunt::TraceAttack( const CTakeDamageInfo &info, const Vector &vecDir, trace_t *ptr )
{
	CTakeDamageInfo ainfo = info;
	
	float flDamage = ainfo.GetDamage();
	
	if ( ptr->hitgroup == 10 && (ainfo.GetDamageType() & (DMG_BULLET | DMG_SLASH | DMG_CLUB)))
	{
		// hit armor
		if ( m_flDamageTime != gpGlobals->curtime || (random->RandomInt(0,10) < 1) )
		{
			CPVSFilter filter( ptr->endpos );
			te->ArmorRicochet( filter, 0.0, &ptr->endpos, &ptr->plane.normal );
			m_flDamageTime = gpGlobals->curtime;
		}

		if ( random->RandomInt( 0, 1 ) == 0 )
		{
			Vector vecTracerDir = vecDir;

			vecTracerDir.x += random->RandomFloat( -0.3, 0.3 );
			vecTracerDir.y += random->RandomFloat( -0.3, 0.3 );
			vecTracerDir.z += random->RandomFloat( -0.3, 0.3 );

			vecTracerDir = vecTracerDir * -512;

			Vector vEndPos = ptr->endpos + vecTracerDir;

			UTIL_Tracer( ptr->endpos, vEndPos, ENTINDEX( edict() ) );
		}

		flDamage -= 20;
		if (flDamage <= 0)
			flDamage = 0.1;// don't hurt the monster much, but allow bits_COND_LIGHT_DAMAGE to be generated

		ainfo.SetDamage( flDamage );
	}
	else
	{
		SpawnBlood( ptr->endpos, BloodColor(), flDamage);// a little surface blood.
		TraceBleed( flDamage, vecDir, ptr, ainfo.GetDamageType() );
	}

	AddMultiDamage( ainfo, this );
}


//=========================================================
// AI Schedules Specific to this monster
//=========================================================
AI_BEGIN_CUSTOM_NPC( monster_alien_grunt, CNPC_AlienGrunt )

	DECLARE_ACTIVITY( ACT_THREAT_DISPLAY )

	DECLARE_TASK ( TASK_AGRUNT_SETUP_HIDE_ATTACK )
	DECLARE_TASK ( TASK_AGRUNT_GET_PATH_TO_ENEMY_CORPSE )

	DECLARE_SQUADSLOT( AGRUNT_SQUAD_SLOT_HORNET1 )
	DECLARE_SQUADSLOT( AGRUNT_SQUAD_SLOT_HORNET2 )
	DECLARE_SQUADSLOT( AGRUNT_SQUAD_SLOT_CHASE )

	//=========================================================
	// Fail Schedule
	//=========================================================
	DEFINE_SCHEDULE
	(
		SCHED_AGRUNT_FAIL,
			"	Tasks"
			"	TASK_STOP_MOVING			0"
			"	TASK_SET_ACTIVITY			ACTIVITY:ACT_IDLE"
			"	TASK_WAIT					2"
			"	TASK_WAIT_PVS				0"
			"	"
			"	Interrupts"
			"	COND_CAN_RANGE_ATTACK1"
			"	COND_CAN_MELEE_ATTACK1"
	)

	//=========================================================
	// Combat Fail Schedule
	//=========================================================
	DEFINE_SCHEDULE
	(
		SCHED_AGRUNT_COMBAT_FAIL,
			"	Tasks"
			"	TASK_STOP_MOVING			0"
			"	TASK_SET_ACTIVITY			ACTIVITY:ACT_IDLE"
			"	TASK_WAIT_FACE_ENEMY		2"
			"	TASK_WAIT_PVS				0"
			"	"
			"	Interrupts"
			"	COND_CAN_RANGE_ATTACK1"
			"	COND_CAN_MELEE_ATTACK1"
	)

	//=========================================================
	// Standoff schedule. Used in combat when a monster is 
	// hiding in cover or the enemy has moved out of sight. 
	// Should we look around in this schedule?
	//=========================================================
	DEFINE_SCHEDULE
	(
		SCHED_AGRUNT_STANDOFF,
		
			"	Tasks"
			"		TASK_STOP_MOVING			0"
			"		TASK_SET_ACTIVITY			ACTIVITY:ACT_IDLE"
			"		TASK_WAIT_FACE_ENEMY		2"
			"	"
			"	Interrupts"
			"		COND_CAN_RANGE_ATTACK1"
			"		COND_CAN_MELEE_ATTACK1"
			"		COND_SEE_ENEMY"
			"		COND_NEW_ENEMY"
			"		COND_HEAR_DANGER"
	)

	//=========================================================
	// Suppress
	//=========================================================
	DEFINE_SCHEDULE
	(
		SCHED_AGRUNT_SUPPRESS_HORNET,
			"	Tasks"
			"	TASK_STOP_MOVING			0"
			"	TASK_RANGE_ATTACK1			0"
	)

	//=========================================================
	// primary range attacks
	//=========================================================
	DEFINE_SCHEDULE
	(
		SCHED_AGRUNT_RANGE_ATTACK,
			"	Tasks"
			"	TASK_STOP_MOVING			0"
			"	TASK_FACE_ENEMY				0"
			"	TASK_RANGE_ATTACK1			0"
			"	"
			"	Interrupts"
			"	COND_NEW_ENEMY"
			"	COND_ENEMY_DEAD"
			"	COND_HEAVY_DAMAGE"
	)

	DEFINE_SCHEDULE
	(
		SCHED_AGRUNT_HIDDEN_RANGE_ATTACK,
			"	Tasks"
			"	TASK_SET_FAIL_SCHEDULE			SCHEDULE:SCHED_AGRUNT_STANDOFF"
			"	TASK_AGRUNT_SETUP_HIDE_ATTACK		0"
			"	TASK_STOP_MOVING					0"
			"	TASK_FACE_IDEAL						0"
			"	TASK_RANGE_ATTACK1_NOTURN			0"
			"	"
			"	Interrupts"
			"	COND_NEW_ENEMY"
			"	COND_HEAVY_DAMAGE"
			"	COND_HEAR_DANGER"
	)

	//=========================================================
	// Take cover from enemy! Tries lateral cover before node 
	// cover! 
	//=========================================================
	DEFINE_SCHEDULE
	(
		SCHED_AGRUNT_TAKE_COVER_FROM_ENEMY,
			"	Tasks"
			"	TASK_STOP_MOVING				0"
			"	TASK_WAIT						0.2"
			"	TASK_FIND_COVER_FROM_ENEMY		0"
			"	TASK_RUN_PATH					0"
			"	TASK_WAIT_FOR_MOVEMENT			0"
			"	TASK_REMEMBER					MEMORY:INCOVER"
			"	TASK_FACE_ENEMY					0"	
			"	"
			"	Interrupts"
			"	COND_NEW_ENEMY"
	)

	//=========================================================
	// Victory dance!
	//=========================================================
	DEFINE_SCHEDULE
	(
		SCHED_AGRUNT_VICTORY_DANCE,
			"	Tasks"
			"	TASK_STOP_MOVING							0"
			"	TASK_SET_FAIL_SCHEDULE						SCHEDULE:SCHED_AGRUNT_THREAT_DISPLAY"
			"	TASK_WAIT									0.2"
			"	TASK_AGRUNT_GET_PATH_TO_ENEMY_CORPSE		0"
			"	TASK_WALK_PATH								0"
			"	TASK_WAIT_FOR_MOVEMENT						0"
			"	TASK_FACE_ENEMY								0"
			"	TASK_PLAY_SEQUENCE							ACTIVITY:ACT_CROUCH"
			"	TASK_PLAY_SEQUENCE							ACTIVITY:ACT_VICTORY_DANCE"
			"	TASK_PLAY_SEQUENCE							ACTIVITY:ACT_VICTORY_DANCE"
			"	TASK_PLAY_SEQUENCE							ACTIVITY:ACT_STAND"
			"	TASK_PLAY_SEQUENCE							ACTIVITY:ACT_THREAT_DISPLAY"
			"	TASK_PLAY_SEQUENCE							ACTIVITY:ACT_CROUCH"
			"	TASK_PLAY_SEQUENCE							ACTIVITY:ACT_VICTORY_DANCE"
			"	TASK_PLAY_SEQUENCE							ACTIVITY:ACT_VICTORY_DANCE"
			"	TASK_PLAY_SEQUENCE							ACTIVITY:ACT_VICTORY_DANCE"
			"	TASK_PLAY_SEQUENCE							ACTIVITY:ACT_VICTORY_DANCE"
			"	TASK_PLAY_SEQUENCE							ACTIVITY:ACT_VICTORY_DANCE"
			"	TASK_PLAY_SEQUENCE							ACTIVITY:ACT_STAND"
			"	"
			"	Interrupts"
			"	COND_NEW_ENEMY"
			"	COND_LIGHT_DAMAGE"
			"	COND_HEAVY_DAMAGE"
	)

	//=========================================================
	//=========================================================
	DEFINE_SCHEDULE
	(
		SCHED_AGRUNT_THREAT_DISPLAY,
			"	Tasks"
			"	TASK_STOP_MOVING		0"
			"	TASK_FACE_ENEMY			0"
			"	TASK_PLAY_SEQUENCE		ACTIVITY:ACT_THREAT_DISPLAY"
			"	"
			"	Interrupts"
			"	COND_NEW_ENEMY"
			"	COND_LIGHT_DAMAGE"
			"	COND_HEAVY_DAMAGE"
			"	COND_HEAR_PLAYER"
			"	COND_HEAR_COMBAT"
			"	COND_HEAR_WORLD"
	)

AI_END_CUSTOM_NPC()
