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

ORBITALSUPPORT_MEDIUM_STARTING_AMMO = 8;
ORBITALSUPPORT_BUDDY_MEDIUM_STARTING_AMMO = 6;
ORBITALSUPPORT_BUDDY_TIME_BONUS = 7;

CONST_ORBITALSUPPORT_MODULE_TIME_BONUS = 15;
CONST_ORBITALSUPPORT_105_RELOAD_TIME = 6;
CONST_ORBITALSUPPORT_105_RELOAD_TIME_UPGRADE = 4;
CONST_ORBITALSUPPORT_40_RELOAD_TIME = 3;
CONST_ORBITALSUPPORT_40_RELOAD_TIME_UPGRADE = 2;
CONST_ORBITALSUPPORT_40_BUDDY_RELOAD_TIME = 5;
CONST_ORBITALSUPPORT_40_BUDDY_RELOAD_TIME_UPGRADE = 3;
CONST_ORBITALSUPPORT_ROCKET_RELOAD_TIME = 6;
CONST_ORBITALSUPPORT_ROCKET_RELOAD_TIME_UPGRADE = 4;
CONST_ORBITALSUPPORT_RELOAD_MASH_TIME_RATIO = (50 / CONST_ORBITALSUPPORT_105_RELOAD_TIME); // => 50 ms time decrease per button press / 6 sec max reload time
CONST_ORBITALSUPPORT_CHATTER_TIME = 10;
CONST_ORBITALSUPPORT_SPEED = 123;
CONST_ORBITALSUPPORT_DURATION = 40;
CONST_ORBITALSUPPORT_HEIGHT = 9275; // ac130 was 4212
CONST_ORBITALSUPPORT_RADIUS = 8000; // ac130 was 4566

init()
{	
	// Initialize the per-map overrides
	level.orbitalsupportoverrides 				= SpawnStruct();
	level.orbitalsupportoverrides.spawnOrigin 	= undefined;
	level.orbitalsupportoverrides.spawnAngle 	= undefined;
	level.orbitalsupportoverrides.spawnAngleMax	= undefined;
	level.orbitalsupportoverrides.spawnAngleMin	= undefined;
	level.orbitalsupportoverrides.spawnRadius 	= undefined;
	level.orbitalsupportoverrides.spawnHeight 	= undefined;	
	level.orbitalsupportoverrides.speed 		= undefined;
	level.orbitalsupportoverrides.turretPitch	= undefined;
	level.orbitalsupportoverrides.leftArc		= undefined;
	level.orbitalsupportoverrides.rightArc		= undefined;
	level.orbitalsupportoverrides.topArc		= undefined;
	level.orbitalsupportoverrides.bottomArc		= undefined;
	
	if ( IsDefined( level.orbitalSupportOverrideFunc ) )
	{
		[[level.orbitalSupportOverrideFunc]]();
	}
		
	level.orbitalsupport_use_duration = CONST_ORBITALSUPPORT_DURATION;
	level.orbitalsupport_speed = CONST_ORBITALSUPPORT_SPEED;
	if ( IsDefined( level.orbitalsupportoverrides.speed ) )
		level.orbitalsupport_speed = level.orbitalsupportoverrides.speed;
	
	minimapOrigins = getEntArray( "minimap_corner", "targetname" );
	ospOrigin = (0,0,0);
	
	if ( miniMapOrigins.size )
		ospOrigin = maps\mp\gametypes\_spawnlogic::findBoxCenter( miniMapOrigins[0].origin, miniMapOrigins[1].origin );
	if ( IsDefined( level.orbitalsupportoverrides.spawnOrigin ) )
		ospOrigin = level.orbitalsupportoverrides.spawnOrigin;
	
	level.ospRig = spawn( "script_model", ospOrigin );
	level.ospRig setModel( "c130_zoomrig" );
	level.ospRig.angles = ( 0, 115, 0 );
	level.ospRig hide();
	thread rotatePlane( level.orbitalsupport_speed );
		
	// All of the players in that level that are not using the OSP.
	level._effect[ "orbitalsupport_cloud" ] 					= LoadFX( "vfx/cloud/orbitalsupport_cloud" );
	
	level._effect[ "orbitalsupport_big_muzzle_flash" ]			= LoadFX( "vfx/muzzleflash/orbitalsupport_105mm_wv" );
	level._effect[ "orbitalsupport_medium_muzzle_flash" ]		= LoadFX( "vfx/muzzleflash/orbitalsupport_40mm_wv" );
	level._effect[ "orbitalsupport_rocket_explode_player" ]		= LoadFX( "vfx/explosion/rocket_explosion_distant" );
	
	level._effect[ "orbitalsupport_entry" ]						= LoadFX( "vfx/vehicle/vehicle_osp_enter_clouds_parent" );// Clouds
	level._effect[ "orbitalsupport_entry_complete" ]				= LoadFX( "vfx/vehicle/vehicle_osp_enter_shock" );// Shockwave
	level._effect[ "vehicle_osp_jet" ]							= LoadFX( "vfx/vehicle/vehicle_osp_jet" );// Shockwave
//	level._effect[ "vehicle_osp_jet2" ]							= LoadFX( "vfx/vehicle/vehicle_osp_jet2" );// Shockwave
	level._effect[ "vehicle_osp_jet_lg" ]						= LoadFX( "vfx/vehicle/vehicle_osp_jet_lg" );// Shockwave
	level._effect[ "vehicle_osp_rocket_marker" ]				= LoadFX( "vfx/unique/vfx_marker_killstreak_guide" );
	level._effect[ "vehicle_osp_jet_lg_trl" ]   				= LoadFX( "vfx/vehicle/vehicle_osp_jet_lg_trl" );
	
	level._effect[ "orbitalsupport_entry_flash" ]					= LoadFX( "vfx/vehicle/vehicle_osp_enter_flash" );// Shockwave
	level._effect[ "orbitalsupport_explosion" ]					= LoadFX( "vfx/explosion/vehicle_mil_blimp_explosion" ); // Falling to death
//	level._effect[ "orbitalsupport_explosion_b" ]					= LoadFX( "vfx/explosion/vehicle_mil_blimp_explosion_2" ); // Falling to death
	level._effect[ "orbitalsupport_explosion_jet" ]				= LoadFX( "vfx/explosion/vehicle_mil_blimp_explosion_jet" ); // Falling to death	
	level._effect[ "orbitalsupport_light" ]				= LoadFX( "vfx/lights/vehicle_osp_light" ); // Falling to death	
	
	level.physicsSphereRadius[ "orbitalsupport_40mm_mp" ] = 600;
	level.physicsSphereRadius[ "orbitalsupport_40mmbuddy_mp" ] = 600;
	level.physicsSphereRadius[ "orbitalsupport_105mm_mp" ] = 1000;
	
	level.physicsSphereForce[ "orbitalsupport_40mm_mp" ] = 3.0;
	level.physicsSphereForce[ "orbitalsupport_40mmbuddy_mp" ] = 3.0;
	level.physicsSphereForce[ "orbitalsupport_105mm_mp" ] = 6.0;
	
	level.orbitalsupportInUse = false;
	
	level thread onOrbitalSupportPlayerConnect();
	
	level.killstreakFuncs["orbitalsupport"] = ::tryUseOrbitalSupport;
	
	level.killstreakWieldWeapons["orbitalsupport_105mm_mp"] = "orbitalsupport";
	level.killstreakWieldWeapons["orbitalsupport_40mm_mp"] = "orbitalsupport";
	level.killstreakWieldWeapons["orbitalsupport_40mmbuddy_mp"] = "orbitalsupport";
	level.killstreakWieldWeapons["orbitalsupport_big_turret_mp"] = "orbitalsupport";
//	level.killstreakWieldWeapons["orbitalsupport_medium_turret_mp"] = "orbitalsupport";
//	level.killstreakWieldWeapons["orbitalsupport_small_turret_mp"] = "orbitalsupport";
	level.killstreakWieldWeapons["orbitalsupport_missile_mp"] = "orbitalsupport";
	
	level.orbitalsupport_chatter_timer = 0;
	level.orbitalsupport_buddy_chatter_timer = 0;
	
/#
	SetDvarIfUninitialized( "scr_paladin_center", "0" );
	SetDvarIfUninitialized( "scr_paladin_stay_on_ground", "0" );
	SetDvarIfUninitialized( "scr_paladin_timeout", "0" );
	SetDvarIfUninitialized( "scr_paladin_angle", 0 );
	SetDvarIfUninitialized( "scr_paladin_radius", 0 );
	SetDvarIfUninitialized( "scr_paladin_height", 0 );
	SetDvarIfUninitialized( "scr_paladin_rightArc", 0 );
	SetDvarIfUninitialized( "scr_paladin_leftArc", 0 );
	SetDvarIfUninitialized( "scr_paladin_topArc", 0 );
	SetDvarIfUninitialized( "scr_paladin_bottomArc", 0 );
#/
	
	// Coop Buddy Assistance VO
	game["dialog"][ "assist_mp_paladin" ] = "ks_paladin_joinreq";
	game["dialog"][ "pilot_sup_mp_paladin" ] = "pilot_sup_mp_paladin";
	game["dialog"][ "pilot_aslt_mp_paladin" ] = "pilot_aslt_mp_paladin";
	game["dialog"][ "copilot_sup_mp_paladin" ] = "copilot_sup_mp_paladin";
	game["dialog"][ "copilot_aslt_mp_paladin" ] = "copilot_aslt_mp_paladin";
	
	// chatter
	game["dialog"][ "copilot_enemykill_mp_paladin" ] = "copilot_enemykill_mp_paladin";
	game["dialog"][ "copilot_marked_mp_paladin" ] = "copilot_marked_mp_paladin";
}

tryUseOrbitalSupport( lifeId, modules )
{
	if ( isDefined( level.orbitalsupport_player ) || level.orbitalsupportInUse )
	{
		self iPrintLnBold( &"MP_ORBITALSUPPORT_IN_USE" );
		return false;
	}
	
	level.orbitalsupportInUse = true;
	self thread playerClearOrbitalSupportOnTeamChange();
	
	result = self maps\mp\killstreaks\_killstreaks::initRideKillstreak( "paladin", false, undefined, 3.0 );
	if ( result != "success" )
	{
		level.orbitalsupportInUse = false;
		return false;
	}
	self setUsingRemote( "orbitalsupport" );
	thread setOrbitalSupportPlayer( self, modules );

	self maps\mp\_matchdata::logKillstreakEvent( "orbitalsupport", self.origin );
	
	level.orbitalsupport_planeModel.crashed = undefined;
	
	return true;
}

playerClearOrbitalSupportOnTeamChange()
{
	self endon( "rideKillstreakBlack" );

	self waittill( "joined_team" );
	
	level.orbitalsupportInUse = false;
}

