#include "./../system/functions/functions_general.txt"

let SCENE_MAIN = 00;
let SCENE_START = 10;
let SCENE_MODE = 11;
let SCENE_DIF = 12;
let SCENE_PLAYER = 13;
let SCENE_PRACTICE = 20;
let SCENE_UPGRADE = 30;
let SCENE_REPLAY = 40;
let SCENE_MUSIC = 50;
let SCENE_OPTION = 60;
let SCENE_QUIT = 70;

let objScene = SCENE_MAIN;
let mainX = 0;
let mainY = 0;
let shiftTime = 0;
let shiftNum = 0;

let pathPlayer = GetCurrentScriptDirectory ~ "../player/";
let pathPlayer0 = pathPlayer ~ "pl0/";
let pathPlayer1 = pathPlayer ~ "pl1/";

let PlayerList = ["Reimu Hakurei","Marisa Kirisame"];

let whitePng = GetCurrentScriptDirectory ~ "../system/img/WhiteSquare.png";


@Initialize
{	
	LoadCommonDataAreaA2("CONFIG",pathConfig);
	LoadCommonDataAreaA2("SCORE",pathScore);
	SetAutoDeleteObject(true);
	TBackground;
}
@MainLoop
{
	yield;
}
@Finalize
{
	EndCurrentMusic;
}
function LoadImages
{
	LoadTexture(whitePng);
	LoadTexture(GetCurrentScriptDirectory ~ "../system/img/RSS_Item.png");
	LoadTexture(GetCurrentScriptDirectory ~ "./img/Title_Main.png");

	LoadTexture(pathPlayer0 ~ "calm.png");
}

