#搶曽抏枊晽[Package]
#Title["Maiden's Exhibition -彮彈払偺抏枊揥棗夛- ver0.01a"]
#Text["Maiden's Exhibition ver0.01a"]
#System["./system/System.txt"]
#Player["./player/Marisa/Marisa.txt","./player/Reimu/Reimu.txt"]

#include".\lib/BossCommon/SpellList.txt"

let PlayerCharacter;
let SoundFlag=1;
let StageMusicFlag=1;
let StageMusicStopFlag=0;
let DemoFlag=0;

let SpellCardInfo_Text=[];
let SpellCardInfo_Get=[];
let SpellCardInfo_Challenge=[];

@Initialize{T_Main();}

@MainLoop{yield;}

@Finalize{}

task T_Main
{
	yield;
	if(LoadCommonDataAreaA1("SPELL_RECORD"))
	{
		LoadCommonDataAreaA1("TUNING");
	}
	else
	{
		let Spell_C_Array=[];
		let Spell_G_Array=[];
		let Spell_Score_Array=[];
		let Spell_First_Array=[];
		let Spell_Early_Array=[];
		let Spell_Favorite_Array=[];
		loop(100)
		{
			Spell_C_Array=Spell_C_Array~[0];
			Spell_G_Array=Spell_G_Array~[0];
			Spell_Favorite_Array=Spell_Favorite_Array~[0];
			Spell_Score_Array=Spell_Score_Array~[0];
			Spell_First_Array=Spell_First_Array~[0];
			Spell_Early_Array=Spell_Early_Array~[100000];
		}

		CreateCommonDataArea("SPELL_RECORD");
		SetAreaCommonData("SPELL_RECORD","Reimu_Challenge",Spell_C_Array);
		SetAreaCommonData("SPELL_RECORD","Reimu_Get",Spell_G_Array);
		SetAreaCommonData("SPELL_RECORD","Reimu_Score",Spell_Score_Array);
		SetAreaCommonData("SPELL_RECORD","Reimu_First",Spell_First_Array);
		SetAreaCommonData("SPELL_RECORD","Reimu_Early",Spell_Early_Array);

		SetAreaCommonData("SPELL_RECORD","Marisa_Challenge",Spell_C_Array);
		SetAreaCommonData("SPELL_RECORD","Marisa_Get",Spell_G_Array);
		SetAreaCommonData("SPELL_RECORD","Marisa_Score",Spell_Score_Array);
		SetAreaCommonData("SPELL_RECORD","Marisa_First",Spell_First_Array);
		SetAreaCommonData("SPELL_RECORD","Marisa_Early",Spell_Early_Array);

		CreateCommonDataArea("TUNING");
		SetAreaCommonData("TUNING","BGM",1);
		SetAreaCommonData("TUNING","PlayerCharacter",0);
		SetAreaCommonData("TUNING","SelectIndex",0);
		SetAreaCommonData("TUNING","SelectPageIndex",0);
		SetAreaCommonData("TUNING","SortType",0);
		SetAreaCommonData("TUNING","PlayerName","NEMO");
		SetAreaCommonData("TUNING","FavoriteMode",0);
		SetAreaCommonData("TUNING","Favorite",Spell_Favorite_Array);
		SetAreaCommonData("TUNING","Setting",[100,100,1,1]);
	}
	yield;
	TTitleScene();
	T_Sound();
}


//----------------------------------------------------
// 僞僀僩儖僔乕儞
//----------------------------------------------------

task T_Sound
{
	let objMusic=ObjSound_Create;
	ObjSound_Load(objMusic,GetCurrentScriptDirectory~"bgm/menu.ogg");
	ObjSound_SetLoopEnable(objMusic,true);
//	ObjSound_SetLoopTime(objMusic,46.439,107.369);
	let MaxVulume=GetAreaCommonData("TUNING","Setting",[])[0];
	if(SoundFlag==1)
	{
		ObjSound_Play(objMusic);
/*
		if(GetAreaCommonData("TUNING","BGM",1)==1)
		{
			ObjSound_SetVolumeRate(objMusic,80);
		}
		else
		{
			ObjSound_SetVolumeRate(objMusic,0);
		}
*/
		while(SoundFlag)
		{
			MaxVulume=GetAreaCommonData("TUNING","Setting",[])[0];
			ObjSound_SetVolumeRate(objMusic,MaxVulume);
			yield;
		}
		ObjSound_SetFade(objMusic,-40);
		loop(60){yield;}
	}
	Obj_Delete(objMusic);
}

