#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include common_scripts\utility;
#include maps\mp\agents\_agent_utility;
#include maps\mp\gametypes\_damage;
#include maps\mp\agents\_scriptedAgents;
#include maps\mp\_audio;
#include maps\mp\_dynamic_events;


main()
{
	maps\mp\mp_recovery_precache::main();
	maps\createart\mp_recovery_art::main();
	maps\mp\mp_recovery_fx::main();
	maps\mp\_load::main();
	maps\mp\mp_recovery_lighting::main();
	maps\mp\mp_recovery_aud::main();
	
	maps\mp\_compass::setupMiniMap( "compass_map_mp_recovery" );
	
	game["attackers"] = "allies";
	game["defenders"] = "axis";
	
	//dynamic event fx
	
	level.missilefx = loadfx("vfx/test/hms_fireball_explosion_xlrg");
		
	thread dynamic_ents();
	thread handle_teleport();
	thread dynamic_pathing_main();
	
	gametype = level.gametype;
	if ( !isdefined ( level.isHorde ) ) 
		if(  !(gametype == "twar" || gametype == "sd" || gametype == "sr" )) //no dynamic event in horde
			level thread DynamicEvent( ::recovery_dynamic_event);

	level thread onPlayerConnect();
	
	//spawns
	thread SpawnSetup();
	level.DynamicEventStatus = "before"; // for recovery killstreak keeping track of event
	level.hp_pause_for_dynamic_event = false;
		
//	level.mapCustomKillstreakFunc = ::recoveryCustomKillstreakFunc;
	
	// paladin overrides
	level.orbitalSupportOverrideFunc = ::recoveryCustomOSPFunc;
	
	// Setting a map-specific OSP light/vision set.
	level.ospvisionset = "mp_recovery_b";
	level.ospLightSet = "mp_recovery_osp";
	
	// Setting a map-specific assault drone vision and light set.
	level.droneVisionSet = "mp_recovery_b";
	level.droneLightSet = "mp_recovery";
}

recoveryCustomOSPFunc()
{
	level.orbitalsupportoverrides.spawnHeight 	= 9324;
	level.orbitalsupportoverrides.spawnAngleMin = 290;
	level.orbitalsupportoverrides.spawnAngleMax = 370;
	level.orbitalsupportoverrides.topArc		= -45;
	thread recoveryEventCustomOSPFunc();
}

recoveryEventCustomOSPFunc()
{
	level waittill( "Gas_Cloud_Start" );
	level.orbitalsupportoverrides.spawnAngleMin = 120;
	level.orbitalsupportoverrides.spawnAngleMax = 180;
	level.orbitalsupportoverrides.turretPitch	= 55;
	level.orbitalsupportoverrides.topArc		= -40;
	level.orbitalsupportoverrides.bottomArc		= 65;
}

//recoveryCustomKillstreakFunc()
//{
//	level thread maps\mp\killstreaks\streak_mp_recovery::init();
//}

SpawnSetup()
{
	level.dynamicHangarSpawns = false;
	level.dynamicSpawns = ::GetListOfGoodSpawnPoints;
}

set_lighting_values()
{
    if ( IsUsingHDR() )
    {        while (true)
        {
            level waittill("connected", player);               // we need to set the dvars for each player as he connects.
           player SetClientDvars(
                "r_tonemap", "1"               
           );
         /*   player SetClientDvars(
                "r_tonemap", "1",
                "r_tonemapadaptspeed", ".07",
                "r_tonemapkey", "0",
                "r_veil", "1",
                "r_veilstrength", ".087",
                "r_tonemapexposure", "-10" 
              );
            
            //For setting the max exposure so we can get contrast back into the map without blowing everything out
            setdvar ("r_tonemapmaxexposure", "-10");
            
            //Controling how much SSAO there is in the scene
            setdvar ("r_ssaoPower", "3.5");
            setdvar ("r_ssaoBlurRadius", "1.5");
          */  
        }
    }
}

