 let s1bg1=GPSD~"img\water.png";
 let s1bg2=GPSD~"img\gradient.png";
 let s1bg3=GPSD~"img\reflection.png";
 let s1bg4=GPSD~"img\mountains.png";
 let s1bg5=GPSD~"img\stars2.png";
 let s1bg6=GPSD~"img\dust.png";
 let s1bg7=GPSD~"img\s1stuff.png";
 let s1title=GPSD~"img\stage1.png";

 let si=0;

// Type, X, Y, V, Angle, a1, a2, a3, a4
//

	let spawnx1=[GetClipMinX-20,GetClipMaxX+20,GetClipMinX-20,GetClipMaxX+20];
	let spawny1=[GetCenterY-40,GetCenterY-40,GetCenterY-80,GetCenterY-80];
	let angle1=[20,160,50,120];
	let spin1=[-2,2,-3,3];

//type,x,y,v,a,ex
//--E1--
//0=main	| [angvel,time],[type,vel,delay,number,offset],interval,delay
//1=spread	|decel time, wait, [type,vel,spread,delay,number, type,vel,number], life
//2=tube	|[angle1,angleadd],decel,accel,[type,vel,vel2,delay]
//3=shotgun	|decel,[angle1,angle2],[numtype,vel,delay,number],[shot types]
//--E2--
//0=aimed lasers	| time1,[rotspeed1,rotspeed2,size1,size2],[vel,length,width,delay],[types]
//1=spread lasers	| time,[rotspeed1,rotspeed2,size1,size2],[vel,length,width,delay],[types]

 task stage1{
	CreateBoss(GetCenterX,-100,0,"s1",["_last","~_explode"],[[0,1],[1,1],[1,1]],diff,1, 2);
	CreateBoss(GetCenterX,-100,0,"s1",["n_3","s_1","n_4","s_2","s_3","~_explode"],[[1,1,1,1,0,1],[1,1,1,1,0,1],[1,1,1,1,1,1]],diff,[4,4,5][diff], 2);
	CreateBoss(GetCenterX,-100,0,"s1",["n_1","n_2","~_exit"],[[1,1,1],[1,1,1],[1,1,1]],diff,2, -1);

	if(IsAWCPlayer&&fullgame==0&&!IsReplay){
		IncTimesPlayed(GetPlayer,1,diff);
	}
	if(IsAWCPlayer&&fullgame==1&&!IsReplay){
		IncTimesPlayed(GetPlayer,0,diff);
	}
	LoadDialoguePics(["ender"]);
	ResetCam;

//	yield;
//	Dialogue(3,2);
//	while(EventActive){yield;}

	s1speed=0;
//	DisplayBorderLine;

	Wait(5);
	Stage1ZoomIn;
	Music(1);

	si=0;
	loop(20){
		S1Water(0,rand(-200,200),si);
		si+=30;
	}

	S1Title(1,100,100,100);

	Wait(210);
	SetEnemyInvincibility(0);

	si=0;
	loop(5){
		E1(0,GetClipMinX-10,150-si*20,4,-30+si*15, [0.4,60],[0,0,0,0],0,20, [E_BLUE,64,2,1]);
		Wait(10);
		E1(0,GetClipMaxX+10,150-si*20,4,210-si*15,[-0.4,60],[0,0,0,0],0,20, [E_YELLOW,64,3,1]);
		Wait(10);
		si++;
	}
	Wait(40);
	si=0;
	loop(5){
		E1(0,GetClipMinX-10,30+si*10,4,si*7, [0.7,60],[0,0,0,0],0,20,[E_GREEN,64,1,1]);
		Wait(10);
		E1(0,GetClipMaxX+10,30+si*10,4,180-si*7,[-0.7,60],[0,0,0,0],0,20,[E_GREEN,64,1,1]);
		Wait(10);
		si++;
	}
	si=5;
	loop(5){
		E1(0,GetCenterX,-20,3,50+si*3, [0.6,80],[0,0,0,0],0,20, [0,0,2,1]);
		si++;
		Wait(10);
		E1(0,GetCenterX,-20,3,130-si*3, [-0.6,80],[0,0,0,0],0,20, [0,0,2,1]);
		si++;
		Wait(10);
	}

	Wait(300);

	E1(0,GetClipMinX-10,100,5,-10,[0.5,600],[BLUE01,2,0,1,0],20,0, [E_BLUE,96,2,1]);
	Wait(40);
	E1(0,GetClipMaxX+10,100,5,190,[-0.5,600],[AQUA01,2,0,1,0],20,0, [E_AQUA,96,2,1]);
	Wait(90);
	si=0;
	loop(6){
		E1(0,GetCenterX+50+si*70,0,4,120, [1,60],[BLUE11,2,0,1,0],30,20, [E_BLUE,96,2,1]);
		Wait(10);
		E1(0,GetCenterX-50-si*70,0,4,60, [-1,60],[AQUA11,2,0,1,0],30,20, [E_AQUA,96,2,1]);
		Wait(10);
		si+=0.5;
	}
	Wait(30);
	E1(1,GetCenterX,0,6,90,50,20,[RED54,2,30,20,5,RED05,1.5,16],350, [20,128,3,3]);
	Wait(80);
	si++;
	loop(8){
		E1(0,GetClipMinX-10,170-si*30,4,-30+si*7, [0.8,40],[RED05,2,8,1,0],40,20, [20,64,3,1]);
		Wait(5);
		E1(0,GetClipMaxX+10,170-si*30,4,210-si*7, [-0.8,40],[ORANGE05,2,8,1,0],40,20, [32,64,3,1]);
		Wait(5);
		si+=0.5;
	}
	Wait(80);

	loop(2){
		E1(0,GetClipMinX-10,200,4,-10, [0,1],[AQUA11,2,3,3,3],15,5, [E_AQUA,96,1,1]);
		Wait(15);
		E1(0,GetClipMaxX+10,200,4,190, [0,1],[GREEN11,2,3,3,3],15,5, [E_BLUE,96,1,1]);
		Wait(30);
	}
	E1(1,GetCenterX,0,7,90, 50,20,[PURPLE54,2,30,30,5,PURPLE11,1.2,20],250, [300,128,2,3]);
	Wait(80);
	E1(1,GetCenterX-20,0,7,40, 50,20,[RED54,2,30,30,5,RED11,1.2,20],250, [0,128,2,3]);
	E1(1,GetCenterX+20,0,7,140, 50,20,[BLUE54,2,30,30,5,BLUE11,1.2,20],250, [220,128,2,3]);
	Wait(130);

	E1(4,GetClipMinX,GetClipMinY-40,0,0, 0,0,0,0, [E_PURPLE,128,0,5]);	//death fairy
	WaitForZeroEnemy;	

	si=-1.5;
	loop(4){
		E1(0,GetCenterX+si*10,0,4,180, [-4,90],[RED01,1.5,5,1,0],4,0, [E_RED,96,3,1]);
		Wait(40);
		E1(0,GetCenterX+80,si*20,4,0, [4,90],[YELLOW01,1.5,5,1,0],4,0, [E_YELLOW,96,3,1]);
		Wait(10);
		E1(0,GetCenterX-80,si*20,4,180, [-4,90],[YELLOW01,1.5,5,1,0],4,0, [E_YELLOW,96,3,1]);
		Wait(20);
		si++;
	}
	E2(0,4,GetClipMaxX+10,50,1.5,170, 60,[-1,-3,20,60],[2,70,8,30],[[BLUE11,AQUA11,GREEN11,YELLOW11],[5,4,3,2]],[2,2]);

	Wait(350);

	WaitForZeroEnemy;
	Wait(50);

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

	Cam2(0,150,1610);
	Cam1(0,-20,1610);

	si=0;
	loop(5){
		S1Water(1,rand(-200,200),si);
		si+=150;
	}


	BGS1(3,3.8,900);

	si=0;
	loop(8){
		E1(0,GetClipMinX-10,170-si*30,4,-50+si*7, [0.8,40],[PURPLE11,2,8,1,0],40,0, [E_PURPLE,96,2,1]);
		E1(0,GetClipMaxX+10,170-si*30,4,230-si*7, [-0.8,40],[RED11,2,8,1,0],40,0, [E_RED,96,3,1]);
		Wait(15);
		si+=0.5;
	}

	Wait(60);

	E2(1,3,GetClipMaxX+10,50,1.5,160, 60,[1,4,20,40],[3,200,8,12],[[RED11,ORANGE11,YELLOW11],[0,1,2]],[2,2]);
	Wait(120);
	E2(1,2,GetClipMinX-10,50,1.5,20, 60,[2,8,10,30],[2,100,8,8],[[GREEN11,AQUA11],[3,4]],[2,2]);
	Wait(120);
	E2(1,4,GetClipMaxX+10,0,1.5,140, 60,[1,4,20,40],[3,200,8,12],[[BLUE11,PURPLE11,BLUE11,PURPLE11],[5,6,5,6]],[2,2]);

	Wait(160);
	E1(0,GetCenterX,0,3,180, [-2,180],[AQUA11,1.5,5,2,7],12,0, [180,96]);E1(0,GetCenterX,0,3,0, [2,180],[RED11,1.5,5,2,7],12,0, [20,96,2,1]);Wait(10);
	E1(0,GetCenterX,0,3,180, [-2,180],[GREEN11,1.5,5,2,7],12,0, [120,96]);E1(0,GetCenterX,0,3,0, [2,180],[ORANGE11,1.5,5,2,7],12,0, [32,96,3,1]);Wait(10);
	E1(0,GetCenterX,0,3,180, [-2,180],[YELLOW11,1.5,5,2,7],12,0, [68,96]);E1(0,GetCenterX,0,3,0, [2,180],[YELLOW11,1.5,5,2,7],12,0, [68,96,1,1]);Wait(10);
	E1(0,GetCenterX,0,3,180, [-2,180],[ORANGE11,1.5,5,2,7],12,0, [32,96]);E1(0,GetCenterX,0,3,0, [2,180],[GREEN11,1.5,5,2,7],12,0, [120,96,2,1]);Wait(10);
	E1(0,GetCenterX,0,3,180, [-2,180],[RED11,1.5,5,2,7],12,0, [20,96]);E1(0,GetCenterX,0,3,0, [2,180],[AQUA11,1.5,5,2,7],12,0, [180,96,3,1]);Wait(10);

	Wait(140);

	E2(0,3,GetClipMaxX+10,150,1.5,200, 60,[-1,-4,20,40],[2,30,20,42],[[RED11,PURPLE11,BLUE11],[0,6,5]],[2,2]);
	E2(0,3,GetClipMinX-10,150,1.5,-20, 60,[1,4,20,40],[2,30,20,42],[[BLUE11,PURPLE11,RED11],[5,6,0]],[2,2]);
	Wait(60);
	E2(0,3,GetClipMaxX+10,100,1.5,150, 60,[-1,-4,20,40],[2,30,20,42],[[YELLOW11,GREEN11,BLUE11],[2,3,5]],[2,2]);
	E2(0,3,GetClipMinX-10,100,1.5,30,  60,[1,4,20,40],[2,30,20,42],[[BLUE11,GREEN11,YELLOW11],[5,3,2]],[2,2]);

	Wait(140);
	si=1;
	loop(5){
		E1(0,GetClipMinX-10,30,2,si*14, [0,1000],[WHITE05,2,5,1,0],0,0, [32,64,3,1]);
		E1(0,GetClipMaxX+10,30,2,180-si*14, [0,1000],[WHITE05,2,5,1,0],0,0, [32,64,3,1]);
		Wait(25);
		si++;

	}
	Wait(40);
	E1(1,GetCenterX-100,0,10,30, 50,20,  [GREEN03,1.5,40,30,5,RED11,1,16],250, [130,128,3,3]);
	E1(1,GetCenterX+100,0,10,150,  50,20,[RED03,  1.5,40,30,5,GREEN11,1,16],250, [10,128,3,3]);
	Wait(120);

	E1(1,GetClipMaxX+10,300,8,-120,50,20,[RED03,  1.5,40,30,5,GREEN11,1,16],250, [10,128,3,3]);
	E1(1,GetClipMinX-10,300,8,-60, 50,20,[GREEN03,1.5,40,30,5,RED11,1,16],250, [130,128,3,3]);

	Wait(180);

	loop(2){
		E1(3,GetClipMaxX+10,rand(30,90),rand(3,6),rand(-15,15)+180, 40,[60,120],[4,2,14,10],[BLUE05,AQUA05,BLUE01,AQUA01], [E_BLUE,128,2,3]);
		Wait(3);
		E1(3,GetClipMinX-10,rand(30,90),rand(3,6),rand(-15,15),  40,[60,120],[4,2,14,10],[YELLOW05,GREEN05,YELLOW01,GREEN01], [E_GREEN,128,2,3]);
		Wait(3);
	}
	Wait(180);
	loop(2){
		E1(3,GetClipMinX+rand(15,40),0,rand(3,6),rand(80,100), 40,[-30,30],[4,1.5,10,7],[GREEN11,AQUA11,GREEN01,AQUA01], [100,128,1,3]);
		Wait(10);
		E1(3,GetClipMaxX-rand(15,40),0,rand(3,6),rand(80,100), 40,[-150,-210],[4,1.5,10,7],[RED11,ORANGE11,RED01,ORANGE01], [30,128,1,3]);
		Wait(10);
	}
	Wait(50);

	let colorss1a=[YELLOW54,ORANGE54,YELLOW01,ORANGE01];

	si=0;
	loop(14){
		E1(1,GetCenterX,0,5+cos(si*(90/7))*2,15+25*si, 50,200,[colorss1a[(si*2)%2],1.5,60,30,3, colorss1a[(si*2)%2+2],1,5],300, [55,128,3,3]);
		si+=0.5;
		Wait(15);
	}


	Wait(300);
//	E2(1,7,GetCenterX,-30,0.8,90, 60,[0,3,30,80], [2,40,40,20],[[RED01,ORANGE01,YELLOW01,GREEN01,AQUA01,BLUE01,PURPLE01],[0,1,2,3,4,5,6]],[1,1]);
//	E2(1,7,GetCenterX,-30,0.8,90, 60,[0,-3,15,40],[2,25,25,20],[[RED01,ORANGE01,YELLOW01,GREEN01,AQUA01,BLUE01,PURPLE01],[0,1,2,3,4,5,6]],[1,1]);


	WaitForZeroEnemy;
	Wait(120);
	DeleteEnemyShotImmediatelyInCircle(ALL,GetCenterX,GetCenterY,600);
	FadeS1(20,200);
	Wait(20);
	Wait(100);
	SetCommonData("StopFish",1);

	Dialogue(1,0);

	WaitEventStep(1);

	SpawnBoss;
	while(EventActive){yield;}
	S1CamTilt(40,15);
	Music(2);

	BeatCircle;
	WaitForBossDead;
	CollectAllItems(5);
	CollectAllSouls(60);
	if(IsFullSoul&&GetCommonDataDefault("Diff",-1)>0){
//	if(true){
		CancelExplosion;
		SpawnBoss;
		WaitForBossDead;
		CollectAllItems(5);
		CollectAllSouls(60);
	}
	ForbidShot2(true);
	Wait(210);
	DeleteDialoguePics(["ender"]);
	Cam4(-15,0,240);
	Wait(240);
	Dialogue(1,1);
	while(EventActive){yield;}
	DisplayResults(1);
	if(fullgame==1){
		FadeOutS1;
	}else{
		StageClear=1;
	}

 }



