#include "./../system/Sound.txt"
#include "./../system/Music.txt"
#include "./../system/SpellName.txt"

let result = [];
let PlayerCount = [];
let selectIndex = 0;
let mainSwitch = false;
let backSwitch = 0;
let PlayerTexts = ["Nue Houjou"];
let imgText = [];
let imgFace = [];
let imgX = [0,35];

@Initialize
{
	LoadCommonDataAreaA2("CONFIG",GetCurrentScriptDirectory ~ "../system/data/CONFIG");
	LoadCommonDataAreaA2("STAGE_UNLOCK",GetCurrentScriptDirectory ~ "../system/data/SCORE");	
	SetAutoDeleteObject(true);
	yield;
	ascent(i in 0..length(PlayerTexts))
		{
		imgFace = imgFace ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		imgText = imgText ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		Obj_SetRenderPriority(imgText[i],9);
		Obj_SetRenderPriority(imgFace[i],7);
		ObjPrim_SetTexture(imgFace[i],0);
		ObjPrim_SetTexture(imgText[i],0);
		let textureT = GetCurrentScriptDirectory ~ "../player/pl" ~ IntToString(i) ~ "/Text.png";
		let textureF = GetCurrentScriptDirectory ~ "../player/pl" ~ IntToString(i) ~ "/Face/Face.png";
		ObjPrim_SetTexture(imgText[i],textureT);
		ObjPrim_SetTexture(imgFace[i],textureF);
		ObjSprite2D_SetSourceRect(imgText[i],0,0,GetTextureWidth(textureT)+1,GetTextureHeight(textureT)+1);
		ObjSprite2D_SetSourceRect(imgFace[i],320,0,0,256);
		ObjSprite2D_SetDestRect(imgFace[i],GetScreenWidth-20-4+imgX[i],20,GetScreenWidth/2+20-imgX[i],GetScreenHeight/2+1);
		ObjSprite2D_SetDestRect(imgText[i],GetScreenWidth/2+20,GetScreenHeight/2,GetScreenWidth-20,GetScreenHeight-20);
		}
	ascent(i in 0..length(PlayerTexts))	
		{
		Obj_SetVisible(imgFace[i],false);
		Obj_SetVisible(imgText[i],false);
		}
	PlayMusic(GetCurrentScriptDirectory ~ "../system/bgm/bgm_Title",0.4,91.825);
	TMain(0);
	TBackground;
}

@MainLoop
{
	yield;
}

task TBackground
{
	let back = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(back,GetCurrentScriptDirectory ~ "img/Title_Main.png");
	ObjSprite2D_SetDestRect(back,-2,-2,GetScreenWidth+2,GetScreenHeight+2);
	ObjRender_SetColorHSV(back,0,0,175);
	Obj_SetRenderPriority(back,0.07);

	let img = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(img,GetCurrentScriptDirectory ~ "img/Title_Effect.png");
	ObjRender_SetAlpha(img,205);
	ObjRender_SetBlendType(img,BLEND_ADD_ARGB);
	ObjSprite2D_SetDestRect(img,-2,-2,GetScreenWidth+2,GetScreenHeight+2);
	Obj_SetRenderPriority(img,0.09);

	let enemy = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(enemy,GetCurrentScriptDirectory ~ "../Stage/Stage1/Boss/img/Face/Face.png");
	ObjSprite2D_SetSourceRect(enemy,0,0,256,336);
	Obj_SetRenderPriority(enemy,0.08);
	ObjRender_SetColorHSV(enemy,0,0,0);

	let player = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(player,GetCurrentScriptDirectory ~ "../Player/pl0/Face/Main.png");
	ObjSprite2D_SetSourceRect(player,0,0,411,500);
	ObjRender_SetScaleXYZ(player,0.9,0.9,0);	
	Obj_SetRenderPriority(player,0.08);
	ObjRender_SetColorHSV(player,0,0,205);

	let xI = 0;
	let yI = 0;
	let xIM = 0;
	let yIM = 0;
	let angleI = 40;

	let xB = 0;
	let yB = 0;
	let speedB = 0.25;
	let angleB = 220;
	
	let xP = 1;
	let xPA = 0;
	let speedP = 75;
	let angleP = 0;

	let xE = 0;
	
	loop
		{
		xB+=speedB*cos(angleB);
		yB+=speedB*sin(angleB);
		ObjSprite2D_SetSourceRect(img,xB,yB,256+xB,256+yB);
		if(backSwitch == 1)
			{
			if(speedP > 0)
				{
				speedP-=1;
				xP = (speedP^3)*1/(75^3)
				}
			else if(xP > 0)
				{
				xP-=1/30;
				}
			else
				{
				xP = 0;	
				angleP+=1;
				xPA+=(1/4500)*sin(angleP);
				}
			}
		else 
			{
			speedP = 0;
			if(xP < 1)
				{
				xP+=1/30;
				}
			}
		if(backSwitch == 1)
			{
			if(xE < 0)
				{
				xE+=1/30;
				}
			else
				{
				xE = 0;
				}
			}
		else if(xE > -1)
			{
			xE-=1/30;
			}
		if(backSwitch == 1)
			{
			xIM = 128;
			yIM = 256;
			}
		else
			{
			xIM = 128+(backSwitch*128*cos(angleI))%256;
			yIM = 256+(backSwitch*128*1.75*sin(angleI))%256;
			}
		xI += (xIM-xI)/25;
		yI += (yIM-yI)/25;
		ObjSprite2D_SetSourceRect(back,xI,yI,256+xI,256+yI);
		ObjSprite2D_SetDestRect(player,GetScreenWidth*(xP+xPA+0.425)+2,27,GetScreenWidth*(xP+xPA+1.15)-2,GetScreenHeight+27);
		ObjSprite2D_SetDestRect(enemy,GetScreenWidth*(xE+0.025)-2,27,GetScreenWidth*(xE+0.5)+2,GetScreenHeight-16);	
		yield;
		}
	
}