dynamic_ents()
{
	emergency_state = GetEnt("mp_recovery_signage","targetname");
	wait(.05);
	emergency_state hide_notsolid();
	FlickerLights();
	
	rightdoorArray = GetEntArray("hangar_door_right","targetname");
	
	leftdoorArray = GetEntArray("hangar_door_left","targetname");
	
	missileArray = GetEntArray("chemical_missile","targetname");
	missile2Array = GetEntArray("chemical_missile2","targetname");
	missile3Array = GetEntArray("chemical_missile3","targetname");
	missile4Array = GetEntArray("chemical_missile4","targetname");
	missile5Array = GetEntArray("chemical_missile5","targetname");
	missile6Array = GetEntArray("chemical_missile6","targetname");
	
	missile7Array = GetEntArray("chemical_missile7","targetname");
	missile8Array = GetEntArray("chemical_missile8","targetname");
	missile9Array = GetEntArray("chemical_missile9","targetname");
	missile10Array = GetEntArray("chemical_missile10","targetname");
	
	GasDeathtrigs_1 = GetEntArray ("deathTrig_1", "targetname");
	GasDeathtrigs_2 = GetEntArray ("deathTrig_2", "targetname");
	GasDeathtrigs_3 = GetEntArray ("deathTrig_3", "targetname");
	
	foreach (trig1 in GasDeathtrigs_1)
	{		
			trig1 DontInterpolate();
			trig1.origin = trig1.origin + (0, 0, -10000);
	}
	
	foreach (trig2 in GasDeathtrigs_2)
	{		
			trig2 DontInterpolate();
			trig2.origin = trig2.origin + (0, 0, -10000);
	}
	
	foreach (trig3 in GasDeathtrigs_3)
	{		
			trig3 DontInterpolate();
			trig3.origin = trig3.origin + (0, 0, -10000);
	}
	
	level waittill( "Missile_Wave2_ended" );
	level.dynamicHangarSpawns = true;
	
	level.DynamicEventStatus = "after";
	
	wait (5);
	
	foreach (trig1 in GasDeathtrigs_1)
	{		
			trig1 DontInterpolate();
			trig1.origin = trig1.origin + (0, 0, 10000);
	}
	
	wait (5);
	
	foreach (trig2 in GasDeathtrigs_2)
	{		
			trig2 DontInterpolate();
			trig2.origin = trig2.origin + (0, 0, 10000);
	}
	
	wait (5);
	
	foreach (trig3 in GasDeathtrigs_3)
	{		
			trig3 DontInterpolate();
			trig3.origin = trig3.origin + (0, 0, 10000);
	}	
}

recovery_dynamic_event()
{	//swap in state b lighting for alarm lights
	hologram_signs = GetEnt("hologram_signs","targetname");
	hologram_signs hide_notsolid();
	emergency_state = GetEnt("mp_recovery_signage","targetname");
	emergency_state show(); // but stay notsolid
	thread KillLights();
	thread spawnHangarDoors();
	//thread spawnMissileAttack1();
	thread volcanoStartEruption();
	//thread spawnMissileAttack2();
	thread gasCloudStart();
	//thread DynamicDeathTriggers();
}

handle_teleport()
{
	gametype = level.gametype;
	if( !( gameType == "dom" || gametype == "ctf" || gametype == "hp" || gametype == "ball" ) )
		return;
	
	if( gametype == "hp" )
		level waittill( "dynamic_event_starting" );
	else
	level waittill( "hangar_doors_opening" );
	
	level.hp_pause_for_dynamic_event = true;
	maps\mp\_teleport::teleport_to_zone( "zone_0", true );
	
	level.useStartSpawns = false;
}

dynamic_pathing_main()
{
	hangar_doors = GetEntArray( "hangar_door_right", "targetname" );
	
	level waittill( "hangar_doors_opening" );
	
	wait( 3 );
	foreach ( door in hangar_doors )
	{
		if( door.classname == "script_brushmodel" )
		door ConnectPaths();
	}
	escape_nodes = GetNodeArray( "escape_gas_dest_node", "targetname" );
	escape_triggers = GetEntArray( "escape_gas_dest_trigger", "targetname" );
	
	foreach( player in level.participants )
	{
		if( IsAI( player ) )
		{
			player maps\mp\bots\_bots_strategy::bot_defend_stop();
			
			switch( level.gametype )
			{
				case "war":
				case "conf":
				case "infect":
				case "dm":
					player thread escape_gas( escape_nodes, escape_triggers );
					break;
					
				default:
					break;
			}
		}
	}
	
	level waittill( "hangar_doors_closed" );

	foreach ( door in hangar_doors )
	{
		if( door.classname == "script_brushmodel" )
		door DisconnectPaths();
}
}

get_escape_gas_dest_node( escape_nodes )
{
	random_int = randomint( escape_nodes.size );
	selected_node = escape_nodes[random_int];
	
	return selected_node;
}

clear_script_goal_on_gas_end()
{
	level endon( "game_ended" );
	self endon( "disconnect" );
	
	level waittill( "hangar_doors_closed" );
	wait(0.05);
	self BotClearScriptGoal();
}