//=======================================================================================================
//=======================================================================================================
//=======================================================================================================
//=======================================================================================================
//=======================================================================================================
task FadeS1(delay, time){
	overlays1a=255;
	let overdec=255/time;
	loop(time){
		overlays1a-=overdec;
		yield;
	}
}

task BGS1(original,change,t){
	change=change/t;
	let frame=1;
	loop(t){
		s1speed=original+change*frame;
		frame++;
		yield;
	}
}

function E1(let et, let xs, let ys, let vs, let as, let arg1, let arg2, let arg3, let arg4, let arg5){
  SetCommonData("E",et);
  SetCommonData("A2",arg2);
  SetCommonData("A3",arg3);
  SetCommonData("A4",arg4);
  SetCommonData("A5",arg5);
  CreateEnemyFromFile(GPSD~"s1\e_enemy1.txt", xs,ys,vs,as,arg1);
}
function E2(let et, let num, let xs, let ys, let vs, let as, let arg1, let arg2, let arg3, let arg4, let arg5){
  SetCommonData("E",et);
  SetCommonData("A1",arg1);
  SetCommonData("A2",arg2);
  SetCommonData("A3",arg3);
  SetCommonData("A4",arg4);
  SetCommonData("A5",arg5);
  SetCommonData("E2Num",num);
  let numloop=0;
  loop(num){
	CreateEnemyFromFile(GPSD~"s1\e_enemy2.txt", xs,ys,vs,as,(360/num)*numloop);
	numloop++;
  }
}