setOrbitalSupportPlayer( player, modules )
{
	self endon ( "orbitalsupport_player_removed" );
	self endon( "disconnect" );
	
	level.orbitalsupport_player = player;
	
	player playerSaveAngles();
	player orbitalsupport_spawn();
	
	level.orbitalsupport_planeModel.incomingMissile = false;
	level.orbitalsupport_planeModel.vehicleType = "paladin";
	level.orbitalsupport_planeModel thread maps\mp\gametypes\_damage::setEntityDamageCallback( 3000, undefined, ::crashPlane, maps\mp\killstreaks\_aerial_utility::heli_ModifyDamage, true );
	level.orbitalsupport_planeModel.modules = modules;
	level.orbitalsupport_planeModel.hasRockets = array_contains( modules, "orbitalsupport_rockets" );
	level.orbitalsupport_planeModel.hasTurret = array_contains( modules, "orbitalsupport_turret" );
	level.orbitalsupport_planeModel.coopOffensive = array_contains( modules, "orbitalsupport_coop_offensive" );
	level.orbitalsupport_planeModel.extraFlare = array_contains( modules, "orbitalsupport_flares" );
	level.orbitalsupport_planeModel.ammoFeeder = array_contains( modules, "orbitalsupport_ammo" );
	level.orbitalsupport_planemodel.player = player;
	
	/#
	thread testCrashing();
	#/
	
	if ( level.orbitalsupport_planeModel.extraFlare )
		numExtraFlares = 1;
	else
		numExtraFlares = 0;
	level.orbitalsupport_planeModel.heliType = "osp";
	level.orbitalsupport_planeModel thread maps\mp\killstreaks\_aerial_utility::heli_flares_monitor( numExtraFlares );	
	
	thread teamPlayerCardSplash( "used_orbitalsupport", player );
	
	player startAC130();
	
	player maps\mp\killstreaks\_aerial_utility::playerDisableStreakStatic();

	player maps\mp\killstreaks\_killstreaks::playerWaittillRideKillstreakComplete();
	
	// with the way we do visionsets we need to wait for the clearRideIntro() is done before we set thermal
	player thread waitSetThermal( 1.0 );
	player thread waitDisableShadows( 1.0 );
	player thread setOSPVisionAndLightSetPerMap( 1.25 );
	player thread clouds();
	
	if ( getDvarInt( "camera_thirdPerson" ) )
		player setThirdPersonDOF( false );
	
	player playerSwitchToTurret( level.orbitalsupport_big_turret );
	player.controlled_orbitalsupport_turret = "medium";
	
	player.reloading_big_orbitalsupport_gun = false;
	player.reloading_medium_orbitalsupport_gun = false;
	player.reloading_rocket_orbitalsupport_gun = false;
	player.reloading_buddy_medium_orbitalsupport_gun = false;
	player.medium_orbitalsupport_ammo = ORBITALSUPPORT_MEDIUM_STARTING_AMMO;
	
	player thread removeOrbitalSupportPlayerOnDisconnect();
	player thread removeOrbitalSupportPlayerOnChangeTeams();
	player thread removeOrbitalSupportPlayerOnSpectate();
	player thread removeOrbitalSupportPlayerOnCrash();
	player thread removeOrbitalSupportPlayerOnGameCleanup();
	
	wait 1;
	maps\mp\gametypes\_hostmigration::waitTillHostMigrationDone();
	
	player playerSetAvailableWeaponsHUD();
	player SetClientOmnvar( "ui_osp_weapon", 1 );
	player SetClientOmnvar( "ui_osp_toggle", 1 );
	player thread waitSetStatic( 0.1 );
	
	player thread pulseOrbitalSupportReloadText();
	player thread shotFired();
	
	duration = level.orbitalsupport_use_duration;
	if ( array_contains( modules, "orbitalsupport_time" ) )
		duration += CONST_ORBITALSUPPORT_MODULE_TIME_BONUS;

/#
	if ( GetDvar( "scr_paladin_timeout", "0" ) != "0" )
		duration = GetDvarFloat( "scr_paladin_timeout", duration );
#/
	player.orbitalsupport_endtime = GetTime() + ( duration * 1000 );
	player SetClientOmnvar( "ui_warbird_countdown", player.orbitalsupport_endtime );
	self NotifyOnPlayerCommand( "orbitalsupport_fire", "+attack" );
	self NotifyOnPlayerCommand( "orbitalsupport_fire", "+attack_akimbo_accessible" );
	
	player thread changeWeapons();
	player thread fireBigOrbitalSupportGun();
	player thread fireMediumOrbitalSupportGun();
	player thread fireRocketOrbitalSupportGun();
	player thread showAerialMarker();
	
	player thread removeOrbitalSupportPlayerAfterTime( duration );
	player thread removeOrbitalSupportPlayerOnCommand();
	
	if ( level.teamBased )
		level thread handleCoopJoining( player );
	
	level thread setupPlayersDuringStreak();

	/#
	thread testOSPFlares( player );
	#/
}


waitSetStatic( delay )
{
	self endon ( "orbitalsupport_player_removed" );
	self endon( "disconnect" );

	wait( delay );
	
	self maps\mp\killstreaks\_aerial_utility::playerEnableStreakStatic();
}

waitSetThermal( delay )
{
	self endon( "disconnect" );
	level endon( "orbitalsupport_player_removed" );
	self endon( "orbitalsupport_player_removed" );
	
	wait( delay	);

	self ThermalVisionFOFOverlayOn();
	
	paladinHeight =  CONST_ORBITALSUPPORT_HEIGHT;
	// Spawn height
	if ( IsDefined( level.orbitalsupportoverrides.spawnHeight ) )
		paladinHeight = level.orbitalsupportoverrides.spawnHeight - level.mapCenter[2];
	
	adsAperature = 0.3;
	adsFocalDistance = paladinHeight;
	normalAperature = 0.3;
	normalFocalDistance = paladinHeight * 0.75;
	focusSpeed = 20;
	aperatureSpeed = 30;
	self thread maps\mp\killstreaks\_aerial_utility::thermalVision( "orbitalsupport_player_removed", adsAperature, adsFocalDistance, normalAperature, normalFocalDistance, focusSpeed, aperatureSpeed );
}

waitDisableShadows( delay )
{
	self endon( "disconnect" );
	level endon( "orbitalsupport_player_removed" );
	self endon( "orbitalsupport_player_removed" );
	
	wait( delay	);

	self SetShadowRendering( false );
}

setOSPVisionAndLightSetPerMap( delay )
{
	self endon( "disconnect" );
	level endon( "orbitalsupport_player_removed" );

	wait( delay );
	
	if ( IsDefined( level.ospvisionset ) )
	{
		self SetClientTriggerVisionSet( level.ospvisionset, 0 );
	}
	
	if ( IsDefined( level.osplightset ) )
	{
		self LightSetForPlayer( level.osplightset );
	}

	self maps\mp\killstreaks\_aerial_utility::handle_player_starting_aerial_view();
}

removeOSPVisionAndLightSetPerMap( delay )
{
	self SetClientTriggerVisionSet( "", delay );
	self LightSetForPlayer( "" );
	self maps\mp\killstreaks\_aerial_utility::handle_player_ending_aerial_view();
}

removeOrbitalSupportPlayerOnCommand()
{
	self endon ( "orbitalsupport_player_removed" );
	
	button_hold_time = 0;
	
	while ( true )
	{
		if ( self UseButtonPressed() )
		{
			button_hold_time += 0.05;
			if ( button_hold_time > 1.0 )
			{
				if ( ( IsDefined( level.orbitalsupport_buddy ) && level.orbitalsupport_buddy.joined == true ) || !IsDefined( level.orbitalsupport_buddy ) )		//Don't let the scorestreak owner to exit until the buddy is fully joined.
				{
					level thread removeOrbitalSupportPlayer( self, false );
					return;
				}
			}
		}
		else
		{
			button_hold_time = 0;
		}
		wait( 0.05 );
	}
}


removeOrbitalSupportPlayerOnGameCleanup()
{
	self endon ( "orbitalsupport_player_removed" );
	
	level waittill ( "game_ended" );
	
	level thread removeOrbitalSupportPlayer( self, false );
}


removeOrbitalSupportPlayerOnCrash()
{
	self endon ( "orbitalsupport_player_removed" );
	
	level.orbitalsupport_planeModel waittill ( "crashing" );
	
	level thread removeOrbitalSupportPlayer( self, false );
}


removeOrbitalSupportPlayerOnDisconnect()
{
	self endon ( "orbitalsupport_player_removed" );
	
	self waittill ( "disconnect" );
	
	level thread removeOrbitalSupportPlayer( self, true );
}


removeOrbitalSupportPlayerOnChangeTeams()
{
	self endon ( "orbitalsupport_player_removed" );
	
	self waittill ( "joined_team" );
	
	level thread removeOrbitalSupportPlayer( self, false);
}


removeOrbitalSupportPlayerOnSpectate()
{
	self endon ( "orbitalsupport_player_removed" );
	
	self waittill_any ( "joined_spectators", "spawned" );
	
	level thread removeOrbitalSupportPlayer( self, false);
}


removeOrbitalSupportPlayerAfterTime( removeDelay )
{
	self endon ( "orbitalsupport_player_removed" );
	
	if ( self _hasPerk( "specialty_blackbox" ) && IsDefined( self.specialty_blackbox_bonus ) )
	{
		removeDelay *= self.specialty_blackbox_bonus;
	}
	
	maps\mp\gametypes\_hostmigration::waitLongDurationWithHostMigrationPause( removeDelay );
	
	if ( IsDefined( level.orbitalsupport_buddy ) )
		maps\mp\gametypes\_hostmigration::waitLongDurationWithHostMigrationPause( ORBITALSUPPORT_BUDDY_TIME_BONUS ); //Give a 7 second time bonus if there is a buddy. This will make sure a buddy will have at least that many seconds in the scorestreak.
	
	level thread removeOrbitalSupportPlayer( self, false );
}


removeOrbitalSupportPlayer( player, disconnected )
{
	player notify ( "orbitalsupport_player_removed" );
	level notify ( "orbitalsupport_player_removed" );
	
	waittillframeend;
	
	level.orbitalsupport_planemodel.player = undefined;
	
	if ( IsDefined( level.orbitalsupport_buddy ) )
	{
		level.orbitalsupport_buddy thread removeOrbitalSupportBuddy( false );
	}
	
	if ( !disconnected )
	{
		player playerResetOSPOmnvars();
		
		player NotifyOnPlayerCommandRemove( "orbitalsupport_fire", "+attack" );
		player NotifyOnPlayerCommandRemove( "orbitalsupport_fire", "+attack_akimbo_accessible" );
		if ( !IsBot( player ) && ( level.orbitalsupport_planeModel.hasRockets || level.orbitalsupport_planeModel.hasTurret ) )
			player NotifyOnPlayerCommandRemove( "switch_orbitalsupport_turret", "weapnext" );
		
		player RemoteControlTurretOff( level.orbitalsupport_big_turret );
		level.orbitalsupport_big_turret Hide();
		player Unlink();
		
		// take the killstreak weapons
		killstreakWeapon = getKillstreakWeapon( "orbitalsupport" );
		player TakeWeapon( killstreakWeapon );
		
		if ( player isUsingRemote() )
			player clearUsingRemote();
		
		maps\mp\killstreaks\_aerial_utility::disableOrbitalThermal( player );
		player SetShadowRendering( true );
		player ThermalVisionFOFOverlayOff();
		player setBlurForPlayer( 0, 0 );
		player removeOSPVisionAndLightSetPerMap( 1.5 );
		player stopAC130();
		
		
		if ( getDvarInt( "camera_thirdPerson" ) )
			player setThirdPersonDOF( true );
		
		if ( isDefined( player.darkScreenOverlay ) )
			player.darkScreenOverlay destroy();
		
		player.reloading_big_orbitalsupport_gun = undefined;
		player.reloading_medium_orbitalsupport_gun = undefined;
		player.reloading_rocket_orbitalsupport_gun = undefined;
		player.reloading_buddy_medium_orbitalsupport_gun = undefined;
		
		player playerRestoreAngles();
	}
	
	if ( isDefined( level.orbitalsupport_planeModel.crashed ) )
	{
		level.orbitalsupport_player = undefined;
		return;
	}
	
	level.orbitalsupport_player = undefined;
	level.orbitalsupport_planeModel StopLoopSound();
	level.orbitalsupport_planeModel playSound( "paladin_orbit_return" );
	
	level.orbitalsupport_planemodel orbitalsupportExit();
}

cleanupOSPEnts()
{
	level.orbitalsupport_planeModel stopLoopSound();
	if ( IsDefined( level.orbitalsupport_targetEnt ) )
	{
		StopFXOnTag( getfx( "vehicle_osp_rocket_marker" ), level.orbitalsupport_targetEnt, "tag_origin" );
		level.orbitalsupport_targetEnt Delete();
	}
	
	if ( IsDefined( level.orbitalsupport_big_turret.muzzleFlashEnt ) )
		level.orbitalsupport_big_turret.muzzleFlashEnt Delete();
	
	if ( IsDefined( level.orbitalsupport_big_turret.mediumMuzzleFlashEnt1 ) )
		level.orbitalsupport_big_turret.mediumMuzzleFlashEnt1 Delete();
	
	if ( IsDefined( level.orbitalsupport_big_turret.mediumMuzzleFlashEnt2 ) )
		level.orbitalsupport_big_turret.mediumMuzzleFlashEnt2 Delete();
	
	if ( IsDefined( level.orbitalsupport_big_turret.mediumMuzzleFlashEnt3 ) )
		level.orbitalsupport_big_turret.mediumMuzzleFlashEnt3 Delete();
	
	if ( IsDefined( level.orbitalsupport_buddy_turret.muzzleFlashEnt ) )
		level.orbitalsupport_buddy_turret.muzzleFlashEnt Delete();
	
	level.orbitalsupport_big_turret turretDeleteSoundEnts();
	level.orbitalsupport_big_turret Delete();
	level.orbitalsupport_buddy_turret turretDeleteSoundEnts();
	level.orbitalsupport_buddy_turret Delete();
	if ( IsDefined( level.orbitalsupport_planeModel.farFlightSound ) )
	{
		level.orbitalsupport_planeModel.farFlightSound StopLoopSound();
		level.orbitalsupport_planeModel.farFlightSound Delete();
	}
	if ( IsDefined( level.orbitalsupport_planeModel.closeFlightSound ) )
	{
		level.orbitalsupport_planeModel.closeFlightSound StopLoopSound();
		level.orbitalsupport_planeModel.closeFlightSound Delete();
	}
	if ( IsDefined( level.orbitalsupport_planemodel.minimapIcon ) )
		level.orbitalsupport_planemodel.minimapIcon Delete();
}