task TMain(Index)
{
	selectIndex = Index;
	let mx = 1;
	let my = 260;
	let texts = ["Start","Spell Practice","Replay","Option","Exit"];
	var countMenu = length(texts);
	let objText = [];
	ascent(var iText in 0 .. countMenu)
		{
		objText = objText ~ [TMenuItem(iText, 300, my, texts[iText])];
		my += 27.5;
		}
	TMove;
	if(mainSwitch == false)
		{
		backSwitch = 1;
		}
	else
		{
		descent(t in 0..60)
			{
			if(CheckPress == false)
				{
				break;
				}
			else if(mx > 0)
				{
				mx = (t^2)*1/(60^2);
				}
			else
				{
				break;
				}
			yield;
			}
		mainSwitch = true;
		mx = 0;
		backSwitch = 1;
		}
	mx = 0;
	while(GetVirtualKeyState(VK_UP) == KEY_PUSH || GetVirtualKeyState(VK_UP) == KEY_HOLD || GetVirtualKeyState(VK_DOWN) == KEY_PUSH || GetVirtualKeyState(VK_DOWN) == KEY_HOLD || GetVirtualKeyState(VK_SHOT) == KEY_PUSH || GetVirtualKeyState(VK_SHOT) == KEY_HOLD){yield;}
	yield;

	TSelectIndex(true,false,countMenu);
	loop
		{
		if(GetVirtualKeyState(VK_OK) == KEY_PUSH)
			{
			alternative(selectIndex)
				case(0,1)
					{
					PlaySound("se_Confirm",120);
					TCharacters(0,[selectIndex]);
					TDel(objText);
					backSwitch += 1;
					return;
					}
				case(2)
					{	
					PlaySound("se_Confirm",120);
					TDel(objText);
					backSwitch += 1;
					let replay = TReplaySelectScene;
					if(length(replay) > 0)
						{
						SetScriptResult(["REPLAY",replay]);
						CloseScript(GetOwnScriptID);
						return;
						}
					else
						{
						backSwitch -= 1;
						PlaySound("se_Cancel",120);
						TMain(selectIndex);
						return;
						}
					}
				case(3)
					{
					PlaySound("se_Confirm",120);
					TOption(0);
					TDel(objText);
					backSwitch += 1;
					return;
					}
				case(4)
					{
					PlaySound("se_Confirm",120);
					result = ["EXIT"];
					SetScriptResult(result);
					loop(20){yield;}
					CloseScript(GetOwnScriptID);
					return;
					}
			}
		if(GetVirtualKeyState(VK_CANCEL) == KEY_PUSH)
			{
			if(selectIndex == length(texts)-1)
				{
				PlaySound("se_Confirm",120);
				result = ["EXIT"];
				SetScriptResult(result);
				loop(20){yield;}
				CloseScript(GetOwnScriptID);
				return;
				}
			else
				{
				PlaySound("se_Confirm",120);
				selectIndex = length(texts)-1;
				loop(2){yield;}
				}
			}
		yield;
		}
	task TMove
		{
		while(!Obj_IsDeleted(objText[0][0]))
			{
			let x = 30;
			ascent(i0 in 0..length(objText))
				{
				let xA = 0;
				if(selectIndex == i0)
					{
					xA = 15;
					}
				ascent(i1 in 0..length(objText[i0]))
					{
					ObjRender_SetX(objText[i0][i1],mx*60+(1-mx)*(x+xA));
					}
				x+=5;
				}
			yield;
			}
		}
}

