// * AdmiralMOD by Matthias Lorenz, http://www.cod2mod.com *



sky_precache() {

	precacheModel("xmodel/eldaba_plane_rig");
	precacheModel("xmodel/vehicle_spitfire_flying");
	precacheModel("xmodel/vehicle_stuka_flying");
	precacheModel("xmodel/prop_stuka_bomb");
		

	// the planes
	level._effect["planeshoot"]					= loadfx ("fx/muzzleflashes/mg42hv_far.efx");
  	level._effect["planeenginesmoke"]			= loadfx ("fx/fire/fire_airplane_trail.efx");  
	level._effect["planeexplosion"]				= loadfx ("fx/explosions/eldaba_plane_explosion.efx");


	level._effect["bomb"] 						= loadfx ("fx/explosions/artilleryExp_desert_building.efx");
	level._effectType["bomb"]					= "bomb";
	level._effect["bomb_impact"]				= level._effect["bomb"];
	level._effectType["bomb_impact"]			= level._effectType["bomb"];


	// misc
	level._effect["thin_black_smoke_M"]			= loadfx ("fx/smoke/thin_black_smoke_M.efx");
	level._effect["med_oil_fire"]				= loadfx ("fx/fire/med_oil_fire.efx");
	level._effect["ground_fire_med"]			= loadfx ("fx/fire/ground_fire_med.efx");
	level._effect["dust wind"]					= loadfx ("fx/dust/dust_wind_eldaba.efx");
	level._effect["orange_smoke"]				= loadfx ("fx/smoke/orange_smoke_20sec.efx");
	level._effect["flak_flash"]					= loadfx ("fx/muzzleflashes/flak_flash.efx");
	level._effect["large_woodhit"]				= loadfx ("fx/impacts/large_woodhit.efx");
	level._effect["dust_door_ambush"]			= loadfx ("fx/dust/dust_door_ambush.efx");
	level._effect["cargoship_fire"]				= loadfx ("fx/fire/cargoship_fire.efx");
	level._effect["thin_light_smoke_L"]			= loadfx ("fx/smoke/thin_light_smoke_L.efx");
//	level._effect["distortion_distant"]			= loadfx ("fx/distortion/distortion_eldaba_distance.efx");
	level._effect["distortion_player_effect"]	= loadfx ("fx/distortion/distortion_playereffect_4k_heatwave.efx");
	
	level._effect["mosque explosion"]			= loadfx ("fx/explosions/silo_explode.efx");

}





sky_main()
{
	skynode = getnodearray("sky_center","targetname");
	
	level.planes = [];
	array_thread (skynode, ::sky_loop);
	
	thread sky_dockflyby();	
	if(!level.jumptodocks)
	{
		thread sky_crashing1();
		thread sky_attacking1();
		thread sky_strafing1();
	}
//	if(!level.jumptoend)
//		thread sky_crashing2();
}

sky_explode()
{
	self endon("death");
	self waittillmatch("single anim", "custom = damaged");
	playfx(level._effect["planeexplosion"], self.plane.origin);
	earthquake(1.0, 1.5, self.plane.origin, 5000);
	self.plane playsound (level.scrsound["plane explode"]);
}

sky_delete_plane()
{
	self waittillmatch("single anim", "end");
	self.plane delete();
	self delete();
}
	
sky_drop_bomb(startnode, speed)
{
	//create the bomb
	bomb = spawn("script_origin", startnode.origin);
	
	node1 = startnode;
	node2 = getent(node1.target, "targetname");
	
	vec = vectorNormalize(node2.origin - node1.origin);
	vec = maps\_utility::vectorScale(vec, speed);
	
	
	model = spawn("script_model", startnode.origin);
	model setmodel ("xmodel/prop_stuka_bomb");
	bomb.angles = model.angles;
	model linkto(bomb);
	bomb.angles = (vec + (0,180,0));
	
	bomb moveGravity(vec, 5);
	
	wait 6;
	model unlink();
	bomb delete();
	model delete();
}