task TBackground
{
	LoadImages;
	loop(90){yield;}
	PlayMusic("bgm_Title");

	let backA = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(backA,"");
	ObjSprite2D_SetDestRect(backA,0,0,640,480);
	Obj_SetRenderPriority(backA,0.11);
	ObjRender_SetColorHSV(backA,0,0,0);

	let back = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(back,GetCurrentScriptDirectory ~ "img/Title_MainA.png");
	ObjSprite2D_SetDestRect(back,0,0,640,480);
	Obj_SetRenderPriority(back,0.01);

	let shad = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(shad,GetCurrentScriptDirectory ~ "img/Title_Effect.png");
	ObjSprite2D_SetDestRect(shad,0,0,640,480);
	Obj_SetRenderPriority(shad,0.01);	
	ObjRender_SetAlpha(shad,70);
	ObjRender_SetBlendType(shad,BLEND_ALPHA);

	let shad2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(shad2,GetCurrentScriptDirectory ~ "img/Title_EffectB.png");
	ObjSprite2D_SetDestRect(shad2,0,0,640,480);
	Obj_SetRenderPriority(shad2,0.01);	
	ObjRender_SetAlpha(shad2,70);
	ObjRender_SetBlendType(shad2,BLEND_ALPHA);


	let counter = -1;
	loop
		{	
		counter+=1;
		let alpha = min(30,counter)/30;
		ObjRender_SetAlpha(backA,(1-alpha)*255);

		ObjRender_SetColorHSV(back,0,0,alpha*255);
		ObjSprite2D_SetSourceRect(back,counter*0.1+240,0,320+counter*0.1+240,240);

		ObjRender_SetAlpha(shad,alpha*185);
		ObjSprite2D_SetSourceRect(shad,-counter*0.3,0,256-counter*0.3,256);

		ObjRender_SetAlpha(shad2,alpha*185);
		ObjSprite2D_SetSourceRect(shad2,-counter*0.45,0,256-counter*0.45,256);	

		if(counter == 0)
			{	
			TScene_Main;
			}
		yield;
		}
}
task TScene_Main
{
	let distm = 170;

	let texts = ["Game Start","Replay","Quit"];
	let glw = length(texts);
	let obj = [];
	ascent(i in 0..glw+1)
		{
		let text;
		obj = obj ~ [CreateText];
		if(i == glw)
			{
			ObjText_SetText(obj[i],texts[0]);		
			ObjRender_SetBlendType(obj[i],BLEND_ADD_ARGB);
			}		
		else
			{
			ObjText_SetText(obj[i],texts[i]);
			
			}
		ObjRender_SetScaleXYZ(obj[i],-1,-1,0);
		ObjText_SetFontSize(obj[i], 32);
		ObjText_SetHorizontalAlignment(obj[i],ALIGNMENT_LEFT);
		ObjText_SetMaxWidth(obj[i],distm);
		Obj_SetRenderPriority(obj[i], 0.1);
		}

	let Rei = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(Rei,GetCurrentScriptDirectory ~ "img/Reimu.png");
	ObjSprite2D_SetSourceRect(Rei,24,0,393,512);
	ObjSprite2D_SetDestRect(Rei,-184*0.67,-256*0.67,184*0.67,256*0.67);
	Obj_SetRenderPriority(Rei,0.02);
	ObjRender_SetY(Rei,GetScreenHeight*0.5);
	ObjRender_SetAngleX(Rei,270);

	let wrd = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(wrd,GetCurrentScriptDirectory ~ "img/Title_Word.png");
	ObjSprite2D_SetSourceRect(wrd,0,0,799,666);
	ObjSprite2D_SetDestRect(wrd,0,0,1,1);
	ObjRender_SetScaleXYZ(wrd,360,360,0);
	Obj_SetRenderPriority(wrd,0.01);
	ObjRender_SetPosition(wrd,GetScreenWidth*0.67,GetScreenHeight*0.4,0);
	let reiT = -1;

	let count = 45;

	let selectIndex = 0;
	let frameKeyHold = 0;
	let frameTurn = 0;
	let dir = 0;
	let scene = SCENE_MAIN;
	objScene = scene;


	let xM = mainX+1;
	let yM = mainY;
	let cenx = GetScreenWidth-45;
	let ceny = GetScreenHeight-75;
	let x = 0;
	let y = 0;

	let counter = 0;
	shiftTime = 105;
	FShiftX(xM);

	loop
		{
		counter+=1;
		reiT+=1;
		x = (xM-mainX)*GetScreenWidth;
		y = (yM-mainY)*GetScreenHeight;
		if(reiT >= 15)
			{
			let time = 90;
			let cA = 1-(max(0,time-(reiT-15))^1.5)/(time^1.5);
			ObjRender_SetX(Rei,x*floor((reiT-15)/time)+512*0.35);		
			ObjRender_SetScaleXYZ(Rei,0.75+0.25*cA,0.75+0.25*cA,0);
			ObjRender_SetAngleX(Rei,(1-cA)*450);
			ObjRender_SetAngleZ(Rei,40+5*cos(counter*0.67)+(1-cA)*120);
			}
		if(reiT < 105)
			{
			ObjRender_SetAlpha(wrd,255);
			}
		else
			{
			ObjRender_SetY(wrd,GetScreenHeight*0.4+x);
			}
		ascent(i in 0..glw+1)
			{
			ObjRender_SetAngleZ(obj[i],180);
			let n = i;
			if(i == glw)
				{
				n = ceil(selectIndex)%glw;
				}
			ObjText_SetText(obj[i],texts[ceil(n)%glw]);
			ObjRender_SetPosition(obj[i],x+60+(n%2)*15+15*cos(counter*(0.5+n*0.25)),y-50+ceny+32*(ceil(n)%glw),0);
			}
		if(scene == objScene && counter >= shiftTime)
			{
			shiftTime = 30;
			let ind = IndexScroll([VK_UP,VK_DOWN],selectIndex,frameKeyHold,frameTurn,glw,dir,5);
			selectIndex = ind[0];
			frameKeyHold = ind[1];
			frameTurn = ind[2];
			dir = ind[3];	
			if(IsKeyState([VK_OK],[KEY_PUSH]))
				{
				PlaySound("se_Confirm",120);
				alternative(selectIndex)
					case(0)
						{
						TScene_Dif(selectIndex);
						//TFakeTexts;
						}
					case(1)
						{
						let replay = TReplaySelectScene;
						if(replay != "")
							{
							SetScriptResult(["REPLAY",replay]);
							CloseScript(GetOwnScriptID);
							return;
							}
						}
					case(length(texts)-1)
						{
						SetScriptResult(["EXIT"]);
						loop(20){yield;}
						CloseScript(GetOwnScriptID);
						return;
						}
				}
			if(IsKeyState([VK_CANCEL],[KEY_PUSH]))
				{
				if(selectIndex == length(texts)-1)
					{
					PlaySound("se_Cancel",120);
					SetScriptResult(["EXIT"]);
					loop(20){yield;}
					CloseScript(GetOwnScriptID);
					return;
					}
				else
					{
					PlaySound("se_Cancel",120);
					selectIndex = length(texts)-1;
					}
				}
			}
		yield;
		}
	task TFakeTexts
		{
		let obj = [];
		ascent(i in 0..glw+1)
			{
			let text;
			obj = obj ~ [CreateText];
			if(i == glw)
				{
				ObjText_SetText(obj[i],texts[(selectIndex)%glw]);		
				ObjRender_SetBlendType(obj[i],BLEND_ADD_ARGB);
				}		
			else
				{
				ObjText_SetText(obj[i],texts[(glw-i)%glw]);
				}
			ObjRender_SetScaleXYZ(obj[i],-1,-1,0);
			ObjText_SetFontSize(obj[i], 32);
			ObjText_SetHorizontalAlignment(obj[i],ALIGNMENT_RIGHT);
			ObjText_SetMaxWidth(obj[i],distm);
			Obj_SetRenderPriority(obj[i], 0.1);
			}
		loop
			{
			if(objScene != SCENE_MAIN)
				{
				ascent(i in 0..glw+1)
					{
					let angle = 180-i*360/glw;
					if(i != glw)
						{
						angle+=selectIndex*360/glw;
						if(cos(angle) >= cos(90-360/7))
							{
							Obj_SetVisible(obj[i],false);
							}
						else
							{
							Obj_SetVisible(obj[i],true);
							}
						}
					angle=(angle+360)%360;
					ObjRender_SetPosition(obj[i],x-distm*0.25+cenx+distm*1.075*0.33*cos(angle+180),y-22+ceny+distm*1.075*0.33*sin(angle+180),0);
					ObjRender_SetAngleZ(obj[i],(angle-180)*0.67+180);
					}
				}
			else
				{
				ascent(i in 0..length(obj))
					{
					Obj_Delete(obj[i]);
					}
				return;
				}
			yield;
			}
		}
}