escape_gas( escape_nodes, escape_triggers )
{
	level endon( "game_ended" );
	level endon( "hangar_doors_closed" );
	self endon( "disconnect" );
	
	self thread clear_script_goal_on_gas_end();
	
	while(1)
	{
		dest_node = get_escape_gas_dest_node( escape_nodes );
		self BotSetScriptGoal( dest_node.origin, 512, "critical" );
		result = self maps\mp\bots\_bots_util::bot_waittill_goal_or_fail( undefined, "death" );
		if ( result == "goal" )
		{
			self BotClearScriptGoal();
			wait(5.0);
		}
		else
		{
			wait(1.0);
		}
	}
}

spawnHangarDoors()
{
	//get hangar door script brushes from radiant
	
	rightdoorArray = GetEntArray("hangar_door_right","targetname");
//	leftdoorArray = GetEntArray("hangar_door_left","targetname");
	
	door_open_time = 12;
	door_close_time = 12;

	level waittill ("Gas_Cloud_Start");
	
	//wait (15);
	
	//opens the doors
	
	level.DynamicEventStatus = "event_in_progress";
	
	foreach (rightdoor in rightdoorArray) 
	{		
		rightdoorOrigin = GetEnt( rightdoor.target , "targetname");
		rightdoor MoveTo( rightdoorOrigin.origin, door_open_time );
	}
		
//	foreach (leftdoor in leftdoorArray) 
//	{		
//		leftdoorOrigin = GetEnt( leftdoor.target , "targetname");
//		leftdoor MoveTo( leftdoorOrigin.origin, door_open_time );
//	}
	
	thread notify_doors_open( door_open_time );
		
	//play sfx
	
	dooropen_sfx = GetEntArray ("hangar_open_sfx", "targetname");
	
	foreach (dooropen in dooropen_sfx)
	{
		snd_play_in_space( "mp_recovery_hangar_door_open", dooropen.origin);
	}
	
	
	//CLOSE DOORS
	
	level waittill( "close_doors" );
	
	foreach (rightdoor in rightdoorArray) 
	{	
		rightdoorOrigin = GetEnt( rightdoor.target , "targetname");		
		rightdoorclose = GetEnt( rightdoorOrigin.target , "targetname");
		rightdoor MoveTo (rightdoorclose.origin , door_close_time);
		level thread maps\mp\mp_recovery_fx::sulfur_door_fx();
		
	}
	
//	foreach (leftdoor in leftdoorArray) 
//	{	
//		leftdoorOrigin = GetEnt( leftdoor.target , "targetname");		
//		leftdoorclose = GetEnt( leftdoorOrigin.target , "targetname");
//		leftdoor MoveTo (leftdoorclose.origin , door_close_time);
//	}
	
	thread notify_doors_close( door_close_time );
	
	//play sfx
	
	dooropen_sfx = GetEntArray ("hangar_open_sfx", "targetname");
	
	foreach (dooropen in dooropen_sfx)
	{
		snd_play_in_space( "mp_recovery_hangar_door_close", dooropen.origin);
	}
} 

notify_doors_close( wait_time )
{
	level notify( "hangar_doors_closing" );
	
	playSoundAtPos( ( 0,0,0 ),"mp_recovery_doors_closing" );   // DORS CLOSING

	wait( wait_time );
	
	level notify( "hangar_doors_closed" );
	
	wait( 2 );
	playSoundAtPos( ( 0,0,0 ), "mp_recovery_doors_sealed" ); // DOORS ARE CLOSED
}

notify_doors_open( wait_time )
{
	level notify( "hangar_doors_opening" );
	
	playSoundAtPos( ( 0,0,0 ), "mp_recovery_doors_opening" );
	
	thread side_b_visionset_reset();
	
	// HP Dynamic Event stuff
	level.hp_pause_for_dynamic_event = false;
	level notify( "ready_for_next_hp_zone" );
	
	wait( wait_time );
	
	level notify( "hangar_doors_opened" );
}

