/***********************************************************************

weapon_shotgun.script

***********************************************************************/

#define SHOTGUN_FIRERATE		1.333
#define SHOTGUN_LOWAMMO			2
#define SHOTGUN_RELOADRATE		2
#define	SHOTGUN_NUMPROJECTILES	13

// blend times
#define SHOTGUN_IDLE_TO_IDLE	0
#define SHOTGUN_IDLE_TO_LOWER	4
#define SHOTGUN_IDLE_TO_FIRE	1
#define	SHOTGUN_IDLE_TO_RELOAD	4
#define	SHOTGUN_IDLE_TO_NOAMMO	4
#define SHOTGUN_NOAMMO_TO_RELOAD 4
#define SHOTGUN_NOAMMO_TO_IDLE	4
#define SHOTGUN_RAISE_TO_IDLE	1
#define SHOTGUN_FIRE_TO_IDLE	4
#define SHOTGUN_RELOAD_TO_IDLE	4
#define	SHOTGUN_RELOAD_TO_FIRE	4
#define SHOTGUN_RELOAD_TO_LOWER 2

object weapon_shotgun : weapon_base {
	float		next_attack;
	float		spread;
	
	void		init();
	
	void		Lower();
	void		Raise();
	void		Idle();
	void		NoAmmo();
	void		Fire();
	void		Reload();
	void		ExitCinematic();
};

void weapon_shotgun::init() {
	next_attack = 0;
	spread		= getFloatKey( "spread" );
	weaponState( "Raise", 0 );
}

void weapon_shotgun::Raise() {
	weaponRising();
	playAnim( ANIMCHANNEL_ALL, "raise" );
	waitUntil( animDone( ANIMCHANNEL_ALL, SHOTGUN_RAISE_TO_IDLE ) );
	weaponState( "Idle", SHOTGUN_RAISE_TO_IDLE );
}

void weapon_shotgun::Lower() {
	weaponLowering();
	playAnim( ANIMCHANNEL_ALL, "putaway" );
	waitUntil( animDone( ANIMCHANNEL_ALL, 0 ) );
	weaponHolstered();
	waitUntil( WEAPON_RAISEWEAPON );
	weaponState( "Raise", 0 );
}

void weapon_shotgun::Idle() {
	float currentTime;
	float ammoClip;
	float avail;
	float clip_size;
	
	clip_size = clipSize();	
	
	if ( !ammoInClip() ) {
		weaponOutOfAmmo();
	} else {
		weaponReady();
	}
	playCycle( ANIMCHANNEL_ALL, "idle" );
	while( 1 ) {
		if ( WEAPON_LOWERWEAPON ) {
			weaponState( "Lower", SHOTGUN_IDLE_TO_LOWER );
		}
		currentTime = sys.getTime();
		ammoClip = ammoInClip();
		if ( ( currentTime >= next_attack ) && WEAPON_ATTACK ) {
			if ( ammoClip > 0 ) {
				weaponState( "Fire", SHOTGUN_IDLE_TO_FIRE );
			} else if ( ammoAvailable() > 0 ) {
				if ( autoReload() ) {
					netReload();
					weaponState( "Reload", SHOTGUN_IDLE_TO_RELOAD );
				} else {
					weaponState( "NoAmmo", SHOTGUN_IDLE_TO_NOAMMO );
				}
			} else {
				weaponState( "NoAmmo", SHOTGUN_IDLE_TO_NOAMMO );
			}
		}
		if ( WEAPON_RELOAD && ( ammoAvailable() > ammoClip ) && ( ammoClip < clip_size ) ) {
			netReload();
			weaponState( "Reload", SHOTGUN_IDLE_TO_RELOAD );
		}
		if ( WEAPON_NETRELOAD ) {
			WEAPON_NETRELOAD = false;
			weaponState( "Reload", SHOTGUN_IDLE_TO_RELOAD );			
		}
		waitFrame();
	}
}

void weapon_shotgun::NoAmmo() {

	if ( WEAPON_NETRELOAD ) {
		WEAPON_NETRELOAD = false;
		weaponState( "Reload", SHOTGUN_IDLE_TO_RELOAD );			
	}

	playAnim( ANIMCHANNEL_ALL, "noammo" );
	waitUntil( animDone( ANIMCHANNEL_ALL, SHOTGUN_NOAMMO_TO_IDLE ) );
	weaponState( "Idle", SHOTGUN_NOAMMO_TO_IDLE );
}

void weapon_shotgun::Fire() {
	float ammoClip;

	if ( WEAPON_NETRELOAD ) {
		WEAPON_NETRELOAD = false;
		weaponState( "Reload", SHOTGUN_IDLE_TO_RELOAD );			
	}

	next_attack = sys.getTime() + SHOTGUN_FIRERATE;
	
	ammoClip = ammoInClip();
	if ( ammoClip == SHOTGUN_LOWAMMO ) {
		startSound( "snd_lowammo", SND_CHANNEL_ITEM, true );
	}
	
	launchProjectiles( SHOTGUN_NUMPROJECTILES, spread, 0, 1.0, 1.0 );
	playAnim( ANIMCHANNEL_ALL, "fire" );
	waitUntil( animDone( ANIMCHANNEL_ALL, SHOTGUN_FIRE_TO_IDLE ) );
	weaponState( "Idle", SHOTGUN_FIRE_TO_IDLE );
}

void weapon_shotgun::Reload() {
	float ammoClip;
	float ammoAvail;
	float clip_size;
	
	clip_size = clipSize();	
	
	weaponReloading();
	playAnim( ANIMCHANNEL_ALL, "reload_start" );
	waitUntil( animDone( ANIMCHANNEL_ALL, 0 ) );
	
	ammoAvail = ammoAvailable();
	ammoClip = ammoInClip();
	while( ( ammoClip < clip_size ) && ( ammoClip < ammoAvail ) ) {
		weaponReloading();
		playAnim( ANIMCHANNEL_ALL, "reload_loop" );
		while( !animDone( ANIMCHANNEL_ALL, 0 ) ) {
			// allow the player to shoot or switch weapons since shotgun is so slow reloading
			if ( WEAPON_ATTACK && ( ammoClip > 0 ) ) {
				break;
			}
			if ( WEAPON_LOWERWEAPON || WEAPON_NETENDRELOAD ) {
				break;
			}
			waitFrame();
		}
		if ( !animDone( ANIMCHANNEL_ALL, 0 ) ) {
			break;
		}
		addToClip( SHOTGUN_RELOADRATE );
		weaponReady();
		waitFrame();
		if ( WEAPON_ATTACK || WEAPON_LOWERWEAPON || WEAPON_NETENDRELOAD ) {
			// allow the player to shoot or switch weapons since shotgun is so slow reloading
			break;
		}
		ammoAvail = ammoAvailable();
		ammoClip = ammoInClip();
	}
	netEndReload();
	playAnim( ANIMCHANNEL_ALL, "reload_end" );
	waitFrame();

	while( !animDone( ANIMCHANNEL_ALL, SHOTGUN_RELOAD_TO_IDLE ) ) {
		if ( WEAPON_LOWERWEAPON ) {
			weaponState( "Lower", SHOTGUN_RELOAD_TO_LOWER );
		}
		if ( WEAPON_ATTACK ) {
			weaponState( "Fire", SHOTGUN_RELOAD_TO_FIRE );
		}
		waitFrame();
	}

	weaponState( "Idle", SHOTGUN_RELOAD_TO_IDLE );
}

void weapon_shotgun::ExitCinematic() {
	next_attack = 0;
	weaponState( "Idle", 0 );
}
