let commonspellbg;
let drawbackground=true;	//set to false to disable stage background drawing for one frame only
let bgspecial=0;

function BGOpacity{return GetCommonDataDefault("SpellBGOpacity",0);}
function SetBGOpacity(number){SetCommonData("SpellBGOpacity",number);}
function RunBGs{
	commonspellbg=GetCommonDataDefault("SpellBG",0);
	if(BGOpacity>0){
		if(absolute(commonspellbg)==1){EnderBG;}
		if(absolute(commonspellbg)==2){EmpeghBG;}
		if(absolute(commonspellbg)==3){OverloadBG;}
		if(absolute(commonspellbg)==4){YumemiBG;}
		if(absolute(commonspellbg)==5){YukariBG;}
		if(absolute(commonspellbg)==20){IddyeaBG;}
		if(absolute(commonspellbg)==21){PatchyBG;}
		if(absolute(commonspellbg)==22){AliceBG;}

	}
}
let bgfadespeedin= [0, 1,1,1,0.25,1, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,1, 1,1];
let bgfadespeedout=[0, 1,1,1,0.5 ,1, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,1, 1,1];
function RunBGLogic{
	commonspellbg=GetCommonDataDefault("SpellBG",0);

	if(commonspellbg>0){
		if(BGOpacity<1){
			SetBGOpacity(lower(BGOpacity+0.02*bgfadespeedin[commonspellbg],1));
		}
	}else if(BGOpacity>0){
		SetBGOpacity(upper(BGOpacity-0.02*bgfadespeedout[absolute(commonspellbg)],0));
	}
	if(BGOpacity>0){
		if(absolute(commonspellbg)==1){EnderBGLogic;}
		if(absolute(commonspellbg)==2){EmpeghBGLogic;}
		if(absolute(commonspellbg)==3){OverloadBGLogic;}
		if(absolute(commonspellbg)==4){YumemiBGLogic;}
		if(absolute(commonspellbg)==5){YukariBGLogic;}
		if(absolute(commonspellbg)==20){IddyeaBGLogic;}
		if(absolute(commonspellbg)==21){PatchyBGLogic;}
		if(absolute(commonspellbg)==22){AliceBGLogic;}
		if(absolute(commonspellbg)!=bgspecial){
			if(bgspecial==0){
				if(absolute(commonspellbg)==22){SpecialBGAlice;}
			}
			bgspecial=absolute(commonspellbg);
		}
	}else{
		commonspellbg=0;
		bgspecial=0;
	}
}



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


	let imgEndBG1=GPSD~"img\white.png";
	let imgEndBG2=GPSD~"img\circuit.png";
	let imgEndBG3=GPSD~"img\grid.png";
	let imgEndBG4=GPSD~"img\enderbg.png";
	let imgEndBG5=GPSD~"img\gradient.png";
	let BGscrollx=0;
	let BGscrolly=0;
	let BGscrollx2=0;
	let BGscrolly2=0;
	let BGscrollx3=0;
	let BGscrolly3=0;
