#using scripts\codescripts\struct;

#using scripts\shared\callbacks_shared;
#using scripts\shared\clientfield_shared;
#using scripts\shared\duplicaterender_mgr;
#using scripts\shared\system_shared;
#using scripts\shared\util_shared;
#using scripts\shared\postfx_shared;
#using scripts\shared\visionset_mgr_shared;

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

#using scripts\shared\_burnplayer;

                                                                                
    	   	                                                                                                                                         	                                	                                                                                                                                                                                                                                                                                                                                                                    	        	     	              	    	   	               	                                              	                                                           	                               	                     	                                                                                                           	                                                                 	                                                              	                                                                                                                                             	                            	                                     	                                       	                                                                                                              	   	                  	       	                                                    	                   	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            	                                                                                                 	                                    	                                                                                        	           	                        	                                            	                                             	                                                   	                                                             	                                                         	                                                                    	                                                                                                                                                                                                                                	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  	    	         	                                                              	                                                          	                                   	                                   	                                                    	                                       	                                           	     	                                                                                                                                                                                                                                                      	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         	                                                               	        	                                                                                                                              	                                                                                                                     	                               	                                                      	                          
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         	   	                                                                                                                                                                                                                                  	                                                                                                                                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                       	   	                                                                      	  	  	
	                               	                    	                                 	                                        	                            	                                                                  	                               	  	                               	                    	                                 	                                        	                            	                                                                  	                             









	


	// This does more traces accordingly.

#precache( "client_fx", "player/fx_plyr_heat_wave" );
#precache( "client_fx", "player/fx_plyr_heat_wave_1p" );
#precache( "client_fx", "player/fx_plyr_heat_wave_distortion_volume" );
#precache( "client_fx", "player/fx_plyr_heat_wave_distortion_volume_air" );
#precache( "client_tagfxset", "ability_hero_heat_wave_player_impact" );

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

function __init__()
{
	clientfield::register( "scriptmover", "heatwave_fx", 1, 1, "int", &set_heatwave_fx, !true, !true );
	clientfield::register( "allplayers", "heatwave_victim", 1, 1, "int", &update_victim, !true, !true );
	clientfield::register( "toplayer", "heatwave_activate", 1, 1, "int", &update_activate, !true, !true );
	
	level.debug_heat_wave_traces = GetDvarInt( "scr_debug_heat_wave_traces", 0 );
	
	visionset_mgr::register_visionset_info( "heatwave", 1, 16, undefined, "heatwave" );
	visionset_mgr::register_visionset_info( "charred", 1, 16, undefined, "charred" );
	
	/#
	level thread updateDvars();
	#/
}

/#
function updateDvars()
{
	while(1)
	{
		level.debug_heat_wave_traces = GetDvarInt( "scr_debug_heat_wave_traces", level.debug_heat_wave_traces );
		wait(1.0);
	}
}
#/
	
function update_activate( localClientNum, oldVal, newVal, bNewEnt, bInitialSnap, fieldName, bWasTimeJump )
{
	if( newVal )
	{
		self thread postfx::PlayPostfxBundle( "pstfx_heat_pulse" );
	}
}
	
function update_victim( localClientNum, oldVal, newVal, bNewEnt, bInitialSnap, fieldName, bWasTimeJump )
{
	if( newVal )
	{
		self endon( "entityshutdown" );
		self util::waittill_dobj( localClientNum );    
		
		self PlayRumbleOnEntity( localClientNum, "heat_wave_damage" );
		PlayTagFXSet( localClientNum, "ability_hero_heat_wave_player_impact", self );
	}
}

function set_heatwave_fx( localClientNum, oldVal, newVal, bNewEnt, bInitialSnap, fieldName, bWasTimeJump )
{
	self clear_heat_wave_fx( localClientNum );
	if( newVal )
	{
		self.heatWaveFx = [];
		self thread aoe_fx( localClientNum );
	}
}

function clear_heat_wave_fx( localClientNum )
{
	if( !isDefined( self.heatWaveFx ) )
	{
		return;
	}
	
	foreach( fx in self.heatWaveFx )
	{
		StopFx( localClientNum, fx );
	}
}

