task Stage4
{
			StageProgress=4;
			CreateEnemyFromFile(csd~"stage4/LoadEnemy.txt",0,0,0,0,0);
			PlayMusic(BGM[7]);
			wait(180);
			Stage4Unit1;
			wait(180);
			Stage4Unit2;
			wait(180);
			Stage4Unit3;
			wait(180);
			Stage4Unit1;
			wait(180);
			Stage4Unit3;
			wait(180);
			Stage4Unit2;
			wait(600);

			Stage4Unit7;
			wait(600);	
			Stage4Unit4;
			wait(60);
			Stage4Unit1;
			wait(150);
			Stage4Unit2;
			wait(150);
			Stage4Unit1;
			wait(150);
			Stage4Unit2;
			wait(150);
			Stage4Unit1;
			wait(150);
			Stage4Unit2;
			wait(480);
			alternative(SelectedDifficult)
			case("Easy")
			{
				CreateEnemyBossFromFile(csd~"stage4/MiddleBossEasy.txt",GetCenterX+60,GetClipMinY,2,0,0);
			}
			case("Normal")
			{
				CreateEnemyBossFromFile(csd~"stage4/MiddleBossNormal.txt",GetCenterX+60,GetClipMinY,2,0,0);
			}
			case("Hard")
			{
				CreateEnemyBossFromFile(csd~"stage4/MiddleBossHard.txt",GetCenterX+60,GetClipMinY,2,0,0);
			}
			case("Lunatic")
			{
				CreateEnemyBossFromFile(csd~"stage4/MiddleBossLunatic.txt",GetCenterX+60,GetClipMinY,2,0,0);
			}

			WaitForZeroEnemy();
			wait(120);
			CreateEnemyFromFile(csd~"stage4/Stage4Enemy04.txt",GetCenterX-120,GetClipMinY,1,90,2);
			CreateEnemyFromFile(csd~"stage4/Stage4Enemy04.txt",GetCenterX+120,GetClipMinY,1,90,2);
			wait(240);
			Stage4Unit5;
			wait(150);
			Stage4Unit6;
			wait(150);
			Stage4Unit5;
			Stage4Unit6;
			wait(300);
			CreateEnemyFromFile(csd~"stage4/Stage4Enemy03.txt",GetCenterX-150,GetClipMinY,1,90,2);
			CreateEnemyFromFile(csd~"stage4/Stage4Enemy03.txt",GetCenterX+150,GetClipMinY,1,90,2);
			wait(90);
			CreateEnemyFromFile(csd~"stage4/Stage4Enemy03.txt",GetCenterX-100,GetClipMinY,1,90,2);
			CreateEnemyFromFile(csd~"stage4/Stage4Enemy03.txt",GetCenterX+100,GetClipMinY,1,90,2);
			wait(90);
			CreateEnemyFromFile(csd~"stage4/Stage4Enemy03.txt",GetCenterX-50,GetClipMinY,1,90,2);
			CreateEnemyFromFile(csd~"stage4/Stage4Enemy03.txt",GetCenterX+50,GetClipMinY,1,90,2);
			wait(120);
			Stage4Unit1;
			wait(120);
			CreateEnemyFromFile(csd~"stage4/Stage4Enemy04.txt",GetCenterX+120,GetClipMinY,1,90,2);
			wait(60);
			Stage4Unit3;
			wait(120);
			Stage4Unit2;
			wait(120);
			CreateEnemyFromFile(csd~"stage4/Stage4Enemy04.txt",GetCenterX-120,GetClipMinY,1,90,2);
			wait(120);
			Stage4Unit3a;
			wait(420);
			Stage4Unit8;
			wait(840);
			attack;
			wait(360);
			PlayMusic(BGM[8]);
			CreateEnemyFromFile(csd~"stage4/DeleteEnemy.txt",0,0,0,0,0);
			alternative(SelectedDifficult)
			case("Easy")
			{
				CreateEnemyBossFromFile(csd~"stage4/Stage4BossEasy.txt",GetCenterX,GetClipMinY,2,0,0);
			}
			case("Normal")
			{
				CreateEnemyBossFromFile(csd~"stage4/Stage4BossNormal.txt",GetCenterX,GetClipMinY,2,0,0);
			}
			case("Hard")
			{
				CreateEnemyBossFromFile(csd~"stage4/Stage4BossHard.txt",GetCenterX,GetClipMinY,2,0,0);
			}
			case("Lunatic")
			{
				CreateEnemyBossFromFile(csd~"stage4/Stage4BossLunatic.txt",GetCenterX,GetClipMinY,2,0,0);
			}
			WaitForZeroEnemy();
			wait(300);
			StageProgress=5;
}

