

let SSS=0;

let Stage3BGCount=0;
let Stage3BGCount2=0;

let PillarPos=0;
let PillarInterval=600;


let MountainY=[];
let MountainY2=[];

let St3MountainAngle=[];
let St3MountainHeight=[];
let St3MountainX=[];
let St3MountainY=[];


let St3MountainAngle2=[];
let St3MountainHeight2=[];
let St3MountainX2=[];
let St3MountainY2=[];

let St3XDir=1;

let St3DrawCenterX=0;
let St3DrawCenterXWide=0;
let St3DrawCenterXAngle=0;

let St3CountX=0;

let MtHeightMin=10;
let MtHeightMid=50;
let MtHeightMax=80;
let MtXMin=140;

////////////////////////////////////////////////////

let St5MagicScroll=0;
let MagicDustAngle=[];
let MagicDustX=[];
let MagicDustY=[];
let MagicDustZ=[];
let MagicDustSize=[];

function Stage5SceneChange
{
	Light;
	WhiteLight;
	wait(240);
	Stage5Phase=1;
	wait(300);
	task AlphaChange
	{
		Light;
	}

	task Light
	{
		let obj = Obj_Create(OBJ_EFFECT);
		Obj_SetPosition(obj, GetCenterX,GetCenterY);
		Obj_SetSpeed(obj, 0);
		Obj_SetAngle(obj, 0);
		ObjEffect_SetTexture(obj,csd~"img\circlelight.png");
		ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(obj,4);

		ObjEffect_SetVertexXY(obj,0,-32,-32);
		ObjEffect_SetVertexXY(obj,1,32,-32);
		ObjEffect_SetVertexXY(obj,2,32,32);
		ObjEffect_SetVertexXY(obj,3,-32,32);
		ObjEffect_SetVertexUV(obj,0,0,0);
		ObjEffect_SetVertexUV(obj,1,64,0);
 		ObjEffect_SetVertexUV(obj,2,64,64);
		ObjEffect_SetVertexUV(obj,3,0,64);

		ObjEffect_SetScale(obj,2,2);
		ObjEffect_SetAngle(obj,0,0,0);
		ObjEffect_SetLayer(obj,7);
	//	ObjEffect_SetRenderState(obj,ADD);

		let alpha=0;

		loop(180)
		{
			ascent(i in 0..4)
			{
			ObjEffect_SetVertexColor(obj,i,alpha,255,255,255);
			}
			alpha+=255/180;
			yield;
		}
		wait(120);
		Obj_Delete(obj);
	}

	task WhiteLight
	{
		wait(50);
		White;
	}

	task White
	{
		let obj = Obj_Create(OBJ_EFFECT);
		Obj_SetPosition(obj, GetCenterX,GetCenterY);
		Obj_SetSpeed(obj, 0);
		Obj_SetAngle(obj, 0);
		ObjEffect_SetTexture(obj,csd~"img\White.png");
		ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(obj,4);

		ObjEffect_SetVertexXY(obj,0,-300,-300);
		ObjEffect_SetVertexXY(obj,1,-300,300);
		ObjEffect_SetVertexXY(obj,2,300,300);
		ObjEffect_SetVertexXY(obj,3,300,-300);
		ObjEffect_SetVertexUV(obj,0,-300,-300);
		ObjEffect_SetVertexUV(obj,1,-300,300);
 		ObjEffect_SetVertexUV(obj,2,300,300);
		ObjEffect_SetVertexUV(obj,3,300,-300);

		ObjEffect_SetScale(obj,1,1);
		ObjEffect_SetAngle(obj,0,0,0);
		ObjEffect_SetLayer(obj,7);
		ObjEffect_SetRenderState(obj,ADD);

		let alpha=0;

		loop(180)
		{
			ascent(i in 0..4)
			{
			ObjEffect_SetVertexColor(obj,i,alpha,255,255,255);
			}
			alpha+=255/180;
			yield;
		}
		ascent(i in 0..4)
		{
			ObjEffect_SetVertexColor(obj,i,alpha,255,255,255);
		}
		yield;
		ObjEffect_SetRenderState(obj,ALPHA);
		wait(60);
		ObjEffect_SetRenderState(obj,ADD);

		loop(300)
		{
			ascent(i in 0..4)
			{
			ObjEffect_SetVertexColor(obj,i,alpha,255,255,255);
			}
			alpha-=255/300;
			yield;
		}
		Obj_Delete(obj);
	}
}


	let CloudSep=10;

let  Stage5Phase=0;


let BGAngle=0;
let CircleAngle=0;


let TransPortZ=1000-1200*5-5460;

let MagicCircleColor=[255,255,255];
let MagicCircleScale=3;

let PillarRadius=[600,630,615,1200,1230,1800,1830];

let PillarHeight=[640,640,640];
let PillarShakeX=[0,0,0];
let PillarShakeY=[0,0,0];