sub EnderBGLogic{
	BGscrollx+=1;
	BGscrolly+=0.5;
	if(BGscrollx>=400){BGscrollx=BGscrollx-400;}
	if(BGscrolly>=300){BGscrolly=BGscrolly-300;}
	if(BGscrollx<0){BGscrollx=BGscrollx+400;}
	if(BGscrolly<0){BGscrolly=BGscrolly+300;}

	BGscrollx2+=-0.5;
	BGscrolly2+=1;
	if(BGscrollx2>=400){BGscrollx2=BGscrollx2-400;}
	if(BGscrolly2>=300){BGscrolly2=BGscrolly2-300;}
	if(BGscrollx2<0){BGscrollx2=BGscrollx2+400;}
	if(BGscrolly2<0){BGscrolly2=BGscrolly2+300;}

	BGscrollx3+=0.5;
	BGscrolly3+=-0.5;
	if(BGscrollx3>=256){BGscrollx3=BGscrollx3-256;}
	if(BGscrolly3>=256){BGscrolly3=BGscrolly3-256;}
	if(BGscrollx3<0){BGscrollx3=BGscrollx3+256;}
	if(BGscrolly3<0){BGscrolly3=BGscrolly3+256;}

}
sub EnderBG {
        SetTexture(imgEndBG1);
	SetGraphicAngle(0,0,0);
	SetGraphicRect(0,0,400,600);
	SetGraphicScale(1,1);
	SetAlpha(BGOpacity*255);
	SetColor(0,0,0);
    	DrawGraphic(GetCenterX, GetCenterY);

        SetTexture(imgEndBG3);
	SetAlpha(BGOpacity*255);

	BGscrolly*=(360/150);

	SetColor(cos(BGscrolly)*120+120,sin(BGscrolly)*120+120,0);
	SetGraphicRect(0+BGscrollx3, 0+BGscrolly3,400+BGscrollx3, 600+BGscrolly3);
    	DrawGraphic(GetCenterX+ScreenShakeX, GetCenterY+ScreenShakeY);

	SetRenderState(ADD);
	SetColor((cos(BGscrolly+240)*120+120)*BGOpacity,0,(sin(BGscrolly+240)*120+120)*BGOpacity);
	SetGraphicRect(0-BGscrollx3, 300+BGscrolly3,400-BGscrollx3, 900+BGscrolly3);
    	DrawGraphic(GetCenterX+ScreenShakeX, GetCenterY+ScreenShakeY);

	BGscrolly/=(360/150);

        SetTexture(imgEndBG2);
	SetColor(0,160*BGOpacity,200*BGOpacity);

	SetGraphicRect(0+BGscrollx, 0+BGscrolly,400+BGscrollx, 600+BGscrolly);
    	DrawGraphic(GetCenterX+ScreenShakeX, GetCenterY+ScreenShakeY);

	SetGraphicScale(-1,-1);
	SetColor(0,200*BGOpacity,80*BGOpacity);
	SetGraphicRect(0+BGscrollx2, 0+BGscrolly2,400+BGscrollx2, 600+BGscrolly2);
    	DrawGraphic(GetCenterX+ScreenShakeX, GetCenterY+ScreenShakeY);

	SetRenderState(SUBTRACT);

	SetTexture(imgEndBG4);
	SetColor(180*BGOpacity,180*BGOpacity,180*BGOpacity);
	SetGraphicScale(2,2);
	SetGraphicRect(0,0+BGscrollx3*13,400/2,600/2+BGscrollx3*13);
	DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY);

	SetGraphicScale(-2,1);
	SetGraphicRect(0,0+BGscrollx3*7,400/2,600/1+BGscrollx3*7);
	DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);

	SetRenderState(ALPHA);
	SetTexture(imgEndBG1);
	SetColor(0,0,0);
	SetGraphicRect(0,0,400,600);
	SetAlpha(100*BGOpacity);
	SetGraphicScale(1,1);
	DrawGraphic(GetCenterX,GetCenterY);

	SetTexture(imgEndBG5);
	SetGraphicRect(0,0,380,128);
	SetAlpha(120*BGOpacity);
	SetGraphicScale(1,3.5);
	DrawGraphic(GetCenterX,GetCenterY);

	SetColor(255,255,255);
}

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

	let imgEmBG1=GPSD~"img\empbg1.png";
	let imgEmBG2=GPSD~"img\staff.png";
	let imgEmBG3=GPSD~"img\empnotes.png";
	let embgscroll=0;
	let emheight=[12,5,10,9,12,4,8,3,12,10];
	let emyv=[0,0,0,0,0,0,0,0,0,0];
	let emrand=[0,0,0,0,0];
	let lastbeat=0;
