#using scripts\codescripts\struct;

#using scripts\shared\callbacks_shared;
#using scripts\shared\util_shared;
#using scripts\shared\system_shared;
#using scripts\shared\filter_shared;
#using scripts\shared\visionset_mgr_shared;
#using scripts\shared\clientfield_shared;
#using scripts\shared\math_shared;

    	   	                                                                                                                                         	                                	                                                                                                                                                                                                                                                                                                                                                                    	        	     	              	    	   	               	                                              	                                                           	                               	                     	                                                                                                           	                                                                 	                                                              	                                                                                                                                             	                            	                                     	                                       	                                                                                                              	   	                  	       	                                                    	                   	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            	                                                                                                 	                                    	                                                                                        	           	                        	                                            	                                             	                                                   	                                                             	                                                         	                                                                    	                                                                                                                                                                                                                                	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  	    	         	                                                              	                                                          	                                   	                                   	                                                    	                                       	                                           	     	                                                                                                                                                                                                                                                      	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         	                                                               	        	                                                                                                                              	                                                                                                                     	                               	                                                      	                          
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         	   	                                                                                                                                                                                                                                  	                                                                                                                                                                                                                                                                                                                                                                                                                                                   
                                    	     	        	  	                                                           	

#namespace drown;




	
	// sqr( 2 )
	
// keep all these between 0 and 1, they will be scaled by DROWN_RADIUS_SCALE later
// where 0 is center, 1 is corner




	




	




	




	
//OPACTIY


	


	


	


	
	
function autoexec __init__sytem__() {     system::register("drown",&__init__,undefined,undefined);    }
	
function __init__()
{
	clientfield::register( "toplayer", "drown_stage", 1, 3, "int", &drown_stage_callback, !true, !true );
	callback::on_localplayer_spawned( &player_spawned );
	
	level.playerMaxHealth = GetGametypeSetting( "playerMaxHealth" );
	level.player_swim_damage_interval = GetDvarFloat( "player_swimDamagerInterval", 5000 ) * 1000;
	level.player_swim_damage = GetDvarFloat( "player_swimDamage", 5000 );
	level.player_swim_time = GetDvarFloat( "player_swimTime", 5000 ) * 1000;
	level.player_swim_death_time = ( level.playerMaxHealth / level.player_swim_damage ) * level.player_swim_damage_interval + 2000;
	
	visionset_mgr::register_overlay_info_style_speed_blur( "drown_blur", 
	                                                      1, 
	                                                      1, 
	                                                      0.04, 
	                                                      1, 
	                                                      1, 
	                                                      0, 
	                                                      0, 
	                                                      125, 
	                                                      125, 
	                                                      0 );

	setup_radius_values();
}

function setup_radius_values()
{
	level.drown_radius["inner"]["begin"][1] = .8;
	level.drown_radius["inner"]["begin"][2] = .6;
	level.drown_radius["inner"]["begin"][3] = .6;
	level.drown_radius["inner"]["begin"][4] = .5;
	
	level.drown_radius["inner"]["end"][1] = .5;
	level.drown_radius["inner"]["end"][2] = .3;
	level.drown_radius["inner"]["end"][3] = .3;
	level.drown_radius["inner"]["end"][4] = .2;
	
	level.drown_radius["outer"]["begin"][1] = 1;
	level.drown_radius["outer"]["begin"][2] = .8;
	level.drown_radius["outer"]["begin"][3] = .8;
	level.drown_radius["outer"]["begin"][4] = .7;
	
	level.drown_radius["outer"]["end"][1] = .8;
	level.drown_radius["outer"]["end"][2] = .6;
	level.drown_radius["outer"]["end"][3] = .6;
	level.drown_radius["outer"]["end"][4] = .5;
	
	
	level.opacity["begin"][1] = .4;
	level.opacity["begin"][2] = .5;
	level.opacity["begin"][3] = .6;
	level.opacity["begin"][4] = .6;
	
	level.opacity["end"][1] = .5;
	level.opacity["end"][2] = .6;
	level.opacity["end"][3] = .7;
	level.opacity["end"][4] = .7;
}

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

	self player_init_drown_values();
	self thread player_watch_drown_shutdown( localClientNum );
}