task TCharacters(let Index,let mainSelect)
{
	let MIndex = 0;
	if(length(PlayerCount) >= 1)
		{
		MIndex = floor(Index/3)*3;
		}
	Obj_SetVisible(imgFace[Index],true);
	Obj_SetVisible(imgText[Index],true);
	selectIndex = Index;
	let mx = 30;
	let my = 32;
	let texts = PlayerTexts;
	var countMenu = length(texts);
	let objText = [];
	ascent(var iText in 0 .. countMenu)
		{
		objText = objText ~ [TMenuItem(iText, mx, my, texts[iText])];
		ascent(i0 in 0..length(objText))
			{
			ascent(i1 in 0..length(objText[i0]))
				{
				ObjText_SetHorizontalAlignment(objText[i0][i1], ALIGNMENT_LEFT);
				}
			}
		my += 32;
		}
	let objTextB = [];
	ascent(i in 0..length(PlayerCount))
		{
		objTextB = objTextB ~ [ObjText_Create()];
		ObjText_SetText(objTextB[i], texts[GetCommonData("PLAYER_" ~ IntToString(i+1),0)%3]);
		ObjText_SetFontSize(objTextB[i], 20);
		ObjText_SetFontBold(objTextB[i], true);
		ObjText_SetFontColorTop(objTextB[i], 255, 128,128);
		ObjText_SetFontColorBottom(objTextB[i], 255, 64, 64);
		ObjText_SetFontBorderType(objTextB[i], BORDER_FULL);
		ObjText_SetMaxWidth(objTextB[i], GetScreenWidth);
		ObjText_SetHorizontalAlignment(objTextB[i], ALIGNMENT_RIGHT);
		ObjText_SetFontBorderColor(objTextB[i],255, 255, 255);
		ObjText_SetFontBorderWidth(objTextB[i], 2);
		Obj_SetRenderPriorityI(objTextB[i], 10);
		ObjRender_SetX(objTextB[i], -50);
		ObjRender_SetY(objTextB[i], (i+1)*32);
		}
	while(GetVirtualKeyState(VK_UP) == KEY_PUSH || GetVirtualKeyState(VK_UP) == KEY_HOLD || GetVirtualKeyState(VK_DOWN) == KEY_PUSH || GetVirtualKeyState(VK_DOWN) == KEY_HOLD || GetVirtualKeyState(VK_SHOT) == KEY_PUSH || GetVirtualKeyState(VK_SHOT) == KEY_HOLD){yield;}
	yield;

	TSelectIndex(true,true,countMenu);
	loop
		{	
		ascent(i in 0..length(imgFace))	
			{
			Obj_SetVisible(imgFace[i],selectIndex == i);
			Obj_SetVisible(imgText[i],selectIndex == i);
			}
		if(GetVirtualKeyState(VK_OK) == KEY_PUSH)
			{
			backSwitch += 1;
			PlaySound("se_Confirm",120);
			ascent(i in 0..length(PlayerTexts))	
				{
				Obj_SetVisible(imgFace[i],false);
				Obj_SetVisible(imgText[i],false);
				}
			ascent(i in 0..length(PlayerCount))
				{
				if(PlayerCount[i] == MIndex+selectIndex)
					{
					PlaySound("se_Invalid",100);
					ascent(i in 0..length(objTextB))
						{
						Obj_Delete(objTextB[i]);	
						}
					TCharacters(mainSelect,MIndex);
					TDel(objText);
					return;
					}
				}
			mainSelect = mainSelect ~ [selectIndex];
			PlayerCount = PlayerCount ~ [MIndex+selectIndex];
			SetCommonData("PLAYER_1",MIndex+selectIndex);
			SetCommonData("PLAYER_2",MIndex+selectIndex);
			SetCommonData("PLAYER_3",MIndex+selectIndex);
			TDel(objText);
			ascent(i in 0..length(objTextB))
				{
				Obj_Delete(objTextB[i]);
				}
			if(length(PlayerCount) == 1)
				{		
				TDifficulty(mainSelect);
				PlayerCount = [];
				}
			else
				{
				ascent(i in 0..length(objTextB))
					{
					Obj_Delete(objTextB[i]);
					}
				TDel(objText);
				let textsA = texts;
				if(length(PlayerCount) == 1)
					{
					textsA = [];
					ascent(i in 0..3)
						{
						textsA = textsA ~ [texts[floor(PlayerCount[length(PlayerCount)-1]/3)*3+i]];
						}
					}
				if(length(PlayerCount) == 1)
					{
					TCharacters(mainSelect,floor(selectIndex/3)*3);
					}
				else
					{
					TCharacters(mainSelect,MIndex);
					}
				}
			return;
			}
		else if(GetVirtualKeyState(VK_CANCEL) == KEY_PUSH)
			{
			backSwitch -= 1;
			PlaySound("se_Cancel",120);
			ascent(i in 0..length(PlayerTexts))	
				{
				Obj_SetVisible(imgFace[i],false);
				Obj_SetVisible(imgText[i],false);
				}
			if(length(PlayerCount) == 0)
				{
				ascent(i in 0..length(objTextB))
					{
					Obj_Delete(objTextB[i]);
					}
				TMain(mainSelect[0]);
				TDel(objText);
				}
			else
				{
				ascent(i in 0..length(objTextB))
					{
					Obj_Delete(objTextB[i]);
					}
				let numb = PlayerCount[length(PlayerCount)-1];
				let player = [];	
				ascent(i in 0..length(PlayerCount)-1)
					{
					player = player ~ [PlayerCount[i]];
					}
				if(length(PlayerCount) == 1)
					{
					MIndex = 0;
					}
				PlayerCount = [];
				ascent(i in 0..length(player))
					{
					PlayerCount = PlayerCount ~ [player[i]];
					}
				let newSelect = [];
				ascent(i in 0..length(mainSelect)-1)
					{
					newSelect = newSelect ~ [mainSelect[i]];
					}
				TCharacters(newSelect,MIndex);
				TDel(objText);
				}
			return;
			}
		yield;
		}
}