function S1init(delay){
	SetSoulReq(1);
	StageClear=0;
	LoadGraphic(s1bg1);loop(delay){yield;}
	LoadGraphic(s1bg2);loop(delay){yield;}
	LoadGraphic(s1bg3);loop(delay){yield;}
	LoadGraphic(s1bg4);loop(delay){yield;}
	LoadGraphic(s1bg5);loop(delay){yield;}
	LoadGraphic(s1bg6);loop(delay){yield;}
	LoadGraphic(s1bg7);loop(delay){yield;}
	LoadGraphic(s1title);loop(delay){yield;}
	if(GetCommonDataDefault("SpellPractice",0)==0){
		LoadMusicNew(1);loop(delay){yield;}
		LoadMusicNew(2);loop(delay){yield;}
		CompileEnemyFromFile(GPSD~"s1\e_enemy1.txt");loop(delay){yield;}
		CompileEnemyFromFile(GPSD~"s1\e_enemy2.txt");loop(delay){yield;}
	}
	CreateRenderTarget("s1water",512,512);
	CreateRenderTarget("s1fish",512,512);
	ClearRenderTarget("s1fish");
	CreateRenderTarget("s1sky",512,512);
	ClearRenderTarget("s1sky");
}
function S1final(fullgame){
	DeleteMusicNew(1);
	DeleteMusicNew(2);
	DeleteGraphic(s1bg1);
	DeleteGraphic(s1bg2);
	DeleteGraphic(s1bg3);
	DeleteGraphic(s1bg4);
	DeleteGraphic(s1bg5);
	DeleteGraphic(s1bg6);
	DeleteGraphic(s1bg7);
	DeleteGraphic(s1title);
	DeleteGraphic("s1water");
	DeleteGraphic("s1fish");
	DeleteGraphic("s1sky");
}

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

    StageTitle(wait,GetCenterX,GetCenterY-100,1,0);

    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    

    loop(wait){yield;}
    x=Obj_GetX(obj);
    y=Obj_GetY(obj);
    Obj_SetPosition   (obj, GetCenterX,GetCenterY);
    Obj_SetSpeed      (obj, 0);
    Obj_SetAngle(obj, 0);
    ObjEffect_SetTexture(obj,s1title);
    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);

    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 ,RCS1,RCS1,RCS1);
	ObjEffect_SetVertexColor(obj,1, opacity ,RCS1,RCS1,RCS1);
	ObjEffect_SetVertexColor(obj,2, opacity ,RCS1,RCS1,RCS1);
	ObjEffect_SetVertexColor(obj,3, opacity ,RCS1,RCS1,RCS1);

        yield;

    }
    Obj_Delete(obj);
    function RCS1{return rand(200,255);}
}


	let s1scrolly=0;
	let s1scroll=0;
	let s1cloudspeed=[1.47,1.28,0.86,1.86,1.53,1.66,0.91,2.11,1.92,1.50,1.37,0.61];
	let s1cloudheight=[-4,6,-1,-6,0,8,8,-4,4,-10,2,-5];
	let s1speed=2;

	let i=0;