sub EmpeghBGLogic{
	embgscroll++;
	emrand[rand_int(0,4)]=rand_int(3,6);
	idrawlogic=rand_int(0,9);
	if((lastbeat!=beatnum&&beatnum!=-1)||(beatnum==-1&&embgscroll%24==0)){
		if(beatnum==0||(beatnum==-1&&embgscroll%(24*4)==0)){
			idrawlogic=0;
			loop(10){
				emheight[idrawlogic]=center(0,emheight[idrawlogic]+emrand[idrawlogic%5],13);
				emyv[idrawlogic]=0;
				idrawlogic++;
			}
		}else{
			loop(6){
				idrawlogic=trunc(emrand[idrawlogic%5]+emheight[(idrawlogic+1)%10]*50)%10;
				emheight[idrawlogic]=center(6,emheight[idrawlogic]+emrand[idrawlogic%5],13);
				emyv[idrawlogic]=0;
			}
		}
		lastbeat=beatnum;
	}
	idrawlogic=0;
	loop(10){
		emyv[idrawlogic]=emyv[idrawlogic]-0.005;
		emheight[idrawlogic]=emheight[idrawlogic]+emyv[idrawlogic];
		if(emheight[idrawlogic]<0){emheight[idrawlogic]=0;}
		idrawlogic++;
	}
}
sub EmpeghBG {
	let xsb=sin(embgscroll*0.5)*50;
	let ysb=cos(embgscroll*0.5)*50;

	ResetColor;
        SetTexture(imgEmBG1);
	SetGraphicRect(0,0,256,256);
	SetGraphicScale(3,3);
	SetAlpha(BGOpacity*255);
	SetGraphicAngle(0,0,embgscroll/8);
    	DrawGraphic(GetCenterX+ScreenShakeX, GetCenterY-100+ScreenShakeY);



	SetAlpha(85*BGOpacity);
	SetGraphicAngle(0,0,embgscroll/8+240);
    	DrawGraphic(GetCenterX+86+ScreenShakeX, GetCenterY+50+ScreenShakeY);
	SetGraphicAngle(0,0,embgscroll/8+120);
    	DrawGraphic(GetCenterX-86+ScreenShakeX, GetCenterY+50+ScreenShakeY);

	SetAlpha(BGOpacity*100);
	SetTexture(imgEmBG2);
	SetGraphicRect(0,0,800,95);
	SetGraphicScale(1,1);
	SetGraphicAngle(0,0,-10);
	SetColor(Rainbow(0,embgscroll/5)*255,Rainbow(1,embgscroll/5)*255,Rainbow(2,embgscroll/5)*255);
	DrawGraphic(GetCenterX+ScreenShakeX,170+ScreenShakeY);

	SetTexture(imgEmBG3);

	SetGraphicScale(1.5,1.5);

	SetAlpha(BGOpacity*200);
	SetGraphicAngle(0,0,15);
	SetGraphicRect(0-embgscroll/5,64,300-embgscroll/5,128);
	ResetColor;DrawGraphic(GetCenterX+ScreenShakeX,400+ScreenShakeY);
	SetGraphicRect(0-embgscroll/5,128,300-embgscroll/5,192);
	SetColorR(0);DrawGraphic(GetCenterX+ScreenShakeX,400+ScreenShakeY);

	SetGraphicAngle(0,0,-40);
	SetGraphicRect(0-embgscroll/7,64,400-embgscroll/7,128);
	ResetColor;DrawGraphic(GetCenterX+ScreenShakeX,140+ScreenShakeY);
	SetGraphicRect(0-embgscroll/7,128,400-embgscroll/7,192);
	SetColorR(120);DrawGraphic(GetCenterX+ScreenShakeX,140+ScreenShakeY);

	SetGraphicAngle(0,0,40);
	SetGraphicRect(0-embgscroll/10,64,400-embgscroll/10,128);
	ResetColor;DrawGraphic(GetCenterX+ScreenShakeX,250+ScreenShakeY);
	SetGraphicRect(0-embgscroll/10,128,400-embgscroll/10,192);
	SetColorR(240);DrawGraphic(GetCenterX+ScreenShakeX,250+ScreenShakeY);

	SetAlpha(BGOpacity*150);
	SetColor(255,255,255);
	SetGraphicRect(0+embgscroll/7,0,256+embgscroll/7,64);
	SetGraphicScale(2,2);
	SetGraphicAngle(0,0,-10);
	DrawGraphic(GetCenterX+ScreenShakeX,156+ScreenShakeY);

	SetGraphicScale(1,1);
	SetAlpha(200*BGOpacity);
	SetGraphicAngle(0,0,-90);

	idrawlogic=0;
	loop(10){
		SetGraphicRect(0,210,trunc(emheight[idrawlogic])*20+1,256);
		DrawGraphic(trunc(GetClipMinX+12+idrawlogic*40),GetClipMaxY-trunc(emheight[idrawlogic])*10);
		idrawlogic++;
	}

}
function SetColorR(offset){
	SetColor(Rainbow(0,embgscroll/3+offset)*255,Rainbow(1,embgscroll/3+offset)*255,Rainbow(2,embgscroll/3+offset)*255);
}




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

	let imgOvBG1=GPSD~"img\overloadbg1.png";
	let imgOvBG2=GPSD~"img\overloadbg2.png";
	let imgOvBG3=GPSD~"img\overloadbg3.png";

	let lightningt=[0,12,24,36,48,60,72,84,96,108];	//time
	let lightningo=[Ro,Ro,Ro,Ro,Ro,Ro,Ro,Ro,Ro,Ro];	//graphic offset
	let lightningr=[Rr,Rr,Rr,Rr,Rr,Rr,Rr,Rr,Rr,Rr];	//rotation
	let lightnings=[Rs,Rs,Rs,Rs,Rs,Rs,Rs,Rs,Rs,Rs];	//rotation speed
	let ovcolorbg=[[0,1,1],[1,0,0],[0,1,0],[0,0.5,1],[1,0,1],[0,1,0.5]];
	let ovrotbg=[[0,30,50],[20,40,90],[60,0,20],[10,-20,30],[10,-20,-50],[-40,0,-90]];
	let ovbgscroll=0;

	function Ro{return rand_int(0,256);}
	function Rr{return rand_int(0,360);}
	function Rs{return rand(-1,1);}

sub OverloadBGLogic{
	ovbgscroll++;
	i=0;
	loop(10){
		lightningt[i]=lightningt[i]+2;
		lightningr[i]=lightningr[i]+lightnings[i]*2;
		lightningo[i]=lightningo[i]-(i%2-0.5)*10;
		if(lightningt[i]>=120){
			lightningt[i]=0;
			lightningo[i]=Ro;
			lightningr[i]=Rr;
			lightnings[i]=Rs;
		}
		i++;
	}
}