task TTitleScene
{
	SpellCardInfo_Text=[];
	SpellCardInfo_Get=[];
	SpellCardInfo_Challenge=[];
	SoundFlag=1;
	StageMusicFlag=0;
	PlayerCharacter = GetAreaCommonData("TUNING","PlayerCharacter",1);//僾儗僀儎乕
	let SortType	= GetAreaCommonData("TUNING","SortType",1);//僜乕僩曽朄
	let FavoriteMode= GetAreaCommonData("TUNING","FavoriteMode",0);//僼僃僀僶儕僢僩儌乕僪
	//僞僀僩儖僔乕儞廔椆僼儔僌
	let bEndTitleScene = false;

	//僞僀僩儖僷僗愝掕
	let dir = GetCurrentScriptDirectory();
	let pathTitle = dir ~ "system/img/Title.png";

	let objTitleImage = ObjPrim_Create(OBJ_SPRITE_2D); //2D僗僾儔僀僩僆僽僕僃僋僩惗惉
	Obj_SetRenderPriorityI(objTitleImage, 20); //昤夋桪愭搙傪愝掕
	ObjPrim_SetTexture(objTitleImage, pathTitle); //僥僋僗僠儍傪愝掕
	ObjSprite2D_SetSourceRect(objTitleImage, 0, 0, 640, 480);
	ObjSprite2D_SetDestRect(objTitleImage, 0, 0, 640, 480);

	//僞僀僩儖暥帤楍
	let objTitleText = ObjText_Create();
	ObjText_SetText(objTitleText, "No 丂SpellCard  丂丂丂丂丂丂丂丂丂丂丂Difficult        Memory");
	ObjText_SetFontSize(objTitleText, 14+4);
	ObjText_SetFontBold(objTitleText, true);
	ObjText_SetFontColorTop(objTitleText, 255, 255, 255);
	ObjText_SetFontColorBottom(objTitleText, 160, 160, 255);

	ObjText_SetFontBorderType(objTitleText, BORDER_NONE);
	ObjText_SetFontBorderColor(objTitleText,255, 255, 255);
	ObjText_SetFontBorderWidth(objTitleText, 2*0);
	Obj_SetRenderPriorityI(objTitleText, 30);
	ObjRender_SetX(objTitleText, 14);
	ObjRender_SetY(objTitleText, 14);

	ObjText_SetFontType(objTitleText,"俵俽 柧挬");

/*************僞僀僩儖僥僉僗僩昞帵僞僗僋****************************/
	task TMenuItem
	{
		function CreateTextObject(let mx, let my, let text)
		{
			let obj = ObjText_Create();
			ObjText_SetText(obj, text);
			ObjText_SetFontSize(obj, 14+4);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontColorTop(obj, 255, 255, 255);

			ObjText_SetFontBorderType(obj, BORDER_NONE);
			ObjText_SetFontBorderColor(obj,255, 255, 255);
			ObjText_SetFontBorderWidth(obj, 2*0);
			Obj_SetRenderPriorityI(obj, 72);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);

			ObjText_SetFontType(obj,"俵俽 柧挬");
			Obj_SetValue(obj,"BaseAlpha",255);
			return obj;
		}

		let mx = 14;
		let my = 14+25;
		let objTextArray=[];
		ascent(i in 0..12)
		{
			objTextArray = objTextArray~[CreateTextObject(mx, my+i%12*25, "")];
		}
		let AlphaRate=1;

		let OldPage=selectPageIndex;

		let R=255;let G=255;let B=255;
		let Alpha=255;
		while(!bEndTitleScene)
		{
			ascent(i in 0..12)
			{
				if(length(SpellCardInfo_Text)>i+selectPageIndex*12)
				{//僇乕僪俶倧乮攝楍乯埲壓側傜昞帵
					ObjText_SetText(objTextArray[i], SpellCardInfo_Text[i+selectPageIndex*12]);
					ObjRender_SetX(objTextArray[i], mx);
					if(SpellCardInfo_Challenge[i+selectPageIndex*12]>=1)
					{//僗儁儖庢摼忬嫷偵傛偭偰怓暘偗
						if(SpellCardInfo_Get[i+selectPageIndex*12]>=1)
						{
							R=128;G=255;B=192;
						}
						else
						{
							R=255;G=64;B=64;
						}
					}
					else
					{
						R=255;G=255;B=255;
					}
					ObjText_SetFontColorBottom(objTextArray[i], R, G, B);
				}
				else
				{//僇乕僪俶倧乮攝楍乯挻偊偨傜壗傕昞帵偟側偄
					ObjText_SetText(objTextArray[i], "");
				}

				if(selectIndex%12==i)
				{//慖戰峴偲懠偺峴偱Alpha抣曄峏
					ObjRender_SetAlpha(objTextArray[i],Alpha);
				}
				else
				{
					ObjRender_SetAlpha(objTextArray[i],Alpha*0.6);
				}
			}

			OldPage=selectPageIndex;
			
			yield;

			if(OldPage!=selectPageIndex && !FavoriteMode)
			{//傌乕僕曄峏偐偮僼傽儃儌乕僪偠傖側偄側傜曄峏僄僼僃僋僩敪惗
				let dir=1;
				if(OldPage+1==selectPageIndex || (selectPageIndex==0 && OldPage==3) )
				{
					dir=1;
				}
				else
				{
					dir=-1;
				}
				ascent(i in 0..12)
				{
					if(length(SpellCardInfo_Text)>i+OldPage*12)
					{
						if(SpellCardInfo_Challenge[i+OldPage*12]>=1)
						{
							if(SpellCardInfo_Get[i+OldPage*12]>=1)
							{
								R=128;G=255;B=192;
							}
							else
							{
								R=255;G=64;B=64;
							}
						}
						else
						{
							R=255;G=255;B=255;
						}

						if(selectIndex%12==i)
						{
							CreateEffectText(mx, my+i%12*25,SpellCardInfo_Text[i+OldPage*12],dir,R,G,B,Alpha);
						}
						else
						{
							CreateEffectText(mx, my+i%12*25,SpellCardInfo_Text[i+OldPage*12],dir,R,G,B,Alpha*0.6);
						}
					}
				}
				CtrlAlpha(dir);
			}
		}
		ascent(i in 0..12)
		{//僞僀僩儖僔乕儞廔椆帪偵嶍彍
			Obj_Delete(objTextArray[i]);
		}

/*************儁乕僕曄峏僄僼僃僋僩僞僗僋(僱僗僩乯*********************/
		task CtrlAlpha(dir)
		{
			mx = 14+100*dir;
			Alpha=0;
			loop(10)
			{
				mx-=10*dir;
				Alpha+=255/10;
				yield;
			}
		}

		task CreateEffectText(let mx, let my, let text,dir,R,G,B,Alpha)
		{
			let obj = ObjText_Create();
			ObjText_SetText(obj, text);
			ObjText_SetFontSize(obj, 14+4);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontColorTop(obj, 255, 255, 255);

			ObjText_SetFontBorderType(obj, BORDER_NONE);
			ObjText_SetFontBorderColor(obj,R,G,B);
			ObjText_SetFontBorderWidth(obj, 2*0);
			Obj_SetRenderPriorityI(obj, 72);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);

			ObjText_SetFontType(obj,"俵俽 柧挬");
			ObjText_SetFontColorBottom(obj, R, G, B);

			let BaseAlpha=Alpha;
			loop(10)
			{
				mx-=100/10*dir;
				Alpha-=BaseAlpha/10;
				ObjRender_SetX(obj, mx);	
				ObjRender_SetAlpha(obj,Alpha);
				yield;
			}
			Obj_Delete(obj);
		}
/*************儁乕僕曄峏僄僼僃僋僩僞僗僋(僱僗僩乯廔傢傝*********************/
	}

