 #include_function GCSD~"..\..\frame.txt"
 #include_function GCSD~"..\..\items.txt"
 #include_function GCSD~"..\..\sound.txt"
 #include_function GCSD~"..\..\functions2.txt"

 let diffselect=0;
 let menuitem=0;
 let InMenu=0;
 let k=0;
 let n=0;
 let istr="";
 let diffopacity=250;
 let stgwhite=GPSD~"img\stgwhite.png";
 let imgSoundThing=GPSD~"img\soundthing.png";
 let ScoreBG1=GPSD~"img\scorebg1.png";
 let ScoreBG2=GPSD~"img\scorebg2.png";
 let imgEnemy=GPSD~"img\minion.png";
 let imgLifebar=GPSD~"img\life.png";
 let fx=[];
 let fxinit=0;
 let scoredisplay=0;
 let scorei=0;
 let ptpoint=0;
 let ptgraze=0;
 let ptstage=0;
 let ptcapture=0;
 let ptplayer=0;
 let ptbomb=0;
 let ptdifficulty=0;
 let ptdifficultystr="";
 let totalscore=0;
 let fixedscroll=0;
 let stagepan=0;
 let panspeed=1;
 let timefrozen=false;
 let timefrozenlastframe=false;
 let playerfrozen=false;
 let playerfrozenlastframe=false;
 let numcontinues=0;
 let drawfunk=true;
 let nextlife=1500000;

 let playtime=[0,0,0,0,0,0];
 let playmult=[60,60,60,24,7,0];
 let playstr="fsmhdw";

 let objects=[];	//main menu objects
 let scorebg=0;		//score background
 let scorebg2=0;	//score background leaf overlay

 let scorebgx=[0,0,0];
 let scorebgy=[0,0,0];

 let expid=[];	//ID
 let expxa=[];	//X position
 let expya=[];	//Y position
 let expaa=[];	//Alpha
 let expsa=[];	//Scale
 let expea=[];	//Expansion
 let expca=[];	//red
 let expta=[];  //type
 let expfa=[];  //frames

 let randindex=48;
 let randomnumbers=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];


 let E_RED=0;let E_ORANGE=40;let E_YELLOW=90;let E_GREEN=140;let E_AQUA=200;let E_BLUE=260;let E_PURPLE=320;	//enemy colors

 #include_function GetCurrentScriptDirectory~"..\..\menus.txt"

function Obj_SetColor(obj,vertices,alpha,r,g,b){
	ObjEffect_SetColor(obj,vertices,alpha,r,g,b);
}

 function ObjEffect_SetColor(obj,num,a,r,g,b){
	let loopcount=0;
	loop(num){
		ObjEffect_SetVertexColor(obj,loopcount,a,r,g,b);
		loopcount++;
	}
 }
 function Wait(let frames){
   loop(frames){yield;}
 }
 function WaitZ(let frames, let frames2){
   loop(frames){yield;}
   loop(frames2){if(GetEnemyNum!=0){yield;}}
 }
 function WaitForZeroEnemy{
  while(GetEnemyNum != 0){yield;}
 }
 function GCX{return GetCenterX;}
 function GCY{return GetCenterY;}
 function WaitForZeroEnemyBoss(radius,color1,color2){
  let bossx=0;let bossy=0;
  while(GetEnemyNum != 0){bossx=EnemyX2;bossy=EnemyY2;yield;}
  SE("explode");
  LeafExplosion(bossx,bossy,radius,color1,color2);
  ShakeScreen(3,120);
 }
 function WaitForZeroEnemyBigBoss(color1,color2){
  //waits for the boss to die, then makes an epic explosion
  WaitForZeroEnemyBigBoss2(color1,color2);
  while(GetEnemyNum!=0){
	yield;
  }
 }
 task WaitForZeroEnemyBigBoss2(color1,color2){
  let bossx=0;let bossy=0;let frames=0;
  while(frames<2){if(GetEnemyNum == 0){frames++;}else{bossx=EnemyX2;bossy=EnemyY2;frames=0;}yield;}
  BOOOOOOM(bossx,bossy,color1,color2);
 }
 function WaitForZeroEnemyMidBoss(color1,color2){
  let bossx=100;let bossy=100;let frames=0;
  while(frames<2){if(GetEnemyNum == 0){frames++;}else{bossx=EnemyX2;bossy=EnemyY2;frames=0;}yield;}
  BOOOOOOMsmall(bossx,bossy,color1,color2);
 }
 function WaitBomb{
  while(OnBomb==true){yield;}
 }


let stageimagelen=[8,12,16,0,0,0,0,0,0,9,8,7];
let stageimages=[
["ender","ender2","ender_cutin","black","circuit","grid","enderbg","gradient"],
["overload","overloadaura","overload_cutin","overloadbg1","overloadbg2","overloadbg3","empegh1","empegh_cutin","empbg1","empnotes","staff","kisume"],
["yumemibg1","yumemibg2","yumemibg3","yumemibg4","cross","yukaribg1","yukaribg2","yukaribg3","yukaribg3","gap","gap2","yukari1","yukarithing","yukariclouds","yumemi","yukaristuff"],
["",""],
["",""],
["",""],
["",""],
["",""],
["",""],
["extrabg1b","extrabg2b","gradient","iddyeasquare","iddyea","iddyeabg1","iddyeabg2","iddyeabg3","dreamclouds"],
["extrabg1c","extrabg2c","gradient","patchy","patchyelements","patchybg1","patchybg2","patchybg3"],
["extrabg1","extrabg2","gradient","alice","alicebg1","alicebg2","alicebg3"]
];