task TDifficulty(let mainSelect)
{
	selectIndex = GetCommonData("DIFFICULTY",0);

	let mx = 30;
	let my = 260;
	let texts = ["Easyish","Harder than Normal","Really Totally Super Easy"];
	var countMenu = length(texts);
	let objText = [];
	ascent(var iText in 0 .. countMenu)
		{
		objText = objText ~ [TMenuItem(iText, mx, my, texts[iText])];
		my += 32;
		}
	TMove;
	while(GetVirtualKeyState(VK_UP) == KEY_PUSH || GetVirtualKeyState(VK_UP) == KEY_HOLD || GetVirtualKeyState(VK_DOWN) == KEY_PUSH || GetVirtualKeyState(VK_DOWN) == KEY_HOLD || GetVirtualKeyState(VK_SHOT) == KEY_PUSH || GetVirtualKeyState(VK_SHOT) == KEY_HOLD){yield;}
	yield;

	TSelectIndex(true,true,countMenu);
	loop
		{
		if(GetVirtualKeyState(VK_OK) == KEY_PUSH)
			{
			PlaySound("se_Confirm",120);
			SetCommonData("DIFFICULTY",selectIndex);
			backSwitch += 1;
			alternative(mainSelect[0])
				case(0)
					{
					SetCommonData("STAGE",1);
					SetCommonData("STAGE_TYPE","TYPE_MAIN");
					SetScriptResult(["START"]);
					CloseScript(GetOwnScriptID);
					}
				case(1)
					{
					TSpellPractice(0,mainSelect ~ [selectIndex]);
					}
			TDel(objText);
			return;
			}
		else if(GetVirtualKeyState(VK_CANCEL) == KEY_PUSH)
			{
			backSwitch -= 1;
			PlaySound("se_Cancel",120);
			TCharacters(mainSelect[length(mainSelect)-1],mainSelect);
			TDel(objText);
			return;
			}
		yield;
		}
	task TMove
		{
		while(!Obj_IsDeleted(objText[0][0]))
			{
			ascent(i0 in 0..length(objText))
				{
				let xA = i0*5;
				if(selectIndex == i0)
					{
					xA += 30;
					}
				ascent(i1 in 0..length(objText[i0]))
					{
					ObjRender_SetX(objText[i0][i1],mx+xA);
					}
				}
			yield;
			}
		}
}