sub OverloadBG {
	let i=0;
	SetTexture(imgOvBG2);
	SetRenderState(ALPHA);
	SetGraphicAngle(0,0,0);
	SetAlpha(255*BGOpacity);
	SetGraphicRect(0+ovbgscroll/1.5,0+ovbgscroll/3,500+ovbgscroll/1.5,500+ovbgscroll/3);
	SetColor(255,100,100);
	SetGraphicScale(1,1);
    	DrawGraphic(GetCenterX+ScreenShakeX, GetCenterY+ScreenShakeY);

	SetAlpha(128*BGOpacity);
	SetGraphicAngle(0,0,90);
	SetGraphicScale(-1,1);
	SetGraphicRect(0-ovbgscroll/3,0+ovbgscroll/1.5,500-ovbgscroll/3,500+ovbgscroll/1.5);
	SetColor(255,255,100);
    	DrawGraphic(GetCenterX+ScreenShakeX, GetCenterY+ScreenShakeY);

	SetGraphicScale(1.5,1.5);


	SetTexture(imgOvBG1);
	SetRenderState(ADD);
	loop(10){
		idrawlogic=sin(lightningt[i]*(180/120))*80*BGOpacity;
		SetColor(idrawlogic,idrawlogic,idrawlogic);
		SetGraphicRect(0,0+lightningo[i],30,400+lightningo[i]);
		SetGraphicAngle(0,0,lightningr[i]);
		DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);
		//DrawGraphic(GetEnemyX,GetEnemyY);
		i++;
	}	

	SetTexture(imgOvBG3);
	SetRenderState(ALPHA);
	SetGraphicAngle(0,0,0);
	SetGraphicRect(0-ovbgscroll,0+ovbgscroll*3,800-ovbgscroll,800+ovbgscroll*3);
	SetAlpha(BGOpacity*50);
	SetGraphicScale(1,1);

	i=0;
	loop(6){
		//SetColor(ovcolorbg[i][0]*64+128,ovcolorbg[i][1]*64+128,ovcolorbg[i][2]*64+128);
		SetColor(ovcolorbg[i][0]*96+128,ovcolorbg[i][1]*96+128,ovcolorbg[i][2]*96+128);
		SetGraphicAngle(ovrotbg[i][0],ovrotbg[i][1],ovrotbg[i][2]);
		DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);
		i++;
	}
}

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

    let imgIddyeaBG1=GPSD~"img\iddyeabg1.png";
    let imgIddyeaBG2=GPSD~"img\iddyeabg2.png";
    let imgIddyeaBG3=GPSD~"img\iddyeabg3.png";
    let imgIddyeaBG4=GPSD~"img\gradient.png";
    let imgIddyeaBG5=GPSD~"img\white.png";

let idrawlogic=0;
sub IddyeaBGLogic{idrawlogic++;}

sub IddyeaBG{
	SetTexture(imgIddyeaBG3);
	SetGraphicRect(0+sin(idrawlogic)*40,0-idrawlogic,400+sin(idrawlogic)*40,500-idrawlogic);
	SetGraphicAngle(0,0,0);
	SetGraphicScale(1,1);
	SetColor(160,160,160);
	SetRenderState(ALPHA);
	SetAlpha(BGOpacity*255);
	DrawGraphic(GetCenterX,GetCenterY);

	SetAlpha(128*BGOpacity);

	SetGraphicScale(-1,-1);
	SetGraphicRect(0+sin(idrawlogic*1.3)*40,0+idrawlogic*1.3,400+sin(idrawlogic*1.2)*40,500+idrawlogic*1.8);
	DrawGraphic(GetCenterX,GetCenterY);
	SetColor(255,255,255);

	SetRenderState(ADD);
	SetTexture(imgIddyeaBG1);
	SetGraphicRect(0,0,256,256);

	SetColor(125*BGOpacity,125*BGOpacity,125*BGOpacity);

	let kk=0;
	loop(6){
		SetGraphicScale(1.2+sin(kk*(360/6)+idrawlogic*0.5)*0.3,1.2+sin(kk*(360/6)+idrawlogic*0.5)*0.3);
		SetGraphicAngle(0,0,-idrawlogic*2+90*kk);
		DrawGraphic(GetCenterX+200*(kk%2-0.5),-128+(700-(idrawlogic+kk*(700/6)+700)%700));
		kk++;
	}

	SetTexture(imgIddyeaBG2);
	SetColor(180*BGOpacity,180*BGOpacity,180*BGOpacity);
	SetGraphicRect(0+idrawlogic*0.7,0-idrawlogic/4,150+idrawlogic*0.7,500-idrawlogic/4);
	SetGraphicScale(3,1);
	SetGraphicAngle(0,0,0);
	DrawGraphic(GetCenterX,GetCenterY);

	SetRenderState(ALPHA);
	SetTexture(imgIddyeaBG5);
	SetGraphicRect(0,0,400,500);
	SetGraphicScale(1,1);
	SetColor(0,0,0);
	SetAlpha(40*BGOpacity);
	DrawGraphic(GetCenterX,GetCenterY);

	SetTexture(imgIddyeaBG4);
	SetGraphicRect(0,0,400,256);
	SetGraphicScale(1,4);
	SetColor(0,0,0);
	SetAlpha(100*BGOpacity);
	DrawGraphic(GetCenterX,470);
}

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

