#using scripts\codescripts\struct;

#using scripts\shared\callbacks_shared;
#using scripts\shared\clientfield_shared;
#using scripts\shared\filter_shared;
#using scripts\shared\util_shared;

#using scripts\shared\abilities\_ability_player;
#using scripts\shared\abilities\_ability_power;
#using scripts\shared\abilities\_ability_util;

    	   	                                                                                                                                         	                                	                                                                                                                                                                                                                                                                                                                                                                    	        	     	              	    	   	               	                                              	                                                           	                               	                     	                                                                                                           	                                                                 	                                                              	                                                                                                                                             	                            	                                     	                                       	                                                                                                              	   	                  	       	                                                    	                   	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            	                                                                                                 	                                    	                                                                                        	           	                        	                                            	                                             	                                                   	                                                             	                                                         	                                                                    	                                                                                                                                                                                                                                	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  	    	         	                                                              	                                                          	                                   	                                   	                                                    	                                       	                                           	     	                                                                                                                                                                                                                                                      	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         	                                                               	        	                                                                                                                              	                                                                                                                     	                               	                                                      	                          
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         	   	                                                                                                                                                                                                                                  	                                                                                                                                                                                                                                                                                                                                                                                                                                                   

#using scripts\shared\system_shared;
#using scripts\shared\visionset_mgr_shared;
#using scripts\shared\postfx_shared;
#using scripts\shared\lui_shared;

                              	                               	                    	                                 	                                        	                            	                                                                  	                             













	
#precache( "client_fx", "player/fx_plyr_ability_screen_blur_overdrive" );



function autoexec __init__sytem__() {     system::register("gadget_overdrive",&__init__,undefined,undefined);    }

function __init__()
{
	callback::on_localclient_connect( &on_player_connect );
	callback::on_localplayer_spawned( &on_localplayer_spawned );
	callback::on_localclient_shutdown( &on_localplayer_shutdown );

	clientfield::register( "toplayer", "overdrive_state", 1, 1, "int", &player_overdrive_handler, !true, true);
	
	visionset_mgr::register_visionset_info( "overdrive", 1, 15, undefined, "overdrive_initialize" );
}

function on_localplayer_shutdown(localClientNum)
{
	self overdrive_shutdown( localClientNum );
}


function on_localplayer_spawned( localClientNum )
{
	if( self != GetLocalPlayer( localClientNum ) )
		return;

	filter::init_filter_overdrive(self);
	filter::disable_filter_overdrive( self,3 );
	DisableSpeedBlur( localClientNum );
}

function on_player_connect( local_client_num )
{
	
}

function player_overdrive_handler(localClientNum, oldVal, newVal, bNewEnt, bInitialSnap, fieldName, bWasTimeJump)
{
	if ( !self IsLocalPlayer() || IsSpectating( localClientNum, false ) || ( (isdefined(level.localPlayers[localClientNum])) && (self GetEntityNumber() != level.localPlayers[localClientNum] GetEntityNumber())) )
	{
		return;
	}
	
	if( (newVal != oldval) && newval ) 
	{
	   	EnableSpeedBlur( localClientNum, GetDvarFloat( "scr_overdrive_amount", 0.15 ), GetDvarFloat( "scr_overdrive_inner_radius", 0.6 ), GetDvarFloat( "scr_overdrive_outer_radius", 1 ), GetDvarInt( "scr_overdrive_velShouldScale", 1 ),GetDvarInt( "scr_overdrive_velScale", 220 ));
		filter::enable_filter_overdrive( self, 3 );
		self UseAlternateAimParams();
		self thread activation_flash(localClientNum);
		self boost_fx_on_velocity(localClientNum);
	}
	else if ( (newVal != oldval) && !newval )
	{
		self overdrive_shutdown( localClientNum );
	}
}


// Flashes the screen white on activation.
function activation_flash(localClientNum)
{
	self notify("activation_flash");
	self endon("activation_flash");
	self endon("death");
	self endon("entityshutdown");
	self endon("stop_player_fx");
	self endon("disable_cybercom");
	           
	self.whiteFlashFade = 1;
	lui::screen_fade( GetDvarFloat("scr_overdrive_flash_fade_in_time", 0.075), GetDvarFloat("scr_overdrive_flash_alpha", 0.7), 0, "white" );
	wait GetDvarFloat("scr_overdrive_flash_fade_in_time", 0.075);
	lui::screen_fade( GetDvarFloat("scr_overdrive_flash_fade_out_time", 0.45), 0, GetDvarFloat("scr_overdrive_flash_alpha", 0.7), "white" );
	self.whiteFlashFade = undefined;
}