/*spawnMissileAttack1()
{
	missileArray = GetEntArray("chemical_missile","targetname");
	missile2Array = GetEntArray("chemical_missile2","targetname");
	missile3Array = GetEntArray("chemical_missile3","targetname");
	missile4Array = GetEntArray("chemical_missile4","targetname");
	missile5Array = GetEntArray("chemical_missile5","targetname");
	missile6Array = GetEntArray("chemical_missile6","targetname");
	
//	level waittill ("dynamic_event");
	
	
	foreach (missile in missileArray) 
	{		
		missileOrigin = GetEnt( missile.target , "targetname");
		missile MoveTo (missileOrigin.origin, 2);
		
		snd_play_in_space( "mp_recovery_hind_missiles_flyover_01", missileOrigin.origin);
		wait (2);
		snd_play_in_space( "mp_recovery_chopper_missile_hit_2", missileOrigin.origin);
		
		missile thread play_event_music();
		
		level thread common_scripts\_exploder::activate_clientside_exploder(1);
		level thread common_scripts\_exploder::activate_clientside_exploder(7);
		
		IPrintLnBold ("Chemical Attack!");
		
	}
	
	foreach (missile2 in missile2Array) 
	{		
		missile2Origin = GetEnt( missile2.target , "targetname");
		missile2 MoveTo (missile2Origin.origin, 3);
		
		snd_play_in_space( "mp_recovery_hind_missiles_flyover_02", missile2Origin.origin);
		wait (3);
		snd_play_in_space( "mp_recovery_chopper_missile_hit_4", missile2Origin.origin);
		
		level thread common_scripts\_exploder::activate_clientside_exploder(2);
		
	}
	
	foreach (missile3 in missile3Array) 
	{		
		missile3Origin = GetEnt( missile3.target , "targetname");
		missile3 MoveTo (missile3Origin.origin, 1);
		
		snd_play_in_space( "mp_recovery_hind_missiles_flyover_02", missile3Origin.origin);
		wait (1);
		snd_play_in_space( "mp_recovery_chopper_missile_hit_3", missile3Origin.origin);
		
		level thread common_scripts\_exploder::activate_clientside_exploder(3);
		level thread common_scripts\_exploder::activate_clientside_exploder(7);
		level thread common_scripts\_exploder::activate_clientside_exploder(8);
	}
	
	foreach (missile4 in missile4Array) 
	{		
		missile4Origin = GetEnt( missile4.target , "targetname");
		missile4 MoveTo (missile4Origin.origin, 3);
		
		snd_play_in_space( "mp_recovery_hind_missiles_flyover_01", missile4Origin.origin);
		wait (3);
		snd_play_in_space( "mp_recovery_chopper_missile_hit_3", missile4Origin.origin);
		
		level thread common_scripts\_exploder::activate_clientside_exploder(4);
		
	}
	
	foreach (missile5 in missile5Array) 
	{		
		missile5Origin = GetEnt( missile5.target , "targetname");
		missile5 MoveTo (missile5Origin.origin, 2);
		
		snd_play_in_space( "mp_recovery_hind_missiles_flyover_01", missile5Origin.origin);
		wait (2);
		snd_play_in_space( "mp_recovery_chopper_missile_hit_5", missile5Origin.origin);
		
		level thread common_scripts\_exploder::activate_clientside_exploder(5);
		level thread common_scripts\_exploder::activate_clientside_exploder(7);
		level thread common_scripts\_exploder::activate_clientside_exploder(8);
		level thread common_scripts\_exploder::activate_clientside_exploder(9);
		
	}
	
	foreach (missile6 in missile6Array) 
	{		
		missile6Origin = GetEnt( missile6.target , "targetname");
		missile6 MoveTo (missile6Origin.origin, 3);
		
		snd_play_in_space( "mp_recovery_hind_missiles_flyover_02", missile6Origin.origin);
		wait (3);
		snd_play_in_space( "mp_recovery_chopper_missile_hit_4", missile6Origin.origin);
		
		level thread common_scripts\_exploder::activate_clientside_exploder(6);
		
	}
	
	wait (5);
	
	level notify( "Missile_Wave1_ended" );
	
	
}*/

volcanoStartEruption()
{	
	thread aud_dyanmic_event();
	
	Earthquake( .6, 2, (0,0,0), 5000 );
	//controller rumble
	foreach( Player in level.players )
	{
		player thread play_earthquake_rumble( .75 );
	}
	level.gas_cloud_origin = getEnt("gas_cloud_origin", "targetname");
	//level.gas_cloud_origin thread play_event_music();
	//start volcano fx
	StopClientExploder(200);
	if( isDefined( level.panoramicfx ) )
		level.panoramicfx delete();
	level thread common_scripts\_exploder::activate_clientside_exploder(100);
	//IPrintLnBold ("The volcano is erupting!");
	wait 8;
	//start first boulder barrage
	thread boulderBarrage(15, 2.0, 10, 19);
	wait 5;
	//change vision set
	foreach ( player in level.players )
		player SetClientTriggerVisionSet( "mp_recovery_post", 10.0 );
	//IPrintLnBold ("Get inside the Hangar!");
	level notify( "Gas_Cloud_Start" );
}