/#
testCrashing()
{
	level endon( "orbitalsupport_player_removed" );

	while ( true )
	{
		waitframe();
		
		if ( GetDvarInt( "scr_paladin_crash", 0 ) == 0 )
			continue;
		
		SetDvar( "scr_paladin_crash", "0" );
		
		level thread crashPlane();
		
		return;
	}
}
#/


/*
=============
///ScriptDocBegin
"Name: orbitalsupport_spawn()"
"Summary: spawns the orbitalsupport plane entity."
"Module: Entity"
"CallOn: player - the user of the orbitalsupport scorestreak."
"Example: player orbitalsupport_spawn();"
"SPMP: MP"
///ScriptDocEnd
=============
*/
orbitalsupport_spawn()
{
	minimapOrigins = getEntArray( "minimap_corner", "targetname" );
	orbitalsupportOrigin = (0,0,0);
	
	if ( miniMapOrigins.size )
	{
		orbitalsupportOrigin = maps\mp\gametypes\_spawnlogic::findBoxCenter( miniMapOrigins[0].origin, miniMapOrigins[1].origin );
		orbitalsupportOrigin = ( orbitalsupportOrigin[0], orbitalsupportOrigin[1], 0 );
	}
	if ( IsDefined( level.orbitalsupportoverrides.spawnOrigin ) )
	{
		orbitalsupportOrigin = level.orbitalsupportoverrides.spawnOrigin;
		orbitalsupportOrigin = ( orbitalsupportOrigin[0], orbitalsupportOrigin[1], 0 );
	}
	
	level.orbitalsupport_planeModel = Spawn( "script_model", orbitalsupportOrigin );
	level.orbitalsupport_planeModel.angles = ( 0, 0, 0 );
	level.orbitalsupport_planeModel setModel( "vehicle_mil_blimp_orbital_platform_ai" );
	level.orbitalsupport_planeModel.owner = self;
	level.orbitalsupport_planeModel make_entity_sentient_mp( self.team );
	
	level.orbitalsupport_planeModel.minimapIcon = spawnPlane( self, "script_model", orbitalsupportOrigin, "compass_objpoint_ac130_friendly", "compass_objpoint_ac130_enemy" );
	level.orbitalsupport_planeModel.minimapIcon SetModel( "tag_origin" );
	level.orbitalsupport_planeModel.minimapIcon LinkToSynchronizedParent( level.orbitalsupport_planemodel, "tag_origin", ( 0, 0, 0 ), ( 0, 0, 0 ) );	
//	level.orbitalsupport_planeModel.minimapIcon Hide();
	
	level.orbitalsupport_planeModel setCanDamage( true );
	level.orbitalsupport_planeModel SetCanRadiusDamage( true );
	level.orbitalsupport_planeModel.maxhealth = 2000;
	level.orbitalsupport_planeModel.health = level.orbitalsupport_planeModel.maxhealth;
	level.orbitalsupport_planemodel.showThreatMarker = false;
	
	level.orbitalsupport_planeModel setRandomOrbitalSupportStartPosition();
	
	level.orbitalsupport_big_turret = spawnOrbitalSupportTurret( "orbitalsupport_big_turret_mp", "orbitalsupport_big_turret", "tag_orbitalsupport_biggun", false );
	level.orbitalsupport_buddy_turret = spawnOrbitalSupportTurret( "orbitalsupport_buddy_turret_mp", "orbitalsupport_small_turret", "tag_orbitalsupport_mediumgun2", true );
	
/#
	if ( GetDvar( "scr_paladin_debug_entry", "0" ) != "0" )
		debugEntrance();
#/
	
	level.orbitalsupport_planeModel thread moveOrbitalSupportToDestination();
}

/#
debugEntrance()
{
	while ( GetDvar( "scr_paladin_debug_entry", "0" ) != "0" )
	{
		level.orbitalsupport_planeModel moveOrbitalSupportToDestination( false );
		
		wait 1;
		
		level.orbitalsupport_planemodel notify( "stopEffects" );

		StopFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_1" );
		StopFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_2" );
		waitframe();
		StopFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_1" );
		StopFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_2" );
		level.orbitalsupport_planemodel ScriptModelClearAnim();
		
		wait 1;
		
		level.orbitalsupport_planemodel.origin += ( 0, 0, 65000 );
	}
}
#/


/*
=============
///ScriptDocBegin
"Name: spawnOrbitalSupportTurret( <turretweaponinfo> , <modelname> , <linktotag> )"
"Summary: returns a misc_turret that is attached to the orbitalsupport platform."
"Module: Entity"
"CallOn: player - the user of the orbitalsupport scorestreak."
"MandatoryArg: <turretweaponinfo>: the turret type from turretsettings.gdt"
"MandatoryArg: <modelname>: the model to use for the turret"
"MandatoryArg: <linktotag>: the tag to which the turret will be attached"
"Example: level.orbitalsupport_big_turret = spawnOrbitalSupportTurret( "orbitalsupport_big_turret_mp", "orbitalsupport_big_turret", "tag_orbitalsupport_biggun" );"
"SPMP: MP"
///ScriptDocEnd
=============
*/
spawnOrbitalSupportTurret( turretweaponinfo, modelname, linktotag, isBuddy )
{
	spawned_turret = SpawnTurret( "misc_turret", level.orbitalsupport_planeModel GetTagOrigin( linktotag ), turretweaponinfo, false );
	spawned_turret.angles = level.orbitalsupport_planeModel GetTagAngles( linktotag );
	spawned_turret SetModel( modelname );
	spawned_turret SetDefaultDropPitch( 45 );
	spawned_turret LinkTo( level.orbitalsupport_planeModel, linktotag, ( 0,0,0 ), ( 0,0,0 ) );
	spawned_turret.owner = undefined;
	spawned_turret.health = 99999;
	spawned_turret.maxHealth = 1000;
	spawned_turret.damageTaken = 0;
	spawned_turret.stunned = false;
	spawned_turret.stunnedTime = 0.0;
	spawned_turret SetCanDamage( false );
	spawned_turret SetCanRadiusDamage( false );
	spawned_turret TurretFireDisable();
	numSounds = 4;
	if ( isBuddy )
		numSounds = 1;
	spawned_turret thread turretSpawnSoundEnts( numSounds, linktotag );
	
	return spawned_turret;
}

turretSpawnSoundEnts( num, linktotag )
{
	self.soundEnts = [];	
	for ( i = 0; i < num; i++ )
	{
		waitframe();
		soundEnt = Spawn( "script_model", self.origin );
		soundEnt SetModel( "tag_origin" );
		soundEnt LinkTo( level.orbitalsupport_planeModel, linktotag, ( 0,0,0 ), ( 0,0,0 ) );
		self.soundEnts[self.soundEnts.size] = soundEnt;
	}
}


/*
=============
///ScriptDocBegin
"Name: pulseOrbitalSupportReloadText()"
"Summary: fades the alpha of the HudElem text for reloading."
"Module: Entity"
"CallOn: a player"
"Example: player thread pulseOrbitalSupportReloadText();"
"SPMP: MP"
///ScriptDocEnd
=============
*/
pulseOrbitalSupportReloadText()
{
	level endon( "orbitalsupport_player_removed" );
	self endon( "orbitalsupport_player_removed" );
	self endon( "switch_orbitalsupport_turret" );
	
	self SetClientOmnvar( "ui_osp_reload_bitfield", 0 );
	
	big_reload_bitfield_value = 1;
	medium_reload_bitfield_value = 2;
	rocket_reload_bitfield_value = 4;
	
	while ( true )
	{
		bitfieldValue = 0;

		if ( self.reloading_big_orbitalsupport_gun )
		{
			bitfieldValue += big_reload_bitfield_value;
		}

		if ( self.reloading_medium_orbitalsupport_gun || self.reloading_buddy_medium_orbitalsupport_gun )
		{
			bitfieldValue += medium_reload_bitfield_value;
		}

		if ( self.reloading_rocket_orbitalsupport_gun )
		{
			bitfieldValue += rocket_reload_bitfield_value;
		}

		self SetClientOmnvar( "ui_osp_reload_bitfield", bitfieldValue );
		
		wait( 0.05 );
	}
}


/*
=============
///ScriptDocBegin
"Name: changeWeapons()"
"Summary: switch between the 3 weapons on the orbitalsupport platform."
"Module: Entity"
"CallOn: a player"
"Example: player thread changeWeapons();"
"SPMP: MP"
///ScriptDocEnd
=============
*/
changeWeapons()
{
	self endon ( "orbitalsupport_player_removed" );
	
	if ( IsBot( self ) )
		return;
	
	hasRockets = level.orbitalsupport_planeModel.hasRockets;
	hasBig = level.orbitalsupport_planeModel.hasTurret;
	
	if ( !hasRockets && !hasBig )
		return;
	
	self NotifyOnPlayerCommand( "switch_orbitalsupport_turret", "weapnext" );
	
	wait( 0.05 );
	
	self SetClientOmnvar( "ui_osp_weapon", 1 );
		
	while ( true )
	{
		self waittill( "switch_orbitalsupport_turret" );
		
		if ( self.controlled_orbitalsupport_turret == "medium" )
		{
			if ( hasRockets )
				self playerSwitchToRocketTurret();
			else
				self playerSwitchToBigTurret();
		}
		else if ( self.controlled_orbitalsupport_turret == "rocket" )
		{
			if ( hasBig )
				self playerSwitchToBigTurret();
			else
				self playerSwitchToMediumTurret();
		}
		else if ( self.controlled_orbitalsupport_turret == "big" )
		{
			self playerSwitchToMediumTurret();
		}
		
		self PlayLocalSound( "paladin_weapon_cycle_plr" );
		//self thread play_sound_on_tag( "ac130_weapon_switch" );
	}
}

playerSetAvailableWeaponsHUD()
{
	hasRockets = level.orbitalsupport_planeModel.hasRockets;
	hasBig = level.orbitalsupport_planeModel.hasTurret;
	
	weaponValue = 1; // mg only
	if ( hasBig )
		weaponValue = weaponValue + 2;
	if ( hasRockets )
		weaponValue = weaponValue + 4;
	self SetClientOmnvar( "ui_osp_avail_weapons", weaponValue );
}

playerSwitchToTurret( turret ) // self == player
{
	self Unlink();
	level thread handleTurretSoundEnt( turret );
	
/#
	if ( GetDvar( "scr_paladin_stay_on_ground", "0" ) != "0" )
		return;
#/
	rightArc	= 25;
	leftArc		= 25;
	topArc		= -25;
	bottomArc	= 60;

	if ( IsDefined( level.orbitalsupportoverrides.rightArc ) )
		rightArc = level.orbitalsupportoverrides.rightArc;
	
	if ( IsDefined( level.orbitalsupportoverrides.leftArc ) )
		leftArc = level.orbitalsupportoverrides.leftArc;
	
	if ( IsDefined( level.orbitalsupportoverrides.topArc ) )
		topArc = level.orbitalsupportoverrides.topArc;
	
	if ( IsDefined( level.orbitalsupportoverrides.bottomArc ) )
		bottomArc = level.orbitalsupportoverrides.bottomArc;
	
/#
	if ( GetDvarFloat( "scr_paladin_rightArc", 0 ) != 0 )
		rightArc = GetDvarFloat( "scr_paladin_rightArc" );
	if ( GetDvarFloat( "scr_paladin_leftArc", 0 ) != 0 )
		leftArc = GetDvarFloat( "scr_paladin_leftArc" );
	if ( GetDvarFloat( "scr_paladin_topArc", 0 ) != 0 )
		topArc = GetDvarFloat( "scr_paladin_topArc" );
	if ( GetDvarFloat( "scr_paladin_bottomArc", 0 ) != 0 )
		bottomArc = GetDvarFloat( "scr_paladin_bottomArc" );
#/
	
	self PlayerLinkWeaponViewToDelta( turret, "tag_player", 0, rightArc, leftArc, topArc, bottomArc, true );
    self PlayerLinkedSetUseBaseAngleForViewClamp( true );
    
	pitch = 45;
	if ( IsDefined( level.orbitalsupportoverrides.turretPitch ) )
		pitch = level.orbitalsupportoverrides.turretPitch;
	
	self RemoteControlTurret( turret, pitch );
}