let loadedimages=[];
let loadedimagesstage=[];
function LoadImages(stage,delay){
	let k=0;
	while(k<stageimagelen[stage-1]){
		LoadGraphic(GPSD~"img\"~stageimages[stage-1][k]~".png");loop(delay){yield;}
		loadedimages=loadedimages~[stageimages[stage-1][k]];
		loadedimagesstage=loadedimagesstage~[stage];
		k++;
	}
	LoadGraphic(GPSD~"img\stage.png");loop(delay){yield;}
}
function DeleteImages(stage){
	let k=0;
	loop(length(loadedimages)){
		if(loadedimagesstage[k]==stage){
			DeleteGraphic(GPSD~"img\"~loadedimages[k]~".png");
			loadedimages=erase(loadedimages,k);
			loadedimagesstage=erase(loadedimagesstage,k);
		}else{
			k++;
		}
	}
}
sub DeleteAllImages{
	let k=0;
	loop(length(loadedimages)){
		DeleteGraphic(loadedimages[k]);
		k++;
	}
	loadedimages=[];
	loadedimagesstage=[];
}
sub DeleteCounterData{
	i=0;
	while(GetCommonDataDefault("Exps"~IntString(i),-1)!=-1){
		DeleteCommonData("Exps"~IntString(i));DeleteCommonData("Expx"~IntString(i));DeleteCommonData("Expy"~IntString(i));DeleteCommonData("Expc"~IntString(i));DeleteCommonData("Expt"~IntString(i));DeleteCommonData("Expf"~IntString(i));DeleteCommonData("Expst"~IntString(i));i++;
	}
	i=0;
	while(GetCommonDataDefault("Change"~IntString(i),0)>0){
		DeleteCommonData("Change"~IntString(i));i++;
	}
	i=0;
	while(GetCommonDataDefault("PointDrop"~IntString(i),[-999])[0]>-999){
		DeleteCommonData("PointDrop"~IntString(i));i++;
	}
	i=0;
	while(GetCommonDataDefault("MultDrop"~IntString(i),[-999])[0]>-999){
		DeleteCommonData("MultDrop"~IntString(i));i++;
	}
	i=0;
	while(GetCommonDataDefault("SoulDrop"~IntString(i),[-999])[0]>-999){
		DeleteCommonData("SoulDrop"~IntString(i));i++;
	}
}
function ForbidShot2(par){
	ForbidShot(par);ForbidBomb(par);
	if(par==true){SetCommonData("ForbidShot",1);}else{DeleteCommonData("ForbidShot");}
}

function upper(num1,num2){
//returns the higher of two numbers
	if(num1>num2){return num1;}else{return num2;}
}
function lower(num1,num2){
//returns the lower of two numbers
	if(num1<num2){return num1;}else{return num2;}
}
let stimerframe=0;
let stimeadvance=0;
function StageFunctions{
	if(GetCommonDataDefault("SetTimeFrozen",false)){
		timefrozen=GetCommonData("TimeFrozen");
		DeleteCommonData("TimeFrozen");
		DeleteCommonData("SetTimeFrozen");
	}
	let i=0;
	while(GetCommonDataDefault("Exps"~IntString(i),-1)!=-1){
		EnemyExplosion(GetCommonData("Expt"~IntString(i)),GetCommonData("Expx"~IntString(i)),GetCommonData("Expy"~IntString(i)),GetCommonData("Exps"~IntString(i)),GetCommonData("Expc"~IntString(i)),GetCommonData("Expe"~IntString(i)),GetCommonData("Expst"~IntString(i)),GetCommonData("Expf"~IntString(i)));
		DeleteCommonData("Exps"~IntString(i));
		DeleteCommonData("Expx"~IntString(i));
		DeleteCommonData("Expy"~IntString(i));
		DeleteCommonData("Expc"~IntString(i));
		DeleteCommonData("Expt"~IntString(i));
		DeleteCommonData("Expf"~IntString(i));
		DeleteCommonData("Expst"~IntString(i));
		i++;
	}

	i=0;
	while(GetCommonDataDefault("DExx"~IntString(i),-1)!=-1){
		DamageExplosion( GetCommonData("DExx"~IntString(i)),GetCommonData("DExy"~IntString(i)),GetCommonData("DExs"~IntString(i)),GetCommonData("DExt"~IntString(i)),GetCommonData("DExc"~IntString(i)) );
		DeleteCommonData("DExx"~IntString(i));
		DeleteCommonData("DExy"~IntString(i));
		DeleteCommonData("DExs"~IntString(i));
		DeleteCommonData("DExt"~IntString(i));
		DeleteCommonData("DExc"~IntString(i));
		i++;
	}

	i=0;
	while(GetCommonDataDefault("Change"~IntString(i),0)>0){
		DelaySound(GetCommonData("Change"~IntString(i)));
		DeleteCommonData("Change"~IntString(i));
		i++;
	}


	i=0;
	while(GetCommonDataDefault("PointDrop"~IntString(i),[-999])[0]>-999){
		SpawnPointItem(GetCommonData("PointDrop"~IntString(i))[0],GetCommonData("PointDrop"~IntString(i))[1],rand(0,360),rand(0,GetCommonData("PointDrop"~IntString(i))[2]),120);
		DeleteCommonData("PointDrop"~IntString(i));
		i++;
	}
	i=0;
	while(GetCommonDataDefault("MultDrop"~IntString(i),[-999])[0]>-999){
		SpawnMultiplierItem(GetCommonData("MultDrop"~IntString(i))[0],GetCommonData("MultDrop"~IntString(i))[1],rand(0,360),rand(0,GetCommonData("MultDrop"~IntString(i))[2]),120);
		DeleteCommonData("MultDrop"~IntString(i));
		i++;
	}

	i=0;
	while(GetCommonDataDefault("SoulDrop"~IntString(i),[-999])[0]>-999){
		SpawnSoul(GetCommonData("SoulDrop"~IntString(i))[2],GetCommonData("SoulDrop"~IntString(i))[0],GetCommonData("SoulDrop"~IntString(i))[1],GetCommonData("SoulDrop"~IntString(i))[3]);
		DeleteCommonData("SoulDrop"~IntString(i));
		i++;
	}
	if(GetCommonDataDefault("SpellCapEffect",0)==1){
		SpellCaptureCircle;
		SpellBonus(GetCommonData("SpellBonus"));
		DeleteCommonData("SpellCapEffect");
		DeleteCommonData("SpellBonus");
	}
	if(GetCommonDataDefault("StartShakeScreenI",0)>0){
		ShakeScreen(GetCommonData("StartShakeScreenI"),GetCommonData("StartShakeScreenT"));
		DeleteCommonData("StartShakeScreenI");
		DeleteCommonData("StartShakeScreenT");
	}

	if(GetScore>=nextlife){
		LifeUp(1);
		nextlife+=1500000;
	}
	if(timefrozen!=timefrozenlastframe){
		if(timefrozen){
			TimeStopStage(1000000,1,1,1);
		}else{
			TimeStopStage(2,0,0,0);
		}
		timefrozenlastframe=timefrozen;
	}else if(playerfrozen!=playerfrozenlastframe){
		if(playerfrozen){
			TimeStopStage(1000000,1,0,0);
		}else{
			TimeStopStage(2,0,0,0);
		}
		playerfrozenlastframe=playerfrozen;
	}else if(GetTimeSpeed!=1){
		stimerframe+=GetTimeSpeed;
		if(stimerframe>=1){
			stimerframe--;
		}else{
			TimeStopStage(0,1,0,0);
		}
	}else{
		stimerframe=0;
	}

	if(randindex>48){randindex-=49;}
	loop(5){
		randomnumbers[rand_int(0,48)]=rand(0,1);
	}

	HandleBeat;
	
	fixedscroll++;
	if(panspeed>0&&!timefrozen){
		stagepan+=lower( ((GetCenterX-GetPlayerX)-stagepan),150 )*panspeed;
	}
	if(GetOption(5)==2){
		MoreFunkyBeats;
	}
	if(GetCommonDataDefault("InAWCMenu",0)==0){
		i=0;
		playtime[0]=playtime[0]+1;
		while(playtime[i]>=playmult[i]&&playmult[i]>0){
			SetCommonData("AWCPlaytime",playtime);
			playtime[i]=playtime[i]-playmult[i];
			i++;
			playtime[i]=playtime[i]+1;
		}
	}
}

sub InitRandTable{
	let i=0;
	loop(49){
		randomnumbers[i]=rand(0,1);
		i++;
	}
}
task SpellCaptureCircle{
	//graphic,left,top,right,bottom,centerX,centerY,radius,width,segments,rotation,layer,alpha,renderstate,red,green,blue

	let circleimg=GPSD~"img\spellcircle.png";
	let rotation=0;
	let alpha=0;
	let frame=0;
//	loop(20){
//		ImageCircle(circleimg,0,128,1024,160,GetPlayerX,GetPlayerY,cos(frame*(90/20))*200,40,48,rotation,2,alpha,ALPHA,255,255,255);
//		if(alpha<255){alpha+=255/13;}
//		frame++;
//		rotation+=8;
//		yield;
//	}
	let outside=200;
	let inside=100;
	SpellCaptureCircle2(circleimg);
	loop(20){
		outside=upper(cos(frame*(90/20))*400,0);
		inside=upper(cos((frame+5)*(90/10))*400,0);
		ImageCircle(circleimg,0,256,1024,288,GetPlayerX,GetPlayerY,(outside+inside)/2,outside-inside,48,rotation,2,alpha*0.9,ALPHA,255,255,255);
		if(alpha<255){alpha+=255/13;}
		frame++;
		rotation+=8;
		yield;
	}
}
task SpellCaptureCircle2(circleimg){
	loop(17){yield;}
	let alpha=255;
	let widthadd=0;
	let rotation=8*17;
	loop(40){
		ImageCircle(circleimg,0,288,1024,320,GetPlayerX,GetPlayerY,widthadd/2,widthadd,48,rotation,2,alpha*0.9,ALPHA,255,255,255);
		widthadd+=30;
		alpha-=255/40;
		rotation+=8;
		yield;
	}
}

task SpellBonus(bonus){
	bonus=trunc(bonus/10)*10;
	AddScore(bonus);
	SetFontStyle(0,0,0,0,0,0);
	SetFontColorNew(255,255,255,255,196,196);
	let obj1=DrawTextNewObj(1010,"Capture Bonus",imgText,GetCenterX,GetCenterY-40,0,2.5,3.5,7,0,255,0);
	let obj2=DrawTextNewObj(1010,IntString(bonus),imgText,GetCenterX,GetCenterY,0,3.5,3.5,7,0,255,0);
	//let obj2=DrawTextNewObj(1010,IntString(GetCommonDataDefault("Stagenum2",-1)),imgText,GetCenterX,GetCenterY,0,3.5,3.5,7,0,255,0);

	let rainbowtextarray=[[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0],[0]];
	let i=0;
	let frame=0;
	loop(20){
		rainbowtextarray[i]=RainbowArray(frame,127,128);
		frame+=360/20;
		i++;
	}

	let strlen1=13;
	let strlen2=length(IntString(bonus));
//	let strlen2=2;
	frame=0;
//	DrawTextNew(1010,string,font,x,y,angle,xsize,ysize,layer,align,alpha,delay);
	let alpha=0;
	loop(20){
		Obj_SetColorRainbow(obj1,strlen1,alpha,frame);
		Obj_SetColorRainbow(obj2,strlen2,alpha,frame);
		Obj_SetPosition(obj1,GetPlayerX,GetPlayerY-70);
		Obj_SetPosition(obj2,GetPlayerX,GetPlayerY-50);
		frame++;
		alpha+=255/10;
		yield;
	}
	loop(80){
		Obj_SetColorRainbow(obj1,strlen1,255,frame);
		Obj_SetColorRainbow(obj2,strlen2,alpha,frame);
		Obj_SetPosition(obj1,GetPlayerX,GetPlayerY-70);
		Obj_SetPosition(obj2,GetPlayerX,GetPlayerY-50);
		frame++;
		yield;
	}
	loop(60){
		Obj_SetColorRainbow(obj1,strlen1,alpha,frame);
		Obj_SetColorRainbow(obj2,strlen2,alpha,frame);
		Obj_SetPosition(obj1,GetPlayerX,GetPlayerY-70);
		Obj_SetPosition(obj2,GetPlayerX,GetPlayerY-50);
		frame++;
		alpha-=255/30;
		yield;
	}
	ClearTextImmediately(1010);

	function Obj_SetColorRainbow(obj,length,alpha,frame){
		frame=trunc(frame/2.5);
		let k=0;
		let k2=0;
		let color=[0,0,0];
		loop(length){
			k2=0;
			color=rainbowtextarray[frame%20];
			loop(6){
				ObjEffect_SetVertexColor(obj,k*6+k2,alpha,color[0],color[1],color[2]);
				k2++;
			}
			frame++;
			k++;
		}
	}

}
task DelaySound(delay){
	let dead=0;
	loop(delay){
		if(OnPlayerMissed==true){
			dead++;
		}else{
			dead=0;
		}
		if(dead<30){
			yield;
		}
	}
	if(dead<30){
		SE("change");
	}
}
task DelaySE(sound,delay){
	let dead=0;
	loop(delay){
		yield;
	}
	SE(sound);
}

task fade2(time,opacity1,opacity2,color1,color2){
	let currenttime=0;
	let strength1=0;
	let strength2=0;

	loop(time){
		strength2=currenttime/time;
		strength1=1-strength2;
		overlaya=((strength1*opacity1)+(strength2*opacity2));
		overlayc=((strength1*color1)+(strength2*color2));
		currenttime++;
		yield;
	}
}

function CreateObject{
    let fxobj=Obj_Create(OBJ_EFFECT);
    fx = fx ~ [fxobj];
    return fxobj;
}
function DeleteObject(id){
    Obj_Delete(fx[id]);
}
function NullObject(id){
    DrawObject(id,"",0,0,0,0,0,0,0,0,ALPHA,0,0,0,0,0,0,0,0);
}
function DrawObject(id,graphic,left,top,right,bottom,centerX,centerY,scalex,scaley,renderstate,alpha,layer,c1,c2,c3,r1,r2,r3){

    let n=0;
    let x1=(right-left)/2;
    let y1=(bottom-top)/2;

    Obj_SetPosition   (id, centerX,centerY);
    Obj_SetSpeed      (id, 0);
    Obj_SetAngle(id, 0);
    ObjEffect_SetTexture(id,graphic);
    ObjEffect_SetPrimitiveType(id,PRIMITIVE_TRIANGLEFAN);
    ObjEffect_SetScale(id,scalex,scaley);
    ObjEffect_SetAngle(id,r1,r2,r3);
    ObjEffect_SetLayer(id,layer);
    ObjEffect_CreateVertex(id,4);
    ObjEffect_SetRenderState(id,renderstate);
    Obj_SetAutoDelete(id,false);

    ObjEffect_SetVertexUV(id, 0, left,  top);
    ObjEffect_SetVertexUV(id, 1, left,  bottom);
    ObjEffect_SetVertexUV(id, 2, right, bottom);
    ObjEffect_SetVertexUV(id, 3, right, top);
    ObjEffect_SetVertexXY(id, 0, -x1,-y1);
    ObjEffect_SetVertexXY(id, 1, -x1,y1);
    ObjEffect_SetVertexXY(id, 2, x1,y1);
    ObjEffect_SetVertexXY(id, 3, x1,-y1);

    n=0;
    loop(4){
	ObjEffect_SetVertexColor(id,n,alpha,c1,c2,c3);
	n++;
    }
}
function RandNew(num1,num2){
	randindex++;
	return randomnumbers[(randindex)%48]*(num2-num1)+num1;
}
task EnemyExplosion(etype,ex,ey,es,color,e,static,frames){
    let obj = Obj_Create(OBJ_EFFECT);
    let alpha=240;

    Obj_SetPosition   (obj, ex,ey);
    Obj_SetSpeed      (obj, 0);
    Obj_SetAngle      (obj, 0);
    ObjEffect_SetTexture(obj,imgEffects);
    ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);
    ObjEffect_SetScale(obj,1,1);
    if(static==0){
	ObjEffect_SetAngle(obj,RandNew(-80,80),RandNew(-80,80),RandNew(0,360));
    }else{
	ObjEffect_SetAngle(obj,0,0,RandNew(0,360));
    }

    ObjEffect_SetLayer(obj,2);
    ObjEffect_CreateVertex(obj,4);
    ObjEffect_SetRenderState(obj,ADD);

    ObjEffect_SetVertexUV(obj, 0, 1+etype*64, 1);
    ObjEffect_SetVertexUV(obj, 1, 1+etype*64, 63);
    ObjEffect_SetVertexUV(obj, 2, 63+etype*64, 63);
    ObjEffect_SetVertexUV(obj, 3, 63+etype*64, 1);
    ObjEffect_SetVertexXY(obj, 0, -32, -32);
    ObjEffect_SetVertexXY(obj, 1, -32,  32);
    ObjEffect_SetVertexXY(obj, 2,  32,  32);
    ObjEffect_SetVertexXY(obj, 3,  32, -32);

    let multiplier=Detail(1,1,2);
    let n=0;
    loop(frames/multiplier){
	Obj_SetPosition(obj,ex+ScreenShakeX,ey+ScreenShakeY);
	n=0;
	loop(4){
		ObjEffect_SetVertexColor(obj,n,alpha,color[0],color[1],color[2]);
		n++;
	}
	ObjEffect_SetScale(obj,es/2,es/2);
	alpha-=(240/frames)*multiplier;
	es=es+e*2*multiplier;
	Wait(multiplier);
	while(timefrozen){yield;}
    }
    Obj_Delete(obj);
}


function UpdateEnemyExplosion(id){
}

function ScoreWait(frames){
	loop(frames){
		yield;
		UpdateScoreObject;
	}
}
function UpdateScoreObject{
	SetRenderTarget("scoreback");
	ClearRenderTarget("scoreback");

	SetTexture(ScoreBG1);	

	SetGraphicScale(1,1);
	SetGraphicRect(0+scorebgx[0],0+scorebgy[0],386+scorebgx[0],450+scorebgy[0]);
	SetColor(100,100,100);
	SetRenderState(ALPHA);
	SetGraphicAngle(0,0,0);
	SetAlpha(255);
	DrawGraphic(384/2,448/2);

	SetRenderState(ADD);
	SetGraphicRect(0-scorebgx[1],0-scorebgy[1],386-scorebgx[1],450-scorebgy[1]);
	SetGraphicAngle(0,0,180);
	SetColor(0,0,180);
	DrawGraphic(384/2,448/2);

	SetGraphicAngle(0,0,90);
	SetGraphicRect(0+scorebgy[2],0-scorebgx[2],450+scorebgy[2],386-scorebgx[2]);
	SetColor(0,180,180);
	DrawGraphic(384/2,448/2);

	SetTexture(ScoreBG2);
	SetGraphicRect(0,0,386,450);
	SetGraphicScale(1,1);
	SetAlpha(255);
	SetColor(180,180,180);
	SetGraphicAngle(0,0,0);
	DrawGraphic(386/2,450/2);

	SetColor(40,40,40);
	SetRenderState(SUBTRACT);
	DrawWhite;	//darken it a bit

	SetColor(0,0,0);
	SetRenderState(ADD);
	DrawWhite;



	SetRenderState(SUBTRACT);
	SetTexture("scoremask");
	SetGraphicRect(0,0,386,450);
	SetGraphicScale(1,1);
	SetAlpha(255);
	DrawGraphic(386/2,450/2);


	SetRenderTarget("DEFAULT");

	DrawObject(scorebg,"scoreback",0,0,384,448,GetCenterX,GetCenterY,1,1,ALPHA,255,6,255,255,255,0,0,0);
	DrawObject(scorebg2,"scoreleaf",0,0,384,448,GetCenterX,GetCenterY,1,1,ALPHA,255,6,255,255,255,0,0,0);
	scorei+=2;

	let k=0;
	loop(3){
		scorebgx[k]=scorebgx[k]+cos(k*120+scorei/10);
		scorebgy[k]=scorebgy[k]+sin(k*120+scorei/10);
		k++;
	}
}
sub DrawWhite{
	SetTexture(stgwhite);
	SetGraphicRect(0,0,400,500);
	SetAlpha(255);
	SetGraphicScale(1,1);
	DrawGraphic(200,250);
}
function FreezeTime(value){
	timefrozen=value;
}
function FreezePlayer(value){
	playerfrozen=value;
}

task FadeMask(state){
	let renderstates=[SUBTRACT,ADD];
	let alpha=[220,150];
	if(state==0){
		SetRenderTarget("scoremask");
		SetTexture(stgwhite);
		SetGraphicRect(0,0,386,450);
		SetGraphicScale(1,1);
		SetGraphicAngle(0,0,0);
		SetAlpha(255);
		SetColor(255,255,255);
		DrawGraphic(386/2,450/2);
		SetRenderTarget("DEFAULT");
	}

	let leafx=[];
	let leafy=[];
	let leafa=[];
	let leafv=[];
	let leafr=[];
	let leafc=[];
	let leafs=[];

	let k=0;
	loop(24){
		leafx=leafx~[rand(-50,-250)];
		leafy=leafy~[k*20];
		leafv=leafv~[rand(0.7,1.3)*((k%2)*0.5+1)];
		leafa=leafa~[rand(0,360)];
		leafr=leafr~[rand(-0.5,0.5)];
		leafc=leafc~[RainbowArray(rand(-60,40),50,205)];
		leafs=leafs~[rand_int(0,1)-0.5];
		k++;
	}

	let i=0;
	loop(120){
		ClearRenderTarget("scoreleaf");
		SetRenderTarget("scoremask");
		SetTexture(LeafSmall3);
		SetAlpha(255);
		SetColor(255,255,255);
		SetGraphicRect(0,0,128,128);
		i=0;
		loop(8){
			k=0;
			loop(24){
				SetGraphicAngle(0,0,leafa[k]);
				SetGraphicScale(0.5*(2-k%2)*leafs[k],0.25*(2-k%2));

				SetRenderState(renderstates[state]);
				DrawGraphic(leafx[k],leafy[k]);
				if(i==7){
					SetRenderTarget("scoreleaf");
					SetRenderState(ALPHA);
					SetAlpha(alpha[state]);
					SetColor(leafc[k][0],leafc[k][1],leafc[k][2]);
					DrawGraphic(leafx[k],leafy[k]);
					SetRenderTarget("scoremask");
					SetColor(255,255,255);
					SetAlpha(255);
				}
				leafx[k]=leafx[k]+leafv[k];
				leafa[k]=leafa[k]+leafr[k];
				k++;
			}
			i++;
		}
		SetRenderTarget("DEFAULT");
		yield;
	}
}
task FadeScore{

	UpdateScoreObject;
	yield;
	let obj=InitEffect(GetClipMinX,GetClipMaxY,imgLoading,4,ALPHA,8);
	ObjEffect_SetXYRect(obj,0,-34,100,0);
	ObjEffect_SetUVRect(obj,0,150,128,184);
	ObjEffect_SetVertexColor(obj,0,255,255,150,150);
	ObjEffect_SetVertexColor(obj,2,255,255,150,150);
	
	while(GetCommonDataDefault("Loading",0)>0){UpdateScoreObject;yield;}

	Obj_Delete(obj);
	ClearTextImmediately(1);
	ClearTextImmediately(2);
	ClearTextImmediately(3);

	FadeMask(1);
	loop(120){
		UpdateScoreObject;
		yield;
	}
	Obj_Delete(scorebg);
	Obj_Delete(scorebg2);
	DeleteGraphic("scoreback");
	DeleteGraphic("scoremask");
	DeleteGraphic("scoreleaf");
}
let TotalPoint=0;
let TotalGraze=0;
function DisplayResults(stage){
	let clearpts=[0,10,15,20,25,30,35,40,50];
	let difficultymod= [1,1.5,2,2];
	let difficultymods=["1","1.5","2","2"];
	let startlifemod= [1.2,1.1,1,0.6,0.3,0.3];
	let startlifemods=["1.2","1.1","1","0.6","0.3","0.3"];
	let startlifedesc=["1","2","3","4","5","U"];

	SetCommonData("Skippable",0);
	drawfunk=false;
	scorebgx=[0,0,0];
	scorebgy=[0,0,0];
	CreateRenderTarget("scoreback",512,512);
	CreateRenderTarget("scoremask",512,512);
	CreateRenderTarget("scoreleaf",512,512);
	FadeMask(0);
	scorebg=Obj_Create(OBJ_EFFECT);
	scorebg2=Obj_Create(OBJ_EFFECT);
	ForbidShot2(true);
	ptpoint=(GetPoint-TotalPoint)*500;
	ptgraze=(GetGraze-TotalGraze)*500;
	if(stage>=10){ptstage=50*10000;}else{
		ptstage=clearpts[stage]*10000;
	}
	ptcapture=GetCommonDataDefault("SPELLS_CAPTURED_STAGE",0)*10000;
	ptdifficulty=difficultymod[GetCommonDataDefault("Diff",0)];
	ptdifficultystr=difficultymods[GetCommonDataDefault("Diff",0)];
	totalscore=(ptpoint+ptgraze+ptstage+ptcapture)*ptdifficulty*startlifemods[GetOption(0)];

	let lalign=GetClipMinX+40;
	let ralign=GetClipMaxX-40;
	ScoreWait(60);

	SetFontStyle(1,0,0,0,10,5);
	SetFontColorNew(0,128,255,255,255,255);
	ScoreWait(DrawTextNew(2,"STAGE CLEAR!",imgText,GetCenterX,45,0,7,7,7,0,255,4)+20);

	SetFontStyle(1,0,0,0,5,5);
	SetFontColorNew(150,100,150,255,255,255);
	DrawTextNew(1,"Stage Bonus",imgText,lalign,100,0,4,4,7,1,255,1);ScoreWait(8);
	DrawTextNew(1,FormatNum(ptstage),imgText,ralign,100,0,4,4,7,-1,255,3);

	DrawTextNew(1,"Item x "~IntString(GetPoint-TotalPoint),imgText,lalign,140,0,4,4,7,1,255,1);ScoreWait(8);
	DrawTextNew(1,FormatNum(ptpoint),imgText,ralign,140,0,4,4,7,-1,255,3);

	DrawTextNew(1,"Graze x "~IntString(GetGraze-TotalGraze),imgText,lalign,180,0,4,4,7,1,255,1);ScoreWait(8);
	DrawTextNew(1,FormatNum(ptgraze),imgText,ralign,180,0,4,4,7,-1,255,3);

	DrawTextNew(1,"Captures x "~IntString(GetCommonDataDefault("SPELLS_CAPTURED_STAGE",0)),imgText,lalign,220,0,4,4,7,1,255,1);ScoreWait(8);
	DrawTextNew(1,FormatNum(ptcapture),imgText,ralign,220,0,4,4,7,-1,255,3);

	DrawTextNew(1,"Difficulty Bonus",imgText,lalign,300,0,3.5,4,7,1,255,2);ScoreWait(8);
	DrawTextNew(1,"x"~ptdifficultystr,imgText,ralign,300,0,4,4,7,-1,255,3);

	DrawTextNew(1,"Starting Lives ("~startlifedesc[GetOption(0)]~")",imgText,lalign,340,0,3.5,4,7,1,255,2);ScoreWait(8);
	DrawTextNew(1,"x"~startlifemods[GetOption(0)],imgText,ralign,340,0,4,4,7,-1,255,3);



	ScoreWait(20);

	SetFontColorNew(0,0,0,0,0,0);
	DrawTextNew(1,"Total",imgText,lalign+1,390+1,0,4,4,7,1,255,2);
	SetFontColorNew(255,50,255,255,255,255);
	DrawTextNew(1,"Total",imgText,lalign,390,0,4,4,7,1,255,2);ScoreWait(8);

	SetFontColorNew(0,0,0,0,0,0);
	DrawTextNew(1,IntString(totalscore),imgText,ralign+1,390+1,0,4,4,7,-1,255,3);
	SetFontColorNew(255,50,255,255,255,255);
	DrawTextNew(1,IntString(totalscore),imgText,ralign,390,0,4,4,7,-1,255,3);ScoreWait(8);

	let pressed=false;
	let currentscore=GetScore;
	let maxscore=totalscore+GetScore;
	let frame=45;

	if(IsAWCPlayer&&stage>=10&&!IsReplay){
		HighScore(SaveScore(maxscore,GetPlayer,stage,0));
	}else{
		if(IsAWCPlayer&&fullgame==0&&!IsReplay){
			HighScore(SaveScore(maxscore,GetPlayer,stage,diff));
		}
		if(IsAWCPlayer&&fullgame==1&&stage==3&&!IsReplay){
			HighScore(SaveScore(maxscore,GetPlayer,0,diff));
		}
	}

	loop(50){
		if((GetKeyState(VK_SHOT)==KEY_PUSH||GetKeyState(VK_SKIP)!=KEY_FREE)&&!pressed){
			pressed=true;
			SE("ok");
		}
		frame--;
		if(frame>=0){
			AddScore((cos(frame*2)*totalscore+currentscore)-GetScore);
		}
		if(frame==-1){
			AddScore(maxscore-GetScore);
		}
		yield;
		UpdateScoreObject;
	}

	while(!pressed){
		if((GetKeyState(VK_SHOT)==KEY_PUSH||GetKeyState(VK_SKIP)!=KEY_FREE)&&!pressed){
			pressed=true;
			SE("ok");
		}
		yield;
		UpdateScoreObject;
	}

	if(fullgame==0||stage==3||stage>=10){
		ClearTextImmediately(1);

		lalign=GetClipMinX+60;
		ralign=GetClipMaxX-60;

		SetFontStyle(1,0,0,0,5,5);
		SetFontColorNew(255,100,100,255,255,255);
		if(stage>=10){
			DrawTextBoldObj(1,GetPlayerStrU~" vs. "~["Iddyea","Patchouli","Alice"][stage-10],imgText,GetCenterX,73,0,3,3,7,0,255,1);ScoreWait(8);
		}else{
			if(fullgame){
				DrawTextBoldObj(1,GetPlayerStrU~" / "~["Moon","Planet","Star"][diff],imgText,GetCenterX,73,0,3,3,7,0,255,1);ScoreWait(8);
			}else{
				DrawTextBoldObj(1,"Stage "~IntString(stage)~" / "~GetPlayerStrU~" / "~["Moon","Planet","Star"][diff],imgText,GetCenterX,73,0,3,3,7,0,255,1);ScoreWait(8);
			}
		}
		SetFontColorNew(255,255,100,255,255,255);
		SetFontColorEmphasis(255,100,255,255,255,255);
		DrawTextNew(1,"Continues:",imgText,lalign,100,0,4,4,7,1,255,1);ScoreWait(8);
		if(stage<10){
			DrawTextNew(1,"#"~IntString(numcontinues),imgText,ralign,100,0,4,4,7,-1,255,3);
		}else{
			DrawTextNew(1,"#"~IntString(numcontinues-2),imgText,ralign,100,0,4,4,7,-1,255,3);
		}
		DrawTextNew(1,"Misses:",imgText,lalign,140,0,4,4,7,1,255,1);ScoreWait(8);
		DrawTextNew(1,"#"~IntString(GetMissCount-GetCommonDataDefault("subtractmisses",0)),imgText,ralign,140,0,4,4,7,-1,255,3);
		DrawTextNew(1,"Bombs:",imgText,lalign,180,0,4,4,7,1,255,1);ScoreWait(8);
		DrawTextNew(1,"#"~IntString(GetBombCount),imgText,ralign,180,0,4,4,7,-1,255,3);
		DrawTextNew(1,"Captures:",imgText,lalign,220,0,4,4,7,1,255,1);ScoreWait(8);
		DrawTextNew(1,"#"~IntString(GetCommonDataDefault("SPELLS_CAPTURED",0)),imgText,ralign,220,0,4,4,7,-1,255,3);
		ScoreWait(60);
		
		SetFontColorNew(255,255,255,255,255,0);
		DrawTextNew(1," Final Score:",imgText,GetCenterX,280,0,5,5,7,0,255,1);ScoreWait(40);
		SetFontColorNew(255,255,255,255,0,0);
		DrawTextNew(1,FormatNum(maxscore),imgText,GetCenterX,320,0,5,5,7,0,255,8);
		ScoreWait(20);

		if(IsAWCPlayer&&fullgame&&GetPlayer<6){
			if(!IsSeenStage(GetPlayer,GetPlayer+9,3)&&numcontinues==0){
				SetSeenStage(GetPlayer,GetPlayer+9,3);
				SetFontColorNew(0,0,0,0,0,0);
				DrawTextNew(3,GetPlayerStrU~"'s bonus stage unlocked!",imgText,GetCenterX+2,390+2,0,4,4,7,0,200,3);
				DrawTextNew(3,"Access it from the Single Stage menu.",imgText,GetCenterX+2,410+2,0,3,3,7,0,200,2);
				SetFontColorNew(255,255,255,255,0,255);
				DrawTextNew(3,GetPlayerStrU~"'s bonus stage unlocked!",imgText,GetCenterX,390,0,4,4,7,0,255,3);
				DrawTextNew(3,"Access it from the Single Stage menu.",imgText,GetCenterX,410,0,3,3,7,0,255,2);
			}else if(!IsSeenStage(GetPlayer,GetPlayer+9,3)&&numcontinues>0){
				SetFontColorNew(0,0,0,0,0,0);
				DrawTextNew(3,"Beat the game without continuing",imgText,GetCenterX+2,390+2,0,3.5,3.5,7,0,200,3);
				DrawTextNew(3,"to unlock a bonus stage!",imgText,GetCenterX+2,410+2,0,3.5,3.5,7,0,200,2);
				SetFontColorNew(255,255,255,255,0,255);
				DrawTextNew(3,"Beat the game without continuing",imgText,GetCenterX,390,0,3.5,3.5,7,0,255,3);
				DrawTextNew(3,"to unlock a bonus stage!",imgText,GetCenterX,410,0,3.5,3.5,7,0,255,2);
			}
		}
		let pastclear=UnlockedCharacters;
		if(fullgame==1){stage=0;}
		if(GetMissCount-GetCommonDataDefault("subtractmisses",0)==0){
			SetClear(GetPlayer,stage,diff,3);	//no miss
		}else if((numcontinues==0||(stage>=10&&numcontinues<=2))&&GetOption(0)!=5){
			SetClear(GetPlayer,stage,diff,2);	//no continues
		}else if(GetOption(0)!=5){
			SetClear(GetPlayer,stage,diff,1);	//continues
		}else{
			SetClear(GetPlayer,stage,diff,0);	//infinite lives
		}
		if(UnlockedCharacters[6]!=pastclear[6]){
			ClearTextImmediately(3);
			SetFontColorNew(0,0,0,0,0,0);
			DrawTextNew(3,"All extra stages clear!",imgText,GetCenterX+2,390+2,0,4,4,7,0,200,3);
			DrawTextNew(3,"Bonus player character unlocked!",imgText,GetCenterX+2,410+2,0,3,3,7,0,200,2);
			SetFontColorNew(255,255,255,255,0,255);
			DrawTextNew(3,"All extra stages clear!",imgText,GetCenterX,390,0,4,4,7,0,255,3);
			DrawTextNew(3,"Bonus player character unlocked!",imgText,GetCenterX,410,0,3,3,7,0,255,2);
		}

		ScoreWait(40);

		while((GetKeyState(VK_SHOT)!=KEY_PUSH&&GetKeyState(VK_SKIP)==KEY_FREE)){
			UpdateScoreObject;
			yield;
		}
		SE("ok");
	}else{
		
	}

//	MiniLoadingScreen;
	if(fullgame==1){
		FadeScore;
	}else{
		let obj=Obj_Create(OBJ_EFFECT);
		DrawObject(obj,stgwhite,0,0,GetClipMaxX-GetClipMinX,GetClipMaxY-GetClipMinY,GetCenterX,GetCenterY,1,1,ALPHA,255*0.7,8,0,0,0,0,0,0);
		playerfrozen=true;
		let result=ReplayScreen(0,0,false,false);
		if(result==0){ClearedGame=1;yield;}
		else{ClearedGame=2;yield;}
	}

	TotalPoint+=GetPoint;
	TotalGraze+=GetGraze;

	ForbidShot2(false);
	SetCommonData("Skippable",1);
	SetCommonData("ResetAWCBombs",true);	//set the player to default bombs

	StopMusic;
}

task HighScore(place){
	let obj=0;
	let obj2=0;
	if(place<5){
		place*=50;
		HighScore2(540,35, 45,400,210,430);
		HighScore2(540,42, place,495,50+place,512);
		
		//obj2=InitEffect(540,25,imgBorder,4,ALPHA,8);
	}
task HighScore2(x,y,left,top,right,bottom){
	let width=right-left;
	let height=bottom-top;
	x-=width/2;
	y-=height/2;
	height/=2;
	let obj=InitEffect(x,y,imgBorder,16,ALPHA,8);
	ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLESTRIP);
	let k=0;
	let color=0;
	let appearframe=0;
	let percent=0;
	let frame=0;
	let frame2=0;
	let yoffset=0;
	loop(320){
		k=0;
		x=0;
		loop(16){
			if(frame2==0){
				percent=sin(center(0,frame*3-k*8,90));
			}else{
				percent=sin(center(0,frame2*3-k*8,90)+90);
			}
			yoffset=sin(frame*5+x*2-width)*2;
			ObjEffect_SetVertexXY(obj,k,x,(-height+yoffset)*percent);
			ObjEffect_SetVertexXY(obj,k+1,x,(height+yoffset)*percent);
			ObjEffect_SetVertexUV(obj,k,left+x,top);
			ObjEffect_SetVertexUV(obj,k+1,left+x,bottom);
			ObjEffect_SetVertexColor(obj,k,255,Rainbow(0,color+x*1.5-width)*130+120,Rainbow(1,color+x*1.5-width)*130+120,Rainbow(2,color+x*1.5-width)*130+120);
			x+=width/7;
			k+=2;
		}
		color+=8;
		frame++;
		if(frame>240){
			frame2++;	//start folding out
		}
		yield;
	}
	Obj_Delete(obj);
}	
}