let imgYumemiBG1=GPSD~"img\yumemibg1.png";
let imgYumemiBG2=GPSD~"img\yumemibg2.png";
let imgYumemiBG3=GPSD~"img\yumemibg3.png";
let imgYumemiBG4=GPSD~"img\yumemibg4.png";

let yumemibgscroll=0;
let yumemik;
let yumemiradial=[0,45,90,135,180,225,270,315];
let yumemiradialmult=[1,1.5,2.1,2.4,1.4,3.2,1.7,2.7];

let yumemisqx=[50,	280,	270,	370,	80,	120,	440,	460,	420, 	150,	260, 	150];
let yumemisqy=[50,	100,	300,	400,	270,	430,	0,	300,	190,	30,	420, 	130];
let yumemisqt=[3,	1,	2,	0,	0,	1,	2,	3,	0, 	2, 	3, 	1];
let yumemisqr=[-2,	1.3,	-1.1,	1.8,	-1.2,	1,	-0.9,	1.6,	-2.1,	2.5, 	-2.4, 	2.6];
let yumemisqs=[0.6,	0.8,	1,	0.6,	1,	0.9,	1,	0.7,	0.5, 	0.3,	0.25, 	0.4];

let yugraphicrect=[[1,1,128,128],[129,1,256,128],[1,129,128,256],[129,129,256,256]];

sub YumemiBGLogic{
	yumemibgscroll++;
	yumemik=0;
	loop(8){
		yumemiradial[yumemik]=yumemiradial[yumemik]+(sin((yumemibgscroll+2000)*yumemiradialmult[yumemik]*0.2)*2);
		yumemik++;
	}
	if(BGOpacity>0.56){
		drawbackground=false;
	}
}
let yumemianimationframe;

sub YumemiBG{
	yumemianimationframe=BGOpacity*200;

	//background
	SetTexture(imgYumemiBG4);
	SetColor(255,255,255);
	SetGraphicAngle(0,0,0);
	SetGraphicScale(1,1);
	yumemik=lower(BGOpacity*450,255);
	SetAlpha(yumemik);
	SetGraphicRect(0-ScreenShakeY,0+yumemibgscroll*5-ScreenShakeX,400-ScreenShakeY,500+yumemibgscroll*5-ScreenShakeX);
	DrawGraphic(GetCenterX,GetCenterY);
	SetGraphicScale(1,-1);
	SetAlpha(yumemik/2);
	SetGraphicRect(128-ScreenShakeY,0-yumemibgscroll*4-ScreenShakeX,528-ScreenShakeY,500-yumemibgscroll*4-ScreenShakeX);
	DrawGraphic(GetCenterX,GetCenterY);

	//squares
	SetTexture(imgYumemiBG2);
	if(BGOpacity<1){
		yumemik=0;
		loop(12){
//			SetAlpha(17*(center(30,yumemianimationframe,45)-30));
			SetAlpha(6.375*(center(yumemik*15,yumemianimationframe,40+yumemik*15)-(yumemik*15)));
			SetGraphicRect(yugraphicrect[yumemisqt[yumemik]][0],yugraphicrect[yumemisqt[yumemik]][1],yugraphicrect[yumemisqt[yumemik]][2],yugraphicrect[yumemisqt[yumemik]][3]);
			SetGraphicAngle(0,0,yumemibgscroll*yumemisqr[yumemik]);
			SetGraphicScale(yumemisqs[yumemik],yumemisqs[yumemik]);
			DrawGraphic(yumemisqx[yumemik]+ScreenShakeX,yumemisqy[yumemik]+ScreenShakeY);
			yumemik++;
		}
	}else{
		SetAlpha(255);
		yumemik=0;
		loop(12){
			SetGraphicRect(yugraphicrect[yumemisqt[yumemik]][0],yugraphicrect[yumemisqt[yumemik]][1],yugraphicrect[yumemisqt[yumemik]][2],yugraphicrect[yumemisqt[yumemik]][3]);
			SetGraphicAngle(0,0,yumemibgscroll*yumemisqr[yumemik]);
			SetGraphicScale(yumemisqs[yumemik],yumemisqs[yumemik]);
			DrawGraphic(yumemisqx[yumemik]+ScreenShakeX,yumemisqy[yumemik]+ScreenShakeY);
			yumemik++;
		}
	}

	SetAlpha(BGOpacity*255);
	SetGraphicScale(1.5,1.5);

	//first triangle
	SetTexture(imgYumemiBG1);
	SetGraphicRect(258,2,511,255);
	SetGraphicAngle(0,0,0);

	if(GetCommonDataDefault("SpellBG",0)>0){
		SetGraphicScale(6.5-lower(yumemianimationframe,30)/6,6.5-lower(yumemianimationframe,30)/6);
		SetAlpha(8.5*lower(yumemianimationframe,30));
	}
	DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);

	//second triangle
	SetGraphicRect(2,2,255,255);
	if(GetCommonDataDefault("SpellBG",0)>0){
		SetGraphicScale(6.5-(center(30,yumemianimationframe,60)-30)/6,(6.5-(center(30,yumemianimationframe,60)-30)/6));
		SetAlpha(8.5*(center(30,yumemianimationframe,60)-30));
	}
	DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);


	//radials
	SetGraphicRect(257,257,512,512);
	SetGraphicScale(1.09,1.09);

	if(GetCommonDataDefault("SpellBG",0)>0){
		SetAlpha(1.3*(center(60,yumemianimationframe,120)-60));
	}else{
		SetAlpha(BGOpacity*78);
	}

	yumemik=0;
	loop(8){
		SetGraphicAngle(0,0,yumemiradial[yumemik]);
		DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);
		yumemik++;
	}


	//clock
	SetGraphicRect(1,257,256,512);
	SetGraphicAngle(0,0,0);
	SetRenderState(ADD);
	//SetGraphicScale(6.5-(center(60,yumemianimationframe,120)-60)/12,(6.5-(center(60,yumemianimationframe,120)-60)/12));
	SetGraphicScale(1.5,1.5);

	if(GetCommonDataDefault("SpellBG",0)>0){
		yumemik=4.25*(center(60,yumemianimationframe,120)-60);
	}else{
		yumemik=BGOpacity*255;
	}
	SetColor(yumemik,yumemik,yumemik);
	DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);

	

	//overlay
	SetTexture(imgYumemiBG3);
	SetRenderState(ALPHA);
	SetGraphicRect(0,0+yumemibgscroll*3,500,550+yumemibgscroll*3);
	SetColor(255,255,255);
	SetGraphicScale(1,1);
	if(GetCommonDataDefault("SpellBG",0)>0){
		SetAlpha(center(110,yumemianimationframe,200)-110);
	}else{
		SetAlpha(BGOpacity*90);
	}
	SetGraphicAngle(0,0,20);
	SetColor(255,196,128);
	DrawGraphic(GetCenterX,GetCenterY);
	SetGraphicAngle(0,0,-20);
	SetGraphicScale(-1,1);
	SetColor(255,128,128);
	DrawGraphic(GetCenterX,GetCenterY);

}