function S1BGLogic{
	s1scrolly+=1.5*GetTimeSpeed;
	s1scroll+=s1speed*GetTimeSpeed;


	SetRenderTarget("s1water");

	SetRenderState(ALPHA);
	SetColor(255,255,255);
	SetTexture(s1bg1);	//water
	SetGraphicScale(1,1);
	SetAlpha(255);
	SetGraphicAngle(0,0,0);
	SetGraphicRect(0+s1scrolly/3,s1scrolly/5+s1scroll,512+s1scrolly/3,512+s1scrolly/5+s1scroll);
	DrawGraphic(256,256);

	SetGraphicScale(1,-1);
	SetGraphicRect(0-s1scrolly/3,128-s1scrolly/5-s1scroll,512-s1scrolly/3,640-s1scrolly/5-s1scroll);
	SetAlpha(128);
	DrawGraphic(256,256);

	SetGraphicScale(1,1);
	SetGraphicScale(-1,-1);
	SetGraphicRect(0,128-s1scrolly/3-s1scroll,512,640-s1scrolly/3-s1scroll);
	SetAlpha(64);
	DrawGraphic(256,256);


	SetTexture(s1bg3);	//moon reflection
	SetGraphicScale(3,-6);
	SetGraphicRect(0,1,59,127);
	SetColor(155,155,155);
	SetRenderState(ADD);
	SetGraphicAngle(0,0,5);
	DrawGraphic(170,230);
	SetRenderState(ALPHA);

	SetTexture(s1bg2);	//gradient
	SetAlpha(230);
	SetColor(0,0,0);
	SetGraphicRect(0,0,500,128);
	SetGraphicScale(1,500/128);
	SetGraphicAngle(0,0,0);
	DrawGraphic(256,256);

	SetTexture(stgwhite);
	SetRenderState(ADD);
	SetGraphicRect(0,0,512,512);
	DrawGraphic(256,256);
	SetRenderState(ALPHA);
	SetColor(255,255,255);

	SetRenderTarget("DEFAULT");

	ClearRenderTarget("s1fish");
	ClearRenderTarget("s1sky");
}