task MiniLoadingScreen{
	let loadtype=prand_int(0,3);
	let loadcolor=prand(0,360);

	let obj=Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(obj,stgwhite);
	ObjEffect_SetLayer(obj,8);ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);ObjEffect_CreateVertex(obj,4);
	ObjEffect_SetVertexUV(obj,0,0,2);
	ObjEffect_SetVertexUV(obj,1,2,2);
	ObjEffect_SetVertexUV(obj,2,2,0);
	ObjEffect_SetVertexUV(obj,3,0,0);
	ObjEffect_SetVertexXY(obj,0,-250,-250);ObjEffect_SetVertexXY(obj,1,0,-250);ObjEffect_SetVertexXY(obj,2,0,0);ObjEffect_SetVertexXY(obj,3,-250,0);
	ObjEffect_SetRenderState(obj,ADD);Obj_SetPosition(obj,640,480);ObjEffect_SetScale(obj,1.8,1.8);


	let obj2=Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(obj2,imgLoading);
	ObjEffect_SetLayer(obj2,8);ObjEffect_SetPrimitiveType(obj2,PRIMITIVE_TRIANGLEFAN);ObjEffect_CreateVertex(obj2,4);
	ObjEffect_SetVertexUV(obj2,0,loadtype*150,0);
	ObjEffect_SetVertexUV(obj2,1,loadtype*150+150,0);
	ObjEffect_SetVertexUV(obj2,2,loadtype*150+150,150);
	ObjEffect_SetVertexUV(obj2,3,loadtype*150,150);
	ObjEffect_SetVertexXY(obj2,0,-150,-150);ObjEffect_SetVertexXY(obj2,1,0,-150);ObjEffect_SetVertexXY(obj2,2,0,0);ObjEffect_SetVertexXY(obj2,3,-150,0);
	ObjEffect_SetRenderState(obj2,ALPHA);Obj_SetPosition(obj2,640,480);ObjEffect_SetScale(obj2,1.2,1.2);

	let k=0;
	let alpha=255;
	loop(4){
		ObjEffect_SetVertexColor(obj2,k,alpha,Rainbow(0,loadcolor)*90,Rainbow(1,loadcolor)*90,Rainbow(2,loadcolor)*90);
		k++;
	}
	ObjEffect_SetVertexColor(obj,0,0,255,255,255);
	ObjEffect_SetVertexColor(obj,1,0,255,255,255);
	ObjEffect_SetVertexColor(obj,2,alpha,255,255,255);
	ObjEffect_SetVertexColor(obj,3,0,255,255,255);

	yield;
	while(GetCommonDataDefault("Loading",0)>0){yield;}
	yield;


	loop(80){
		k=0;
		loop(4){
			ObjEffect_SetVertexColor(obj2,k,alpha,Rainbow(0,loadcolor)*90,Rainbow(1,loadcolor)*90,Rainbow(2,loadcolor)*90);
			k++;
		}
		ObjEffect_SetVertexColor(obj,0,0,255,255,255);
		ObjEffect_SetVertexColor(obj,1,0,255,255,255);
		ObjEffect_SetVertexColor(obj,2,alpha,255,255,255);
		ObjEffect_SetVertexColor(obj,3,0,255,255,255);

		alpha-=255/80;
		yield;
	}

	Obj_Delete(obj);
	Obj_Delete(obj2);
}

