//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:		Antlion - nasty bug
//				Optimized version for hl2wars
//
//=============================================================================//

#include "cbase.h"
#include "npc_antlion.h"
#include "baseparticleentity.h"
#include "props.h"
#include "particle_parse.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

//==================================================
// CNPC_Antlion::m_DataDesc
//==================================================

class CNPC_AntlionFast : public CAI_BaseAntlionBase
{
public:

	DECLARE_CLASS( CNPC_AntlionFast, CAI_BaseAntlionBase  );
	DECLARE_DATADESC();

	CNPC_AntlionFast( void );

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

	// Thinking, including core thinking, movement, animation
	virtual void		NPCThink( void );
};

BEGIN_DATADESC( CNPC_AntlionFast )
END_DATADESC()

LINK_ENTITY_TO_CLASS( npc_antlion_fast, CNPC_AntlionFast );

#define	ANTLION_MODEL			"models/antlion.mdl"

extern ConVar	sk_antlion_health;

//==================================================
// CNPC_Antlion
//==================================================

CNPC_AntlionFast::CNPC_AntlionFast( void )
{
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CNPC_AntlionFast::Spawn( void )
{
	Precache();

	SetModel( ANTLION_MODEL );
	SetBloodColor( BLOOD_COLOR_ANTLION );

	SetHullType(HULL_MEDIUM);
	SetHullSizeNormal();
	SetDefaultEyeOffset();
	
	SetNavType( NAV_GROUND );

	m_NPCState	= NPC_STATE_NONE;

	m_iHealth	= sk_antlion_health.GetFloat();

	SetSolid( SOLID_BBOX );
	AddSolidFlags( FSOLID_NOT_STANDABLE );

	
	SetMoveType( MOVETYPE_STEP );

	//Only do this if a squadname appears in the entity
	if ( m_SquadName != NULL_STRING )
	{
		CapabilitiesAdd( bits_CAP_SQUAD );
	}

	SetCollisionGroup( HL2COLLISION_GROUP_ANTLION );

	CapabilitiesAdd( bits_CAP_MOVE_GROUND | bits_CAP_MOVE_JUMP | bits_CAP_INNATE_MELEE_ATTACK1 | bits_CAP_INNATE_MELEE_ATTACK2 );

	// JAY: Optimize these out for now
	CapabilitiesAdd( bits_CAP_SKIP_NAV_GROUND_CHECK );

	NPCInit();

	// Antlions will always pursue
	m_flDistTooFar = FLT_MAX;

	BaseClass::Spawn();

	m_nSkin = random->RandomInt( 0, ANTLION_SKIN_COUNT-1 );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CNPC_AntlionFast::Activate( void )
{
	BaseClass::Activate();
}

// Use all the gibs
#define	NUM_ANTLION_GIBS_UNIQUE	3
extern const char *pszAntlionGibs_Unique[];

#define	NUM_ANTLION_GIBS_MEDIUM	3
extern const char *pszAntlionGibs_Medium[];

// XBox doesn't use the smaller gibs, so don't cache them
#define	NUM_ANTLION_GIBS_SMALL	3
extern const char *pszAntlionGibs_Small[];
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CNPC_AntlionFast::Precache( void )
{
	{
		PrecacheModel( ANTLION_MODEL );
		PropBreakablePrecacheAll( MAKE_STRING( ANTLION_MODEL ) );
		PrecacheParticleSystem( "blood_impact_antlion_01" );
		PrecacheParticleSystem( "AntlionGib" );
	}

	for ( int i = 0; i < NUM_ANTLION_GIBS_UNIQUE; ++i )
	{
		PrecacheModel( pszAntlionGibs_Unique[ i ] );
	}
	for ( int i = 0; i < NUM_ANTLION_GIBS_MEDIUM; ++i )
	{
		PrecacheModel( pszAntlionGibs_Medium[ i ] );
	}
	for ( int i = 0; i < NUM_ANTLION_GIBS_SMALL; ++i )
	{
		PrecacheModel( pszAntlionGibs_Small[ i ] );
	}

	PrecacheScriptSound( "NPC_Antlion.RunOverByVehicle" );
	PrecacheScriptSound( "NPC_Antlion.MeleeAttack" );
	PrecacheScriptSound( "NPC_Antlion.BurrowIn" );
	PrecacheScriptSound( "NPC_Antlion.BurrowOut" );
	PrecacheScriptSound( "NPC_Antlion.FootstepSoft" );
	PrecacheScriptSound( "NPC_Antlion.FootstepHeavy" );
	PrecacheScriptSound( "NPC_Antlion.MeleeAttackSingle" );
	PrecacheScriptSound( "NPC_Antlion.MeleeAttackDouble" );
	PrecacheScriptSound( "NPC_Antlion.Distracted" );
	PrecacheScriptSound( "NPC_Antlion.Idle" );
	PrecacheScriptSound( "NPC_Antlion.Pain" );
	PrecacheScriptSound( "NPC_Antlion.Land" );
	PrecacheScriptSound( "NPC_Antlion.WingsOpen" );
	PrecacheScriptSound( "NPC_Antlion.LoopingAgitated" );
	PrecacheScriptSound( "NPC_Antlion.Distracted" );

#ifdef HL2_EPISODIC
	PrecacheScriptSound( "NPC_Antlion.PoisonBurstScream" );
	PrecacheScriptSound( "NPC_Antlion.PoisonBurstScreamSubmerged" );
	PrecacheScriptSound( "NPC_Antlion.PoisonBurstExplode" );
	PrecacheScriptSound( "NPC_Antlion.MeleeAttack_Muffled" );
	PrecacheScriptSound( "NPC_Antlion.TrappedMetal" );
	PrecacheScriptSound( "NPC_Antlion.ZappedFlip" );
	PrecacheScriptSound( "NPC_Antlion.PoisonShoot" );
	PrecacheScriptSound( "NPC_Antlion.PoisonBall" );
#endif

	BaseClass::Precache();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CNPC_AntlionFast::NPCThink( void )
{
	if ( GetEfficiency() < AIE_DORMANT && GetSleepState() == AISS_AWAKE )
	{
				if ( m_flNextDecisionTime <= gpGlobals->curtime )
				{
					bRanDecision = true;
					m_ScheduleState.bTaskRanAutomovement = false;
					m_ScheduleState.bTaskUpdatedYaw = false;
					RunAI();
				}
				else
				{
					if ( m_ScheduleState.bTaskRanAutomovement )
						AutoMovement();
					if ( m_ScheduleState.bTaskUpdatedYaw )
						GetMotor()->UpdateYaw();
				}

				PostRun();

				PerformMovement();

				m_bIsMoving = IsMoving();

				PostMovement();

				SetSimulationTime( gpGlobals->curtime );
	}

	SetNextThink( gpGlobals->curtime + .25 );
}