task TSpellPractice(Index,mainSelect)
{
	selectIndex = Index;
	let Dif = GetCommonData("DIFFICULTY",0);
	let stage = mainSelect[length(mainSelect)-1]+1;
	let MaxIndex = 5;

	let mx = 30;
	let my = 32;
	let texts = [];

	TInitializeSpellName(1);
	let UNLOCK = GetAreaCommonData("STAGE_UNLOCK","STAGE1_SPELL",[]);

	let name = GetCommonData("SPELL_NAMES",["ERROR"]);
	ascent(i in 0..length(name)/3)
		{
		texts = texts ~ [ToString(name[i*3+Dif])];
		if(UNLOCK[i*3+Dif] == false)
			{
			texts[i] = "?????";
			}
		}
	var countMenu = length(texts);
	let objText = [];
	ascent(var iText in 0 .. countMenu)
		{
		objText = objText ~ [TMenuItem(iText, mx, my, texts[iText])];
		my += 25;
		}
	ascent(i0 in 0..length(objText))
		{
		ascent(i1 in 0..length(objText[i0]))
			{
			let color = 0.9;
			let obj = objText[i0][i1];
			ObjText_SetFontSize(obj, 19);
			ObjText_SetFontColorTop(obj, 128*color, 255*color, 255*color);
			ObjText_SetFontColorBottom(obj, 64*color, 196*color, 196*color);
			ObjText_SetFontBorderType(obj, BORDER_FULL);
			ObjText_SetFontBorderColor(obj,64, 64, 64);
			}
		}
	ascent(i in 0..length(name)/3)
		{
		if(UNLOCK[i*3+Dif] == false)
			{
			ObjText_SetFontColorTop(objText[i][0], 64, 64, 128);
			ObjText_SetFontColorBottom(objText[i][0], 32, 32, 64);
			ObjText_SetFontBorderColor(objText[i][0],128, 128, 128);
			}
		}
	TMove;
	while(GetVirtualKeyState(VK_UP) == KEY_PUSH || GetVirtualKeyState(VK_UP) == KEY_HOLD || GetVirtualKeyState(VK_DOWN) == KEY_PUSH || GetVirtualKeyState(VK_DOWN) == KEY_HOLD || GetVirtualKeyState(VK_SHOT) == KEY_PUSH || GetVirtualKeyState(VK_SHOT) == KEY_HOLD){yield;}
	yield;

	TSelectIndex(true,true,countMenu);

	loop
		{
		if(GetVirtualKeyState(VK_OK) == KEY_PUSH)
			{
			if(UNLOCK[selectIndex*3] == true)
				{
				backSwitch += 1;
				PlaySound("se_Confirm",120);
				SetCommonData("INIT_SPELL",selectIndex*3);
				SetCommonData("CURRENT_SPELL",selectIndex*3);
				SetCommonData("STAGE_TYPE","TYPE_SPELL_PRACTICE");
				SetCommonData("STAGE",1);
				SetScriptResult(["START"]);
				CloseScript(GetOwnScriptID);
				TDel(objText);
				return;
				}
			else
				{
				PlaySound("se_Invalid",100);
				TSpellPractice(selectIndex,mainSelect);
				TDel(objText);
				return;
				}
			}
		else if(GetVirtualKeyState(VK_CANCEL) == KEY_PUSH)
			{
			backSwitch -= 1;
			PlaySound("se_Cancel",120);
			let newSelect = [];
			ascent(i in 0..length(mainSelect)-1)
				{
				newSelect = newSelect ~ [mainSelect[i]];
				}
			TDifficulty(newSelect);
			TDel(objText);
			return;
			}
		yield;
		}
	task TMove
		{
		while(!Obj_IsDeleted(objText[0][0]))
			{
			ascent(i0 in 0..length(objText))
				{
				let xA = 0;
				if(selectIndex == i0)
					{
					xA = 30;
					}
				ascent(i1 in 0..length(objText[i0]))
					{
					ObjRender_SetX(objText[i0][i1],mx+xA);
					}
				}
			yield;
			}
		}
}


