if(nextphase==1 && stagephase==1){ phase+=1; frame=0; enemyx=0; enemyy=0; enemiescalled=0; nextphase=0; SetCommonData("IncreasePhase",0); }

if(phase==1 && stagephase==1){
	if(frame%10==0){
	enemiescalled++; }
	if(enemiescalled>=9){ nextphase=1; }
}

if(phase==2 && stagephase==1){
	if(enemiescalled==0){ newviewy=-15; viewmovetimexy=150; }
	if(frame%210==0){
		if(enemiescalled==0){ CreateEnemyFromFile("script\Enemies\Nightmare\806.txt",cx,miny-32,0,0,[2,2]); }
		if(enemiescalled==1){
			CreateEnemyFromFile("script\Enemies\Nightmare\806.txt",cx-120,miny-32,0,0,[2,2,0]);
			CreateEnemyFromFile("script\Enemies\Nightmare\806.txt",cx+120,miny-32,0,0,[2,2,0]);
		}
	enemiescalled++; }
	if(enemiescalled>=4){ nextphase=1; }
}

if(phase==3 && stagephase==1){
	if(frame%60==0){
		if(enemiescalled>=1 && enemiescalled<7){ CreateEnemyFromFile("script\Enemies\Nightmare\802.txt",cx+rand(-160,160),miny-32,0,0,1); }
		if(enemiescalled==8){ CreateEnemyFromFile("script\Enemies\Nightmare\802.txt",cx+rand(-50,50),miny-32,0,0,2); }
	enemiescalled++; }
	if(enemiescalled>=12){ nextphase=1; }
}
if(phase==4 && stagephase==1){
	if(frame%80==0){
		if(enemiescalled<5){ CreateEnemyFromFile("script\Enemies\Nightmare\805.txt",maxx+32,miny+rand(70,120),0,0,rand(10,20)); }
		if(enemiescalled>=5 && enemiescalled<10){ CreateEnemyFromFile("script\Enemies\Nightmare\805.txt",minx-32,miny+rand(70,120),0,0,rand(10,20)); }
		enemiescalled++; }
	if(enemiescalled>=13){ nextphase=1; }
}
if(phase==5 && stagephase==1){
	if(enemiescalled==0){ newviewy=0; viewmovetimexy=150; }
	if(frame%70==0){
		if(enemiescalled<5){ CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",cx+rand(-80,80),miny-32,0,0,[enemiescalled+2,1]); }
	enemiescalled++; }
	if(enemiescalled>=9){ nextphase=1; }
}
if(phase==6 && stagephase==1){
	if(frame%150==0){
		if(enemiescalled<4 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Nightmare\804.txt",minx-32,miny+rand(40,80),0,0,8); }
		if(enemiescalled<4 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Nightmare\804.txt",maxx+32,miny+rand(40,80),0,0,8); }
		enemiescalled++; }
	if(enemiescalled>=6){ nextphase=1; }
}
if(phase==7 && stagephase==1){
	if(frame%120==0){
		if(enemiescalled<4 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Nightmare\801.txt",cx+rand(130,160),maxy+32,0,0,1); }
		if(enemiescalled<4 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Nightmare\801.txt",cx-rand(130,160),maxy+32,0,0,1); }
		if(enemiescalled==4){ CreateEnemyFromFile("script\Enemies\Nightmare\801.txt",cx+rand(-30,30),minx-32,0,0,2); }
		enemiescalled++; }
	if(enemiescalled>=7){ nextphase=1; }
}

if(phase==8 && stagephase==1){
	if(enemiescalled==0){ newviewy=-15; viewmovetimexy=150; }
	if(frame%210==0){
		if(enemiescalled==0){
			CreateEnemyFromFile("script\Enemies\Nightmare\806.txt",cx-80,miny-32,0,0,[3,2.5,0]);
			CreateEnemyFromFile("script\Enemies\Nightmare\806.txt",cx+80,miny-32,0,0,[3,2.5,0]);
		}
		if(enemiescalled==1){ CreateEnemyFromFile("script\Enemies\Nightmare\806.txt",cx,miny-32,0,0,[3,2.5,1]); }
	enemiescalled++; }
	if(enemiescalled>=4){ nextphase=1; }
}

if(phase==9 && stagephase==1){
	if(frame%50==0){
		if(enemiescalled<10){ CreateEnemyFromFile("script\Enemies\Nightmare\802.txt",cx+rand(-160,160),miny-32,0,0,1); }
		if(enemiescalled>=11 && enemiescalled<16){ CreateEnemyFromFile("script\Enemies\Nightmare\802.txt",cx+rand(-160,160),miny-32,0,0,2); }
	enemiescalled++; }
	if(enemiescalled>=22){ nextphase=1; }
}
if(phase==10 && stagephase==1){
	if(frame%100==0){
		if(enemiescalled<10 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Nightmare\805.txt",maxx+32,miny+60+enemyy,0,0,rand(10,20)); }
		if(enemiescalled<10 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Nightmare\805.txt",minx-32,miny+60+enemyy,0,0,rand(10,20)); }
		enemiescalled++; enemyy+=20; }
	if(enemiescalled>=13){ nextphase=1; }
}
if(phase==11 && stagephase==1){
	if(enemiescalled==0){ newviewy=0; viewmovetimexy=150; }
	if(frame%120==0){
		if(enemiescalled==0){ CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",cx-rand(120,80),miny-32,0,0,[5,2]); }
		if(enemiescalled==1){ CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",cx+rand(120,80),miny-32,0,0,[5,2]); }
		if(enemiescalled==3){ CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",minx-32,miny+150,0,0,[5,3]); }
		if(enemiescalled==4){ CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",maxx+32,miny+200,0,0,[5,3]); }
		if(enemiescalled==6){
		CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",cx-rand(90,50),miny-32,0,0,[4,2]);
		CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",cx+rand(90,50),miny-32,0,0,[4,2]);
		}
	enemiescalled++; }
	if(enemiescalled>=10){ nextphase=1; }
}
if(phase==12 && stagephase==1){
	if(frame%120==0){
		if(enemiescalled<4 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Nightmare\804.txt",minx-32,miny+rand(40,80),0,0,16); }
		if(enemiescalled<4 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Nightmare\804.txt",maxx+32,miny+rand(40,80),0,0,16); }
		enemiescalled++; }
	if(enemiescalled>=7){ nextphase=1; }
}
if(phase==13 && stagephase==1){
	if(frame%90==0){
		if(enemiescalled==0){ CreateEnemyFromFile("script\Enemies\Nightmare\801.txt",cx+rand(40,80),miny-32,0,0,2); }
		if(enemiescalled==1){ CreateEnemyFromFile("script\Enemies\Nightmare\801.txt",cx-rand(40,80),miny-32,0,0,2); }
		if(enemiescalled>=3 && enemiescalled<11 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Nightmare\801.txt",cx+rand(130,160),maxy+32,0,0,1); }
		if(enemiescalled>=3 && enemiescalled<11 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Nightmare\801.txt",cx-rand(130,160),maxy+32,0,0,1); }
		enemiescalled++; }
	if(enemiescalled>=14){ nextphase=1; }
}

if(phase==14 && stagephase==1){
	if(frame%150==0){
		if(enemiescalled==0){ CreateEnemyFromFile("script\Enemies\Nightmare\806.txt",cx,miny-32,0,0,[4,3,2]); }
		if(enemiescalled==1){ CreateEnemyFromFile("script\Enemies\Nightmare\806.txt",cx-100,miny-32,0,0,[4,3,0]); }
		if(enemiescalled==2){ CreateEnemyFromFile("script\Enemies\Nightmare\806.txt",cx+100,miny-32,0,0,[4,3,0]); }
	enemiescalled++; }
	if(enemiescalled>=6){ nextphase=1; }
}

if(phase==15 && stagephase==1){
	if(frame%90==0){
		if(enemiescalled<10 && enemiescalled%2==0){
			CreateEnemyFromFile("script\Enemies\Nightmare\802.txt",cx+rand(120,160),miny-32,0,0,2);
			CreateEnemyFromFile("script\Enemies\Nightmare\802.txt",cx-rand(120,160),miny-32,0,0,1);
		}
		if(enemiescalled<10 && enemiescalled%2!=0){
			CreateEnemyFromFile("script\Enemies\Nightmare\802.txt",cx+rand(120,160),miny-32,0,0,1);
			CreateEnemyFromFile("script\Enemies\Nightmare\802.txt",cx-rand(120,160),miny-32,0,0,2);
		}
	enemiescalled++; }
	if(enemiescalled>=13){ nextphase=1; }
}
if(phase==16 && stagephase==1){
	if(frame%80==0){
		if(enemiescalled<10){
			CreateEnemyFromFile("script\Enemies\Nightmare\805.txt",maxx+32,miny+60+enemyy,0,0,rand(10,20));
			CreateEnemyFromFile("script\Enemies\Nightmare\805.txt",minx-32,miny+60+enemyy,0,0,rand(10,20));
		}
		enemiescalled++; enemyy+=20; }
	if(enemiescalled>=13){ nextphase=1; }
}
if(phase==17 && stagephase==1){
	if(frame%90==0){
		if(enemiescalled==0){ CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",cx-rand(120,80),miny-32,0,0,[5,1]); }
		if(enemiescalled==1){ CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",minx-32,miny+200,0,0,[8,4]); }
		if(enemiescalled==2){ CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",cx+rand(120,80),miny-32,0,0,[5,1]); }
		if(enemiescalled==3){ CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",maxx+32,miny+150,0,0,[8,4]); }
		if(enemiescalled==5){
			CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",cx-rand(90,50),miny-32,0,0,[4,2]);
			CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",cx+rand(90,50),miny-32,0,0,[4,2]);
		}
		if(enemiescalled==6){ CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",minx-32,miny+150,0,0,[8,3]); }
		if(enemiescalled==7){ CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",maxx+32,miny+100,0,0,[8,3]); }
		if(enemiescalled==9){
			CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",cx-rand(90,50),miny-32,0,0,[4,2]);
			CreateEnemyFromFile("script\Enemies\Nightmare\803.txt",cx+rand(90,50),miny-32,0,0,[4,2]);
		}
	enemiescalled++; }
	if(enemiescalled>=17){ nextphase=1; }
}
if(phase==18 && stagephase==1){
	if(frame%120==0){
		if(enemiescalled<6 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Nightmare\804.txt",minx-32,miny+rand(40,80),0,0,25); }
		if(enemiescalled<6 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Nightmare\804.txt",maxx+32,miny+rand(40,80),0,0,25); }
		enemiescalled++; }
	if(enemiescalled>=9){ nextphase=1; }
}
if(phase==19 && stagephase==1){
	if(frame%60==0){
		if(enemiescalled==0){
			CreateEnemyFromFile("script\Enemies\Nightmare\801.txt",cx+rand(120,160),miny-32,0,0,2);
			CreateEnemyFromFile("script\Enemies\Nightmare\801.txt",cx+rand(40,80),miny-32,0,0,2);
			CreateEnemyFromFile("script\Enemies\Nightmare\801.txt",cx-rand(40,80),miny-32,0,0,2);
			CreateEnemyFromFile("script\Enemies\Nightmare\801.txt",cx-rand(120,160),miny-32,0,0,2);
		}
		if(enemiescalled>=6 && enemiescalled<18 && enemiescalled%2==0){ CreateEnemyFromFile("script\Enemies\Nightmare\801.txt",cx+rand(130,160),maxy+32,0,0,1); }
		if(enemiescalled>=6 && enemiescalled<18 && enemiescalled%2!=0){ CreateEnemyFromFile("script\Enemies\Nightmare\801.txt",cx-rand(130,160),maxy+32,0,0,1); }
		enemiescalled++; }
	if(enemiescalled>=24){ nextphase=1; }
}

if(phase==20 && stagephase==1){
	if(frame%90==0){
		if(enemiescalled==0){ CreateEnemyFromFile("script\Enemies\Nightmare\806.txt",cx,miny-32,0,0,[5,4,1]); }
		if(enemiescalled==1){
			CreateEnemyFromFile("script\Enemies\Nightmare\806.txt",cx+150,miny-32,0,0,[5,4,1]);
			CreateEnemyFromFile("script\Enemies\Nightmare\806.txt",cx-150,miny-32,0,0,[5,4,1]);
		}
	enemiescalled++; }
	if(enemiescalled>=8){ nextphase=1; }
}

if(phase==21 && stagephase==1){
	if(frame%60==0){
		if(enemiescalled==0){ CreateEnemyFromFile("script\Enemies\Nightmare\810.txt",cx,miny-32,0,0,0); }
	enemiescalled++; }
	if(GetCommonData("IncreasePhase")==1){ nextphase=1; }
}
if(phase==22 && stagephase==1){
	if(frame%60==0){ enemiescalled++; }
	if(enemiescalled>=1){ SetCommonData("PlayMusic",2); nextphase=1; }
}