function player_init_drown_values()
{
	if( !isDefined( self.drown_start_time ) )
	{
		self.drown_start_time = 0;
		self.drown_outerRadius = 0;
		self.drown_innerRadius = 0;
		self.drown_opacity = 0;
	}
}

function player_watch_drown_shutdown( localClientNum )
{
	self util::waittill_any ( "entityshutdown", "death" );
	self disable_drown( localClientNum );
}

function enable_drown( localClientNum , stage )
{
	filter::init_filter_drowning_damage( localClientNum );
	filter::enable_filter_drowning_damage( localClientNum, 1 );
	self.drown_start_time = GetServerTime( localClientNum ) - ( stage - 1 ) * level.player_swim_damage_interval;
	self.drown_outerRadius = 0;
	self.drown_innerRadius = 0;
	self.drown_opacity = 0;
}

function disable_drown( localClientNum )
{
	filter::disable_filter_drowning_damage( localClientNum, 1 );
}

function player_drown_fx( localClientNum, stage )
{
	self endon ( "death" );
	self endon ( "entityshutdown" );
	self endon ( "player_fade_out_drown_fx" );
	self notify ( "player_drown_fx" );
	self endon ( "player_drown_fx" );
	
	self player_init_drown_values();
	
	lastOutWaterTimeStage = self.drown_start_time + ( stage - 1 ) * level.player_swim_damage_interval;
	
	stageDuration = level.player_swim_damage_interval;
	if( stage == 1 )
	{
		stageDuration = 2000;
	}
		
	while( 1 )
	{
		currentTime = GetServerTime( localClientNum );
		elapsedTime = currentTime - self.drown_start_time;

		stageRatio = math::clamp( ( currentTime - lastOutWaterTimeStage ) / stageDuration, 0.0, 1.0 );
		self.drown_outerRadius = lerpFloat(level.drown_radius["outer"]["begin"][stage], level.drown_radius["outer"]["end"][stage], stageRatio ) * 1.41421;
		self.drown_innerRadius = lerpFloat(level.drown_radius["inner"]["begin"][stage], level.drown_radius["inner"]["end"][stage], stageRatio ) * 1.41421;
		self.drown_opacity = lerpFloat(level.opacity["begin"][stage], level.opacity["end"][stage], stageRatio );
		
		filter::set_filter_drowning_damage_inner_radius( localClientNum, 1, self.drown_innerRadius );
		filter::set_filter_drowning_damage_outer_radius( localClientNum, 1, self.drown_outerRadius );
		filter::set_filter_drowning_damage_opacity( localClientNum, 1, self.drown_opacity );
		
		{wait(.016);};
	}
}

function player_fade_out_drown_fx( localClientNum )
{
	self endon ( "death" );
	self endon ( "entityshutdown" );
	self endon ( "player_drown_fx" );
	self notify ( "player_fade_out_drown_fx" );
	self endon ( "player_fade_out_drown_fx" );
	
	self player_init_drown_values();
	
	fadeStartTime = GetServerTime( localClientNum );
	currentTime = GetServerTime( localClientNum );
	while( currentTime - fadeStartTime < 250 )
	{
		ratio = ( currentTime - fadeStartTime ) / 250;
		outerRadius = lerpFloat( self.drown_outerRadius, 1.41421, ratio );
		innerRadius = lerpFloat( self.drown_innerRadius, 1.41421, ratio );
		opacity = lerpFloat( self.drown_opacity, 0, ratio );
		
		filter::set_filter_drowning_damage_outer_radius( localClientNum, 1, outerRadius );
		filter::set_filter_drowning_damage_inner_radius( localClientNum, 1, innerRadius );
		filter::set_filter_drowning_damage_opacity( localClientNum, 1, opacity );
		
		{wait(.016);};
		currentTime = GetServerTime( localClientNum );
	}
	
	self disable_drown( localClientNum );
}

function drown_stage_callback( localClientNum, oldVal, newVal, bNewEnt, bInitialSnap, fieldName, bWasTimeJump )
{
	if( newVal > 0 )
	{
		self enable_drown( localClientNum, newVal );
		self thread player_drown_fx( localClientNum, newVal );
	}
	else if( !bNewEnt )
	{
		self thread player_fade_out_drown_fx( localClientNum );
	}
	else
	{
		self disable_drown( localClientNum );
	}
}