handleTurretSoundEnt( turret ) // self == level
{
	turret endon( "death" );
	
	turret notify( "startHandleSoundEnt" );
	turret endon( "startHandleSoundEnt" );

/#
	if ( GetDvar( "scr_paladin_stay_on_ground", "0" ) != "0" )
		return;
#/
	
	foreach ( soundEnt in turret.soundEnts )
		soundEnt Hide();
	
	foreach ( player in level.players )
	{
		if ( IsDefined( turret.owner ) && turret.owner != player )
		{
			foreach ( soundEnt in turret.soundEnts )
				soundEnt ShowToPlayer( player );
		}
	}
	
	while ( true )
	{
		level waittill( "connected", player );
		
		foreach ( soundEnt in turret.soundEnts )
			soundEnt ShowToPlayer( player );
	}
}

playerSwitchToBigTurret()
{
	self.controlled_orbitalsupport_turret = "big";
	self SetClientOmnvar( "ui_osp_weapon", 0 );
	self thread pulseOrbitalSupportReloadText();
	if ( IsDefined( level.orbitalsupport_targetEnt ) )
		StopFXOnTag( getfx( "vehicle_osp_rocket_marker" ), level.orbitalsupport_targetEnt, "tag_origin" );
}

playerSwitchToRocketTurret()
{
	self.controlled_orbitalsupport_turret = "rocket";
	self SetClientOmnvar( "ui_osp_weapon", 3 );
	self thread pulseOrbitalSupportReloadText();
	if ( IsDefined( level.orbitalsupport_targetEnt ) )
		PlayFXOnTag( getfx( "vehicle_osp_rocket_marker" ), level.orbitalsupport_targetEnt, "tag_origin" );
}

playerSwitchToMediumTurret()
{
	self.controlled_orbitalsupport_turret = "medium";	
	self SetClientOmnvar( "ui_osp_weapon", 1 );
	self thread pulseOrbitalSupportReloadText();
	if ( IsDefined( level.orbitalsupport_targetEnt ) )
		StopFXOnTag( getfx( "vehicle_osp_rocket_marker" ), level.orbitalsupport_targetEnt, "tag_origin" );
}

playerGetTurretEndpoint( isBuddy )
{
/#
	if ( GetDvar( "scr_paladin_stay_on_ground", "0" ) != "0" && self == level.player )
	{
		start = self GetEye();
		dir = AnglesToForward( self GetPlayerAngles() );
		end = start + ( dir * 3000 );
		trace = BulletTrace( start, end, false, self );
		if ( trace["fraction"] < 1 )
			return trace["position"];
	}
#/
	if ( !IsDefined( isBuddy ) || !isBuddy )
		return level.orbitalsupport_big_turret GetTagOrigin( "tag_player" ) + AnglesToForward( level.orbitalsupport_big_turret GetTagAngles( "tag_player" ) ) * 20000;
	else
		return level.orbitalsupport_buddy_turret GetTagOrigin( "tag_player" ) + AnglesToForward( level.orbitalsupport_buddy_turret GetTagAngles( "tag_player" ) ) * 20000;
}

/*
=============
///ScriptDocBegin
"Name: fireBigOrbitalSupportGun()"
"Summary: uses MagicBullet to fire projectiles for the big orbitalsupport gun."
"Module: Entity"
"CallOn: a player"
"Example: player thread fireBigOrbitalSupportGun();"
"SPMP: MP"
///ScriptDocEnd
=============
*/
fireBigOrbitalSupportGun()
{
	self endon ( "orbitalsupport_player_removed" );
	
	if ( !level.orbitalsupport_planeModel.ammoFeeder )
		reloadTime = CONST_ORBITALSUPPORT_105_RELOAD_TIME;
	else
		reloadTime = CONST_ORBITALSUPPORT_105_RELOAD_TIME_UPGRADE;
	
	while ( !IsDefined( level.orbitalsupport_planeModel.paladinflying ) )
		waitframe();

	if ( !IsDefined( level.orbitalsupport_big_turret.muzzleFlashEnt ) )
		level.orbitalsupport_big_turret.muzzleFlashEnt = spawnMuzzleFlashEnt( level.orbitalsupport_big_turret, "tag_missile1", level.orbitalsupport_player );
	
	while ( true )
	{
		self.reloading_big_orbitalsupport_gun = false;
		
		self waittill( "orbitalsupport_fire" );
		
		if ( isDefined( level.hostMigrationTimer ) )
			continue;
		
		if ( self.controlled_orbitalsupport_turret == "big" )
		{
			endpoint = self playerGetTurretEndpoint();
			startpoint = level.orbitalsupport_big_turret GetTagOrigin( "tag_missile1" );
			bullet_ent = MagicBullet( "orbitalsupport_105mm_mp", startpoint, endpoint, self );
			bullet_ent.vehicle_fired_from = level.orbitalsupport_planeModel;
			level.orbitalsupport_planeModel playsound( "paladin_cannon_snap" );
			bullet_ent playsound( "orbitalsupport_105mm_proj_travel" );
			
			// Play muzzle flash effects on all but the player using the OSP.
			PlayFXOnTag( getfx( "orbitalsupport_big_muzzle_flash" ), level.orbitalsupport_big_turret.muzzleFlashEnt, "tag_origin" );
			
			self playRumbleOnEntity( "ac130_105mm_fire" );
			self PlayLocalSound( "paladin_cannon_reload" );
			Earthquake( 0.3, 1, level.orbitalsupport_planeModel.origin, 1000 );
			
			self.reloading_big_orbitalsupport_gun = true;
			
//			waittillReloadedWithMashHelp( reloadTime, self, "Big" );
			wait reloadTime;
		}
	}
}


/*
=============
///ScriptDocBegin
"Name: fireMediumOrbitalSupportGun()"
"Summary: uses MagicBullet to fire projectiles for the medium orbitalsupport gun."
"Module: Entity"
"CallOn: a player"
"Example: player thread fireMediumOrbitalSupportGun();"
"SPMP: MP"
///ScriptDocEnd
=============
*/
fireMediumOrbitalSupportGun()
{
	self endon ( "orbitalsupport_player_removed" );
	
	while ( !IsDefined( level.orbitalsupport_planeModel.paladinflying ) )
		waitframe();
	
	if ( !IsDefined( level.orbitalsupport_big_turret.muzzleFlashEnt ) )
		level.orbitalsupport_big_turret.muzzleFlashEnt = spawnMuzzleFlashEnt( level.orbitalsupport_big_turret, "tag_missile1", level.orbitalsupport_player );
	
	while ( true )
	{
		self.reloading_medium_orbitalsupport_gun = false;
		
		if ( !level.orbitalsupport_planeModel.ammoFeeder )
			reloadTime = CONST_ORBITALSUPPORT_40_RELOAD_TIME;
		else
			reloadTime = CONST_ORBITALSUPPORT_40_RELOAD_TIME_UPGRADE;
		
		if ( self.controlled_orbitalsupport_turret == "medium" && self AttackButtonPressed() && !IsDefined( level.hostMigrationTimer ) )
		{
			startpoint = level.orbitalsupport_big_turret GetTagOrigin( "tag_missile1" );
			endpoint = self playerGetTurretEndpoint();
			level.orbitalsupport_planeModel playsound( "paladin_mgun_burst_plr" );
			bullet_ent = MagicBullet( "orbitalsupport_40mm_mp", startpoint, endpoint, self );
			bullet_ent.vehicle_fired_from = level.orbitalsupport_planeModel;
			bullet_ent playsound( "paladin_mgun_shot_dist_npc" );
			// Play muzzle flash effects on all but the player using the OSP.
			PlayFXOnTag( getfx( "orbitalsupport_medium_muzzle_flash" ), level.orbitalsupport_big_turret.muzzleFlashEnt, "tag_origin" );
			
			bullettraceresults = BulletTrace( startpoint, endpoint, false );
			
			wait( 0.05 );
			
			Earthquake( 0.1, 0.5, level.orbitalsupport_planeModel.origin, 1000 );
			
			//Dummy set 1
			fireMediumOrbitalSupportVolley( bullettraceresults["position"], "orbitalsupport_40mm_mp" );
			//Dummy set 2
			fireMediumOrbitalSupportVolley( bullettraceresults["position"], "orbitalsupport_40mm_mp" );
			//Dummy set 3
			fireMediumOrbitalSupportVolley( bullettraceresults["position"], "orbitalsupport_40mm_mp" );
			
			self.medium_orbitalsupport_ammo--;
			if ( self.medium_orbitalsupport_ammo <= 0 )
			{
				self.reloading_medium_orbitalsupport_gun = true;
//				waittillReloadedWithMashHelp( reloadTime, self, "Medium" );
				wait reloadTime;
				self.medium_orbitalsupport_ammo = ORBITALSUPPORT_MEDIUM_STARTING_AMMO;
			}
		}
		wait( 0.05 );
	}
}

fireBuddyThreatGrenades()
{
	self endon ( "orbitalsupport_player_removed" );
	
	Assert( level.orbitalsupport_buddy_turret.soundEnts.size > 0 );
	soundent = level.orbitalsupport_buddy_turret.soundEnts[0];
	
	while ( true )
	{
		self waittill( "orbitalsupport_fire" );
		
		self maps\mp\killstreaks\_aerial_utility::playerFakeShootPaintMissile( soundEnt );
		
		wait 2;
	}
}

fireBuddyMediumOrbitalSupportGun()
{
	self endon ( "orbitalsupport_player_removed" );
	
	buddy_orbitalsupport_ammo = ORBITALSUPPORT_BUDDY_MEDIUM_STARTING_AMMO;
	self.reloading_buddy_medium_orbitalsupport_gun = false;
	self.controlled_orbitalsupport_turret = "buddy";
	self thread pulseOrbitalSupportReloadText();
	
	if ( IsDefined( level.orbitalsupport_buddy_turret.muzzleFlashEnt ) )
		level.orbitalsupport_buddy_turret.muzzleFlashEnt Delete();
	level.orbitalsupport_buddy_turret.muzzleFlashEnt = spawnMuzzleFlashEnt( level.orbitalsupport_buddy_turret, "tag_missile1", level.orbitalsupport_buddy );
	
	while ( true )
	{
		self.reloading_buddy_medium_orbitalsupport_gun = false;
		
		if ( !level.orbitalsupport_planeModel.ammoFeeder )
			reloadTime = CONST_ORBITALSUPPORT_40_BUDDY_RELOAD_TIME;
		else
			reloadTime = CONST_ORBITALSUPPORT_40_BUDDY_RELOAD_TIME_UPGRADE;
		
		if ( self AttackButtonPressed() )
		{
			startpoint = level.orbitalsupport_buddy_turret GetTagOrigin( "tag_missile1" );
			endpoint = self playerGetTurretEndpoint( true );
			level.orbitalsupport_planeModel playsound( "paladin_mgun_burst_plr" );
			bullet_ent = MagicBullet( "orbitalsupport_40mmbuddy_mp", startpoint, endpoint, self );
			bullet_ent.vehicle_fired_from = level.orbitalsupport_planeModel;
			bullet_ent playsound( "paladin_mgun_shot_dist_npc" );
			// Play muzzle flash effects on all but the player using the OSP.
			PlayFXOnTag( getfx( "orbitalsupport_medium_muzzle_flash" ), level.orbitalsupport_buddy_turret.muzzleFlashEnt, "tag_origin" );
			
			bullettraceresults = BulletTrace( startpoint, endpoint, false );
			
			waitframe();
			Earthquake( 0.1, 0.5, level.orbitalsupport_planeModel.origin, 1000 );
			
			//Dummy set 1
			fireMediumOrbitalSupportVolley( bullettraceresults["position"], "orbitalsupport_40mmbuddy_mp" );
			//Dummy set 2
			fireMediumOrbitalSupportVolley( bullettraceresults["position"], "orbitalsupport_40mmbuddy_mp" );
			//Dummy set 3
			fireMediumOrbitalSupportVolley( bullettraceresults["position"], "orbitalsupport_40mmbuddy_mp" );
			
			buddy_orbitalsupport_ammo--;
			if ( buddy_orbitalsupport_ammo <= 0 )
			{
				self.reloading_buddy_medium_orbitalsupport_gun = true;
//				waittillReloadedWithMashHelp( reloadTime, self, "BuddyMedium" );
				wait reloadTime;
				buddy_orbitalsupport_ammo = ORBITALSUPPORT_BUDDY_MEDIUM_STARTING_AMMO;
			}
		}
		wait( 0.05 );
	}
}