task Stage4Unit1
{
	ascent(let i in 0..5)
	{
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy01.txt",GetClipMinX-i*10,GetCenterY-90+i*30,5,-10,2);
	wait(45);
	}
}

task Stage4Unit2
{
	ascent(let i in 0..5)
	{
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy01.txt",GetClipMaxX+i*10,GetCenterY-90+i*30,5,190,2);
	wait(45);
	}
}

task Stage4Unit3
{
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy02.txt",GetCenterX-100,GetClipMinY,1,90,2);
	wait(120);
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy02.txt",GetCenterX,GetClipMinY,1,90,2);
	wait(120);
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy02.txt",GetCenterX+100,GetClipMinY,1,90,2);
}

task Stage4Unit3a
{
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy02.txt",GetCenterX+100,GetClipMinY,1,90,2);
	wait(60);
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy02.txt",GetCenterX,GetClipMinY,1,90,2);
	wait(60);
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy02.txt",GetCenterX-100,GetClipMinY,1,90,2);
}

task Stage4Unit4
{
ascent(let i in 0..3)
{
			CreateEnemyFromFile(csd~"stage4/Stage4Enemy04.txt",GetCenterX-60-30*i,GetClipMinY,1,90,2);
			wait(180);
			CreateEnemyFromFile(csd~"stage4/Stage4Enemy04.txt",GetCenterX+60+30*i,GetClipMinY,1,90,2);
			wait(180);
}
}

task Stage4Unit5
{
	ascent(let i in 0..8)
	{
		CreateEnemyFromFile(csd~"stage4/Stage4Enemy05.txt",GetClipMinX,GetCenterY-50+rand(-20,20),2.5,0,1);
		wait(20);
	}
}

task Stage4Unit6
{
	ascent(let i in 0..8)
	{
		CreateEnemyFromFile(csd~"stage4/Stage4Enemy05.txt",GetClipMaxX,GetCenterY-50+rand(-20,20),2.5,180,1);
		wait(20);
	}
}


task Stage4Unit7
{
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy06.txt",GetCenterX-80,GetClipMinY-16,1,90,2);
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy06.txt",GetCenterX+80,GetClipMinY-16,1,90,2);
	wait(60);
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy06.txt",GetCenterX+80,GetClipMinY-16,1,90,2);
	wait(45);
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy06.txt",GetCenterX+120,GetClipMinY-16,1,90,2);
	wait(45);
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy06.txt",GetCenterX-80,GetClipMinY-16,1,90,2);
	wait(45);
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy06.txt",GetCenterX-80,GetClipMinY-16,1,90,2);
	wait(45);
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy06.txt",GetCenterX-120,GetClipMinY-16,1,90,2);
	wait(45);
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy06.txt",GetCenterX,GetClipMinY-16,1,90,2);
	wait(45);
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy06.txt",GetCenterX-40,GetClipMinY-16,1,90,2);
	wait(45);
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy06.txt",GetCenterX+40,GetClipMinY-16,1,90,2);
	wait(45);
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy06.txt",GetCenterX-80,GetClipMinY-16,1,90,2);
	wait(45);
	CreateEnemyFromFile(csd~"stage4/Stage4Enemy06.txt",GetCenterX+80,GetClipMinY-16,1,90,2);
	wait(45);
}

