
let deletename=false;
function PlayerSelect(allowarray,showclears){
	//returns 0 if the bomb key is pushed, otherwise returns 1 and sets the character

	let pagewidth=500;

	let charpage=[0,0,0,3,3,3,1];	//page
	let charposx=[-1,0,1,0,0,0,0];	//position
	let chargfx=[0,1,2,0,0,0,3];	//left bound of graphic
	let charx=[94,240,475,0,0,0,220+pagewidth];	//name
	let chary=[50,40,35,0,0,0,30];
	let clearx=[80,300,405,0,0,0,210+pagewidth];
	let cleary=[100,30,55,0,0,0,90];
	let namegfx=[0,1,2,0,0,0,3];	//position of name in playernames

	let frame=0;
	let drawingnum=-1;

	let keeplooping=true;	//set to false when character is selected
	let keeploopingsoft=true;	//set to false if Z is pushed but the thing is still being drawn
	let ret=1;
	let choice=GetPlayer;
	if(choice<0){choice=0;}
	if(!allowarray[choice]){choice=0;}
	while(!allowarray[choice]){
		choice++;
	}

	let currentx=charx[choice];
	let currenty=chary[choice];


	let lastchoice=-1;
	let drawingdescription=false;
	BlackBox;
	let k=0;
	let obj=[0,0,0,0,0,0,0];
	let alpha=[0,0,0,0,0,0,0];
	let color=[255,255,255,255,255,255,255];

	let targetxscroll=charpage[choice]*pagewidth;
	let xscroll=targetxscroll;

	let nametype=[0,0,1,-1,-1,-1,0];

	loop(7){
		obj[k]=InitEffect(0,0,imgCharselect,4,ALPHA,8);
		ObjEffect_SetXYRect(obj[k],-125,-175,125,175);
		ObjEffect_SetUVRect(obj[k],chargfx[k]*250,0,chargfx[k]*250+250,350);

		if(allowarray[k]){
			if(nametype[k]==0){
				DrawCharacterName(k);
			}else if(nametype[k]==1){
				DrawCharacterNameSmooth(k);
			}
			if(showclears){
				DrawClearThingy(k);
			}
		}

		k++;
	}

	if(allowarray[6]){
		DrawScrollArrow(1, 0);
		DrawScrollArrow(-1,1);
	}
	let extra=false;
	while(keeplooping){
		k=0;
		loop(7){
			if(allowarray[k]){
				alpha[k]=alpha[k]+( ([100,255][k==choice]) -alpha[k])/10;
			}else{
				alpha[k]=alpha[k]+( 150-alpha[k])/10;
				color[k]=0;
			}
			Obj_SetColor(obj[k],4,alpha[k],color[k],color[k],color[k]);
			Obj_SetPosition(obj[k],round(320+pagewidth*charpage[k]+charposx[k]*170-xscroll),170);
			k++;
		}
		if(GetKeyState(VK_LEFT)==KEY_PUSH&&keeploopingsoft){
			let lastchoice=choice;
			choice--;
			if(choice<0){choice=lastchoice;}
			while(!allowarray[choice]){
				choice--;
				if(choice<0){choice=lastchoice;}
			}
			if(choice!=lastchoice){
				SE("select");
			}
		}
		if(GetKeyState(VK_RIGHT)==KEY_PUSH&&keeploopingsoft){
			let lastchoice=choice;
			choice++;
			if(choice>=7){choice=lastchoice;}
			while(!allowarray[choice]){
				choice++;
				if(choice>=7){choice=lastchoice;}
			}
			if(choice!=lastchoice){
				SE("select");
			}
		}
		if(choice!=lastchoice){
			if(choice>2){
				targetxscroll=pagewidth;
			}else{
				targetxscroll=0;
			}
			deletename=true;
			yield;
			deletename=false;
			lastchoice=choice;
			DrawPlayerDescription(choice);
		}

		if(GetKeyState(VK_BOMB)==KEY_PUSH&&keeploopingsoft){
			SE("cancel");ret=0;keeploopingsoft=false;
		}
		if(GetKeyState(VK_SHOT)==KEY_PUSH&&keeploopingsoft){
			if(GetKeyState(VK_UP)==KEY_HOLD){extra=true;}
			SE("ok2");keeploopingsoft=false;
			FadeOutMusic(titlemusic,30);
		}
		if(!keeploopingsoft&&!drawingdescription){
			keeplooping=false;
		}
		xscroll+=(targetxscroll-xscroll)/14;
		loop(2){
			currentx+=(charx[choice]-currentx)/16;
			currenty+=(chary[choice]-currenty)/16;

			k=distance(currentx,currenty,charx[choice],chary[choice])^0.5;

			Sparkle2(currentx+cos(frame*1.2)*12+cos(frame*1.7)*12,currenty+sin(frame*2)*10+sin(frame*2.3)*10,
				rand(0,1),rand(0,360),lower(k/60,0.5)+0.5,60,lower(k*2,225)+30);

			frame+=1.5;
		}
		yield;
	}
	yield;
	if(extra){choice+=0.5;}
	deletename=true;
	SetPlayer(choice);
	if(ret==0){
		ClearTextImmediately(401);
		ClearTextImmediately(402);
	}
	k=0;
	loop(7){
		if(k!=trunc(choice)||ret==0){
			FadeChar(obj[k],alpha[k],color[k]);
		}
		k++;
	}
	return ret;


task DrawCharacterName(num){
	let obj=InitEffect(charx[num],chary[num],imgCharnames,4,ALPHA,8);
	ObjEffect_SetXYRect(obj,-62,-40,63,40);
	let frame=0;
	let alpha=0;
	let alphatarget=[0.5,1];
	while(keeplooping){
		Obj_SetX(obj,round(charx[num]-xscroll));
		alpha+=(alphatarget[choice==num]-alpha)/10;
		Obj_SetColor(obj,4,alpha*255,255,255,255);
		if(frame%4==0){
			ObjEffect_SetUVRect(obj,128*namegfx[num]+2,(frame/4%3)*80,128*namegfx[num]+127,80+(frame/4%3)*80);
		}
		frame++;
		yield;
	}
	Obj_Delete(obj);
}
task DrawCharacterNameSmooth(num){
	let k=0;
	let obj=[0,0,0];
	loop(3){
		obj[k]=InitEffect(charx[num],chary[num],imgCharnames,4,ADD,8);
		ObjEffect_SetXYRect(obj[k],-63,-40,63,40);
		ObjEffect_SetUVRect(obj[k],128*namegfx[num]+1,k*80,128*namegfx[num]+127,k*80+80);
		k++;
	}
	let alpha=[0,0,0];
	let alphamult=0;
	let state=0;
	let alphatarget=[0.15,1];
	while(keeplooping){
		alphamult+=(alphatarget[choice==num]-alphamult)/10;

		alpha[state%3]=alpha[state%3]+0.15;
		alpha[(state+2)%3]=alpha[(state+2)%3]-0.15;
		if(alpha[state%3]>=1){
			alpha[state%3]=1;
			alpha[(state+2)%3]=0;
			state++;
		}
		k=0;
		loop(3){
			Obj_SetX(obj[k],round(charx[num]-xscroll));
			Obj_SetColor(obj[k],4,alpha[k]*255*alphamult,255,255,255);
			k++;
		}
		yield;
	}
	Obj_Delete(obj[0]);
	Obj_Delete(obj[1]);
	Obj_Delete(obj[2]);

}
task DrawClearThingy(num){
	let cleartype=GetClearType(num,stage,diff);
	let obj=InitEffect(0,cleary[num],imgCharselect,4,ALPHA,8);
	ObjEffect_SetXYRect(obj,-50,-36,50,36);
	ObjEffect_SetUVRect(obj,100*cleartype,412,100+100*cleartype,484);
	let frame=0;
	let alpha=0;
	let alphatarget=[0,1];
	while(keeplooping){
		Obj_SetX(obj,clearx[num]-xscroll);
		ObjEffect_SetScale(obj,alpha,alpha);
		alpha+=(alphatarget[choice==num]-alpha)/5;
		Obj_SetColor(obj,4,alpha*255,255,255,255);
		frame++;
		yield;
	}
	Obj_Delete(obj);
}

task FadeChar(obj,alpha,color){
	let alphaadd=alpha/20;
	loop(20){
		Obj_SetColor(obj,4,alpha,color,color,color);
		alpha-=alphaadd;
		yield;
	}
	Obj_Delete(obj);
}

task Sparkle2(x,y,v,angle,size,time,alpha){
	let obj=InitEffect(x,y,imgEffects,4,ADD,8);
	ObjEffect_SetXYRect(obj,-32,-32,32,32);
	ObjEffect_SetUVRect(obj,128,1,191,63);
	let graphicangle=rand(0,360);
	let angvel=rand(-10,10);
	let sizeadd=size/time;
	let xvel=cos(angle)*v;
	let yvel=sin(angle)*v;
	Obj_SetColor(obj,4,alpha,255,255,255);
	let percent=0;
	while(percent<1&&ret!=0){
		ObjEffect_SetAngle(obj,0,0,graphicangle);
		ObjEffect_SetScale(obj,size,size);
		Obj_SetPosition(obj,x-xscroll,y);
		graphicangle+=angvel;
		size-=sizeadd;
		percent+=1/time;
		x+=xvel*percent;
		y+=yvel*percent;
		yield;
	}
	Obj_Delete(obj);
}


task DrawPlayerDescription(num){
	while(drawingdescription==true){yield;}

	let Name=["Lymia, the heavenly butterfly","Raibys, the bashful shapeshifter","Nhimor, the lovestruck prokaryote","","","","Ender, the mechanical maniac"];

	let NameColor=[[255,255,120],[255,90,90],[100,255,255],[0],[0],[0],[255,200,100]];	//most text
	let NameColor2=[[255,80,255],[60,255,60],[100,100,255],[0],[0],[0],[255,50,50]];	//headings

	let shotautodesc=[true,true,true,true,true,true,false];	//if true, "Shot:" and "Spell card x:" are automatically prefixed

	let ShotName=["Zappy Zap","Adaptive Needle","Pisclet Storm","","","","#Level 1: Chaos Slash"];
	let ShotDescA=["After the lightning hits an enemy,","A direct forward shot (most of the","Most of his attack power comes from","","","","A series of quick cuts. Destroys"];
	let ShotDescB=["it re-aims towards another one.","time). Good in most situations.","Pisclets. Hard to use, but strong.","","","","shots, but has no invincibility time."];

	let BombName1=["Inverted Shockbarrier","Shard Storm","Denary Fission","","","","#Level 2: Vorpal Blade"];
	let BombDesc1A=["A spectacular storm of","A short-range circular burst with","Creates ten clones, all of which","","","","A large area of destruction"];
	let BombDesc1B=["high-powered lightning.","lots of invincibility time.","move and act independently.","","","","aimed directly in front of him."];

	let BombName2=["Superlumina","Genetic Lance","Caustic Stream","","","","#Level 3: Nevella"];
	let BombDesc2A=["A high-speed flying attack that can","A narrow but powerful attack that can","All Pisclets shoot powerful streams","","","","The ultimate weapon."];
	let BombDesc2B=["be controlled while it's active.","be aimed by holding left or right.","of corrosive acid directly forward.","","","","Its effect is unknown."];

	let UnfocusedSpeed=[5.5,4,  4.5,0,0,0,4];
	let FocusedSpeed=  [3,  2.5,2.5,0,0,0,2];

	let StartingBombs=["3","3","4","","","","-"];

	let Notes=[
		"Lymia's unfocused shot is angled, which gives her a wide range, but it won't hit directly in front of her. The focused shot is aimed straight forward, however, which makes it more useful during boss fights. Overall, she's a very balanced and fluffy character.",
		"Raibys has six different forms that he can switch between by focusing. Each one has advantages and disadvantages, so learn all of them and how to use them effectively. He's also relatively slow, which makes dodging in close quarters easier.",
		"Nhimor's main ability is creating extensions of himself called Pisclets. Tap the shot button to place one at his current position, and they follow him if you focus. Pressing shot and focus simultaneously resets them. If used strategically, Pisclets can be extremely powerful.",
		"",
		"",
		"",
		"Ender's shot is not rapid fire - every tap of the shot button has a reloading time. He has no bombs, and instead must damage enemies to fill up his special meter. When at least one notch is full, holding the bomb button will charge up energy, and releasing the button will fire it.",
	];


	let x=3;
	let centerx=x+320/2;
	let y=480-170;

	if(num==choice&&drawingnum!=choice){
		drawingnum=choice;

		ClearTextImmediately(401);
		ClearTextImmediately(402);

		drawingdescription=true;
		SetFontStyle(0,0,0,0,0,0);
		SetFontColorNew(255,255,255,255,255,255);
		if(num==6){DrawTextBoldObj(401,"(Bonus Character: no dialogue, and the story won't make any sense)",imgText,320,272,0,2.5,2.5,8,0,255,0.5);}
		SetFontColorNew(255,255,255,NameColor[num][0],NameColor[num][1],NameColor[num][2]);
		SetFontColorEmphasis(255,255,255,NameColor2[num][0],NameColor2[num][1],NameColor2[num][2]);

		DrawTextBoldObj(401,"#"~Name[num],imgText,320,285,0,2.5,2.5,8,0,255,0.5);


		if(shotautodesc[num]){
			DrawTextBoldObj(401,"#Shot: "~ShotName[num],imgText,centerx,y,0,2.5,2.5,8,0,255,0.5);
		}else{
			DrawTextBoldObj(401,ShotName[num],imgText,centerx,y,0,2.5,2.5,8,0,255,0.5);
		}
		NextRow;
		DrawTextBoldObj(401,ShotDescA[num],imgText,centerx,y,0,2.5,2.5,8,0,255,0.5);
		NextRow;
		DrawTextBoldObj(401,ShotDescB[num],imgText,centerx,y,0,2.5,2.5,8,0,255,0.5);

		NextSection;
		if(shotautodesc[num]){
			DrawTextBoldObj(401,"#Spell Card 1: "~BombName1[num],imgText,centerx,y,0,2.5,2.5,8,0,255,0.5);
		}else{
			DrawTextBoldObj(401,BombName1[num],imgText,centerx,y,0,2.5,2.5,8,0,255,0.5);
		}
		NextRow;
		DrawTextBoldObj(401,BombDesc1A[num],imgText,centerx,y,0,2.5,2.5,8,0,255,0.5);
		NextRow;
		DrawTextBoldObj(401,BombDesc1B[num],imgText,centerx,y,0,2.5,2.5,8,0,255,0.5);

		NextSection;
		if(shotautodesc[num]){
			DrawTextBoldObj(401,"#Spell Card 2: "~BombName2[num],imgText,centerx,y,0,2.5,2.5,8,0,255,0.5);
		}else{
			DrawTextBoldObj(401,BombName2[num],imgText,centerx,y,0,2.5,2.5,8,0,255,0.5);
		}
		NextRow;
		DrawTextBoldObj(401,BombDesc2A[num],imgText,centerx,y,0,2.5,2.5,8,0,255,0.5);
		NextRow;
		DrawTextBoldObj(401,BombDesc2B[num],imgText,centerx,y,0,2.5,2.5,8,0,255,0.5);

		y=480-170;
		x=320;
		centerx=320*1.5;

		DrawTextBoldObj(401,"#Unfocused speed:# "~DecimalString(UnfocusedSpeed[num]),imgText,x+5,y,0,2.5,2.5,8,1,255,0.5);
		NextRow;
		DrawTextBoldObj(401,"#  Focused speed:# "~DecimalString(FocusedSpeed[num]),imgText,x+5,y,0,2.5,2.5,8,1,255,0.5);
		DrawTextBoldObj(401,"# Spell Cards:# "~StartingBombs[num],imgText,centerx+20,y,0,2.5,2.5,8,1,255,0.5);

		NextSection;
		let strings=WrapText("#Other notes: #"~Notes[num],39);
		let numstrings=0;
		while(numstrings<length(strings)){
			DrawTextBoldObj(401,strings[numstrings],imgText,x+5,y,0,2.45,2.5,8,1,255,0.5);
			numstrings++;
			y+=13;
			if(y%2==0){yield;}
		}


//		DrawTextNew(value,string,font,x,y,angle,xsize,ysize,layer,align,alpha,delay);


		Wait(10);
		drawingdescription=false;

	}

	sub NextRow{
		y+=14;
		yield;
	}
	sub NextSection{
		y+=22;
		yield;
	}

}
task BlackBox{
	let objblack=InitEffect(0,0,stgwhite,4,ALPHA,8);
	let obj=InitEffect(0,480-179,imgStatbox,4,ADD,8);

	ObjEffect_SetXYRect(objblack,0,0,640,480);
	ObjEffect_SetUVRect(objblack,0,0,640,480);

	ObjEffect_SetXYRect(obj,0,0,640,179);
	ObjEffect_SetUVRect(obj,0,0,640,179);

	let color1=[0,0,0];
	let color2=[0,0,0];
	let frame=0;
	let alpha=5;
	while(keeplooping||ret==1){
		if(alpha<255){
			alpha+=10;
		}
		color1=RainbowArray(frame,60,105);
		color2=RainbowArray(frame+90,60,105);
		frame-=1;
		ObjEffect_SetVertexColor(obj,0,alpha,color1[0],color1[1],color1[2]);
		ObjEffect_SetVertexColor(obj,1,alpha,color2[0],color2[1],color2[2]);
		ObjEffect_SetVertexColor(obj,2,alpha,color2[0],color2[1],color2[2]);
		ObjEffect_SetVertexColor(obj,3,alpha,color1[0],color1[1],color1[2]);

		Obj_SetColor(objblack,4,alpha*0.8,0,0,0);
		yield;
	}
	while(alpha>0){
		alpha-=10;
		color1=RainbowArray(frame,100,105);
		color2=RainbowArray(frame+90,100,105);
		frame-=1;
		ObjEffect_SetVertexColor(obj,0,alpha,color1[0],color1[1],color1[2]);
		ObjEffect_SetVertexColor(obj,1,alpha,color2[0],color2[1],color2[2]);
		ObjEffect_SetVertexColor(obj,2,alpha,color2[0],color2[1],color2[2]);
		ObjEffect_SetVertexColor(obj,3,alpha,color1[0],color1[1],color1[2]);

		Obj_SetColor(objblack,4,alpha*0.8,0,0,0);
		yield;
	}
	Obj_Delete(obj);
	Obj_Delete(objblack);
}

task DrawScrollArrow(dir,page){
	page*=pagewidth;
	let obj=InitEffect(320,140,imgCharselect,4,ADD,8);
	ObjEffect_SetXYRect(obj,-32,-40,0,40);
	ObjEffect_SetScale(obj,dir,1);
	let obj2=InitEffect(320,140,imgCharselect,4,ADD,8);
	ObjEffect_SetXYRect(obj2,-32,-40,0,40);
	ObjEffect_SetScale(obj2,dir,1);
	let anim;
	let frame=0;
	let alpha;
	while(keeplooping){
		Obj_SetX(obj,round(320+300*dir+sin(frame*7)*5-xscroll+page));
		Obj_SetX(obj2,round(320+300*dir+sin(frame*7)*5-xscroll+page));
		alpha=sin(frame*(180/8))*127+127;
		Obj_SetColor(obj,4,(alpha)*     upper(1-absolute(xscroll-page)/(pagewidth/1.5),0),255,255,255);
		Obj_SetColor(obj2,4,(254-alpha)*upper(1-absolute(xscroll-page)/(pagewidth/1.5),0),255,255,255);
		if(frame%16==0){
			anim=(frame/8)%8;
			ObjEffect_SetUVRect(obj,403+anim*32,410,435+anim*32,490);
		}
		if(frame%16==8){
			anim=(frame/8)%8;
			ObjEffect_SetUVRect(obj2,403+anim*32,410,435+anim*32,490);
		}
		frame++;
		yield;
	}
	Obj_Delete(obj);
	Obj_Delete(obj2);
}
}//nested function