/*
=============
///ScriptDocBegin
"Name: fireMediumOrbitalSupportVolley()"
"Summary: fires one bullet from each of the 3 "dummy" turrets on the Orbital Support Platform"
"Module: Entity"
"CallOn: N/A"
"MandatoryArg: <endpoint_coords>: vector - the endpoint at which the bullet should be shot."
"Example: fireMediumOrbitalSupportVolley(  );"
"SPMP: MP"
///ScriptDocEnd
=============
*/
fireMediumOrbitalSupportVolley( endpoint_coords, weaponName )
{
	startpoint = level.orbitalsupport_planemodel GetTagOrigin( "tag_orbitalsupport_mediumgun1" );
	randFloatX = RandomFloat( 400 ) - 200;
	randFloatY = RandomFloat( 400 ) - 200;
	bullet_ent = MagicBullet( weaponName, startpoint, ( endpoint_coords[0] + randFloatX, endpoint_coords[1] + randFloatY, endpoint_coords[2] ), self );
	bullet_ent.vehicle_fired_from = level.orbitalsupport_planeModel;
	bullet_ent playsound( "paladin_mgun_shot_dist_npc" );
	self playRumbleOnEntity( "ac130_25mm_fire" );
	
	// Play muzzle flash effects on all but the player using the OSP.
	if ( !IsDefined( level.orbitalsupport_big_turret.mediumMuzzleFlashEnt1 ) )
		level.orbitalsupport_big_turret.mediumMuzzleFlashEnt1 = spawnMuzzleFlashEnt( level.orbitalsupport_planemodel, "tag_orbitalsupport_mediumgun1", level.orbitalsupport_player );	
	PlayFXOnTag( getfx( "orbitalsupport_medium_muzzle_flash" ), level.orbitalsupport_big_turret.mediumMuzzleFlashEnt1, "tag_origin" );
	wait( 0.05 );
	
	startpoint = level.orbitalsupport_planemodel GetTagOrigin( "tag_orbitalsupport_mediumgun0" );
	randFloatX = RandomFloat( 400 ) - 200;
	randFloatY = RandomFloat( 400 ) - 200;
	bullet_ent = MagicBullet( weaponName, startpoint, ( endpoint_coords[0] + randFloatX, endpoint_coords[1] + randFloatY, endpoint_coords[2] ), self );
	bullet_ent.vehicle_fired_from = level.orbitalsupport_planeModel;
	bullet_ent playsound( "paladin_mgun_shot_dist_npc" );
	self playRumbleOnEntity( "ac130_25mm_fire" );
	
	if ( !IsDefined( level.orbitalsupport_big_turret.mediumMuzzleFlashEnt2 ) )
		level.orbitalsupport_big_turret.mediumMuzzleFlashEnt2 = spawnMuzzleFlashEnt( level.orbitalsupport_planemodel, "tag_orbitalsupport_mediumgun0", level.orbitalsupport_player );
	PlayFXOnTag( getfx( "orbitalsupport_medium_muzzle_flash" ), level.orbitalsupport_big_turret.mediumMuzzleFlashEnt2, "tag_origin" );
	wait( 0.05 );
	
	startpoint = level.orbitalsupport_planemodel GetTagOrigin( "tag_orbitalsupport_mediumgun3" );
	randFloatX = RandomFloat( 400 ) - 200;
	randFloatY = RandomFloat( 400 ) - 200;
	bullet_ent = MagicBullet( weaponName, startpoint, ( endpoint_coords[0] + randFloatX, endpoint_coords[1] + randFloatY, endpoint_coords[2] ), self );
	bullet_ent.vehicle_fired_from = level.orbitalsupport_planeModel;
	bullet_ent playsound( "paladin_mgun_shot_dist_npc" );
	self playRumbleOnEntity( "ac130_25mm_fire" );
	
	if ( !IsDefined( level.orbitalsupport_big_turret.mediumMuzzleFlashEnt3 ) )
		level.orbitalsupport_big_turret.mediumMuzzleFlashEnt3 = spawnMuzzleFlashEnt( level.orbitalsupport_planemodel, "tag_orbitalsupport_mediumgun3", level.orbitalsupport_player );
	PlayFXOnTag( getfx( "orbitalsupport_medium_muzzle_flash" ), level.orbitalsupport_big_turret.mediumMuzzleFlashEnt3, "tag_origin" );
	wait( 0.05 );
}


random_vector( num )
{
	return( RandomFloat( num ) - num * 0.5, RandomFloat( num ) - num *0.5, RandomFloat( num ) - num * 0.5 );
}


fireRocketOrbitalSupportGun()
{
	self endon( "orbitalsupport_player_removed" );
	
	rocketClip = 3;
	remainingRocketShots = rocketClip;

	self SetClientOmnvar( "ui_osp_rockets", remainingRocketShots );

	if ( !level.orbitalsupport_planeModel.ammoFeeder )
		reloadTime = CONST_ORBITALSUPPORT_ROCKET_RELOAD_TIME;
	else
		reloadTime = CONST_ORBITALSUPPORT_ROCKET_RELOAD_TIME_UPGRADE;
	
	thread updateShootingLocation();

	while ( !IsDefined( level.orbitalsupport_planeModel.paladinflying ) )
		waitframe();
	
	while ( true )
	{
		self.reloading_rocket_orbitalsupport_gun = false;

		if ( self.controlled_orbitalsupport_turret == "rocket" && self AttackButtonPressed() && !IsDefined( level.hostMigrationTimer ) )
		{
			Earthquake( 0.3, 1, level.orbitalsupport_planeModel.origin, 1000 );
			PlayFXOnTag(level.chopper_fx["rocketlaunch"]["warbird"], level.orbitalsupport_big_turret, "tag_missile1");
			
			missile_tag_origin = level.orbitalsupport_big_turret GetTagOrigin( "tag_missile1" );
			player_forward = VectorNormalize( AnglesToForward( self GetPlayerAngles() ) );
			osp_velocity = VectorNormalize( AnglesToForward( level.orbitalsupport_planeModel GetTagAngles( "tag_origin" ) ) );
			
			for ( i = 0; i < 4; i++ )
			{
				fire_direction = player_forward + ( 0, 0, .4 ) + random_vector( 1 );
				
				missile = MagicBullet( "orbitalsupport_missile_mp", missile_tag_origin, missile_tag_origin + fire_direction, self );
				missile.vehicle_fired_from = level.orbitalsupport_planeModel;
				
				self thread playerFireSounds( "paladin_missile_shot_2d", "paladin_missile_shot_3d", false, i );
				self PlayRumbleOnEntity( "ac130_40mm_fire" );
				
				missile Missile_SetTargetEnt( level.orbitalsupport_targetEnt );
				missile Missile_SetFlightmodeDirect();
				missile thread missilePlayExplodeEffectForPlayer( self );
				
				wait 0.1;
			}
			
			remainingRocketShots--;
			self SetClientOmnvar( "ui_osp_rockets", remainingRocketShots );

			if ( remainingRocketShots == 0 )
			{
				self.reloading_rocket_orbitalsupport_gun = true;
				thread rocketReloadSound( reloadTime );
//				waittillReloadedWithMashHelp( reloadTime, self, "Rocket" );
				wait reloadTime;
				remainingRocketShots = rocketClip;
				self SetClientOmnvar( "ui_osp_rockets", remainingRocketShots );
				self notify( "rocketReloadComplete" );
				continue;
			}
			else
			{
				wait 1;
			}
		}
		waitframe();
	}
}

missilePlayExplodeEffectForPlayer( player ) // self == missile
{
	player endon( "disconnect" );
	player endon( "orbitalsupport_player_removed" );
	
	self waittill( "explode", location );
	
	fxObj = SpawnFXForClient( getfx( "orbitalsupport_rocket_explode_player" ), location, player );
	TriggerFX( fxObj );
	
	wait 5;
	
	fxObj Delete();
}

updateShootingLocation()
{
	self endon( "orbitalsupport_player_removed" );
	
	level.orbitalsupport_targetEnt = Spawn( "script_model", ( 0, 0, 0 ) );
	level.orbitalsupport_targetEnt SetModel( "tag_origin" );
	level.orbitalsupport_big_turret TurretSetGroundAimEntity( level.orbitalsupport_targetEnt );
		
	while ( true )
	{
		startpoint = level.orbitalsupport_big_turret GetTagOrigin( "tag_player" );
		endpoint = level.orbitalsupport_big_turret GetTagOrigin( "tag_player" ) + AnglesToForward( level.orbitalsupport_big_turret GetTagAngles( "tag_player" ) ) * 20000;
		trace = BulletTrace( startpoint, endpoint, false, level.orbitalsupport_big_turret );
		
		point = trace[ "position" ];
				
		level.orbitalsupport_targetEnt.origin = point;
/#		
	if ( GetDvar( "scr_paladin_stay_on_ground", "0" ) != "0" && self == level.player )
	{
		start = self GetEye();
		dir = AnglesToForward( self GetPlayerAngles() );
		end = start + ( dir * 3000 );
		trace = BulletTrace( start, end, false, self );
		if ( trace["fraction"] < 1 )
			level.orbitalsupport_targetEnt.origin = trace["position"];
	}
#/		
		waitframe();
	}
}


rocketReloadSound( reload_time )
{
	self endon( "rocketReloadComplete" );
	self endon( "orbitalsupport_player_removed" );

	missile_count = 3;
	
	self PlayLocalSound( "warbird_missile_reload_bed" );
	
	wait(0.5);

	while ( true )
	{
		self PlayLocalSound( "warbird_missile_reload" );
		wait( reload_time / missile_count );
	}	
}


showAerialMarker()
{
	level.orbitalsupport_planemodel endon( "death" );
	
	while ( !IsDefined( level.orbitalsupport_planeModel.paladinflying ) )
		waitframe();
	
	level.orbitalsupport_planemodel.showThreatMarker = true;
	level.orbitalsupport_planeModel thread maps\mp\killstreaks\_killstreaks::updateAerialKillStreakMarker();
	
	level.orbitalsupport_planemodel waittill_either( "crashing", "leaving" );
	
	level.orbitalsupport_planemodel.showThreatMarker = false;
	level.orbitalsupport_planeModel thread maps\mp\killstreaks\_killstreaks::updateAerialKillStreakMarker();
}


clouds()
{
	self endon ( "orbitalsupport_player_removed" );
	
	wait 6;
	clouds_create();
	while ( true )
	{
		wait( randomfloatrange( 40, 80 ) );
		clouds_create();
	}
}


clouds_create()
{
	if ( ( isdefined( level.playerWeapon ) ) && ( issubstr( tolower( level.playerWeapon ), "25" ) ) )
		return;
	playfxontagforclients( level._effect[ "orbitalsupport_cloud" ], level.orbitalsupport_planeModel, "tag_player", level.orbitalsupport_player );
}


shotFired()
{
	self endon ( "orbitalsupport_player_removed" );
	
	while ( true )
	{
		self waittill( "projectile_impact", weaponName, position, radius );
		
		if ( issubstr( tolower( weaponName ), "105" ) )
		{
			earthquake( 0.4, 1.0, position, 3500 );
		}
		else if ( issubstr( tolower( weaponName ), "40" ) )
		{
			earthquake( 0.2, 0.5, position, 2000 );
		}
		else if ( weaponName == "orbitalsupport_missile_mp" )
		{
			earthquake( 0.3, 0.5, position, 2000 );
		}
		
		if ( getIntProperty( "ac130_ragdoll_deaths", 0 ) )
			thread shotFiredPhysicsSphere( position, weaponName );
			
		wait 0.05;
	}
}