task Stage4Unit8
{
			loop(2)
			{			
				CreateEnemyFromFile(csd~"stage4/Stage4Enemy07.txt",GetCenterX+120,GetClipMinY,1,90,2);
				CreateEnemyFromFile(csd~"stage4/Stage4Enemy07.txt",GetCenterX,GetClipMinY,1,90,2);
				CreateEnemyFromFile(csd~"stage4/Stage4Enemy07.txt",GetCenterX-120,GetClipMinY,1,90,2);
				wait(120);
				CreateEnemyFromFile(csd~"stage4/Stage4Enemy07.txt",GetCenterX+40,GetClipMinY,1,90,2);
				CreateEnemyFromFile(csd~"stage4/Stage4Enemy07.txt",GetCenterX-40,GetClipMinY,1,90,2);
				wait(120);
				CreateEnemyFromFile(csd~"stage4/Stage4Enemy07.txt",GetCenterX+150,GetClipMinY,1,90,2);
				CreateEnemyFromFile(csd~"stage4/Stage4Enemy07.txt",GetCenterX,GetClipMinY,1,90,2);
				CreateEnemyFromFile(csd~"stage4/Stage4Enemy07.txt",GetCenterX-150,GetClipMinY,1,90,2);
				wait(120);
				CreateEnemyFromFile(csd~"stage4/Stage4Enemy07.txt",GetCenterX+80,GetClipMinY,1,90,2);
				CreateEnemyFromFile(csd~"stage4/Stage4Enemy07.txt",GetCenterX-80,GetClipMinY,1,90,2);
				wait(120);
			}
}

task attack
{
let x=[GetClipMinX+64,GetClipMaxX-64,GetClipMinX+128,GetClipMaxX-128];
let y=[GetClipMaxY-64,GetClipMaxY-196,GetClipMaxY-324,GetClipMaxY-388];
let rangle=rand(0,360);
let range=196;
let way=12;
let bullet=8;
alternative(SelectedDifficult)
case("Easy")
{
	way=10;
	bullet=8;
}
case("Normal")
{
	way=12;
	bullet=8;
}
case("Hard")
{
	way=16;
	bullet=8;
}
case("Lunatic")
{
	way=20;
	bullet=8;
}
		DeleteEnemyShotInCircle(SHOT,x[0],y[0],range);
		DeleteEnemyShotInCircle(SHOT,x[1],y[0],range);
ascent(let j in 0..bullet)
{
	ascent(let i in 0..way)
	{
		fllowerattack(x[0],y[0],2-0.4*j,i*360/way+rangle);
		fllowerattack(x[1],y[0],2-0.4*j,i*360/way+rangle);
	}
}
wait(10);
let rangle=rand(0,360);
		DeleteEnemyShotInCircle(SHOT,x[0],y[1],range);
		DeleteEnemyShotInCircle(SHOT,x[1],y[1],range);
ascent(let j in 0..bullet)
{
	ascent(let i in 0..way)
	{
		fllowerattack(x[0],y[1],2-0.4*j,i*360/way+rangle);
		fllowerattack(x[1],y[1],2-0.4*j,i*360/way+rangle);
	}
}
wait(10);
let rangle=rand(0,360);
		DeleteEnemyShotInCircle(SHOT,x[0],y[2],range);
		DeleteEnemyShotInCircle(SHOT,x[1],y[2],range);
ascent(let j in 0..bullet)
{
	ascent(let i in 0..way)
	{
		fllowerattack(x[0],y[2],2-0.4*j,i*360/way+rangle);
		fllowerattack(x[1],y[2],2-0.4*j,i*360/way+rangle);
	}
}
wait(10);
let rangle=rand(0,360);
		DeleteEnemyShotInCircle(SHOT,x[0],y[3],range);
		DeleteEnemyShotInCircle(SHOT,x[1],y[3],range);
ascent(let j in 0..bullet)
{
	ascent(let i in 0..way)
	{
		fllowerattack(x[2],y[3],2-0.4*j,i*360/way+rangle);
		fllowerattack(x[3],y[3],2-0.4*j,i*360/way+rangle);
	}
}
wait(60);
DeleteAllEnemyWithoutBoss;
DeleteEnemyShotInCircle(SHOT,GetCenterX,GetCenterY,range);
}

task fllowerattack(let x ,let y ,let speed,let angle)
{
        let obj = Obj_Create(OBJ_SHOT);
        ObjShot_SetGraphic(obj, YELLOW11);
        Obj_SetAngle(obj, angle);
        Obj_SetSpeed(obj, speed);
        ObjShot_SetDelay(obj, 60);
        Obj_SetPosition(obj,x, y);
	ObjShot_SetBombResist(obj,true);
	wait(60);
	wait(30);
	speed=0;
	Obj_SetSpeed(obj, speed);
	wait(60);
	while(!Obj_BeDeleted(obj))
	{
		Obj_SetSpeed(obj, speed);
		if(speed<2)
		{
			speed+=0.02;
		}
		yield;
	}
}