sky_crashing2()
{
	trig = getent("look_planecrash2", "targetname");
	trig waittill ("trigger");
	plane = sky_makeplane("well_p_crash", undefined, 180);
	plane thread sky_crashing1_flame();
	plane thread sky_quake();
	plane thread playSoundOnTag(level.scr_sound ["stuka_1"], "tag_prop");
	plane waittill("reached_end_node");
	
	maps\_utility::exploder(9);
	plane notify ("delete me");
	plane delete();
}

sky_crashing1()
{
	end = getvehiclenode("p2", "targetname");
	soundorg = spawn("script_model", end.origin);
	
	time = 26;
	stime = 1.65;
	
	wait (time - stime);
	soundorg playsound(level.scrsound["plane_crashing"]);
	level notify("oh_look_its_got_one");
	
	wait (stime);
	plane = sky_makeplane("p2", undefined, 250);
	soundorg linkto(plane);
	plane thread sky_crashing1_flame();
	plane thread sky_quake2();
	plane waittill("reached_end_node");
	
	
	earthquake(1.0, 1.5, plane.origin, 8000); // scale duration source radius
	maps\_utility::exploder(3);
	plane notify ("delete me");
	plane delete();
	
}
sky_crashing1_flame()
{
	self endon ("death");
	self endon ("delete me");
	while (1)
	{
		playfxOnTag ( level._effect["planeenginesmoke"], self, "tag_engine_left" );
		wait .1;
	}
}

sky_dockflyby()
{
	level waittill ("start_dock_scene");
	
	trig = getent("introdockflyby_lookat","targetname");
	trig waittill("trigger");
	
	plane = sky_makeplane("dockintroflyby");
	plane thread playSoundOnTag(level.scr_sound ["stuka_1"], "tag_prop");
	wait 1;
	plane2 = sky_makeplane("dockintroflyby", "xmodel/vehicle_spitfire_flying");
	plane thread playSoundOnTag(level.scr_sound ["spitfire_1"], "tag_prop");
	
	plane waittill("reached_end_node");
	plane delete();
	plane2 delete();	
}

sky_makeplane(name, type, speed)
{
	node = getVehicleNode( name, "targetname" );
	plane = undefined;
	if(isdefined(type))
	{
		plane = spawnVehicle( type, "plane", "stuka", (0,0,0), (0,0,0) );
		plane setmodel (type);
	}
	else
	{
		plane = spawnVehicle( "xmodel/vehicle_stuka_flying", "plane", "stuka", (0,0,0), (0,0,0) );
		plane setmodel ("xmodel/vehicle_stuka_flying");
	}
	plane attachPath( node );
	plane startPath();
	if(isdefined(speed))
		plane setspeed(speed, speed);
	
	plane thread sky_quake();
	return plane;
}

sky_strafing1()
{
	wait 50;	
	plane = sky_makeplane("intro_straf1", undefined, 220);
	plane.firing = false;
	plane thread sky_quake();
	plane thread sky_fireFX();
	
	wait 5.5;
	plane notify ("yo start firing");
	plane.firing = true;

	wait .5;
	plane thread playSoundOnTag(level.scr_sound ["stuka_1"], "tag_prop");
	wait 1.5;
	plane.firing = false;
	
	plane waittill("reached_end_node");
	plane delete();
}

sky_intro_attacking()
{
	plane = sky_makeplane("lpn", undefined, 150);
	plane thread sky_quake();
////////////////////
	wait 2.5;
	plane thread playSoundOnTag(level.scr_sound ["stuka_3"], "tag_prop");
	wait .8;
	temp 				= getent("dropbomb_entrance","targetname");
	bombnode 			= spawn("script_origin", ((plane getTagOrigin("tag_BIGbomb"))));
	bombnode.target 	= temp.targetname;
	thread sky_drop_bomb(bombnode, 3000);
	level notify ("entrance_bomb_released");
////////////////////	
	plane waittill("reached_end_node");
	plane delete();
}