shotFiredPhysicsSphere( center, weapon )
{
	wait 0.1;
	physicsExplosionSphere( center, level.physicsSphereRadius[ weapon ], level.physicsSphereRadius[ weapon ] / 2, level.physicsSphereForce[ weapon ] );
}

crashPlane( attacker, weapon, meansOfDeath, damage )
{
	level.orbitalsupport_planeModel notify( "crashing" );
	level.orbitalsupport_planeModel.crashed = true;
	
	level.orbitalsupport_planeModel maps\mp\gametypes\_damage::onKillstreakKilled( attacker, weapon, meansOfDeath, damage, "paladin_destroyed", undefined, "callout_destroyed_orbitalsupport", true );
	
	thread crashFX();
	
	level.orbitalsupport_planemodel StopSounds();
	playSoundAtPos( level.orbitalsupport_planemodel.origin, "paladin_ground_death" );
	
	waitframe();
	
	cleanupOSPEnts();
	level.orbitalsupport_planemodel Delete();
	level.orbitalsupportInUse = false;
}

crashFX()
{
	tag_engine_b = getOSPTagInfo( "TAG_FX_ENGINE_B" );
	tag_origin= getOSPTagInfo( "tag_origin" );
	tag_belly = getOSPTagInfo( "tag_light_belly" );
	tag_engine_L1 = getOSPTagInfo( "TAG_FX_ENGINE_L_1" );
	tag_engine_L2 = getOSPTagInfo( "TAG_FX_ENGINE_L_2" );
	tag_engine_R1 = getOSPTagInfo( "TAG_FX_ENGINE_R_1" );
	tag_engine_R2 = getOSPTagInfo( "TAG_FX_ENGINE_R_2" );
//	PlayFX( getfx( "orbitalsupport_explosion_b" ), tag_engine_b.origin, tag_engine_b.dir );
	PlayFX( getfx( "orbitalsupport_explosion" ), tag_origin.origin, tag_belly.dir );
	PlayFX( getfx( "orbitalsupport_explosion_jet" ), tag_engine_L1.origin, tag_engine_L1.dir );
	PlayFX( getfx( "orbitalsupport_explosion_jet" ), tag_engine_L2.origin, tag_engine_L2.dir );
	wait(.05);
	PlayFX( getfx( "orbitalsupport_explosion_jet" ), tag_engine_R1.origin, tag_engine_R1.dir );
	PlayFX( getfx( "orbitalsupport_explosion_jet" ), tag_engine_R2.origin, tag_engine_R2.dir );
	
	/*
	tags = [ "tag_player", "tag_light_tail", "tag_light_belly", "tag_orbitalsupport_biggun", "tag_orbitalsupport_mediumgun0", "tag_orbitalsupport_mediumgun1", "tag_orbitalsupport_mediumgun2", "tag_orbitalsupport_mediumgun3", "tag_orbitalsupport_smallgun" ];
	while ( true )
	{
		tags = array_randomize( tags );
		
		foreach ( tag in tags )
		{
			PlayFXOnTag( getfx( "orbitalsupport_explosion" ), level.orbitalsupport_planemodel, tag );
			
			wait RandomFloatRange( 0.5, 2 );
		}
	}
	(*/
}

getOSPTagInfo( tagName )
{
	info = SpawnStruct();
	info.origin = level.orbitalsupport_planemodel GetTagOrigin( tagName );
	info.dir = AnglesToForward( level.orbitalsupport_planemodel GetTagAngles( tagName ) );
	return info;
}

////=================================================================================================================//
////												BUDDY						                                     //
////=================================================================================================================//

handleCoopJoining( player )
{
	splashRef = "orbitalsupport_coop_defensive";
	joinText = &"MP_JOIN_ORBITALSUPPORT_DEF";
	buddyJoinedVO = "pilot_sup_mp_paladin";
	joinedVO = "copilot_sup_mp_paladin";
	if ( level.orbitalsupport_planeModel.coopOffensive )
	{
		splashRef = "orbitalsupport_coop_offensive";
		joinText = &"MP_JOIN_ORBITALSUPPORT_OFF";
		buddyJoinedVO = "pilot_aslt_mp_paladin";
		joinedVO = "copilot_aslt_mp_paladin";
	}
	
	while ( true )
	{
		id = maps\mp\killstreaks\_coop_util::promptForStreakSupport( player.team, joinText, splashRef, "assist_mp_paladin", buddyJoinedVO, player, joinedVO );
		
		level thread watchForJoin( id, player );
		
		result = waittillPromptComplete( "orbitalsupport_buddy_added" );
		
		maps\mp\killstreaks\_coop_util::stopPromptForStreakSupport( id );
		
		if ( !IsDefined( result ) )
			return;
		
		result = waittillPromptComplete( "orbitalsupport_buddy_removed" );
		
		if ( !IsDefined( result ) )
			return;
		
		waittillframeend;
	}
}

waittillPromptComplete( text )
{
	level endon( "orbitalsupport_player_removed" );
	
	level waittill( text );
	
	return true;
}

watchForJoin( id, player )
{
	level endon( "orbitalsupport_player_removed" );
	
	buddy = maps\mp\killstreaks\_coop_util::waittillBuddyJoinedStreak( id );
	
	buddy thread setOrbitalSupportBuddy( player );
}

/*
=============
///ScriptDocBegin
"Name: onOrbitalSupportPlayerConnect()"
"Summary: does stuff to a connecting player for the orbitalsupport scorestreak."
"Module: Entity"
"CallOn: level"
"Example: level thread onOrbitalSupportPlayerConnect();"
"SPMP: MP"
///ScriptDocEnd
=============
*/
onOrbitalSupportPlayerConnect()
{
	level endon( "game_ended" );
	
	while ( true )
	{
		level waittill( "connected", player );
		
		player.orbitalsupport_hold_time = 0;
	}
}


/*
=============
///ScriptDocBegin
"Name: setOrbitalSupportBuddy( <player> )"
"Summary: sets the joining player for the orbitalsupport scorestreak."
"Module: Entity"
"CallOn: the player that is joining the scorestreak."
"MandatoryArg: <player>: the primary player for the scorestreak"
"Example: setOrbitalSupportBuddy( player );"
"SPMP: MP"
///ScriptDocEnd
=============
*/
setOrbitalSupportBuddy( player )
{
	self endon ( "orbitalsupport_player_removed" );
	
	level.orbitalsupport_buddy = self;
	level.orbitalsupport_buddy.joined = false;	
	level.orbitalsupport_buddy_chatter_timer = 0;
	level notify( "orbitalsupport_buddy_added" );
	
	player playerSaveAngles();
	
	setupFlightSounds();
	
	level thread teamPlayerCardSplash( "joined_orbitalsupport", self );
	
	if ( getDvarInt( "camera_thirdPerson" ) )
		self setThirdPersonDOF( false );
	
	self thread PlayerDoRideKillstreak();
	self waittill( "initRideKillstreak_complete", success );
	if ( !success )
		return;
	
	self setUsingRemote( "orbitalsupport" );
	self startAC130();
	self maps\mp\killstreaks\_aerial_utility::playerDisableStreakStatic();

	self playerSwitchToTurret( level.orbitalsupport_buddy_turret );
	
	self thread waitSetStatic( 0.1 );
	self thread waitSetThermal( 1.0 );
	self thread waitDisableShadows( 1.0 );
	self thread setOSPVisionAndLightSetPerMap( 1.25 );
//	self thread setOrbitalSupportBuddyVision();
	
	self thread clouds();

	self.reloading_big_orbitalsupport_gun = false;
	self.reloading_medium_orbitalsupport_gun = false;
	self.reloading_rocket_orbitalsupport_gun = false;
	self.reloading_buddy_medium_orbitalsupport_gun = false;

	if ( IsDefined( level.orbitalsupport_planeModel ) && level.orbitalsupport_planeModel.coopOffensive )
	{
		self SetClientOmnvar( "ui_osp_avail_weapons", 1 );
		self SetClientOmnvar( "ui_osp_weapon", 1 );
		self thread fireBuddyMediumOrbitalSupportGun();
	}
	else
	{
		self NotifyOnPlayerCommand( "orbitalsupport_fire", "+attack" );
		self NotifyOnPlayerCommand( "orbitalsupport_fire", "+attack_akimbo_accessible" );
		self SetClientOmnvar( "ui_osp_weapon", 4 );
		self thread fireBuddyThreatGrenades();
	}

	self thread removeOrbitalSupportBuddyOnDisconnect();
	self thread removeOrbitalSupportBuddyOnChangeTeams();
	self thread removeOrbitalSupportBuddyOnSpectate();
	if ( !IsBot( self ) )
		self thread removeOrbitalSupportBuddyOnCommand();
	
	wait( 0.5 );
	level.orbitalsupport_buddy.joined = true;		//Want to notify the owner of the scorestreak that the buddy is fully joined.
	self SetClientOmnvar( "ui_osp_toggle", 2 );

	self SetClientOmnvar( "ui_warbird_countdown", player.orbitalsupport_endtime );

	coop_primary_num = player getEntityNumber();
	self SetClientOmnvar( "ui_coop_primary_num", coop_primary_num );
}

playerDoRideKillstreak()
{
	result = self maps\mp\killstreaks\_killstreaks::initRideKillstreak( "coop" );
	
	if ( result != "success" )
	{
		self removeOrbitalSupportBuddy( result == "disconnect" );

		self notify( "initRideKillstreak_complete", false );
		return;
	}
	
	self notify( "initRideKillstreak_complete", true );
}

removeOrbitalSupportBuddy( disconnected )
{
	self notify ( "orbitalsupport_player_removed" );
	level notify( "orbitalsupport_buddy_removed" );
	
	if ( !disconnected )
	{
		self playerResetOSPOmnvars();
		self thread removeOSPVisionAndLightSetPerMap( 0.5 );
		self revertVisionSetForPlayer( 0 );
		self NotifyOnPlayerCommandRemove( "ExitButtonDown", "+activate" );
		self NotifyOnPlayerCommandRemove( "ExitButtonUp", "-activate" );
		self NotifyOnPlayerCommandRemove( "ExitButtonDown", "+usereload" );
		self NotifyOnPlayerCommandRemove( "ExitButtonUp", "-usereload" );
		if ( !IsDefined( level.orbitalsupport_planeModel ) || !level.orbitalsupport_planeModel.coopOffensive )
		{
			self NotifyOnPlayerCommandRemove( "orbitalsupport_fire", "+attack" );
			self NotifyOnPlayerCommandRemove( "orbitalsupport_fire", "+attack_akimbo_accessible" );
		}
		
		self RemoteControlTurretOff( level.orbitalsupport_buddy_turret );
		self Unlink();
		
		level.orbitalsupport_buddy_turret Hide();
		
		maps\mp\killstreaks\_aerial_utility::disableOrbitalThermal( self );
		self SetShadowRendering( true );
		self ThermalVisionFOFOverlayOff();
		self setBlurForPlayer( 0, 0 );
		self maps\mp\killstreaks\_aerial_utility::handle_player_ending_aerial_view();
		self stopAC130();
		
		if ( getDvarInt( "camera_thirdPerson" ) )
			self setThirdPersonDOF( true );
		
		if ( isDefined( self.darkScreenOverlay ) )
			self.darkScreenOverlay destroy();
		
		if ( self isUsingRemote() )
			self clearUsingRemote();
		self.reloading_big_orbitalsupport_gun = undefined;
		self.reloading_medium_orbitalsupport_gun = undefined;
		self.reloading_rocket_orbitalsupport_gun = undefined;
		self.reloading_buddy_medium_orbitalsupport_gun = undefined;
		
		self playerRestoreAngles();
		
		self maps\mp\killstreaks\_coop_util::playerResetAfterCoopStreak();	
		self playerRemoteKillstreakShowHud();
	}
	
	level.orbitalsupport_buddy = undefined;
	
	setupFlightSounds();
}

removeOrbitalSupportBuddyOnDisconnect()
{
	self endon ( "orbitalsupport_player_removed" );
	
	self waittill ( "disconnect" );
	
	self thread removeOrbitalSupportBuddy( true );
}