task TScene_Dif(type_main)
{
	let textsMode = ["Easy","Normal","Hard","Lunatic"];
	let textsDesc = ["A little water never hurt anyone","It seems scary, but the brave can make it through","Brace yourself for the coming storm","Don't you know that dodging rain is impossible?"];
	let textsDesc2 = ["Drizzle Level","Downpour Level","Monsoon Level","Curtain Fire Level"];
	
	let distx = GetScreenWidth*0.5-100;
	
	let glw = length(textsMode);
	let objMode = [];
	ascent(i in 0..glw+1)
		{
		let text;
		objMode = objMode ~ [CreateText];
		if(i == glw)
			{
			ObjText_SetText(objMode[i],textsMode[0]);		
			ObjRender_SetBlendType(objMode[i],BLEND_ADD_ARGB);
			}		
		else
			{
			ObjText_SetText(objMode[i],textsMode[i]);
			}
		ObjText_SetFontSize(objMode[i], 48);
		ObjText_SetHorizontalAlignment(objMode[i],ALIGNMENT_LEFT);
		Obj_SetRenderPriority(objMode[i], 0.12);
		}
	ObjRender_SetAlpha(objMode[glw],85);

	let objDesc = [];
	ascent(i in 0..glw+1)
		{
		let text;
		objDesc = objDesc ~ [CreateText];
		objDesc = objDesc ~ [CreateText];
		if(i == glw)
			{
			ObjText_SetText(objDesc[i*2],textsDesc[0]);	
			ObjText_SetText(objDesc[i*2+1],textsDesc2[0]);		
			ObjRender_SetBlendType(objMode[i],BLEND_ADD_ARGB);
			}		
		else
			{
			ObjText_SetText(objDesc[i*2],textsDesc[i]);
			ObjText_SetText(objDesc[i*2+1],textsDesc2[i]);
			}
		ObjText_SetFontSize(objDesc[i*2], 20);
		ObjText_SetFontSize(objDesc[i*2+1], 15);
		ObjText_SetHorizontalAlignment(objDesc[i*2],ALIGNMENT_LEFT);
		ObjText_SetHorizontalAlignment(objDesc[i*2+1],ALIGNMENT_LEFT);
		Obj_SetRenderPriority(objDesc[i*2], 0.12);
		Obj_SetRenderPriority(objDesc[i*2+1], 0.12);
		}
	ObjRender_SetAlpha(objDesc[glw*2],85);
	ObjRender_SetAlpha(objDesc[glw*2+1],85);

	let objMaj = CreateText;
	ObjText_SetText(objMaj,"Select Difficulty");
	ObjText_SetFontSize(objMaj, 54);
	ObjText_SetHorizontalAlignment(objMaj,ALIGNMENT_LEFT);
	Obj_SetRenderPriority(objMaj, 0.12);

	let back;
	local
		{
		back = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
		ObjPrim_SetTexture(back,whitePng);
		Obj_SetRenderPriority(back,0.11);
		ObjRender_SetAlpha(back,155);

		descent(i in 0..2)
			{	
			ObjSpriteList2D_SetDestRect(back,-distx-i*2,103-i*2,distx+i*2,450+i*2);
			ObjRender_SetColorHSV(back,0,0,i*155);
			ObjSpriteList2D_AddVertex(back);
			}
		ObjSpriteList2D_CloseVertex(back);
		}

	let selectIndex = 0;
	let ind = selectIndex;
	let frameKeyHold = 0;
	let frameTurn = 0;
	let dir = 0;
	let scene = SCENE_DIF;
	objScene = scene;
	
	let xM = mainX+1;
	let yM = mainY;
	let cenx = GetScreenWidth*0.5;
	let ceny = 0;
	let x = 0;
	let y = 0;
	let xm = x;
	let ym = y;

	let shiftingBack = false;
	let counterShift = 0;
	let counter = 0;

	FShiftX(xM);

	loop
		{
		counter+=1;
		x = (xM-mainX)*GetScreenWidth;
		y = (yM-mainY)*GetScreenHeight;
		
		if(ind != selectIndex || xm != x || ym != y)
			{
			ind = selectIndex;
			xm = x;
			ym = y;
			alternative(trunc(selectIndex))
				case(0)
					{
					ascent(i in 0..glw)
						{
						if(i == trunc(selectIndex))
							{
							ObjRender_SetColor(objMode[i],64,255,255);
							ObjRender_SetColor(objDesc[i*2],64,196,196);
							ObjRender_SetColor(objDesc[i*2+1],64,196,196);
							}	
						else
							{
							ObjRender_SetColor(objMode[i],64,64,64);
							ObjRender_SetColor(objDesc[i*2],32,32,32);
							ObjRender_SetColor(objDesc[i*2+1],32,32,32);
							}
						}
					ObjRender_SetColor(objMaj,225,255,255);
					}
				case(1)
					{
					ascent(i in 0..glw)
						{
						if(i == trunc(selectIndex))
							{
							ObjRender_SetColor(objMode[i],64,255,64);
							ObjRender_SetColor(objDesc[i*2],64,196,64);
							ObjRender_SetColor(objDesc[i*2+1],64,196,64);
							}	
						else
							{
							ObjRender_SetColor(objMode[i],64,64,64);
							ObjRender_SetColor(objDesc[i*2],32,32,32);
							ObjRender_SetColor(objDesc[i*2+1],32,32,32);
							}
						}
					ObjRender_SetColor(objMaj,225,255,225);	
					}
				case(2)
					{
					ascent(i in 0..glw)
						{
						if(i == trunc(selectIndex))
							{
							ObjRender_SetColor(objMode[i],255,64,64);
							ObjRender_SetColor(objDesc[i*2],196,64,64);
							ObjRender_SetColor(objDesc[i*2+1],196,64,64);
							}	
						else
							{
							ObjRender_SetColor(objMode[i],64,64,64);
							ObjRender_SetColor(objDesc[i*2],32,32,32);
							ObjRender_SetColor(objDesc[i*2+1],32,32,32);
							}
						}
					ObjRender_SetColor(objMaj,255,225,225);	
					}
				case(3)
					{
					ascent(i in 0..glw)
						{
						if(i == trunc(selectIndex))
							{
							ObjRender_SetColor(objMode[i],255,64,255);
							ObjRender_SetColor(objDesc[i*2],196,64,196);
							ObjRender_SetColor(objDesc[i*2+1],196,64,196);
							}	
						else
							{
							ObjRender_SetColor(objMode[i],64,64,64);
							ObjRender_SetColor(objDesc[i*2],32,32,32);
							ObjRender_SetColor(objDesc[i*2+1],32,32,32);
							}
						}
					ObjRender_SetColor(objMaj,255,225,255);	
					}	
			ascent(i in 0..glw+1)
				{
				let v = i;
				if(i == glw)
					{
					v = trunc(selectIndex);
					ObjText_SetText(objMode[i],textsMode[v]);
					ObjText_SetText(objDesc[i*2],textsDesc[v]);
					ObjText_SetText(objDesc[i*2+1],textsDesc2[v]);
					}
				let yA = y+100+v*325/glw;
				ObjRender_SetPosition(objMode[i],x+GetScreenWidth*(0.6725-length(textsMode[v])*0.5/40)-116,yA,0);
				ObjRender_SetPosition(objDesc[i*2],x+GetScreenWidth*(0.6725-length(textsDesc[v])*0.5/80)-116+4,yA+45,0);
				ObjRender_SetPosition(objDesc[i*2+1],x+GetScreenWidth*(0.6725-length(textsDesc2[v])*0.5/100)-116+4,yA+45+20,0);
				}
			ObjRender_SetPosition(back,x+cenx,y+ceny,0);
			ObjRender_SetPosition(objMaj,x+cenx-125,y+ceny+40,0);
			}
		if(scene == objScene && counter >= shiftTime)
			{
			let ind = IndexScroll([VK_UP,VK_DOWN],selectIndex,frameKeyHold,frameTurn,glw,dir,4);
			selectIndex = ind[0];
			frameKeyHold = ind[1];
			frameTurn = ind[2];
			dir = ind[3];
			if(shiftingBack == false)
				{	
				if(IsKeyState([VK_CANCEL],[KEY_PUSH]))
					{
					PlaySound("se_Cancel",120);
					FShiftX(xM-1);
					shiftingBack = true;
					}
				if(IsKeyState([VK_OK],[KEY_PUSH]))
					{
					SetCommonData("DIFFICULTY",selectIndex);
					if(type_main == 0)
						{
						PlaySound("se_Confirm",120);
						TScene_PlayerMain(type_main);
						}
					}
				}
			else
				{
				counterShift+=1;
				if(counterShift >= shiftTime)
					{
					ascent(i in 0..length(objMode))
						{
						Obj_Delete(objMode[i]);
						Obj_Delete(objDesc[i*2]);
						Obj_Delete(objDesc[i*2+1]);
						}
					Obj_Delete(objMaj);
					Obj_Delete(back);
					if(type_main == 0)
						{
						objScene = SCENE_MAIN;
						}
					return;
					}
				}
			}
		yield;
		}
}
task TScene_PlayerMain(type_main)
{
	let textsPlayer = PlayerList;
	let textsStatM = ["Danmaku Toy","Spell Card"];
	let textsStat = ["Homing Amulet[r]Persuasion Needle","Ultra Despair Inducing Orb","",
			"Illusion Laser[r]Magic Missile","Beato's Closed Room Murder",""];
	let textsDesc = ["Homing type.[r]Damage increases when unfocused.",
			"Forward focus type.[r]Missiles explode, clearing nearby enemies.",];
	
	let distx = GetScreenWidth*0.5-150;
	
	let objMode = CreateText;
	ObjText_SetText(objMode,textsPlayer[0]);
	ObjText_SetFontSize(objMode, 48);
	ObjText_SetHorizontalAlignment(objMode,ALIGNMENT_LEFT);
	Obj_SetRenderPriority(objMode, 0.12);

	let objDesc = CreateText;
	ObjText_SetText(objDesc,textsDesc[0]);
	ObjText_SetFontSize(objDesc, 17);
	ObjText_SetHorizontalAlignment(objDesc,ALIGNMENT_LEFT);
	Obj_SetRenderPriority(objDesc, 0.12);
	ObjText_SetLinePitch(objDesc,1);
	ObjText_SetSidePitch(objDesc,0.875);

	let objMaj = CreateText;
	ObjText_SetText(objMaj,"Select Your Main Character");
	ObjText_SetFontSize(objMaj, 42);
	ObjText_SetHorizontalAlignment(objMaj,ALIGNMENT_LEFT);
	Obj_SetRenderPriority(objMaj, 0.12);

	let objStatM = [];
	let objStat = [];
	ascent(i in 0..length(textsStatM))
		{
		objStatM = objStatM ~ [CreateText];
		ObjText_SetText(objStatM[i],textsStatM[i]);
		ObjText_SetFontSize(objStatM[i], 30);
		ObjText_SetHorizontalAlignment(objStatM[i],ALIGNMENT_LEFT);
		Obj_SetRenderPriority(objStatM[i], 0.12);

		objStat = objStat ~ [CreateText];
		ObjText_SetFontType(objStat[i],"");
		ObjText_SetText(objStat[i],textsStat[i]);
		ObjText_SetFontSize(objStat[i], 13);
		ObjText_SetHorizontalAlignment(objStat[i],ALIGNMENT_LEFT);
		Obj_SetRenderPriority(objStat[i], 0.12);
		ObjRender_SetScaleY(objStat[i],1.175);
		ObjText_SetLinePitch(objStat[i],0.5);
		}

	let back;
	local
		{
		back = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
		ObjPrim_SetTexture(back,whitePng);
		Obj_SetRenderPriority(back,0.11);
		ObjRender_SetAlpha(back,155);

		descent(i in 0..2)
			{	
			ObjSpriteList2D_SetDestRect(back,-distx-i*2,50-i*2,distx+i*2,110+i*2);
			ObjRender_SetColorHSV(back,0,0,i*155);
			ObjSpriteList2D_AddVertex(back);	

			ObjSpriteList2D_SetDestRect(back,-distx-i*2,113-i*2,distx+i*2,425+i*2);
			ObjRender_SetColorHSV(back,0,0,i*155);
			ObjSpriteList2D_AddVertex(back);
	
			ObjSpriteList2D_SetDestRect(back,-distx+5-i*2,320-8-i*2,distx-5+i*2,425-8+i*2);
			ObjRender_SetColorHSV(back,0,0,i*155);
			ObjSpriteList2D_AddVertex(back);
			}
		ObjSpriteList2D_CloseVertex(back);
		}

	let face = [];
	ascent(i in 0..length(PlayerList))
		{
		face = face ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		alternative(i)
			case(0)
				{
				ObjPrim_SetTexture(face[i],pathPlayer0 ~ "face.png");
				ObjSprite2D_SetSourceRect(face[i],0,0,398,512);
				}
			case(1)
				{
				ObjPrim_SetTexture(face[i],pathPlayer1 ~ "face.png");
				ObjSprite2D_SetSourceRect(face[i],0,0,398,512);
				}
		ObjRender_SetScaleXYZ(face[i],0.875,0.875,0);
		ObjSprite2D_SetDestCenter(face[i]);
		}
	let faceMA = 0;
	let faceA = faceMA;

	let selectIndex = 0;
	let ind = selectIndex;
	let frameKeyHold = 0;
	let frameTurn = 0;
	let dir = 0;
	let scene = SCENE_PLAYER;
	objScene = scene;
	
	let xM = mainX+1;
	let yM = mainY;
	let cenx = GetScreenWidth*0.75;
	let ceny = 0;
	let x = 0;
	let y = 0;
	let xm = x;
	let ym = y;

	let shiftingBack = false;
	let counterShift = 2;
	let counter = 0;

	FShiftX(xM);

	loop
		{
		counter+=1;
		x = (xM-mainX)*GetScreenWidth;
		y = (yM-mainY)*GetScreenHeight;

		if(ind != selectIndex || xm != x || ym != y)
			{
			let num = trunc(selectIndex);
			ind = selectIndex;
			xm = x;
			ym = y;
			let color = 0;		
			let alpha = 0;
			alternative(num)
				case(0)
					{
					color = 240;
					alpha = 40;
					}
				case(1)
					{
					color = 60;
					alpha = 30;
					}
				case(2)
					{
					color = 330;
					alpha = 45;
					}
	
			ObjText_SetText(objMode,textsPlayer[num]);
			ObjRender_SetPosition(objMode,x+cenx-[137,137,137][num],y+ceny+50,0);
			ObjRender_SetColorHSV(objMode,color,alpha,255);
			ObjText_SetText(objDesc,textsDesc[num]);
			ObjRender_SetPosition(objDesc,x+cenx-185,y+315,0);
			ObjRender_SetPosition(back,x+cenx-25,y+ceny,0);
			ObjRender_SetPosition(objMaj,x+GetScreenWidth*0.5-160,y+ceny,0);
			ascent(i in 0..length(objStat))
				{
				let yA = y+120+i*160/(length(objStat)-0.33);
				ObjRender_SetPosition(objStatM[i],x+cenx-180,yA,0);
				ObjRender_SetColorHSV(objStatM[i],color,alpha,255);
				ObjRender_SetPosition(objStat[i],x+cenx-180+15,yA+30,0);
				ObjText_SetText(objStat[i],textsStat[num*3+i]);
				}
			ascent(i in 0..length(face))
				{	
				if(i == num)
					{
					ObjRender_SetColorHSV(face[i],0,0,255);
					ObjRender_SetAlpha(face[i],255);
					Obj_SetRenderPriority(face[i], 0.10);
					}
				else
					{
					ObjRender_SetColorHSV(face[i],color,255,25);
					ObjRender_SetAlpha(face[i],0);
					Obj_SetRenderPriority(face[i], 0.09);
					}
				let v = i+faceA;
				ObjRender_SetPosition(face[i],x+135+v*50,y+GetScreenHeight*0.5+v*30,0);
				}
			}
		if(scene == objScene && counter >= shiftTime)
			{
			let ind = IndexScroll([VK_UP,VK_DOWN],selectIndex,frameKeyHold,frameTurn,length(PlayerList),dir,4);
			selectIndex = ind[0];
			frameKeyHold = ind[1];
			frameTurn = ind[2];
			dir = ind[3];
			if(selectIndex%3 != 0)
				{	
				faceMA = trunc(selectIndex+0.5-0.5*dir)%length(face);
				faceA += (faceMA-trunc(selectIndex+0.5+0.5*dir)%length(face))/4;
				}
			if(shiftingBack == false)
				{	
				if(IsKeyState([VK_CANCEL],[KEY_PUSH]))
					{
					PlaySound("se_Cancel",120);
					FShiftX(xM-1);
					shiftingBack = true;
					}
				if(IsKeyState([VK_OK],[KEY_PUSH]))
					{
					PlaySound("se_Confirm",120);
					SetCommonData("PLAYER",selectIndex);

					SetCommonData("STAGE",1);
					SetCommonData("STAGE_TYPE","TYPE_MAIN");
					SetScriptResult(["START"]);
					CloseScript(GetOwnScriptID);
					}
				}
			else
				{
				counterShift+=1;
				if(counterShift >= shiftTime)
					{
					Obj_Delete(objMode);
					Obj_Delete(objDesc);
					Obj_Delete(objMaj);
					Obj_Delete(back);
					ascent(i in 0..length(objStat))
						{
						Obj_Delete(objStatM[i]);
						Obj_Delete(objStat[i]);
						}
					ascent(i in 0..length(face))
						{
						Obj_Delete(face[i]);
						}
					if(type_main == 0)
						{
						objScene = SCENE_DIF;
						}
					return;
					}
				}
			}
		yield;
		}
}

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

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

	return GetScriptResult(idScript);

}
task FShiftX(x)
	{
	let xM = mainX;
	shiftNum+=1;
	let time = shiftTime;
	let num = shiftNum;
	ascent(t in 0..time)
		{
		if(num != shiftNum){return;}
		mainX+=(x-xM)*(1-cos(t*360/time))/time;
		yield;
		}
	}