task TOption(Index)
{
	selectIndex = Index;

	let mx = 25;
	let my = 230;	
	let type = [GetAreaCommonData("CONFIG","TEXT_ENABLED",true),GetAreaCommonData("CONFIG","CHEAT_ENABLED",false),GetAreaCommonData("CONFIG","FLASH_KEY",true)];
	let numb = [0,0,0];
	ascent(i in 0..3)
		{
		if(type[i] == true)
			{
			numb[i] = 1;
			}
		}
	let vol = [GetAreaCommonData("CONFIG","MUSIC_VOLUME",1)*100,GetAreaCommonData("CONFIG","SOUND_VOLUME",0.85)*100,GetAreaCommonData("CONFIG","EFFECT",0),numb[0],numb[1],numb[2]];
	let vary = ["Off","Off","X"];
	ascent(i in 0..2)
		{
		if(vol[i+3] == 1)
			{
			vary[i] = "On";
			}
		}
	let texts = ["BGM Volume      " ~ IntToString(vol[0]) ~ " %","SE Volume      " ~ IntToString(vol[1]) ~ " %","Effect Cut      " ~ IntToString(vol[2]),"Dialogue      Off","Cheat      Off", "Flashbomb Key      X Key","Default","Exit"];
	if(type[0] == true)
		{
		texts[3] = "Dialogue      On";
		}
	if(type[1] == true)
		{
		texts[4] = "Cheat      On";
		}
	if(type[2] == true)
		{
		texts[5] = "Flashbomb Key      C Key";
		}
	var countMenu = length(texts);
	let objText = [];
	ascent(var iText in 0 .. countMenu)
		{
		objText = objText ~ [TMenuItem(iText, mx, my, iText)];
		ascent(i0 in 0..length(objText))
			{
			ascent(i1 in 0..length(objText[i0]))
				{
				ObjText_SetHorizontalAlignment(objText[i0][i1], ALIGNMENT_LEFT);
				}
			}
		my += 27.5;
		}
	ascent(i in 0..length(objText[length(objText)-2]))
		{	
		let obj = objText[length(objText)-2][i];
		let color = 0.8;
		ObjText_SetFontColorTop(obj, 128*color, 255*color, 255*color);
		ObjText_SetFontColorBottom(obj, 64*color, 196*color, 196*color);
		ObjText_SetFontBorderType(obj, BORDER_FULL);
		ObjText_SetFontBorderColor(obj,64, 64, 64);
		}
	let stage = 0;
	TMove;
	while(GetVirtualKeyState(VK_UP) == KEY_PUSH || GetVirtualKeyState(VK_UP) == KEY_HOLD || GetVirtualKeyState(VK_DOWN) == KEY_PUSH || GetVirtualKeyState(VK_DOWN) == KEY_HOLD || GetVirtualKeyState(VK_SHOT) == KEY_PUSH || GetVirtualKeyState(VK_SHOT) == KEY_HOLD){yield;}
	yield;

	let frameKeyHold = 0;
	let sideKeyHold = 0;
	let soundHold = 0;	
	loop
		{
		LoadCommonDataAreaA2("CONFIG",GetCurrentScriptDirectory ~ "../system/data/CONFIG");	
		alternative(selectIndex)
			case(0,1,2)
				{
				let i = selectIndex;
				let numb = [100,2];
				if(i == 2)
					{
					numb = [4,60];
					}
				if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD)
					{		
					if(sideKeyHold%numb[1] == 0)
						{
						if(vol[i] > 0)	
							{
							vol[i] = vol[i]-1;
							}
						else
							{
							vol[i] = 0;
							}	
						}
					sideKeyHold+=1;	
					}
				else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH ||  GetVirtualKeyState(VK_RIGHT) == KEY_HOLD)
					{
					if(sideKeyHold%numb[1] == 0)
						{
						if(vol[i] < numb[0])	
							{
							vol[i] = vol[i]+1;
							}
						else
							{
							vol[i] = numb[0];
							}
						}
					sideKeyHold+=1;	
					}
				else
					{
					sideKeyHold = 0;	
					}
				if(i == 1)
					{
					if(soundHold%60 == 59)
						{
						PlaySound("se_Time1",120);
						}
					soundHold += 1;
					}	
				else
					{
					soundHold = 0;
					}
				}
			case(3,4,5)
				{	
				let i = selectIndex;
				if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD)
					{	
					if(vol[i] != 0)
						{
						PlaySound("se_Confirm",120);
						}
					vol[i] = 0;
					}
				else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH ||  GetVirtualKeyState(VK_RIGHT) == KEY_HOLD)
					{
					if(vol[i] != 1)
						{
						PlaySound("se_Confirm",120);
						}
					vol[i] = 1;	
					}
				}
			case(6)
				{
				if(GetVirtualKeyState(VK_SHOT) == KEY_PUSH)
					{	
					PlaySound("se_Confirm",120);
					vol = [100,85,0,0,1,1];
					}
				}
		texts[0] = "BGM Volume      " ~ IntToString(vol[0]) ~ " %";
		texts[1] = "SE Volume      " ~ IntToString(vol[1]) ~ " %";
		texts[2] = "Effect Cut      " ~ IntToString(vol[2]);
		if(vol[3] == 0)
			{
			texts[3] = "Dialogue      Off";
			SetAreaCommonData("CONFIG","TEXT_ENABLED",false);
			SetCommonData("TEXT_ENABLED",false);
			}
		else
			{
			texts[3] = "Dialogue      On";
			SetAreaCommonData("CONFIG","TEXT_ENABLED",true);
			SetCommonData("TEXT_ENABLED",true);
			}
		if(vol[4] == 0)
			{
			texts[4] = "Cheat      Off";
			SetAreaCommonData("CONFIG","CHEAT_ENABLED",false);
			SetCommonData("CHEAT_ENABLED",false);
			}
		else
			{
			texts[4] = "Cheat      On";
			SetAreaCommonData("CONFIG","CHEAT_ENABLED",true);
			SetCommonData("CHEAT_ENABLED",true);
			}
		if(vol[5] == 0)
			{
			texts[5] = "Flashbomb Key      X Key";
			SetAreaCommonData("CONFIG","FLASH_KEY",false);
			SetCommonData("FLASH_KEY",false);
			AddVirtualKey(VK_USER2,KEY_X,KEY_INVALID);
			}
		else
			{
			texts[5] = "Flashbomb Key      C Key";
			SetAreaCommonData("CONFIG","FLASH_KEY",true);
			SetCommonData("FLASH_KEY",true);
			AddVirtualKey(VK_USER2,KEY_C,KEY_INVALID);
   			}
		SetAreaCommonData("CONFIG","MUSIC_VOLUME",vol[0]/100);	
		SetAreaCommonData("CONFIG","SOUND_VOLUME",vol[1]/100);
		SetAreaCommonData("CONFIG","EFFECT",vol[2]);	
		SetCommonData("EFFECT",vol[2]);				
		ObjSound_SetVolumeRate(musicObj,vol[0]);	
		SaveCommonDataAreaA2("CONFIG",GetCurrentScriptDirectory ~ "../system/data/CONFIG");	
		if(GetVirtualKeyState(VK_CANCEL) == KEY_PUSH || (GetVirtualKeyState(VK_OK) == KEY_PUSH && selectIndex == 7))
			{
			backSwitch -= 1;
			PlaySound("se_Cancel",120);
			TMain(5);
			TDel(objText);	
			return;
			}

		if(GetVirtualKeyState(VK_UP) == KEY_PUSH)
			{
			PlaySound("se_Select",120);
			selectIndex--;
			}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH)
			{
			PlaySound("se_Select",120);
			selectIndex++;
			}
		else if(GetVirtualKeyState(VK_UP) == KEY_HOLD)
			{
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0)))
				{
				PlaySound("se_Select",120);
				selectIndex--;
				}
			}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_HOLD)
			{
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0)))
				{
				PlaySound("se_Select",120);
				selectIndex++;
				}
			}
		else
			{
			frameKeyHold = 0;
			}

		if(selectIndex < 0) 
			{
			selectIndex = countMenu - 1;
			}
		else
			{
			selectIndex %= countMenu;
			}
		yield;
		}

	function TMenuItem(let index, let mx, let my, let numb)
		{
		function CreateTextObject(let mx, let my, let numb)
			{
			let obj = ObjText_Create();
			ObjText_SetText(obj, texts[numb]);
			ObjText_SetFontType(obj,"Century Gothic");
			ObjText_SetFontSize(obj, 24);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontColorTop(obj, 128, 255, 255);
			ObjText_SetFontColorBottom(obj, 64, 196, 196);
			ObjText_SetFontBorderType(obj, BORDER_FULL);
			ObjText_SetFontBorderColor(obj,64,64,64);
			ObjText_SetFontBorderWidth(obj, 2);
			Obj_SetRenderPriorityI(obj, 10);
			ObjText_SetMaxWidth(obj, GetScreenWidth);
			ObjText_SetHorizontalAlignment(obj, ALIGNMENT_RIGHT);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);
			return obj;
			}
	
		let objText = CreateTextObject(mx, my, numb);
		let objSelect = CreateTextObject(mx, my, numb);
		ObjRender_SetBlendType(objSelect, BLEND_ADD_RGB);
		TVis;
		return [objText,objSelect];
		task TVis
			{
			while(!Obj_IsDeleted(objText))
				{
				ObjText_SetText(objText, texts[numb]);
				ObjText_SetText(objSelect, texts[numb]);
				Obj_SetVisible(objSelect, index == selectIndex);
				yield;
				}
			}
		}
	task TMove
		{
		while(!Obj_IsDeleted(objText[0][0]))
			{
			ascent(i0 in 0..length(objText))
				{
				let xA = i0*7.5;
				if(selectIndex == i0)
					{
					xA += 30;
					}
				ascent(i1 in 0..length(objText[i0]))
					{
					ObjRender_SetX(objText[i0][i1],mx+xA);
					}
				}
			yield;
			}
		}
}