removeOrbitalSupportBuddyOnChangeTeams()
{
	self endon ( "orbitalsupport_player_removed" );
	
	self waittill ( "joined_team" );
	
	self thread removeOrbitalSupportBuddy( false );
}


removeOrbitalSupportBuddyOnSpectate()
{
	self endon ( "orbitalsupport_player_removed" );
	
	self waittill_any ( "joined_spectators", "spawned" );
	
	self thread removeOrbitalSupportBuddy( false );
}


/*
=============
///ScriptDocBegin
"Name: removeOrbitalSupportBuddyOnCommand()"
"Summary: removes the OSP buddy after they hold down the X button for 1/2 of a second."
"Module: Entity"
"CallOn: a player"
"Example: player thread removeOrbitalSupportBuddyOnCommand();"
"SPMP: MP"
///ScriptDocEnd
=============
*/
removeOrbitalSupportBuddyOnCommand()
{
	self endon( "orbitalsupport_player_removed" );
	
	self NotifyOnPlayerCommand( "ExitButtonDown", "+activate" );
	self NotifyOnPlayerCommand( "ExitButtonUp", "-activate" );
	self NotifyOnPlayerCommand( "ExitButtonDown", "+usereload" );
	self NotifyOnPlayerCommand( "ExitButtonUp", "-usereload" );
	
	while ( true )
	{
		self waittill( "ExitButtonDown" );
		
		self thread startOSPBuddyExitCommand();
		self thread cancelOSPBuddyExitCommand();
	}
}


/*
=============
///ScriptDocBegin
"Name: startOSPBuddyExitCommand()"
"Summary: "
"Module: Entity"
"CallOn: a player"
"Example: player thread startOSPBuddyExitCommand();"
"SPMP: MP"
///ScriptDocEnd
=============
*/
startOSPBuddyExitCommand()
{
	self endon( "orbitalsupport_player_removed" );
	self endon( "ExitButtonUp" );
	
	self.osp_buddy_exit = true;
	
	wait( 0.5 );
	
	if ( self.osp_buddy_exit == true )
	{
		self thread removeOrbitalSupportBuddy( false );
	}
}


/*
=============
///ScriptDocBegin
"Name: cancelOSPBuddyExitCommand()"
"Summary: when a player lets go of the X button before 1/2 of a second, it will cancel their exit attempt."
"Module: Entity"
"CallOn: a player"
"Example: player thread cancelOSPBuddyExitCommand();"
"SPMP: MP"
///ScriptDocEnd
=============
*/
cancelOSPBuddyExitCommand()
{
	self endon( "orbitalsupport_player_removed" );
	
	self waittill( "ExitButtonUp" );
	
	self.osp_buddy_exit = false;
}

/*
=============
///ScriptDocBegin
"Name: setRandomOrbitalSupportStartPosition()"
"Summary: sets a random yaw angle for a plane and then moves it to the right by 5000 units."
"Module: Entity"
"CallOn: a plane entity floating above the center of the map."
"Example: level.orbitalsupport_planeModel thread setRandomOrbitalSupportStartPosition();"
"SPMP: MP"
///ScriptDocEnd
=============
*/
setRandomOrbitalSupportStartPosition()
{
	height_modifier = level.mapCenter[2] + CONST_ORBITALSUPPORT_HEIGHT;
	radius_modifier = CONST_ORBITALSUPPORT_RADIUS;
	angle_modifier = ( 0, RandomInt( 360 ), 0 );
		
	// Spawn angle
	if ( IsDefined( level.orbitalsupportoverrides.spawnAngle ) )
		angle_modifier = ( 0, level.orbitalsupportoverrides.spawnAngle, 0 );
	else if ( IsDefined( level.orbitalsupportoverrides.spawnAngleMin ) && IsDefined( level.orbitalsupportoverrides.spawnAngleMax ) )
		angle_modifier = ( 0, RandomIntRange( level.orbitalsupportoverrides.spawnAngleMin, level.orbitalsupportoverrides.spawnAngleMax ), 0 );
	
	// Spawn radius
	if ( IsDefined( level.orbitalsupportoverrides.spawnRadius ) )
		radius_modifier = level.orbitalsupportoverrides.spawnRadius;
	
	// Spawn height
	if ( IsDefined( level.orbitalsupportoverrides.spawnHeight ) )
		height_modifier = level.orbitalsupportoverrides.spawnHeight;
	
	/#
	// Dvar overrides for testing
	PrintLn( "Paladin angle: " + angle_modifier );
	PrintLn( "Paladin radius: " + radius_modifier );
	PrintLn( "Paladin height: " + height_modifier );
	dvarAngle = GetDvarFloat( "scr_paladin_angle", -1.0 );
	dvarRadius = GetDvarFloat( "scr_paladin_radius", -1.0 );
	dvarHeight = GetDvarFloat( "scr_paladin_height", -1.0 );
	if ( dvarAngle > 0 )
		angle_modifier = ( 0, dvarAngle, 0 );
	if ( dvarRadius > 0 )
		radius_modifier = dvarRadius;
	if ( dvarHeight > 0 )
		height_modifier = dvarHeight;
	#/
	
	level.orbitalsupport_planeModel.angles = angle_modifier;
	
	// This will offset the orbitalsupport from the center of the map.
	level.orbitalsupport_planeModel.origin -= VectorNormalize( -1 * AnglesToRight( level.orbitalsupport_planeModel GetTagAngles( "tag_origin" ) ) ) * radius_modifier;
	level.orbitalsupport_planemodel.origin += ( 0, 0, height_modifier );
	
/#
	if ( GetDvar( "scr_paladin_center", "0" ) != "0" )
		level.orbitalsupport_planemodel.origin = ( 0, 0, height_modifier );
#/
	//level.orbitalsupport_planeModel CloakingEnable();
	
	level.orbitalsupport_planeModel.destination2 = SpawnStruct();
	level.orbitalsupport_planeModel.destination2.origin = level.orbitalsupport_planeModel.origin;
	level.orbitalsupport_planeModel.destination2.angles = level.orbitalsupport_planeModel.angles;
	level.orbitalsupport_planeModel.origin += ( 0, 0, 65000);
}


/*
=============
///ScriptDocBegin
"Name: moveOrbitalSupportToDestination()"
"Summary: "
"Module: Entity"
"CallOn: An entity"
"Example: level.orbitalsupport_planeModel thread moveOrbitalSupportToDestination();"
"SPMP: MP"
///ScriptDocEnd
=============
*/
moveOrbitalSupportToDestination( shouldRotate )
{
	self endon( "death" );
	self endon( "crashing" ); 
	level endon( "game_ended" );
	level endon( "orbitalsupport_player_removed" );
	
	if ( !IsDefined( shouldRotate) )
		shouldRotate = true;
	
	thread rotatePlane( 1, "off" );
	level.orbitalsupport_planemodel thread playJetFX();
	
	thread playEntrySoundDelayed();
	level.orbitalsupport_planemodel ScriptModelPlayAnimDeltaMotion( "paladin_ks_callin", "paladin_notetrack" );
	
	if ( IsDefined( level.orbitalsupport_planemodel.owner ) )
		level.orbitalsupport_planemodel.owner thread playerDelayRumble( 1.5 );

	level.orbitalsupport_planeModel waittillmatch( "paladin_notetrack", "engines_full" );
	
	level.orbitalsupport_planeModel waittillmatch( "paladin_notetrack", "downward_stop" );
	if ( IsDefined( level.orbitalsupport_planemodel.owner ) )
	{
		level.orbitalsupport_planemodel.owner StopRumble( "orbital_laser_charge" );
		level.orbitalsupport_planemodel.owner PlayRumbleOnEntity( "ac130_105mm_fire" );
		Earthquake( 0.2, 2, level.orbitalsupport_planeModel.destination2.origin, 1000 );
	}
	
	level.orbitalsupport_planeModel waittillmatch( "paladin_notetrack", "engines_idle" );
	
	if ( shouldRotate )
	{
		level.orbitalsupport_planemodel LinkToSynchronizedParent( level.ospRig, "tag_player" );
		
		thread rotatePlane( level.orbitalsupport_speed );
	}
	
	level.orbitalsupport_planeModel waittillmatch( "paladin_notetrack", "end" );
	
	level.orbitalsupport_planemodel ScriptModelClearAnim();
	level.orbitalsupport_planemodel ScriptModelPlayAnim( "paladin_ks_loop", "paladin_notetrack" );
	
	if ( IsDefined( level.orbitalsupport_planemodel.owner ) )
	{
		level.orbitalsupport_planeModel.closeFlightSound = Spawn( "script_origin", ( 0, 0, 0 ) );
		level.orbitalsupport_planeModel.closeFlightSound LinkToSynchronizedParent( level.orbitalsupport_planeModel, "tag_origin", ( 0, 0, 0 ), ( 0, 0, 0 ) );
		level.orbitalsupport_planeModel.closeFlightSound PlayLoopSound( "paladin_flight_loop_near" );
	}
	
	level.orbitalsupport_planeModel.farFlightSound = Spawn( "script_origin", ( 0, 0, 0 ) );
	level.orbitalsupport_planeModel.farFlightSound LinkToSynchronizedParent( level.orbitalsupport_planeModel, "tag_origin", ( 0, 0, 0 ), ( 0, 0, 0 ) );
	level.orbitalsupport_planeModel.farFlightSound playLoopSound( "paladin_flight_loop_dist" );
	
	setupFlightSounds();
		
	level.orbitalsupport_planeModel.paladinflying = true;
}

setupFlightSounds()
{
	if ( IsDefined( level.orbitalsupport_planeModel.closeFlightSound ) )
	{
		level.orbitalsupport_planeModel.closeFlightSound Hide();
		
		if ( IsDefined( level.orbitalsupport_planemodel.owner ) )
			level.orbitalsupport_planeModel.closeFlightSound ShowToPlayer( level.orbitalsupport_planemodel.owner );
		
		if ( IsDefined( level.orbitalsupport_buddy ) && !level.splitscreen && !bothPlayersSplitscreen( level.orbitalsupport_planemodel.owner, level.orbitalsupport_buddy ) )
			level.orbitalsupport_planeModel.closeFlightSound ShowToPlayer( level.orbitalsupport_buddy );
	}
	
	if ( IsDefined( level.orbitalsupport_planeModel.farFlightSound ) )
	{
		level.orbitalsupport_planeModel.farFlightSound Hide();
		
		foreach ( player in level.players )
		{
			if ( level.splitscreen || ( IsDefined( level.orbitalsupport_planemodel.owner ) && bothPlayersSplitscreen( level.orbitalsupport_planemodel.owner, player ) ) )
				continue;
			
			if ( IsDefined( level.orbitalsupport_planemodel.owner ) && player != level.orbitalsupport_planemodel.owner )
				level.orbitalsupport_planeModel.farFlightSound ShowToPlayer( player );
		}
	}
}

bothPlayersSplitscreen( player1, player2 )
{
	return ( player1 IsSplitscreenPlayer() && player2 IsSplitscreenPlayer() );
}

playerDelayRumble( time ) // self == player
{
	self endon( "disconnect" );
	self endon( "orbitalsupport_player_removed" );
	wait time;
	self PlayRumbleOnEntity( "orbital_laser_charge" );
}