aud_dyanmic_event()
{
	playSoundAtPos ( ( 2067, -2296 ,186 ), "emt_event_volcano_erupt" );
	
	thread aud_handle_alarms();
	
	wait( 4 );
	
	playSoundAtPos( ( 0,0,0 ), "mp_recovery_volcanic_activity" );  // BIG EARTHQUAKE HIT SOUND
}

aud_handle_alarms()
{
	level endon( "hangar_doors_closed" );
	while(1)
	{
		playSoundAtPos( ( 0,0,0 ),"mp_recovery_alarms" );  // ALARMS LAST 5 Seconds.
	
		wait(4);		
	}
}

gasCloudStart()
{
	level waittill( "Gas_Cloud_Start" );
	wait 5;
	
	//start poison gas cloud
	level.gas_cloud_origin = getEnt("gas_cloud_origin", "targetname");
	playfxontag(getfx("pyroclastic_flow_1"), level.gas_cloud_origin, "tag_origin");
	//IPrintLnBold ("Escape the Gas!");
	wait 5;
	
	//start second boulder barrage
	thread boulderBarrage(15, 2.0, 20, 29);
	
	//start third boulder barrage
	delayThread(15, ::boulderBarrage, 15, 1.5, 30, 39);
	
	//move spawn points to safe side of map
	level.dynamicHangarSpawns = true;
		
	//gas cloud is moving
	gas_cloud_travel_time = 40;
	door_close_time = 12;
	level.gas_cloud_origin moveto( (level.gas_cloud_origin.origin + (0, 3912, 0) ), gas_cloud_travel_time );
	level.gas_cloud_origin thread killPlayersInCloud(gas_cloud_travel_time + door_close_time, 7.5);
	start_door_close_time =  gas_cloud_travel_time - (door_close_time / 2);
	thread setup_poison_gas_death();
	
	//close doors
	//noself_delayCall(start_door_close_time - 5, ::IPrintLnBold, "Hurry!");
	wait start_door_close_time;
	level notify("close_doors");
	
	//end first part of poison gas cloud
	wait door_close_time;
	stopfxontag(getfx("pyroclastic_flow_1"), level.gas_cloud_origin, "tag_origin");
	
	//force kill anyone not in the hangar the cloud didn't get
	//level.gas_cloud_origin thread killPlayersInCloud(1, 10000);
	level.gas_cloud_origin thread instantKillPlayersInCloud();
	
	//spawn lingering poison gas fx
	level thread common_scripts\_exploder::activate_clientside_exploder(102);
	
	wait 0.05;
	level notify( "gas_cloud_finished" );
	
	//kill sulfur dust, dust and steam vfx on starting side of map
	StopClientExploder(40);
}	

onPlayerConnect()
{
	safe_from_gas_trigger = getEnt("safe_from_gas", "targetname");
	while(true)
	{
		level waittill("connected", player);
		player thread swapFogAndFX( safe_from_gas_trigger );
	}
}

swapFogAndFX( safe_from_gas_trigger )
{
	level endon("gas_cloud_finished");
	self endon("disconnect");

	//if player moves between gas and non-gas side of the map
	while( true )
	{
		if ( !isDefined( safe_from_gas_trigger ) || !isDefined( self ) )
		{
			break;
		}

		if ( level.DynamicEventStatus == "event_in_progress" && IsAlive( self ) )
		{
			//move to gas side
			if( !( self isTouching( safe_from_gas_trigger ) ) && isDefined( self.onNoPoisonSide ) )
			{
				//swap vision set
				self SetClientTriggerVisionSet( "mp_recovery_post", 5.0 );
				//swap gas fx
				stopfxontag(getfx("pyroclastic_flow_2"), level.gas_cloud_origin, "tag_origin");
				playfxontag(getfx("pyroclastic_flow_1"), level.gas_cloud_origin, "tag_origin");
				self.onNoPoisonSide = undefined;
			}
			//move to non-gas side
			else if( self isTouching( safe_from_gas_trigger ) && !isDefined( self.onNoPoisonSide ) )
			{
				//swap vision set
				self SetClientTriggerVisionSet( "", 5.0 );
				//swap gas fx
				stopfxontag(getfx("pyroclastic_flow_1"), level.gas_cloud_origin, "tag_origin");
				playfxontag(getfx("pyroclastic_flow_2"), level.gas_cloud_origin, "tag_origin");
				self.onNoPoisonSide = true;
			}
			wait( 0.2 );
		}
		else
		{
			wait( 1.0 );
		}
	}		
}
	   		
	   
FlickerLights()
{
	FlckLgtEnts = GetScriptableArray( "stairlgt_die_3", "targetname" );
	foreach ( FlckLgtEnt in FlckLgtEnts )
	{
		FlckLgtEnt SetScriptablePartState(0, 1);
	}
	
	FlckrLgtaEnts = GetScriptableArray( "die_2", "targetname" );
	foreach ( FlckrLgtaEnt in FlckrLgtaEnts )
	{
		FlckrLgtaEnt SetScriptablePartState(0, 1);
	}
}