/*************僞僀僩儖僥僉僗僩昞帵僞僗僋廔傢傝*************************/

	yield;//堦墳晧壸懳嶔

	//儊僯儏乕攝抲
	let mx = 14;
	let my = 14+25;
	let SpellCardSetting=[
	["乽僟乕僋僱僗僒乕僕儏乿","仚仚","儖乕儈傾"],
	["乽僼儕乕僘儔儞僒乕乿","仚","僠儖僲"],
	["乽擑怓偺晽乿","仚","戝梔惛"],
	["乽僀儞僼僃儖僫儖僌儕儌傾乿","仚仚仚仚","彫埆杺"],
	["乽儗僀僕儞僌儈僗僩乿","仚仚仚仚","僷僠儏儕乕丒僲乕儗僢僕"],
	["乽儚儞僞僢僠奐暵搨嶱乿","仚仚仚","懡乆椙丂彫嶱"],
	["乽堈丒愯帠棯檙乿","仚仚","烌"],
	["乽愮濻枩濻偺巰嶗乿","仚仚仚仚仚","惣峴帥丂桯乆巕"],
	["乽僄僐乕儘働乕僔儑儞乿","仚仚仚","桯扟丂嬁巕"],
	["乽渇渞偊傞桯楈慏乿","仚仚仚仚仚仚仚","懞幯丂悈枿"],
	["乽億僀僘儞僼僃僓乕乿","仚仚仚仚仚","儊僨傿僗儞丒儊儔儞僐儕乕"],
	["乽堦搧嶶棆寕乿","仚仚仚仚仚仚仚","嵃楫丂梔柌"],
	["乽敀揺骐缁乿","仚仚仚","場敠丂偰傤"],
	["乽埵憡挷棩乮僂僃僀僽僔僼僞乕乯乿","仚仚仚仚仚仚仚","楅愬丒桪撥壺堾丒僀僫僶"],
	["乽僗僾儕儞僌丒僄僼僃儊儔儖乿","仚","儕儕乕儂儚僀僩"],
/*
	["乽僽儔僢僋傾僂僩僉儍僲僺乕乿","仚","儖乕儈傾"],
	["乽僼僃僀僞儖僼儕乕僕傾乿","仚仚","僠儖僲"],
	["乽僩儕僢僉乕僌儕僢僪乿","仚仚","戝梔惛"],
	["乽尪憐愮杮嶗乿","仚仚","儕儕乕儂儚僀僩"],
	["乽僽儔僀儞僪儘乕儗儔僀乿","仚仚","儈僗僥傿傾丒儘乕儗儔僀"],
	["乽揤敀晽敍恘乿","仚仚","幩柦娵丂暥"],
	["乽傾儞僇乕儊僀儖僗僩儘儉乿","仚仚仚","懞幯丂悈枿"],
	["乽崘巰墌晳挶乿","仚仚仚仚仚","惣峴帥丂桯乆巕"],
	["乽僜僂儖僗僥傿乕儔乕乿","仚仚仚","彫埆杺"],
	["乽傾儔僂儞僪僼儗傾乿","仚仚仚","楈塆楬丂嬻"],
	["乽僇儚乕僪儕乕儔價僢僩乿","仚仚仚仚","場敠丂偰傤"],
	["乽惎扖濶濷乮傾乕儈儔儕僔僃僀僋乯乿","仚仚仚仚","楅愬丒桪撥壺堾丒僀僫僶"],

	["乽僥儞僟乕僴儖僔僱僀僔儑儞乿","仚仚仚仚仚","儊僨傿僗儞丒儊儔儞僐儕乕"],
	["乽僼僃僯僢僋僗僥僼儔乿","仚仚仚仚","摗尨丂枀峠"],
	["乽僗儁乕僗僀乕僞乕乿","仚仚仚仚","廫榋栭丂嶇栭"],
	["乽僗僞乕僟僗僩僥僀儖乿","仚仚仚仚仚","僗僞乕僒僼傽僀傾"],
	["乽儉乕儞儔僨傿僄乕僔儑儞乿","仚仚仚仚仚","儖僫僠儍僀儖僪"],
	["乽僜乕儔儔僀僘儗乕僓乕乿","仚仚仚仚仚","僒僯乕儈儖僋"],
	["乽儈僗僥傿乕儕儞僌乿","仚仚仚仚仚","僷僠儏儕乕丒僲乕儗僢僕"],
	["乽僨僀僕乕僋儘乕儔乕乿","仚仚仚仚仚","晽尒丂桯崄"],
	["乽嵃楫搄棿慚乿","仚仚仚仚仚仚","嵃楫丂梔柌"],
	["乽僪儕乕儈儞僌僕儏僄儖乿","仚仚仚仚仚","朒棄嶳丂婸栭"],
	["乽僩僁儖僑償傿僔儏僥偺峠偒栭乿","仚仚仚仚仚","儗儈儕傾丒僗僇乕儗僢僩"],
	["乽僀儕僨僢僙儞僩僄儞僽儗僀僗乿","仚仚仚仚仚","僼儔儞僪乕儖丒僗僇乕儗僢僩"],

	["乽渎婼椨壩嬍乿","仚仚仚仚仚","埳悂丂渎崄"],
	["乽嶰搑愳偺渇渞楈乿","仚仚仚仚仚","彫栰捤丂彫挰"],
	["乽晻曉偟偺棆抏乿","仚仚仚仚仚","晻廱丂偸偊"],
	["乽垷嬻娫抏枊揮憲寢奅乿","仚仚仚仚仚","敧塤丂巼"],
	["乽恖宍憃巺慄乿","仚仚仚仚仚仚","傾儕僗丒儅乕僈僩儘僀僪"],
	["乽嵸偒偺揤棆乿","仚仚仚仚仚仚","塱峕丂堖嬭"],
	["乽媑徦揤彈朄乿","仚仚仚仚仚仚","烌"],
	["乽敀愥偺恄屜乿","仚仚仚仚仚仚","敧塤丂棔"],
	["乽媞惎塮偟偺恄晽乿","仚仚仚仚仚仚","搶晽扟丂憗昪"],
	["乽堄婥徴揤偺寱慚乿","仚仚仚仚仚仚","斾撨柤嫃丂揤巕"],
	["乽僆乕儘儔僂僃乕僽乿","仚仚仚仚仚仚","枺杺"],
	["乽垻媮壋彈晳乿","仚仚仚仚仚仚","旴揷丂垻媮"],

	["乽惎揤攋嵱媩乿","仚仚仚仚仚仚仚","敧堄丂塱椩"],
	["乽慞埆傪惓偡屼岝乿","仚仚仚仚仚仚仚","巐婫塮昉丒儎儅僓僫僪僁"],
	["乽僄僗僇僩僗僆乕僨傿乕儖乿","仚仚仚仚仚仚仚","恄鉟"],
	["乽擑棿楢夊捾乿","仚仚仚仚仚仚仚仚","峠丂旤楅"],
	["乽傾儖僗僐儞價僫僩儕傾乿","仚仚仚仚仚仚仚仚","儈儏乕僘"],
	["乽儕儕僗偺埫偒寧乿","仚仚仚仚仚仚仚仚","儖乕儈傾"],
	["乽僔儏僾儕乕儉僗僾儕儞僌乿","仚仚仚仚仚仚仚仚","儕儕乕儂儚僀僩"],
	["乽僊儞僰儞僈偺怺暎乿","仚仚仚仚仚仚仚仚","彫埆杺"],
	["乽僽儖乕僇乕僶儞僋儖乿","仚仚仚仚仚仚仚仚","朒棄嶳丂婸栭"],
	["乽儅僀儞僊儍儔僋僔乕乿","仚仚仚仚仚仚仚仚","枺杺"]
*/
	];

	/*********************僜乕僩晹***************************/
	alternative(SortType)
	case(0){}//僨僼僅儖僩
	case(1)
	{//嶌昳弴
		ascent(i in 0..length(SpellCardSetting))
		{
			ascent(j in i..length(SpellCardSetting))
			{
				if(GetNameNumber(SpellCardSetting[i][2])>GetNameNumber(SpellCardSetting[j][2]))
				{
					let Temp=SpellCardSetting[i];
					SpellCardSetting[i]=SpellCardSetting[j];
					SpellCardSetting[j]=Temp;
				}
			}
		}
	}	
	case(2)
	{//擄堈搙弴
		ascent(i in 0..length(SpellCardSetting))
		{
			ascent(j in i..length(SpellCardSetting))
			{
				if(SpellCardSetting[i][1]>SpellCardSetting[j][1])
				{
					let Temp=SpellCardSetting[i];
					SpellCardSetting[i]=SpellCardSetting[j];
					SpellCardSetting[j]=Temp;
				}
			}
		}
	}
	/******************僜乕僩廔傢傝*************************/

	let texts=[];
	let Name_texts=[];
	let SpellName_texts=[];

	let C_Array=[];
	let G_Array=[];
	alternative(PlayerCharacter)
	case(0)
	{
		C_Array=GetAreaCommonData("SPELL_RECORD","Reimu_Challenge",[]);
		G_Array=GetAreaCommonData("SPELL_RECORD","Reimu_Get",[]);
	}
	case(1)
	{
		C_Array=GetAreaCommonData("SPELL_RECORD","Marisa_Challenge",[]);
		G_Array=GetAreaCommonData("SPELL_RECORD","Marisa_Get",[]);
	}

	ascent(i in 0..length(SpellCardSetting))
	{
	SetSpellCardNumber(SpellCardSetting[i][0]);//嫟捠僨乕僞"SpellNumber"偵柤慜偵墳偠偰屌掕斣崋傪擖傟傞
	if(GetAreaCommonData("TUNING","Favorite",[0])[GetCommonData("SpellNumber",0)]==1 || FavoriteMode==0)
	{//僼傽儃儌乕僪偺帪偼傆偀傏偟偰傞偺偩偗張棟
		SpellName_texts=SpellName_texts~[SpellCardSetting[i][0]];
		Name_texts=Name_texts~[SpellCardSetting[i][2]];
		if(C_Array[GetCommonData("SpellNumber",0)]>=1)
		{
			texts=texts~[SpellCardSetting[i][0]];
		}
		else
		{
			texts=texts~["挧愴幰曞廤拞"];
		}

		SetSpellCardNumber(SpellCardSetting[i][0]);
		let AddStr;

	/****3寘偵側傞傛偆偵摢偵0傪偮偗偰偄傞丄偦偺屻僗儁乕僗捛壛*****/
		if(i+1<10)
		{
			AddStr="00"~IntToString(i+1);
		}
		else if(i+1<100)
		{
			AddStr="0"~IntToString(i+1);
		}
		else
		{
			AddStr=IntToString(i+1);
		}

	/****偍婥偵擖傝偩偭偨傜*傪偮偗傞*****/

		SetSpellCardNumber(SpellCardSetting[i][0]);
		let F_Array=GetAreaCommonData("TUNING","Favorite",[0]);
		if(F_Array[GetCommonData("SpellNumber",0)]==1)
		{
			AddStr=AddStr~"*";
		}
		else
		{
			AddStr=AddStr~" ";
		}

	/************僗儁儖柤捛壛偟僗儁乕僗偱挷惍*****************/
		texts[i]=AddStr~texts[i];

		while(length(texts[i])<38)
		{
			texts[i]=texts[i]~"丂";
		}

	/************僗儁儖柤捛壛偟僗儁乕僗偱挷惍*****************/
		texts[i]=texts[i]~SpellCardSetting[i][1];


		while(length(texts[i])<56)
		{
			texts[i]=texts[i]~"丂";
		}

	/************庢摼夞悢/挧愴夞悢捛壛*************************/
		AddStr=IntToString(G_Array[GetCommonData("SpellNumber",0)]);
		texts[i]=texts[i]~AddStr;

		AddStr=" / ";
		texts[i]=texts[i]~AddStr;

		AddStr=IntToString(C_Array[GetCommonData("SpellNumber",0)]);
		texts[i]=texts[i]~AddStr;
	}
	else
	{
		texts=texts~[""];
	}
	}
	/************嬻梫慺傪嶍彍*************************/
	descent(i in 0..length(texts))
	{
		if(texts[i]=="")
		{
			texts=erase(texts,i);
		}
	}

	if(length(texts)==0)
	{//僼僃僀僶儕僢僩儌乕僪偵偟傛偆偲偟偨偗偳傆偀傏偑側偄偐傜傗傝捈偟
		FavoriteMode=0;
		SetAreaCommonData("TUNING","FavoriteMode",FavoriteMode);
		bEndTitleScene = true;
		Obj_Delete(objTitleImage);
		Obj_Delete(objTitleText);
		yield;
		TTitleScene;
		return;
	}
	ascent(var iText in 0 .. length(texts))
	{
		SetSpellCardNumber(SpellName_texts[iText]);
		SpellCardInfo_Text=SpellCardInfo_Text~[texts[iText]];

		SpellCardInfo_Get=SpellCardInfo_Get~[G_Array[GetCommonData("SpellNumber",0)]];
		SpellCardInfo_Challenge=SpellCardInfo_Challenge~[C_Array[GetCommonData("SpellNumber",0)]];
	}

	//儊僯儏乕
	let INDEX_START = 0;
	let INDEX_REPLAY = 1;
	let INDEX_QUIT = 2;
	let selectIndex = GetAreaCommonData("TUNING","SelectIndex",0);//慖戰埵抲
	let selectPageIndex=GetAreaCommonData("TUNING","SelectPageIndex",0);//慖戰埵抲
	TMenuItem();//僗儁儖僥僉僗僩昞帵

	let MaxselectPageIndex=0;
	let countMenu = 12;//峴悢愝掕
	//僉乕忬懺偑儕僙僢僩偝傟傞傑偱懸婡
	while(GetVirtualKeyState(VK_OK) != KEY_FREE){yield;}

	SelectEffect();
	Explain();

	//儊僯儏乕慖戰張棟
	let frameKeyHold = 0;//僉乕墴偟偭傁側偟僼儗乕儉悢
	MaxselectPageIndex=floor(length(texts)/countMenu)+1;
	if(length(texts)%countMenu==0){MaxselectPageIndex-=1;}
	let FreeCount=0;
	DemoFlag=0;
	let CanselHoldCount=0;
	loop
	{
		SetAreaCommonData("TUNING","SelectIndex",selectIndex);
		SetAreaCommonData("TUNING","SelectPageIndex",selectPageIndex);
		SetAreaCommonData("TUNING","PlayerCharacter",PlayerCharacter);
		SetAreaCommonData("TUNING","SortType",SortType);

		//寛掕
		if(GetVirtualKeyState(VK_OK) == KEY_PUSH)
		{
			SetSpellCardNumber(SpellName_texts[selectIndex+selectPageIndex*countMenu]);
			SetCommonData("SpellCardNo",GetCommonData("SpellNumber",0));
			SetCommonData("SpellCardID",selectIndex+1+selectPageIndex*countMenu);
			SetCommonData("BossName",Name_texts[selectIndex+selectPageIndex*countMenu]);
			SetCommonData("SpellCardName",SpellName_texts[selectIndex+selectPageIndex*countMenu]);
			SetCommonData("PlayerCharacter",PlayerCharacter);

			let info=SpellCardInfo_Text[selectIndex+selectPageIndex*countMenu];
			let tx=info[38..54];
			let difficult=0;
			ascent(i in 0..8)
			{
				if(tx[i*2..i*2+2]=="仚"){difficult++;}
			}
			SetCommonData("SpellCardDifficult",difficult);
			SetCommonData("Ori_SpellCardBonus",length(SpellCardSetting[selectIndex+selectPageIndex*countMenu][1])/2*50000+100000);

			TStageScene("");

			break;
		}

		//僇乕僜儖堏摦
		if(GetVirtualKeyState(VK_UP) == KEY_PUSH)
		{
			selectIndex--;
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH)
		{
			selectIndex++;
		}
		else if(GetVirtualKeyState(VK_UP) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 24 || (frameKeyHold > 24 && (frameKeyHold % 8 == 0)))
			{
				selectIndex--;
			}
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 24 || (frameKeyHold > 24 && (frameKeyHold % 8 == 0)))
			{
				selectIndex++;
			}
		}

		else if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH)
		{
			selectPageIndex--;
			if(selectPageIndex<0)
			{
				selectPageIndex=MaxselectPageIndex-1;
			}
		}
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH)
		{
			selectPageIndex++;
			if(selectPageIndex>=MaxselectPageIndex)
			{
				selectPageIndex=0;
			}
		}
		else if(GetVirtualKeyState(VK_LEFT) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 24 || (frameKeyHold > 24 && (frameKeyHold % 8 == 0)))
			{
				selectPageIndex--;
				if(selectPageIndex<0)
				{
					selectPageIndex=MaxselectPageIndex-1;
				}
			}
		}
		else if(GetVirtualKeyState(VK_RIGHT) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 24 || (frameKeyHold > 24 && (frameKeyHold % 8 == 0)))
			{
				selectPageIndex++;
				if(selectPageIndex>=MaxselectPageIndex)
				{
					selectPageIndex=0;
				}
			}
		}
		else
		{
			frameKeyHold = 0;
		}

		if(selectIndex < 0) 
		{
			if(selectPageIndex==MaxselectPageIndex-1 && length(texts)%countMenu!=0)
			{//嵟廔儁乕僕偐偮偦偺儁乕僕偺峴偑12偱側偄
				selectIndex = length(texts)%countMenu-1;
			}
			else
			{
				selectIndex = countMenu - 1;
			}
		}
		else
		{
			if(selectPageIndex==MaxselectPageIndex-1 && length(texts)%countMenu!=0)
			{
				if(selectIndex > length(texts)%countMenu-1)
				{//峴傪挻偊偰偄傞偲偒
					selectIndex = 0;
				}
			}
			selectIndex %= countMenu;
		}

		if(GetVirtualKeyState(VK_PAUSE) == KEY_PUSH)
		{//僾儗僀儎乕僉儍儔曄峏
			PlayerCharacter++;
			PlayerCharacter=PlayerCharacter%2;
			SetAreaCommonData("TUNING","PlayerCharacter",PlayerCharacter);

			bEndTitleScene = true;
			yield;
			TTitleScene();
			break;

		}

		if(GetVirtualKeyState(VK_USER1) == KEY_PUSH && GetVirtualKeyState(VK_SPELL) == KEY_FREE)
		{//儕僾儗僀慖戰夋柺堏峴
			yield;
			TReplaySelectScene();
			break;
		}

		if(GetVirtualKeyState(VK_USER2) == KEY_PUSH)
		{//僜乕僩僞僀僾偺曄峏
			SortType+=1;
			SortType=SortType%3;
			SetAreaCommonData("TUNING","SortType",SortType);

			bEndTitleScene = true;
			yield;
			TTitleScene();
			break;

		}

		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH && GetVirtualKeyState(VK_SPELL) == KEY_FREE)
		{//偍婥偵擖傝捛壛丄夝彍
			let F_Array=GetAreaCommonData("TUNING","Favorite",[0]);
			
			SetSpellCardNumber(SpellName_texts[selectIndex+selectPageIndex*countMenu]);
			let Num=GetCommonData("SpellNumber",0);

			if(F_Array[Num]==0)
			{
				F_Array[Num]=1;
			}
			else
			{
				F_Array[Num]=0;
			}
			SetAreaCommonData("TUNING","Favorite",F_Array);

			if(selectPageIndex==MaxselectPageIndex-1 && selectIndex == length(texts)%countMenu-1 && FavoriteMode)
			{
				selectIndex-=1;
				if(selectIndex==-1)
				{
					selectIndex=0;
					FavoriteMode=0;
					SetAreaCommonData("TUNING","FavoriteMode",FavoriteMode);
				}
			}
			SetAreaCommonData("TUNING","SelectIndex",selectIndex);
			SetAreaCommonData("TUNING","SelectPageIndex",selectPageIndex);

			bEndTitleScene = true;
			yield;
			TTitleScene();
			break;

		}
		if(GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH && GetVirtualKeyState(VK_SPELL) == KEY_HOLD)
		{//偍婥偵擖傝丄捠忢儌乕僪傊偺堏峴
			if(FavoriteMode==1)
			{
				FavoriteMode=0;
			}
			else
			{
				FavoriteMode=1;
			}
			SetAreaCommonData("TUNING","FavoriteMode",FavoriteMode);
			selectPageIndex=0;
			selectIndex=0;
			SetAreaCommonData("TUNING","SelectIndex",selectIndex);
			SetAreaCommonData("TUNING","SelectPageIndex",selectPageIndex);
			bEndTitleScene = true;
			yield;
			TTitleScene();
			break;
		}

		if(GetVirtualKeyState(VK_USER1) == KEY_PUSH && GetVirtualKeyState(VK_SPELL) == KEY_HOLD)
		{//愝掕丄捠忢儌乕僪傊偺堏峴
/*			if(FavoriteMode==1)
			{
				SettingMode=0;
			}
			else
			{
				SettingMode=1;
			}
			SetAreaCommonData("TUNING","SettingMode",SettingMode);
			selectPageIndex=0;
			selectIndex=0;
			SetAreaCommonData("TUNING","SelectIndex",selectIndex);
			SetAreaCommonData("TUNING","SelectPageIndex",selectPageIndex);
			bEndTitleScene = true;
			yield;
			TTitleScene();
			break;
*/
			yield;
			TSettingScene();
			break;
		}
	/**********************僨儌僾儗僀晹******************/