let overlays1a=0;

function S1BG{

	//cam1 = height
	//cam2 = zoom
	//cam3 = -1: clouds, 1: fog
	//cam4 = roll

	SetViewTo(0+ScreenShakeX,0+cam2,25+cam1+ScreenShakeY);
	SetViewFrom(cam1+400,cam4+90,-78+cam3);

	SetColor(255,255,255);

	SetTexture(stgwhite);
	SetGraphicRect(0,0,400,500);
	SetGraphicScale(1,1);
	SetGraphicAngle(0,0,0);
	SetAlpha(255);
//	SetColor(0,0,20);
	SetColor(10,0,20);
	SetRenderState(ALPHA);
	DrawGraphic(GetCenterX,GetCenterY);

	SetTexture(s1bg2);	//gradient
	SetGraphicRect(0,0,400,128);
	SetGraphicScale(1,2);
	SetGraphicAngle(0,0,0);
	SetAlpha(255);
//	SetColor(0,0,120);
	SetColor(60,0,120);
	DrawGraphic(GetCenterX,GetCenterY-128);


	SetTexture(s1bg5);	//stars
	SetGraphicScale(2,2);
	SetColor(255,255,255);

	SetGraphicAngle(90,0,0);
	let starscroll=s1scrolly/25;
	SetGraphicRect(0-trunc(starscroll),0,500-trunc(starscroll),500);
	DrawGraphic3D(0+starscroll-trunc(starscroll),1100,0);

	SetGraphicAngle(90,0,90);
	starscroll=s1scrolly/15;
	SetGraphicRect(0,0+trunc(starscroll),500,500+trunc(starscroll));
	DrawGraphic3D(0+starscroll-trunc(starscroll),900,0);

	SetTexture("s1sky");	//sky objects
	SetGraphicScale(2,-2);
	SetGraphicRect(0,0,512,512);
	SetColor(255,255,255);
	SetGraphicAngle(90,0,0);
	DrawGraphic3D(0,1100,150);

	SetTexture(s1bg3);	//moon
	SetGraphicRect(60,1,127,65);
	SetGraphicAngle(90,0,0);
	SetColor(255,255,225);
	SetGraphicScale(2.5,-2.5);
	DrawGraphic3D(-180,800,200);

	SetTexture(s1bg4);	//mountains
	SetGraphicRect(0,1,512,128);
	SetGraphicScale(2,-2);
	SetGraphicAngle(90,0,0);
	SetColor(255,255,255);
	DrawGraphic3D(0, 250,120);

	SetTexture("s1water");
	SetAlpha(255);
	SetGraphicScale(1,1);
	SetGraphicAngle(0,0,0);
	i=0;
	let variable;	//temporary variable
	loop(10){
		variable=i*50;	//scroll
		SetGraphicRect(0,0+variable,500,50+variable);
		DrawGraphic3D(0+sin(variable+s1scrolly)*5,variable-250+25,0);
		i++;
	}

	SetTexture(s1bg4);	//mountains reflection
	SetGraphicScale(2,2);
	SetGraphicAngle(90,0,0);
	SetAlpha(70);
	i=0;
	loop(4){
		SetGraphicRect(0,1+i,512,31+i);
		DrawGraphic3D(sin(-i*50+s1scrolly)*5, 250,-215+i*2);
		i+=30;
	}


	SetTexture("s1fish");
	SetAlpha(255);
	SetGraphicScale(1,-1);
	SetGraphicAngle(0,0,0);
	SetGraphicRect(0,0,512,512);
	SetAlpha(100);
	DrawGraphic3D(0,80,-80);


	SetTexture(s1bg6);	//clouds
	SetColor(255,255,255);

	if(cam3<0){	//clouds
		SetGraphicAngle(0,0,90);
		SetGraphicScale(4,2);
		SetGraphicRect(0,128,256,256);
		i=0;
		loop(12){
			variable=((s1scrolly+1000)*s1cloudspeed[i]*2+s1scroll*5)%1600;	//z position
			SetAlpha(lower((variable/4),150)*-cam3);
			DrawGraphic3D((i-5.5)*30,-variable+800,170+s1cloudheight[i]*5);
			i++;
		}
	}
	if(cam3>0){
		SetGraphicAngle(90,0,0);
		SetGraphicScale(2,1);
		SetGraphicRect(0,128,256,256);
		i=0;
		loop(12){
			variable=((s1scrolly+1000)*s1cloudspeed[i]*0.5)%800;	//x position
//			SetAlpha(lower((variable/4),150)*cam3);
			SetAlpha(255*cam3);
			DrawGraphic3D(variable-400,s1cloudheight[i],i*40-200);

			variable=((s1scrolly+1000)*s1cloudspeed[i]*0.5+400)%800;	//x position

			DrawGraphic3D(variable-400,s1cloudheight[i],i*40-180);
			i++;
		}		
	}
	SetTexture(stgwhite);
	SetGraphicRect(0,0,400,500);
	SetGraphicScale(1,1);
	SetGraphicAngle(0,0,0);
	SetAlpha(overlays1a);
	DrawGraphic(GetCenterX,GetCenterY);
}