//=======================================================================================================================================================
//=======================================================================================================================================================
let yubgscroll=-200;
let yubgxscroll=0;
let yubgyscroll=0;

let imgYuBG1=GPSD~"img\yukaribg1.png";
let imgYuBG2=GPSD~"img\yukaribg2.png";
let imgYuBG3=GPSD~"img\yukaribg3.png";

sub YukariBGLogic{
	yubgscroll++;
	yubgxscroll+=cos(yubgscroll/10);
	yubgyscroll+=sin(yubgscroll/10);
}

sub YukariBG {
	SetRenderState(ALPHA);
        SetTexture(imgYuBG1);
	SetGraphicRect(0+yubgscroll-ScreenShakeX,0-ScreenShakeY,256+yubgscroll-ScreenShakeX,256-ScreenShakeY);
	SetGraphicScale(2.5,-2.5);
	SetAlpha(255*BGOpacity);
	SetGraphicAngle(0,0,-40);
	SetColor(255,255,255);
    	DrawGraphic(GetCenterX, GetCenterY);


	SetTexture(imgYuBG3);
	SetRenderState(ADD);
	SetColor((60+sin(yubgscroll*2)*60)*BGOpacity,(60+sin(yubgscroll*2)*60)*BGOpacity,(60+sin(yubgscroll*2)*60)*BGOpacity);
	SetGraphicRect(0,0,256,256);
	SetGraphicScale(2.2,2.2);
	SetGraphicAngle(0,0,-yubgscroll/2);
	DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);
	SetColor(255,255,255);


	SetRenderState(ALPHA);
	SetAlpha(20*BGOpacity);
	SetTexture(imgYuBG2);
	SetGraphicRect(0+yubgxscroll,0+yubgyscroll,512+yubgxscroll,512+yubgyscroll);
	SetGraphicScale(1.5,1.5);
	SetGraphicAngle(0,0,-yubgscroll/10+90);
	DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);
	SetGraphicAngle(0,0,-yubgscroll/10-90);
	DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);

	SetTexture(imgYumemiBG3);
	SetAlpha(70*BGOpacity);
	SetColor(255,255,255);
	SetGraphicAngle(0,0,0);
	SetGraphicRect(0,yubgscroll,GetClipMaxX-GetClipMinX,GetClipMaxY-GetClipMinY+yubgscroll);
	DrawGraphic(GetCenterX,GetCenterY);
}

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

    let imgPatchyBG1=GPSD~"img\patchybg1.png";
    let imgPatchyBG2=GPSD~"img\patchybg2.png";
    let imgPatchyBG3=GPSD~"img\patchybg3.png";

    let patchyalphas=[0,0,0,0];