/*
		if(GetVirtualKeyState(VK_LEFT)==KEY_FREE
		&&GetVirtualKeyState(VK_RIGHT)==KEY_FREE
		&&GetVirtualKeyState(VK_UP)==KEY_FREE
		&&GetVirtualKeyState(VK_DOWN)==KEY_FREE
		&&GetVirtualKeyState(VK_PAUSE)==KEY_FREE
		)
		{
			FreeCount+=1;
			if(FreeCount==1800)
			{
				yield;
			//	DemoScene();
				SetAreaCommonData("TUNING","DemoCount",GetAreaCommonData("TUNING","DemoCount",0)+1);
				break;
			}
		}
		else{FreeCount=0;}
*/
	/**********************廔椆晹******************/
		if(GetVirtualKeyState(VK_SPELL) == KEY_HOLD)
		{
			CanselHoldCount+=1;
			if(CanselHoldCount>=120){ClosePackage();}
		}
		else
		{
			CanselHoldCount=0;
		}
		yield;
	}

	bEndTitleScene = true;
	Obj_Delete(objTitleImage);
	Obj_Delete(objTitleText);

	task SelectEffect
	{//慖戰帪偺惎僄僼僃僋僩
		let x=0;
		let y=0;
		while(!bEndTitleScene)
		{
			y=39+25*selectIndex;
			Light(rand(0,640),y+rand(15,16),rand(1,2)/3,270+rand(-5,5));
			Light(rand(0,640),y+rand(1,0),rand(1,2)/3,90+rand(-5,5));
			yield;
		}

		task Light(X,Y,speed,angle)
		{
			let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
			ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/effect/star.png");
			ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
			ObjPrim_SetVertexCount(obj, 4);
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
			ObjRender_SetAngleZ(obj,0);

			ObjPrim_SetVertexPosition(obj, 0, -16, -16, -0);
			ObjPrim_SetVertexPosition(obj, 1, -16, 16, -0);
			ObjPrim_SetVertexPosition(obj, 2, 16, -16, 0);
			ObjPrim_SetVertexPosition(obj, 3, 16,16, 0);

			ObjPrim_SetVertexUVT(obj, 0, 0, 0);
			ObjPrim_SetVertexUVT(obj, 1, 0, 32);
			ObjPrim_SetVertexUVT(obj, 2, 32, 0);
			ObjPrim_SetVertexUVT(obj, 3, 32, 32);
			ObjRender_SetPosition(obj,X,Y,0);

			let scale=0.5;
			let Alpha=180;
			let Zangle=0;
			let ZanglePlus=rand(-5,5);

			loop(30)
			{
				scale-=0.5/30;
				Zangle+=ZanglePlus;
				ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
				ObjRender_SetScaleXYZ(obj,scale,scale,scale);
				ObjRender_SetAngleZ(obj,Zangle);
				ObjRender_SetColor(obj,Alpha*0.75,Alpha*0.75,Alpha);
				yield;
			}
			Obj_Delete(obj);
		}
	}

	task Explain
	{//僗儁儖忣曬昞帵
		UnderExplain();//壓晹偵憖嶌愢柧昞帵

		function CreateTextObject(let mx, let my, let text)
		{
			let obj = ObjText_Create();
			ObjText_SetText(obj, text);
			ObjText_SetFontSize(obj, 14+4);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontColorTop(obj, 255, 255, 255);
			ObjText_SetFontColorBottom(obj, 255, 160, 255);
			if(mx==504 || mx==584)
			{
				ObjText_SetFontColorBottom(obj, 160, 160, 255);
			}
			if(my==440 && mx!=584)
			{
				ObjText_SetFontColorBottom(obj, 255, 128, 128);
			}
		//	ObjText_SetFontBorderType(obj, BORDER_FULL);
			ObjText_SetFontBorderType(obj, BORDER_NONE);
			ObjText_SetFontBorderColor(obj,255, 255, 255);
			ObjText_SetFontBorderWidth(obj, 2*0);
			Obj_SetRenderPriorityI(obj, 72);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);

			ObjText_SetFontType(obj,"俵俽 柧挬");

			return obj;
		}

		let objText = CreateTextObject(14, 352,"巊梡幰丗丂"~"Unknown");
		let objText2 = CreateTextObject(14, 352+20, "弶庢摼挧愴悢丂"~"N/A");
		let objText3 = CreateTextObject(14, 352+20*2, "嵟崅僗僐傾丂丂"~"N/A");
		let objText4 = CreateTextObject(14, 352+20*3, "嵟抁寕攋帪娫丂"~"N/A");

		let objText5 = CreateTextObject(504, 352, "Page丂"~"1 / "~IntToString(MaxselectPageIndex));
		let objText6 = CreateTextObject(356, 352+20*1, "憤庢摼枃悢丂丂丂"~"18/42");
		let objText7 = CreateTextObject(356, 352+20*2, "憤崌嵟崅僗僐傾丂");
		let objText8 = CreateTextObject(356, 352+20*3, "憤寕攋帪娫丂丂丂"~"N/A");

		let objText9 = CreateTextObject(14, 352+20*4+8, "Player丂"~"攷楉丂楈柌");

		let objText10 = CreateTextObject(584, 440,"");

		while(!bEndTitleScene)
		{
			SetSpellCardNumber(SpellName_texts[selectIndex+selectPageIndex*countMenu]);

			let C_Array=[];
			let G_Array=[];
			let S_Array=[];
			let Fi_Array=[];
			let E_Array=[];
			alternative(PlayerCharacter)
			case(0)
			{
				C_Array=GetAreaCommonData("SPELL_RECORD","Reimu_Challenge",[]);
				G_Array=GetAreaCommonData("SPELL_RECORD","Reimu_Get",[]);
				S_Array=GetAreaCommonData("SPELL_RECORD","Reimu_Score",[]);
				Fi_Array=GetAreaCommonData("SPELL_RECORD","Reimu_First",[]);
				E_Array=GetAreaCommonData("SPELL_RECORD","Reimu_Early",[]);
			}
			case(1)
			{
				C_Array=GetAreaCommonData("SPELL_RECORD","Marisa_Challenge",[]);
				G_Array=GetAreaCommonData("SPELL_RECORD","Marisa_Get",[]);
				S_Array=GetAreaCommonData("SPELL_RECORD","Marisa_Score",[]);
				Fi_Array=GetAreaCommonData("SPELL_RECORD","Marisa_First",[]);
				E_Array=GetAreaCommonData("SPELL_RECORD","Marisa_Early",[]);
			}

			if(C_Array[GetCommonData("SpellNumber",0)]!=0)
			{
				ObjText_SetText(objText, "巊梡幰丗丂"~Name_texts[selectIndex+selectPageIndex*countMenu]);
			}
			else
			{
				ObjText_SetText(objText, "巊梡幰丗丂"~"Unknown");
			}
			if(Fi_Array[GetCommonData("SpellNumber",0)]!=0)
			{
			ObjText_SetText(objText2, "弶庢摼挧愴悢丂"~IntToString(Fi_Array[GetCommonData("SpellNumber",0)]));
			}
			else
			{
				ObjText_SetText(objText2, "弶庢摼挧愴悢丂"~"N/A");
			}

			if(S_Array[GetCommonData("SpellNumber",0)]!=0)
			{
				ObjText_SetText(objText3, "嵟崅僗僐傾丂丂"~IntToString(S_Array[GetCommonData("SpellNumber",0)]));
			}
			else
			{
				ObjText_SetText(objText3, "嵟崅僗僐傾丂丂"~"N/A");
			}

			if(E_Array[GetCommonData("SpellNumber",0)]!=100000)
			{
				let TimeArray=IntToString(floor(E_Array[GetCommonData("SpellNumber",0)]*100/60));
				alternative(length(TimeArray))
				case(3){TimeArray=TimeArray[0..1]~"."~TimeArray[1..3]~" s";}
				case(4){TimeArray=TimeArray[0..2]~"."~TimeArray[2..4]~" s";}
				ObjText_SetText(objText4, "嵟抁寕攋帪娫丂"~TimeArray);
			}
			else
			{
				ObjText_SetText(objText4, "嵟抁寕攋帪娫丂"~"N/A");
			}

			ObjText_SetText(objText5, "Page丂"~IntToString(selectPageIndex+1)~" / "~IntToString(MaxselectPageIndex));

			if(PlayerCharacter==0)
			{
				ObjText_SetText(objText9, "Player丂"~"攷楉丂楈柌");
			}
			else if(PlayerCharacter==1)
			{
				ObjText_SetText(objText9, "Player丂"~"柖塉丂杺棟嵐");
			}

			ObjRender_SetX(objText10,584);
			let fpslength=5;
			if(GetCurrentFps>=100){fpslength=6;ObjRender_SetX(objText10,574);}
			ObjText_SetText(objText10,vtos("3f",GetCurrentFps)[0..fpslength]);

			let TotalGet=0;
			let TotalScore=0;
			let TotalTime=0;
			ascent(i in 0..length(SpellCardSetting))
			{
				SetSpellCardNumber(SpellCardSetting[i][0]);
				if(G_Array[GetCommonData("SpellNumber",0)]>=1)
				{
					TotalGet+=1;
				}
				if(S_Array[GetCommonData("SpellNumber",0)]>=0)
				{
					TotalScore+=S_Array[GetCommonData("SpellNumber",0)];
				}
				if(E_Array[GetCommonData("SpellNumber",0)]<100000)
				{
					TotalTime+=E_Array[GetCommonData("SpellNumber",0)];
				}
			}
			ObjText_SetText(objText6, "憤庢摼枃悢丂丂丂"~IntToString(TotalGet)~"丂/丂"~IntToString(length(SpellCardSetting)));
			ObjText_SetText(objText7, "憤崌嵟崅僗僐傾丂"~IntToString(TotalScore));
			if(TotalGet==length(SpellCardSetting))
			{
				let TimeArray=IntToString(floor(TotalTime)*100/60);
				alternative(length(TimeArray))
				case(4){TimeArray=TimeArray[0..2]~"."~TimeArray[2..4]~" s";}
				case(5){TimeArray=TimeArray[0..3]~"."~TimeArray[3..5]~" s";}
				case(6){TimeArray=TimeArray[0..4]~"."~TimeArray[4..6]~" s";}
				case(7){TimeArray=TimeArray[0..5]~"."~TimeArray[5..7]~" s";}
				ObjText_SetText(objText8, "憤寕攋帪娫丂丂丂"~TimeArray);
			}

			yield;
		}
		Obj_Delete(objText);
		Obj_Delete(objText2);
		Obj_Delete(objText3);
		Obj_Delete(objText4);
		Obj_Delete(objText5);
		Obj_Delete(objText6);
		Obj_Delete(objText7);
		Obj_Delete(objText8);
		Obj_Delete(objText9);
		Obj_Delete(objText10);
	}

	task UnderExplain
	{//夋柺壓晹憖嶌愢柧昞帵
		let text="Shot Decide  Pause CharacterChange  User1 Replay  User2 SortChange  Cansel_Hold Quit";
		let text2="Slow Favorite  Cansel_Hold+Slow Favorite_Mode  Cansel_Hold+User1 Setting_Mode";
		let obj = ObjText_Create();
		ObjText_SetText(obj,text);
		ObjText_SetFontSize(obj, 12);
		ObjText_SetFontBold(obj, true);
		ObjText_SetFontBorderType(obj, BORDER_NONE);
		ObjText_SetFontBorderColor(obj,255, 255, 255);
		ObjText_SetFontBorderWidth(obj, 0);
		Obj_SetRenderPriorityI(obj, 30);


		ObjRender_SetX(obj, 14);
		ObjRender_SetY(obj, 464);

		let Alpha=255;
		let AlphaAngle=270;
		let x=14;
		let x2=488;
		while(!bEndTitleScene)
		{
			ObjRender_SetX(obj, x);
			ObjRender_SetAlpha(obj,255*absolute(cos(AlphaAngle)));
			AlphaAngle+=0.5;
			if(AlphaAngle%360<90 || AlphaAngle%360>270)
			{
			ObjText_SetText(obj,text);
			}
			else
			{
			ObjText_SetText(obj,text2);
			}
			yield;
		}
		Obj_Delete(obj);
	}
}