sub BackGroundLoop
{
	if(StageState!=Menu && BGDrawing==1)
	{
		SSS+=1;
		if(StageProgress==1)
		{

			SetTexture(csd~"img\sky.png");
			SetAlpha(stage1alpha4);
			SetGraphicScale(2,-1.5);
			SetColor(255,255,255);
			SetGraphicRect(0, 0 , 1536, 480);
			SetGraphicAngle(75+DrawAngleX, 0+DrawAngleY, 0);
			DrawGraphic3D(0+DrawX+ScreenShakeX-SkyRect, 0+DrawY+ScreenShakeY, 106+DrawZ);

			SetTexture(csd~"img\mountains.png");
			SetAlpha(stage1alpha5);
			SetRenderState(ALPHA);
   			SetGraphicScale(2,-1.5);
    			SetColor(255,255,255);
   			SetGraphicRect(0, 0 , 256, 128);
			SetGraphicAngle(75+DrawAngleX, 0+DrawAngleY, 0);
			DrawGraphic3D(0+DrawX+ScreenShakeX, 0+DrawY+ScreenShakeY,210+DrawZ-5);

			SetTexture(csd~"img\mountains.png");
			SetAlpha(stage1alpha5);
			SetRenderState(ALPHA);
   			SetGraphicScale(2,1.5-DrawY/100-0.015);
    			SetColor(200,255,255);
   			SetGraphicRect(0, 0 , 256, 128);
			SetGraphicAngle(75+DrawAngleX, 0+DrawAngleY, 0);
			let sep=10;
			ascent(i in 0..sep)
			{
				SetGraphicRect(0,128-128/sep-128/sep*i,256,128-128/sep*i);
				DrawGraphic3D(0+15*cos(i*32/sep+bgX4*360/640+DrawX)+ScreenShakeX, 0+ScreenShakeY, 107-128/sep*i*1.4+12.8-12.8+DrawZ-5);
			}

			WriteZBuffer(false);
      			UseZBuffer(false);

			SetTexture(csd~"img\st1bg1.png");
			SetAlpha(stage1alpha1*1.1);
			SetColor(150,200,255);
			SetRenderState(ALPHA);
			SetGraphicScale(1,1);
			let WaterLeng=60;
			if(WaterWaving)
			{
				let X=0;
				let Z=-2*sin(WaterWave)-10;
				ascent(i in 0..85)
				{
				let Wangle=2*cos(i*18-WaterWave);
				let Wangle2=2*cos(i*18+18-WaterWave);
				let angle=2*cos(i*18);
				let angle2=2*cos(i*18+18);
				SetGraphicAngle(0+DrawAngleX, -Wangle+DrawAngleY, 0);
				SetGraphicRect(i*WaterLeng+bgX, 0+bgY  ,WaterLeng*(i+1)+bgX, 3000 + bgY);
				DrawGraphic3D(0+ScreenShakeX+X-500+DrawX, 0+ScreenShakeY, Z+DrawZ);
				X+=WaterLeng*0.5*(absolute(cos(Wangle))+absolute(cos(Wangle2)));
				Z+=WaterLeng*0.5*(sin(Wangle)+sin(Wangle2));
				}
			}
			else
			{
				SetGraphicRect(0+bgX, 0+bgY  ,1000+bgX, 1500 + bgY);
				SetGraphicAngle(0+DrawAngleX, 0+DrawAngleY, 0);
				DrawGraphic3D(0+ScreenShakeX+DrawX, 0+ScreenShakeY+DrawY, -0+DrawZ);
			}


			SetFog(200,3500,180,180,225);

			SetTexture(csd~"img\st1bg1.png");
			SetGraphicScale(1,1);
			SetAlpha(stage1alpha2);
			SetColor(150,200,255);
			SetRenderState(ALPHA);

			if(WaterWaving)
			{
				let X=0;
				let Z=2*sin(WaterWave);
				ascent(i in 0..85)
				{
				let Wangle=2*cos(i*18+WaterWave);
				let Wangle2=2*cos(i*18+18+WaterWave);
				let angle=2*cos(i*18);
				let angle2=2*cos(i*18+18);
				SetGraphicAngle(0+DrawAngleX, -Wangle+DrawAngleY, 0);
				SetGraphicRect(i*WaterLeng-bgX2, 0+bgY2  ,WaterLeng*(i+1)-bgX2, 3000 + bgY2);
				DrawGraphic3D(0+ScreenShakeX+X-500+DrawX, 0+ScreenShakeY, Z+DrawZ);
				X+=WaterLeng*0.5*(absolute(cos(Wangle))+absolute(cos(Wangle2)));
				Z+=WaterLeng*0.5*(sin(Wangle)+sin(Wangle2));
				}
				}
			else
			{
				SetGraphicRect(0-bgX2, 0+bgY2  ,1000-bgX2, 1500 + bgY2);
				SetGraphicAngle(0+DrawAngleX, 0+DrawAngleY, 0);
				DrawGraphic3D(0+ScreenShakeX+DrawX,0+ScreenShakeY+DrawY, 0+DrawZ);
			}

			WriteZBuffer(true);
      			UseZBuffer(true);

				SetPerspectiveClip(10,1400);
				SetViewTo(ViewToX,ViewToY,ViewToZ);
				SetViewFrom(600+ViewFromX,ViewFromY,ViewFromZ);
			//	SetFog(200,500,200,200,250);
				if(stage1alpha3>0)
				{
					SetTexture(csd~"img\cloud.png");
					SetGraphicRect(0+bgX3, 0+bgY3  ,2400+bgX3, 3000 + bgY3);
					SetGraphicAngle(0+DrawAngleX, 0+DrawAngleY, 0);
					SetGraphicScale(4,4);
					SetAlpha(stage1alpha3*2.5);
				//SetAlpha(255);
					SetRenderState(ALPHA);
					SetColor(255,255,200);
					DrawGraphic3D(0+DrawX,0+DrawY, 5+DrawZ);

					SetTexture(csd~"img\cloud.png");
					SetGraphicRect(0+bgX3, 0+bgY3  ,2400+bgX3, 3000 + bgY3);
					SetGraphicAngle(-15+DrawAngleX, 0+DrawAngleY, 0);
					SetGraphicScale(4,4);
					SetAlpha(stage1alpha3*1.5);
					SetRenderState(ALPHA);
					SetColor(255,255,200);
					DrawGraphic3D(0+DrawX,118+DrawY, 150+DrawZ);
				}
				if(ViewFromX>0)
				{
					SetTexture(csd~"img\cloud.png");
					SetGraphicRect(0+bgX/1.2, 0+bgY/1.2 ,2400+bgX/1.2, 3000 + bgY/1.2);
					SetGraphicAngle(-75+DrawAngleX, 0+DrawAngleY, 0);
					SetGraphicScale(3,3);
					SetAlpha(200*ViewFromX/300);
					if(stage1alpha1<80)
					{
						SetAlpha(stage1alpha3*2.5);
					}
					SetColor(150,150,255);
					SetRenderState(ALPHA);
					DrawGraphic3D(0, 180, 10);
				}

			}
/////////////////////////////////////////////////////////////////////////////////////////////////////
			if(StageProgress==2)
			{
				SetFog(200,3500,255/4,180/4,180/4);

			WriteZBuffer(true);
      			UseZBuffer(true);
			SetViewTo(0,0,0);
			SetViewFrom(500,90,20);

			SetAlpha(stage2alpha1);
			SetRenderState(ALPHA);
		
			SetGraphicAngle(90,0,0);
			SetGraphicScale(2,2);
			SetTexture(csd~"img\st2bg2.png");
			SetGraphicRect(0,0-bgY2,1000,2000-bgY2);
			DrawGraphic3D(0+ScreenShakeX,-275+ScreenShakeY,0);

			SetTexture(csd~"img\MountainChip.png");
			SetAlpha(stage2alpha1);
			SetGraphicRect(0,1,256,255);
			SetGraphicScale(1,1.5);
			SetColor(255,255,255);
			SetRenderState(ALPHA);

			if(length(MountainAngle)!=0)
			{
			descent(i in 0..8)
			{
				let ZPos=(i*150)-Stage2BGCount%150+MountainY[i];
				SetColor(255-(ZPos/5),255-(ZPos/5),255-(ZPos/6));
				DrawMountain(MountainAngle[i],MountainX[i]+ScreenShakeX,MountainHeight[i]+0,ZPos+ScreenShakeY);

				let ZPos2=(i*150)-Stage2BGCount%150+75+MountainY2[i];
				SetColor(255-(ZPos2/5),255-(ZPos2/5),255-(ZPos2/6));
				DrawMountain(MountainAngle2[i],MountainX2[i]+ScreenShakeX,MountainHeight2[i]+0,ZPos2+75+ScreenShakeY);
			}
			}


			SetColor(255,255,255);
			SetGraphicAngle(160,0,0);
			SetGraphicScale(3.7,3.7);
			SetGraphicRect(0,0,256,256);
			SetAlpha(stage2alpha1);
			SetTexture(csd~"img\0024.png");
			DrawGraphic3D(0+ScreenShakeX,-200+ScreenShakeY,0-800+0);

			SetColor(255,255,255);
			SetGraphicAngle(160,0,0);
			SetGraphicScale( MoonScale, MoonScale);
			SetColor(MoonColor[0],MoonColor[1],MoonColor[2]);
			SetTexture(csd~"img\moon.png");
			SetGraphicRect(0,0,256,256);

			DrawGraphic3D(MoonX+ScreenShakeX,MoonY+ScreenShakeY,0-790+0);

			SetTexture(csd~"img\st4bg1.png");

			SetAlpha(stage2alpha1/6);
			SetGraphicScale(1.5*3,1.5*3);

		ascent(i in -3..4)
		{
			SetGraphicAngle(90,0,0);
			SetGraphicRect(0,20-(Stage2BGCount*2),1000-300,2020-(Stage2BGCount*2));
			DrawGraphic3D(0+ScreenShakeX,-260+10*i+ScreenShakeY,0);
		}
		
			SetGraphicScale(1,15);SetAlpha(255);
		
			function DrawMountain(angle,let xpos,let ypos,let zpos)
			{
				SetGraphicScale(2*sin(angle)*1.3,2);
				let TexNum=3;
				ascent(i in 0..TexNum)
				{
					SetGraphicAngle(angle,-90+90/TexNum+180/TexNum*i,0);
					DrawGraphic3D(xpos,-ypos,220-zpos);
					SetGraphicAngle(angle,-90+90/TexNum+180/TexNum*i,0);
					DrawGraphic3D(xpos*2,-ypos+50,220-zpos);
				}
			}

			}

			if(StageProgress==3)
			{

				if(stage3alpha4>0)
				{
				SetTexture(csd~"img\cloud2.png");
				SetGraphicRect(0+bgX2, 0+bgY2  ,2400+bgX2, 3000 + bgY2);
				SetGraphicAngle(90-15, 0, 45);
				SetGraphicScale(2,2);
				SetAlpha(stage3alpha4);
				SetColor(200,200,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0+ScreenShakeX, 200+ScreenShakeY, -300);

				SetTexture(csd~"img\cloud2.png");
				SetGraphicRect(0+bgX2, 0+bgY2  ,2400+bgX2, 3000 + bgY2);
				SetGraphicAngle(90-15, 0, -45);
				SetGraphicScale(2,2);
				SetAlpha(stage3alpha4);
				SetColor(200,200,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0+ScreenShakeX, 200+ScreenShakeY,-300);
				}

			SetViewTo(0,0,0);
			SetViewFrom(700+ViewFromX,90,45+ViewFromZ-2);
			

			SetAlpha(255);
			SetRenderState(ALPHA);
			
	

			SetColor(255, 255, 255);


			WriteZBuffer(true);
      			UseZBuffer(true);

			SetGraphicAngle(90,0,0);
			SetGraphicScale(1,1);
			SetTexture(csd~"img\st3bg1.png");
			SetAlpha(stage3alpha1);
			SetColor(255,240,240);
			SetGraphicRect(300+bgX*0,0+bgY,1600+bgX*0,2000+bgY);
			DrawGraphic3D(0+ScreenShakeX,0+ScreenShakeY,-210);

			WriteZBuffer(false);
      			UseZBuffer(false);

					SetTexture(csd~"img\st4bg1.png");
					SetGraphicRect(0+bgX/1.2, 0+bgY/1.2 ,2400+bgX/1.2, 3000 + bgY/1.2);
					SetGraphicAngle(90, 0, 0);
					SetGraphicScale(5,5);
					SetAlpha(stage3alpha1/4);
					SetColor(255,150,150);
					SetRenderState(ALPHA);
					DrawGraphic3D(0, 0, -215);

		//	SetFog(200,500,180,180,225);

			WriteZBuffer(true);
      			UseZBuffer(true);

			SetColor(255,255,255);
			if(length(St3MountainAngle)==20)
			{
			SetTexture(csd~"img\MountainChip2.png");
			SetGraphicRect(1,11,255,255);
			SetAlpha(stage3alpha1);
			descent(i in 0..20)
			{
					let ZPos=-(i*75)+Stage3BGCount%(5*75)+St3MountainY[i]+450;
					let ZPos2=-(i*75)+Stage3BGCount%(5*75)+St3MountainY[i]+450;
					DrawMountain(St3MountainAngle[i],St3MountainX[i]+ScreenShakeX,St3MountainHeight[i]+0,ZPos+ScreenShakeY);
					DrawMountain(St3MountainAngle2[i],St3MountainX2[i]+ScreenShakeX,St3MountainHeight2[i]+0,ZPos2+ScreenShakeY);
			}
			}

			function DrawMountain(angle,let xpos,let ypos,let zpos)
			{

				ypos+=80;
				SetGraphicScale(1.08*sin(angle)*2,1*2);
				SetGraphicAngle(angle,-60,0);
				DrawGraphic3D(xpos,ypos,zpos);
			
				SetGraphicAngle(angle,0,0);
				DrawGraphic3D(xpos,ypos,zpos);
		
				SetGraphicAngle(angle,60,0);
				DrawGraphic3D(xpos,ypos,zpos);

				SetGraphicAngle(270-(angle-90),60,0);
				DrawGraphic3D(xpos,ypos,zpos);
		
				SetGraphicAngle(270-(angle-90),0,0);
				DrawGraphic3D(xpos,ypos,zpos);

				SetGraphicAngle(270-(angle-90),-60,0);
				DrawGraphic3D(xpos,ypos,zpos);

			}

			WriteZBuffer(false);
      			UseZBuffer(false);

		//	if(stage3alpha2>0)
		//	{
			SetAlpha(stage3alpha2/1.1);
			SetGraphicAngle(90,0,0);
			SetGraphicScale(4,3);
			SetTexture(csd~"img\st3cloudshadow.png");
			SetGraphicRect(0-bgX2/4,0-bgY2/3,1200-bgX2/4,1500-bgY2/3);
			DrawGraphic3D(0+ScreenShakeX,100+ScreenShakeY,0);

			SetGraphicAngle(90,0,0);
			SetGraphicScale(4,3);
			SetAlpha(stage3alpha2-70*(300-ViewFromX)/300);
			SetRenderState(ALPHA);
			SetTexture(csd~"img\st3cloud.png");
			SetGraphicRect(0-bgX2/4,0-bgY2/3,1200-bgX2/4,1500-bgY2/3);
			DrawGraphic3D(0+ScreenShakeX,500+ScreenShakeY,100);
		//	}

			SetTexture(csd~"img\Black.png");
			SetAlpha(stage3alpha3);
			SetGraphicAngle(-30,0,0);
			SetGraphicRect(0,0,50,50);
			SetGraphicScale(24,35);
			SetColor(255,255,255);
			DrawGraphic3D(0,0,0);

			}
			if(StageProgress==4)
			{

				SetFog(0,25000,stage4fogRGB[0],stage4fogRGB[1],stage4fogRGB[2]);

				SetTexture(csd~"img\st4bg2.png");
				SetGraphicRect(0, 0  ,256, 256);
				SetGraphicAngle(90, 0, 0-stage4bg2zangle);
				SetGraphicScale(stage4bg2scale,stage4bg2scale);
				SetAlpha(stage4alpha2);
				SetColor(255,255,255);
				SetRenderState(ALPHA);
	//			DrawGraphic3D(0+ScreenShakeX, 800+ScreenShakeY, 0);

				SetViewTo(0,-60+stage4bgfromz,50);
				SetViewFrom(500-stage4bgfromz*2.5,90-stage4bgfromy,-90);

				SetTexture(csd~"img\st4bg1.png");
				SetGraphicRect(0+bgX, 0+bgY  ,2400+bgX, 3000 + bgY);
				SetGraphicAngle(-0+stage4bgfromz, -65,0);
				SetGraphicScale(3,3);
				SetAlpha(stage4alpha3/3);
				SetColor(255,255,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(30+ScreenShakeX, 0+ScreenShakeY, 0);

				SetTexture(csd~"img\st4bg1.png");
				SetGraphicRect(0-bgX, 0+bgY  ,2400-bgX, 3000 + bgY);
				SetGraphicAngle(-0+stage4bgfromz, -65,0);
				SetGraphicScale(3,3);
				SetAlpha(stage4alpha3/3);
				SetColor(255,255,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(35+ScreenShakeX, 0+ScreenShakeY, 0);

				SetTexture(csd~"img\st4bg1.png");
				SetGraphicRect(0+bgX2, 0+bgY2  ,2400+bgX2, 3000 + bgY2);
				SetGraphicAngle(180-stage4bgfromz, 0+180-65, 0);
				SetGraphicScale(3,3);
				SetAlpha(stage4alpha3/3);
				SetColor(255,255,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(-50-stage4bgLeftLayerX+ScreenShakeX, 0+ScreenShakeY, 0);

				SetTexture(csd~"img\st4bg1.png");
				SetGraphicRect(0-bgX2, 0+bgY2  ,2400-bgX2, 3000 + bgY2);
				SetGraphicAngle(180-stage4bgfromz, 0+180-65, 0);
				SetGraphicScale(3,3);
				SetAlpha(stage4alpha3/3);
				SetColor(255,255,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(-55-stage4bgLeftLayerX+ScreenShakeX, 0+ScreenShakeY, 0);

				if(stage4alpha1>0)
				{
					SetTexture(csd~"img\st4bg1.png");
					SetGraphicRect(0+bgX, 0+bgY  ,2400+bgX, 3000 + bgY);
					SetGraphicAngle(-90, 0-65,0 );
					SetGraphicScale(3,3);
					SetAlpha(stage4alpha1);
					SetColor(255,255,255);
					SetRenderState(ALPHA);
					DrawGraphic3D(30, 0, 0);

					SetTexture(csd~"img\st4bg1.png");
					SetGraphicRect(0+bgX2, 0+bgY2  ,2400+bgX2, 3000 + bgY2);
					SetGraphicAngle(90, 0+180-65, 0);
					SetGraphicScale(3,3);
					SetAlpha(stage4alpha1);
					SetColor(255,255,255);
					SetRenderState(ALPHA);
					DrawGraphic3D(-50, 0, 0);
				}
			}

		if(StageProgress==5 && Stage5Phase==0)
		{

			SetViewTo(ViewToX+ScreenShakeX,50,ViewToZ+ScreenShakeY);
			SetViewFrom(1+ViewFromZ/4,90,-90+ViewFromZ);
			WriteZBuffer(true);
			UseZBuffer(true);

			SetRenderState(ALPHA);

			SetTexture(csd~"img\st5bg1.png");
			SetColor(150,150,150);
			SetGraphicScale((105/40)*0.929/CloudSep,0.2);
			SetAlpha(stage5alpha1);


			SetFog(10,300,0,0,0);
			ascent(i in 0..CloudSep)
			{
				ascent(k in 0..4)
				{

					SetTexture(csd~"img\st5bg1.png");
					SetGraphicRect(0+i*40+bgX,2000+bgY+400*k,40+i*40+bgX,2400+bgY+400*k);
					SetGraphicAngle(0,i*(360/CloudSep),0);
					DrawGraphic3D(sin(i*(360/CloudSep))*15,-100+160+80*k,cos(i*(360/CloudSep))*15);
					SetTexture(csd~"img\st5bg1.png");
					SetGraphicRect(10+i*40-bgX,2080+400*k+bgY,50+i*40-bgX,2480+400*k+bgY);
					SetGraphicAngle(0,i*(360/CloudSep),0);
					DrawGraphic3D(sin(i*(360/CloudSep))*15,-100+160+80*k,cos(i*(360/CloudSep))*15);
				}
			}	


			SetTexture(csd~"img\MagicDust.png");

			if(length(MagicDustX)>0)
			{
				ascent(k in 0..40)
				{
				DrawMagicDust(k,MagicDustX[k],MagicDustZ[k],MagicDustAngle[k],MagicDustY[k],MagicDustSize[k]);
				}
			}	

			function DrawMagicDust(num,x,z,angle,y,size){

				SetAlpha(stage5alpha2);
				SetGraphicScale(0.1*size,0.1*size);
				SetGraphicAngle(-60,angle,0);
				SetGraphicRect(0,0,40,40);
				DrawGraphic3D(x,0+num*5-((St5MagicScroll)%5)+y,z);
			}

			SetRenderState(ALPHA);

		}

		if(StageProgress==5 && Stage5Phase==1)
		{
			SetViewTo(ViewToX+0+ScreenShakeX,ViewToY+150+ScreenShakeY,400);
			SetViewFrom(1000,90+ViewFromY,5);
			SetPerspectiveClip(10,4500*2);

			WriteZBuffer(true);
			UseZBuffer(true);

			SetTexture(csd~"img\s0145.png");
			SetRenderState(ALPHA);
			SetGraphicScale(3,3);
			SetGraphicAngle(90,0,0);
			SetAlpha(stage5alpha1);
			SetColor(255,255,255);
			SetGraphicRect(0-bgX,0-bgY,6000-bgX,6000-bgY);
			DrawGraphic3D(0,0,0);

			SetGraphicRect(0+bgX,0-bgY,6000+bgX,6000-bgY);
			DrawGraphic3D(0,0,0);

			if(stage5alpha5>0)
			{
			SetGraphicScale(5,5);
			SetAlpha(stage5alpha5);
			SetColor(255,255,255);
			SetGraphicAngle(0,0,0);
			SetGraphicRect(0-bgX,0-bgY2,8000-bgX,8000-bgY2);
			DrawGraphic3D(0,-20000,200);
			SetGraphicRect(0+bgX,0-bgY2,8000+bgX,8000-bgY2);
			DrawGraphic3D(0,-20000,200);
			}

			SetTexture(csd~"img\circle.png");
			SetRenderState(ALPHA);
			SetGraphicScale(3,3);
			SetGraphicAngle(90,0,SSS);
			SetAlpha(stage5alpha4);
			SetColor(255,255,255);
			SetGraphicRect(256,256,512,512);
			DrawGraphic3D(-1800,1,TransPortZ);

			SetGraphicAngle(90,0,-SSS);
			DrawGraphic3D(-1800,1,TransPortZ);

			WriteZBuffer(false);
			UseZBuffer(false);

			SetTexture(csd~"img\Sphere.png");
			SetRenderState(ALPHA);
			SetGraphicRect(0,1,1500,170);
			SetGraphicScale(1,6);
			SetGraphicRect(0,0,256,256);
			SetGraphicScale(5,5);
			SetGraphicAngle(0,0,0);
			SetColor(50,20,50);
			SetAlpha(stage5alpha2);
			DrawGraphic3D(ViewToX,800,-2600);

			WriteZBuffer(true);
			UseZBuffer(true);

			SetTexture(csd~"img\Pillar1.png");
			SetGraphicScale(1,1);


			SetColor(255,255,255);
			SetAlpha(255);

			if(PillarPos>PillarInterval)
			{
				PillarPos-=PillarInterval;
			}
			ascent(j in -8..2)
			{
			ascent(i in -7..3)
			{
			SetGraphicRect(0,0,128,768);
			SetGraphicScale(0.25,0.625);
			SetAlpha(stage5alpha3*(160+j*20+20*PillarPos/PillarInterval));
			if(PillarPos+j*PillarInterval>1000){SetAlpha(150);}
			SetGraphicAngle(0,0,0);
			DrawGraphic3D(i*PillarInterval+PillarInterval/2,240,PillarPos+j*PillarInterval);
			DrawGraphic3D(i*PillarInterval+PillarInterval/2,240,PillarPos+j*PillarInterval+32);

			SetGraphicAngle(0,90,0);
			DrawGraphic3D(i*PillarInterval+PillarInterval/2-16,240,PillarPos+j*PillarInterval+16);
			DrawGraphic3D(i*PillarInterval+PillarInterval/2+16,240,PillarPos+j*PillarInterval+16);

			}
			}
		}

		if(StageProgress==5 && Stage5Phase==2)
		{
			SetViewTo(ViewToX+0+ScreenShakeX,ViewToY+150+ScreenShakeY,400);
			SetViewFrom(1000,90+ViewFromY,ViewFromZ);
			SetPerspectiveClip(10,4500*2);

			WriteZBuffer(true);
			UseZBuffer(true);

			SetTexture(csd~"img\st4bg1.png");
			SetRenderState(ALPHA);
			SetGraphicScale(5,5);
			SetGraphicAngle(90,0,0);
			SetAlpha(stage5alpha1);
			SetColor(255,255,255);
			SetGraphicRect(0-bgX,0-bgY,6000-bgX,6000-bgY);
			DrawGraphic3D(0,0,0);

			SetGraphicRect(0+bgX,0-bgY,6000+bgX,6000-bgY);
			DrawGraphic3D(0,5,0);

			SetTexture(csd~"img\Pillar2.png");
			SetGraphicScale(1,1);


			SetColor(255,255,255);
			SetAlpha(255);

			if(PillarPos>PillarInterval)
			{
				PillarPos-=PillarInterval;
			}
			ascent(j in -8..2)
			{
			ascent(i in -1..1)
			{
			SetGraphicRect(0,0,256,1280);
			SetGraphicScale(0.25,0.75);
			SetAlpha(stage5alpha2*(160+j*20+20*PillarPos/PillarInterval));
			SetGraphicAngle(0,0,0);
			DrawGraphic3D(i*PillarInterval+PillarInterval/2,480,PillarPos+j*PillarInterval);
			DrawGraphic3D(i*PillarInterval+PillarInterval/2,480,PillarPos+j*PillarInterval+64);

			SetGraphicAngle(0,90,0);
			DrawGraphic3D(i*PillarInterval+PillarInterval/2-32,480,PillarPos+j*PillarInterval+32);
			DrawGraphic3D(i*PillarInterval+PillarInterval/2+32,480,PillarPos+j*PillarInterval+32);

			}
			}
			WriteZBuffer(false);
			UseZBuffer(false);
			SetTexture(csd~"img\st4bg1.png");
			SetRenderState(ALPHA);
			SetGraphicScale(5,5);
			SetGraphicAngle(0,0,0);
			SetAlpha(stage5alphacloud);
			SetColor(255,255,255);
			SetGraphicRect(0-bgX,0-bgY,6000-bgX,6000-bgY);
			DrawGraphic3D(0,0,500);

			SetGraphicRect(0+bgX,0-bgY,6000+bgX,6000-bgY);
			DrawGraphic3D(0,5,500);
		}

		if(StageProgress==5 && Stage5Phase==3)
		{
			SetViewTo(0+ScreenShakeX,ViewToY+150+ScreenShakeY,400);
			SetViewFrom(ViewFromX,90+ViewFromY/4,ViewFromZ);
			SetPerspectiveClip(10,2500+ViewFromX);

			WriteZBuffer(true);
			UseZBuffer(true);

			SetTexture(csd~"img\st4bg1.png");
			SetRenderState(ALPHA);
			SetGraphicScale(5,5);
			SetGraphicAngle(90,0,0);
			SetAlpha(stage5alpha1);
			SetColor(255,255,255);
			SetGraphicRect(0-bgX,0-bgY,6000-bgX,800-bgY);
			DrawGraphic3D(0,0,0);

			SetGraphicRect(0+bgX,0-bgY,6000+bgX,800-bgY);
			DrawGraphic3D(0,1,0);

			if(stage5alpha2>0)
			{
			SetTexture(csd~"img\circle.png");
			SetRenderState(ALPHA);
			SetGraphicScale(MagicCircleScale,MagicCircleScale);
			SetGraphicAngle(90,0,CircleAngle+BGAngle);
			SetAlpha(stage5alpha2);
			SetColor(MagicCircleColor[0],MagicCircleColor[1],MagicCircleColor[2]);
			SetGraphicRect(256,256,512,512);
			DrawGraphic3D(0,2,0);

			SetGraphicAngle(90,0,-CircleAngle+BGAngle);
			DrawGraphic3D(0,2,0);
			}

			SetTexture(imgWhite);

			SetGraphicRect(0,1,1500,170);
			SetGraphicScale(2,10);
			SetGraphicAngle(0,0,0);
			SetColor(50,20,50);
			SetAlpha(stage5alpha4);
			DrawGraphic3D(0,600,-2000+100);

			SetTexture(csd~"img\st4bg1.png");
			SetGraphicAngle(0,0,0);
			SetAlpha(stage5alpha4);

			SetColor(255,255,255);
			SetAlpha(stage5alpha5);
			SetGraphicScale(3,3);
			SetGraphicRect(0+bgX2*4,0,3000+bgX2*4,2048);
			DrawGraphic3D(0,400,-2000+100);
			SetGraphicScale(3,3);
			SetGraphicRect(0+bgX2,sin(bgY2/4)*80,1500+bgX2,1024+sin(bgY2/4)*80);
			DrawGraphic3D(0,400,-2000+100);

			ascent(i in 0..6)
			{		
				SetTexture(imgWhite);
				SetColor(MagicCircleColor[0],MagicCircleColor[1],MagicCircleColor[2]);
				SetGraphicRect(0,0,240,2560);
				SetGraphicScale(0.125,0.5);
				if(PillarHeight[2]>-640 && sin(BGAngle+i*60)<0.9)
				{
					SetAlpha(stage5alpha6);
					SetGraphicAngle(0,-BGAngle+90-i*60,0);
					DrawGraphic3D(PillarShakeX[0]+PillarRadius[0]*cos(BGAngle+i*60),PillarHeight[0],PillarShakeY[0]+PillarRadius[0]*sin(BGAngle+i*60));
					DrawGraphic3D(PillarShakeX[0]+PillarRadius[1]*cos(BGAngle+i*60),PillarHeight[0],PillarShakeY[0]+PillarRadius[1]*sin(BGAngle+i*60));

					SetGraphicAngle(0,90-BGAngle+90-i*60,0);
					DrawGraphic3D(PillarShakeX[0]+PillarRadius[2]*cos(BGAngle+i*60)+15*cos(BGAngle+i*60+90),PillarHeight[0],PillarShakeY[0]+PillarRadius[2]*sin(BGAngle+i*60)+15*sin(BGAngle+i*60+90));
					DrawGraphic3D(PillarShakeX[0]+PillarRadius[2]*cos(BGAngle+i*60)-15*cos(BGAngle+i*60+90),PillarHeight[0],PillarShakeY[0]+PillarRadius[2]*sin(BGAngle+i*60)-15*sin(BGAngle+i*60+90));
				}

				SetAlpha(stage5alpha6*0.75/2);
				SetGraphicAngle(0,-BGAngle+90-i*60-30,0);
				if(PillarHeight[2]>-640 && sin(BGAngle+i*60+30)<-0.5)
				{
				DrawGraphic3D(PillarShakeX[1]+PillarRadius[3]*cos(BGAngle+i*60+30),PillarHeight[1],PillarShakeY[1]+PillarRadius[3]*sin(BGAngle+i*60+30));
				DrawGraphic3D(PillarShakeX[1]+PillarRadius[4]*cos(BGAngle+i*60+30),PillarHeight[1],PillarShakeY[1]+PillarRadius[4]*sin(BGAngle+i*60+30));
				}

				SetAlpha(stage5alpha6*0.5/2);
				SetGraphicAngle(0,-BGAngle+90-i*60,0);
				if(PillarHeight[2]>-640 && sin(BGAngle+i*60)<-0.5)
				{
				DrawGraphic3D(PillarShakeX[2]+PillarRadius[5]*cos(BGAngle+i*60),PillarHeight[2],PillarShakeY[2]+PillarRadius[5]*sin(BGAngle+i*60));
				DrawGraphic3D(PillarShakeX[2]+PillarRadius[6]*cos(BGAngle+i*60),PillarHeight[2],PillarShakeY[2]+PillarRadius[6]*sin(BGAngle+i*60));
				}
			}

			ascent(i in 0..6)
			{
				SetTexture(csd~"img\Pillar2.png");
				SetAlpha(stage5alpha3);
				SetGraphicRect(0,0,240,2560);
				SetGraphicScale(0.125,0.5);

				if(PillarHeight[0]>-640 && sin(BGAngle+i*60)<0.9)
				{
				SetColor(255,255,255);
				SetGraphicAngle(0,-BGAngle+90-i*60,0);
				DrawGraphic3D(PillarShakeX[0]+PillarRadius[0]*cos(BGAngle+i*60),PillarHeight[0],PillarShakeY[0]+PillarRadius[0]*sin(BGAngle+i*60));
				DrawGraphic3D(PillarShakeX[0]+PillarRadius[1]*cos(BGAngle+i*60),PillarHeight[0],PillarShakeY[0]+PillarRadius[1]*sin(BGAngle+i*60));

				SetGraphicAngle(0,90-BGAngle+90-i*60,0);
				DrawGraphic3D(PillarShakeX[0]+PillarRadius[2]*cos(BGAngle+i*60)+15*cos(BGAngle+i*60+90),PillarHeight[0],PillarShakeY[0]+PillarRadius[2]*sin(BGAngle+i*60)+15*sin(BGAngle+i*60+90));
				DrawGraphic3D(PillarShakeX[0]+PillarRadius[2]*cos(BGAngle+i*60)-15*cos(BGAngle+i*60+90),PillarHeight[0],PillarShakeY[0]+PillarRadius[2]*sin(BGAngle+i*60)-15*sin(BGAngle+i*60+90));
				}

				SetColor(255*0.75,255*0.75,255*0.75);
				SetGraphicAngle(0,-BGAngle+90-i*60-30,0);

				if(PillarHeight[1]>-640 && sin(BGAngle+i*60+30)<-0.5)
				{
				DrawGraphic3D(PillarShakeX[1]+PillarRadius[3]*cos(BGAngle+i*60+30),PillarHeight[1],PillarShakeY[1]+PillarRadius[3]*sin(BGAngle+i*60+30));
				DrawGraphic3D(PillarShakeX[1]+PillarRadius[4]*cos(BGAngle+i*60+30),PillarHeight[1],PillarShakeY[1]+PillarRadius[4]*sin(BGAngle+i*60+30));
				}

				SetColor(255*0.5,255*0.5,255*0.5);
				SetGraphicAngle(0,-BGAngle+90-i*60,0);

				if(PillarHeight[2]>-640 && sin(BGAngle+i*60)<-0.5)
				{
				DrawGraphic3D(PillarShakeX[2]+PillarRadius[5]*cos(BGAngle+i*60),PillarHeight[2],PillarShakeY[2]+PillarRadius[5]*sin(BGAngle+i*60));
				DrawGraphic3D(PillarShakeX[2]+PillarRadius[6]*cos(BGAngle+i*60),PillarHeight[2],PillarShakeY[2]+PillarRadius[6]*sin(BGAngle+i*60));
				}
			}

			WriteZBuffer(false);
			UseZBuffer(false);
			SetTexture(csd~"img\st4bg1.png");
			SetRenderState(ALPHA);
			SetGraphicScale(5,5);
			SetGraphicAngle(0,0,0);
			SetAlpha(stage5alphacloud);
			SetColor(255,255,255);
			SetGraphicRect(0-bgX,0-bgY,6000-bgX,6000-bgY);
			DrawGraphic3D(0,0,1000);

			SetGraphicRect(0+bgX,0-bgY,6000+bgX,6000-bgY);
			DrawGraphic3D(0,5,1000);

		}
LoadGraphic(csd~"img\st5bg2.png");
		if(StageProgress==5 && Stage5Phase==4)
		{

			SetViewTo(ViewToX+ScreenShakeX,50,ViewToZ+ScreenShakeY);
			SetViewFrom(1,ViewFromY,-90+ViewFromZ);
			WriteZBuffer(true);
			UseZBuffer(true);
ViewFromY-=1;
ViewFromZ=16+16*cos(cccc);
ViewFromZ=16+16*cos(bbbb);
ViewToX=80*cos(aaaa);
aaaa+=0.7;
bbbb+=0.3;
cccc+=0.17;
			SetRenderState(ALPHA);

			SetTexture(csd~"img\st5bg2.png");
			SetColor(100,100,100);
			SetGraphicScale(0.16*10,0.2*10);
			SetAlpha(stage5alpha1);

			let FogColor=HueDifine(BGHueCount);
			SetFog(10,6000,FogColor[0]*0.5,FogColor[1]*0.5,FogColor[2]*0.5);
		CloudSep=4;
			ascent(i in 0..CloudSep)
			{
				ascent(k in 0..4)
				{

					SetTexture(csd~"img\st5bg2.png");
					SetGraphicRect(0+i*200+bgX*0,2000+bgY+1000*k,200+i*200+bgX*0,3000+bgY+1000*k);
					SetGraphicAngle(0,i*(360/CloudSep)+BoxAngle,0);
					DrawGraphic3D(sin(i*(360/CloudSep)+BoxAngle)*150,-100+160+2000*k,cos(i*(360/CloudSep)+BoxAngle)*150);
					SetTexture(csd~"img\st5bg2.png");
					SetGraphicRect(10+i*40-bgX,2080+400*k+bgY,50+i*40-bgX,2480+400*k+bgY);
					SetGraphicAngle(0,i*(360/CloudSep)+BoxAngle,0);
				//	DrawGraphic3D(sin(i*(360/CloudSep))*15+BoxAngle,-100+160+80*k,cos(i*(360/CloudSep))*15+BoxAngle);
				}
			}
		BGHueCount+=1;
		}
	}
}
let BGHueCount=0;
let aaaa=90;
let bbbb=90;
let cccc=90;
let BoxAngle=0;

task BGGraphicRect
{ViewFromX=0;
	if(StageProgress==1)
	{
		if(StagePhaseProgress<=0 || StagePractice==false)
		{
		descent(let i in 0..240)
		{
			if(stage1alpha1<=80)
			{
				stage1alpha1+=80/60;
			}
			if(stage1alpha2<=120)
			{
				stage1alpha2+=120/60;
			}
			if(stage1alpha3<=40)
			{
				stage1alpha3+=20/60;
			}
			if(stage1alpha4<200)
			{
				stage1alpha4+=250/60;
			}
			if(stage1alpha5<230)
			{
				stage1alpha5+=230/60;
			}
			if(absolute(bgX)>=512){bgX=0;}
			if(absolute(bgY)>=512){bgY=0;}
			if(absolute(bgX2)>=512){bgX2=0;}
			if(absolute(bgY2)>=512){bgY2=0;}
			if(absolute(bgY3)>=512){bgY3=0;}
			if(ViewFromX>0)
			{
				ViewFromX-=300/240;
			}
			bgX += 0.5;
			bgY += 3+18*i/240;
			bgX2 += 0.5;
			bgY2 += 3+18*i/240;
			bgY3 += 2+12*i/240;
			bgX4 += 0.5;
			if(Stage1BossAdjust<15)
			{
				Stage1BossAdjust+=15/240;
			}
			SkyRect+=0.2;
			WaterWave+=0.5;
			yield;
		}
		stage1scale3=1;
		}
		if(StagePhaseProgress==1 && StagePractice==true)
		{
			Stage1BGPhase1;
		}
		if(StagePhaseProgress==2 && StagePractice==true)
		{
			Stage1BGPhase1;
		}
		if(StagePhaseProgress==3 && StagePractice==true)
		{
			Stage1BGPhase3;
		}
	}
	while(StageProgress==1)
	{
		if(Stage1BossAdjust<20)
		{
			Stage1BossAdjust+=5/240;
		}
		WaterWave+=0.5;
		SkyRect+=0.2;
		if(SkyRect>=1024){SkyRect-=1024;}
		if(absolute(bgX)>=512){bgX=0;}
		if(absolute(bgY)>=512){bgY=0;}
		if(absolute(bgX2)>=512){bgX2=0;}
		if(absolute(bgY2)>=512){bgY2=0;}
		if(absolute(bgY3)>=512){bgY3=0;}

			if(GetCommonDataDefault("STAGE1MIDBOSS",false))
			{
				if(StageProgress==1)
				{
				bgX += 0.5;
				bgY += 1+2*stage1BGCount/30;
				bgX2 += 0.5;
				bgY2 += 1+2*stage1BGCount/30;
				bgY3 += 1+2*stage1BGCount/30;
				bgX4 += 0.5;
				}
				if(stage1BGCount>0)
				{
					stage1BGCount-=1;
				}		
				if(BossAdjust>-15)
				{
					BossAdjust-=0.1;
				}
			}
			else if(GetCommonDataDefault("STAGE1BOSS",false))
			{
				bgX += 0.5;
				bgY += 1+2*stage1BGCount/30;
				bgX2 += 0.5;
				bgY2 += 1+2*stage1BGCount/30;
				bgY3 += 1+2*stage1BGCount/30;
				bgX4 += 0.5;
				if(ViewFromX<100)
				{
		//		ViewFromX+=100/120;
				}
				if(stage1BGCount>0)
				{
					stage1BGCount-=1;
				}
				if(BossAdjust>-12)
				{
					BossAdjust-=0.1;
				}
			}	
			else 
			{
				bgX += 0.5;
				bgY += 3;
				bgX2 += 0.5;
				bgY2 += 3;
				bgY3 += 2;
				bgX4 += 0.5;
				if(stage1BGCount<30)
				{
					stage1BGCount+=1;
				}
				if(BossAdjust<0)
				{
				BossAdjust+=0.1;
				}
			}
		if(StageChange){break;}

		yield;
	}
	let ViewFromXAngle=0;
	let S1count=0;
	while(StageChange)
	{
		if(S1count>=120)
		{
		ViewFromX-=1+2*sin(ViewFromXAngle);
		if(ViewFromXAngle<90)
		{
		ViewFromXAngle+=1;
		}
			DrawZ-=0.5*sin(ViewFromXAngle);
			DrawY-=0.5*sin(ViewFromXAngle);
		}
		S1count+=1;
		if(absolute(bgX)>=640){bgX=0;}
		if(absolute(bgY)>=480){bgY=0;}
		if(absolute(bgX2)>=640){bgX2=0;}
		if(absolute(bgY2)>=480){bgY2=0;}
		if(absolute(bgY3)>=512){bgY3=0;}

				bgX += 0.5;
				bgY += 1;
				bgX2 += 0.5;
				bgY2 += 1;
				bgY3 += 1;
				bgX4 += 0.5;
				if(ViewFromX<100)
				{
		//		ViewFromX+=100/120;
				}
				if(BossAdjust>-15)
				{
					BossAdjust-=0.1;
				}
		if(Stage1BossAdjust>0)
		{
			Stage1BossAdjust-=10/240;
		}

		if(stage1alpha1>0)
		{
			stage1alpha1-=80/240;
		}
		if(stage1alpha2>0)
		{
			stage1alpha2-=120/240;
		}
			if(stage1alpha4>0)
			{
				stage1alpha4-=250/240;
			}
			if(stage1alpha5>0)
			{
				stage1alpha5-=255/240;
			}
		yield;
	}

	bgX=0;
	bgX2=300;
	bgY=0;
	bgY2=300;
	bgY3=300;
	BossAdjust=0;
	ViewFromX=0;

	let st2bg1angle=0;
	let MidBossEscape=false;

	if(StageProgress==2)
	{
	Stage2BGInitialize;
	if(StagePhaseProgress==1 && StagePractice==true)
	{
		MoonX=-20;
		MoonY=-10;
		MoonScale=0.97;
		while(stage2alpha1<255)
		{
		stage2alpha1+=255/120;
		Stage2BGCount+=2;
		bgY2+=2;
		if(Stage2BGCount>=150)
		{
			St2MtSet;
		}
		if(absolute(bgX)>=480){bgX=0;}
		if(absolute(bgY)>=480){bgY=0;}
		if(absolute(bgX2)>=478){bgX2=0;}
		if(absolute(bgY2)>=478){bgY2=0;}
		yield;
		}
		MidBossEscape=true;
	}
	if(StagePhaseProgress==2 && StagePractice==true)
	{
		MoonX=0;
		MoonY=50;
		MoonScale=1.0;
		while(stage2alpha1<255)
		{
		stage2alpha1+=255/120;
		Stage2BGCount+=2;
		bgY2+=2;
		if(Stage2BGCount>=150)
		{
			St2MtSet;
		}
		if(absolute(bgX)>=480){bgX=0;}
		if(absolute(bgY)>=480){bgY=0;}
		if(absolute(bgX2)>=478){bgX2=0;}
		if(absolute(bgY2)>=478){bgY2=0;}
		yield;
		}
		MidBossEscape=true;
	}
	if(StagePhaseProgress==3 && StagePractice==true)
	{
		MoonX=200;
		MoonY=100;
		MoonScale=1.3;
		while(stage2alpha1<255)
		{
		stage2alpha1+=255/120;
		Stage2BGCount+=2;
		bgY2+=2;
		if(Stage2BGCount>=150)
		{
			St2MtSet;
		}
		if(absolute(bgX)>=480){bgX=0;}
		if(absolute(bgY)>=480){bgY=0;}
		if(absolute(bgX2)>=478){bgX2=0;}
		if(absolute(bgY2)>=478){bgY2=0;}
		yield;
		}
	}
	else
	{
		while(stage2alpha1<255)
		{
		stage2alpha1+=255/120;
		Stage2BGCount+=2;
		bgY2+=1;
		if(Stage2BGCount>=150)
		{
			St2MtSet;
		}
		if(absolute(bgX)>=480){bgX=0;}
		if(absolute(bgY)>=480){bgY=0;}
		if(absolute(bgX2)>=478){bgX2=0;}
		if(absolute(bgY2)>=256){bgY2=0;}
		yield;
		}
	}
	}
	while(StageProgress==2)
	{
			if(GetCommonDataDefault("STAGE2MIDBOSS",false))//中ボス
			{
				Stage2BGCount+=2*cos(st2bg1angle);
				bgY2+=1*cos(st2bg1angle);
					if(MoonX<0)
					{
						MoonX+=200/2400;
					}
					if(MoonY<0)
					{
						MoonY+=50/2400;
					}
					if(MoonScale<1)
					{
						MoonScale+=0.3/2400;
					}
				if(MoonColor[2]>=150)
				{
					MoonColor[2]=MoonColor[2]-0.2;
				}
				if(st2bg1angle<60)
				{
					st2bg1angle+=0.5;
				}
				MidBossEscape=true;
			}
			else if(GetCommonDataDefault("STAGE2BOSS",false))//ボス
			{
				Stage2BGCount+=2*cos(st2bg1angle);
				bgY2+=1*cos(st2bg1angle);
					if(MoonX<200)
					{
						MoonX+=200/2400;
					}
					if(MoonY<100)
					{
						MoonY+=50/2400;
					}
					if(MoonScale<1.3)
					{
						MoonScale+=0.3/2400;
					}
				if(MoonColor[2]>200)
				{
					MoonColor[1]=MoonColor[1]-0.2;
					MoonColor[2]=MoonColor[2]-0.2;
				}
				if(st2bg1angle<60)
				{
					st2bg1angle+=0.5;
				}
			}	
			else 
			{
				if(MidBossEscape)//道中後半
				{
					Stage2BGCount+=2*cos(st2bg1angle);
					bgY2+=1*cos(st2bg1angle);
				if(st2bg1angle>0)
				{
					st2bg1angle-=0.5;
				}
					if(MoonX<200)
					{
						MoonX+=200/2400;
					}
					if(MoonY<100)
					{
						MoonY+=50/2400;
					}
					if(MoonScale<1.3)
					{
						MoonScale+=0.3/2400;
					}
				if(MoonColor[2]<255)
				{
					MoonColor[2]=MoonColor[2]+0.5;
				}
				}
				else//道中前半
				{
					Stage2BGCount+=2*cos(st2bg1angle);
					bgY2+=1*cos(st2bg1angle);
					if(MoonX<0)
					{
						MoonX+=200/2400;
					}
					if(MoonY<0)
					{
						MoonY+=50/2400;
					}
					if(MoonScale<1)
					{
						MoonScale+=0.3/2400;
					}
				}
			}

		if(Stage2BGCount>=150)
		{
			St2MtSet;
		}
		if(absolute(bgX)>=480){bgX=0;}
		if(absolute(bgY)>=480){bgY=0;}
		if(absolute(bgX2)>=478){bgX2=0;}
		if(absolute(bgY2)>=256){bgY2=0;}

		if(StageChange){break;}
		yield;
	}

	while(StageChange)
	{
		if(stage2alpha1>0)
		{
		stage2alpha1-=255/240;
		}
		Stage2BGCount+=1;
		bgY2+=1;
		if(Stage2BGCount>=150)
		{
			St2MtSet;
		}
		if(absolute(bgX)>=480){bgX=0;}
		if(absolute(bgY)>=480){bgY=0;}
		if(absolute(bgX2)>=478){bgX2=0;}
		if(absolute(bgY2)>=478){bgY2=0;}


		if(stage2alpha1>0)
		{
			stage2alpha1-=255/240;
		}
		if(stage2alpha2>0)
		{
			stage2alpha2-=120/240;
		}
		yield;
	}

	MidBossEscape=false;
	let st3bgYspeed=5;
	let st3bgYspeed2=5;
	if(StageProgress==3)
	{
		ViewFromX=300;
		ViewFromZ=15;
		if(StagePhaseProgress==1 && StagePractice==true)
		{
		ViewFromX=0;
		ViewFromZ=0;
			IniMtSet;
			while(stage3alpha1<255)
			{
				stage3alpha1+=255/120;
				stage3alpha2+=80/120;
				bgY -= st3bgYspeed;
				bgY3 += st3bgYspeed;
				bgY2 += st3bgYspeed;
				bgX2 -= 2;
				if(absolute(bgX)>=480){bgX-=480;}
			//	if(absolute(bgY)>=480){bgY-=480;}
				if(absolute(bgX2)>=1024){bgX2=0;}
				if(absolute(bgY2)>=768){bgY2=0;}
				if(absolute(bgX3)>=478){bgX3=0;}
				if(absolute(bgY3)>=478){bgY3=0;}

				Stage3BGCount+=st3bgYspeed;
				if(Stage3BGCount>=75)
				{
					St3MtSet;
				}
				yield;
			}
		}
		else if(StagePhaseProgress==2 && StagePractice==true)
		{
		ViewFromX=0;
		ViewFromZ=0;
			st3bgYspeed=1;
			MidBossEscape=true;
			IniMtSet;
			while(stage3alpha1<255)
			{
				stage3alpha1+=255/120;
				stage3alpha2+=80/120;
				bgY -= st3bgYspeed;
				bgY3 += st3bgYspeed;
				bgY2 += st3bgYspeed;
				bgX2 -= 2;
				if(absolute(bgX)>=480){bgX-=480;}
			//	if(absolute(bgY)>=480){bgY-=480;}
				if(absolute(bgX2)>=1024){bgX2=0;}
				if(absolute(bgY2)>=768){bgY2=0;}
				if(absolute(bgX3)>=478){bgX3=0;}
				if(absolute(bgY3)>=478){bgY3=0;}

				Stage3BGCount+=st3bgYspeed;
				if(Stage3BGCount>=75)
				{
					St3MtSet;
				}
				yield;
			}
		}
		else if(StagePhaseProgress==3 && StagePractice==true)
		{
		ViewFromX=0;
		ViewFromZ=0;
			MtHeightMin=-60;
			MtHeightMid-=100;
			MtHeightMax-=100;
			MtXMin-=100;
			St3DrawCenterXWide-=80;
			MidBossEscape=true;
			IniMtSet;

			while(stage3alpha1<255)
			{
				stage3alpha1+=255/120;
				stage3alpha3+=120/120;
				bgY -= st3bgYspeed;
				bgY3 += st3bgYspeed;
				bgY2 += st3bgYspeed;
				bgX2 -= 2;
				if(absolute(bgX)>=480){bgX-=480;}
			//	if(absolute(bgY)>=480){bgY-=480;}
				if(absolute(bgX2)>=1024){bgX2=0;}
				if(absolute(bgY2)>=768){bgY2=0;}
				if(absolute(bgX3)>=478){bgX3=0;}
				if(absolute(bgY3)>=478){bgY3=0;}

				Stage3BGCount+=st3bgYspeed;
				if(Stage3BGCount>=75)
				{
					St3MtSet;
				}
				yield;
			}
		}
		else
		{
			IniMtSet;
			while(stage3alpha1<255)
			{
				stage3alpha1+=255/120;
				stage3alpha2+=80/120;
				bgY -= st3bgYspeed;
				bgY3 += st3bgYspeed;
				bgY2 += st3bgYspeed;
				bgX2 -= 2;
				if(absolute(bgX)>=480){bgX-=480;}
			//	if(absolute(bgY)>=480){bgY-=480;}
				if(absolute(bgX2)>=1024){bgX2=0;}
				if(absolute(bgY2)>=768){bgY2=0;}
				if(absolute(bgX3)>=478){bgX3=0;}
				if(absolute(bgY3)>=478){bgY3=0;}

				Stage3BGCount+=st3bgYspeed;
				if(Stage3BGCount>=75)
				{
					St3MtSet;
				}
				yield;
			}
		}
	}

	while(StageProgress==3)
	{
		St3DrawCenterX=St3DrawCenterXWide*sin(St3DrawCenterXAngle);
		St3DrawCenterXAngle+=1;
		if(ViewFromX>0 && Stage3BGCount2>=1800)
		{
		ViewFromX-=300/600;
		ViewFromZ-=15/600;
		}
		else
		{
		Stage3BGCount2+=1;
		}
		if(absolute(bgX)>=256){bgX-=256;}
		if(absolute(bgX2)>=1024){bgX2=0;}
		if(absolute(bgY2)>=768){bgY2=0;}
		if(absolute(bgX3)>=478){bgX3-=478;}
		if(absolute(bgY3)>=478){bgY3-=478;}
				Stage3BGCount+=st3bgYspeed;
				if(Stage3BGCount>=75)
				{
					St3MtSet;
				}

			if(GetCommonDataDefault("STAGE3MIDBOSS",false))//中ボス
			{
				bgY -= st3bgYspeed;
				bgY3 += st3bgYspeed;
				bgY2 += st3bgYspeed;
				bgX2 -= 2;

				if(st3bgYspeed>1)
				{
				st3bgYspeed-=0.05;
				}
				MidBossEscape=true;
			}
			else if(GetCommonDataDefault("STAGE3BOSS",false))//ボス
			{
				bgY -= st3bgYspeed;
				bgY3 += st3bgYspeed;
				bgY2 += st3bgYspeed;
				bgX2 -= 2;

				if(st3bgYspeed>1)
				{
				st3bgYspeed-=0.05;
				}
				if(BossAdjust>-20)
				{
					BossAdjust-=0.1;
				}
				if(ViewFromZ>-20)
				{
					ViewFromZ-=0.1;
				}
				if(ViewFromZ<-10)
				{
					stage3alpha4=80;
				}
			}	
			else//道中
			{
				bgY -= st3bgYspeed;
				bgY3 += st3bgYspeed;
				bgY2 += st3bgYspeed;
				bgX2 -= 2;
				if(st3bgYspeed<5)
				{
					st3bgYspeed+=0.05;
				}
				if(MidBossEscape==true && !GetCommonDataDefault("STAGE3MIDBOSSHAYAMAWASI",false))
				{
					if(MtHeightMin>-60)
					{
					MtHeightMin-=0.03;
					MtHeightMid-=0.05;
					MtHeightMax-=0.05;
					MtXMin-=0.05;
					St3DrawCenterXWide-=0.04;
					}
					if(stage3alpha2>0)
					{
						stage3alpha2-=80/2000;
						stage3alpha3+=120/2000;
					}
				}
			}
		if(StageChange){break;}
		yield;
	}

	while(StageChange)
	{
		if(absolute(bgX)>=256){bgX=0;}
		if(absolute(bgY)>=256){bgY=0;}
		if(absolute(bgX2)>=1024){bgX2=0;}
		if(absolute(bgY2)>=768){bgY2=0;}
		if(absolute(bgX3)>=478){bgX3=0;}
		if(absolute(bgY3)>=478){bgY3=0;}
				Stage3BGCount+=st3bgYspeed;
				if(Stage3BGCount>=75)
				{
					St3MtSet;
				}
				bgY -= st3bgYspeed;
				bgY3 += st3bgYspeed;
				bgY2 += st3bgYspeed;
				bgX2 -= 2;

		if(stage3alpha1>0)
		{
			stage3alpha1-=255/240;
			stage3alpha3-=120/240;
			stage3alpha4-=80/240;
		}
		yield;
	}
	if(StageProgress==4)
	{
		stage4bgfromz=45;
		if(StagePhaseProgress<=0 || StagePractice==false)
		{
			loop(1620)
			{
				if(stage4alpha1<120)
				{
					stage4alpha1+=120/120;
				}
				bgY += 5;
			bgY2 -= 5;
				if(absolute(bgX)>=400){bgX=0;}
				if(absolute(bgY)>=400){bgY=0;}
				if(absolute(bgX2)>=400){bgX2=0;}
				if(absolute(bgY2)>=400){bgY2=0;}
				yield;
			}
			loop(480)
			{
				stage4bgfromz-=8/2000;
				stage4bgfromy+=0.19;
				stage4alpha1-=120/480;
				stage4alpha3+=255/480;
				bgY += 5;
				bgY2 -= 5;
				if(absolute(bgX)>=400){bgX=0;}
				if(absolute(bgY)>=400){bgY=0;}
				if(absolute(bgX2)>=400){bgX2=0;}
				if(absolute(bgY2)>=400){bgY2=0;}
				yield;
			}
		}
		if(StagePhaseProgress==1 && StagePractice==true)
		{
			Stage4BGPhase1;
		}
		if(StagePhaseProgress<=1 || StagePractice==false)
		{
			loop
			{
			if(stage4bgfromz>0)
			{
				stage4bgfromz-=5/2000;
			}


				stage4bgfromy+=0.19;
				bgY += 5;
				bgY2 -= 5;
				if(absolute(bgX)>=400){bgX=0;}
				if(absolute(bgY)>=400){bgY=0;}
				if(absolute(bgX2)>=400){bgX2=0;}
				if(absolute(bgY2)>=400){bgY2=0;}
				yield;
				if(GetCommonDataDefault("STAGE4MIDBOSS",false)){break;}
			}

			loop
			{
				stage4bgfromy+=0.1;
				bgY += 5;
				bgY2 -= 5;
				if(absolute(bgX)>=400){bgX=0;}
				if(absolute(bgY)>=400){bgY=0;}
				if(absolute(bgX2)>=400){bgX2=0;}
				if(absolute(bgY2)>=400){bgY2=0;}
				if(stage4fogRGB[1]>175)
				{
				stage4fogRGB[1]=stage4fogRGB[1]-0.2;
				stage4fogRGB[2]=stage4fogRGB[2]-0.2;
				}
				if(!GetCommonDataDefault("STAGE4MIDBOSSHAYAMAWASHI",true) && !GetCommonDataDefault("STAGE4MIDBOSS",false)){break;}
				yield;
			}
		}
		if(StagePhaseProgress==2 && StagePractice==true)
		{
			stage4bgfromz=40;
			Stage4BGPhase2;
		}
		if(StagePhaseProgress<=2 || StagePractice==false)
		{
			Adjuststage4bgfromy=stage4bgfromy%360;
				
			loop(900)
			{
				if(stage4alpha1<255 && stage4bgfromz>=0)
				{
					stage4alpha1+=255/240;
				}
				else if(stage4alpha1>0)
				{
					stage4alpha1-=255/480;
				}
				if(stage4alpha1>=255)
				{
					stage4bgfromz-=0.5;
				}
				stage4bgfromy+=0.1;
				stage4bgfromy+=(290-Adjuststage4bgfromy)/4400;
				bgY += 5;
				bgY2 -= 5;
				bgX += 2;
				bgX2 -= 2;
				if(absolute(bgX)>=400){bgX=0;}
				if(absolute(bgY)>=400){bgY=0;}
				if(absolute(bgX2)>=400){bgX2=0;}
				if(absolute(bgY2)>=400){bgY2=0;}
				if(stage4fogRGB[1]<255)
				{
				stage4fogRGB[1]=stage4fogRGB[1]+0.2;
				stage4fogRGB[2]=stage4fogRGB[2]+0.2;
				}
				yield;
			}
		}
		if(StagePhaseProgress==3 && StagePractice==true)
		{
			Stage4BGPhase3;
		}
			loop
			{
				if(stage4alpha1<255 && stage4bgfromz>=0)
				{
					stage4alpha1+=255/240;
				}
				else if(stage4alpha1>0)
				{
					stage4alpha1-=255/480;
				}
				if(stage4alpha1>=255)
				{
					stage4bgfromz-=0.5;
				}
				stage4bg2zangle+=1;
				stage4bgfromy+=0.1;
				stage4bgfromy+=(290-Adjuststage4bgfromy)/4400;
				bgY += 5;
				bgY2 -= 5;
				bgX += 2;
				bgX2 -= 2;
				if(absolute(bgX)>=400){bgX=0;}
				if(absolute(bgY)>=400){bgY=0;}
				if(absolute(bgX2)>=400){bgX2=0;}
				if(absolute(bgY2)>=400){bgY2=0;}
				if(stage4fogRGB[1]>175)
				{
					stage4fogRGB[1]=stage4fogRGB[1]-0.1;
				}
				if(stage4alpha2<50)
				{
					stage4alpha2+=0.02;
				}
				if(stage4bg2scale<3)
				{
					stage4bg2scale+=4/2500;
				}
				if(GetCommonDataDefault("STAGE4BOSS",false)){break;}
				yield;
			}
		loop(150)
		{
			stage4bg2zangle+=1;
			stage4bgfromy+=0.1;
			if(stage4bgfromz<90)
			{
			stage4bgfromz+=0.8;
			stage4bgLeftLayerX+=8.0;
			}
			bgY += 5;
			bgY2 -= 5;
			bgX += 2;
			bgX2 -= 2;
			if(absolute(bgX)>=400){bgX=0;}
			if(absolute(bgY)>=400){bgY=0;}
			if(absolute(bgX2)>=400){bgX2=0;}
			if(absolute(bgY2)>=400){bgY2=0;}
			yield;
		}
		stage4bg2scale=3;
		let fromzplus=0.5;
		let LeftLayerX=stage4bgLeftLayerX/2;
		loop(240)
		{
			if(stage4bgfromz<180)
			{
			stage4bgfromz+=fromzplus;
			stage4bgLeftLayerX-=fromzplus*10/2+LeftLayerX/180;
			}

			if(fromzplus>1)
			{
				fromzplus-=0.5/120;
			}
			bgY += 5;
			bgY2 -= 5;
			bgX += 2;
			bgX2 -= 2;
			if(absolute(bgX)>=400){bgX=0;}
			if(absolute(bgY)>=400){bgY=0;}
			if(absolute(bgX2)>=400){bgX2=0;}
			if(absolute(bgY2)>=400){bgY2=0;}
			yield;
		}

		loop
		{
			stage4bg2zangle-=1;
			stage4bgfromy-=0.1;
			bgY += 5;
			bgY2 -= 5;
			bgX += 2;
			bgX2 -= 2;
			if(absolute(bgX)>=400){bgX=0;}
			if(absolute(bgY)>=400){bgY=0;}
			if(absolute(bgX2)>=400){bgX2=0;}
			if(absolute(bgY2)>=400){bgY2=0;}
			if(StageProgress!=4){break;}
			if(StageChange){break;}
			yield;
		}
	}

	while(StageChange)
	{
		stage4bg2zangle-=1;
		stage4bgfromy-=0.1;
		bgY += 5;
		bgY2 -= 5;
		bgX += 2;
		bgX2 -= 2;
		if(absolute(bgX)>=400){bgX=0;}
		if(absolute(bgY)>=400){bgY=0;}
		if(absolute(bgX2)>=400){bgX2=0;}
		if(absolute(bgY2)>=400){bgY2=0;}

		if(stage4alpha1>0)
		{
			stage4alpha1-=255/240;
		}
		if(stage4alpha2>0)
		{
			stage4alpha2-=50/240;
		}
		if(stage4alpha3>0)
		{
			stage4alpha3-=255/240;
		}
		yield;
	}

	let ScrollSpeed=0;
	let ViewToWeit=5;
	let ViewToMove=0.1;
	ViewFromY=0;
	ViewToX=0;
	ViewToY=0;
	ViewToZ=0;
	ViewFromZ=60;
	if(StagePractice)
	{
	alternative(StagePhaseProgress)
	case(0){Stage5Phase=0;}
	case(1){Stage5Phase=0;}
	case(2){Stage5Phase=1;}
	case(3){Stage5Phase=3;}
	}
	if(StageProgress==5 && Stage5Phase==0)
	{
		Stage5BGInitialize;
	}
	if(StagePractice && StagePhaseProgress==1)
	{
		ScrollSpeed=5;
		ViewFromZ=5;
	}
	while(StageProgress==5 && Stage5Phase==0)
	{

			if(stage5alpha1<=80)
			{
				stage5alpha1+=80/120;
			}
			if(stage5alpha2<=40)
			{
				stage5alpha2+=40/120;
			}
			if(ViewFromZ>5)
			{
				ViewFromZ-=0.05;
				if(ViewFromZ>10)
				{
					ViewFromZ-=0.05;
				}	
			}
			if(ScrollSpeed<5)
			{
				ScrollSpeed+=0.01;
			}
			bgX+=1;
			bgY+=ScrollSpeed;
			St5MagicScroll+=3/5*ScrollSpeed;
			if(St5MagicScroll>=5)
			{
			Stage5MagicSet;
			St5MagicScroll-=5;
			}
			if(GetPlayerX<GetCenterX)
			{
				if(ViewToX>ViewToWeit*(GetPlayerX-GetCenterX)/GetCenterX)
				{
					ViewToX-=ViewToMove;
				}
				else
				{
					ViewToX+=ViewToMove;
				}
			}
			else
			{
				if(ViewToX<ViewToWeit*(GetPlayerX-GetCenterX)/GetCenterX)
				{
					ViewToX+=ViewToMove;
				}
				else
				{
					ViewToX-=ViewToMove;
				}
			}

			if(GetPlayerY<GetCenterY)
			{
				if(ViewToZ>-ViewToWeit*(GetPlayerY-GetCenterY)/GetCenterY)
				{
					ViewToZ-=ViewToMove;
				}
				else
				{
					ViewToZ+=ViewToMove;
				}
			}
			else
			{
				if(ViewToZ<-ViewToWeit*(GetPlayerY-GetCenterY)/GetCenterY)
				{
					ViewToZ+=ViewToMove;
				}
				else
				{
					ViewToZ-=ViewToMove;
				}
			}
			if(GetCommonDataDefault("STAGE5MIDBOSSHAYAMAWASI",false))
			{
				if(ViewToWeit>0.01)
				{
				ViewToWeit-=5/2000;
				ViewToMove-=0.1/2000;
				ViewFromZ-=5/2000;
				}
			}

		//	ViewToX=5*(GetPlayerX-GetCenterX)/GetCenterX;
		//	ViewToZ=-5*(GetPlayerY-GetCenterY)/GetCenterY;
			if(absolute(bgX)>=600){bgX=0;}
		//	if(absolute(bgY)>=480){bgY=0;}
			if(absolute(bgX2)>=640){bgX2=0;}
			if(absolute(bgY2)>=480){bgY2=0;}
			if(absolute(bgY3)>=512){bgY3=0;}

		yield;
	}
	//ViewFromZ=5;
	stage5alpha1=0;
	stage5alpha2=0;
	ViewToWeit=3;
	ViewToMove=0.06;
	ScrollSpeed=0;
	let ViewToYScrollSpeed=0;
	if(StagePractice && StagePhaseProgress==2){PillarPos+=1500;}
	while(StageProgress==5 && Stage5Phase==1)
	{
			if(stage5alpha1<100)
			{
				stage5alpha1+=100/120;
			}
			if(stage5alpha2<255)
			{
				stage5alpha2+=255/120;
			}
			if(stage5alpha3<1)
			{
				stage5alpha3+=1/120;
			}
			bgX+=1;
			bgY+=5+ScrollSpeed;
			
			PillarPos+=5+ScrollSpeed;

			if(GetPlayerX<GetCenterX)
			{
				if(ViewFromY>ViewToWeit*(GetPlayerX-GetCenterX)/GetCenterX)
				{
					ViewFromY-=ViewToMove;
				}
				else
				{
					ViewFromY+=ViewToMove;
				}
			}
			else
			{
				if(ViewFromY<ViewToWeit*(GetPlayerX-GetCenterX)/GetCenterX)
				{
					ViewFromY+=ViewToMove;
				}
				else
				{
					ViewFromY-=ViewToMove;
				}
			}

			if(GetCommonDataDefault("Stage5MoveSignal0",false)==true)
			{
				ViewToX-=1800/1740;
			}

			if(GetCommonDataDefault("Stage5MoveSignal1",false)==true)
			{
				
				if(TransPortZ<100000)
				{
				TransPortZ+=5+ScrollSpeed;
				}
				if(stage5alpha4<100)
				{
				stage5alpha4+=100/1200;
				}
				if(ScrollSpeed<5.5)
				{
					ScrollSpeed+=0.01;
				}
			}
			else if(GetCommonDataDefault("Stage5MoveSignal2",false)==true)
			{
				if(TransPortZ<100000)
				{
				TransPortZ+=5+ScrollSpeed;
				}
				if(ScrollSpeed>-5)
				{
					ScrollSpeed-=10/300;
				}
				if(ScrollSpeed<-5)
				{
					ScrollSpeed=-5;
				}
			}
			if(GetCommonDataDefault("Stage5MoveSignal3",false)==true)
			{
				if(stage5alpha5<150)
				{
					stage5alpha5+=150/60;
					stage5alpha1-=100/60;
				}
				if(ViewToYScrollSpeed<15)
				{
					ViewToYScrollSpeed+=0.2;
				}
				ViewToY-=ViewToYScrollSpeed;
			}
			if(GetCommonDataDefault("Stage5MoveSignal4",false)==true)
			{
				if(ViewToYScrollSpeed>0)
				{
					ViewToYScrollSpeed-=0.1;
				}
				ViewToY-=ViewToYScrollSpeed;
			}
			if(GetCommonDataDefault("Stage5MoveSignal5",false)==true)
			{
				if(stage5alpha5>0)
				{
					stage5alpha5-=150/120;
				}
			}
			
		//	ViewToX=5*(GetPlayerX-GetCenterX)/GetCenterX;
		//	ViewToZ=-5*(GetPlayerY-GetCenterY)/GetCenterY;
		//	if(absolute(bgX)>=600){bgX=0;}
		//	if(absolute(bgY)>=480){bgY=0;}
			if(absolute(bgX2)>=640){bgX2=0;}
			if(absolute(bgY2)>=480){bgY2=0;}
			if(absolute(bgY3)>=512){bgY3=0;}

		yield;
	}
	stage5alpha1=0;
	stage5alpha2=0;
	stage5alpha3=0;
	stage5alpha4=0;
	stage5alpha5=0;
	ViewToX=0;
	ViewToY=0;
	ViewFromZ=5;
	ScrollSpeed=0;
	while(StageProgress==5 && Stage5Phase==2)
	{
			bgX+=3;
			bgY+=2;
			
			PillarPos+=5+ScrollSpeed;

			if(GetPlayerX<GetCenterX)
			{
				if(ViewFromY>ViewToWeit*(GetPlayerX-GetCenterX)/GetCenterX)
				{
					ViewFromY-=ViewToMove;
				}
				else
				{
					ViewFromY+=ViewToMove;
				}
			}
			else
			{
				if(ViewFromY<ViewToWeit*(GetPlayerX-GetCenterX)/GetCenterX)
				{
					ViewFromY+=ViewToMove;
				}
				else
				{
					ViewFromY-=ViewToMove;
				}
			}

			if(GetCommonDataDefault("Stage5MoveSignal6",false)==true)
			{
			if(stage5alphacloud<100)
			{
				stage5alphacloud+=100/120;
				stage5alpha1-=10/120;
				stage5alpha2-=1/120;
			}
			else
			{
				if(ViewFromZ<60)
				{
				ViewFromZ+=55/360;
				ViewFromX+=2000/360;
				}
			}
			}
			else
			{
			if(stage5alpha1<10)
			{
				stage5alpha1+=10/360;
			}
			if(stage5alpha2<1)
			{
				stage5alpha2+=1/360;
			}
			}
			
		//	ViewToX=5*(GetPlayerX-GetCenterX)/GetCenterX;
		//	ViewToZ=-5*(GetPlayerY-GetCenterY)/GetCenterY;
		//	if(absolute(bgX)>=600){bgX=0;}
		//	if(absolute(bgY)>=480){bgY=0;}
			if(absolute(bgX2)>=640){bgX2=0;}
			if(absolute(bgY2)>=480){bgY2=0;}
			if(absolute(bgY3)>=512){bgY3=0;}

		yield;
	}
	stage5alpha1=0;
	stage5alpha2=0;
	stage5alpha3=0;
	stage5alpha4=0;
	stage5alpha5=0;
	ViewFromX=2000;
	ViewFromZ=60;
	ViewToY=-60;
	let ViewToYSpeed=2.0;
	let ViewFromZSpeed=0.2;
	let ViewFromXSpeed=1.5;
	let ViewFromZReal=0;
	let ViewFromZAdjust=0;
	let HCount=0;
	let PillarShakeCount=0;
	if(StagePractice && StagePhaseProgress==3)
	{
		ViewFromX=500;
		ViewFromZ=20;
	}
	while(StageProgress==5 && Stage5Phase==3)
	{
			if(stage5alpha1<20)
			{
				stage5alpha1+=20/120;
				stage5alpha2+=30/120;
				stage5alpha3+=250/120;
				stage5alpha4+=250/120;
				stage5alpha5+=100/120;
			}
			if(stage5alphacloud>0)
			{
				stage5alphacloud-=100/120;
			}
			if(ViewFromX>1500)
			{
				ViewFromX-=500/180;
				ViewFromZ-=20/180;
			}
			else if(ViewFromX>500)
			{
				ViewFromX-=500/180;
				ViewFromZ-=10/180;
			}
			else if(ViewFromZSpeed>0 && GetCommonDataDefault("Stage5MoveSignal7",false)==true)
			{
				ViewFromX-=ViewFromXSpeed;
				ViewToY+=ViewToYSpeed;
				ViewToYSpeed-=2.0/180;
				ViewFromZ-=ViewFromZSpeed;
				ViewFromZSpeed-=0.2/180;
				ViewFromXSpeed-=1.5/180;
				BGAngle+=0.2;
				ViewFromZReal=ViewFromZ;
				if(stage5alpha2>1)
				{
					MagicCircleScale+=9/120;
					stage5alpha2-=250/120;
					stage5alpha6+=150/120;
				}
			}	
			else if(ViewFromZSpeed<=0)
			{
				BGAngle+=0.2;
				ViewFromZ=ViewFromZReal+ViewFromZAdjust;
			}
			else
			{
				if(stage5alpha2<250 && MagicCircleScale==3)
				{
					stage5alpha2+=220/120;
				}
				else
				{

				}
			}

			MagicCircleColor=HueDifine(HCount);
			HCount+=1;
			CircleAngle+=1;
			bgX+=3;
			bgY+=2;
			bgX2+=1;
			bgY2+=1;
			
			PillarPos+=5+ScrollSpeed;

			if(stage5phase==1)
			{
				PillarHeight[2]=PillarHeight[2]-4;
				if(PillarShakeCount==0)
				{
				PillarShake(2,20,320);
				PillarShakeCount=1;
				}
			}
			if(stage5phase==2)
			{
				PillarHeight[1]=PillarHeight[1]-4;
				if(PillarShakeCount==1)
				{
				PillarShake(1,20,320);
				PillarShakeCount=2;
				}
			}
			if(stage5phase==3)
			{
				PillarHeight[0]=PillarHeight[0]-4;
				if(PillarShakeCount==2)
				{
				PillarShake(0,20,320);
				PillarShakeCount=3;
				}
			}

task PillarShake(num,intensity,time)
{
	let frame=prand_int(0,4800);
	let intensityPlus=intensity/time;
	loop(time)
	{
		PillarShakeX[num]=sin(frame*60)*intensity*0.5;
		PillarShakeY[num]=sin(frame*90)*intensity;
		intensity-=intensityPlus/2;
		yield;
		frame++;
	}
}

			if(GetPlayerX<GetCenterX)
			{
				if(ViewFromY>ViewToWeit*(GetPlayerX-GetCenterX)/GetCenterX)
				{
					ViewFromY-=ViewToMove;
				}
				else
				{
					ViewFromY+=ViewToMove;
				}
			}
			else
			{
				if(ViewFromY<ViewToWeit*(GetPlayerX-GetCenterX)/GetCenterX)
				{
					ViewFromY+=ViewToMove;
				}
				else
				{
					ViewFromY-=ViewToMove;
				}
			}

			if(GetPlayerY<GetCenterY)
			{
				if(ViewToZ>-ViewToWeit*(GetPlayerY-GetCenterY)/GetCenterY)
				{
					ViewToZ-=ViewToMove;
				}
				else
				{
					ViewToZ+=ViewToMove;
				}
			}
			else
			{
				if(ViewToZ<-ViewToWeit*(GetPlayerY-GetCenterY)/GetCenterY)
				{
					ViewToZ+=ViewToMove;
				}
				else
				{
					ViewToZ-=ViewToMove;
				}
			}
			
			
		//	ViewToX=5*(GetPlayerX-GetCenterX)/GetCenterX;
		//	ViewToZ=-5*(GetPlayerY-GetCenterY)/GetCenterY;
		//	if(absolute(bgX)>=600){bgX=0;}
		//	if(absolute(bgY)>=480){bgY=0;}
		//	if(absolute(bgX2)>=640){bgX2=0;}
		//	if(absolute(bgY2)>=480){bgY2=0;}
			if(absolute(bgY3)>=512){bgY3=0;}

		yield;
	}
	ViewFromZ=5;
	while(StageProgress==5 && Stage5Phase==4)
	{///////しおり
			if(stage5alpha1<100)
			{
				stage5alpha1+=80/120;
			}
			if(stage5alpha2<255)
			{
				stage5alpha2+=80/120;
			}
			if(stage5alpha3<1)
			{
				stage5alpha3+=1/120;
			}
			bgX+=1;
			bgY+=25+ScrollSpeed;

			
		//	ViewToX=5*(GetPlayerX-GetCenterX)/GetCenterX;
		//	ViewToZ=-5*(GetPlayerY-GetCenterY)/GetCenterY;
		//	if(absolute(bgX)>=600){bgX=0;}
		//	if(absolute(bgY)>=480){bgY=0;}
			if(absolute(bgX2)>=640){bgX2=0;}
			if(absolute(bgY2)>=480){bgY2=0;}
			if(absolute(bgY3)>=512){bgY3=0;}

		yield;
	}

	while(StageProgress>=6)
	{
		if(stage6alpha1<=120)
		{
			stage6alpha1+=120/240;
		}
		if(stage6alpha2<=80)
		{
			stage6alpha2+=80/240;
		}
			bgX += 1;
			if (bgX >= 6000)
			{
				bgX = 0;
			}
			bgX2 += 1;
			if (bgX >= 6000)
			{
				bgX2 = 0;
			}
				bgrt += 1;
				if(bgrt > 512){bgrt = bgrt - 512;}
				if(bgrt <   0){bgrt = bgrt + 512;}
		yield;
	}
}
	task BGStage1MidBossEnd
	{
	while(stage1alpha3>0)
	{
		stage1alpha3-=40/2000;
		yield;
	}
	}

	task Stage1End
	{

	}

	task BGStage2MidBossEnd
	{

	}

	task Stage2Bgshift
	{
		loop(240)
		{
		stage2alpha2+=120/240;
		yield;
		}
	}

	task Stage2End
	{

	}


	task Stage3End
	{

	}

	task BGStage3MidBossEnd
	{

	}

	task Stage4End
	{

	}

	task BGStage4MidBossEnd
	{

	}

	task Stage1BGPhase1
	{
		ViewFromX=0;
		Stage1BossAdjust=20*0;
		loop(120)
		{
			if(stage1alpha1<=80)
			{
				stage1alpha1+=80/60;
			}
			if(stage1alpha2<=120)
			{
				stage1alpha2+=120/60;
			}
			if(stage1alpha3<=40)
			{
				stage1alpha3+=20/60;
			}
			if(stage1alpha4<200)
			{
				stage1alpha4+=200/60;
			}
			if(stage1alpha5<255)
			{
				stage1alpha5+=255/60;
			}
			yield;
		}
	}

	task Stage1BGPhase2
	{
		ViewFromX=0;
		Stage1BossAdjust=20*0;
		loop(120)
		{
			if(stage1alpha1<=80)
			{
				stage1alpha1+=80/60;
			}
			if(stage1alpha2<=120)
			{
				stage1alpha2+=120/60;
			}
			if(stage1alpha3<=40)
			{
				stage1alpha3+=20/60;
			}
			if(stage1alpha4<200)
			{
				stage1alpha4+=200/60;
			}
			if(stage1alpha5<255)
			{
				stage1alpha5+=255/60;
			}
			yield;
		}
	}

	task Stage1BGPhase3
	{
		ViewFromX=0;
		stage1alpha3=0;
		Stage1BossAdjust=20*0;
		loop(120)
		{
			if(stage1alpha1<=80)
			{
				stage1alpha1+=80/60;
			}
			if(stage1alpha2<=120)
			{
				stage1alpha2+=120/60;
			}
			if(stage1alpha4<200)
			{
				stage1alpha4+=200/60;
			}
			if(stage1alpha5<255)
			{
				stage1alpha5+=255/60;
			}
			yield;
		}
	}


	task Stage2BGPhase3
	{
		loop(120)
		{
			stage2alpha2+=1;
			yield;
		}
	}

	task Stage4BGPhase1
	{
		loop(120)
		{
			stage4alpha3+=255/120;
			yield;
		}
	}

	task Stage4BGPhase2
	{
		loop(120)
		{
			stage4alpha3+=255/120;
			yield;
		}
	}
	
	task Stage4BGPhase3
	{
		stage4bgfromz=0;
		stage4fogRGB[1]=205;
		stage4bg2scale=4;
		stage4alpha2=0;
		loop(120)
		{	stage4alpha1=0;
			stage4alpha3+=255/120;
			yield;
		}
		loop(120)
		{
			stage4alpha2+=50/120;
			yield;
		}
	}

		function St2MtSet
		{
			MountainAngle=MountainAngle~[rand(140,160)];
			MountainAngle=erase(MountainAngle,0);
			MountainHeight=MountainHeight~[rand(80,160)];
			MountainHeight=erase(MountainHeight,0);
			MountainX=MountainX~[rand(100,200)];
			MountainX=erase(MountainX,0);
			MountainY=MountainY~[rand(-50,50)];
			MountainY=erase(MountainY,0);

			MountainAngle2=MountainAngle2~[rand(140,160)];
			MountainAngle2=erase(MountainAngle2,0);
			MountainHeight2=MountainHeight2~[rand(80,160)];
			MountainHeight2=erase(MountainHeight2,0);
			MountainX2=MountainX2~[rand(-200,-100)];
			MountainX2=erase(MountainX2,0);
			MountainY2=MountainY2~[rand(-50,50)];
			MountainY2=erase(MountainY2,0);

			Stage2BGCount-=150;
		}

function IniMtSet
{
ascent(i in 0..20)
{
St3MountainAngle=St3MountainAngle~[rand(140,160)];
St3MountainHeight=St3MountainHeight~[rand(MtHeightMin,MtHeightMid)];
St3MountainX=St3MountainX~[rand(MtXMin,210)*St3XDir];
St3MountainY=St3MountainY~[rand(0,0)];

St3MountainAngle2=St3MountainAngle2~[rand(130,150)];
St3MountainHeight2=St3MountainHeight2~[rand(MtHeightMid,MtHeightMax)];
St3MountainX2=St3MountainX2~[rand(MtXMin+80,300)*St3XDir];
St3MountainY2=St3MountainY2~[rand(0,0)];

St3XDir*=-1;
}
}

		function St3MtSet
		{
			St3MountainAngle=St3MountainAngle~[rand(130,150)];
			St3MountainAngle=erase(St3MountainAngle,0);
			St3MountainHeight=St3MountainHeight~[rand(MtHeightMin,MtHeightMid)];
			St3MountainHeight=erase(St3MountainHeight,0);
			St3MountainX=St3MountainX~[rand(MtXMin,210)*(rand_int(0,1)*2-1)+St3DrawCenterX];
			St3MountainX=erase(St3MountainX,0);
			St3MountainY=St3MountainY~[rand(-50,50)];
			St3MountainY=erase(St3MountainY,0);

			St3MountainAngle2=St3MountainAngle2~[rand(130,150)];
			St3MountainAngle2=erase(St3MountainAngle2,0);
			St3MountainHeight2=St3MountainHeight2~[rand(MtHeightMid,MtHeightMax)];
			St3MountainHeight2=erase(St3MountainHeight2,0);
			St3MountainX2=St3MountainX2~[rand(MtXMin+80,300)*St3XDir+St3DrawCenterX];
			St3MountainX2=erase(St3MountainX2,0);
			St3MountainY2=St3MountainY2~[rand(-50,50)];
			St3MountainY2=erase(St3MountainY2,0);

			St3XDir*=-1;
			Stage3BGCount-=75;
		}

	function Stage5MagicSet
	{
		let Radius=rand(5,14);
		let Angle=rand(0,360);

		MagicDustAngle=MagicDustAngle~[Angle];
		MagicDustZ=MagicDustZ~[Radius*cos(Angle)];
		MagicDustX=MagicDustX~[Radius*sin(Angle)];
		MagicDustSize=MagicDustSize~[rand(0.15,0.25)];
		MagicDustY=MagicDustY~[rand(-5,5)];

		MagicDustAngle=erase(MagicDustAngle,0);
		MagicDustZ=erase(MagicDustZ,0);
		MagicDustX=erase(MagicDustX,0);
		MagicDustSize=erase(MagicDustSize,0);
		MagicDustY=erase(MagicDustY,0);
	}

function Stage2BGInitialize
{
ascent(i in 0..8)
{
MountainAngle=MountainAngle~[rand(130,160)];
MountainHeight=MountainHeight~[rand(80,160)];
MountainX=MountainX~[rand(100,200)];
MountainY=MountainY~[rand(-50,50)];

MountainAngle2=MountainAngle2~[rand(140,160)];
MountainHeight2=MountainHeight2~[rand(80,160)];
MountainX2=MountainX2~[rand(-100,-200)];
MountainY2=MountainY2~[rand(-50,50)];
}
}

function Stage5BGInitialize
{
	ascent(i in 0..40)
	{
		let Radius=rand(5,14);
		let Angle=rand(0,360);

		MagicDustAngle=MagicDustAngle~[Angle];
		MagicDustZ=MagicDustZ~[Radius*cos(Angle)];
		MagicDustX=MagicDustX~[Radius*sin(Angle)];
		MagicDustSize=MagicDustSize~[rand(0.15,0.25)];
		MagicDustY=MagicDustY~[rand(-5,5)];

	}
}