sub PatchyBGLogic{
	idrawlogic++;
	if(idrawlogic%2==0){
		patchyalphas[idrawlogic/2%4]=rand(0.5,1);
	}
}

sub PatchyBG{
	SetTexture(imgPatchyBG1);


	SetRenderState(ALPHA);
	SetColor(128,128,128);
	SetAlpha(255*BGOpacity);
	SetGraphicScale(1,1);
	SetGraphicRect(0,0+sin(idrawlogic/2)*50,512,512+sin(idrawlogic/2)*50);
	SetGraphicAngle(0,0,90);
	DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);

	SetGraphicScale(1,1);
	SetRenderState(ADD);
	SetColor(128*BGOpacity,128*BGOpacity,128*BGOpacity);
	SetGraphicRect(0,0-idrawlogic,512,512-idrawlogic);
	SetGraphicAngle(0,0,0);
	DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);

	SetTexture(imgPatchyBG2);
	BGscrollx=(sin(idrawlogic*3.3)*40+190)*BGOpacity;

	SetColor(BGscrollx*patchyalphas[0]*BGOpacity,BGscrollx*patchyalphas[0]*BGOpacity,BGscrollx*patchyalphas[0]*BGOpacity);
	SetGraphicRect(0,0,256,256);
	DrawGraphic(GetClipMinX+128+yubgxscroll+ScreenShakeX,GetClipMinY+128+yubgyscroll+ScreenShakeY);

	SetColor(BGscrollx*patchyalphas[1]*BGOpacity,BGscrollx*patchyalphas[1]*BGOpacity,BGscrollx*patchyalphas[1]*BGOpacity);
	SetGraphicRect(256,0,512,256);
	DrawGraphic(GetClipMaxX-128+ScreenShakeX,GetClipMinY+128+ScreenShakeY);

	SetColor(BGscrollx*patchyalphas[2]*BGOpacity,BGscrollx*patchyalphas[2]*BGOpacity,BGscrollx*patchyalphas[2]*BGOpacity);
	SetGraphicRect(0,256,256,512);
	DrawGraphic(GetClipMinX+128+ScreenShakeX,GetClipMaxY-128+ScreenShakeY);

	SetColor(BGscrollx*patchyalphas[3]*BGOpacity,BGscrollx*patchyalphas[3]*BGOpacity,BGscrollx*patchyalphas[3]*BGOpacity);
	SetGraphicRect(256,256,512,512);
	DrawGraphic(GetClipMaxX-128+ScreenShakeX,GetClipMaxY-128+ScreenShakeY);

	SetTexture(imgPatchyBG3);
	SetRenderState(ALPHA);
	SetGraphicScale(1,1);
	SetAlpha(BGOpacity*255);
	SetGraphicRect(0+idrawlogic*2,0+idrawlogic*4,570+idrawlogic*2,570+idrawlogic*4);
	SetGraphicAngle(0,0,30);
	SetColor(0,0,100);
	DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);

	SetGraphicRect(0-idrawlogic*1,0+idrawlogic*2,570-idrawlogic*1,570+idrawlogic*2);
	SetGraphicAngle(0,0,-30);
	SetColor(100,0,0);
	DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);



	SetRenderState(ALPHA);
}

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

    let imgAliceBG1=GPSD~"img\alicebg1.png";
    let imgAliceBG2=GPSD~"img\alicebg2.png";
    let imgAliceBG3=GPSD~"img\alicebg3.png";

    let alicea=[rand(0,360),rand(0,360),rand(0,360),rand(0,360)];
    let alicet=[-1,-1,-1,-1];
    let alicer=[1,1,1,1];
    let ascrollx=0;
    let ascrolly=0;

sub AliceBGLogic{
	if(trunc(idrawlogic%10)==0&&(idrawlogic/10%6)<4){
		alicea[trunc(idrawlogic/10%6)]=rand(0,360);
		alicet[trunc(idrawlogic/10%6)]=idrawlogic;
		if(trunc(idrawlogic/10%12)<6){
			alicer[trunc(idrawlogic/10%6)]=1;
		}else{
			alicer[trunc(idrawlogic/10%6)]=-1;
		}
	}
	idrawlogic++;
	ascrollx+=cos(idrawlogic);
	ascrolly+=sin(idrawlogic/3);
}