//----------------------------------------------------
// 僗僥乕僕僔乕儞
//----------------------------------------------------
task TStageScene(let pathReplay)
{
	let dirCurrent = GetCurrentScriptDirectory();
	let dirModule = GetModuleDirectory();
	let pathMainScript = dirCurrent ~ "SpellCard/Stage.txt";
	let pathPlayer;
	if(PlayerCharacter==0)
	{
		pathPlayer = dirCurrent ~ "player/Reimu/Reimu.txt";
	}
	else if(PlayerCharacter==1)
	{
		pathPlayer = dirCurrent ~ "player/Marisa/Marisa.txt";
	}
	//--------------------------------

	//夋柺慗堏
	RenderSceneToTransitionTexture();
	TTransition();

	SoundFlag=0;

	//--------------------------------
	//僗僥乕僕弶婜壔
	InitializeStageScene();
	if(length(pathReplay) > 0)
	{
		SetStageReplayFile(pathReplay);
	}

	//--------------------------------
	//弶婜僗僥乕僕愝掕

	let indexStage = 1+1; //僗僥乕僕僀儞僨僢僋僗
	SetStageIndex(indexStage);
	SetStageMainScript(pathMainScript);
	SetStagePlayerScript(pathPlayer);
	StartStageScene();

	StageMusicFlag=1;
	TStageSound;

	//--------------------------------
	//僗僥乕僕憖嶌

	MB_Save();

	loop
	{
		if(GetVirtualKeyState(VK_SHOT) == KEY_PUSH && DemoFlag==1)
		{
			TerminateStageScene();
		}
		//--------------------------------
		//堦帪掆巭僠僃僢僋
		if(GetVirtualKeyState(VK_PAUSE) == KEY_PUSH && DemoFlag==0)
		{
			let resPause = RunPauseScene();
			alternative(resPause)
			case(RESULT_RETRY)
			{
				//嵟弶偐傜傗傝捈偡
				if(!IsReplay())
				{
					//儕僩儔僀

					TerminateStageScene();
					TStageScene("");
					return;
				}
			}
			case(RESULT_END)
			{
				//堦帪掆巭拞偺廔椆
				TerminateStageScene();
			}
		}

		//--------------------------------
		//僗僥乕僕姰椆懸偪
		let stgSceneState = GetStageSceneState();
		if(stgSceneState == STAGE_STATE_FINISHED)
		{
			//僗僥乕僕廔椆寢壥傪庢摼
			let stageResult = GetStageSceneResult();
			alternative(stageResult)
			case(STAGE_RESULT_CLEARED)
			{
				//僗僥乕僕僋儕傾
				if(indexStage == 2)
				{
					//僗僥乕僕2傪僋儕傾偟偨応崌偼廔椆
					if(IsReplay)
					{
						TEndScene();
					}
					else
					{
						TClearScene();
					}
					break;
				}
				else
				{
					//師偺僗僥乕僕傊慗堏
					indexStage++;
					SetStageIndex(indexStage);
					SetStageMainScript(pathMainScript); //傂偲傑偢摨偠僗僥乕僕...
					SetStagePlayerScript(pathPlayer);
					StartStageScene();
					TTransition();
				}
			}
			case(STAGE_RESULT_PLAYER_DOWN)
			{
				//帺婡巆婡0
				TEndScene();
				break;
			}
			case(STAGE_RESULT_BREAK_OFF)
			{
				//僗僥乕僕搑拞廔椆
				//堦帪掆巭拞偺廔椆
				if(IsReplay)
				{
					TEndScene();
				}
				else
				{
					TTitleScene();
					T_Sound();
				}
				break;
			}
		}
		yield;
	}	

	TTransition();
}