KillLights()
{
	//turn off light fixtures
	KillLgtEnts = GetScriptableArray( "killed_lights", "targetname" );
	foreach ( KillLgtEnt in KillLgtEnts )
	{
		wait(.1);
		KillLgtEnt SetScriptablePartState(0, 1);
	}
	
	//turn on danger lighting
	DangerLgtEnts = GetScriptableArray( "danger_red", "targetname" );
	foreach ( DangerLgtEnt in DangerLgtEnts )
	{
		DangerLgtEnt SetScriptablePartState(0, 1);
	}
	
	//kill and flicker wall lights in buildings
	FlckrLgtEnts = GetScriptableArray( "die", "targetname" );
	foreach ( FlckrLgtEnt in FlckrLgtEnts )
	{
		FlckrLgtEnt SetScriptablePartState(0, 2);
		wait(.1);
		FlckrLgtEnt SetScriptablePartState(0, 3);
	}	
	
	FlckrLgtaEnts = GetScriptableArray( "die_2", "targetname" );
	foreach ( FlckrLgtaEnt in FlckrLgtaEnts )
	{
		FlckrLgtaEnt SetScriptablePartState(0, 3);
	}
	
	FlckLgtEnts = GetScriptableArray( "stairlgt_die_3", "targetname" );
	foreach ( FlckLgtEnt in FlckLgtEnts )
	{
		FlckLgtEnt SetScriptablePartState(0, 3);
	}
	
	//kill rock bldg lights
	StairLgtEnts = GetScriptableArray( "stairlgt_die", "targetname" );
	foreach ( StairLgtEnt in StairLgtEnts )
	{
		wait(.1);
		StairLgtEnt SetScriptablePartState(0, 1);
		wait(.15);
		StairLgtEnt SetScriptablePartState(0, 3);
	}
	
	//kill rock bldg lights version 2
	StairLgt2Ents = GetScriptableArray( "stairlgt_die_2", "targetname" );
	foreach ( StairLgt2Ent in StairLgt2Ents )
	{
		StairLgt2Ent SetScriptablePartState(0, 3);
		wait(.2);
		StairLgt2Ent SetScriptablePartState(0, 1);
	}
	
	//turn on evacuation lighting
	EvacuateLgtEnts = GetScriptableArray( "evacuate_lights", "targetname" );
	foreach ( EvacuateLgtEnt in EvacuateLgtEnts )
	{
		EvacuateLgtEnt SetScriptablePartState(0, 1);
	}
	
	EvacuatePillLgtEnts = GetScriptableArray( "evacuate_pill_lights", "targetname" );
	foreach ( EvacuatePillLgtEnt in EvacuatePillLgtEnts )
	{
		EvacuatePillLgtEnt SetScriptablePartState(0, 1);
	}
	
	HoloLgtEnts = GetScriptableArray( "hologram_lgt", "targetname" );
	foreach ( HoloLgtEnt in HoloLgtEnts )
	{
		HoloLgtEnt SetScriptablePartState(0, 2);
	}
	
	//turn off lights in cave and hologram light
	CaveLgtEnts = GetScriptableArray( "cave_kill", "targetname" );
	foreach ( CaveLgtEnt in CaveLgtEnts )
	{
		CaveLgtEnt SetScriptablePartState(0, 1);
		wait(.2);
		CaveLgtEnt SetScriptablePartState(0, 2);
	}
}

	
boulderBarrage( duration, frequency, exploderMin, exploderMax )
{
	endTime = getTime() + ( duration * 1000 );
	randInt = 0;
	lastInt = 0;
	
	while( getTime() < endTime )
	{
		wait( randomFloatRange( ( frequency / 2 ), frequency ) );
		while( randInt == 0 )
		{
			randInt = randomIntRange( exploderMin, exploderMax );
			if ( randInt == lastInt )
				randInt = 0;
			else
			{
				lastInt = randInt;
				level thread common_scripts\_exploder::activate_clientside_exploder(randInt);
				level thread maps\mp\mp_recovery_fx::setup_boulder_audio(randInt);
				randInt = 0;
				break;
			}
			wait 0.05;
		}
	}
}

