#include_function".\GameData\StageData\Stage4_BG.txt";
area4_standby=true;

function Stage4
{
	let enemycurrent=GetCurrentScriptDirectory~".\GameData\EnemyData\";
	let enemy=[enemycurrent~"Zako\Stage4\Enemy041.txt",
			   enemycurrent~"Zako\Stage4\Enemy042.txt",
			   enemycurrent~"Zako\Stage4\Enemy043.txt",
			   enemycurrent~"Zako\Stage4\Enemy044.txt",
			   enemycurrent~"Zako\Stage4\Enemy045.txt",
			   enemycurrent~"Zako\Stage4\Enemy046.txt"];
	let boss=[enemycurrent~"Boss\Youmu\Youmu_Plural.txt",
			  enemycurrent~"Boss\Youmu\Youmu_Plural.txt",
			  enemycurrent~"Boss\Utsuho\Utsuho_Plural.txt",
			  enemycurrent~"Boss\Sakuya\Sakuya_Plural.txt",
			  enemycurrent~"Boss\Daiyousei\DaiyouseiAnother_Plural.txt",
			  enemycurrent~"Boss\Utsuho\Utsuho_Plural.txt"];
	
	Stage4_Const(STAGEBG_INIT);
	
	if(gamemode!=2){PlaySE(se[SE_WIND]);}
	wait(60);
	let course=0;
	SetBGPosition;
//	test;
	
	if(gamemode!=2)
	{
		if(AreaSelect)
		{
			MusicLoad("area4_boss.wav");
			stagepart=1;
			SetMusic(bgm[0],BGM_PLAY);
			StageTelop(4);
			Part1;
			Part2;
			Part3;
			
			SetMusic(bgm[0],BGM_FADEOUT_10);
			Warning;
			wait(180);
			frame_rflg=true;
			wait(80);
			CreateEnemyBossFromFile(boss[gamemode],cenX,minY,0,0,0);
			memory_flg[3]=true;
			SetMusic(bgm[1],BGM_PLAY);
			waitzero;
			frame_rflg=false;
			SetMusic(bgm[1],BGM_FADEOUT_10);
			wait(240);
		}
		else
		{
			if(gamemode==0){MusicLoad("area4_boss2.wav");}
			if(gamemode==1){MusicLoad("area3_boss2.wav");}
			stagepart=1;
			course=1;
			SetMusic(bgm[0],BGM_PLAY);
			StageTelop(4);
			Part2;
			Part1;
			Part4;
			
			SetMusic(bgm[0],BGM_FADEOUT_10);
			Warning;
			wait(180);
			frame_rflg=true;
			wait(80);
			CreateEnemyBossFromFile(boss[gamemode+3],cenX,minY,0,0,0);
			memory_flg[3]=true;
			SetMusic(bgm[1],BGM_PLAY);
			waitzero;
			frame_rflg=false;
			SetMusic(bgm[1],BGM_FADEOUT_10);
			wait(240);
		}
	}
	else
	{
		MusicLoad("area4_boss2.wav");
		stagepart=1;
		SetMusic(bgm[0],BGM_PLAY);
		StageTelop(4);
		Part1;
		Part2;
		Part3;
		
		SetMusic(bgm[0],BGM_FADEOUT_10);
		Warning;
		wait(180);
		frame_rflg=true;
		wait(80);
		CreateEnemyBossFromFile(boss[gamemode],cenX,minY,0,0,0);
		memory_flg[3]=true;
		SetMusic(bgm[1],BGM_PLAY);
		waitzero;
		frame_rflg=false;
		SetMusic(bgm[1],BGM_FADEOUT_10);
		wait(240);
	}
	
	
	
	
	
	Result;
	
//	Stage4_Const(STAGEBG_DEST);
	DataDelete;
	
	yield;
	
	function test
	{
		wait(100);
		//zako06(minX,minY);
		/*
		*/
		CreateEnemyBossFromFile(boss[gamemode],cenX,minY,0,0,0);
		loop{yield;}
	}
	
	function Part1
	{
		wait(100);

		
		local
		{
			let sx=[-80,60,-120,150];
			ascent(a in 0..length(sx))
			{
				zako01(cenX+sx[a],3.2);
				wait(20);
			}
		}
		wait(90);
		
		local
		{
			let sy=[100,120,90,150,130,110];
			let sp=0;
			ascent(a in 0..length(sy))
			{
				zako02(minX,minY+sy[a],3+sp,-10);
				sp+=0.4;
				wait(12);
			}
		}
		local
		{
			let sy=[100,120,90,150,130,110];
			let sp=0;
			ascent(a in 0..length(sy))
			{
				zako02(maxX,minY+sy[a],3+sp,190);
				sp+=0.4;
				wait(12);
			}
		}
		wait(70);
		zako03(cenX,3);
		wait(40);
		
		local
		{
			let sx=[-90,90,-120,120];
			ascent(a in 0..length(sx))
			{
				zako01(cenX+sx[a],4);
				wait(40);
			}
		}
		wait(90);
		zako04(cenX-50,2);
		zako04(cenX+50,2);
		wait(100);
		local
		{
			let sx=[-50,-20,-80];
			ascent(a in 0..length(sx))
			{
				zako01(cenX+sx[a],3.5);
				wait(50);
			}
		}
		zako03(cenX+90,2.5);
		wait(100);
		zako_f1(3,-1,4.5,0.2,8);
		wait(150);
		zako04(cenX-60,2);
		zako04(cenX-100,3);
		wait(150);
		local
		{
			let sx=[50,120,30];
			ascent(a in 0..length(sx))
			{
				zako01(cenX+sx[a],4);
				wait(50);
			}
		}
		wait(60);
		local
		{
			let sy=[100,120,90,150,130,110,140];
			let sp=0;
			ascent(a in 0..length(sy))
			{
				let r=1;
				loop(2)
				{
					let sx=cenX+cenX*r;
					let angle=90+120*r;
					zako02(sx,minY+sy[a],3+sp,angle);
					r=-r;
				}
				sp+=0.4;
				wait(12);
			}
		}
		wait(120);
		zako03(cenX-100,3);
		wait(100);
		local
		{
			let sx=[-80,60,-120,150];
			ascent(a in 0..length(sx))
			{
				zako01(cenX+sx[a],4.5);
				wait(20);
			}
		}
		wait(60);
		zako03(cenX+120,3.5);
		wait(160);
		zako06(minX,minY);
		
		wait(250);
		zako_f1(3,1,4,0.2,8);
		wait(120);
		local
		{
			let r=1;
			loop(2)
			{
				let sx=[30,90,150];
				ascent(a in 0..length(sx))
				{
					zako05(cenX+sx[a]*r,minY,4,90);
				}
				wait(120);
				r=-r;
			}
		}
		local
		{
			let r=1;
			let wide=100;
			loop(4)
			{
				wait(40);
				zako05(cenX+wide*r,minY,4,90);
				wide+=10;
				r=-r;
			}
		}
		zako03(cenX,4);
		wait(120);
		zako_f1(2,1,3.5,0.3,9);
		zako_f1(2,-1,3.5,0.3,9);
		wait(80);
		zako04(cenX+60,3.5);
		zako04(cenX-60,3.5);
		wait(160);
		local
		{
			let r=-1;
			loop(2)
			{
				let sx=[30,90,150];
				ascent(a in 0..length(sx))
				{
					zako05(cenX+sx[a]*r,minY,4,90);
				}
				wait(80);
				r=-r;
			}
		}
		local
		{
			let sx=[-40,60,-80,100];
			let sp=0;
			ascent(a in 0..length(sx))
			{
				zako01(cenX+sx[a],3+sp);
				sp+=0.3;
				wait(50);
			}
		}
		wait(70);
		zako03(cenX-90,3.5);
		wait(80);
		zako_f1(3,1,3,0.4,9);
		wait(120);
		local
		{
			let sp=0;
			let wide=40;
			loop(2)
			{
				let sx=cenX-wide/2;
				while(sx<=cenX+wide/2+1)
				{
					zako05(sx,minY,3+sp,90);
					sx+=wide;
				}
				sp+=0.5;
				wide+=50;
				wait(50);
			}
		}
		wait(60);
		zako03(cenX+90,3.5);
		wait(80);
		zako_f1(3,-1,3,0.4,9);
		wait(90);
		local
		{
			let sp=0;
			let sx=[-90,-30,30];
			ascent(a in 0..length(sx))
			{
				zako04(cenX+sx[a],2.5+sp);
				sp+=0.5;
				wait(60);
			}
		}
		local
		{
			let sp=0;
			let wide=40;
			loop(3)
			{
				let bx=cenX+70;
				let sx=bx-wide/2;
				while(sx<=bx+wide/2+1)
				{
					zako05(sx,minY,4.5-sp,90);
					sx+=wide;
				}
				sp+=0.5;
				wide+=40;
				wait(40);
			}
		}
	}
	
	
	function Part2
	{
		wait(100);
		local
		{
			let sp=0;
			let wide=300;
			loop(2)
			{
				let speed=4+sp;
				let sx=cenX-wide/2;
				while(sx<=cenX+wide/2+1)
				{
					zako05(sx,minY,speed,90);
					sx+=60;
				}
				wide-=60;
				wait(90);
			}
		}
		zako03(cenX-100,3);
		wait(60);
		local
		{
			let bx=cenX;
			let sp=0;
			loop(4)
			{
				let sx=[90,120];
				ascent(a in 0..length(sx))
				{
					zako05(bx+sx[a],minY,4.5-sp,90);
				}
				sp+=0.4;
				bx-=60;
				wait(40);
			}
		}
		wait(100);
		local
		{
			let sy=[100,120,90,150,130,110];
			let sp=0;
			ascent(a in 0..length(sy))
			{
				zako02(maxX,minY+sy[a],3+sp,190);
				sp+=0.4;
				wait(8);
			}
		}
		local
		{
			let sy=[100,120,90,150,130,110];
			let sp=0;
			ascent(a in 0..length(sy))
			{
				zako02(minX,minY+sy[a],3+sp,-10);
				sp+=0.4;
				wait(8);
			}
		}
		wait(100);
		local
		{
			let sp=0;
			let sx=-120;
			loop(3)
			{
				zako04(cenX+sx,3+sp);
				sp+=0.4;
				sx+=120;
				wait(70);
			}
		}
		wait(160);
		zako06(maxX,minY);
		wait(150);
		zako_f1(2,1,3.5,0.3,8);
		wait(90);
		zako04(cenX-30,4.5);
		wait(120);
		zako_f1(3,-1,3.5,0.3,8);
		wait(90);
		local
		{
			let sp=0;
			let sx=[60,-30,120,-90];
			ascent(a in 0..length(sx))
			{
				zako01(cenX+sx[a],3+sp);
				sp+=0.4;
				wait(45);
			}
		}
		wait(90);
		zako04(cenX+50,2.5);
		wait(80);
		zako04(cenX+100,3.5);
		wait(90);
		local
		{
			let wide=120;
			let sp=0;
			loop(2)
			{
				let bx=cenX-70;
				let sx=bx-wide/2;
				while(sx<=bx+wide/2+1)
				{
					zako05(sx,minY,3+sp,90);
					sx+=30;
				}
				wait(80);
				sp+=1.2;
				wide+=30;
			}
		}
		wait(80);
		local
		{
			let sy=[100,120,90,150,130,110];
			let sp=0;
			ascent(a in 0..length(sy))
			{
				zako02(maxX,minY+sy[a],3+sp,190);
				sp+=0.4;
				wait(12);
			}
		}
		wait(60);
		local
		{
			let sx=[40,70,100,130,160];
			let sp=0;
			ascent(a in 0..length(sx))
			{
				zako01(cenX+sx[a],2.5+sp);
				sp+=0.4;
				wait(40);
			}
		}
		zako03(cenX,4);
		wait(90);
		zako_f1(3,-1,3,0.4,8);
		wait(100);
		local
		{
			let sx=[40,70,100,130,160];
			let sp=0;
			ascent(a in 0..length(sx))
			{
				zako01(cenX-sx[a],2.5+sp);
				sp+=0.4;
				wait(20);
			}
		}
		wait(90);
		local
		{
			let sx=[40,70,100,130,160];
			let sp=0;
			ascent(a in 0..length(sx))
			{
				zako01(cenX+sx[a],2.5+sp);
				sp+=0.4;
				wait(20);
			}
		}
		wait(120);
		zako04(cenX+40,3);
		zako04(cenX-40,3);
		wait(150);
		zako04(cenX-100,4);
		zako04(cenX+100,4);
		wait(180);
	}
	
	function Part3
	{
		zako06(maxX,minY);
		wait(350);
		zako06(minX,minY+90);
		wait(300);
		zako03(cenX,3);
		wait(100);
		stagepart=2;
	}
	
	function Part4
	{
		zako03(cenX,2);
		wait(200);
		zako06(maxX,cenY);
		wait(240);
		zako_f1(4,1,3,0.4,6);
		wait(180);
		zako_f1(3,-1,3,0.4,8);
		wait(140);
		zako06(minX,minY);
		wait(300);
		zako03(cenX-60,4);
		zako03(cenX+60,4);
		wait(120);
		stagepart=2;
	}
	
	task SetBGPosition
	{
		while(stagepart==0){yield;}
		let r=1;
		if(course){r=-r;}
		Sub;
		while(stagepart==1){yield;}
		while(bg_fog_perspectiveclip[1]>=1200)
		{
			bg_fog_perspectiveclip[1]=bg_fog_perspectiveclip[1]-0.5;
			yield;
		}
		
		
		task Sub
		{
			while(stagenum==4)
			{
				bg_eye_viewfrom[1]=bg_eye_viewfrom[1]+0.2*r;
				yield;
			}
		}
	}
	
	function MusicLoad(let file)
	{
		let dir=[GetCurrentScriptDirectory~".\GameData\SoundData\BGM\"~file];
		bgm=bgm~dir;
		LoadMusic(bgm[length(bgm)-1]);
	}
	function DataDelete
	{
		ascent(a in 0..length(stage_tex)){DeleteGraphic(stage_tex[a]);}
		ascent(a in 0..length(bgm)){DeleteMusic(bgm[a]);}
	}
	
	function zako01(let x,let spd){CreateEnemyFromFile(enemy[0],x,minY,spd,90,0);}
	function zako02(let x,let y,let spd,let ag){CreateEnemyFromFile(enemy[1],x,y,spd,ag,0);}
	function zako03(let x,let spd){CreateEnemyFromFile(enemy[2],x,minY,spd,90,0);}
	function zako04(let x,let spd){CreateEnemyFromFile(enemy[3],x,minY,spd,90,0);}
	function zako05(let x,let y,let spd,let ag){CreateEnemyFromFile(enemy[4],x,y,spd,ag,0);}
	function zako06(let x,let y){CreateEnemyFromFile(enemy[5],x,y,0,0,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)
			{
				zako02(cenX+x[a%length(x)]*r,minY,speed+s,90);
				s+=acs;
				wait(w);
			}
			i++;
		}
	}
}