task TStageSound
{
	let StageMusicObject=ObjSound_Create;
	SetCommonData("MusicStep",0);
	let Difficult=GetCommonData("SpellCardDifficult",1);
	if(Difficult<=4)
	{
		ObjSound_Load(StageMusicObject,GetCurrentScriptDirectory~"bgm/bgm1.ogg");
	}
	else if(Difficult<=8)
	{
		ObjSound_Load(StageMusicObject,GetCurrentScriptDirectory~"bgm/bgm2.ogg");
	}
	else
	{
		ObjSound_Load(StageMusicObject,GetCurrentScriptDirectory~"bgm/bgm3.ogg");
	}
	ObjSound_SetLoopEnable(StageMusicObject,true);
//	ObjSound_SetLoopTime(StageMusicObject,47.297,169.970);
	ObjSound_SetSoundDivision(StageMusicObject,SOUND_BGM);
	ObjSound_Play(StageMusicObject);
	ObjSound_SetRestartEnable(StageMusicObject,true);
	Obj_SetValue(StageMusicObject,"Stop",false);
	let CurrentVulume=1;
	let MaxVulume=GetAreaCommonData("TUNING","Setting",[])[0];
	while(StageMusicFlag)
	{
		if(CurrentVulume<1){CurrentVulume+=0.05;}
		if(StageMusicStopFlag)
		{
			ObjSound_Stop(StageMusicObject);
			ObjSound_SetVolumeRate(StageMusicObject,0);
			CurrentVulume=0;
		}
		else
		{
			yield;
			ObjSound_Play(StageMusicObject);
			if(GetAreaCommonData("TUNING","BGM",1)==1)
			{
				ObjSound_SetVolumeRate(StageMusicObject,MaxVulume*CurrentVulume);
			}
			else
			{
				ObjSound_SetVolumeRate(StageMusicObject,0);
			}
		}
		if(GetCommonData("BossDefeat",false))
		{
			loop(240)
			{
				CurrentVulume-=1/240;
				ObjSound_SetVolumeRate(StageMusicObject,MaxVulume*CurrentVulume);
				yield;
			}
			loop
			{
				yield;
				if(StageMusicFlag==0){break;}
			}
		}
		yield;
	}
/*
	CurrentVulume=1;
	while(CurrentVulume>0)
	{
		CurrentVulume-=1/240;
		if(StageMusicStopFlag)
		{
			ObjSound_Stop(StageMusicObject);
			//CurrentVulume=ObjSound_GetVolumeRate(StageMusicObject);
			ObjSound_SetVolumeRate(StageMusicObject,0);
		}
		else
		{
			yield;
			ObjSound_Play(StageMusicObject);
			if(GetAreaCommonData("TUNING","BGM",1)==1)
			{
				ObjSound_SetVolumeRate(StageMusicObject,100*CurrentVulume);
			}
			else
			{
				ObjSound_SetVolumeRate(StageMusicObject,0);
			}
		}
		yield;
	}
*/
	Obj_Delete(StageMusicObject);
}

