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

weapon_plasmagun.script

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

#define PLASMAGUN_FIRERATE			0.125 //changed by Tim
#define PLASMAGUN_LOWAMMO			10
#define PLASMAGUN_NUMPROJECTILES	1

// blend times
#define PLASMAGUN_IDLE_TO_LOWER		4
#define PLASMAGUN_IDLE_TO_FIRE		1
#define	PLASMAGUN_IDLE_TO_RELOAD	4
#define PLASMAGUN_RAISE_TO_IDLE		4
#define PLASMAGUN_FIRE_TO_IDLE		4
#define PLASMAGUN_RELOAD_TO_IDLE	4

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

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

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

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

void weapon_plasmagun::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", PLASMAGUN_IDLE_TO_LOWER );
		}
		currentTime = sys.getTime();
		ammoClip = ammoInClip();
		if ( ( currentTime >= next_attack ) && WEAPON_ATTACK ) {
			if ( ammoClip > 0 ) {
				weaponState( "Fire", PLASMAGUN_IDLE_TO_FIRE );
			} else if ( ammoAvailable() > 0 ) {
				if ( autoReload() ) {
					netReload();
					weaponState( "Reload", PLASMAGUN_IDLE_TO_RELOAD );
				}
			}
		}
		if ( WEAPON_RELOAD && ( ammoAvailable() > ammoClip ) && ( ammoClip < clip_size ) ) {
			netReload();
			weaponState( "Reload", PLASMAGUN_IDLE_TO_RELOAD );
		}
		if ( WEAPON_NETRELOAD ) {
			WEAPON_NETRELOAD = false;
			weaponState( "Reload", PLASMAGUN_IDLE_TO_RELOAD );			
		}
		waitFrame();
	}
}

void weapon_plasmagun::Fire() {
	float ammoClip;
	float currentTime;
	
	next_attack = sys.getTime() + PLASMAGUN_FIRERATE;
	
	ammoClip = ammoInClip();
	if ( ammoClip == PLASMAGUN_LOWAMMO ) {
		startSound( "snd_lowammo", SND_CHANNEL_ITEM, true );
	}

	launchProjectiles( PLASMAGUN_NUMPROJECTILES, spread, 0, 1.0, 1.0 );
	playAnim( ANIMCHANNEL_ALL, "fire" );
	while( !animDone( ANIMCHANNEL_ALL, PLASMAGUN_FIRE_TO_IDLE ) ) {
		currentTime = sys.getTime();
		ammoClip = ammoInClip();
		if ( ( currentTime >= next_attack ) && WEAPON_ATTACK && ( ammoClip > 0 ) ) {
			weaponState( "Fire", 0 );
		}
		waitFrame();
	}

	weaponState( "Idle", PLASMAGUN_FIRE_TO_IDLE );
}

void weapon_plasmagun::Reload() {
	weaponReloading();
	playAnim( ANIMCHANNEL_ALL, "reload" );
	waitUntil( animDone( ANIMCHANNEL_ALL, PLASMAGUN_RELOAD_TO_IDLE ) );
	addToClip( clipSize() );
	weaponState( "Idle", PLASMAGUN_RELOAD_TO_IDLE );
}

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