task Stage1ZoomIn{
	//cam1 = height
	//cam2 = zoom
	//cam3 = -1: clouds, 1: fog
	//cam4 = roll

//	cam1=-70;
//	cam2=140;
	cam1=-50;
	cam2=100;
	cam3=1;

	Wait(180);
	Cam1(-50,0,500);
	Wait(180);
	Cam2(100,0,500);
	Wait(440);
//	Cam3(1,-1,400);
	Wait(130);
	BGS1(0,3,100);
	Cam3(1,-1,100);

	Wait(120);
	let lasteventtime=800;
	SetCommonData("StopFish",0);
	while(GetCommonData("StopFish")==0){
		if(rand_int(0,300/s1speed)==0){
			S1Water(0,rand(-220,220),0);
		}
		if(rand_int(0,1500)==0){
			loop(rand_int(2,6)){
				S1Sky(0,rand(100,400),rand(-290,-180),rand(0,180));	//shooting star
			}
		}
		if(rand_int(0,2000)==0){
			S1Sky(1,(rand_int(0,1)-0.5)*2,rand(-50,-50),0);	//satellite
		}
		if(rand_int(0,6000)==0&&lasteventtime>1200){
			Wait(300);
			S1Event(rand_int(0,4));
			lasteventtime=0;	
		}
		lasteventtime++;
		yield;
		while(timefrozen){yield;}
	}
	DeleteCommonData("StopFish");


task S1Event(num){
	if(num==0){
		loop(20){
			S1Sky(0,rand(-250,400),rand(-250,-150),0);	//meteor shower
			Wait(10);
		}
	}
	if(num==1||num==2){
		let x=rand(-150,150);
		loop(25){
			S1Water(num-1,x+rand(-80,80),0);	//fish
			Wait(20);
		}
	}
	if(num==3){
		Cam3(-1,0.8,400);	//fog
		Wait(800);
		Cam3(0.8,-1,400);
	}
	if(num==4){
		S1Sky(2,1,30,600);	//yoofoh
	}

}
}	//nested, not a typo


