 let s3bg1=GPSD~"img\labpath.png";
 let s3bg2=GPSD~"img\capsule.png";
 let s3bg3=GPSD~"img\capsule2.png";
 let s3bg4=GPSD~"img\gradient.png";
 let s3bg5=GPSD~"img\bubbles.png";
 let s3bg6=GPSD~"img\stars2.png";
 let s3bg7=GPSD~"img\holoscreen.png";
 let s3bg8=GPSD~"img\void1.png";
 let s3bg9=GPSD~"img\void2.png";
 let s3bg10=GPSD~"img\void3.png";
 let s3bg11=GPSD~"img\marble.png";
 let s3bg12=GPSD~"img\noise.png";
 let s3bg13=GPSD~"img\dust.png";
 let s3title=GPSD~"img\stage3.png";

 let si=0;

// Type, X, Y, V, Angle, a1, a2, a3, a4
//
 let s3crossfade=0;
 function CompileEnemiesS3(delay){
	CreateBoss(GetCenterX,-100,6,"s3",["_last"],[[0],[1],[1]],diff,1, 0);
	Wait(delay);
	CreateBoss(GetCenterX+200,-100,5,"s3",["n_1","s_1","n_2","s_2","n_3","s_3","s_4","n_4","s_5"],[[1,1,1,1,1,0,1,1,1],[1,1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1,1]],diff,[8,9,9][diff], 0);
	Wait(delay);
	CreateBoss(GetCenterX+300,GetCenterY,4,"s3",["_1","_2","_3"],[[1,1,1],[1,1,1],[1,1,1]],diff,3, 0);
	Wait(delay);
 }

 task stage3(fullgame){
	SetSeenStage(GetPlayer,2,diff);

	if(IsAWCPlayer&&fullgame==0&&!IsReplay){
		IncTimesPlayed(GetPlayer,3,diff);
	}
	LoadDialoguePics(["yumemi","yukari"]);
	s3bgangle=-20;
	s3bgheight=10;	//side view
	s3bgdist=220;
	s3bgspeed=1;

	s3overlay1=[255,0,255];
	s3overlay2=[0,0,0];

	let si=0;
	s3currentbg=0;

	S3Title(150,100,100,100);

	if(fullgame==0){
		s3crossfade=0;
		s3voidinit=0;
		VoidInit(600);
	}else{
		s3crossfade=1;
		s3voidinit=0;
		VoidInitFull;
	}

	s3overlay1=[0,0,255];
	s3overlay2=[0,255,155];

	SetShotAutoDeleteClip(10,10,10,10);

	Wait(175);
	Music(5);
	Wait(60);

	SetEnemyInvincibility(120);
	E31(0,GetCenterX,170,130,90,  [BLUE02,3,10],[BLUE11,1.5,3,10,5],56,300,  [E_BLUE,128,2,1,0]);
	Wait(50);
	E31(0,GetCenterX-80,120,130,90,  [BLUE02,3,15],[BLUE11,1.5,3,10,7],56,200,  [E_BLUE,128,2,1,0]);
	E31(0,GetCenterX+80,120,130,90,  [BLUE02,3,15],[BLUE11,1.5,3,10,7],56,200,  [E_BLUE,128,2,1,0]);
	Wait(120);
	SetEnemyInvincibility(0);
	si=-2;
	loop(3){
		E31(0,GetCenterX-150,GetCenterY+si*80,90,0,   [YELLOW02,2,10],[YELLOW11,1.5,3,5,1],32,200,  [E_YELLOW,172,3,1,1]);
		E31(0,GetCenterX+150,GetCenterY+si*80,90,180, [YELLOW02,2,10],[YELLOW11,1.5,3,5,1],32,200,  [E_YELLOW,172,3,1,1]);

		si++;
		Wait(50);
	}

	Wait(70);

	si=-90;
	loop(10){
		E31(0,GetCenterX+cos(si)*90,150+sin(si)*60,90,90,  [0,0,0],[0,0,0,0,0],56,200,  [0,0,0,0,0]);
		si+=36;
		Wait(2);
	}

	SetEnemyInvincibility(200);
	E31(1,GetCenterX,150,90,90,  [PURPLE04,2,60,5],[PURPLE21,3,-5,5,180],30,240,  [E_PURPLE,255,1,1,1]);
	E31(5,GetCenterX-40,150,90,90,  [GREEN02,4,80,10],[BLUE04,2,60,180],7,240,  [E_BLUE,255,2,1,0]);
	E31(5,GetCenterX+40,150,90,90,  [GREEN02,4,80,10],[BLUE04,2,60,180],7,240,  [E_BLUE,255,2,1,0]);

	Wait(160);

	si=-2;
	loop(5){
		E31(1,GetCenterX+si*70,100,90,90,  [PURPLE04,2,60,5],[PURPLE21,3,0,3,60],90,240,  [E_PURPLE,128,1,1,1]);
		Wait(18);
		si++;
	}

	Wait(100);







	S3BGChange(0,0.75,240);
//	S3BGChange(0,1,240);
	S3LabInit(240);


//lab

	Wait(400);
	SetEnemyInvincibility(25);
//	E31(0,GetCenterX-150,GetCenterY+si*80,90,0,   [YELLOW02,2,10],[YELLOW11,1.5,3,5,1],32,200,  [E_YELLOW,172,3,1,1]);

	loop(5){
		E32(0,GetCenterX+150,-10,0,0, [[3],[60,100,0],[4,4,4],[90,225,70],[1,40,60]],1,[RED52,0,2,120,10],[18,7], [E_RED,160,2,0]);
		Wait(20);
	}
	Wait(60);
	loop(6){
		E32(0,GetCenterX-120,-10,0,0, [[2],[60,0],[4,4,4],[90,-550],[1,220]],2,[WHITE04,-2,2,120,7],[15,8], [E_BLUE,160,2,0]);
		Wait(22);
	}
	Wait(80);
	loop(5){
		E32(1,GetCenterX-160,-10,0,0, [[2],[60,0],[4,4,4],[90,0],  [1,60]],1,[RED52,RED21,1,2,180,80],[6,15], [E_RED,160,2,0]);
		E32(1,GetCenterX+160,-10,0,0, [[2],[60,0],[4,4,4],[90,180],[1,60]],1,[RED52,RED21,1,2,180,80],[6,15], [E_RED,160,2,0]);
		Wait(20);
	}
	Wait(100);
	si=0;
	SetEnemyInvincibility(20);

	loop(3){
		loop(4){
			E32(3,GetCenterX-230,70+si,0,0, [[1],[0],[2.5],[0],[1]],0.7,[RED02,-2,6,-1,1,200],[40,80], [E_RED,160,2,0]);
			Wait(10);
		}
		si+=35;
		loop(4){
			E32(3,GetCenterX+230,70+si,0,0, [[1],[0],[2.5],[180],[1]],0.7,[RED02,-2,6,-1,1,200],[40,80], [E_RED,160,2,0]);
			Wait(10);
		}
		si+=35;
	}

	S3BGChange(0.75,1,240);

	Wait(200);
	SetEnemyInvincibility(300);
	E31(6,GetCenterX,GetCenterY-70,90,90,   0,0,0,0,  [0,0,0,6,1]);	//death fairy
	

	SetEnemyInvincibility(60);
	Wait(400);
	si=0;
	loop(9){
		E31(0,GetCenterX+cos(si)*100,150+sin(si)*100,90,90,  [BLUE12,3,10],[0,0,0,0,0],56,200,  [E_AQUA,70,2,1,0]);
		si-=180/8;
		Wait(10);
	}

	Wait(60);
	loop(6){
		E32(0,GetCenterX-120,-10,0,0, [[2],[60,0],[3,3,3],[90,-550],[1,220]],1,[RED12,0,2,120,4],[30,8], [E_RED,160,2,0]);
		E32(0,GetCenterX+120,-10,0,0, [[2],[60,0],[3,3,3],[90,550-180],[1,220]],1,[RED12,0,2,120,4],[30,8], [E_RED,160,2,0]);
		Wait(22);
	}
	Wait(60);
	si=0;
	let k=0;
	SetEnemyInvincibility(120);
	loop(5){
		k=-si/2;
		loop(si+1){
			E32(2,GetCenterX+k*50,-10,0,0, [[1],[0],[2],[90],[1]],0.9,[YELLOW01,4,2,0.5,15,rand(0,360)],40, [E_YELLOW,160,3,0]);	//triangle formation
			k++;
		}
		si++;
		Wait(30);
	}
	SetEnemyInvincibility(60);
	Wait(90);
	loop(5){
		E32(0,GetCenterX+150,-10,0,0, [[3],[60,100,0],[3,3,3],[90,225,70],[1,40,60]],1,[BLUE52,1,1,120,9],[34,5], [E_BLUE,160,2,0]);
		E32(0,GetCenterX-150,-10,0,0, [[3],[60,100,0],[3,3,3],[90,-45,110],[1,40,60]],1,[BLUE52,1,1,120,9],[34,5], [E_BLUE,160,2,0]);
		Wait(20);
	}

	Wait(180);
	loop(3){
		loop(3){
			if(GetPlayerY>GetCenterY-100){
				E32(1,GetPlayerX+100,GetClipMaxY+50,0,0, [[3],[25,85,0],[7,1,3],[-90,-90,-90-(180+rand(-30,30))],[1,60,60]],1, [GREEN52,GREEN21,0.5,1.5,120,60],[rand_int(9,12),8], [E_GREEN,160,2,0]);		//to player's sides
				E32(1,GetPlayerX-100,GetClipMaxY+50,0,0, [[3],[25,85,0],[7,1,3],[-90,-90,-90+(180+rand(-30,30))],[1,60,60]],1, [GREEN52,GREEN21,0.5,1.5,120,60],[rand_int(9,12),8], [E_GREEN,160,2,0]);		//to player's sides
			}else{
				E32(1,GetPlayerX+100,GetClipMinY-50,0,0, [[3],[25,85,0],[7,1,3],[90,90,90-(180+rand(-30,30))],[1,60,60]],1, [GREEN52,GREEN21,0.5,2,120,120],[rand_int(9,12),8], [E_GREEN,160,2,0]);		//to player's sides
				E32(1,GetPlayerX-100,GetClipMinY-50,0,0, [[3],[25,85,0],[7,1,3],[90,90,90+(180+rand(-30,30))],[1,60,60]],1, [GREEN52,GREEN21,0.5,2,120,120],[rand_int(9,12),8], [E_GREEN,160,2,0]);		//to player's sides
			}
			Wait(10);
			if(GetPlayerY>GetCenterY-100){
				E32(2,GetPlayerX+100,GetClipMaxY+50,0,0, [[3],[25,85,0],[7,1,3],[-90,-90,-90-(180+rand(-30,30))],[1,60,60]],1, [YELLOW26,3,5,0.5,10,rand(0,360)],1, [E_YELLOW,160,2,0]);		//to player's sides
				E32(2,GetPlayerX-100,GetClipMaxY+50,0,0, [[3],[25,85,0],[7,1,3],[-90,-90,-90+(180+rand(-30,30))],[1,60,60]],1, [YELLOW26,3,5,0.5,10,rand(0,360)],1, [E_YELLOW,160,2,0]);		//to player's sides
			}else{
				E32(2,GetPlayerX+100,GetClipMinY-50,0,0, [[3],[25,85,0],[7,1,3],[90,90,90-(180+rand(-30,30))],[1,60,60]],1, [YELLOW26,3,5,0.5,10,rand(0,360)],1, [E_YELLOW,160,2,0]);		//to player's sides
				E32(2,GetPlayerX-100,GetClipMinY-50,0,0, [[3],[25,85,0],[7,1,3],[90,90,90+(180+rand(-30,30))],[1,60,60]],1, [YELLOW26,3,5,0.5,10,rand(0,360)],1, [E_YELLOW,160,2,0]);		//to player's sides
			}
			Wait(10);
		}
		Wait(110);
	}

	Wait(180);
	SetEnemyInvincibility(240);
	E31(4,GetCenterX,170,130,90,  [BLUE23,3,90,3],[120,110,100,90,80,70,60],4,120,  [E_AQUA-20,200,2,1,2]);
	Wait(130);
	E31(4,GetCenterX-90,220,130,90,  [BLUE23,3,70,3],[120,110,100,90,80,70,60],5,120,  [E_AQUA-20,200,2,1,2]);	//Cirno fairies
	E31(4,GetCenterX+90,220,130,90,  [BLUE23,3,110,3],[120,110,100,90,80,70,60],5,120,  [E_AQUA-20,200,2,1,2]);
	Wait(150);



	SetEnemyInvincibility(180);

	E31(4,GetCenterX-120,180,130,90,  [BLUE23,2,60,2],[60,70,80,90,100,110,120],7,120,  [E_AQUA-20,200,2,1,2]);	//Cirno fairies
	E31(4,GetCenterX+120,180,130,90,  [BLUE23,2,120,2],[60,70,80,90,100,110,120],7,120,  [E_AQUA-20,200,2,1,2]);
	Wait(25);

	E31(4,GetCenterX-30,50,130,90,  [BLUE23,2.5,60,2],[60,70,80,90,100,110,120],7,120,  [E_AQUA-20,200,2,1,2]);	//Cirno fairies
	E31(4,GetCenterX+30,50,130,90,  [BLUE23,2.5,120,2],[60,70,80,90,100,110,120],7,120,  [E_AQUA-20,200,2,1,2]);
	Wait(25);

	E31(4,GetCenterX,170,130,90,  [BLUE23,3,90,5],[100,90,80,70,60],4,120,  [E_AQUA-20,200,2,1,2]);

	si=0;
	Wait(300);
	loop(6){
		E32(0,GetCenterX-180,-10,0,0, [[2],[110-si*20,0],[3,3,3],[90,90-90],[1,60]],1,[BLUE56,-1,1.2,60,20],[16,(110-si*20)/16], [E_BLUE,160,2,0]);
		E32(0,GetCenterX+180,-10,0,0, [[2],[110-si*20,0],[3,3,3],[90,90+90],[1,60]],1,[BLUE56,-1,1.2,60,20],[16,(110-si*20)/16], [E_BLUE,160,2,0]);
		si++;
		Wait(18);
	}

	Wait(220);
	//appearing bullets

/*
	E33(2,GetCenterX,100,90,90, [WHITE12,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//center

	Wait(70);
	si=0;
	loop(6){
		E33(2,GetCenterX-150,50+si,90,90, [WHITE12,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//left side
		si+=70;	Wait(10);
	}
	loop(6){
		si-=70;
		E33(2,GetCenterX+150,50+si,90,90, [WHITE12,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//right side
		Wait(10);
	}

	Wait(40);
	si=-4;
	loop(9){
		E33(2,GetCenterX+si*40,GetClipMaxY-50,rand(150,170),90, [WHITE12,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//bottom
		si++;	Wait(1);
	}
	Wait(10);
	si=4;
	loop(9){
		E33(2,GetCenterX+si*40,GetClipMinY+50,rand(150,170),90, [WHITE12,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//top
		si--;	Wait(1);
	}
	Wait(60);
*/

	E33(0,GetCenterX,130,150,90, [0,GREEN21,-4,2,3,60,50],0,0, [0,0.1,0.5,30], [E_GREEN,100,0,0,0]);	//explosion center
	Wait(110);
	E33(0,GetCenterX-90,80,90,90, [0,ORANGE21,-4,2,3,60,50],0,0, [0,0.1,0.5,30], [E_ORANGE,100,0,0,0]);	//explosion left
	E33(0,GetCenterX+90,80,90,90, [0,ORANGE21,-4,2,3,60,50],0,0, [0,0.1,0.5,30], [E_ORANGE,100,0,0,0]);	//explosion right
	Wait(90);

	E33(0,GetCenterX-110,120,90,90, [0,RED21,-4,2,3,60,30],0,0, [0,0.1,0.5,30], [E_RED,100,0,0,0]);	//explosion left
	E33(0,GetCenterX+110,120,90,90, [0,RED21,-4,2,3,60,30],0,0, [0,0.1,0.5,30], [E_RED,100,0,0,0]);	//explosion right
	E33(0,GetCenterX-60,50,90,90, [0,RED21,-4,2,3,60,30],0,0, [0,0.1,0.5,30], [E_RED,100,0,0,0]);	//explosion left
	E33(0,GetCenterX+60,50,90,90, [0,RED21,-4,2,3,60,30],0,0, [0,0.1,0.5,30], [E_RED,100,0,0,0]);	//explosion right

	Wait(180);

	loop(20){
		E33(0,GetCenterX+rand(-120,120),rand(0,150),90,90, [1,SP09,5,1.5,3,30,20],0,0, [0,0.1,0.5,30], [E_RED,100,0,0,0]);	//explosion random
		Wait(13);
	}


	Wait(350);
	ClearBullets(170,180);
	Wait(180);


	Dialogue(3,0);
	while(EventStep!=1&&EventActive){
		yield;
	}
	si=0;
	SpawnBoss;
	WaitForBossDead;
	DeleteDialoguePics(["yumemi"]);

	S3BGChange(0,0,10);

	Wait(60);
	SetCommonData("SpellTextOpacity",0);
	SetShotAutoDeleteClip(64,64,64,64);


	Wait(100);

//	S3BGChange(0.75,0,240);
//	Wait(300);
	S3BGChange(0,-0.75,240);
	S3StairInit(300);

	Wait(300);

	SetEnemyInvincibility(60+130);

	E31(7,GetCenterX-190,GetCenterY-250, 130,90, [3,40], [GREEN02,4,6,1,50],120,[30,5],  [E_GREEN,200,1,1,2]);	//aimed spray
	E31(7,GetCenterX+190,GetCenterY-250, 130,90, [3,140],[RED02  ,4,6,1,50],120,[30,5],  [E_RED,200,1,1,2]);	//aimed spray

	SetEnemyInvincibility(0);
	si=0;
	loop(4){
		E34(0,GetCenterX-200,100+si*15,4,-20-si*3, [0.3,50,30,20], [RED02,2,20,180],[RED01,2,80,45],[RED11,2,60,90, 4],  [E_RED,200,3,1,0]);	//bullet tail
		E34(0,GetCenterX+200,130+si*15,4,180+si*4, [0.3,50,30,20], [PURPLE02,2,20,180],[PURPLE01,2,80,45],[PURPLE11,2,60,90, 4],  [E_PURPLE,200,3,1,0]);	//bullet tail
		si++;
		Wait(50);
	}
	SetEnemyInvincibility(60+130);
	
	E31(7,GetCenterX-190,GetCenterY-50, 130,90, [2,-60],[GREEN02,3,4,3,50],120,[20,5],  [E_GREEN,200,1,1,2]);	//aimed spray
	E31(7,GetCenterX+190,GetCenterY-50, 130,90, [2,-120],[RED02  ,3,4,3,50],120,[20,5],  [E_RED,200,1,1,2]);	//aimed spray

	Wait(200);
	E31(7,GetCenterX-150,GetCenterY-190, 130,90, [2,20],[RED02,6,7,4,30],120,[60,5],  [E_RED,200,1,1,2]);	//aimed spray
	E31(7,GetCenterX+150,GetCenterY-190, 130,90, [2,160],[GREEN02,6,7,3,30],120,[60,5],  [E_GREEN,200,1,1,2]);	//aimed spray

	SetEnemyInvincibility(120+130);
//	Wait(200);
//	E31(7,GetCenterX,0, 130,90, [1.5,90],[BLUE02,6,7,9,15],120,[60,5],  [E_BLUE,200,1,1,2]);	//aimed spray

	S3BGChange(-0.75,-1,120);

	Wait(200);
	E34(2,GetCenterX-140,0,6,80, 60,[BLUE12,2.5,3],[5,5.4],[2,60],   [E_BLUE,200,2,1,0]);	//spiral

	SetEnemyInvincibility(70);
	Wait(90);
	E34(2,GetCenterX+140,0,6,150, 80,[AQUA12,2.5,3],[4,5.4],[3,40],   [E_AQUA,200,2,1,0]);	//spiral
	Wait(30);
	E34(2,GetCenterX+200,40,6,110, 80,[AQUA12,2.5,3],[4,5.4],[3,40],   [E_AQUA,200,2,1,0]);	//spiral
	Wait(80);

	Wait(100);
	SetEnemyInvincibility(180);
	E34(2,GetCenterX,0,6,90, 60,[WHITE11,2,2.5],[4,2.4],[1,200],   [0,0,2,1,1]);	//spiral

	Wait(200);
	SetEnemyInvincibility(90);
	E34(2,GetCenterX,0,6,90+30, 30,[RED11,2,2.5],[4,2.4],[2,100],   [E_RED,100,2,1,1]);	//spiral
	E34(2,GetCenterX,0,6,90-30, 30,[RED11,2,2.5],[4,-2.4],[2,100],   [E_RED,100,2,1,1]);	//spiral

	Wait(170);
	S3OverlayChange([0,0,255],[255,255,255],50,120);
	S3Angle(-20,20,120);	//switch to other side

	Wait(50);
	E34(3,GetCenterX,0,3,90, [60,120],[WHITE01,10,90,120],[ORANGE21,2,1,100,10],50,  [E_ORANGE,200,3,2,0]);		//physics fairy
	Wait(140);
	loop(2){
		E35(0,GetCenterX-100,0,4,30,   [60,30,4,4], [PURPLE01,1,6,60,30],[PURPLE21,1.5,60,35],0, [E_PURPLE,100,3,1]);	//aimed trail
		Wait(10);
		E35(0,GetCenterX+100,0,4,150,  [60,30,-4,4],[PURPLE01,1,6,60,30],[PURPLE21,1.5,60,35],0, [E_PURPLE,100,3,1]);	//aimed trail
		Wait(10);
	}
	E34(3,GetCenterX,0,3,90+60, [90,120],[WHITE01,10,90+10,120],[YELLOW21,2,1,100,8],50,  [E_YELLOW,200,3,2,0]);		//physics fairy
	E34(3,GetCenterX,0,3,90-60, [90,120],[WHITE01,10,90-10,120],[YELLOW21,2,1,100,8],50,  [E_YELLOW,200,3,2,0]);		//physics fairy
	Wait(350);
	SetEnemyInvincibility(20);
	si=0;
	loop(3){
		E34(0,GetCenterX+140,0,3,90+(40+10*si), [0.3,50,30,20], [AQUA02,2,40,180],[AQUA01,2,80,90],[AQUA11,2,60,35, 3],  [E_AQUA,200,2,1,0]);	//bullet tail
		E34(0,GetCenterX-140,0,3,90-(40+10*si), [-0.3,50,30,20], [AQUA02,2,40,180],[AQUA01,2,80,90],[AQUA11,2,60,35, 3],  [E_AQUA,200,2,1,0]);	//bullet tail
		Wait(50);
		si++;
	}
	Wait(220);
	SetEnemyInvincibility(300);
	E35(2,GetCenterX-199,0,8,30, [60,8,120],[SP04,2,8,70],[8,13,3,-22.5],1000, [E_PURPLE,100,0,6]);	//glowy death diamonds
	Wait(40);
	SetEnemyInvincibility(20);
	si=0;
	loop(3){
		E34(0,GetCenterX+140,0,3,90+(40+10*si), [0.3,50,30,20], [ORANGE02,2,40,180],[ORANGE01,2,80,90],[ORANGE11,2,60,35, 3],  [E_ORANGE,200,2,1,0]);	//bullet tail
		E34(0,GetCenterX-140,0,3,90-(40+10*si), [-0.3,50,30,20], [ORANGE02,2,40,180],[ORANGE01,2,80,90],[ORANGE11,2,60,35, 3],  [E_ORANGE,200,2,1,0]);	//bullet tail
		Wait(50);
		si++;
	}
	S3Speed(0.5,1.5,500);
	Wait(300);


	S3Height(10,20,240);
	S3Angle(20,-5,120);	//switch to other side
	S3OverlayChange([255,0,255],[255,0,0],50,300);

	Wait(200);
	SetEnemyInvincibility(60);
	si=0;
	loop(3){
		E35(0,GetCenterX-100,0,2,100, [120,80,2,2],[AQUA01,3,6,60,10],[AQUA52,3-si*0.15,60,3],0, [E_AQUA,100,2,1]);	//aimed trail
		Wait(50);
		si++;
		E35(0,GetCenterX+100,0,2,80, [120,80,-2,2],[AQUA01,3,6,60,10],[AQUA52,3-si*0.15,60,3],0, [E_AQUA,100,2,1]);	//aimed trail
		Wait(50);
		si++;
	}
	Wait(60);

	si=1;
	loop(2){
		E35(0,GetCenterX+si*40,0,2-si*0.3,90, [120,80,-2,2],[GREEN01,1,6,60,10],[GREEN21,3,60,15],0, [E_GREEN,100,1,1]);	//aimed trail
		E35(0,GetCenterX-si*40,0,2-si*0.3,90, [120,80,2,2],[GREEN01,1,6,60,10],[GREEN21,3,60,15],0, [E_GREEN,100,1,1]);	//aimed trail
		si++;
		Wait(42);
	}

	Wait(140);
	si=0;
	loop(24){
		E35(0,GetCenterX+rand(-180,180),0,rand(1,3),90+rand(-30,30), [60,80,-3*(rand_int(0,1)*2-1),5],[WHITE01,-1,-3,20,90],[WHITE52,si%3+1.5,60,15],0, [0,0,3,1]);	//aimed trail
		Wait(7);
		si++;
	}

	Wait(200);

	si=1;
	if(GetPlayerX>GetCenterX){si=-1;}

	E34(3,GetCenterX-100*si,0,6,90-60*si, [60,120],[WHITE01,10,90-50*si,120],[RED21,1.5,0.5,100,10],50,  [E_RED,150,2,1,0]);		//physics fairy
	E34(3,GetCenterX-140*si,0,6,90-30*si, [60,120],[WHITE01,10,90-70*si,120],[ORANGE21,1.5,0.5,100,10],50,  [E_ORANGE,150,2,1,0]);		//physics fairy
	E34(3,GetCenterX-50*si,0,6,90, [60,120],[WHITE01,10,90-20*si,120],[YELLOW21,1.5,0.5,100,10],50,  [E_YELLOW,150,2,1,0]);		//physics fairy

	Wait(360);

	E34(3,GetCenterX+20*si,0,6,90-60*si, [60,120],[WHITE01,13,90+70*si,120],[YELLOW21,1.5,0.5,100,10],50,  [E_YELLOW,150,2,1,0]);		//physics fairy
	E34(3,GetCenterX-140*si,0,6,90-30*si, [60,120],[WHITE01,13,90+10*si,120],[AQUA21,1.5,0.5,100,10],50,  [E_AQUA,150,2,1,0]);		//physics fairy
	E34(3,GetCenterX+70*si,0,6,90, [60,120],[WHITE01,13,90+50*si,120],[GREEN21,1.5,0.5,100,10],50,  [E_GREEN,150,2,1,0]);		//physics fairy

	Wait(360);
	let siadd=-si;
	si*=3;
	let color=0;
	loop(7){
		E34(3,GetCenterX+70*si,0,rand(2,3),90, [60,120],[WHITE01,20,90,120],[RED21+color,0.7,0.3,100,-8],30,  [color*(360/7),150,3,1,0]);		//physics fairy
		Wait(30);
		si+=siadd;
		color++;
	}

	S3Angle(-5,0,300);	//switch to other side
	S3Distance(220,280,120);


	Wait(200);

	E31(7,GetCenterX-190,GetCenterY-50, 130,90, [2,-60], [BLUE02,2,3,3,50],120,[20,5],  [E_BLUE,200,3,1,2]);	//aimed spray
	E31(7,GetCenterX+190,GetCenterY-50, 130,90, [2,-120],[AQUA02,2,3,3,50],120,[20,5],  [E_AQUA,200,3,1,2]);	//aimed spray

	Wait(160);

	si=0;
	loop(3){
		E35(1,GetCenterX-200,50+si*20,4,10, [50,90,35],[[3,5],[2,3],[2,3],[1,2]],[RED02,RED02,RED12,RED11],0, [E_RED,100,1,1]);	//popcorn
		Wait(50);
		E35(1,GetCenterX+200,50+si*20,4,170, [50,90,35],[[3,5],[2,3],[2,3],[1,2]],[GREEN02,GREEN02,GREEN12,GREEN11],0, [E_GREEN,100,1,1]);	//popcorn
		Wait(50);
		si++;
	}

	S3Speed(1.5,2.5,950);
	S3OverlayChange([0,0,200],[200,0,200],60,1400);

	Wait(310);
	E33(2,GetCenterX-180,60,180,90, [RED11,2,3,5,20,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+180,60,180,90, [RED11,2,3,5,20,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX,   160,180,90,    [RED11,2,3,5,20,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom

	Wait(80);
	E33(2,GetCenterX-200,400,180,90,    [ORANGE11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+200,400,180,90,    [ORANGE11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX    ,100,180,90,    [ORANGE11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom

	Wait(90);
	E33(2,GetCenterX-170,GetCenterY-200,180,90,    [YELLOW11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+170,GetCenterY-200,180,90,    [YELLOW11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX-170,GetCenterY+200,180,90,    [YELLOW11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+170,GetCenterY+200,180,90,    [YELLOW11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom

	Wait(100);
	E33(2,GetCenterX    ,GetCenterY-200,180,90,    [GREEN11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX    ,GetCenterY+200,180,90,    [GREEN11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX-170,GetCenterY    ,180,90,    [GREEN11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+170,GetCenterY    ,180,90,    [GREEN11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom

	Wait(110);
	E33(2,GetCenterX-170,GetCenterY-130,180,90,    [AQUA11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+170,GetCenterY-130,180,90,    [AQUA11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX-110,GetCenterY-200,180,90,    [AQUA11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+110,GetCenterY-200,180,90,    [AQUA11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX-170,GetCenterY+130,180,90,    [AQUA11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+170,GetCenterY+130,180,90,    [AQUA11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX-110,GetCenterY+200,180,90,    [AQUA11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+110,GetCenterY+200,180,90,    [AQUA11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom

	Wait(120);
	E33(2,GetCenterX-90,GetCenterY-90,180,90,    [BLUE11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+90,GetCenterY-90,180,90,    [BLUE11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX-90,GetCenterY+90,180,90,    [BLUE11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+90,GetCenterY+90,180,90,    [BLUE11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	Wait(5);
	E33(2,GetCenterX-190,GetCenterY  ,180,90,    [BLUE11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+190,GetCenterY  ,180,90,    [BLUE11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom


	Wait(140);
	E33(2,GetCenterX    ,GetCenterY    ,180,90,    [PURPLE11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	Wait(10);
	E33(2,GetCenterX    ,GetCenterY-100,180,90,    [PURPLE11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX    ,GetCenterY+100,180,90,    [PURPLE11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX-85 ,GetCenterY    ,180,90,    [PURPLE11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+85 ,GetCenterY    ,180,90,    [PURPLE11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	Wait(10);
	E33(2,GetCenterX    ,GetCenterY-200,180,90,    [PURPLE11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX    ,GetCenterY+200,180,90,    [PURPLE11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX-170,GetCenterY    ,180,90,    [PURPLE11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+170,GetCenterY    ,180,90,    [PURPLE11,2,3,5,10,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	Wait(10);
	E33(2,GetCenterX-170,GetCenterY-200,180,90,    [PURPLE11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+170,GetCenterY-200,180,90,    [PURPLE11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX-170,GetCenterY+200,180,90,    [PURPLE11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom
	E33(2,GetCenterX+170,GetCenterY+200,180,90,    [PURPLE11,2,3,5,15,1],0,0, [0,0.1,0.5,30], [0,0,0,0,0]);	//boom




//	E34(0,GetCenterX-140,0,4,50, [0.3,50,30,20], [RED02,2,40,180],[RED01,2,80,90],[RED11,2,60,5, 5],  [E_RED,200,1,1,0]);	//bullet tail
//	E34(1,GetCenterX-140,0,3,90, -0.2,[RED21,3,93,3],[20,30,120, 30,16],[2,80],  [E_RED,200,1,1,0]);	//doom tube

//	E34(3,GetCenterX-140,0,3,90, [60,120],[WHITE01,10,60,120],[BLUE21,2,1,100,10],50,  [E_BLUE,200,1,1,0]);		//physics fairy


//	E35(0,GetCenterX-100,0,4,100, [60,30,4,2],[RED01,1,6,60,30],[RED21,3,60,15],0, [E_RED,100,1,1]);	//aimed trail


//	E35(1,GetCenterX-200,0,4,10, [50,90,15],[[3,5],[2,3],[2,3],[1,2]],[YELLOW02,YELLOW02,YELLOW12,YELLOW11],0, [E_YELLOW,100,1,1]);	//popcorn

//	E35(2,GetCenterX-199,0,8,30, [60,5,120],[SP04,2,8,70],[8,13,3,-22.5],1500, [E_PURPLE,100,1,1]);	//glowy death diamonds


//	E31(7,GetCenterX-150,GetCenterY-150, 130,90, [2,20],[RED02,6,7,2,30],120,[60,5],  [E_RED,200,1,1,2]);	//aimed spray


	Wait(160);
	S3Speed(2.5,1,120);
	S3Distance(300,220,120);
	S3AngleFluctuate(0);
	Wait(280);

	Dialogue(3,1);
	while(EventStep!=1&&EventActive){
		yield;
	}

	SpawnBoss;

	Wait(30);
	while(EventActive){yield;}
	Music(6);

	WaitForBossDead;
	CollectAllItems(5);
	CollectAllSouls(60);
	ForbidShot2(true);
	Wait(150);
	ForbidShot2(false);
	if(IsFullSoul&&GetCommonDataDefault("Diff",1)>0){
		SpawnBoss;
		WaitForBossDead;
		CollectAllItems(5);
		CollectAllSouls(60);
	}
	ForbidShot2(true);
	Wait(150);

	Dialogue(3,2);
	yield;
	if(EventActive){
		CreateEnemyFromFile(GPSD~"s3\y_end.txt",0,0,0,0,0);
	}
	while(EventActive){yield;}
	Wait(95);
	DeleteDialoguePics(["yukari"]);
	DisplayResults(3);
	if(fullgame==1){
		PlayCredits;
	}
	StageClear=1;

 }

//=======================================================================================================
//=======================================================================================================
//=======================================================================================================
//=======================================================================================================
//=======================================================================================================

 function E31(et, xs, ys, vs, as, arg1, arg2, arg3, arg4, arg5){
	//appearing fairy
  SetCommonData("E",et);
  SetCommonData("A2",arg2);
  SetCommonData("A3",arg3);
  SetCommonData("A4",arg4);
  SetCommonData("A5",arg5);
  CreateEnemyFromFile(GPSD~"enemy\31.txt", xs,ys,vs,as,arg1);

  InwardExplosion(2,xs,ys,3,[150,150,150],vs,vs/2,-9);

 }
 function E32(et, xs, ys, vs, as, arg1, arg2, arg3, arg4, arg5){
	//moon thing
  SetCommonData("E",et);
  SetCommonData("A2",arg2);
  SetCommonData("A3",arg3);
  SetCommonData("A4",arg4);
  SetCommonData("A5",arg5);
  CreateEnemyFromFile(GPSD~"enemy\32.txt", xs,ys,vs,as,arg1);
 }

 task E33(et, xs, ys, vs, as, arg1, arg2, arg3, arg4, arg5){
	//random appearing bullets
  let color=RainbowArray(arg5[0],arg5[1],255-arg5[1]);
  InwardExplosion(3,xs,ys,1.5,color,vs,vs/2,9);
  InwardExplosion(3,xs,ys,4,[55,55,55],vs,vs/2,-9);

//  InwardExplosion(2,xs,ys,3,[150,150,150],vs,vs/2,-9);
  Wait(vs);
  SetCommonData("E",et);
  SetCommonData("A2",arg2);
  SetCommonData("A3",arg3);
  SetCommonData("A4",arg4);
  SetCommonData("A5",arg5);
  CreateEnemyFromFile(GPSD~"enemy\33.txt", xs,ys,vs,as,arg1);
  EnemyExplosion(arg4[0],xs,ys,arg4[1],color,arg4[2]*rand(0.9,1.1),1,arg4[3]*rand(0.9,1.1));

  loop(1){
	EnemyExplosion(arg4[0],xs,ys,arg4[1]/2,color,arg4[2]/2*rand(0.9,1.1),0,arg4[3]*rand(0.9,1.1));
  }
 }
 function E34(et, xs, ys, vs, as, arg1, arg2, arg3, arg4, arg5){
	//movable fairy
  SetCommonData("E",et);
  SetCommonData("A2",arg2);
  SetCommonData("A3",arg3);
  SetCommonData("A4",arg4);
  SetCommonData("A5",arg5);
  CreateEnemyFromFile(GPSD~"enemy\34.txt", xs,ys,vs,as,arg1);
 }

 function E35(et, xs, ys, vs, as, arg1, arg2, arg3, arg4, arg5){
	//exchikke
  SetCommonData("E",et);
  SetCommonData("A2",arg2);
  SetCommonData("A3",arg3);
  SetCommonData("A4",arg4);
  SetCommonData("A5",arg5);
  CreateEnemyFromFile(GPSD~"enemy\35.txt", xs,ys,vs,as,arg1);
 }

 function S3init(delay){
	SetSoulReq(3);
	LoadGraphic(s3bg1);
	LoadGraphic(s3bg2);loop(delay){yield;}
	LoadGraphic(s3bg3);
	LoadGraphic(s3bg4);loop(delay){yield;}
	LoadGraphic(s3bg5);
	LoadGraphic(s3bg6);loop(delay){yield;}
	LoadGraphic(s3bg7);
	LoadGraphic(s3bg8);loop(delay){yield;}
	LoadGraphic(s3bg9);
	LoadGraphic(s3bg10);loop(delay){yield;}
	LoadGraphic(s3bg11);
	LoadGraphic(s3bg12);loop(delay){yield;}
	LoadGraphic(s3bg13);
	if(GetCommonDataDefault("SpellPractice",0)==0){
		CompileEnemiesS3(delay);
		LoadMusicNew(5);loop(delay){yield;}
		LoadMusicNew(6);loop(delay){yield;}
		LoadEnemies([31,32,33,34,35],delay);
		CompileEnemyFromFile(GPSD~"s3\y_end.txt");
	}

	si=4;
	loop(4){
		holoscreenx[si]=  prand(-150,150);
		holoscreeny[si]=  prand(50,150);
		si++;
	}

 }

function S3Alt(num){
	SetStagePanSpeed(0);
	if(num==0){
		s3currentbg=1;
		s3bgspeed=1;
		s3bgangle=0;
		s3bgheight=50;
		s3bgdist=220;
		s3bgscroll=220;
	}
	if(num==1){
		s3currentbg=-1;
		s3bgspeed=2.5;
		s3overlay1=[0,0,200];
		s3overlay2=[200,0,200];

		s3bgangle=0;
		s3bgheight=20;
		s3bgdist=220;
		S3AngleFluctuate(1);
	}
}

 function S3final{
	DeleteGraphic(s3bg1);
	DeleteGraphic(s3bg2);
	DeleteGraphic(s3bg3);
	DeleteGraphic(s3bg4);
	DeleteGraphic(s3bg5);
	DeleteGraphic(s3bg6);
	DeleteGraphic(s3bg7);
	DeleteGraphic(s3bg8);
	DeleteGraphic(s3bg9);
	DeleteGraphic(s3bg10);
	DeleteGraphic(s3bg11);
	DeleteGraphic(s3bg12);
	DeleteGraphic(s3bg13);
	DeleteMusicNew(1);
	DeleteMusicNew(2);
 }

task S3Title(wait,fade,wait2,unfade) {

    StageTitle(wait,GetCenterX,GetCenterY-70,3,0);

    LoadGraphic(s3title);

    loop(wait){yield;}

    let x=0;
    let y=0;
    let obj = Obj_Create(OBJ_EFFECT);
    let timefade=fade;
    let opacity=0;
    let phase=0;
    let phasetimer=0;

    // initial settings    

    x=Obj_GetX(obj);
    y=Obj_GetY(obj);
    Obj_SetPosition   (obj, GetCenterX,GetCenterY);
    Obj_SetSpeed      (obj, 0);
    Obj_SetAngle(obj, 0);
    ObjEffect_SetTexture(obj,s3title);
    ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);
    ObjEffect_SetScale(obj,1.25,1.25);
    ObjEffect_SetAngle(obj,0,0,0);
    ObjEffect_SetLayer(obj,4);
    ObjEffect_CreateVertex(obj,4);
    ObjEffect_SetVertexXY(obj,0,x-128,y-75);
    ObjEffect_SetVertexXY(obj,1,x-128,y+75);
    ObjEffect_SetVertexXY(obj,2,x+128,y+75);
    ObjEffect_SetVertexXY(obj,3,x+128,y-75);
    ObjEffect_SetVertexUV(obj, 0, 0, 0);
    ObjEffect_SetVertexUV(obj, 1, 0, 150);
    ObjEffect_SetVertexUV(obj, 2, 256, 150);
    ObjEffect_SetVertexUV(obj, 3, 256, 0);

    let frame=0;
    while(! Obj_BeDeleted(obj)) {

	if(0-timefade==unfade){
		Obj_Delete(obj);
	}
	if(phase!=1){
		timefade--;
		if(timefade>=0){
			opacity=255-(timefade/fade)*255;
		} else {
			if(phase<1){phase++;}
			opacity=255-(absolute(timefade)/unfade)*255;

		}	
	} else {
		phasetimer++;
		if(phasetimer>=wait2){phase++;}
		opacity=255;
	}

	if(opacity<0){opacity=0;}
	if(opacity>255){opacity=255;}

	ObjEffect_SetVertexColor(obj,0, opacity*OpacityPercent(0,frame) ,255,255,255);
	ObjEffect_SetVertexColor(obj,1, opacity*OpacityPercent(1,frame) ,255,255,255);
	ObjEffect_SetVertexColor(obj,2, opacity*OpacityPercent(2,frame) ,255,255,255);
	ObjEffect_SetVertexColor(obj,3, opacity*OpacityPercent(3,frame) ,255,255,255);

	frame++;
        yield;

    }
    Obj_Delete(obj);
    DeleteGraphic(s3title);

    function OpacityPercent(vertex,frame){
//	return sin(frame*30+vertex*90)*0.1+0.8;
	return 1;
    }
}

let s3bgscroll=0;
let s3bgspeed=0;
let adjpar=0;
let adjpar2=0;
let si31=0;

function S3BGLogic{
	if(s3currentbg>0){ 	//lab
		s3bgscroll+=s3bgspeed*GetTimeSpeed;
		s3starscroll+=1*GetTimeSpeed;
//		si=0;
//		loop(8){
//			capsulebubble1[si]=capsulebubble1[si]+(capsulebubblespeed1[si]/10)*GetTimeSpeed;
//			capsulebubble2[si]=capsulebubble2[si]+(capsulebubblespeed2[si]/10)*GetTimeSpeed;
//			capsulebubble3[si]=capsulebubble3[si]+(capsulebubblespeed3[si]/10)*GetTimeSpeed;
//			si++;
//		}

//		si=0;
//		loop(10){
//			s3linesz[si]=s3linesz[si]-20;
//			if(s3linesz[si]<-400){
//				s3linesx[si]=prand(40,100)*(prand_int(0,1)-0.5);
//				s3linesy[si]=prand(200,10);
//				s3linesz[si]=s3linesz[si]+1200;
//			}
//			si++;
//		}
		si=rand_int(0,3);
		if((trunc(s3bgscroll+300*si))%1200<10){
			//holoscreenx[si*2]=  prand(160,300)*(prand_int(0,1)-0.5);
			//holoscreenx[si*2+1]=prand(160,300)*(prand_int(0,1)-0.5);
			holoscreenx[si*2]=  prand(-150,150);
			holoscreenx[si*2+1]=prand(-150,150);
				
			holoscreeny[si*2]=  prand(50,150);
			holoscreeny[si*2+1]=prand(50,150);
		}
		si++;
	}
	if(s3currentbg<0){	//stairs
		s3bgscroll+=s3bgspeed*GetTimeSpeed;
		s3cloudscroll+=1*GetTimeSpeed;
		si=0;
		loop(6){
			cloudphase[si]=cloudphase[si]+1*GetTimeSpeed;
			if(cloudphase[si]>=240||clouddir[si]==0){
				cloudphase[si]=cloudphase[si]-240;
				clouddir[si]=(prand_int(0,1)-0.5)*2;
				cloudoffset[si]=prand(-150,30)*clouddir[si];
				cloudposition[si]=prand_int(0,199);
			}
			si++;
		}
	}

	if(absolute(s3currentbg)<1){	//void
		s3voidscroll+=1*GetTimeSpeed;
		si=0;
		if((1-absolute(s3currentbg))-0.3>=0){
			loop(6){
				s3voidt[si]=s3voidt[si]+1*GetTimeSpeed;
				if(s3voidt[si]>=120){
					s3voidt[si]=0;
					s3voidx[si]=prand(GetClipMinX,GetClipMaxX);
					s3voidy[si]=prand(GetClipMinY,GetClipMaxY);
	
				}
				si++;
			}
		}
	}
}

let s3currentbg=-1;

function S3BG{
	if(s3currentbg>0){
		S3Lab;
	}else if(s3currentbg<0){
		S3Stair;
	}
	if(absolute(s3currentbg)<1){
		S3Void(1-absolute(s3currentbg));
	}
//	SetPlayerY(500);
}
task S3BGChange(one,two,time){
	let change=(two-one)/time;
	loop(time){
		yield;
		one+=change;
		s3currentbg=one;
	}
	s3currentbg=two;
}

let s3voidscroll=0;
let s3voidx=[0,0,0,0,0,0];
let s3voidy=[0,0,0,0,0,0];
let s3voidt=[0,20,40,60,80,100];

let s3draw=0;

let s3voidinit=0;

task VoidInit(frames){
	let vadd=1/frames;
	loop(frames){
		yield;
		s3voidinit+=vadd;
	}
	s3voidinit=1;
}
task VoidInitFull{
	let segments=50;
	let objl=InitEffect(0,0,GPSD~"img\gap2.png",segments*2,ALPHA,2);
	let objr=InitEffect(0,0,GPSD~"img\gap2.png",segments*2,ALPHA,2);
	ObjEffect_SetPrimitiveType(objl,PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_SetPrimitiveType(objr,PRIMITIVE_TRIANGLESTRIP);
	let frame=0;
	let k=0;
	let y=0;
	let width=2;
	let fluctuate=600;
	let frame=0;

	loop(60){
		S2BGLogic;
		yield;
	}
	let vadd=0.2/120;
	loop(120){
		s3voidinit+=vadd;
		width-=1/120;
		fluctuate=(cos(frame*(90/120)+90)+1)*600;
		frame++;
		k=0;
		y=GetClipMinY;
		loop(segments){
			S3GapIntro(0);
			k+=2;
			y+=(GetClipMaxY-GetClipMinY)/(segments-1);
		}
		ObjEffect_SetColor(objl,segments*2,lower((2-width)*600,255),255,255,255);
		ObjEffect_SetColor(objr,segments*2,lower((2-width)*600,255),255,255,255);
		S2BGLogic;
		yield;
	}

	let objc1=InitEffect(0,0,GPSD~"img\gap.png",4,ALPHA,2);
	let objc2=InitEffect(0,0,GPSD~"img\gap.png",4,ALPHA,2);

	fluctuate=0;
	s3voidinit=0.2;
	s3crossfade=1;
	let frames=30;
	vadd=0.8/frames;
	let fadeadd=2/frames;
	loop(frames){
		S2BGLogic;
		s3voidinit+=vadd;
		s3crossfade-=fadeadd;
		if(s3crossfade<0){s3crossfade=0;}
		frame++;
		k=0;
		y=GetClipMinY;
		loop(segments){
			S3GapIntro((GetClipMaxX-GetClipMinX)*(1-s3crossfade)*0.5);
			k+=2;
			y+=(GetClipMaxY-GetClipMinY)/(segments-1);
		}
		S3CenterGap((GetClipMaxX-GetClipMinX)*(1-s3crossfade)*0.5,80);
		yield;
	}
	s3voidinit=1;
	s3crossfade=0;
	Obj_Delete(objr);
	Obj_Delete(objl);
	let alpha=80;
	loop(200){
		alpha-=80/200;
		S3CenterGap((GetClipMaxX-GetClipMinX)*(1-s3crossfade)*0.5,alpha);
		frame++;
		yield;
	}
	Obj_Delete(objc1);
	Obj_Delete(objc2);

function S3GapIntro(openwidth){
	ObjEffect_SetVertexXY(objl,k,  GetCenterX+         sin(frame*7+y*2.4)*fluctuate-openwidth+cos(frame*19+y*11)*fluctuate*0.1,y+sin(frame*19+y*11)*fluctuate*0.2);
	ObjEffect_SetVertexXY(objl,k+1,GetCenterX-20*width+sin(frame*7+y*2.4)*fluctuate-openwidth+cos(frame*19+y*11)*fluctuate*0.1,y+sin(frame*19+y*11)*fluctuate*0.2);
	ObjEffect_SetVertexUV(objl,k,y+frame*7,1);
	ObjEffect_SetVertexUV(objl,k+1,y+frame*7,49);

	ObjEffect_SetVertexXY(objr,k,  GetCenterX+         sin(frame*7+y*2.4)*fluctuate+openwidth+cos(frame*19+y*11)*fluctuate*0.1,y+sin(frame*19+y*11)*fluctuate*0.2);
	ObjEffect_SetVertexXY(objr,k+1,GetCenterX+20*width+sin(frame*7+y*2.4)*fluctuate+openwidth+cos(frame*19+y*11)*fluctuate*0.1,y+sin(frame*19+y*11)*fluctuate*0.2);
	ObjEffect_SetVertexUV(objr,k,y+frame*7,1);
	ObjEffect_SetVertexUV(objr,k+1,y+frame*7,49);
}

function S3CenterGap(openwidth,alpha){
	ObjEffect_SetXYRect(objc1,(GetCenterX-openwidth),(GetClipMinY),(GetCenterX+openwidth),(GetClipMaxY));
	ObjEffect_SetUVRect(objc1,(GetCenterX-openwidth)+frame*5,(GetClipMinY)-frame*13,(GetCenterX+openwidth)+frame*5,(GetClipMaxY)-frame*13);

	ObjEffect_SetXYRect(objc2,(GetCenterX-openwidth),(GetClipMinY),(GetCenterX+openwidth),(GetClipMaxY));
	ObjEffect_SetUVRect(objc2,(GetCenterX-openwidth)-frame*13,(GetClipMinY)-frame*5,(GetCenterX+openwidth)-frame*13,(GetClipMaxY)-frame*5);

	ObjEffect_SetColor(objc1,4,alpha*1.2,255,255,255);
	ObjEffect_SetColor(objc2,4,alpha*1.2,255,255,255);
}

}

function S3Void(opacity){
	if(s3crossfade>0){
		S2BG;
		SetTexture(stgwhite);
		SetGraphicRect(0,0,(GetClipMaxX-GetClipMinX)*(1-s3crossfade),500);
		SetGraphicAngle(0,0,0);
		SetGraphicScale(1,1);
		SetColor(0,0,0);
		SetAlpha(255);
		DrawGraphic(GetCenterX,GetCenterY);
	}
	let void1=lower(s3voidinit*3,1);
	let void2=(upper(s3voidinit,0.5)-0.5)*2;

	let opacity2=(opacity-0.3)*1.3;
	SetColor(255,255,255);
	SetTexture(s3bg8);
	SetGraphicScale(2,2);
	SetAlpha(opacity*255*void2);
	SetGraphicAngle(0,0,40-s3voidscroll/4);
	SetGraphicRect(0,0+s3voidscroll*3,300,300+s3voidscroll*3);
	DrawGraphic(GetCenterX,GetCenterY);
	SetAlpha(opacity*127*void2);
	SetGraphicAngle(0,0,-20+s3voidscroll/8);
	SetGraphicRect(0,0+s3voidscroll*3,300,300+s3voidscroll*3);
	DrawGraphic(GetCenterX,GetCenterY);
	SetAlpha(opacity*63*void2);
	SetGraphicAngle(0,0,-40-s3voidscroll/3);
	SetGraphicRect(0+s3voidscroll*3,0,300+s3voidscroll*3,300);
	DrawGraphic(GetCenterX,GetCenterY);

	if(opacity2>0){
		SetTexture(s3bg9);
		SetGraphicRect(1,1,127,127);
		SetGraphicScale(4,4);
		si=0;
		loop(6){
			SetGraphicAngle(0,0,si*60);
			SetColor(lower(si*84,252),20,lower(252-si*84,252));
			SetAlpha(opacity2*255*(sin(s3voidt[si]*(180/120)))*void1);
			DrawGraphic(s3voidx[si],s3voidy[si]);
			si++;
		}
	}
	SetTexture(s3bg10);
	SetGraphicRect(0,0,256,256);
	SetGraphicAngle(0,0,0);

//	SetColor(127+sin(s3voidscroll)*127,0,127-sin(s3voidscroll)*127);
	SetColor(lower(255+sin(s3voidscroll*2)*255,255),0,lower(255,255-sin(s3voidscroll*2)*255));
	SetAlpha(opacity*(80-s3voidscroll%80)*3*s3voidinit);
	SetGraphicScale(1.5+(s3voidscroll%80)/30,1.5+(s3voidscroll%80)/30);
	DrawGraphic(GetCenterX+cos(s3voidscroll*100)*5,GetCenterY+sin(s3voidscroll*123)*5);

	s3voidscroll+=40;
	SetAlpha(opacity*(80-s3voidscroll%80)*3*s3voidinit);
	SetGraphicScale(1.5+(s3voidscroll%80)/30,1.5+(s3voidscroll%80)/30);
	DrawGraphic(GetCenterX+cos(s3voidscroll*100)*5,GetCenterY+sin(s3voidscroll*123)*5);
	s3voidscroll-=40;

	SetAlpha(opacity*200*s3voidinit);
	SetGraphicScale(1.5,1.5);
	DrawGraphic(GetCenterX+cos(s3voidscroll*100)*5,GetCenterY+sin(s3voidscroll*123)*5);

	s3draw++;
}

let s3linesx=[0,0,0,0,0,0,0,0,0,0];
let s3linesy=[-10,-10,-10,-10,-10,-10,-10,-10,-10,-10];
let s3linesz=[800,680,560,440,320,200,80,-40,-160,-280];

let holoscreenx=[60,-30,10,-20,  0,0,0,0];
let holoscreeny=[70,50,110,90,  0,0,0,0];
let holoscreent=[1,2,0,3,1,2,0,3];

function DrawS3Lines{
	SetTexture(stgwhite);
	SetColor(255,255,255);
	SetGraphicRect(0,0,2,328);
	SetGraphicAngle(90,0,0);
	SetGraphicScale(0.3,1);
	si=0;
	loop(10){
		SetAlpha(200*(s3linesz[si]+400)/1200);
		DrawGraphic3D(s3linesx[si],s3linesy[si],s3linesz[si]);
		si++;
	}
}
let s3bgangle=0;
let s3bgheight=0;
let s3starscroll=0;
//let s3labbgcolor=[0,0,35];
let s3labbgcolor=[40,40,64];
let capsulebubble1=[268,73,399,377,290,197,289,166];
let capsulebubble2=[364,345,132,341,279,259,307,209];
let capsulebubble3=[352,89,388,288,394,86,67,372];
let capsulecross=[1,-1,1,-1,1,-1,1,-1];

let capsulebubblespeed1=[27,29,26,12,23,29,10,30];
let capsulebubblespeed2=[25,19,12,11,28,14,30,21];
let capsulebubblespeed3=[17,12,16,25,30,21,26,18];

let s3sin=[0,0.7071,1,0.7071,0,-0.7071,-1,-0.7071];
let s3cos=[1,0.7071,0,-0.7071,-1,-0.7071,0,0.7071];
//-----------------------------------------------------------------------------------------------------------------------------------------
sub S3Lab{

//	s3bgangle=center(-500,adjpar,500);

	SetTexture(stgwhite);
	SetGraphicRect(0,0,1000,1000);
	SetColor(s3labbgcolor[0],s3labbgcolor[1],s3labbgcolor[2]);
	SetGraphicScale(1,1);
	SetGraphicAngle(0,0,0);
	DrawGraphic(GetCenterX,GetCenterY);

	SetTexture(s3bg6);
	SetColor(255,255,255);
	SetGraphicScale(1,1);
	SetGraphicRect(0,0,1300,1300);
	SetGraphicAngle(0,0,-s3starscroll/12);
	DrawGraphic(GetCenterX-s3bgangle*5+ScreenShakeX,GetCenterY+200+s3bgheight*2+ScreenShakeY);
	SetGraphicAngle(0,0,-s3starscroll/24);
	DrawGraphic(GetCenterX-s3bgangle*5+ScreenShakeX,GetCenterY+300+s3bgheight*2+ScreenShakeY);	//stars


	SetTexture(s3bg4);
	SetGraphicRect(0,0,500,256);
	SetGraphicAngle(0,0,0);
	SetGraphicScale(1,-2);
	SetColor(0,0,0);
	DrawGraphic(GetCenterX,GetCenterY-330+s3bgheight*2);	//gradient
	SetColor(255,255,255);

	SetTexture(s3bg5);
	SetGraphicScale(1,1);
	SetGraphicRect(1,133,122,253);
	SetGraphicScale(1,1);
	SetGraphicAngle(0,0,0);
	DrawGraphic(350-s3bgangle*3+ScreenShakeX,30+s3bgheight*1+ScreenShakeY);	//moon

	WriteZBuffer(true);
	UseZBuffer(true);

//	SetTexture(stgwhite);
	SetTexture(s3bg1);
	SetViewTo(0-s3bgangle*4+ScreenShakeX,150-s3bgheight*1.5+ScreenShakeY,400-s3bgangle*4);
	SetViewFrom(1+s3bgangle*2,90+s3bgangle-GetPan*0.05,20+s3bgangle/6-s3bgheight/3);
	SetGraphicScale(2.5,1);
	SetGraphicAngle(90,0,0);
	si=0;
	SetColor(255,255,255);
	loop(5){
//		SetColor(si31,si31,si31);
		SetAlpha(si*51);
//		SetAlpha(255);

		SetGraphicRect(0,0-trunc(s3bgscroll)+si*50,512,50-trunc(s3bgscroll)+si*50);
		DrawGraphic3D(0,0,-450+si*50);
		si++;
	}
	SetGraphicRect(0,0-trunc(s3bgscroll)+250,512,530-trunc(s3bgscroll)+250);
	DrawGraphic3D(0,0,40);

	si31=0;
	loop(4){
		capsuledepth=(trunc(s3bgscroll))%300+300*si31;
		let vis=lower(capsuledepth/1.5,255)/255;	//opacity
		if(vis>0.01){

			if(capsuledepth<830){
				DrawCapsule((si31-trunc(s3bgscroll/300)+40000)%4,1,0,capsuledepth-500,vis);
				DrawCapsule((si31-trunc(s3bgscroll/300)+40000)%4+4,-1,0,capsuledepth-500,vis,);
				DrawHoloscreen(((si31-trunc(s3bgscroll/300)+40000)%4)*2,capsuledepth-410,vis);
				DrawHoloscreen(((si31-trunc(s3bgscroll/300)+40000)%4)*2+1,capsuledepth-260,vis);
			}
		}

		si31++;
	}
	si31=0;

	SetTexture(stgwhite);
	SetGraphicRect(0,0,384,448);
	SetColor(sin(s3bgscroll*4)*20+20,0,0);
	SetGraphicScale(1,1);
	SetGraphicAngle(0,0,0);
	SetAlpha(50);
	DrawGraphic(GetCenterX,GetCenterY);

//	DrawS3Lines;
}
let si32=0;
let capsuledepth=0;
let bubbledraw=0;

let shadowangle=15;

function DrawHoloscreen(id,z, visibility){
	let opacity=lower(visibility*2,1);
	if(z>230){opacity*=1-((z-230)/100);opacity=upper(opacity,0);}
	if(opacity>0){
		let x=holoscreenx[id];
		let y=holoscreeny[id];
		SetTexture(s3bg7);
//		SetColor((255-s3labbgcolor[0])*visibility+s3labbgcolor[0],(255-s3labbgcolor[1])*visibility+s3labbgcolor[1],(255-s3labbgcolor[2])*visibility+s3labbgcolor[2]);
		SetColor(255,255,255);

		SetGraphicScale(0.7,-0.7);
		SetGraphicAngle(0,0,0);
		if(s3starscroll%8<4){
			SetGraphicRect(0,0,130,85);
		}else{
			SetGraphicRect(0,90,130,85+90);
		}
		SetAlpha(opacity*200);

		DrawGraphic3D(x,y,z);

		SetAlpha(opacity*255);
		SetGraphicRect(-48,holoscreent[id]*48,0,(holoscreent[id]+1)*48);
		SetGraphicScale(0.8,-0.8);
		DrawGraphic3D(x,y+2,z);
	}
}

function DrawCapsule(id, x,y,z, visibility){

		let side=x;
		x*=80;

		SetColor((255-s3labbgcolor[0])*visibility+s3labbgcolor[0],(255-s3labbgcolor[1])*visibility+s3labbgcolor[1],(255-s3labbgcolor[2])*visibility+s3labbgcolor[2]);
		let opacity=lower(visibility*2,1);

		SetTexture(s3bg4);
		SetColor(0,0,0);
		SetAlpha(opacity*120);
		SetGraphicRect(0,0,58,256);
		SetGraphicAngle(90,0,shadowangle);
		SetGraphicScale(1,-0.5);
		DrawGraphic3D(x-15,y+1,z+55);
		SetColor(255,255,255);

		SetTexture(s3bg2);
		SetAlpha(opacity*255);

		si=-168.75; //position the "seam" at the back
		si32=0;
		loop(8){
			if(si32<3||si32>5){
				SetGraphicScale(1,1);
				SetGraphicAngle(0,si32*45,0);			//middle of base
				SetGraphicRect(si32*24,0,(si32+1)*24,32);
				//DrawGraphic3D(x+sin(si)*29,y+22,z+cos(si)*29);
				DrawGraphic3D(x+s3sin[si32%8]*28,y+14,z+s3cos[si32%8]*28);
				SetGraphicScale(1,1);	
				SetGraphicAngle(-15,si32*45,0);			//top of base
				SetGraphicRect(si32*24,32,(si32+1)*24,48);
				DrawGraphic3D(x+s3sin[si32%8]*26,y+37,z+s3cos[si32%8]*26);
	
			}


			si+=45;
			si32++;
		}

		WriteZBuffer(false);
		SetTexture(s3bg3);
		SetAlpha(opacity*120);
		si=-168.75; //position the "seam" at the back
		//si=11.75;
//		si=-60.76;
		si32=0;
		loop(8){
			SetGraphicScale(1,1);
			SetGraphicAngle(0,si32*45,0);			//capsule
			SetGraphicRect(si32*20,0,(si32+1)*20,400);
			DrawGraphic3D(x+s3sin[si32]*23.5,y+200,z+s3cos[si32]*23.5);

			si+=45;
			si32++;
		}
		WriteZBuffer(true);

		SetTexture(s3bg5);
		SetRenderState(ADD);
		SetColor(opacity*200,opacity*200,opacity*200);

		SetGraphicScale(0.4*capsulecross[id],-1);
		SetGraphicRect(128,2,256,256);
		SetGraphicAngle(0,-s3bgangle,0);
		DrawGraphic3D(x,y+148,z);

		SetRenderState(ALPHA);
		SetColor(255,255,255);
		SetAlpha(opacity*255);

		SetGraphicScale(0.8,-0.8);
//		if(bubbledraw%3==0){
		SetGraphicRect(0,0,32,110);
		DrawGraphic3D(x,y+(capsulebubblespeed1[id]/10*fixedscroll)%400-50,z);
//		}else if(bubbledraw%3==1){
		SetGraphicRect(32,0,64,110);
		DrawGraphic3D(x,y+(capsulebubblespeed2[id]/10*fixedscroll)%400-50,z);
//		}else{
		SetGraphicRect(64,0,96,110);
		DrawGraphic3D(x,y+(capsulebubblespeed3[id]/10*fixedscroll)%400-50,z);
//		}

	//x,y,z == +r,  +u,  +f
	//x,y,z == l/r, u/d, f/b
}
task S3LabInit(delay){
	NoPan;
	s3bgspeed=0;
	s3bgscroll=220;
	s3bgangle=20;
	s3bgheight=30;
	Wait(delay);
	S3Angle(20,0,480);
	Wait(180);
	S3Speed(0,1,240);
	S3Height(30,50,240);
	Wait(240);
	SetStagePanSpeed(0.01);
}
//-----------------------------------------------------------------------------------------------------------------------------------------

let s3overlay1=[0,0,0];
let s3overlay2=[0,0,0];

let s3overlayalpha=50;
let s3cloudscroll=0;
let clouddir=[0,0,0,0,0,0];
let cloudoffset=[0,0,0,0,0,0];		//x position
let cloudposition=[0,0,0,0,0,0];	//z position
let cloudphase=[0,40,80,120,160,220];	//timer
let s3bgdist=200;


sub S3Stair{
	SetViewTo(0+ScreenShakeX,0+(s3bgdist-200)/8,0+ScreenShakeY);
	SetViewFrom(s3bgdist,90+s3bgangle,s3bgheight);
	SetTexture(s3bg11);
	SetColor(255,255,255);
	SetGraphicScale(1,1);
	SetGraphicAngle(90,0,0);
	WriteZBuffer(true);
	UseZBuffer(true);
	SetAlpha(255);
	SetColor(255,255,255);
	si32=-70;
	loop(16){
		SetGraphicRect(0,0-si32*2-trunc(s3bgscroll/30)*60,140,15-si32*2-trunc(s3bgscroll/30)*60);
		DrawGraphic3D(0,0+si32-trunc(s3bgscroll)%30,0-si32+trunc(s3bgscroll)%30);
		si32+=15;
	}
	SetColor(100,100,100);
	si32=-70+7.5;
	SetGraphicAngle(0,0,0);
	loop(16){
		SetGraphicRect(0,0-si32*2-trunc(s3bgscroll/30)*60,140,16-si32*2-trunc(s3bgscroll/30)*60);
		DrawGraphic3D(0,0+si32-trunc(s3bgscroll)%30,0-si32+trunc(s3bgscroll)%30);
		si32+=15;
	}



	SetGraphicRect(17,0+trunc(s3bgscroll)*1.414,-4,500+trunc(s3bgscroll)*1.414);

	SetGraphicScale(1,1);
	SetGraphicAngle(0,90,-45);
	DrawGraphic3D(62,14,-8);
	DrawGraphic3D(-62,14,-8);

	SetColor(255,255,255);

	SetGraphicRect(0,0+s3bgscroll*1.414,17,500+s3bgscroll*1.414);

	SetGraphicAngle(-45,0,0);
	DrawGraphic3D(70,20,0);
	SetGraphicScale(-1,1);
	DrawGraphic3D(-70,20,0);

	SetTexture(s3bg12);	//grass
	SetColor(0,50,0);
	SetGraphicRect(0,0+s3bgscroll*2.828,350,900+s3bgscroll*2.828);
	SetGraphicScale(0.5,0.5);
	SetGraphicAngle(-45,0,0);
	DrawGraphic3D(175,-10,-30);
	DrawGraphic3D(-175,-10,-30);


	SetTexture(s3bg13);
	SetColor(255,255,255);
	SetGraphicAngle(-20-s3bgheight,0,0);
	SetGraphicScale(0.6,0.4);
	si32=0;
	UseZBuffer(false);
	WriteZBuffer(false);
	loop(6){
		if(cloudphase[si32]>=0){
			si31=(s3bgscroll+cloudposition[si32])%200;
			si=(cloudphase[si32])%240;
			SetAlpha(sin(si*(180/240))*255);
			SetGraphicRect(0,128,256,256);
			DrawGraphic3D(si*clouddir[si32]+cloudoffset[si32],40-si31+100,0+si31-100);
			SetGraphicRect(0,0,256,128);
			DrawGraphic3D(si/2*clouddir[si32]+cloudoffset[si32],40-si31+100,0+si31-100);
		}
		si32++;
	}
//	UseZBuffer(true);

	SetTexture(stgwhite);
	SetGraphicRect(0,1,390,256);
	SetGraphicAngle(0,0,0);
	SetAlpha(s3overlayalpha);
	SetColor(s3overlay2[0],s3overlay2[1],s3overlay2[2]);
	SetGraphicScale(1,2);
	DrawGraphic(GetCenterX,GetCenterY);
	SetTexture(s3bg4);
	SetColor(s3overlay1[0],s3overlay1[1],s3overlay1[2]);
	SetGraphicScale(1,-2);
	DrawGraphic(GetCenterX,GetCenterY-75);
}

//	lr,	ud, 	bf
//	-+	+-	-+

task S3StairInit(delay){
	s3bgspeed=0.5;
	s3bgscroll=0;

	s3bgangle=0;
	s3bgheight=20;	//top view
	s3bgdist=300;

	Wait(delay);
	S3Distance(300,220,200);
	Wait(30);
	S3Angle(0,-20,240);
	Wait(60);
	S3Height(20,10,120);

//	s3bgangle=-20;
//	s3bgheight=10;	//side view
//	s3bgdist=220;


}
//-----------------------------------------------------------------------------------------------------------------------------------------
task S3Angle(one,two,time){
	let frame=0;
	loop(time){
		s3bgangle=(cos((frame)*(180/time)+180)+1)*((two-one)*0.5)+one;
		frame++;
		yield;
	}
	s3bgangle=two;
}
task S3Height(one,two,time){
	let frame=0;
	loop(time){
		s3bgheight=(cos((frame)*(180/time)+180)+1)*((two-one)*0.5)+one;
		frame++;
		yield;
	}
	s3bgheight=two;
}
task S3Distance(one,two,time){
	let frame=0;
	loop(time){
		s3bgdist=(cos((frame)*(180/time)+180)+1)*((two-one)*0.5)+one;
		frame++;
		yield;
	}
	s3bgdist=two;
}
task S3Speed(one,two,time){let oadd=(two-one)/time;loop(time){one+=oadd;s3bgspeed=one;yield;}}

task S3OverlayChange(new1,new2,alpha,time){
	let old1=s3overlay1;
	let old2=s3overlay2;
	let add1=[0,0,0];
	let add2=[0,0,0];
	let k=0;
	loop(3){
		add1[k]=(new1[k]-old1[k])/time;
		add2[k]=(new2[k]-old2[k])/time;
		k++;
	}
	let alphaadd=(alpha-s3overlayalpha)/time;
	loop(time){
		k=0;
		loop(3){
			old1[k]=old1[k]+add1[k];
			s3overlay1=old1;
			old2[k]=old2[k]+add2[k];
			s3overlay2=old2;
			k++;
		}
		s3overlayalpha+=alphaadd;
		yield;
	}
	s3overlay1=new1;
	s3overlay2=new2;
	s3overlayalpha=alpha;
}

task S3AngleFluctuate(start){
	let frame=0;
	let intensity=10*start;
	while(StageClear!=1){
		s3bgangle=sin(frame/1.5)*intensity;
		frame++;
		if(intensity<10){
			intensity+=0.05;
		}
		yield;
		while(timefrozen){yield;}
	}
}