task CenterCircle(graphic,left,top,right,bottom,centerX,centerY,radius,segments,rotation,layer,alpha,renderstate,red,green,blue){

    let obj = Obj_Create(OBJ_EFFECT);
    let n=0;

    Obj_SetPosition   (obj, centerX,centerY);
    Obj_SetSpeed      (obj, 0);
    Obj_SetAngle(obj, 0);
    ObjEffect_SetTexture(obj,graphic);
    ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLESTRIP);
    ObjEffect_SetScale(obj,1,1);
    ObjEffect_SetAngle(obj,0,0,rotation);
    ObjEffect_SetLayer(obj,layer);
    ObjEffect_CreateVertex(obj,(segments*2)+2);
    ObjEffect_SetRenderState(obj,renderstate);

    n=0;
    loop((segments*2)+2){
	if(n%2==0){
		ObjEffect_SetVertexUV(obj,n,left+(right/segments)*floor(n/2),top);
		ObjEffect_SetVertexXY(obj,n, cos((360/segments)*floor(n/2))*radius, sin((360/segments)*floor(n/2))*radius );
		ObjEffect_SetVertexColor(obj,n,0,red,green,blue);
	} else {
		ObjEffect_SetVertexUV(obj,n,left+(right/segments)*floor(n/2),bottom);
		ObjEffect_SetVertexXY(obj,n,0,0);
		ObjEffect_SetVertexColor(obj,n,alpha,red,green,blue);
	}
	n++;
    }

    yield;
    Obj_Delete(obj);
}

task BOOOOOOM(x,y,color1,color2){
	let rot1=[0,0,0];
	let rot2=0;

	let delay=[0,50,90,110];
	let i=0;
	loop(4){
		rot1=[prand(-70,70),prand(-70,70),prand(0,360)];
		rot2=prand(-1.5,1.5);
		LightRays(x,y,rot1,rot2,2,160-delay[i],30,delay[i],[255,255,255],1, 0);
		LightRays(x,y,rot1,rot2,2,160-delay[i],30,delay[i],[255,255,255],-1, 0);
		DelaySE("light"~IntString(i%2+1),delay[i]);
		i++;
	}
	loop(80){yield;}
	InwardExplosion(3,x,y,10,[128,128,128],100,60,-15);
	loop(30){yield;}
	InwardExplosion(3,x,y,10,[196,196,196],70,40,-15);
	loop(30){yield;}
	InwardExplosion(3,x,y,10,[255,255,255],40,20,-15);
	loop(70){yield;}
	SE("explode");
	CenterExplosion(x,y);
	let color1full=[Rainbow(0,color1)*170+85,Rainbow(1,color1)*170+85,Rainbow(2,color1)*170+85];
	let color2full=[Rainbow(0,color2)*170+85,Rainbow(1,color2)*170+85,Rainbow(2,color2)*170+85];
	i=0;
	loop(15){
		rot1=[prand(-70,70),prand(-70,70),prand(0,360)];
		rot2=prand(-1.5,1.5);
		LightRays(x,y,rot1,rot2,7,i*9+50,60,i,color1full,1, 0);
		LightRays(x,y,rot1,rot2,7,i*9+50,60,i,color2full,-1, 0);
		i++;
	}
	BOOM2(x,y);
	ShakeScreen(3,180);

	loop(10){yield;}
	DeleteAllEnemyWithoutBoss;

	let var=0.4;
	loop(60){
		//SlowTime(var);
		var+=0.01;
		Wait(2);
	}
//	NormalTime;
}
task BOOM2(x,y){
	let expand=10;
	let frame=0;
	loop(240){
		EnemyExplosion(2,x,y,0.1,[prand(0,220),prand(0,220),prand(0,220)],prand(expand/3,expand),frame%2,16);
		expand-=10/240;
		frame++;
		yield;
	}
}
task BOOOOOOMsmall(x,y,color1,color2){
	let rot1=[0,0,0];
	let rot2=0;

	SE("explode");
	CenterExplosion(x,y);
	let color1full=[Rainbow(0,color1)*170+85,Rainbow(1,color1)*170+85,Rainbow(2,color1)*170+85];
	let color2full=[Rainbow(0,color2)*170+85,Rainbow(1,color2)*170+85,Rainbow(2,color2)*170+85];
	i=0;
	loop(15){
		rot1=[prand(-70,70),prand(-70,70),prand(0,360)];
		rot2=prand(-1.5,1.5);
		LightRays(x,y,rot1,rot2,7,i*9+50,60,i,color1full,1, 0);
		LightRays(x,y,rot1,rot2,7,i*9+50,60,i,color2full,-1, 0);
		i++;
	}
	BOOM2(x,y);
	ShakeScreen(3,180);
	let var=0.4;
	loop(60){
		//SlowTime(var);
		var+=0.01;
		Wait(2);
	}
//	NormalTime;
}

task LightRays(x,y,angle,rotspeed,maxscale,time,fadetime,delay,color,direction, type){
    loop(delay){yield;}
    if(direction==-1){
	angle[2]=angle[2]+180;
    }
    let obj = Obj_Create(OBJ_EFFECT);


    ObjEffect_SetTexture(obj,imgEffects);
    ObjEffect_CreateVertex(obj,4);
    ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);
    ObjEffect_SetRenderState(obj,ADD); 
    ObjEffect_SetLayer(obj,2+direction);
    ObjEffect_SetVertexXY(obj,0,0,-32);ObjEffect_SetVertexXY(obj,1,0,32);ObjEffect_SetVertexXY(obj,2,96,32);ObjEffect_SetVertexXY(obj,3,96,-32);
    ObjEffect_SetVertexUV(obj,0,95,65);ObjEffect_SetVertexUV(obj,1,95,125);ObjEffect_SetVertexUV(obj,2,1,125);ObjEffect_SetVertexUV(obj,3,1,65);
    let scale=0;
    let rotation=0;
    let frame=0;
    let alpha=255;
    loop(time+fadetime){
	Obj_SetPosition(obj,x+ScreenShakeX,y+ScreenShakeY);
//	angle=angle+rotspeed;
	angle=angle+[0,0,rotspeed];

	ObjEffect_SetAngle(obj,angle[0],angle[1],angle[2]);
	if(frame>time){alpha-=255/fadetime;}

	if(type==0){
		if(scale<1){
			scale+=1/10;
		}


		ObjEffect_SetScale(obj,scale*maxscale,maxscale*0.7);
		ObjEffect_SetColor(obj,4,alpha,color[0],color[1],color[2]);
	}else{
		if(scale<1){
			scale+=1/time;
		}

		ObjEffect_SetScale(obj,maxscale,maxscale*0.7);
		ObjEffect_SetColor(obj,4,alpha,color[0]*scale,color[1]*scale,color[2]*scale);
	}

	frame++;
	yield;
    }
    Obj_Delete(obj);
}
task CenterExplosion(x,y){
    let obj = Obj_Create(OBJ_EFFECT);

    Obj_SetPosition(obj,x,y);

    ObjEffect_SetTexture(obj,imgEffects);
    ObjEffect_CreateVertex(obj,4);
    ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);
    ObjEffect_SetRenderState(obj,ADD);
    ObjEffect_SetXYRect(obj,-32,-32,32,32);
    ObjEffect_SetUVRect(obj,128,64,191,127);
//    ObjEffect_SetVertexXY(obj,0,-32,-32);ObjEffect_SetVertexXY(obj,1,-32,32);ObjEffect_SetVertexXY(obj,2,32,32);ObjEffect_SetVertexXY(obj,3,32,-32);
//    ObjEffect_SetVertexUV(obj,0,193,65);ObjEffect_SetVertexUV(obj,1,193,127);ObjEffect_SetVertexUV(obj,2,129,127);ObjEffect_SetVertexUV(obj,3,129,65);
    let scale=0;
    let frame=0;
    let alpha=255;
    loop(150){
	if(frame<100){
		scale+=1;
	}
	if(frame>30){
		alpha-=255/(150-30);
	}
	ObjEffect_SetScale(obj,scale,scale);
	ObjEffect_SetColor(obj,4,alpha*0.7,255,255,255);
	frame++;
	yield;
    }
    Obj_Delete(obj);
}