killPlayersInCloud( duration, dmgAmount ) // self == gas cloud
{
	cloud_height_z = 800;
	safe_from_gas_trigger = getEnt("safe_from_gas", "targetname");
	endTime = getTime() + ( duration * 1000 );

	foreach( player in level.players )
		player.isInGas = false;

	while( getTime() < endTime )
	{
		foreach( player in level.players )
		{
			//if player's Y pos is less than gas cloud and they aren't in safe zone, kill them
			playerViewOrigin = player getViewOrigin();
			if( ( player.origin[1] < ( self.origin[1] - 500 ) ) && !( player isTouching( safe_from_gas_trigger ) ) )
			{
				player DoDamage(dmgAmount, player.origin);
				//if camera is inside the cloud, set fog effect
				if ( !player isUsingRemote() && ( playerViewOrigin[1] < self.origin[1] ) && ( playerViewOrigin[2] < cloud_height_z ) )
				{
					player SetClientTriggerVisionSet( "poison_gas", 1.5 );
					player shellshock( "mp_lab_gas", 1, true, true, 0);
					//player ShockThink();
				}
				player.isInGas = true;
			}
			else if ( !player isUsingRemote() )
			{
				if( player.isInGas == true )
				{
					if (player isTouching( safe_from_gas_trigger ) )
						player revertVisionSetForPlayer( 1.5 );
					else
						player SetClientTriggerVisionSet( "mp_recovery_post", 1.5 );
					//player revertVisionSetForPlayer( 0.5 );
					
					player.isInGas = false;
				}
			}
		}
		
		if( level.gametype == "ctf" )
		{
			foreach( flag in level.teamFlags )
			{
				//if flag's Y pos is less than gas cloud and it isn't in safe zone, return it
				if( ( flag.curOrigin[1] < self.origin[1] ) && !( flag.visuals[0] isTouching( safe_from_gas_trigger ) ) )
					flag maps\mp\gametypes\ctf::returnFlag();
			}
		}
		
		wait 0.25;
	}
}

instantKillPlayersInCloud()
{
	safe_from_gas_trigger = getEnt("safe_from_gas", "targetname");
	
	foreach( player in level.players )
	{
		if( !( player isTouching( safe_from_gas_trigger ) ) )
			player DoDamage(10000, player.origin);	
	}
}

side_b_visionset_reset()
{
	level endon( "game_ended" );
	level endon( "hangar_doors_closed" );
	
	safe_from_gas_trigger = getEnt("safe_from_gas", "targetname");
	while( true )
	{
		safe_from_gas_trigger waittill( "trigger", player );
		if( IsDefined( player ) &&  isPlayer( player ) && !isDefined( player.safeFromGas ) )
		{
			player revertVisionSetForPlayer( 3.0 );
			player.safeFromGas = true;
		}
		//else
		//	player.safeFromGas = undefined;
	}
}


setup_poison_gas_death()
{
	foreach( player in level.players )
		player thread onPlayerDeath();
}
	
onPlayerDeath()
{
	level endon( "game_ended" );
	self endon( "disconnect" );
	
	self waittill( "spawned" );
	
	self revertVisionSetForPlayer( 0 );
}

ShockThink()
{
	if (!isDefined(self.inGas))
	{
		self shellshock( "mp_lab_gas", 1);
	}
}

play_event_music()
{
	//self PlayLoopSound( "mp_recovery_music" );
	//wait(30);
	//self stopsounds();
}

play_earthquake_rumble( time )
{
	self endon( "death" );
	level endon( "game_ended" );
	
	frames = time*20;
	
	while( frames >= 0 )
	{
		self PlayRumbleOnEntity( "damage_light" );
		wait( .1 );
		frames = frames - 2;
	}
}