task TSelectIndex(confirm,cancel,countMenu)
{
	let frameKeyHold = 0;
	loop
		{
		if(GetVirtualKeyState(VK_OK) == KEY_PUSH && confirm == true)
			{
			return;
			}
		else if(GetVirtualKeyState(VK_CANCEL) == KEY_PUSH && cancel == true)
			{
			return;
			}

		if(GetVirtualKeyState(VK_UP) == KEY_PUSH)
			{
			PlaySound("se_Select",120);
			selectIndex--;
			}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH)
			{
			PlaySound("se_Select",120);
			selectIndex++;
			}
		else if(GetVirtualKeyState(VK_UP) == KEY_HOLD)
			{
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0)))
				{
				PlaySound("se_Select",120);
				selectIndex--;
				}
			}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_HOLD)
			{
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0)))
				{
				PlaySound("se_Select",120);
				selectIndex++;
				}
			}
		else
			{
			frameKeyHold = 0;
			}

		if(selectIndex < 0) 
			{
			selectIndex = countMenu - 1;
			}
		else
			{
			selectIndex %= countMenu;
			}
		yield;
		}
}

function TMenuItem(let index, let mx, let my, let text)
{
	function CreateTextObject(let mx, let my, let text)
		{
		let obj = ObjText_Create();
		ObjText_SetText(obj, text);
		ObjText_SetFontType(obj,"Century Gothic");
		ObjText_SetFontSize(obj, 24);
		ObjText_SetFontBold(obj, true);
		ObjText_SetFontColorTop(obj, 128, 255, 255);
		ObjText_SetFontColorBottom(obj, 64, 196, 196);
		ObjText_SetFontBorderType(obj, BORDER_FULL);
		ObjText_SetFontBorderColor(obj,64, 64, 64);
		ObjText_SetFontBorderWidth(obj, 2);
		ObjText_SetMaxWidth(obj, GetScreenWidth);
		Obj_SetRenderPriorityI(obj, 10);
		ObjRender_SetX(obj, mx);
		ObjRender_SetY(obj, my);
		return obj;
		}

	let objText = CreateTextObject(mx, my, text);
	let objSelect = CreateTextObject(mx, my, text);
	ObjRender_SetBlendType(objSelect, BLEND_ADD_RGB);
	TVis;
	return [objText,objSelect];
	task TVis
		{
		while(!Obj_IsDeleted(objText))
			{
			Obj_SetVisible(objSelect, index == selectIndex);
			yield;
			}
		}
}

task TDel(obj)
{
	ascent(i0 in 0..length(obj))
		{
		ascent(i1 in 0..length(obj[i0]))
			{
			Obj_Delete(obj[i0][i1]);
			}
		}
}

task TDel2(obj)
{
	ascent(i0 in 0..length(obj))
		{
		Obj_Delete(obj[i0]);
		}
}

function TReplaySelectScene
{
	let idScript = LoadScript(GetCurrentScriptDirectory ~ "ReplaySelect.txt");
	StartScript(idScript);

	while(!IsCloseScript(idScript))
		{
		yield;
		}

	let replay = GetScriptResult(idScript);
	if(length(replay) == 0)
		{
		return [];
		}
 	else
 		{
		return replay;
		}

}

function CheckPress
{
	if(GetVirtualKeyState(VK_OK) != KEY_PUSH && GetVirtualKeyState(VK_UP) != KEY_PUSH && GetVirtualKeyState(VK_DOWN) != KEY_PUSH && GetVirtualKeyState(VK_RIGHT) != KEY_PUSH && GetVirtualKeyState(VK_LEFT) != KEY_PUSH)
		{
		return true;
		}
	return false;
}
