#include maps\mp\_utility;
#include common_scripts\utility;

lasersight_think()
{
	self endon( "death" );
	self endon( "disconnect" );
	self endon( "faux_spawn" );
	
	self.laser_on = undefined;
	self.wasEMP = false;
	
	while( true )
	{
		
		has_laser = false;
		
		attachment_list = GetWeaponAttachments( self GetCurrentWeapon() );
		
		if( isDefined( attachment_list ) )
		{
			foreach( attachment in attachment_list )
			{
				if( attachment == "lasersight" )
				{
					has_laser = true;
					break;
				}
			}
		}
		
		// Handling for EMP


		while( self isEMPed() && has_laser)
		{
			wait( 0.05);
			self LaserOff();
			self.wasEMP = true;
			continue;
		}
		if ( self.wasEMP && has_laser)
		{
			self.wasEMP = false;
			if( self maps\mp\gametypes\_class::isExoXMG( self GetCurrentWeapon() ) || self maps\mp\gametypes\_class::isSac3( self GetCurrentWeapon() ) )
				self LaserOn( "mp_attachment_lasersight_short" );
			else
				self LaserOn( "mp_attachment_lasersight" );
		}

		
		// Adding a special case for the LSR launcher. It should have a laser on even though it doesn't have a laser attachment.
		if ( IsSubStr( self GetCurrentWeapon(), "maaws" ) )
		{
			has_laser = true;
		}
		
		if ( has_laser && self IsThrowingGrenade() )
		{
			if( isDefined( self.laser_on ) && self.laser_on )
			{
				self LaserOff();
				self.laser_on = false;
				
				// god help us, all of this is required to get the timing correct...
				
				// IsUsingOffhand() turns on the moment the viewmodel starts holding the grenade weapon
				// IsThrowingGrenade() is here to ensure we don't get stuck if the player cancels the grenade process
				while ( !self IsUsingOffhand() && self IsThrowingGrenade() )
					wait( 0.05 );
				
				// IsUsingOffhand() turns off the moment the viewmodel starts holding the primary weapon again
				// IsThrowingGrenade() is here to ensure we don't get stuck if the player cancels the grenade process
				while ( self IsUsingOffhand() && self IsThrowingGrenade() )
					wait( 0.05 );

				// IsThrowingGrenade() turns off the moment the primary weapon is usable again
				while ( self IsThrowingGrenade() )
					wait( 0.05 );
				
				if( self maps\mp\gametypes\_class::isExoXMG( self GetCurrentWeapon() ) || self maps\mp\gametypes\_class::isSac3( self GetCurrentWeapon() ) )
					self LaserOn( "mp_attachment_lasersight_short" );
				else
					self LaserOn( "mp_attachment_lasersight" );
				
				self.laser_on = true;
				

			}
		}
		
		if ( !has_laser )
		{
			if( isDefined( self.laser_on ) && self.laser_on )
			{
				self LaserOff();
				self.laser_on = false;
			}
		}
		else
		{
			if( !isDefined( self.laser_on ) || !self.laser_on )
			{
				if( self maps\mp\gametypes\_class::isExoXMG( self GetCurrentWeapon() ) || self maps\mp\gametypes\_class::isSac3( self GetCurrentWeapon() ) )
					self LaserOn( "mp_attachment_lasersight_short" );
				else
					self LaserOn( "mp_attachment_lasersight" );
		
				self.laser_on = true;
			}
		}
		
		
		wait( 0.05 );
	}	
}