playJetFX()
{
	level.orbitalsupport_planemodel endon( "death" );
	level.orbitalsupport_planemodel endon( "crashing" ); 
	level endon( "game_ended" );
	level endon( "orbitalsupport_player_removed" );
	level.orbitalsupport_planemodel endon( "stopEffects" );

	PlayFXOnTag( getfx( "orbitalsupport_entry" ), level.orbitalsupport_planemodel, "tag_origin" );
	waitframe();
	PlayFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_1" );
	PlayFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_2" );
	PlayFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_1" );
	PlayFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_2" );
	
	waitframe();
	
	PlayFX( getfx( "orbitalsupport_entry_flash" ), level.orbitalsupport_planeModel.destination2.origin );
	
	level.orbitalsupport_planeModel waittillmatch( "paladin_notetrack", "engines_full" );
	PlayFXOnTag( getfx( "orbitalsupport_light" ), level.orbitalsupport_planemodel, "tag_light_tail" );
	PlayFXOnTag( getfx( "orbitalsupport_entry_complete" ), level.orbitalsupport_planemodel, "tag_light_belly" );
	waitframe();
	StopFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_1" );
	StopFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_1" );
	PlayFXOnTag( getfx( "vehicle_osp_jet_lg" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_1" );
	PlayFXOnTag( getfx( "vehicle_osp_jet_lg" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_2" );
	waitframe();
	StopFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_2" );
	StopFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_2" );
	PlayFXOnTag( getfx( "vehicle_osp_jet_lg" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_1" );
	PlayFXOnTag( getfx( "vehicle_osp_jet_lg" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_2" );
	
	level.orbitalsupport_planeModel waittillmatch( "paladin_notetrack", "engines_idle" );

	StopFXOnTag( getfx( "vehicle_osp_jet_lg" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_1" );
	StopFXOnTag( getfx( "vehicle_osp_jet_lg" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_1" );
	PlayFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_1" );
	PlayFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_1" );
	waitframe();
	StopFXOnTag( getfx( "vehicle_osp_jet_lg" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_2" );
	StopFXOnTag( getfx( "vehicle_osp_jet_lg" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_2" );
	PlayFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_2" );
	PlayFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_2" );
	waitframe();
	StopFXOnTag( getfx( "orbitalsupport_entry" ), level.orbitalsupport_planemodel, "tag_origin" );

}

playEntrySoundDelayed()
{
	self endon( "death" );
	self endon( "crashing" ); 
	level endon( "game_ended" );
	level endon( "orbitalsupport_player_removed" );

	wait 1;
	
	playSoundAtPos( level.orbitalsupport_planeModel.destination2.origin, "paladin_orbit_drop" );
}

/*
=============
///ScriptDocBegin
"Name: orbitalsupportExit()"
"Summary: flies the orbitalsupport out of the area."
"Module: Entity"
"CallOn: An entity"
"Example: level.orbitalsupport_planeModel thread orbitalsupportExit();"
"SPMP: MP"
///ScriptDocEnd
=============
*/
orbitalsupportExit()
{
	level.orbitalsupport_planeModel endon( "crashing" );
	
	level.orbitalsupport_planemodel notify( "leaving" );
	
	level.orbitalsupport_planemodel Unlink();
	level.orbitalsupport_planemodel ScriptModelPlayAnimDeltaMotion( "paladin_ks_exit", "paladin_notetrack" );

	StopFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_1" );
	StopFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_2" );
	StopFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_1" );
	StopFXOnTag( getfx( "vehicle_osp_jet" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_2" );
	waitframe();
	PlayFXOnTag( getfx( "vehicle_osp_jet_lg_trl" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_1" );
	PlayFXOnTag( getfx( "vehicle_osp_jet_lg_trl" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_L_2" );
	PlayFXOnTag( getfx( "vehicle_osp_jet_lg_trl" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_1" );
	PlayFXOnTag( getfx( "vehicle_osp_jet_lg_trl" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_R_2" );
	waitframe();
	PlayFXOnTag( getfx( "vehicle_osp_jet_lg_trl" ), level.orbitalsupport_planemodel, "TAG_FX_ENGINE_B" );

	wait 4.8; // 146 frames / 30 fps
	
	cleanupOSPEnts();
	level.orbitalsupport_planeModel Delete();
	level.orbitalsupportInUse = false;
}

setupPlayersDuringStreak() // self == level
{
	level.orbitalsupport_chatter_timer = 0;
	foreach ( player in level.players )
	{
		if ( IsDefined( player.team ) && level.orbitalsupport_planemodel.owner.team == player.team )
		{
			continue;
		}
		else if ( !IsDefined( player.team ) )
		{
			player onPlayerSpawnedDuringStreak();
			continue;
		}
		
		player thread playerMonitorDeath();
		player thread playerMonitorMarkedTarget();
	}
	
	level thread onPlayerConnectDuringStreak();
}

onPlayerConnectDuringStreak() // self == level
{
	level endon( "game_ended" );
	level.orbitalsupport_planemodel.owner endon( "orbitalsupport_player_removed" );
	
	while ( true )
	{
		level waittill( "connected", player );
		
		player thread onPlayerSpawnedDuringStreak();
	}
}

onPlayerSpawnedDuringStreak() // self == player
{
	self waittill( "spawned_player" );
	
	self thread playerMonitorDeath();
	self thread playerMonitorMarkedTarget();
	
	setupFlightSounds();
}

playerMonitorMarkedTarget() // self == player
{
	self endon( "disconnect" );
	
	level.orbitalsupport_planemodel.owner endon( "orbitalsupport_player_removed" );
	
	team = level.orbitalsupport_planemodel.owner.team;
	
	while ( true )
	{
		self waittill( "paint_marked_target", attacker );
		
		if ( self.team == team || !IsDefined( attacker ) )
			continue;
		
		if ( IsDefined( level.orbitalsupport_buddy ) && attacker == level.orbitalsupport_buddy && GetTime() > level.orbitalsupport_buddy_chatter_timer )
		{
			level.orbitalsupport_buddy_chatter_timer = GetTime() + ( CONST_ORBITALSUPPORT_CHATTER_TIME * 1000 );
			if ( !level.orbitalsupport_planeModel.coopOffensive )
				attacker leaderDialogOnPlayer( "copilot_marked_mp_paladin" );
		}
	}
}

playerMonitorDeath() // self == player
{
	self endon( "disconnect" );
	level.orbitalsupport_planemodel.owner endon( "orbitalsupport_player_removed" );
	
	team = level.orbitalsupport_planemodel.owner.team;
	
	while ( true )
	{
		self waittill( "death", attacker, sMeansOfDeath, sWeapon );
		
		if ( self.team == team || !IsDefined( attacker ) )
			continue;
			
		if ( attacker == level.orbitalsupport_planemodel.owner && GetTime() > level.orbitalsupport_chatter_timer )
		{
			level.orbitalsupport_chatter_timer = GetTime() + ( CONST_ORBITALSUPPORT_CHATTER_TIME * 1000 );
			attacker leaderDialogOnPlayer( "copilot_enemykill_mp_paladin" );
		}
		
		if ( IsDefined( level.orbitalsupport_buddy ) && attacker == level.orbitalsupport_buddy && GetTime() > level.orbitalsupport_buddy_chatter_timer )
		{
			level.orbitalsupport_buddy_chatter_timer = GetTime() + ( CONST_ORBITALSUPPORT_CHATTER_TIME * 1000 );
			if ( level.orbitalsupport_planeModel.coopOffensive )
				attacker leaderDialogOnPlayer( "copilot_enemykill_mp_paladin" );
		}
	}
}

//waittillReloadedWithMashHelp( reloadTime, player, type )
//{
//	self endon( "playerReloadTimeout" + type );
//	
//	player thread playerReloadTimeout( reloadTime, type );
//	
//	player NotifyOnPlayerCommand( "UseButtonUp", "-activate" );
//	
//	timeDecreasePerButtonUp = reloadTime * CONST_ORBITALSUPPORT_RELOAD_MASH_TIME_RATIO;
//	reloadTimeMS = reloadTime * 1000;
//	reloadTimeComplete = GetTime() + reloadTimeMS;
//	
//	maxDecreaseTime = reloadTimeMS * 0.2;
//
////	numTimes = 0;
////	startTime = GetTime();
//	
//	while ( GetTime() < reloadTimeComplete )
//	{
//		player waittill( "UseButtonUp" );
//		if ( maxDecreaseTime - timeDecreasePerButtonUp >= 0 )
//		{
//			reloadTimeComplete -= timeDecreasePerButtonUp;
//			maxDecreaseTime -= timeDecreasePerButtonUp;
//		}
////		numTimes++;
//	}
//	
//	player notify( "playerReloadTimeoutMashed" + type );
//	
////	time = (GetTime() - startTime)/1000;
////	timeRemoved = (numTimes * timeDecreasePerButtonUp)/1000;
////	PrintLn( "type: " + type + ", NumUps: " + numTimes + ", TimeRemoved: " + timeRemoved + ", TimeReload: " + time );
//}
//
//playerReloadTimeout( reloadTime, type )
//{
//	self endon( "disconnect" );
//	self endon( "playerReloadTimeoutMashed" + type );
//	
//	wait reloadTime;
//	
//	self notify( "playerReloadTimeout" + type );
//}

playerFireSounds( sound2d, sound3d, isBuddy, index )
{
	turret = level.orbitalsupport_big_turret;
	if ( isBuddy )
		turret = level.orbitalsupport_buddy_turret;
	
	if ( !IsDefined( index ) || index >= turret.soundEnts.size )
		index = 0;
	
	turret.soundEnts[index] PlaySoundOnMovingEnt( sound3d );
	
/#
	if ( GetDvar( "scr_paladin_stay_on_ground", "0" ) != "0" )
		return;
#/
	
	self PlayLocalSound( sound2d );
}

turretDeleteSoundEnts()
{
	foreach ( ent in self.soundEnts )
		ent Delete();
}

playerResetOSPOmnvars()
{
	self SetClientOmnvar( "ui_killstreak_optic", false );
	self SetClientOmnvar( "ui_osp_rockets", 0 );
	self SetClientOmnvar( "ui_osp_avail_weapons", 0 );
	self SetClientOmnvar( "ui_osp_weapon", 0 );
	self SetClientOmnvar( "ui_osp_reload_bitfield", 0 );
	self SetClientOmnvar( "ui_osp_toggle", 0 );
	self SetClientOmnvar( "ui_coop_primary_num", 0 );
	self maps\mp\killstreaks\_aerial_utility::playerDisableStreakStatic();
}

rotatePlane( speed, toggle )
{
	level notify( "stop_rotatePlane_thread" );
	level endon( "stop_rotatePlane_thread" );
	
	if ( !IsDefined( toggle ) )
		toggle = "on";
	
	if (toggle == "on")
	{
		level.ospRig rotateyaw( 360, speed, 0.5 );
		wait speed;
		
		for (;;)
		{
			level.ospRig rotateyaw( 360, speed );
			wait speed;
		}
	}
	else if (toggle == "off")
	{
		slowdownDegrees = 10;
		rotateTime = ( speed / 360 ) * slowdownDegrees;
		level.ospRig rotateyaw( level.ospRig.angles[ 2 ] + slowdownDegrees, rotateTime, 0, rotateTime );
	}
}

spawnMuzzleFlashEnt( parent, tagname, hideFromPlayer )
{
	muzzleEnt = Spawn( "script_model", ( 0, 0, 0 ) );
	muzzleEnt SetModel( "tag_origin" );
	muzzleEnt LinkTo( parent, tagname, ( 0, 0, 0 ), ( 0, 0, 0 ) );
	muzzleEnt Hide();
	foreach ( player in level.players )
	{
		if ( player != hideFromPlayer )
			muzzleEnt ShowToPlayer( player );
	}
	thread onPlayerConnectMuzzleFlashEnt( muzzleEnt );
	return muzzleEnt;
}

onPlayerConnectMuzzleFlashEnt( muzzleEnt )
{
	muzzleEnt endon( "death" );
	
	while ( true )
	{
		level waittill( "connected", player );
		
		self thread onPlayerSpawnedMuzzleFlashEnt( muzzleEnt, player );
	}
}

onPlayerSpawnedMuzzleFlashEnt( muzzleEnt, player )
{
	muzzleEnt endon( "death" );
	player endon( "disconnect" );
	
	player waittill( "spawned_player" );
	
	muzzleEnt ShowToPlayer( player );
}

/#
testOSPFlares( player )
{
	player endon( "orbitalsupport_player_removed" );

	SetDvarIfUninitialized( "scr_osp_flares", "0" );

	cur = "none";
	while ( true )
	{
		if ( GetDvarInt( "scr_osp_flares", 0 ) == 0 )
		{
			waitframe();
			continue;
		}
		
		SetDvar( "scr_osp_flares", "0" );
		
		if ( cur == "none" )
		{
			player SetClientOmnvar( "ui_warbird_flares", 3 );
			cur = "threat";
		}
		else if ( cur == "threat" )
		{
			player SetClientOmnvar( "ui_warbird_flares", 1 );
			cur = "flare1";
		}
		else if ( cur == "flare1" )
		{
			player SetClientOmnvar( "ui_warbird_flares", 2 );
			cur = "flare2";
		}
		else  // if ( cur == "flare2" )
		{
			player SetClientOmnvar( "ui_warbird_flares", 0 );
			cur = "none";
		}
		
		
		waitframe();
	}
}
#/