
let MENU_END=0;
let MENU_TOP=1;
let MENU_LOCATION=2;
let MENU_LOCATION_SETTING=3;
let MENU_MEMORY=4;
let MENU_LIST=5;
let MENU_TUNING=6;
let MENU_DIFFICULTY=7;


function GameMenu
{
	SetMusic(bgm[0],BGM_LOAD);
	SetMusic(bgm[0],BGM_PLAY);
	let menustate=1;
	let menu_from=MENU_TOP;
	let menu_number=0;
	let effect_l=2;
	
	let from_wait=10;
	let frame_wait=5;
	
	let t_flg=memory_flg[9];
	
	let location_flg=false;
	let location_setting_red=memory_others[4];
	let location_setting_base=memory_others[5];
	if(location_setting_base==0){location_setting_base=10000;}
	
	
	Menu_BackGround(32,16,416,464);
	Menu_Text(t_flg);
	MenuState;
	Menu_Top(t_flg);
	
	
	SetMusic(bgm[0],BGM_FADEOUT_10);
	
	menu_flg=false;
	
	
	task MenuState
	{
		while(menu_flg)
		{
			ForbidShot(true);
			ForbidBomb(true);
			SetPlayerX(cenX);
			SetPlayerY(380);
			yield;
		}
		ForbidShot(false);
		ForbidBomb(false);
		
		alternative(gamemode)
		case(0)
		{
			extend_1up=1400;
			extend_bomb=800;
		}
		case(1)
		{
			extend_1up=800;
			extend_bomb=600;
		}
		case(2)
		{
			extend_1up=1000;
			extend_bomb=700;
		}
		case(3)
		{
			extend_1up=2000;
			extend_bomb=1000;
		}
		
		if(location_flg)
		{
			alternative(location_setting_red)
			case(0){red_num=0;}
			case(1){red_num=5001;}
			case(2){red_num=8001;}
			case(3){red_num=10000;}
			
			itempoint=location_setting_base;
		}
	}
	
	
	function Menu_Top(let trueflg)
	{
		let objlist=[];
		let max=5;
		if(trueflg){max=6;}
		ascent(a in 0..max)
		{
			let rect=[0,256,224,288];
			if(trueflg)
			{
				alternative(a)
				case(1){rect=[0,288,224,320];}
				case(2){rect=[0,320,144,352];}
				case(3){rect=[0,352,256,384];}
				case(4){rect=[0,384,80,416];}
				case(5){rect=[0,416,80,448];}
			}
			else
			{
				alternative(a)
				case(1){rect=[0,320,144,352];}
				case(2){rect=[0,352,256,384];}
				case(3){rect=[0,384,80,416];}
				case(4){rect=[0,416,80,448];}
			}
			
			let sx=60+a*30;
			let sy=80+a*45;
			
			let obj=[SetObjectText(sx,sy,rect,a,MENU_TOP)];
			objlist=objlist~obj;
		}
		while(menu_from>=MENU_TOP)
		{
			location_flg=false;
			fullsequence_flg=true;
			if(GetKeyState(VK_UP)==KEY_PUSH||GetKeyState(VK_LEFT)==KEY_PUSH){menu_number--;PlaySE(se[SE_MENU_SELECT]);}
			if(GetKeyState(VK_DOWN)==KEY_PUSH||GetKeyState(VK_RIGHT)==KEY_PUSH){menu_number++;PlaySE(se[SE_MENU_SELECT]);}
			
			if(menu_number>=max){menu_number-=max;}
			else if(menu_number<0){menu_number+=max;}
			
			
			if(GetKeyState(VK_SHOT)==KEY_PUSH)
			{
				PlaySE(se[SE_MENU_DECIDE]);
				let GAMESTART=0;
				let PHANTASM=1;
				let LOCATION=2;
				let RESULT=3;
				let CONFIG=4;
				let EXIT=5;
				if(!trueflg)
				{
					LOCATION=1;
					RESULT=2;
					CONFIG=3;
					EXIT=4;
					PHANTASM=99;
				}
				alternative(menu_number)
				case(GAMESTART)		//FullSequenceOrder
				{
					SetEffect(GAMESTART);
					loop(from_wait){yield;}
					if(tuning_difficulty==3)
					{
						menu_from=MENU_DIFFICULTY;
						
						Menu_Difficulty(objlist[GAMESTART],MENU_TOP);
					}
					else
					{
						menu_from=MENU_END;
						gamemode=tuning_difficulty;
					}
					menu_playstage=0;
				}
				case(PHANTASM)		//PhantasmagoriaTrues
				{
				}
				case(LOCATION)		//AreaLocation
				{
					SetEffect(LOCATION);
					loop(from_wait){yield;}
					menu_from=MENU_LOCATION;
					let frame=Menu_Frame(Obj_GetX(objlist[LOCATION])-128,Obj_GetY(objlist[LOCATION]),2.5,1.8,MENU_LOCATION);
					Menu_Location(frame);
				}
				case(RESULT)		//Archivement of Memory
				{
					SetEffect(RESULT);
					loop(from_wait){yield;}
					menu_from=MENU_MEMORY;
					let frame=Menu_Frame(-16,-16,6,4,MENU_MEMORY);
					
					Menu_Memory(frame);
				}
				case(CONFIG)		//Tuning
				{
					SetEffect(CONFIG);
					loop(from_wait){yield;}
					menu_from=MENU_TUNING;
					let frame=Menu_Frame(Obj_GetX(objlist[CONFIG])+48,Obj_GetY(objlist[CONFIG])-64,2.4,1.8,MENU_TUNING);
					Menu_Tuning(frame);
				}
				case(EXIT)			//Away
				{
					SetEffect(EXIT);
					loop(60){yield;}
					SetClearFrameEffectEnable(false);
					ClearStage;
				}
			}
			
			task SetEffect(let number)
			{
				let rect=[0,256,256,288];
				if(trueflg)
				{
					alternative(number)
					case(1){rect=[0,288,224,320];}
					case(2){rect=[0,320,144,352];}
					case(3){rect=[0,352,256,384];}
					case(4){rect=[0,384,80,416];}
					case(5){rect=[0,416,80,448];}
				}
				else
				{
					alternative(number)
					case(1){rect=[0,320,144,352];}
					case(2){rect=[0,352,256,384];}
					case(3){rect=[0,384,80,416];}
					case(4){rect=[0,416,80,448];}
				}
				
				loop(effect_l)
				{
					SetObjectTextEffect(objlist[number],rect);
					yield;
				}
			}
			
			yield;
		}
		
		Menu_WhiteOut;
		wait(20);
	}
	
	
	
	
	
	function Menu_Location(let base)
	{
		menu_number=0;
		wait(frame_wait);
		let objlist=[];
		let max=10;
		ascent(a in 0..max)
		{
			let rect=[432,320,512,352];//1
			alternative(a)
			case(1){rect=[432,352,512,384];}//2
			case(2){rect=[432,384,512,416];}//3
			case(3){rect=[432,416,512,448];}//4
			case(4){rect=[432,448,512,480];}//5
			case(5){rect=[432,480,512,512];}//6
			
			case(6){rect=[256,416,416,448];}//u1
			case(7){rect=[256,448,416,480];}//u2
			case(8){rect=[256,480,416,512];}//u3
			
			case(9){rect=[256,320,336,352];}//setting
			let sx=Obj_GetX(base)-140;
			let sy=Obj_GetY(base)-75+a*32;
			if(a>=6)
			{
				sx=Obj_GetX(base)-10;
				sy=Obj_GetY(base)-75+(a-3)*32;
			}
			if(a==9)
			{
				sx=Obj_GetX(base)-10;
				sy=Obj_GetY(base)-75;
			}
			
			let obj=[SetObjectText(sx,sy,rect,a,MENU_LOCATION)];
			objlist=objlist~obj;
		}
		while(menu_from>=MENU_LOCATION)
		{
			location_flg=true;
			fullsequence_flg=false;
			if(GetKeyState(VK_UP)==KEY_PUSH)
			{
				if(menu_number==0){menu_number=5;}
				else if(menu_number==6){menu_number=9;}
				else if(menu_number==9){menu_number=8;}
				else{menu_number--;}
				PlaySE(se[SE_MENU_SELECT]);
			}
			if(GetKeyState(VK_DOWN)==KEY_PUSH)
			{
				if(menu_number==5){menu_number=0;}
				else if(menu_number==8){menu_number=9;}
				else if(menu_number==9){menu_number=6;}
				else{menu_number++;}
				PlaySE(se[SE_MENU_SELECT]);
			}
			if(GetKeyState(VK_LEFT)==KEY_PUSH||GetKeyState(VK_RIGHT)==KEY_PUSH)
			{
				if(menu_number<3){menu_number=9;}
				else if(menu_number<6){menu_number+=3;}
				else if(menu_number==9){menu_number=0;}
				else{menu_number-=3;}
				PlaySE(se[SE_MENU_SELECT]);
			}
			
			
			
			if(GetKeyState(VK_BOMB)==KEY_PUSH)
			{
				PlaySE(se[SE_MENU_CANCEL]);
				menu_from=MENU_TOP;
				menu_number=0;
			//	loop(10){yield;}
			}
			
			if(GetKeyState(VK_SHOT)==KEY_PUSH)
			{
				loop(1)
				{
					alternative(menu_number)
					case(0){if(!area1_standby){break;}}
					case(1){if(!area2_standby){break;}}
					case(2){if(!area3_standby){break;}}
					case(3){if(!area4_standby){break;}}
					case(4){if(!area5_standby){break;}}
					case(5){if(!area6_standby){break;}}
					case(6){if(!unseenarea1_standby){break;}}
					case(7){if(!unseenarea2_standby){break;}}
					case(8){if(!unseenarea3_standby){break;}}
					if(!memory_flg[menu_number]&&menu_number!=9){break;}
					PlaySE(se[SE_MENU_DECIDE]);
					SetEffect(menu_number);
					loop(from_wait){yield;}
					menu_playstage=menu_number+1;
					if(menu_number==9)
					{
						menu_from=MENU_LOCATION_SETTING;
						let frame=Menu_Frame(Obj_GetX(base)+80,Obj_GetY(base)-30,2.4,0.8,MENU_LOCATION_SETTING);
						Menu_LocationSetting(frame);
					}
					else if(tuning_difficulty==3)
					{
						menu_from=MENU_DIFFICULTY;
						Menu_Difficulty(objlist[menu_number],MENU_LOCATION);
					}
					else
					{
						menu_from=MENU_END;
						gamemode=tuning_difficulty;
					}
				}
			}
			yield;
		}
		
		task SetEffect(let number)
		{
			let rect=[432,320,512,352];//1
			alternative(number)
			case(1){rect=[432,352,512,384];}//2
			case(2){rect=[432,384,512,416];}//3
			case(3){rect=[432,416,512,448];}//4
			case(4){rect=[432,448,512,480];}//5
			case(5){rect=[432,480,512,512];}//6
			
			case(6){rect=[256,416,416,448];}//u1
			case(7){rect=[256,448,416,480];}//u2
			case(8){rect=[256,480,416,512];}//u3
			case(9){rect=[256,320,336,352];}//setting
			
			loop(effect_l)
			{
				SetObjectTextEffect(objlist[number],rect);
				yield;
			}
		}
	}
	
	function Menu_LocationSetting(let base)
	{
		menu_number=0;
		wait(frame_wait);
		let objlist=[];
		let max=2;
		ascent(a in 0..max)
		{
			let rect=[256,352,320,384];//red
			alternative(a)
			case(1){rect=[256,384,320,416];}//base
			
			let sx=Obj_GetX(base)-120;
			let sy=Obj_GetY(base)-0+a*40;
			
			let obj=[SetObjectText(sx,sy,rect,a,MENU_LOCATION_SETTING)];
			objlist=objlist~obj;
			Select(a,sx,sy);
		}
		
		let trg_delay=20;
		while(menu_from>=MENU_LOCATION_SETTING)
		{
			if(GetKeyState(VK_UP)==KEY_PUSH){menu_number--;PlaySE(se[SE_MENU_SELECT]);}
			if(GetKeyState(VK_DOWN)==KEY_PUSH){menu_number++;PlaySE(se[SE_MENU_SELECT]);}
			
			if(menu_number>=max){menu_number-=max;}
			else if(menu_number<0){menu_number+=max;}
			
			if(GetKeyState(VK_BOMB)==KEY_PUSH)
			{
				PlaySE(se[SE_MENU_CANCEL]);
				menu_from=MENU_LOCATION;
				menu_number=0;
				SetCommonData("Memory_Others",memory_others);
			//	loop(10){yield;}
			}
			
			alternative(menu_number)
			case(0)//red
			{
				if(GetKeyState(VK_LEFT)==KEY_PUSH)
				{
					location_setting_red--;
					PlaySE(se[SE_MENU_SELECT]);
				}
				if(GetKeyState(VK_RIGHT)==KEY_PUSH)
				{
					location_setting_red++;
					PlaySE(se[SE_MENU_SELECT]);
				}
				
				location_setting_red=(location_setting_red+4)%4;
				
				memory_others[4]=location_setting_red;
			}
			case(1)//base
			{
				if(GetKeyState(VK_LEFT)==KEY_PUSH)
				{
					location_setting_base-=10000;
					PlaySE(se[SE_MENU_SELECT]);
				}
				if(GetKeyState(VK_RIGHT)==KEY_PUSH)
				{
					location_setting_base+=10000;
					PlaySE(se[SE_MENU_SELECT]);
				}
				
				if(GetKeyState(VK_LEFT)==KEY_HOLD)
				{
					if(trg_delay<=0)
					{
						location_setting_base-=10000;
						PlaySE(se[SE_MENU_SELECT]);
					}
					trg_delay--;
				}
				
				if(GetKeyState(VK_RIGHT)==KEY_HOLD)
				{
					if(trg_delay<=0)
					{
						location_setting_base+=10000;
						PlaySE(se[SE_MENU_SELECT]);
					}
					trg_delay--;
				}
				
				
				if(GetKeyState(VK_RIGHT)==KEY_FREE&&GetKeyState(VK_LEFT)==KEY_FREE){trg_delay=30;}
				
				
				if(location_setting_base<10000){location_setting_base=10000;}
				if(location_setting_base>1000000){location_setting_base=1000000;}
				
				memory_others[5]=location_setting_base;
			}
			yield;
		}
		
		task Select(let num,let sx,let sy)
		{
			let obj=SetEffectObject(tex[TEX_SYSTEM],8);
			ObjEffect_SetRenderState(obj,ALPHA);
			sx+=150;
			let rect=[];
			let alpha=255;
			while(!Obj_BeDeleted(obj))
			{
				alternative(num)
				case(0)
				{
					alternative(location_setting_red)
					case(0){rect=[336,352,384,384];}//0%
					case(1){rect=[384,352,432,384];}//50%
					case(2){rect=[336,384,384,416];}//80%
					case(3){rect=[384,384,432,416];}//max
					
					if(num==menu_number){alpha=255;}
					else{alpha=48;}
					SetEffectObjectRect(obj,rect[0],rect[1],rect[2],rect[3]);
					Obj_SetPosition(obj,sx+(rect[2]-rect[0])/2,sy+(rect[1]-rect[3])/2);
					ascent(a in 0..4)
					{
						ObjEffect_SetVertexColor(obj,a,alpha,255,255,255);
					}
					if(menu_from<MENU_LOCATION_SETTING){Obj_Delete(obj);}
				}
				case(1)
				{
					let lp=floor(log10(location_setting_base))+1;
					let i=lp;
					loop(lp)
					{
						let leng=16;
						let posx=sx+(lp-i)*(leng/1.4);
						let posy=sy-16;
						let rc=(location_setting_base%(10^i)-location_setting_base%(10^(i-1)))/(10^(i-1));
						
						Number(rc,posx,posy);
						i--;
					}
					if(menu_from<MENU_LOCATION_SETTING){Obj_Delete(obj);}
				}

				yield;
			}
			
			task Number(let num,let x,let y)
			{
				let obj=SetEffectObject(tex[TEX_SYSTEM],8);
				SetEffectObjectRect(obj,256+16*num,32,272+16*num,48);
				let alpha=128;
				if(menu_number!=1){alpha=16;}
				let count=0;
				while(!Obj_BeDeleted(obj))
				{
					ObjEffect_SetRenderState(obj,ADD);
					Obj_SetPosition(obj,x,y);
					ObjEffect_SetScale(obj,1.5,1.5);
					ascent(a in 0..4)
					{
						ObjEffect_SetVertexColor(obj,a,alpha,255,255,255);
					}
					if(count>1||menu_from<MENU_LOCATION_SETTING){Obj_Delete(obj);}
					count++;
					yield;
				}
			}
		}
	}
	
	function Menu_Memory(let base)
	{
		let SCORE=0;
		let AREA=1;
		let STANDARD=2;
		let ADVANCED=3;
		let UNLIMITED=4;
		let PHANTASM=5;
		let PLAYTIME=6;
		let PLAYCOUNT=7;
		wait(frame_wait);
		local
		{
			let vx=240;let vy=24;
			let yp=16;
			//standard
			let sx=40;
			let sy=60;
			//if(!memory_flg[9]&&!memory_flg[10]){sy=180;}
			ObjectText(STANDARD,sx,sy-vy*2);
			ObjectText(SCORE,sx+60,sy-vy);
			ObjectText(AREA,sx+vx,sy-vy);
			ascent(a in 0..10)
			{
				Set(memory_highscore[a],sx,sy);
				Number(memory_highscore_stage[a],sx+vx,sy);
				sy+=yp;
			}
			
			//advanced
			sx=320+40;
			sy=60;
			//if(!memory_flg[9]&&!memory_flg[10]){sy=180;}
			ObjectText(ADVANCED,sx,sy-vy*2);
			ObjectText(SCORE,sx+60,sy-vy);
			ObjectText(AREA,sx+vx,sy-vy);
			ascent(a in 10..20)
			{
				Set(memory_highscore[a],sx,sy);
				Number(memory_highscore_stage[a],sx+vx,sy);
				sy+=yp;
			}
			
			//unlimited
			if(1)//if(memory_flg[10])
			{
				sx=40;
				if(!memory_flg[9]){sx=200;}
				sy=290;
				ObjectText(UNLIMITED,sx,sy-vy*2);
				ObjectText(SCORE,sx+60,sy-vy);
				ObjectText(AREA,sx+vx,sy-vy);
				ascent(a in 20..30)
				{
					Set(memory_highscore[a],sx,sy);
					Number(memory_highscore_stage[a],sx+vx,sy);
					sy+=yp;
				}
			}
				
			
			//phantasmagoria
			if(0)//if(memory_flg[9])
			{
				sx=320+40;
				sy=290;
				if(!memory_flg[10]){sx=200;}
				ObjectText(PHANTASM,sx,sy-vy*2);
				ObjectText(SCORE,sx+60,sy-vy);
				ObjectText(AREA,sx+vx,sy-vy);
				ascent(a in 30..40)
				{
					Set(memory_highscore[a],sx,sy);
					Number(memory_highscore_stage[a],sx+vx,sy);
					sy+=yp;
				}
			}
			local
			{
				sx=100;
				sy=460;
				ObjectText(PLAYTIME,sx,sy);
				Set(timer_hours,sx+60,sy);
				Number(11,sx+90,sy);
				Set(timer_minutes,sx+105,sy);
				Number(11,sx+130,sy);
				Set(timer_seconds,sx+150,sy);
				
				sx=400;
				ObjectText(PLAYCOUNT,sx,sy);
				Set(play_count,sx+70,sy);
			}
		}
		
		task Set(let scorenum,let sx,let sy)
		{
			let lp=floor(log10(scorenum))+1;
			let i=lp;
			loop(lp)
			{
				let leng=16;
				let posx=sx+(lp-i)*(leng/1.4);
				let posy=sy;
				let rc=(scorenum%(10^i)-scorenum%(10^(i-1)))/(10^(i-1));
				
				Number(rc,posx,posy);
				
				if((i)%4==1&&i>1)
				{
					Number(10,posx,posy);
				}
				i--;
			}
			if(scorenum==0)
			{
				Number(0,sx,sy);
			}
		}
		task ObjectText(let type,let sx,let sy)
		{
			let rect=[];
			alternative(type)
			case(SCORE){rect=[0,48,48,64];}
			case(AREA){rect=[448,32,496,48];}
			case(STANDARD){rect=[256,48,320,64];}
			case(ADVANCED){rect=[320,48,384,64];}
			case(UNLIMITED){rect=[384,48,448,64];}
			case(PHANTASM){rect=[448,48,512,64];}
			case(PLAYTIME){rect=[256,128,320,144];}
			case(PLAYCOUNT){rect=[320,128,400,144];}
			let obj=SetEffectObject(tex[TEX_SYSTEM],8);
			ObjEffect_SetRenderState(obj,ALPHA);
			SetEffectObjectRect(obj,rect[0],rect[1],rect[2],rect[3]);
			Obj_SetPosition(obj,sx,sy);
			while(!Obj_BeDeleted(obj))
			{
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,255,255,255,255);
				}
				if(menu_from<MENU_MEMORY){Obj_Delete(obj);}
				yield;
			}
		}
		task Number(let num,let x,let y)
		{
			let obj=SetEffectObject(tex[TEX_SYSTEM],8);
			ObjEffect_SetRenderState(obj,ALPHA);
			SetEffectObjectRect(obj,256+16*num,0,272+16*num,16);
			Obj_SetPosition(obj,x,y);
			while(!Obj_BeDeleted(obj))
			{
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,255,255,255,255);
				}
				if(menu_from<MENU_MEMORY){Obj_Delete(obj);}
				yield;
			}
		}
		
		
		while(menu_from>=MENU_MEMORY)
		{
			if(GetKeyState(VK_SHOT)==KEY_PUSH)
			{
				menu_from=MENU_LIST;
				let frame=Menu_Frame(-16,-16,6,4,MENU_LIST);
				menu_number=0;
				Menu_SpellList(frame);
			}
			if(GetKeyState(VK_BOMB)==KEY_PUSH)
			{
				PlaySE(se[SE_MENU_CANCEL]);
				menu_from=MENU_TOP;
				menu_number=0;
				loop(10){yield;}
			}
			yield;
		}
	}
	
	function Menu_SpellList(let base)
	{
		wait(frame_wait);
		
		function ListCheck(let st,let ed)
		{
			let flg=0;
			while(!flg)
			{
				
				ascent(a in st..ed)
				{
					let data_total=GetCommonData("SpellResult_TotalCount");
					if(data_total[a]>0){flg=1;}
				}
			}
		}
		
		task SpellList(let st,let ed,let from)
		{
			ObjectText(0,16,16,from);
			ObjectText(1,180,16,from);
			ObjectText(2,470,16,from);
			let sy=40;
			ascent(a in st..ed)
			{
				
				let num=a+1;
				let lp=floor(log10(num))+1;
				let leng=16;
				let i=lp;
				loop(lp)
				{
					let posx=16+(lp-i)*(leng/1.4);
					let rc=(num%(10^i)-num%(10^(i-1)))/(10^(i-1));
					Number(rc,posx,sy,from);
					i--;
				}
				
				SpellName(a,310,sy,from);
				SetResult(a,420,sy,from);
				sy+=20;
			}
		}
		task SetResult(let num,let sx,let sy,let from)
		{
			let data_total=GetCommonData("SpellResult_TotalCount");
			let data_get=GetCommonData("SpellResult_GetCount");
			let getnum=data_get[num];
			let totalnum=data_total[num];
			let posy=sy;
			if(totalnum>0)
			{
				let lp=floor(log10(getnum))+1;
				let leng=16;
				let i=lp;
				loop(lp)
				{
					let posx=sx+(lp-i)*(leng/1.4);
					let rc=(getnum%(10^i)-getnum%(10^(i-1)))/(10^(i-1));
					Number(rc,posx,posy,from);
					i--;
				}
				if(getnum==0)
				{
					Number(0,sx,sy,from);
				}
				sx+=48;
				Number(10,sx,sy,from);
				
				sx+=32;
				let lp=floor(log10(totalnum))+1;
				let i=lp;
				loop(lp)
				{
					let posx=sx+(lp-i)*(leng/1.4);
					let rc=(totalnum%(10^i)-totalnum%(10^(i-1)))/(10^(i-1));
					Number(rc,posx,posy,from);
					i--;
				}
				if(totalnum==0)
				{
					Number(0,sx,sy,from);
				}
				
				
				sx+=64;
				let pct=0;
				if(getnum>0)
				{
					pct=(getnum/totalnum)*100;
					if(pct>50){pct=floor(pct);}
					else{pct=ceil(pct);}
				}
				let lp=floor(log10(pct))+1;
				let i=lp;
				loop(lp)
				{
					let posx=sx+(lp-i)*(leng/1.4);
					let rc=(pct%(10^i)-pct%(10^(i-1)))/(10^(i-1));
					Number(rc,posx,posy,from);
					i--;
				}
				if(pct==0)
				{
					Number(0,sx,sy,from);
				}
				Number(11,sx+40,sy,from);
				
			}
			
		}
		
		task SpellName(let num,let x,let y,let from)
		{
			let flg=0;
			let rc=0;

			if(memory_spell_totalcount[num]==0){flg=1;}
			while(num>=64){rc++;num-=64;}
			let obj=SetEffectObject(tex[TEX_SPELLNAME],8);
			ObjEffect_SetRenderState(obj,ALPHA);
			if(flg==1){SetEffectObjectRect(obj,1536,1984,2048,2016);}
			else{SetEffectObjectRect(obj,0+512*rc,0+32*num,512+512*rc,32+32*num);}
			Obj_SetPosition(obj,x,y);
			while(!Obj_BeDeleted(obj))
			{
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,255,255,255,255);
				}
				if(menu_number!=from||menu_from<MENU_LIST){Obj_Delete(obj);}
				yield;
			}
		}
		task Number(let num,let x,let y,let from)
		{
			let obj=SetEffectObject(tex[TEX_SPELLNAME],8);
			ObjEffect_SetRenderState(obj,ALPHA);
			SetEffectObjectRect(obj,1728+16*num,2016,1744+16*num,2032);
			Obj_SetPosition(obj,x,y);
			while(!Obj_BeDeleted(obj))
			{
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,255,255,255,255);
				}
				if(menu_number!=from||menu_from<MENU_LIST){Obj_Delete(obj);}
				yield;
			}
		}
		
		task ObjectText(let type,let sx,let sy,let from)
		{
			let rect=[];
			alternative(type)
			case(0){rect=[1536,2016,1600,2048];}
			case(1){rect=[1600,2016,1664,2048];}
			case(2){rect=[1664,2016,1728,2048];}
			let obj=SetEffectObject(tex[TEX_SPELLNAME],8);
			ObjEffect_SetRenderState(obj,ALPHA);
			SetEffectObjectRect(obj,rect[0],rect[1],rect[2],rect[3]);
			Obj_SetPosition(obj,sx,sy);
			while(!Obj_BeDeleted(obj))
			{
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,255,255,255,255);
				}
				if(menu_number!=from||menu_from<MENU_LIST){Obj_Delete(obj);}
				yield;
			}
		}
		
		local
		{
			SpellList(0,12,0);
		}
		
		while(menu_from>=MENU_LIST)
		{
			if(GetKeyState(VK_LEFT)==KEY_PUSH||GetKeyState(VK_RIGHT)==KEY_PUSH)
			{
				let max=11;
				if(GetKeyState(VK_LEFT)==KEY_PUSH){menu_number--;}
				if(GetKeyState(VK_RIGHT)==KEY_PUSH){menu_number++;}
				menu_number=(menu_number+max)%max;
				
				alternative(menu_number)
				case(0){SpellList(0,12,menu_number);}//area1
				case(1){SpellList(12,27,menu_number);}//area2
				case(2){SpellList(27,45,menu_number);}//area3
				case(3){SpellList(45,60,menu_number);}//area4
				case(4){SpellList(60,70,menu_number);}//area4another
				case(5){SpellList(70,91,menu_number);}//area5
				case(6){SpellList(91,110,menu_number);}//area6
				case(7){SpellList(110,122,menu_number);}//unseen1
				case(8){SpellList(122,138,menu_number);}//unseen2
				case(9){SpellList(138,158,menu_number);}//unseen3
				case(10){SpellList(158,180,menu_number);}//phatasm
			}
			if(GetKeyState(VK_BOMB)==KEY_PUSH)
			{
				PlaySE(se[SE_MENU_CANCEL]);
				menu_from=MENU_MEMORY;
				loop(10){yield;}
			}
			yield;
		}	
	}
	
	
	
	function Menu_Tuning(let base)
	{
		menu_number=0;
		wait(frame_wait);
		let objlist=[];
		let max=5;
		ascent(a in 0..max)
		{
			let rect=[256,160,384,192];//difficulty
			alternative(a)
			case(1){rect=[256,192,384,224];}//lightmode
			case(2){rect=[256,224,384,256];}//playmusic
			case(3){rect=[256,256,384,288];}//continue
			case(4){rect=[256,288,384,320];}//datareset
			
			let sx=Obj_GetX(base)-120;
			let sy=Obj_GetY(base)-60+a*40;
			
			let obj=[SetObjectText(sx,sy,rect,a,MENU_TUNING)];
			objlist=objlist~obj;
			if(a<4){Select(a,sx,sy);}
		}
		
		let reset_trg=60;
		while(menu_from>=MENU_TUNING)
		{
			if(GetKeyState(VK_UP)==KEY_PUSH){menu_number--;PlaySE(se[SE_MENU_SELECT]);}
			if(GetKeyState(VK_DOWN)==KEY_PUSH){menu_number++;PlaySE(se[SE_MENU_SELECT]);}
			
			if(menu_number>=max){menu_number-=max;}
			else if(menu_number<0){menu_number+=max;}
			
			if(GetKeyState(VK_BOMB)==KEY_PUSH)
			{
				PlaySE(se[SE_MENU_CANCEL]);
				menu_from=MENU_TOP;
				menu_number=0;
				SetCommonData("Memory_ConfigData",memory_tuning);
			//	loop(10){yield;}
			}
			
			alternative(menu_number)
			case(0)
			{
				if(GetKeyState(VK_LEFT)==KEY_PUSH){tuning_difficulty--;PlaySE(se[SE_MENU_SELECT]);}
				if(GetKeyState(VK_RIGHT)==KEY_PUSH){tuning_difficulty++;PlaySE(se[SE_MENU_SELECT]);}
				
				tuning_difficulty=(tuning_difficulty+4)%4;
				memory_tuning[menu_number]=tuning_difficulty;
			}
			case(1)
			{
				if(GetKeyState(VK_LEFT)==KEY_PUSH||GetKeyState(VK_RIGHT)==KEY_PUSH){tuning_lightmode++;PlaySE(se[SE_MENU_SELECT]);}
				tuning_lightmode=tuning_lightmode%2;
				memory_tuning[menu_number]=tuning_lightmode;
			}
			
			case(2)
			{
				if(GetKeyState(VK_LEFT)==KEY_PUSH||GetKeyState(VK_RIGHT)==KEY_PUSH){tuning_playmusic++;PlaySE(se[SE_MENU_SELECT]);}
				tuning_playmusic=tuning_playmusic%2;
				memory_tuning[menu_number]=tuning_playmusic;
			}
			case(3)
			{
				if(GetKeyState(VK_LEFT)==KEY_PUSH||GetKeyState(VK_RIGHT)==KEY_PUSH){tuning_continue++;PlaySE(se[SE_MENU_SELECT]);}
				tuning_continue=tuning_continue%2;
				memory_tuning[menu_number]=tuning_continue;
			}
			case(4)
			{
				if(GetKeyState(VK_SHOT)==KEY_HOLD){reset_trg--;}
				else{reset_trg=60;}
				
				if(reset_trg<0)
				{
					PlaySE(se[SE_MENU_DECIDE]);
					menu_from=MENU_TOP;
					menu_number=0;
					loop(20){yield;}
					MemoryReset;
				}
			}
			
			yield;
		}
		
		task Select(let num,let sx,let sy)
		{
			let obj=SetEffectObject(tex[TEX_SYSTEM],8);
			ObjEffect_SetRenderState(obj,ALPHA);
			sx+=150;
			let rect=[];
			let alpha=255;
			while(!Obj_BeDeleted(obj))
			{
				alternative(num)
				case(0)
				{
					alternative(tuning_difficulty)
					case(0){rect=[400,160,512,192];}//Standard
					case(1){rect=[400,192,512,224];}//Advanced
					case(2){rect=[400,224,512,256];}//Unlimited
					case(3){rect=[400,256,512,288];}//Select
				}
				case(1)
				{
					if(tuning_lightmode){rect=[448,288,512,320];}
					else{rect=[384,288,448,320];}
				}
				case(2)
				{
					if(tuning_playmusic){rect=[448,288,512,320];}
					else{rect=[384,288,448,320];}
				}
				case(3)
				{
					if(tuning_continue){rect=[448,288,512,320];}
					else{rect=[384,288,448,320];}
				}
				if(num==menu_number){alpha=255;}
				else{alpha=48;}
				SetEffectObjectRect(obj,rect[0],rect[1],rect[2],rect[3]);
				Obj_SetPosition(obj,sx+(rect[2]-rect[0])/2,sy+(rect[1]-rect[3])/2);
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,alpha,255,255,255);
				}
				if(menu_from<MENU_TUNING){Obj_Delete(obj);}
				yield;
			}
		}
	}
	
	
	
	function Menu_Difficulty(let base,let prev)
	{
		menu_number=0;
		let frame=Menu_Frame(Obj_GetX(base)-24,Obj_GetY(base),1,1,MENU_DIFFICULTY);
		wait(frame_wait);
		let objlist=[];
		let max=3;
		ascent(a in 0..max)
		{
			let rect=[400,160,512,192];
			alternative(a)
			case(1){rect=[400,192,512,224];}
			case(2){rect=[400,224,512,256];}
			let sx=Obj_GetX(frame)-55;
			let sy=Obj_GetY(frame)-18+a*36;
			
			let obj=[SetObjectText(sx,sy,rect,a,MENU_DIFFICULTY)];
			objlist=objlist~obj;
		}
		while(menu_from>=MENU_DIFFICULTY)
		{
			if(GetKeyState(VK_UP)==KEY_PUSH){menu_number--;PlaySE(se[SE_MENU_SELECT]);}
			if(GetKeyState(VK_DOWN)==KEY_PUSH){menu_number++;PlaySE(se[SE_MENU_SELECT]);}
			
			if(menu_number>=max){menu_number-=max;}
			else if(menu_number<0){menu_number+=max;}
			
			if(GetKeyState(VK_BOMB)==KEY_PUSH)
			{
				PlaySE(se[SE_MENU_CANCEL]);
				menu_from=prev;
				menu_number=0;
			//	loop(10){yield;}
			}
			
			if(GetKeyState(VK_SHOT)==KEY_PUSH)
			{
				PlaySE(se[SE_MENU_DECIDE]);
				gamemode=menu_number%3;
				SetEffect(gamemode);
				loop(from_wait){yield;}
				menu_from=MENU_END;
			}
			yield;
		}
		
		task SetEffect(let number)
		{
			let rect=[400,160,512,192];
			alternative(number)
			case(1){rect=[400,192,512,224];}
			case(2){rect=[400,224,512,256];}
			
			loop(effect_l)
			{
				SetObjectTextEffect(objlist[number],rect);
				yield;
			}
		}
	}
	
	
	
	function SetObjectText(let x,let y,let rect,let number,let state)
	{
		let obj=SetEffectObject(tex[TEX_SYSTEM],8);
		SetEffectObjectRect(obj,rect[0],rect[1],rect[2],rect[3]);
		ObjEffect_SetRenderState(obj,ALPHA);
		Obj_SetPosition(obj,x+(rect[2]-rect[0])/2,y+(rect[1]-rect[3])/2);
		Run(obj);
		return obj;
		
		task Run(let obj)
		{
			let alpha=80;
			while(!Obj_BeDeleted(obj))
			{
				if(menu_from==state)
				{
					if(menu_number==number){alpha=240;}
					else{alpha=80;}
				}
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,alpha,255,255,255);
				}
				if(menu_from<state){Obj_Delete(obj);}
				yield;
			}
		}
	}
	
	task SetObjectTextEffect(let baseobj,let rect)
	{
		let obj=SetEffectObject(tex[TEX_SYSTEM],8);
		SetEffectObjectRect(obj,rect[0],rect[1],rect[2],rect[3]);
		ObjEffect_SetRenderState(obj,ADD);
		Obj_SetPosition(obj,Obj_GetX(baseobj),Obj_GetY(baseobj));
		
		let xscale=1;
		let alpha=200;
		while(!Obj_BeDeleted(obj))
		{
			ObjEffect_SetScale(obj,xscale,1);
			ascent(a in 0..4)
			{
				ObjEffect_SetVertexColor(obj,a,alpha,255,255,255);
			}
			xscale+=0.05;
			alpha-=10;
			if(alpha<10){Obj_Delete(obj);}
			yield;
		}
	}
	
	
	task Menu_Text(let trueflg)
	{
		Back;
		Text;
		task Text
		{
			let obj=SetEffectObject(tex[TEX_MENUTEXT],8);
			ObjEffect_SetRenderState(obj,ALPHA);
			Obj_SetPosition(obj,256,480-32);
			while(!Obj_BeDeleted(obj))
			{
				let rc=14;
				alternative(menu_from)
				case(MENU_TOP)
				{
					if(trueflg)
					{
						alternative(menu_number)
						case(0){rc=0;}
						case(1){rc=1;}
						case(2){rc=2;}
						case(3){rc=3;}
						case(4){rc=4;}
						case(5){rc=5;}
					}
					else
					{
						alternative(menu_number)
						case(0){rc=0;}
						case(1){rc=2;}
						case(2){rc=3;}
						case(3){rc=4;}
						case(4){rc=5;}
					}
				}
				case(MENU_DIFFICULTY)
				{
					alternative(menu_number)
					case(0){rc=6;}
					case(1){rc=7;}
					case(2){rc=8;}
				}
				case(MENU_TUNING)
				{
					alternative(menu_number)
					case(0){rc=9;}
					case(1){rc=10;}
					case(2){rc=11;}
					case(3){rc=12;}
					case(4){rc=13;}
				}
				
				SetEffectObjectRect(obj,0,64*rc,512,64+64*rc);
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,255,255,255,255);
				}
				
				if(!menu_flg){Obj_Delete(obj);}
				yield;
			}
		}
		
		task Back
		{
			let obj=SetEffectObject(tex[TEX_MENUTEXT],8);
			SetEffectObjectRect(obj,0,960,512,1024);
			ObjEffect_SetRenderState(obj,SUBTRACT);
			Obj_SetPosition(obj,320,480-32);
			while(!Obj_BeDeleted(obj))
			{
				let color=128;
				ObjEffect_SetScale(obj,2,1);
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,255,color,color,color);
				}
				if(menu_from==MENU_END){Obj_Delete(obj);}
				yield;
			}
		}
	}
	
	function Menu_Frame(let x,let y,let xscale,let yscale,let state)
	{
		let obj=SetEffectObject(tex[TEX_SYSTEM],8);
		SetEffectObjectRect(obj,128,128,256,256);
		ObjEffect_SetRenderState(obj,ALPHA);
		Run;
		return obj;
		task Run
		{
			let alpha=220;
			let limit=frame_wait;
			let count=0;
			let xsc=0;
			let ysc=0;
			while(!Obj_BeDeleted(obj))
			{
				let sx=x+64*xsc;
				let sy=y+64*ysc;
				Obj_SetPosition(obj,sx,sy);
				ObjEffect_SetScale(obj,xsc,ysc);
				
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,alpha,255,255,255);
				}
				if(menu_from<state)
				{
					xsc-=xscale/limit;
					ysc-=yscale/limit;
					if(count>=limit){Obj_Delete(obj);}
					count++;
				}
				else
				{
					if(xsc<xscale){xsc+=xscale/limit;}
					if(ysc<yscale){ysc+=yscale/limit;}
				}
				yield;
			}
		}
	}
	
	task Menu_WhiteOut
	{
		loop(8)
		{
			WhiteOut;
			wait(2);
		}
		task WhiteOut
		{
			let obj=SetEffectObject(tex[TEX_WHITE],7);
			SetEffectObjectRect(obj,0,0,320,240);
			ObjEffect_SetRenderState(obj,ADD);
			Obj_SetPosition(obj,cenX,cenY);
			
			let count=0;
			let scale=1;
			let alpha=16;
			while(alpha>2)
			{
				ObjEffect_SetScale(obj,scale,scale);
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,alpha,255,255,255);
				}
				if(count>40){alpha-=10;}
				else if(alpha<250){alpha+=2;}
				scale+=0.2;
				count++;
				yield;
			}
			Obj_Delete(obj);
		}
	}
	
	task Menu_BackGround(let left,let up,let right,let down)
	{
		let obj=SetEffectObject(tex[TEX_FRAMEBACK],7);
		ObjEffect_SetRenderState(obj,ALPHA);
		
		let sx=left+(right-left)/2;
		let sy=up+(down-up)/2;
		let color=255;
		Obj_SetPosition(obj,sx,sy);
		
		let frame_y=0;
		while(!Obj_BeDeleted(obj))
		{
			
			SetEffectObjectRect(obj,left,up+frame_y,right,down+frame_y);
			ascent(a in 0..4)
			{
				ObjEffect_SetVertexColor(obj,a,255,color,color,color);
			}
			frame_y+=-1;
			if(!menu_flg){Obj_Delete(obj);}
			yield;
		}
	}
}
