#include_function".\GameData\StageData\Stage3_BG.txt";

area3_standby=true;

function Stage3
{
	let enemycurrent=GetCurrentScriptDirectory~".\GameData\EnemyData\";
	let enemy=[enemycurrent~"Zako\Stage3\Enemy031.txt",
			   enemycurrent~"Zako\Stage3\Enemy032.txt",
			   enemycurrent~"Zako\Stage3\Enemy033.txt",
			   enemycurrent~"Zako\Stage3\Enemy034.txt",
			   enemycurrent~"Zako\Stage3\Enemy035.txt",
			   enemycurrent~"Zako\Stage3\Enemy036.txt"];
	let boss=[enemycurrent~"Boss\Chen\Chen_Plural.txt",
			  enemycurrent~"Boss\Ran\Ran_Plural.txt",
			  enemycurrent~"Boss\Nue\Nue_Plural.txt",
			  enemycurrent~"Boss\Chen\Chen_Plural2.txt",
			  enemycurrent~"Boss\Ran\Ran_Plural2.txt",
			  enemycurrent~"Boss\Murasa\Murasa_Plural.txt"];
	
	Stage3_Const(STAGEBG_INIT);
	
	wait(120);
	SetBGPosition;
	StageTelop(3);
	SetMusic(bgm[0],BGM_PLAY);
//	test;
	Part1;
	Part2;
	
	Result;
	
	Stage3_Const(STAGEBG_DEST);
	
	yield;
	
	function test
	{
		wait(60);
		stagepart=1;
		CreateEnemyBossFromFile(boss[gamemode+3],maxX+64,minY,0,0,0);
		stagepart=2;
	//	zako06(cenX,3);
		//zako03(maxX,90,1.5,170);
		/*
		Warning;
		wait(180);
		frame_rflg=true;
		wait(80);
		CreateEnemyBossFromFile(boss[gamemode+3],cenX,minY,0,0,0);
		if(gamemode==2){wait(100);}
		memory_flg[2]=true;
		SetMusic(bgm[1],BGM_PLAY);
		waitzero;
		SetMusic(bgm[1],BGM_FADEOUT_10);
		//*/
		loop{yield;}
	}
	
	function Part1
	{
		local
		{
			let sp=0;
			let sx=cenX-150;
			while(sx<=cenX)
			{
				zako01(sx,2+sp);
				sp+=0.3;
				sx+=30;
				wait(6);
			}
			let sx=cenX+150;
			while(sx>=cenX)
			{
				zako01(sx,2+sp);
				sp+=0.3;
				sx+=-30;
				wait(6);
			}
		}
		wait(120);
		local
		{
			let sx=[100,120,60,90,20,150,40];
			let r=1;
			loop(2)
			{
				let sp=0;
				ascent(a in 0..7)
				{
					zako02(cenX-sx[a]*r,3.6+sp,90-10*r);
					sp+=0.2;
					wait(9);
				}
				wait(20);
				r=-r;
			}
		}
		wait(100);
		zako03(maxX,90,1.5,170);
		wait(150);
		local
		{
			let sx=[90,140,60,80,120];
			let sp=0;
			ascent(a in 0..5)
			{
				zako01(cenX+sx[a],2.6+sp);
				sp+=0.2;
				wait(8);
			}
			wait(60);
			ascent(a in 0..4)
			{
				zako02(cenX-sx[a],2+sp,80);
				sp+=0.3;
				wait(15);
			}
		}
		wait(140);
		zako04(cenX,3);
		wait(80);
		local
		{
			let sp=0;
			let sx=cenX-160;
			while(sx<=cenX)
			{
				zako01(sx,2+sp);
				sp+=0.3;
				sx+=40;
				wait(9);
			}
		}
		wait(200);
		zako03(minX,170,1.4,-10);
		wait(90);
		local
		{
			let x=[100,140,60,80,120];
			let s=0;
			ascent(a in 0..5)
			{
				zako02(cenX-x[a],2.6+s,80);
				s+=0.3;
				wait(8);
			}
		}
		wait(120);
		zako04(cenX-70,3);
		wait(80);
		zako_f1(4,1,4,0,6);
		wait(90);
		zako_f1(4,-1,4,0,6);
		wait(160);
		zako04(cenX+100,3.5);
		wait(180);
		zako03(minX,60,1.4,10);
		wait(150);
		zako03(maxX,90,1.4,170);
		wait(60);
		zako_f1(3,-1,4,0,9);
		wait(170);
		zako_f1(3,1,4,0,9);
		wait(80);
		local
		{
			let r=1;
			loop(2)
			{
				let x=[100,140,60,80,120];
				let s=0;
				ascent(a in 0..length(x))
				{
					zako02(cenX+x[a]*r,2.6+s,80);
					s+=0.2;
					wait(8);
				}
				r=-r;
			}
		}
		wait(100);
		loop(2)
		{
			let x=[-50,90,-120,10,150,-160];
			ascent(a in 0..6)
			{
				zako06(cenX+x[a],2.5);
				wait(30);
			}
		}
		wait(200);
		zako04(cenX+60,3);
		zako04(cenX-60,3);
		
		stagepart=1;
		
		wait(200);
		
		if(gamemode==0||gamemode==1)
		{
			CreateEnemyBossFromFile(boss[gamemode],cenX,minY,0,0,0);
		}
		else
		{
			CreateEnemyBossFromFile(boss[gamemode],maxX+64,cenY,0,0,0);
		}
		waitzero;
	}
	
	function Part2
	{
		loop(2)
		{
			zako_f1(4,-1,4,0,8);
			wait(15);
			zako_f1(4,1,4,0,8);
			wait(120);
		}
		wait(80);
		zako06(cenX+140,3);
		zako06(cenX-140,3);
		wait(60);
		zako06(cenX+100,2.5);
		zako06(cenX-100,2.5);
		wait(60);
		local
		{
			let s=0;
			let x=cenX;
			while(x<=cenX+120)
			{
				zako01(x,2+s);
				s+=0.1;
				x+=20;
				wait(8);
			}
			let x=cenX;
			while(x>=cenX-120)
			{
				zako01(x,2+s);
				s+=0.1;
				x+=-20;
				wait(8);
			}
		}
		wait(120);
		zako03(maxX,160,1.4,190);
		wait(100);
		zako_f1(5,1,4,0,8);
		wait(160);
		zako04(cenX,2.5);
		wait(100);
		local
		{
			let sx=[100,120,60,90,20,150,40];
			let sp=0;
			ascent(a in 0..7)
			{
				zako02(cenX+sx[a],3+sp,90-20);
				zako02(cenX-sx[a],3+sp,90+20);
				sp+=0.2;
				wait(9);
			}
		}
		wait(100);
		zako06(cenX+120,4);
		zako06(cenX+90,3);
		zako06(cenX+60,2);
		wait(80);
		zako_f1(3,-1,4,0,9);
		wait(140);
		zako06(cenX-120,4);
		zako06(cenX-90,3);
		zako06(cenX-60,2);
		wait(80);
		zako_f1(3,1,4,0,9);
		wait(140);
		zako03(minX,220,1.4,-10);
		wait(60);
		zako_f1(4,-1,4,0,10);
		
		stagepart=2;
		
		wait(180);
		local
		{
			let sp=0;
			let sx=cenX-150;
			while(sx<=cenX)
			{
				zako01(sx,3+sp);
				sp+=0.15;
				sx+=30;
				wait(6);
			}
			let sx=cenX+150;
			while(sx>=cenX)
			{
				zako01(sx,3+sp);
				sp+=0.15;
				sx+=-30;
				wait(6);
			}
		}
		wait(220);
		zako04(cenX+60,3);
		zako04(cenX-60,3);
		SetMusic(bgm[0],BGM_FADEOUT_5);
		wait(100);
		Warning;
		wait(180);
		frame_rflg=true;
		wait(80);
		CreateEnemyBossFromFile(boss[gamemode+3],cenX,minY,0,0,0);
		if(gamemode==2){wait(100);}
		memory_flg[2]=true;
		SetMusic(bgm[1],BGM_PLAY);
		waitzero;
		SetMusic(bgm[1],BGM_FADEOUT_10);
		wait(240);
	}
	
	task SetBGPosition
	{
		while(stagepart==0){yield;}
		while(bg_eye_viewfrom[2]>-90)
		{
			bg_eye_viewfrom[2]=bg_eye_viewfrom[2]-0.1;
			yield;
		}
		while(stagepart==1){yield;}
		while(bg_eye_viewfrom[0]>600)
		{
			if(bg_fog_perspectiveclip[0]<1000){bg_fog_perspectiveclip[0]=bg_fog_perspectiveclip[0]+1;}
			bg_eye_viewfrom[0]=bg_eye_viewfrom[0]-0.5;
			yield;
		}
	}
	
	
	function zako01(let x,let spd){CreateEnemyFromFile(enemy[0],x,minY,spd,90,0);}
	function zako02(let x,let spd,let ag){CreateEnemyFromFile(enemy[1],x,minY,spd,ag,0);}
	function zako03(let x,let y,let spd,let ag){CreateEnemyFromFile(enemy[2],x,y,spd,ag,0);}
	function zako04(let x,let spd){CreateEnemyFromFile(enemy[3],x,minY,spd,90,0);}
	function zako05(let x,let spd,let ag){CreateEnemyFromFile(enemy[4],x,minY,spd,ag,0);}
	function zako06(let x,let spd){CreateEnemyFromFile(enemy[5],x,minY,spd,90,0);}
	
	task zako_f1(
	let lo, 
	let reb,
	let speed,
	let acs,
	let w
	)
	{
		let r=reb;
		let i=0;
		loop(lo)
		{
			let arg=0;
			let s=0;
			let x=[60,90,10,150,120,140,40,30,120];
			ascent(a in i..i+4)
			{
				zako05(cenX+x[a%length(x)]*r,speed+s,90);
				s+=acs;
				wait(w);
			}
			i++;
		}
	}
	
}