sky_attacking1()
{
	wait 46.3;
	plane = sky_makeplane("p1", undefined, 220);
	
	node = getvehiclenode("auto242", "targetname");
	plane setwaitnode(node);
	bombnode = getent("dropbomb1","targetname");
	
	plane waittill("reached_wait_node");
	plane thread playSoundOnTag(level.scr_sound ["stuka_2"], "tag_prop");
	thread sky_drop_bomb(bombnode, 4200);
	level notify("tank bomb dropped");
	
	plane waittill("reached_end_node");
	plane delete();
}

#using_animtree("eldaba_dogfight");
sky_anim(node, animation, trigger)
{
	trigger waittill("trigger");
	pnode = spawn("script_origin", node.origin);
	
	pnode.angles = node.angles;//` + (0, -90, 0);
	
	pair = [];
	
	rig 				= spawn("script_model", (0,0,0));
	rig.animname 		= "plane british";
	rig setmodel ("xmodel/eldaba_plane_rig");
	rig hide();
	rig UseAnimTree(#animtree);
	
	plane 				= spawn ("script_model",(0,0,0));
	plane setmodel ("xmodel/vehicle_spitfire_flying");
	plane linkto (rig, "tag_rotator_b", (0,0,0), (0,0,0));
	plane thread playLoopSoundOnTag(level.scr_sound ["spitfire_plane_loop"], "tag_prop");
	rig.plane = plane;
	//plane thread sky_quake();	
	rig linkto (pnode);
	rig.pnode = pnode;
	pair[pair.size] = rig;
	
	rig = undefined;
	plane = undefined;
	
	rig 				= spawn("script_model", (0,0,0));
	rig.animname 		= "plane german";
	rig setmodel ("xmodel/eldaba_plane_rig");
	rig hide();
	rig UseAnimTree(#animtree);
	
	plane 				= spawn ("script_model",(0,0,0));
	plane setmodel ("xmodel/vehicle_stuka_flying");
	plane linkto (rig, "tag_rotator_g", (0,0,0), (0,0,0));
	plane thread playLoopSoundOnTag(level.scr_sound ["stuka_plane_loop"], "tag_prop");
	rig.plane = plane;
	//plane thread sky_quake();	
	rig linkto (pnode);	
	rig.pnode = pnode;	
	pair[pair.size] = rig;
	
	pnode thread maps\_anim::anim_single(pair, animation);
	
	array_thread (pair, ::sky_fire);
}

#using_animtree("eldaba_dogfight");
sky_loop()
{
	node = self;
	pair = [];
	loc = node.origin;
	pnode = spawn("script_origin", loc);
	
	
	rig 				= spawn("script_model", (0,0,0));
	rig.animname 		= "plane british";
	rig setmodel ("xmodel/eldaba_plane_rig");
	rig hide();
	rig UseAnimTree(#animtree);
	
	plane 				= spawn ("script_model",(0,0,0));
	plane setmodel ("xmodel/vehicle_spitfire_flying");
	plane linkto (rig, "tag_rotator_b", (0,0,0), (0,0,0));
	plane thread playLoopSoundOnTag(level.scr_sound ["spitfire_plane_loop"], "tag_prop");
	rig.plane = plane;
	//plane thread sky_quake();	
	rig linkto (pnode);
	rig.pnode = pnode;
	level.planes[level.planes.size] = rig;
	pair[pair.size] = rig;
	
	
	rig = undefined;
	plane = undefined;
	
	rig 				= spawn("script_model", (0,0,0));
	rig.animname 		= "plane german";
	rig setmodel ("xmodel/eldaba_plane_rig");
	rig hide();
	rig UseAnimTree(#animtree);
	
	plane 				= spawn ("script_model",(0,0,0));
	plane setmodel ("xmodel/vehicle_stuka_flying");
	plane linkto (rig, "tag_rotator_g", (0,0,0), (0,0,0));
	plane thread playLoopSoundOnTag(level.scr_sound ["stuka_plane_loop"], "tag_prop");
	rig.plane = plane;
	//plane thread sky_quake();	
	rig linkto (pnode);	
	rig.pnode = pnode;	
	level.planes[level.planes.size] = rig;
	pair[pair.size] = rig;
	
	pnode.angles = node.angles;
	
	wait(randomfloat(1.5));
	pnode thread maps\_anim::anim_loop(pair, "loop");//, undefined, "stop plane loop");
	pnode = undefined;

	array_thread (pair, ::sky_fire);
}

sky_fireFX()
{
	self endon ("death");
	
	while(1)
	{	
		while(!self.firing)
			self waittill ("yo start firing");
		
		self thread playLoopSoundOnTag(level.scr_sound ["stuka gun loop"], "tag_gunLeft");
		self thread playLoopSoundOnTag(level.scr_sound ["stuka gun loop"], "tag_gunRight");
	
		while(self.firing)
		{
			//find orientation
			ang = anglesToForward (self.angles);
			ang2 = ang;
			ang = maps\_utility::vectorScale(ang, 5000);
			ang2 = maps\_utility::vectorScale(ang2, 700);
			
			//left
			playfxOnTag ( level._effect["planeshoot"], self, "tag_gunLeft" );
			org = self gettagorigin("tag_gunLeft");
			org = org+ang2;
			bulletTracer(org, org + ang);
			
			//right
			playfxOnTag ( level._effect["planeshoot"], self, "tag_gunRight" );
			org = self gettagorigin("tag_gunRight");
			org = org+ang2;
			bulletTracer(org, org + ang);

			wait (0.05);
		}
		self notify("stop sound" + level.scr_sound["stuka gun loop"]);	
	}

}

sky_fire()
{
	self endon ("death");
	self.plane endon("death");
	self.plane.firing = false;
	self.plane thread sky_fireFX();
	while(1)
	{
		self waittill("looping anim", notetrack);
		if(notetrack == "fire = start")
		{
			self.plane.firing = true;
			self.plane notify ("yo start firing");	
		}
		else if(self.plane.firing == true && notetrack == "fire = end")
			self.plane.firing = false;	
	}
}

sky_quake()
{
	self endon("death");
	while (1)
	{
		earthquake(0.5, 0.1, self.origin, 3000); // scale duration source radius
		wait (0.1);
	}
}

sky_quake2()
{
	self endon("death");
	while (1)
	{
		earthquake(0.75, 0.1, self.origin, 4000); // scale duration source radius
		wait (0.1);
	}
}

sky_killplanes()
{
	for(i=0;i<level.planes.size;i++)
		level.planes[i].plane delete();
}




end_flybywait()
{
	level endon("end_flyby");
	self waittill("trigger");
	level notify("end_flyby");
}

end_flyby()
{
	level waittill("end_mission");
	trigger = getentarray("topgun_flyby", "targetname");
	array_thread(trigger, ::end_flybywait);
	
	level waittill("end_flyby");
	
	plane1 = sky_makeplane("endflyby1", "xmodel/vehicle_spitfire_flying");
	plane1 thread playSoundOnTag(level.scr_sound ["spitfire_2"], "tag_prop");
	plane2 = sky_makeplane("endflyby2", "xmodel/vehicle_spitfire_flying");
	plane2 thread playSoundOnTag(level.scr_sound ["spitfire_3"], "tag_prop");
	
	wait 5;
	plane3 = sky_makeplane("endflyby1", "xmodel/vehicle_spitfire_flying");
	plane3 thread playSoundOnTag(level.scr_sound ["spitfire_1"], "tag_prop");
	plane4 = sky_makeplane("endflyby2", "xmodel/vehicle_spitfire_flying");
	plane4 thread playSoundOnTag(level.scr_sound ["spitfire_2"], "tag_prop");
	
	plane1 waittill("reached_end_node");
	plane1 delete();
	plane2 delete();
	
	plane3 waittill("reached_end_node");
	plane3 delete();
	plane4 delete();
}