function FadeOutS1{
	//transition effect in story mode
	InitStage(2,2,-1);
	s1speed=1;
	cam3=0;
	let frame=0;
	let cam1s=cam1;
	let cam2s=cam2;
	Wait(40);
	BGS1(1,12,110);
	Wait(40);

	let obj=InitEffect(GetClipMinX,GetClipMinY,stgwhite,4,SUBTRACT,1);
	ObjEffect_SetXYRect(obj,0,0,GetClipMaxX-GetClipMinX,GetClipMaxY-GetClipMinY);
	let alpha=0;
	let alphaadd=255/80;
	loop(80){
		alpha+=alphaadd;
		ObjEffect_SetColor(obj,4,255,alpha,alpha,alpha);
		cam2=(cos(frame*(90/80)+180)+1)*(450-cam2s)+cam2s;
		cam1=(cos(frame*(90/80)+180)+1)*(-40-cam1s)+cam1s;
		frame++;
		yield;
	}
	StageClear=1;
	Obj_Delete(obj);
}

function S1Alt(num){
	
	//alternate start points for spell practice
	ResetCam;
	if(num==0){
		cam1=0;
		cam2=0;
		cam3=-1;
		cam4=0;
		s1speed=3;
	}
	if(num==1){
		cam1=-20;
		cam2=150;
		cam3=-1;
		cam4=-15;
		s1speed=6;
	}
}