/*spawnMissileAttack2()
{
	missile7Array = GetEntArray("chemical_missile7","targetname");
	missile8Array = GetEntArray("chemical_missile8","targetname");
	missile9Array = GetEntArray("chemical_missile9","targetname");
	missile10Array = GetEntArray("chemical_missile10","targetname");
	
	
	level waittill ("Missile_Wave1_ended");
	
	IPrintLnBold ("Get inside the Hangar!");
	
	
	foreach (missile7 in missile7Array) 
	{		
		missile7Origin = GetEnt( missile7.target , "targetname");
		missile7 MoveTo (missile7Origin.origin, 2);
		
		snd_play_in_space( "mp_recovery_hind_missiles_flyover_01", missile7Origin.origin);
		wait (2);
		snd_play_in_space( "mp_recovery_chopper_missile_hit_2", missile7Origin.origin);
		
		level thread common_scripts\_exploder::activate_clientside_exploder(1);
		level thread common_scripts\_exploder::activate_clientside_exploder(7);
		
	}
	
	foreach (missile8 in missile8Array) 
	{		
		missile8Origin = GetEnt( missile8.target , "targetname");
		missile8 MoveTo (missile8Origin.origin, 3);
		
		snd_play_in_space( "mp_recovery_hind_missiles_flyover_02", missile8Origin.origin);
		wait (3);
		snd_play_in_space( "mp_recovery_chopper_missile_hit_4", missile8Origin.origin);
		
		level thread common_scripts\_exploder::activate_clientside_exploder(2);
		
	}
	
	foreach (missile9 in missile9Array) 
	{		
		missile9Origin = GetEnt( missile9.target , "targetname");
		missile9 MoveTo (missile9Origin.origin, 1);
		
		snd_play_in_space( "mp_recovery_hind_missiles_flyover_02", missile9Origin.origin);
		wait (1);
		snd_play_in_space( "mp_recovery_chopper_missile_hit_3", missile9Origin.origin);
		
		level thread common_scripts\_exploder::activate_clientside_exploder(3);
		level thread common_scripts\_exploder::activate_clientside_exploder(7);
		level thread common_scripts\_exploder::activate_clientside_exploder(8);
	}
	
	foreach (missile10 in missile10Array) 
	{		
		missile10Origin = GetEnt( missile10.target , "targetname");
		missile10 MoveTo (missile10Origin.origin, 3);
		
		snd_play_in_space( "mp_recovery_hind_missiles_flyover_01", missile10Origin.origin);
		wait (3);
		snd_play_in_space( "mp_recovery_chopper_missile_hit_3", missile10Origin.origin);
		
		level thread common_scripts\_exploder::activate_clientside_exploder(4);
		
	}
	
	
	IPrintLnBold ("Get inside the Hangar!");
	
	wait (5);
	
	IPrintLnBold ("Escape the Gas!");
	
	wait (5);
	
	IPrintLnBold ("Hurry!");
	
	level notify( "Missile_Wave2_ended" );
		
}

DynamicDeathTriggers()
{
	
	GasDeathtrigs_1 = GetEntArray ("deathTrig_1", "targetname");
	GasDeathtrigs_2 = GetEntArray ("deathTrig_2", "targetname");
	GasDeathtrigs_3 = GetEntArray ("deathTrig_3", "targetname");
	
	foreach (trig1 in GasDeathtrigs_1)
	{		
			trig1 DontInterpolate();
			trig1.origin = trig1.origin + (0, 0, -10000);
	}
	
	foreach (trig2 in GasDeathtrigs_2)
	{		
			trig2 DontInterpolate();
			trig2.origin = trig2.origin + (0, 0, -10000);
	}
	
	foreach (trig3 in GasDeathtrigs_3)
	{		
			trig3 DontInterpolate();
			trig3.origin = trig3.origin + (0, 0, -10000);
	}
	
	level waittill( "Missile_Wave2_ended" );
	level.dynamicHangarSpawns = true;
	
	wait (5);
	
	foreach (trig1 in GasDeathtrigs_1)
	{		
			trig1 DontInterpolate();
			trig1.origin = trig1.origin + (0, 0, 10000);
	}
	
	wait (5);
	
	foreach (trig2 in GasDeathtrigs_2)
	{		
			trig2 DontInterpolate();
			trig2.origin = trig2.origin + (0, 0, 10000);
	}
	
	wait (5);
	
	foreach (trig3 in GasDeathtrigs_3)
	{		
			trig3 DontInterpolate();
			trig3.origin = trig3.origin + (0, 0, 10000);
	}
	
	
	
}*/


//SPAWNS///////////////////
/// 

GetListOfGoodSpawnPoints(fullListOfSpawnPoints)
{
	goodpoints = [];
	
	// Getting all neutral spawns
	foreach(spawn in fullListOfSpawnPoints)
	{
		if( !IsDefined( spawn.targetname ) || ( spawn.targetname == "" ) )
		{
			goodpoints = add_to_array(goodpoints, spawn);
		}
		else if(spawn GetValidSpawns() == true)
		{
			goodpoints = add_to_array(goodpoints, spawn);
		}
	}
	return goodpoints;
}
GetValidSpawns()
{
	if(level.dynamicHangarSpawns == false)
	{
		if(self.targetname == "first_map_spawns")
		{
			return true;
		}
	}
	else if(level.dynamicHangarSpawns == true)
	{
		if(self.targetname == "second_map_spawns")
		{
			return true;
		}
	}	
	return false;
}