task InwardExplosion(type,x,y,size,color,time,fadeintime,rotate){
    let obj = Obj_Create(OBJ_EFFECT);
    let alpha=0;
    let angle=prand(0,360);

    Obj_SetPosition   (obj, x,y);
    ObjEffect_SetTexture(obj,imgEffects);
    ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);

    ObjEffect_SetLayer(obj,2);
    ObjEffect_CreateVertex(obj,4);
    ObjEffect_SetRenderState(obj,ADD);

    ObjEffect_SetVertexUV(obj, 0, 1+type*64, 1);ObjEffect_SetVertexUV(obj, 1, 1+type*64, 63);ObjEffect_SetVertexUV(obj, 2, 63+type*64, 63);ObjEffect_SetVertexUV(obj, 3, 63+type*64, 1);
    ObjEffect_SetVertexXY(obj, 0, -32, -32);ObjEffect_SetVertexXY(obj, 1, -32,  32);ObjEffect_SetVertexXY(obj, 2,  32,  32);ObjEffect_SetVertexXY(obj, 3,  32, -32);

    let alphaadd=255/fadeintime;
    let frame=0;
    loop(time){
	ObjEffect_SetAngle(obj,0,0,angle);
	ObjEffect_SetScale(obj,cos(frame*(90/time))*size,cos(frame*(90/time))*size);
	angle+=rotate;
	alpha+=alphaadd;
	ObjEffect_SetColor(obj,4,lower(alpha,255),color[0],color[1],color[2]);
	frame++;
	yield;
    }
    Obj_Delete(obj);
}
let LeafSmall3=GPSD~"img\leafsmall3.png";
task LeafExplosion(x,y,radius,color1,color2){
	x=center(GetClipMinX-radius*2,x,GetClipMaxX+radius*2);
	y=center(GetClipMinY-radius*2,y,GetClipMaxY+radius*2);
	let angle=rand(0,360);
	loop(60){
		LeafExp2(x,y,angle,rand(60,180),rand(color1,color2));
		angle+=360/30;
	}
	let size=0.2;
	let color;
	loop(8){
		EnemyExplosion(0,x,y,0.1,RainbowArray(rand(color1,color2),50,50),size,0,rand(60,180));
		EnemyExplosion(3,x,y,0.1,RainbowArray(rand(color1,color2),50,50),size,0,rand(60,180));
		size+=0.08;
	}
	ShakeScreen(3,180);
}
task LeafExp2(x,y,dir,time,color){
    let rotation=rand(0,360);
    let rotspeed=rand(-8,8);
    let size=rand(0.5,1);
    let speed=rand(0.1,2.5);

    let obj = Obj_Create(OBJ_EFFECT);
    Obj_SetPosition   (obj, x,y);
    Obj_SetSpeed      (obj, 0);
    Obj_SetAngle(obj, 0);
    ObjEffect_SetTexture(obj,LeafSmall3);
    ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);
    ObjEffect_SetLayer(obj,3);
    ObjEffect_CreateVertex(obj,4);
    ObjEffect_SetRenderState(obj,ADD);
    Obj_SetAutoDelete(obj,false);

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

	let xv=cos(dir)*speed;
	let yv=sin(dir)*speed;
	let colorarray=[Rainbow(0,color)*100+150,Rainbow(1,color)*100+150,Rainbow(2,color)*100+150];
	let alpha=255;
	let sizeadd=size/time;
	loop(time){
		Obj_SetPosition(obj,x+ScreenShakeX,y+ScreenShakeY);
		Obj_SetColor(obj,4,alpha,colorarray[0],colorarray[1],colorarray[2]);
		ObjEffect_SetAngle(obj,0,0,rotation);
		ObjEffect_SetScale(obj,size,size);
		x+=xv;
		y+=yv;
		alpha-=255/time;
		rotation+=rotspeed;
		size-=sizeadd;
		yield;
	}
	Obj_Delete(obj);

}

task ExExplosion(x,y,color1,color2,intensity){
	ShakeScreen(10,200);
	let k=0;
	loop(26*intensity){
		LeafExp2(x,y,rand(0,360),300-k*6,rand(color1,color2));
		k++;
	}
	k=0;
	loop(40*intensity){
		ExExplosionLine(x,y,250-k*3,rand(color1,color2));
		k++;
	}
	k=0;
	loop(10*intensity){
		ExExplosionCircle(x,y,270-k*10,rand(color1,color2));
		k++;
	}
	k=7;
	let frame=0;
	let angle;
	let saturation;
	loop(240*intensity){
		if(rand_int(0,1)==0){
			EnemyExplosion(0,x,y,0.01,[prand(0,255),prand(0,255),prand(0,255)],prand(k/2,k)/3,frame%2,16);
		}else{
			saturation=rand_int(50,200);
			EnemyExplosion(0,x,y,0.01,RainbowArray(rand(color1,color2),saturation,255-saturation),prand(k/2,k)/3,frame%2,16);
		}
		if(frame<140*intensity){
			LeafExp2(x,y,rand(0,360),rand_int(60,120),rand(color1,color2));
			angle=rand(0,360);
			EnemyExplosion(2,x+cos(angle)*k*rand(0,40),y+sin(angle)*k*rand(0,40),0.01,[prand(0,220),prand(0,220),prand(0,220)],prand(k/3,k)/30,frame%2,90);
		}
		frame++;
		k-=7/(240*intensity);
		yield;
		if(intensity<1){
			yield;
		}
	}
}
task ExExplosionCircle(x,y,time,coloroffset){
	let obj=InitEffect(x,y,imgEffects,4,ADD,4);
	ObjEffect_SetXYRect(obj,-32,-32,32,32);
	ObjEffect_SetUVRect(obj,129,129,255,255);

	let size=0;
	let sizeadd=rand(0.1,0.3);

	let color=RainbowArray(coloroffset,150,105);

	let angle=[rand(0,360),rand(0,360),rand(0,360)];
	let angleadd=[rand(0.1,0.2)*RandDir,rand(0.1,0.2)*RandDir,rand(5,20)*RandDir];
	let k=0;
	let alpha=1;
	loop(time){
		Obj_SetPosition(obj,x+ScreenShakeX,y+ScreenShakeY);
		size+=sizeadd;
		ObjEffect_SetScale(obj,size,size);
		Obj_SetColor(obj,4,alpha*150,color[0],color[1],color[2]);
		k=0;
		loop(3){
			angle[k]=angle[k]+angleadd[k];
		}
		ObjEffect_SetAngle(obj,angle[0],angle[1],angle[2]);
		alpha-=1/time;
		yield;
	}
	Obj_Delete(obj);
}
task ExExplosionLine(x,y,time,coloroffset){
	let obj=InitEffect(x,y,imgEffects,4,ADD,4);
	ObjEffect_SetXYRect(obj,-32,-32,32,32);
	ObjEffect_SetUVRect(obj,128,64,191,127);
	let size=rand(4,7);
	ObjEffect_SetScale(obj,0.2,3*size);

	let colorintensity=rand(0,190);
	let color=RainbowArray(coloroffset,colorintensity,255-colorintensity);

	let angle=[rand(0,360),rand(0,360),rand(0,360)];
	let angleadd=[rand(0.5,1)*RandDir,rand(0.5,1)*RandDir,rand(0.5,1)*RandDir];
	let k=0;
	let alpha=1;
	loop(time){
		Obj_SetPosition(obj,x+ScreenShakeX,y+ScreenShakeY);
		Obj_SetColor(obj,4,alpha*255,color[0],color[1],color[2]);
		k=0;
		loop(3){
			angle[k]=angle[k]+angleadd[k];
		}
		ObjEffect_SetAngle(obj,angle[0],angle[1],angle[2]);
		alpha-=1/time;
		yield;
	}
	Obj_Delete(obj);
}

function Rainbow(color,offset){
	return center(0,sin((offset-(color*120)+70))+0.5,1);
}
function RainbowArray(offset,mult,adda){
	return [Rainbow(0,offset)*mult+adda,Rainbow(1,offset)*mult+adda,Rainbow(2,offset)*mult+adda];
}
task SetLifeBar(iterations,explosiontype, explosionpars){
	ManualEnemyMarker(false);
	yield;
	DeleteCommonData("cancelexp");

	let obj=0;let obj2=0;
	let obj3=0;let obj4=0;
	let maxlife=0;
	let lastlife=0;
	let percentage=0;
	let loopbool=0;
	let fill=0;
	let currbar=0;
	let alpha=0;
	let markerflash=0;
	let sinmf=0;
	let k=0;

	obj = Obj_Create(OBJ_EFFECT);
	LifeInitiate(obj,12,14);
//	ObjEffect_SetColor(obj,4,255,150,100,100);

	obj3 = Obj_Create(OBJ_EFFECT);
	LifeInitiate(obj3,0,0);

	obj2 = Obj_Create(OBJ_EFFECT);
	obj4 = Obj_Create(OBJ_EFFECT);

	let objmarker=[];
	k=0;
	loop(2){
		objmarker=objmarker~[InitEffect(GetCenterX,480,imgEffects,4,ALPHA,8)];
		ObjEffect_SetXYRect(objmarker[k],-28,-16,28,0);
		ObjEffect_SetUVRect(objmarker[k],199,65,199+28*2,64+16);
		k++;
	}
	Obj_SetX(objmarker[1],-200);

	let infinite=false;
	let iterations2=iterations;
	if(iterations<0){infinite=true;}

	while(currbar<iterations||infinite){
		lifebarsremaining=iterations-currbar;
		LifeInitiate(obj2,12,42);
//		ObjEffect_SetColor(obj2,4,255,255,200,200);

		LifeInitiate(obj4,0,28);
//		ObjEffect_SetColor(obj4,4,255,200,200,255);

		maxlife=GetCommonDataDefault("BossLife",0);
		lastlife=maxlife;
		percentage=0;
		fill=1;
		loopbool=0;


		if(GetCommonDataDefault("SurvivalCardD",0)==1){
			ObjEffect_SetColor(obj2,4,255,0,255,92);
			DeleteCommonData("SurvivalCardD");
		}
		DeleteCommonData("NextLifeBar");
		while(!GetCommonDataDefault("NextLifeBar",false)){
			if(alpha<255){
				alpha+=255/60;
				ObjEffect_SetColor(obj,4,alpha,255,255,255);
				ObjEffect_SetColor(obj2,4,alpha,255,255,255);
				ObjEffect_SetColor(obj3,4,alpha,255,255,255);
				ObjEffect_SetColor(obj4,4,alpha,255,255,255);
			}
			lastlife=GetCommonDataDefault("BossLife",0);

			percentage=lower(GetCommonDataDefault("BossLife",0)/maxlife,fill/40);


			LifeBar(obj2,12,percentage,42);

			if(infinite){

			}else if(currbar==0){
				percentage=lower((GetCommonDataDefault("BossLife",0)/maxlife)/iterations + ((iterations-currbar-1)/iterations),fill/(40*iterations));
			}else{
				percentage=(GetCommonDataDefault("BossLife",0)/maxlife)/iterations + ((iterations-currbar-1)/iterations);
			}
			LifeBar(obj4,0,percentage,28);

			fill++;

			percentage=(lower(maxlife,GetCommonDataDefault("BossLife",0))/maxlife);
			markerflash+=(1-percentage)*35+5;
			sinmf=sin(markerflash)*128+127;

			if(!GetCommonDataDefault("ManualEnemyMarker",false)){
				Obj_SetX(objmarker[0],EnemyX2);
				ObjEffect_SetColor(objmarker[0],4,alpha,255-sinmf*percentage,255,255-sinmf*(1-percentage));
			}else{
				Obj_SetX(objmarker[0],GetCommonData("CreateEnemyMarker0")[0]);
				Obj_SetX(objmarker[1],GetCommonData("CreateEnemyMarker1")[0]);
				k=0;
				loop(2){
					ObjEffect_SetColor(objmarker[k],4,alpha*GetCommonDataDefault("CreateEnemyMarker"~IntString(k),[0,0])[1],255-sinmf*percentage,255,255-sinmf*(1-percentage));
					k++;
				}

			}


			yield;
		}
		AttackFinishFlash;
		loop(2){
			AttackFinishExplosion(EnemyX2,EnemyY2,rand(0,360),rand(0,360));
		}
		AttackFinishExplosion(EnemyX2,EnemyY2,rand_int(0,1)*180,0);
		DeleteCommonData("NextLifeBar");
		yield;
		currbar++;
	}
	lifebarsremaining=0;
	BossDeathExplosion(explosiontype,explosionpars);

	Obj_Delete(obj2);
	Obj_Delete(obj4);
	alpha=255;
	ClearText(251);
	loop(30){
		alpha-=255/30;
		ObjEffect_SetColor(obj,4,alpha,255,255,255);
		ObjEffect_SetColor(obj3,4,alpha,255,255,255);
		ObjEffect_SetColor(objmarker[0],4,alpha,255,255,255);
		yield;
	}
	Obj_Delete(obj);
	Obj_Delete(obj3);
	Obj_Delete(objmarker[0]);


	task AttackFinishFlash{
		let obj=InitEffect(0,0,stgwhite,4,ADD,2);
		ObjEffect_SetXYRect(obj,GetClipMinX,GetClipMinY,GetClipMaxX,GetClipMaxY);
		ObjEffect_SetUVRect(obj,0,0,10,10);
		let alpha=40;
		loop(20){
			Obj_SetColor(obj,4,alpha,255,255,255);
			alpha-=2;
			yield;
		}
		Obj_Delete(obj);
	}

	task AttackFinishExplosion(x,y,a1,a2){
		let obj=InitEffect(x,y,imgEffects,4,ADD,2);
		ObjEffect_SetXYRect(obj,-63,-63,63,63);
		ObjEffect_SetUVRect(obj,129,129,255,255);
		let a3=rand(0,360);
		let alpha=255;
		let scale=0;
		let scaleadd=rand(0.3,0.6);
		loop(30){
			scale+=scaleadd;
			a3+=4;
			ObjEffect_SetAngle(obj,a1,a2,a3);
			ObjEffect_SetScale(obj,scale,scale);
			Obj_SetColor(obj,4,alpha,255,255,255);
			alpha-=255/30;
			yield;
		}
		Obj_Delete(obj);
	}
}