task S1CamTilt(time,angle){
	let frame=0;
	loop(time){
		frame++;
		cam4=(cos((frame/time)*90)-1)*angle;
		yield;
	}
	ShakeScreen(1.5,60);
}
task S1Water(type,x,delay){	//makes a fishie
	Wait(delay);
	let y=0;
	let alpha=0;
	let v=0;
	let angle=90;
	let xv=0;
	let yv=1;
	let dir=0;
	let animationframe=0;
	let xsize=rand(0.6,1);
	let ysize=rand(0.6,1);
	let prevtarget="";
	while(y<512){
		if(alpha<255){
			alpha+=5;
		}
		prevtarget=SetRenderTarget("s1fish");
		SetTexture(s1bg7);
		if(type==0){
			SetGraphicScale(0.4*xsize,0.7*ysize);
			if(v<1){
				S1WaterRect(0,0);
			}else{
				S1WaterRect(0,1);
			}
		}else{
			SetGraphicScale(0.7*xsize,0.7*ysize);
			S1WaterRect(0,2+trunc(animationframe/8)%2);
		}
		SetAlpha(alpha);
		SetGraphicAngle(0,0,angle-90);
		SetRenderState(ALPHA);
		SetColor(255,255,255);
		DrawGraphic(x+256,y);
		SetRenderTarget(prevtarget);
		if(type==0){
			if(v==0&&rand_int(0,50)==0){
				v=rand(3,5);
				angle=rand(60,120);
				xv=cos(angle);
				yv=sin(angle);
			}else if(v>0){
				v-=0.05;
			}
			if(v<0){
				v=0;
			}
		}else{
			if(animationframe%2==0){
				if(rand_int(0,30)==0||animationframe==0){
					dir=rand(-2,2);
					v=2;
				}
				angle=center(45,angle+dir,135);
				xv=cos(angle);
				yv=sin(angle);
			}
			animationframe++;
		}
		x+=xv*v;
		y+=yv*v+s1speed*GetTimeSpeed*0.5;
		yield;
		while(timefrozen){yield;}
	}
}
task S1Sky(type,x,y,delay){
	Wait(delay);
	let v=rand(2,4);
	let alpha=0;
	let frame=0;
	let maxalpha=rand(0.7,1);
	let dir=x;
	if(type==1||type==2){
		x=-256*dir;
		v=rand(0.8,1.1);
	}
	let prevtarget="";
	while(y<512){
		if(type==0){		//comet
			alpha+=5;
			if(alpha>255){alpha=255;}
			prevtarget=SetRenderTarget("s1sky");
			SetTexture(s1bg7);
			SetGraphicRect(64,0,96,32);
			SetAlpha(alpha*rand(0.7,1)*maxalpha);
			SetGraphicAngle(0,0,0);
			SetRenderState(ALPHA);
			SetColor(255,255,255);
			SetGraphicScale(2,2);
			DrawGraphic(256+x,256+y);
			SetRenderTarget(prevtarget);
			y+=v;
			x-=v;
		}else if(type==1||type==2){	// satellite or UFO
			if(trunc(frame/5)%10==0||type==2){
				prevtarget=SetRenderTarget("s1sky");
				SetTexture(s1bg7);
				SetGraphicRect(32+type*32,32,64+type*32,64);
				SetAlpha(255);
				if(type==1){
					SetGraphicAngle(0,0,0);
				}else{
					SetGraphicAngle(0,0,sin(frame*4)*5);
				}
				SetRenderState(ALPHA);
				SetColor(255,255,255);
				SetGraphicScale(1,1);
				DrawGraphic(256+x,256+y);
				SetRenderTarget(prevtarget);
			}
			x+=(dir/(4-type))*v;
			if(x<-300||x>300){
				y=512;
			}
		}
		frame++;
		yield;
		while(timefrozen){yield;}
	}
}
function S1WaterRect(row,column){
	SetGraphicRect(row*64,column*64,row*64+64,column*64+64);
}