// Turn on the "hyperdrive" boost effects on camera
function enable_boost_camera_fx(localClientNum)
{
	if (isDefined(self.firstperson_fx_overdrive))
	{
		StopFX(localClientNum, self.firstperson_fx_overdrive);
		self.firstperson_fx_overdrive = undefined;
   	}
	self.firstperson_fx_overdrive = PlayFXOnCamera( localClientNum, "player/fx_plyr_ability_screen_blur_overdrive", (0,0,0), (1,0,0), (0,0,1) );
	self thread watch_stop_player_fx( localClientNum,  self.firstperson_fx_overdrive );
}

function watch_stop_player_fx( localClientNum, fx )
{
	self notify("watch_stop_player_fx");
	self endon("watch_stop_player_fx");
	self endon("entityshutdown");
	
	self util::waittill_any( "stop_player_fx","death","disable_cybercom" );
	
	if ( IsDefined( fx ) )
	{
		StopFx( localClientNum, fx );	
		self.firstperson_fx_overdrive = undefined;  	   
	}
}

// Turn off the "hyperdrive" boost effects on camera
function stop_boost_camera_fx(localClientNum)
{
	self notify( "stop_player_fx" );
	
	if(( isdefined( self.whiteFlashFade ) && self.whiteFlashFade ))
	{
		lui::screen_fade( GetDvarFloat("scr_overdrive_flash_fade_out_time", 0.45), 0, GetDvarFloat("scr_overdrive_flash_alpha", 0.7), "white" );
	}
}

// Stop FX when we die or hit a cybercom turnoff event.
function overdrive_boost_fx_interrupt_handler(localClientNum)
{
	self endon("overdrive_boost_fx_interrupt_handler");
	self endon("end_overdrive_boost_fx");
	self endon("entityshutdown");
	
	self util::waittill_any( "death", "disable_cybercom" );
	
	self overdrive_shutdown( localClientNum );
}

// Turns off all effects and ability buffs given by overdrive.
// self == player
function overdrive_shutdown( localClientNum )
{
	if (isdefined(localClientNum))
	{
		self stop_boost_camera_fx( localClientNum );
		self ClearAlternateAimParams();
		filter::disable_filter_overdrive( self,3 );
	   	DisableSpeedBlur( localClientNum );
	   	self notify( "end_overdrive_boost_fx");
	}
}

// Turn on boost FX (hyperdrive!) when we're moving forward fast enough.
// This effectively translates to sprinting forward with its current tuning.
function boost_fx_on_velocity(localClientNum)	//self == player
{
	self endon("disable_cybercom");
	self endon("death");
	self endon("end_overdrive_boost_fx");
	self endon("disconnect");
	
	// Ensure the boost plays for at least a short time on activation.
	self enable_boost_camera_fx(localClientNum);

	//Keep track of when we might need to turn force off the boost FX
	self thread overdrive_boost_fx_interrupt_handler(localclientnum);

	wait GetDvarFloat("scr_overdrive_boost_fx_time", 0.75);
	
	while (isDefined(self))
	{
		// Get forward direction and speed information.
		v_player_velocity = self GetVelocity();
		v_player_forward = Anglestoforward(self.angles);
		n_dot = VectorDot(VectorNormalize(v_player_velocity), v_player_forward);
		n_speed = Length(v_player_velocity);
	
		// If we're moving forward fast enough:
		if (n_speed >= GetDvarInt( "scr_overdrive_boost_speed_tol", 280 ) && n_dot > 0.8)
		{
			if (!isdefined(self.firstperson_fx_overdrive))
			{
				self enable_boost_camera_fx(localClientNum);
			}
		}
		else
		{
			if (isdefined(self.firstperson_fx_overdrive))
			{
				self stop_boost_camera_fx(localClientNum);
			}
		}
		{wait(.016);};
	}
}