task BossDeathExplosion(explosiontype,explosionpars){
	CollectAllItems(2);
	CollectAllSouls(60);

	let x=EnemyX2;
	let y=EnemyY2;
	let x2=-999;
	let y2=-999;
	if(GetCommonDataDefault("ExplosionX",-999)!=-999){
		x=GetCommonData("ExplosionX");
		y=GetCommonData("ExplosionY");
		x2=GetCommonDataDefault("ExplosionX2",-999);
		y2=GetCommonDataDefault("ExplosionY2",-999);
	}
	if(OffScreen(x,y,0)){
		let point=GetLineBorderPoint(GetCenterX,GetCenterY,x,y);
		x=point[1];
		y=point[2];
	}
	if(OffScreen(x2,y2,0)&&x2!=-999){
		let point=GetLineBorderPoint(GetCenterX,GetCenterY,x2,y2);
		x2=point[1];
		y2=point[2];
	}
	if(explosiontype==0){	//leaves
		SE("explode");
		LeafExplosion(x,y,explosionpars[2],explosionpars[0],explosionpars[1]);
		ShakeScreen(5,120);
	}else if(explosiontype==1){	//light beams
		BOOOOOOMsmall(x,y,explosionpars[0],explosionpars[1]);
	}else if(explosiontype==2){	//delayed light beams
		yield;
		if(!GetCommonDataDefault("cancelexp",false)){
			BOOOOOOM(x,y,explosionpars[0],explosionpars[1]);
		}
		DeleteCommonData("cancelexp");
	}else if(explosiontype==3){	//ex-boss explosion
		if(!GetCommonDataDefault("cancelexp",false)){
			SE("explode");
			if(x2==-999){
				ExExplosion(x,y,explosionpars[0],explosionpars[1],1);
			}else{	//double explosion
				ExExplosion(x, y, explosionpars[0],explosionpars[0],0.5);
				ExExplosion(x2,y2,explosionpars[1],explosionpars[1],0.5);
			}
		}
		DeleteCommonData("cancelexp");
	}
}
function LifeInitiate(obj,y,graphicy){
	DeleteCommonData("SurvivalCard");
	Obj_SetPosition   (obj, 40,26);ObjEffect_SetTexture(obj,imgLifebar);ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);
	ObjEffect_SetScale(obj,1,1);ObjEffect_SetLayer(obj,4);ObjEffect_CreateVertex(obj,4);ObjEffect_SetRenderState(obj,ALPHA);Obj_SetAutoDelete(obj,false);

	ObjEffect_SetVertexXY(obj,0,0  ,-6+y);
	ObjEffect_SetVertexXY(obj,1,340,-6+y);
	ObjEffect_SetVertexXY(obj,2,340, 6+y);
	ObjEffect_SetVertexXY(obj,3,0  , 6+y);
	ObjEffect_SetVertexUV(obj,0,1  , 1+graphicy);
	ObjEffect_SetVertexUV(obj,1,341, 1+graphicy);
	ObjEffect_SetVertexUV(obj,2,341,13+graphicy);
	ObjEffect_SetVertexUV(obj,3,1  ,13+graphicy);
}
function LifeBar(obj,y,percentage,graphicy){
	ObjEffect_SetVertexXY(obj,0,0             ,-6+y);
	ObjEffect_SetVertexXY(obj,1,340*percentage,-6+y);
	ObjEffect_SetVertexXY(obj,2,340*percentage, 6+y);
	ObjEffect_SetVertexXY(obj,3,0             , 6+y);
	ObjEffect_SetVertexUV(obj,0,1             , 1+graphicy);
	ObjEffect_SetVertexUV(obj,1,341*percentage, 1+graphicy);
	ObjEffect_SetVertexUV(obj,2,341*percentage,13+graphicy);
	ObjEffect_SetVertexUV(obj,3,1             ,13+graphicy);
}

task DrawName(name,c1,c2,c3,c4,c5,c6){
	yield;
	let loopbool=0;
	SetFontStyle(0,0,0,0,0,10);
	SetFontColorNew(0,0,0,0,0,0);
	DrawTextNew(251,name,imgText,53,18,0, 5,4, 4,1,190,0);
	SetFontColorNew(c1,c2,c3,c4,c5,c6);
	DrawTextNew(251,name,imgText,50,17,0, 5,4, 4,1,220,0);
}
task DrawNameDelay(name,c1,c2,c3,c4,c5,c6,delay){
	let loopbool=0;
	SetFontColorNew(c1,c2,c3,c4,c5,c6);
	SetFontStyle(0,0,0,0,0,10);
	let obj1=DrawTextNewObj(251,name,imgText,53,18,0, 5,4, 4,1,0,0);
	let obj2=DrawTextNewObj(251,name,imgText,50,17,0, 5,4, 4,1,0,0);
	Wait(delay);
	let alpha=0;
	let k=0;
	loop(60){
		alpha+=220/60;
		k=0;
		loop(length(name)){
			ObjEffect_SetVertexColor(obj1,k,alpha*0.85,0,0,0);
			ObjEffect_SetVertexColor(obj1,k+1,alpha*0.85,0,0,0);
			ObjEffect_SetVertexColor(obj1,k+2,alpha*0.85,0,0,0);
			ObjEffect_SetVertexColor(obj1,k+3,alpha*0.85,0,0,0);
			ObjEffect_SetVertexColor(obj1,k+4,alpha*0.85,0,0,0);
			ObjEffect_SetVertexColor(obj1,k+5,alpha*0.85,0,0,0);

			ObjEffect_SetVertexColor(obj2,k,alpha,c1,c2,c3);
			ObjEffect_SetVertexColor(obj2,k+1,alpha,c1,c2,c3);
			ObjEffect_SetVertexColor(obj2,k+2,alpha,c1,c2,c3);
			ObjEffect_SetVertexColor(obj2,k+3,alpha,c4,c5,c6);
			ObjEffect_SetVertexColor(obj2,k+4,alpha,c4,c5,c6);
			ObjEffect_SetVertexColor(obj2,k+5,alpha,c4,c5,c6);
			k+=6;
		}
		yield;
	}
}

task ImageCircle(graphic,left,top,right,bottom,centerX,centerY,radius,width,segments,rotation,layer,alpha,renderstate,red,green,blue){

    yield;
    let obj = Obj_Create(OBJ_EFFECT);
    let n=0;

    Obj_SetPosition   (obj, centerX,centerY);
    Obj_SetSpeed      (obj, 0);
    Obj_SetAngle(obj, 0);
    ObjEffect_SetTexture(obj,graphic);
    ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLESTRIP);
    ObjEffect_SetScale(obj,1,1);
    ObjEffect_SetAngle(obj,0,0,rotation);
    ObjEffect_SetLayer(obj,layer);
    ObjEffect_CreateVertex(obj,(segments*2)+2);
    ObjEffect_SetRenderState(obj,renderstate);

    n=0;
    loop((segments*2)+2){
	ObjEffect_SetVertexColor(obj,n,alpha,red,green,blue);
	if(n%2==0){ObjEffect_SetVertexUV(obj,n,left+(right/segments)*floor(n/2),top);
	} else {   ObjEffect_SetVertexUV(obj,n,left+(right/segments)*floor(n/2),bottom);
	}
	ObjEffect_SetVertexXY(obj,n, cos((360/segments)*floor(n/2))*upper(radius-((n%2)-0.5)*width,0), sin((360/segments)*floor(n/2))*upper(radius-((n%2)-0.5)*width,0) );
	n++;
    }

    yield;
    Obj_Delete(obj);
}
function MotionBlurStage(frames,intensity,rendertype){
	CreateEnemyFromFile(GetCurrentScriptDirectory~"..\other\blurenemy.txt", 0,0,0,0,[frames,intensity,rendertype]);
}
function TimeStopStage(frames,player,bullets,bg){
	CreateEnemyFromFile(GetCurrentScriptDirectory~"..\other\timeenemy.txt", 0,0,0,0,[frames,player,bullets,bg]);
}
task ShakeScreen(intensity,time){
	let frame=prand_int(0,4800);
	let intensityadd=intensity/time;
	loop(time){
		SetCommonData("ScreenShakeX",sin(frame*55)*intensity*0.65);
		SetCommonData("ScreenShakeY",sin(frame*80)*intensity);
		intensity-=intensityadd;
		yield;
		frame++;
	}
	DeleteCommonData("ScreenShakeX");
	DeleteCommonData("ScreenShakeY");
}
function ScreenShakeY{return GetCommonDataDefault("ScreenShakeY",0);}
function ScreenShakeX{return GetCommonDataDefault("ScreenShakeX",0);}

function SlowTime(number){
	SetCommonData("TimeSpeed",number);
}
sub NormalTime{
	DeleteCommonData("TimeSpeed");
}
function GetTimeSpeed{
	return GetCommonDataDefault("TimeSpeed",1);
}

task DamageExplosion(x,y,size,time,color){
	SetCommonData("NumDamageExplosions",GetCommonDataDefault("NumDamageExplosions",0)+1);
	let i=0;
	while(GetCommonDataDefault("DamageExplosionX"~IntString(i),-1000)!=-1000){
		i++;
	}
	SetCommonData("DamageExplosionX"~IntString(i),x);
	SetCommonData("DamageExplosionY"~IntString(i),y);
	SetCommonData("DamageExplosionS"~IntString(i),size);


	let obj = Obj_Create(OBJ_EFFECT);
	let alpha=255;
	let angle=prand(0,360);

	Obj_SetPosition(obj, x,y);
	ObjEffect_SetTexture(obj,imgEffects);
	ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);

	ObjEffect_SetLayer(obj,2);
	ObjEffect_CreateVertex(obj,4);
	ObjEffect_SetRenderState(obj,ADD);

	let type=3;


	ObjEffect_SetVertexUV(obj, 0, 1+type*64, 1);
	ObjEffect_SetVertexUV(obj, 1, 1+type*64, 63);
	ObjEffect_SetVertexUV(obj, 2, 63+type*64, 63);
	ObjEffect_SetVertexUV(obj, 3, 63+type*64, 1);
	ObjEffect_SetVertexXY(obj, 0, -32, -32);
	ObjEffect_SetVertexXY(obj, 1, -32,  32);
	ObjEffect_SetVertexXY(obj, 2,  32,  32);
	ObjEffect_SetVertexXY(obj, 3,  32, -32);


	let sizepercent=0;
	let frame=0;
	let k=0;
	loop(time){
		sizepercent=lower(sizepercent+1/(time/5),1);
		angle-=20;
		k=0;
		loop(4){
			ObjEffect_SetVertexColor(obj,k,alpha,color[0],color[1],color[2]);
			k++;
		}
		ObjEffect_SetScale(obj,size*sizepercent/32,size*sizepercent/32);
		ObjEffect_SetAngle(obj,0,0,angle);
//		ImageCircle(stgwhite,0,0,5,5,x,y,size,1,12,0,1,255,ALPHA,color[0],color[1],color[2]);
		frame++;
		if(frame>time/3){
			alpha-=255/(time/3);
		}
		yield;
	}
	SetCommonData("NumDamageExplosions",GetCommonDataDefault("NumDamageExplosions",0)-1);
	DeleteCommonData("DamageExplosionX"~IntString(i));
	DeleteCommonData("DamageExplosionY"~IntString(i));
	DeleteCommonData("DamageExplosionS"~IntString(i));
	Obj_Delete(obj);
}