function aoe_fx( localClientNum )
{
	self endon ( "entityshutdown" );
	
	center = self.origin + ( 0, 0, 30 );
	
	startPitch = -90;
	yaw_count = [];
	yaw_count[ 0 ] = 1;
	yaw_count[ 1 ] = 4;
	yaw_count[ 2 ] = 6;
	yaw_count[ 3 ] = 8;
	yaw_count[ 4 ] = 6;
	yaw_count[ 5 ] = 4;
	yaw_count[ 6 ] = 1;
	
	pitch_vals = [];
	pitch_vals[0] = 90;
	pitch_vals[3] = 0;
	pitch_vals[6] = -90;
	
	trace = bullettrace( center, center + ( 0, 0, -1 ) * 400, false, self );
	if ( trace["fraction"] < 1.0 )
	{
		pitch_vals[1] = 90 - atan( 150 / ( trace["fraction"] * 400 ) );	// evenly spaced 200 units away
		pitch_vals[2] = 90 - atan( 300 / ( trace["fraction"] * 400 ) );
	}
	else
	{
		pitch_vals[1] = 60;
		pitch_vals[2] = 30;
	}
	
	trace = bullettrace( center, center + ( 0, 0, 1 ) * 400, false, self );
	if ( trace["fraction"] < 1.0 )
	{
		pitch_vals[5] = -90 + atan( 150 / ( trace["fraction"] * 400 ) );	// evenly spaced 200 units away
		pitch_vals[4] = -90 + atan( 300 / ( trace["fraction"] * 400 ) );
	}
	else
	{
		pitch_vals[5] = -60;
		pitch_vals[4] = -30;
	}
	
	currentPitch = startPitch;
	for ( yaw_level = 0; yaw_level < yaw_count.size; yaw_level++ )
	{
		currentPitch = pitch_vals[yaw_level];
		do_fx( localClientNum, center, yaw_count[yaw_level], currentPitch );
	}
}

function do_fx( localClientNum, center, yaw_count, pitch )
{
	currentYaw = RandomInt( 360 );
	for( fxCount = 0; fxCount < yaw_count; fxCount++ )
	{
		randomOffsetPitch = RandomInt( 5 ) - 2.5;
		randomOffsetYaw = RandomInt( 30 ) - 15;
		angles = ( pitch + randomOffsetPitch, currentYaw + randomOffsetYaw, 0 );
		traceDir = AnglesToForward( angles );
		currentYaw += 360 / yaw_count;
		fx_position = center + traceDir * 400;
		trace = bullettrace( center, fx_position, false, self ); 
		sphere_size = 5;
		angles = ( 0, RandomInt( 360 ), 0 );
		forward = AnglesToForward( angles );
		
		if ( trace["fraction"] < 1.0 )
		{
			fx_position = center + traceDir * 400 * trace["fraction"];
			
			/#
			if( level.debug_heat_wave_traces )
			{
				 Sphere( fx_position, sphere_size, ( 1, 0, 1 ), 1, true, 8, 300 );
				 Sphere( trace["position"], sphere_size, ( 1, 1, 0 ), 1, true, 8, 300 );
			}
			#/
			
			normal = trace["normal"];
			if ( LengthSquared( normal ) == 0 )
			{
				normal = -1 * traceDir;
			}
			right = ( normal[2] * -1, normal[1] * -1, normal[0] );
			if( LengthSquared( VectorCross( forward, normal ) ) == 0 )
			{
				forward = VectorCross( right, forward );
			}
			self.heatWaveFx[self.heatWaveFx.size] = PlayFx( localClientNum, "player/fx_plyr_heat_wave_distortion_volume", trace["position"], normal, forward  );
		}
		else
		{
			/#
			if( level.debug_heat_wave_traces )
			{
				Line( fx_position + ( 0,0,50 ), fx_position - ( 0,0,50 ), ( 1, 0, 0 ), 1, false, 300 );
				Sphere( fx_position, sphere_size, ( 1, 0, 1 ), 1, true, 8, 300 );
			}
			#/
				
			if( LengthSquared( VectorCross( forward, traceDir * -1 ) ) == 0 )
			{
				forward = VectorCross( right, forward );
			}
			self.heatWaveFx[self.heatWaveFx.size] = PlayFx( localClientNum, "player/fx_plyr_heat_wave_distortion_volume_air", fx_position, traceDir * -1, forward  );
		}
		
		if( fxCount % 2 )
		{
			{wait(.016);};
		}
	}
}