//----------------------------------------------------
// 廔椆僔乕儞
//----------------------------------------------------
task TClearScene()
{
	if(IsReplay())
	{
		TTitleScene();
		T_Sound();
		return;
	}

	//--------------------------------
	//僗僥乕僕廔椆張棟
	FinalizeStageScene();

	//--------------------------------
	//廔椆僔乕儞僗僋儕僾僩婲摦
	let dirModule = GetModuleDirectory();
	let pathScript = GetCurrentScriptDirectory() ~ "system/ClearScene.txt";
	let idScript = LoadScript(pathScript);
	StartScript(idScript);

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

	//--------------------------------
	//廔椆僔乕儞寢壥張棟

	MB_Save();

	let result = GetScriptResult(idScript);
	alternative(result)
	case(RESULT_SAVE_REPLAY)
	{
		//儕僾儗僀曐懚
		TReplaySaveScene();
	}
	case(RESULT_END)
	{
		//嵞惗廔椆
		TTitleScene();
		T_Sound();
	}
	case(RESULT_RETRY)
	{
		//儕僩儔僀
		TStageScene("");
	}
}

task TEndScene()
{
	if(DemoFlag==1)
	{
		SoundFlag=1;
		StageMusicFlag=0;
		T_Sound;
	//	TReplaySelectScene();
		TTitleScene();
		T_Sound;
		return;
	}
	if(IsReplay())
	{
		SoundFlag=1;
		StageMusicFlag=0;
		T_Sound;
		TReplaySelectScene();
	//	TTitleScene();
		return;
	}

	//--------------------------------
	//僗僥乕僕廔椆張棟
	FinalizeStageScene();

	//--------------------------------
	//廔椆僔乕儞僗僋儕僾僩婲摦
	let dirModule = GetModuleDirectory();
	let pathScript = GetCurrentScriptDirectory() ~ "system/EndScene.txt";
	let idScript = LoadScript(pathScript);
	StartScript(idScript);

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

	//--------------------------------
	//廔椆僔乕儞寢壥張棟

	MB_Save();

	let result = GetScriptResult(idScript);
	alternative(result)
	case(RESULT_SAVE_REPLAY)
	{
		//儕僾儗僀曐懚
		TReplaySaveScene();
	}
	case(RESULT_END)
	{
		//嵞惗廔椆
		TTitleScene();
		T_Sound();
	}
	case(RESULT_RETRY)
	{
		//儕僩儔僀
		TStageScene("");
	}
}