task DisplayBorderLine{

	let objline=[0,0];
	let k=0;

	loop(2){
		objline[k] = Obj_Create(OBJ_EFFECT);

		ObjEffect_SetTexture(objline[k],stgwhite);
		ObjEffect_SetPrimitiveType(objline[k],PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetLayer(objline[k],6);
		ObjEffect_SetRenderState(objline[k],ALPHA);
		ObjEffect_CreateVertex(objline[k],4);
		ObjEffect_SetVertexXY(objline[k],0,0,0);
		ObjEffect_SetVertexXY(objline[k],1,120,0);
		ObjEffect_SetVertexXY(objline[k],2,120,2);
		ObjEffect_SetVertexXY(objline[k],3,0,2);
		ObjEffect_SetVertexUV(objline[k], 0, 0, 0);
		ObjEffect_SetVertexUV(objline[k], 1, 10, 0);
		ObjEffect_SetVertexUV(objline[k], 2, 10, 10);
		ObjEffect_SetVertexUV(objline[k], 3, 0, 10);
		Obj_SetAutoDelete(objline[k],false);

		Obj_SetColor(objline[k],4,255,255,255,255);

		ObjEffect_SetAngle(objline[k],0,0,0);

		k++;
	}
	ObjEffect_SetScale(objline[0],1,1);
	Obj_SetPosition(objline[0],GetClipMinX,PointOfCollection);
	ObjEffect_SetScale(objline[1],-1,1);
	Obj_SetPosition(objline[1],GetClipMaxX,PointOfCollection);
	let color=0;

	SetFontColorNew(255,255,255,255,255,255);
	SetFontStyle(0,0,0,0,0,0);
	let objtext=DrawTextNewObj(900,"Item Get Border Line ",imgText,GetCenterX,PointOfCollection-14,0,2,2.5,6,0,255,0);

	loop(180){
		color+=40;
		k=0;
		loop(2){
			Obj_SetColor(objline[k],4,255,250,200+sin(color)*50,150);
			k++;
		}
		Obj_SetColor(objtext,20*6,255,250,200+sin(color)*50,150);
		yield;
	}
	k=0;
	loop(2){
		Obj_Delete(objline[k]);
		k++;
	}
	ClearTextImmediately(900);
}

function SetEnemyInvincibility(time){
	SetCommonData("EnemyInvincibilityA",time);
}

function SetStagePanSpeed(num){
	panspeed=num;
}
function GetPan{
	return stagepan;
}
sub NoPan{
	panspeed=0;
	stagepan=0;
}

task ContinueScreen{
	Wait(40);
	if(IsReplay==true){ClearedGame=1;yield;}
	FreezePlayer(true);

	if(IsAWCPlayer&&GetCommonDataDefault("Stagenum2",-1)>=10&&!IsReplay){
		HighScore(SaveScore(GetScore,GetPlayer,GetCommonData("Stagenum2"),0));
	}else if(IsAWCPlayer&&fullgame==0&&!IsReplay&&GetCommonDataDefault("Stagenum2",-1)>-1){
		HighScore(SaveScore(GetScore,GetPlayer,GetCommonData("Stagenum2"),diff));
	}
	if(IsAWCPlayer&&fullgame==1&&!IsReplay){
		HighScore(SaveScore(GetScore,GetPlayer,0,diff));
	}

	let selected=0;
	let selectedmenu=0;

	let obj=Obj_Create(OBJ_EFFECT);
	DrawObject(obj,stgwhite,0,0,GetClipMaxX-GetClipMinX,GetClipMaxY-GetClipMinY,GetCenterX,GetCenterY,1,1,ALPHA,255*0.4,8,0,0,0,0,0,0);

	if(numcontinues==3&&(fullgame==1||stage>=10)){
		GameOver(obj,0,600);
		Wait(300);
		selectedmenu=ReplayScreen(obj,600,true,true);
		if(selectedmenu==0){	//save replay
			ClearedGame=1;
		}else{
			ClearedGame=2;
		}
		loop{yield;}
	}

	let timer=600;

	SetFontColorNew(255,255,255,255,200,200);
	SetFontStyle(1,3,0,0,1,10);
	DrawTextNew(9,"Continue?",imgText,GetCenterX,180,0,4,4,8,0,255,0);
	if((fullgame==1||stage>=10)){
		if(numcontinues==2){
			DrawTextNew(9,"(This is your last chance!)",imgText,GetCenterX,200,0,3,3,8,0,255,0);
		}else{
			DrawTextNew(9,"("~IntString(3-numcontinues)~" remaining!)",imgText,GetCenterX,200,0,3,3,8,0,255,0);
		}
	}
	SetFontColorNew(255,255,255,200,200,200);
	DrawTextNew(7,"Yes",imgText,GetCenterX,230,0,4,4,8,0,180,0);
	DrawTextNew(7,"No",imgText,GetCenterX,260,0,4,4,8,0,180,0);

	SetFontColorNew(255,255,255,196,220,255);
	DrawTextNew(8,"Yes",imgText,GetCenterX,230,0,4,4,8,0,255,0);
	let ycenter=[230,260];
	while(selected==0){
		if(GetKeyState(VK_UP)==KEY_PUSH||GetKeyState(VK_DOWN)==KEY_PUSH){
			SE("select");
			yield;
			ClearTextImmediately(8);
			yield;
			selectedmenu=1-selectedmenu;
			SetFontColorNew(255,255,255,255,200,200);
			SetFontStyle(1,3,0,0,1,10);
			if(selectedmenu==0){
				DrawTextNew(8,"Yes",imgText,GetCenterX,230,0,4,4,8,0,255,0);
			}else{
				DrawTextNew(8,"No",imgText,GetCenterX,260,0,4,4,8,0,255,0);
			}
		}

		if(GetKeyState(VK_SHOT)==KEY_PUSH){selected=1;SE("ok");}//continue

		Sparkle(GetCenterX+cos(-timer*187)*30,    ycenter[selectedmenu]+16+sin(-timer*187)*12,    cos(-timer*187),0,0.3,30,255);

		TimerTick(timer);
		timer--;
		if(timer==0){
			selected=1;
			selectedmenu=1;
		}

		yield;
	}

	ClearTextImmediately(9);
	ClearTextImmediately(8);

	if(selectedmenu==1){
		ClearTextImmediately(7);
		ClearTextImmediately(8);
		ClearTextImmediately(9);
		ClearTextImmediately(10);
		GameOver(obj,255*0.4,600);
		Wait(300);
		selectedmenu=ReplayScreen(obj,600,true,true);
		if(selectedmenu==0){	//save replay
			ClearedGame=1;
		}else{
			ClearedGame=2;
		}
		loop{yield;}
	}

	yield;

	if(selectedmenu==0){	//continue
		ClearTextImmediately(7);
		ClearTextImmediately(8);
		ClearTextImmediately(9);
		ClearTextImmediately(10);
		Obj_Delete(obj);

		AddScore(-GetScore/2);
		nextlife=1500000;
		while(nextlife<GetScore){
			nextlife+=1500000;
		}
		ExtendPlayer((GetCommonDataDefault("OptionsMenu",[2,0,0,0,0])[0]+101)-GetPlayerLife);
		FreezePlayer(false);
		numcontinues++;
	}else if(selectedmenu==1){	//save replay
		ClearedGame=1;
	}else{
		ClearedGame=2;
	}
}
function ReplayScreen(obj,timer,timeractive,drawyesno){
	yield;
	let ycenter=[230,260];
	SetFontStyle(1,3,0,0,1,10);
	if(!timeractive||drawyesno){
		SetFontColorNew(255,255,255,200,200,200);
		DrawTextNew(7,"Yes",imgText,GetCenterX,230,0,4,4,8,0,180,0);
		DrawTextNew(7,"No",imgText,GetCenterX,260,0,4,4,8,0,180,0);
	}

	SetFontColorNew(255,255,255,255,200,200);
	DrawTextNew(9,"Save replay?",imgText,GetCenterX,180,0,4,4,8,0,255,0);	
	DrawTextBoldObj(9,"You need to choose      next screen if you pick yes.",imgText,GetCenterX,197,0,2.2,2.8,8,0,255,0);
	DrawTextBoldObj(9,"Blame that on Danmakufu, not me.",imgText,GetCenterX,209,0,2.2,2.8,8,0,255,0);
	DrawTextNewKana(9,"ha i",imgText,GetCenterX-19,197,0,5,3.5,8,0,255,0);
	let selected=-1;
	let selectedmenu=1;
	SetFontColorNew(255,255,255,196,220,255);
	DrawTextNew(8,"No",imgText,GetCenterX,260,0,4,4,8,0,255,0);
	while(selected==-1){
		if(GetKeyState(VK_UP)==KEY_PUSH||GetKeyState(VK_DOWN)==KEY_PUSH){
			SE("select");
			yield;
			ClearTextImmediately(8);
			yield;
			selectedmenu=(1-selectedmenu);
			SetFontColorNew(255,255,255,255,200,200);
			SetFontStyle(1,3,0,0,1,10);
			if(selectedmenu==0){
				DrawTextNew(8,"Yes",imgText,GetCenterX,230,0,4,4,8,0,255,0);
			}else{
				DrawTextNew(8,"No",imgText,GetCenterX,260,0,4,4,8,0,255,0);
			}
		}

		if(GetKeyState(VK_SHOT)==KEY_PUSH){selected=1;SE("ok");}//continue

		if(timeractive){
			TimerTick(timer);
		}
		if(timer==0&&timeractive){
			//GameOver(obj,255*0.4,400);
			selected=selectedmenu;
			SE("ok");
		}
		timer--;
		Sparkle(GetCenterX+cos(-timer*187)*30,    ycenter[selectedmenu]+16+sin(-timer*187)*12,    cos(-timer*187),0,0.3,30,255);
		yield;
	}
	ClearTextImmediately(7);
	ClearTextImmediately(8);
	ClearTextImmediately(9);
	ClearTextImmediately(10);
	return selectedmenu;
}

task TimerTick(timer){
	if(timer%60==0){
		SetFontColorNew(255,150,150,255,100,100);
		SetFontStyle(1,3,0,0,1,10);
		ClearTextImmediately(10);
		let obj=DrawTextNewObj(10,IntString(timer/60-1),imgText,GetCenterX,150,0,10,10,8,0,255,0);
		DrawTextNew(10,IntString(timer/60-1),imgText,GetCenterX,150,0,10,10,8,0,255,0);
		let scale=1;
		let opacity=255;
		loop(45){
			ObjEffect_SetScale(obj,scale,scale);
			ObjEffect_SetColor(obj,12,opacity*0.8,255,150,150);
			scale+=0.1;
			opacity-=255/45;
			yield;
		}
		Obj_Delete(obj);
	}
}
task GameOver(obj,startopacity,frames){
		LoadGraphic(GPSD~"img\gameover.png");
		frames/=4;

		let pos=[
		113,147,79,129,75,139, 79,129,48,188,69,168, 69,157,48,188,60,213, 60,213,57,193,111,211, 111,211,91,212,112,189, 106,199,110,171,111,196, 111,171,92,172,110,180,	//g
		115,217,155,131,152,164, 155,131,152,164,188,247, 176,204,169,187,133,186, //a
		196,233,206,133,217,165, 206,133,224,169,225,199, 225,199,222,168,260,143, 260,143,260,239,244,157, //m
		283,159,298,144,272,231, 298,144,339,161,293,159, 282,186,311,191,281,196, 276,217,272,231,326,223, //e

		130,223,98,234,99,252, 130,223,154,254,118,231, 154,254,138,241,151,307, 151,307,148,281,124,298, 151,307,98,302,104,284, 98,302,111,288,98,234, //o
		159,226,183,276,175,301, 175,301,177,275,232,205, //v
		208,305,215,253,232,239, 232,239,253,249,223,250, 217,269,238,272,218,277, 210,297,251,297,208,305, //e
		248,330,258,253,276,232, 276,232,269,247,300,250, 300,250,290,249,291,274, 291,274,294,262,262,276, 263,276,293,309,273,273 //r
		];

		let k=0;
		let frameadd=rand(0,4096);
		let xoffset=rand(0,512);
		loop(length(pos)/6){
			GameOver2(pos[k..k+6],frames*1.5,50,2,3, frameadd,xoffset);
			k+=6;
		}
		k=0;
		loop(length(pos)/6){
			GameOver2(pos[k..k+6],frames*1.5,255,0,0, frameadd,xoffset);
			k+=6;
		}

		let frame=0;
		loop(frames){
			frame+=1/frames;
			DrawObject(obj,stgwhite,0,0,GetClipMaxX-GetClipMinX,GetClipMaxY-GetClipMinY,GetCenterX,GetCenterY,1,1,ALPHA,startopacity+((255*0.3)-startopacity)*frame,8,255*frame,0,0,0,0,0);
			yield;
		}
		frame=0;
		loop(frames){
			frame+=1/frames;
			DrawObject(obj,stgwhite,0,0,GetClipMaxX-GetClipMinX,GetClipMaxY-GetClipMinY,GetCenterX,GetCenterY,1,1,ALPHA,255*0.3*frame+255*0.3,8,255,0,0,0,0,0);
			yield;
		}
		frame=0;
		loop(frames){
			frame+=1/frames;
			DrawObject(obj,stgwhite,0,0,GetClipMaxX-GetClipMinX,GetClipMaxY-GetClipMinY,GetCenterX,GetCenterY,1,1,ALPHA,255*0.6+(255*0.4*frame),8,255-255*frame,0,0,0,0,0);
			yield;
		}
}
task GameOver2(pos,frames,color,x,y,scroll,textureoffset){
	//pos: [x1,y1,x2,y2,x3,y3]
	let centerx=(pos[0]+pos[2]+pos[4])/3;
	let centery=(pos[1]+pos[3]+pos[5])/3;
	let xp=[pos[0]-centerx,pos[2]-centerx,pos[4]-centerx];
	let yp=[pos[1]-centery,pos[3]-centery,pos[5]-centery];

	let obj=InitEffect(centerx+GetClipMinX+x,centery+GetClipMinY+y,GPSD~"img\gameover.png",3,ALPHA,8);
	Obj_SetColor(obj,4,0,0,0,0);
	let k=0;

	k=0;
	loop(3){
		ObjEffect_SetVertexXY(obj,k,xp[k]+x,yp[k]+y);
		k++;
	}

	FadeInObj(obj,60);
	DestroyObj(obj,frames-10);

	while(!Obj_BeDeleted(obj)){
		k=0;
		loop(3){
			ObjEffect_SetVertexUV(obj,k,pos[k*2]+sin(scroll*1.5)*50+x+textureoffset,pos[k*2+1]+scroll*2+y);
			k++;
		}
		scroll++;
		yield;
	}
	task FadeInObj(obj,frames){
		Wait(centerx/3-10);
		let alpha=0;
		let alphaadd=1/frames;
		let angle=rand(0,360);
		let dist=rand(100,200);
		let xv=cos(angle)*dist;
		let yv=sin(angle)*dist;
		let angles=[rand(-300,300),rand(-300,300),rand(-300,300)];
		let percent;
		loop(frames){
			alpha+=alphaadd;
			Obj_SetColor(obj,4,alpha*255,color,color,color);
			percent=cos(alpha*90+90)+1;
			//Obj_SetPosition(obj,centerx+GetClipMinX+x+xv*percent,centery+GetClipMinY+y+yv*percent);
			ObjEffect_SetAngle(obj,angles[0]*percent,angles[1]*percent,angles[2]*percent);
			ObjEffect_SetScale(obj,1+4*percent,1+4*percent);
			yield;
		}
	}

	task DestroyObj(obj,frames){
		Wait(frames+centerx/2);
		let xv=rand(-2,2);
		let yv=0;
		let angle=[0,0,0];
		let k=0;
		let angvel=[rand(-1,1),rand(-1,1),rand(-1,1)];
		while(centery<GetClipMaxY+70){
			Obj_SetPosition(obj,centerx+GetClipMinX+x,centery+GetClipMinY+y);
			ObjEffect_SetAngle(obj,angle[0],angle[1],angle[2]);
			centerx+=xv;
			centery+=yv;
			yv+=0.2;
			k=0;
			loop(3){
				angle[k]=angle[k]+angvel[k]*4;
				k++;
			}
			yield;
		}
		Obj_Delete(obj);
	}
}

task LoadEnemies(scripts,delay){
	let k=0;
	loop(length(scripts)){
		CompileEnemyFromFile(GPSD~"enemy\"~IntString(scripts[k])~".txt");
		loop(delay){yield;}
		k++;
	}
}

task NewCharge_XY(x,y,time,size,frequency,r,g,b){
let frame=0;
let crand=0;
loop(time){
	while(frame>=1){
		crand=rand(0,1);
		ChargeEffect2(x,y,size,rand(0,359),r*crand,g*crand,b*crand,rand(-1,1));
		frame--;
	}
	frame+=frequency;
	yield;
}
}

task NewChargeShrink_XY(x,y,time,size,frequency,r,g,b){
let frame=0;
let crand=0;
let sizeadd=size/time;
loop(time){
	while(frame>=1){
		crand=rand(0,1);
		ChargeEffect2(x,y,size,rand(0,359),r*crand,g*crand,b*crand,rand(-1,1));
		frame--;
	}
	size-=sizeadd;
	frame+=frequency;
	yield;
}
}

task ChargeEffect2(x,y,size,angle,cr,cg,cb,rotation){
    let obj = Obj_Create(OBJ_EFFECT);
    let timer=0;
    let offset=100;
    let offset2=rand(0,offset);
    let calpha=0;
    let i=0;

    Obj_SetAngle(obj, 0);ObjEffect_SetTexture(obj,imgEnemy);
    ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);
    ObjEffect_SetAngle(obj,0,0,angle+90);ObjEffect_SetLayer(obj,2);ObjEffect_SetRenderState(obj,ADD);
    ObjEffect_CreateVertex(obj,4);ObjEffect_SetVertexXY(obj,0,0-16,0-16);
    ObjEffect_SetVertexXY(obj,1,0-16,0+16);ObjEffect_SetVertexXY(obj,2,0+16,0+16);ObjEffect_SetVertexXY(obj,3,0+16,0-16);ObjEffect_SetVertexUV(obj, 0, 1, 97);ObjEffect_SetVertexUV(obj, 1, 1, 127);ObjEffect_SetVertexUV(obj, 2, 31, 127);ObjEffect_SetVertexUV(obj, 3, 31, 97);
    Obj_SetAutoDelete(obj,false);
    Obj_SetPosition(obj,x,y);

    let erand=0;
    let cosangle=cos(angle);
    let sinangle=sin(angle);
    loop(20){

	ObjEffect_SetAngle(obj,0,0,angle+90);

	ObjEffect_SetScale(obj,0.3,(20-timer)*size*0.2);

	angle+=rotation;

	timer++;

	if(timer<=10){calpha+=20;}
	else{calpha-=20;}
	i=0;
	loop(4){
		ObjEffect_SetVertexColor(obj, i, calpha, cr,cg,cb);
		i++;
	}

        yield;

    }
    Obj_Delete(obj);
}

task DrawTitleShadow{
	let numv=10;
	let width=950;	//width of object segment
	let width2=950;	//width of graphic
	let obj=InitEffect(290,180,diffTitle,numv,ALPHA,8);
	ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_SetAngle(obj,0,0,-30);
	ObjEffect_SetScale(obj,0.7,0.7);
	let k=0;
	let i=0;
	loop(numv/2){
		ObjEffect_SetVertexUV(obj,k  ,i,0);
		ObjEffect_SetVertexUV(obj,k+1,i,60);
		k+=2;
		i+=width2/((numv/2)-1);
	}
	let x;
	let frame=rand_int(0,1000);
	let xfreq=[rand(1,3),rand(1,3)];
	let yfreq=[rand(1,3),rand(1,3)];
	let xsize=[rand(10,25)*2,rand(10,25)*2];
	let ysize=[rand(10,25)*2,rand(10,25)*2];
	let wavelength=[rand(40,50),rand(40,50)];

	let xoffset;
	let yoffset;
	i=0;

	let alpha=0;
	let size=1;
	FadeInAlpha;

	width/=(numv/2)-1;

	loop{
		Obj_SetColor(obj,numv,alpha*0.5,100,100,100);
		k=0;
		x=-((numv/2)-1)/2;
		loop(numv/2){
			xoffset=0;
			yoffset=0;
			i=0;
			loop(2){
				xoffset+=cos(frame*xfreq[i]+k*wavelength[i])*xsize[i]*0.5;
				yoffset+=sin(frame*yfreq[i]+k*wavelength[i])*ysize[i]*0.5;
				i++;
			}
			ObjEffect_SetVertexXY(obj,k  ,x*width+xoffset*size,-30+yoffset*size);
			ObjEffect_SetVertexXY(obj,k+1,x*width+xoffset*size, 30+yoffset*size);

			x++;
			k+=2;
		}
		frame++;
		yield;
	}
	Obj_Delete(obj);
	task FadeInAlpha{
		loop(20){
			yield;
		}
		loop(90){
			alpha+=255/90;
			yield;
		}
	}
}
function IsInfiniteLives{
	if(setextralives){
		return 1;
	}else if(GetOption(0)==5){
		return 2;
	}else{
		return 0;
	}
}

let cam1=0;let cam2=0;let cam3=0;let cam4=0;
task Cam1(one,two,time){let frame=0;loop(time){cam1=(cos((frame)*(180/time)+180)+1)*((two-one)*0.5)+one; frame++; yield;while(timefrozen){yield;}}cam1=two;}
task Cam2(one,two,time){let frame=0;loop(time){cam2=(cos((frame)*(180/time)+180)+1)*((two-one)*0.5)+one; frame++; yield;while(timefrozen){yield;}}cam2=two;}
task Cam3(one,two,time){let frame=0;loop(time){cam3=(cos((frame)*(180/time)+180)+1)*((two-one)*0.5)+one; frame++; yield;while(timefrozen){yield;}}cam3=two;}
task Cam4(one,two,time){let frame=0;loop(time){cam4=(cos((frame)*(180/time)+180)+1)*((two-one)*0.5)+one; frame++; yield;while(timefrozen){yield;}}cam4=two;}
sub ResetCam{cam1=0;cam2=0;cam3=0;cam4=0;}

let bossid=0;
let lifebarsremaining=0;
function CreateBoss(x,y,id,directory,attacklist,diffmask,difficulty,numhealthbars, explosiontype){
	CreateBoss2(x,y,id,directory,attacklist,diffmask,difficulty,numhealthbars, explosiontype,3);
}
function CreateBossNoDelay(x,y,id,directory,attacklist,diffmask,difficulty,numhealthbars, explosiontype){
	CreateBoss2(x,y,id,directory,attacklist,diffmask,difficulty,numhealthbars, explosiontype,0);
}

task CreateBoss2(x,y,id,directory,attacklist,diffmask,difficulty,numhealthbars, explosiontype,delay){
	//id: 0=Ender, 1=Empegh, 2=Overload, 3=Iddyea, 4=Yumemi, 5=Yukari, 6=Chen, 7=Patchy, 8=Alice, 9=Kisume
	let extraload1=["lw\an1l", "lw\an2l", "lw\an3l", "lw\an4l", "lw\an5l", "lw\as1l", "lw\as3l", "lw\as4l" ];
	let extraload2=["lw\an1ld","lw\an2ld","lw\an3ld","lw\an4ld","lw\an5ld","lw\as1ld","lw\as3ld","lw\as4ld"];


	bossid=upper(0,bossid)+1;
	let currentbossid=bossid-1;
	let difficulties=["f","h","l","l"];
	let j;
	let prefixes=["e","em","o","i","u","y","c","p","a","k"];
	let explosionpars=[[0,60],[60,160],[60,100],[300,360],[60,160,50],[0,300,50],[0,50,50],[360,220],[0,360],[0,50,50]];	//color1, color2, radius (leaf only)
	if(id==0&&GetPlayerEx==6){
		explosionpars=[[150,250]];
	}

	let fullattacklist=[];
	let extraloadlist=[];
	let k=0;
	loop(length(attacklist)){
		if(diffmask[difficulty][k]==1){
			if(attacklist[0]==""){	//if in spell practice
				fullattacklist=fullattacklist~[GPSD~directory~".txt"];
			}else if(attacklist[0][0]=='~'){	//if the first letter is ~, don't add the difficulty letter at the end (used for escape things and such that are constant for all difficulties)
				fullattacklist=fullattacklist~[GPSD~directory~"\"~prefixes[id]~erase(attacklist[0],0)~".txt"];
			}else{
				fullattacklist=fullattacklist~[GPSD~directory~"\"~prefixes[id]~attacklist[0]~difficulties[difficulty]~".txt"];
			}
		}
		k++;
		attacklist=erase(attacklist,0);
	}
	k=0;
	loop(length(fullattacklist)){
		CompileEnemyFromFile(fullattacklist[k]);
		j=0;
		loop(length(extraload1)){
			if(fullattacklist[k]==GPSD~extraload1[j]~".txt"){
				CompileEnemyFromFile(GPSD~extraload2[j]~".txt");
			}
			j++;
		}
		Wait(delay);
		k++;
	}
	yield;
	while(bossid!=currentbossid&&bossid!=-1){yield;}	//wait for SpawnBoss command
	if(bossid!=-1){
		CreateEnemyFromFile(fullattacklist[0],x,y,0,0,erase(fullattacklist,0));
		if(GetCommonDataDefault("SpellPractice",0)==1){
			SetLifeBar(numhealthbars, -1,explosionpars[id]);
		}else{
			SetLifeBar(numhealthbars, explosiontype,explosionpars[id]);
		}
		yield;
		alternative(id)
			case(0){ DrawNameDelay("Ender",255,255,255,255,20,20,0); }
			case(1){ DrawNameDelay("Empegh",255,255,255,190,190,0,0); }
			case(2){ DrawNameDelay("Overload",255,255,255,190,100,0,0); }
			case(3){ DrawNameDelay("Iddyea",255,255,255,255,100,255,0); }
			case(4){ DrawNameDelay("Yumemi",255,255,255,50,255,50,120); }
			case(5){ DrawNameDelay("Yukari",255,255,255,250,150,250,0); }
			case(6){ DrawNameDelay("Chen",255,255,255,250,200,60,0); }
			case(7){ DrawNameDelay("Patchouli",255,255,255,250,200,250,0); }
			case(8){ DrawNameDelay("Alice",255,255,255,60,200,255,0); }
			case(9){ DrawNameDelay("Kisume",255,255,255,100,100,255,0); }
		if(GetCommonDataDefault("SpellPractice",0)==1){
			while(lifebarsremaining>0){yield;}
			if((!OnPlayerMissed&&GetMissCount==0)||GetCommonDataDefault("PracticeAutoReset",0)==0){
				BossDeathExplosion(explosiontype,explosionpars[id]);
			}
		}
	}
}
sub SpawnBoss{
	bossid--;
}
sub ClearBossList{
	bossid=-1;
}
sub CancelExplosion{
	SetCommonData("cancelexp",true);
}
function WaitForBossDead{
	yield;
	yield;
	while(lifebarsremaining>0){yield;}
}
function WaitForBossDeadPractice(ID){
	yield;
	yield;
	let keeplooping=true;
	let timesincelastpress=0;
	while(keeplooping){
		if(lifebarsremaining<=0){keeplooping=false;}
		if(GetKeyState(VK_SKIP)==KEY_PUSH&&GetOption(1)==0){keeplooping=false;}
		if(GetKeyState(VK_SKIP)==KEY_PUSH&&GetOption(1)==2){
			if(timesincelastpress<0){
				timesincelastpress=30;
			}else{
				keeplooping=false;
			}
		}
		timesincelastpress--;
		yield;
	}
	return lifebarsremaining>0;
}
function IsSeenStage(char,stage,diff){
	if(stage<=0&&diff!=3){
		return UnlockedCharacters[char];
	}
	else{
		return GetCommonDataDefault("SeenStage"~IntString(char)~IntString(stage)~IntString(diff),false);
	}
}
function IsSeenStageAll(stage){
	let d=0;
	let c=0;
	let isseen=false;
	loop(4){
		c=0;
		loop(3){
			if(IsSeenStage(c,stage,d)){isseen=true;}
			c++;
		}
		d++;
	}
	return isseen;
}
function IsSeenStageAnyChar(stage,diff){
	let c=0;
	let isseen=false;
	loop(3){
		if(IsSeenStage(c,stage,diff)){isseen=true;}
		c++;
	}
	return isseen;
}
function SetSeenStage(char,stage,diff){
	if(!IsReplay&&char>=0){
		SetCommonData("SeenStage"~IntString(char)~IntString(stage)~IntString(diff),true);
		SaveCommonData;
	}
}
function DifficultySeenArray(stage){
	return [IsSeenStageAnyChar(stage,0),IsSeenStageAnyChar(stage,1),IsSeenStageAnyChar(stage,2),IsSeenStageAnyChar(stage,3)];
}
function AllowedCharacterArray(stage,diff){
	return [IsSeenStage(0,stage,diff),IsSeenStage(1,stage,diff),IsSeenStage(2,stage,diff),false,false,false,IsSeenStage(6,stage,diff)];
}
function UnlockedCharacters{
	let lock7=false;
	if(GetClearType(0,10,3)>=0&&GetClearType(1,11,3)>=0&&GetClearType(2,12,3)>=0){lock7=true;}
	return [true,true,true,false,false,false,lock7];
}
function SetClear(char,stage,diff,type){
	//type: 0=infinite lives, 1=continued, 2=no continues, 3=no-miss
	if(!IsReplay&&char>=0&&GetClearType(char,stage,diff)<type){
		SetCommonData("Clear"~IntString(char)~IntString(stage)~IntString(diff),type);
		SaveCommonData;
	}
}
function GetClearType(char,stage,diff){
	return GetCommonDataDefault("Clear"~IntString(char)~IntString(stage)~IntString(diff),-1);
}