task FShiftY(y)
	{
	let yM = mainY;
	shiftNum+=1;
	let time = shiftTime;
	let num = shiftNum;
	ascent(t in 0..time)
		{
		if(num != shiftNum){return;}
		mainY+=(y-yM)*(1-cos(t*360/time))/time;
		yield;
		}
	}

function IndexScroll(Key,selectIndex,frameKeyHold,frameTurn,countMenu,dir,scrollSpeed)
{
	if(dir != 0)
		{
		selectIndex+=dir/scrollSpeed;
		if(IsKeyState(Key,[KEY_HOLD]))
			{
			frameTurn = (frameTurn+1)%scrollSpeed;
			if(frameTurn == 0)
				{
				dir = 0;
				selectIndex = round(selectIndex)%countMenu;
				}
			}
		else
			{
			frameTurn = (frameTurn+1)%scrollSpeed;
			if(frameTurn == 0)
				{
				dir = 0;	
				selectIndex = round(selectIndex)%countMenu;
				}
			}
		}
	else 
		{
		dir = 0;
		if(IsKeyState([Key[0]],[KEY_PUSH]))
			{
			PlaySound("se_Select",120);
			dir = -1;
			}
		else if(IsKeyState([Key[1]],[KEY_PUSH]))
			{
			PlaySound("se_Select",120);
			dir = 1;
			}
		else if(IsKeyState([Key[0]],[KEY_HOLD]))
			{
			frameKeyHold++;
			if(frameKeyHold == 30 || frameKeyHold > 30)
				{
				PlaySound("se_Select",120);
				dir = -1;
				}
			}
		else if(IsKeyState([Key[1]],[KEY_HOLD]))
			{
			frameKeyHold++;
			if(frameKeyHold == 30 || frameKeyHold > 30)
				{
				PlaySound("se_Select",120);
				dir = 1;
				}
			}
		else
			{
			frameKeyHold = 0;
			}
		
		selectIndex+=absolute(dir)*dir/scrollSpeed;
		frameTurn=absolute(dir)*((frameTurn+1)%scrollSpeed);
		}
	if(selectIndex < 0) 
		{
		selectIndex += countMenu;
		}
	else if(selectIndex >= countMenu)
		{
		selectIndex -= countMenu;
		}
	return [selectIndex,frameKeyHold,frameTurn,dir];
}
function CreateText
{
	let obj = ObjText_Create;
	ObjText_SetFontType(obj,"Cirno");
	ObjText_SetFontColorTop(obj, 255, 255, 255);
	ObjText_SetFontColorBottom(obj, 196, 196, 196);
	ObjText_SetFontBorderType(obj, BORDER_FULL);
	ObjText_SetFontBorderColor(obj,64, 64, 64);
	ObjText_SetFontBorderWidth(obj, 2);
	ObjText_SetSidePitch(obj,1);
	return obj;	
}

task DeleteArray(array)
	{
	ascent(i in 0..length(array))
		{
		Obj_Delete(array[i]);
		}
	}