//----------------------------------------------------
// 堦帪掆巭僔乕儞
//----------------------------------------------------
function RunPauseScene()
{
	StageMusicStopFlag=1;
	RenderSceneToTransitionTexture();
	PauseStageScene(true);

	let dirModule = GetModuleDirectory();
//	let pathScript = dirModule ~ "script/default_system/Default_Pause.txt";
	let pathScript = GetCurrentScriptDirectory() ~ "system/Pause.txt";

	//--------------------------------
	//堦帪掆巭僔乕儞儞僗僋儕僾僩婲摦
	let idScript = LoadScript(pathScript);
	StartScript(idScript);

	while(!IsCloseScript(idScript))
	{
		yield;
	}
	StageMusicStopFlag=0;
	PauseStageScene(false);

	let res = GetScriptResult(idScript);
	return res;
}


//----------------------------------------------------
// 儕僾儗僀慖戰僔乕儞
//----------------------------------------------------
task TReplaySelectScene()
{
	let dirCurrent = GetCurrentScriptDirectory();
	let pathScript = dirCurrent ~ "system/ReplaySelectScene.txt";

	//--------------------------------
	//儕僾儗僀慖戰僔乕儞僗僋儕僾僩婲摦
	let idScript = LoadScript(pathScript);
	StartScript(idScript);

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

	//--------------------------------
	//儕僾儗僀慖戰僔乕儞寢壥張棟
	let result = GetScriptResult(idScript);

	if(length(result) == 0)
	{
		//僞僀僩儖僔乕儞傊栠傞
		TTitleScene();
	}
 	else
 	{
		//儕僾儗僀嵞惗奐巒
		TStageScene(result);
	}
}


//----------------------------------------------------
// 僨儌僔乕儞
//----------------------------------------------------
task DemoScene()
{
	DemoFlag=1;
	let dirCurrent = GetCurrentScriptDirectory();
	let pathScript = dirCurrent ~ "system/DemoScene.txt";

	//--------------------------------
	//儕僾儗僀慖戰僔乕儞僗僋儕僾僩婲摦
	let idScript = LoadScript(pathScript);
	StartScript(idScript);

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

	//--------------------------------
	//儕僾儗僀慖戰僔乕儞寢壥張棟
	let result = GetScriptResult(idScript);

	TStageScene(result);
}

//----------------------------------------------------
// 儕僾儗僀曐懚僔乕儞
//----------------------------------------------------
task TReplaySaveScene()
{
	let dirCurrent = GetCurrentScriptDirectory();
	let pathScript = dirCurrent ~ "system/ReplaySaveScene.txt";

	//--------------------------------
	//儕僾儗僀曐懚僔乕儞僗僋儕僾僩婲摦
	let idScript = LoadScript(pathScript);
	StartScript(idScript);

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


	//--------------------------------
	//僞僀僩儖僔乕儞傊慗堏
	TTitleScene();
	T_Sound;
}

//----------------------------------------------------
// 愝掕僔乕儞
//----------------------------------------------------
task TSettingScene()
{
	let dirCurrent = GetCurrentScriptDirectory();
	let pathScript = dirCurrent ~ "system/SettingScene.txt";

	//--------------------------------
	//儕僾儗僀慖戰僔乕儞僗僋儕僾僩婲摦
	let idScript = LoadScript(pathScript);
	StartScript(idScript);

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

	//--------------------------------
	//儕僾儗僀慖戰僔乕儞寢壥張棟
	let result = GetScriptResult(idScript);

	if(length(result) == 0)
	{
		//僞僀僩儖僔乕儞傊栠傞
		TTitleScene();
	}
 	else
 	{//巊梡偟側偄		
		TStageScene(result);
	}
}

//----------------------------------------------------
// 夋柺慗堏僄僼僃僋僩
//----------------------------------------------------
function RenderSceneToTransitionTexture()
{
	//尰嵼偺僔乕儞撪梕傪夋柺慗堏梡僥僋僗僠儍偵昤夋
	let textureName = GetTransitionRenderTargetName();
	RenderToTextureA1(textureName, 0, 100, true);
}

task TTransition
{
	let textureName = GetTransitionRenderTargetName();

	let objImage = ObjPrim_Create(OBJ_SPRITE_2D); //2D僗僾儔僀僩僆僽僕僃僋僩惗惉
	Obj_SetRenderPriorityI(objImage, 100); //昤夋桪愭搙傪愝掕
	ObjPrim_SetTexture(objImage, textureName); //僥僋僗僠儍傪愝掕
	ObjSprite2D_SetSourceRect(objImage, 0, 0, 640, 480);
	ObjSprite2D_SetDestRect(objImage, 0, 0, 640, 480);

	let alpha = 255;
	while(alpha > 0)
	{
		ObjRender_SetAlpha(objImage, alpha);
		alpha -= 16;
		yield;
	}
	Obj_Delete(objImage);
}

function MB_Save
{
	if(!IsReplay())
	{
		SaveCommonDataAreaA1("SPELL_RECORD");
		SaveCommonDataAreaA1("TUNING");
	}
}