sub AliceBG{
	let alicecolor1=GetCommonDataDefault("AliceBGColor1",[0.5,0,0]);
	let alicecolor2=GetCommonDataDefault("AliceBGColor2",[1,0,0]);
	SetTexture(stgwhite);
	SetGraphicRect(0,0,400,500);
	SetGraphicScale(1,1);
	SetGraphicAngle(0,0,0);
	SetColor(alicecolor1[0]*35,alicecolor1[1]*35,alicecolor1[2]*35);
	SetRenderState(ALPHA);
	SetAlpha(255*BGOpacity);
	DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);
	SetTexture(imgAliceBG2);
	SetRenderState(ADD);
	SetColor(alicecolor2[0]*35*BGOpacity,alicecolor2[1]*35*BGOpacity,alicecolor2[2]*35*BGOpacity);
	SetGraphicRect(0+ascrollx,0+ascrolly,600+ascrollx,600+ascrolly);
	SetGraphicAngle(0,0,-idrawlogic/8);
	DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);
	SetGraphicRect(0-ascrollx/2,128-ascrolly/2,600-ascrollx/2,600-ascrolly/2+128);
	SetGraphicAngle(0,0,idrawlogic/8+90);
	DrawGraphic(GetCenterX+ScreenShakeX,GetCenterY+ScreenShakeY);

//	SetGraphicRect(0,128+idrawlogic*2,400,500+128+idrawlogic*2);
//	DrawGraphic(GetCenterX,GetCenterY);

	SetTexture(imgAliceBG1);
	SetGraphicRect(0,0,4,2);
	i=0;
	loop(4){
		if(alicet[i]>=0){
			AliceStar(GetCenterX,GetCenterY,idrawlogic-alicet[i],alicea[i],i, sin(90-(idrawlogic-alicet[i])*(-90/60)),alicer[i]);
		}
		i++;
	}

/*
	SetTexture(imgAliceBG3);
	SetColor(255,255,255);
	SetGraphicScale(1,1);
	SetGraphicAngle(0,0,0);
	i=0;
	loop(32){
		yumemik=sin(idrawlogic*3+i*10);
		SetGraphicRect(0+yumemik*20,i*16,400+yumemik*20,i*16+16);
		DrawGraphic(GetCenterX,GetCenterY-200+i*16);
		i++;
	}
*/

	function AliceStar(x,y,size,angle,color,alpha,rot){
		angle+=rot*size;
		//SetColor((150-color*50)*alpha,(150-color*30)*alpha,150*alpha);
		SetColor((170-alicecolor2[0]*56*color)*alpha*BGOpacity,(170-alicecolor2[1]*56*color)*alpha*BGOpacity,(170-alicecolor2[2]*56*color)*alpha*BGOpacity);
		let i=0;
		loop(6){
			SetGraphicAngle(0,0,angle+i);
			SetGraphicScale(1,size*1.72*2.5);
			DrawGraphic(x+cos(angle+i)*size*2.5+ScreenShakeX,y+sin(angle+i)*size*2.5+ScreenShakeY);
			i+=60;
		}
	}
}

task SpecialBGAlice{
	let numsegments=10;
	let segmentmult=20/numsegments;
	let obj=InitEffect(0,GetClipMaxY-512,imgAliceBG3,numsegments*2+2,ADD,1);
	ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLESTRIP);
	let i=0;
	let x;
	let y;
	let lastbgopacity=-1;
	let frame=0;
	while(BGOpacity>0){
		Obj_SetPosition(obj,ScreenShakeX,GetClipMaxY-512+ScreenShakeY);
		if(BGOpacity!=lastbgopacity){
			Obj_SetColor(obj,numsegments*2+2,80*BGOpacity,255,255,255);
			lastbgopacity=BGOpacity;
		}
		i=0;
		loop(numsegments+1){
			x=sin(idrawlogic*3+i*30*segmentmult)*20;
			y=sin(idrawlogic*2+i*10*segmentmult)*20;
			if(frame%2==0){
				ObjEffect_SetVertexUV(obj,i  ,0+idrawlogic/2,i*(256/numsegments));
				ObjEffect_SetVertexXY(obj,i  ,0+x,i*(300/numsegments)+y);
			}else{
				ObjEffect_SetVertexUV(obj,i+1,450+idrawlogic/2,i*(256/numsegments));
				ObjEffect_SetVertexXY(obj,i+1,450-x,i*(300/numsegments)-y);
			}

//			ObjEffect_SetVertexUV(obj,i  ,0+idrawlogic/2,i*(256/numsegments));
//			ObjEffect_SetVertexUV(obj,i+1,450+idrawlogic/2,i*(256/numsegments));

//			ObjEffect_SetVertexXY(obj,i  ,0+x,i*(300/numsegments)+y);
//			ObjEffect_SetVertexXY(obj,i+1,450-x,i*(300/numsegments)-y);

			i+=2;
		}
		frame++;
		yield;
	}
	DeleteCommonData("AliceBGColor1");
	DeleteCommonData("AliceBGColor1");
	Obj_Delete(obj);
}

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

sub ResetColor{SetColor(255,255,255);}
