#搶曽抏枊晽[Package]
#Title["Maiden's Banquet -彮彈払偺抏枊嵳- ver1.20a"]
#Text["Maiden's Banquet ver1.20a"]
#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=[];
		loop(100)
		{
			Spell_C_Array=Spell_C_Array~[0];
			Spell_G_Array=Spell_G_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","PlayerName","NEMO");
	}
	yield;
//	GetLoadFreePlayerScriptList;
	TTitleScene();
	T_Sound;
//	DrawTitleEffect;
//	DrawTitleFrame;
//	ContinueSystem;
}


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

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);
	if(SoundFlag==1)
	{
	ObjSound_Play(objMusic);
	if(GetAreaCommonData("TUNING","BGM",1)==1)
	{
	ObjSound_SetVolumeRate(objMusic,80);
	}
	else
	{
	ObjSound_SetVolumeRate(objMusic,0);
	}
	while(SoundFlag){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 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_FULL);
	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,"俵俽 柧挬");

	//儊僯儏乕
	let INDEX_START = 0;
	let INDEX_REPLAY = 1;
	let INDEX_QUIT = 2;
	let selectIndex = GetAreaCommonData("TUNING","SelectIndex",0);//慖戰埵抲
	let selectPageIndex=GetAreaCommonData("TUNING","SelectPageIndex",0);//慖戰埵抲
	let MaxselectPageIndex=0;
	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_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,"俵俽 柧挬");
			Obj_SetValue(obj,"BaseAlpha",255);
			return obj;
		}

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

		let OldPage=selectPageIndex;
		let dir=1;
		while(!bEndTitleScene)
		{
			ascent(i in 0..12)
			{
				if(length(SpellCardInfo_Text)>i+selectPageIndex*12)
				{
				ObjText_SetText(objTextArray[i], SpellCardInfo_Text[i+selectPageIndex*12]);
				if(SpellCardInfo_Challenge[i+selectPageIndex*12]>=1){
				if(SpellCardInfo_Get[i+selectPageIndex*12]>=1){ObjText_SetFontColorBottom(objTextArray[i], 128, 255, 192);}
				else{ObjText_SetFontColorBottom(objTextArray[i], 255, 64, 64);}
				}
				else{ObjText_SetFontColorBottom(objTextArray[i], 255, 255, 255);}
				}
				else
				{
				ObjText_SetText(objTextArray[i], "");
				}
			}
			while(OldPage==selectPageIndex)
			{
				if(GetVirtualKeyState(VK_RIGHT) == KEY_PUSH || GetVirtualKeyState(VK_RIGHT) == KEY_HOLD)
				{
					dir=1;
				}
				if(GetVirtualKeyState(VK_LEFT) == KEY_PUSH || GetVirtualKeyState(VK_LEFT) == KEY_HOLD)
				{
					dir=-1;
				}
				if(bEndTitleScene){break;}
				yield;
			}
			if(bEndTitleScene){break;}

			ascent(i in 0..12)
			{
				Obj_SetValue(objTextArray[i],"BaseAlpha",0);
				ObjRender_SetX(objTextArray[i], 14+100*dir);
				ObjRender_SetAlpha(objTextArray[i],0);
			}
			ascent(i in 12..24)
			{
				Obj_SetValue(objTextArray[i],"BaseAlpha",255);
				ObjRender_SetX(objTextArray[i], 14);
				ObjRender_SetAlpha(objTextArray[i],255);
				if(selectIndex%12==i)
				{
					ObjRender_SetAlpha(objTextArray[i],Obj_GetValue(objTextArray[i],"BaseAlpha"));
				}
				else
				{
					ObjRender_SetAlpha(objTextArray[i],Obj_GetValue(objTextArray[i],"BaseAlpha")*0.6);
				}
			}
			ascent(i in 0..12)
			{
				if(length(SpellCardInfo_Text)>i+selectPageIndex*12)
				{
				ObjText_SetText(objTextArray[i], SpellCardInfo_Text[i+selectPageIndex*12]);
				if(SpellCardInfo_Challenge[i+selectPageIndex*12]>=1){
				if(SpellCardInfo_Get[i+selectPageIndex*12]>=1){ObjText_SetFontColorBottom(objTextArray[i], 128, 255, 192);}
				else{ObjText_SetFontColorBottom(objTextArray[i], 255, 64, 64);}
				}
				else{ObjText_SetFontColorBottom(objTextArray[i], 255, 255, 255);}
				}
				else
				{
				ObjText_SetText(objTextArray[i], "");
				}
			}
			ascent(i in 12..24)
			{
				if(length(SpellCardInfo_Text)>i+OldPage*12-12)
				{
				ObjText_SetText(objTextArray[i], SpellCardInfo_Text[i+OldPage*12-12]);
				if(SpellCardInfo_Challenge[i+OldPage*12-12]>=1){
				if(SpellCardInfo_Get[i+OldPage*12-12]>=1){ObjText_SetFontColorBottom(objTextArray[i], 128, 255, 192);}
				else{ObjText_SetFontColorBottom(objTextArray[i], 255, 64, 64);}
				}
				else{ObjText_SetFontColorBottom(objTextArray[i], 255, 255, 255);}
				}
				else
				{
				ObjText_SetText(objTextArray[i], "");
				}
			}
			OldPage=selectPageIndex;
			let AlphaRate=0;
			let AlphaRate2=1;
			let Time=10;

			loop(Time)
			{
				AlphaRate+=1/Time;
				ascent(i in 0..12)
				{
				ObjRender_SetX(objTextArray[i], ObjRender_GetX(objTextArray[i])-100/Time*dir);
				Obj_SetValue(objTextArray[i],"BaseAlpha",AlphaRate*255);
				}
				AlphaRate2-=1/Time;
				ascent(i in 12..24)
				{
				ObjRender_SetX(objTextArray[i], ObjRender_GetX(objTextArray[i])-100/Time*dir);
				Obj_SetValue(objTextArray[i],"BaseAlpha",AlphaRate2*255);
				}
				yield;
			}
		}

		ascent(i in 0..24)
		{
			Obj_Delete(objTextArray[i]);
		}

		task AlphaCont
		{
			while(!bEndTitleScene)
			{
				ascent(i in 0..24)
				{
				if(selectIndex%12==i)
				{
					ObjRender_SetAlpha(objTextArray[i],Obj_GetValue(objTextArray[i],"BaseAlpha"));
				}
				else
				{
					ObjRender_SetAlpha(objTextArray[i],Obj_GetValue(objTextArray[i],"BaseAlpha")*0.6);
				}
				}
				yield;
			}	
		}
	}
	yield;

	//儊僯儏乕攝抲
	let mx = 14;
	let my = 14+25;
	let texts = ["Start", "Replay", "Quit"];
	let SpellCardSetting=[
	["乽僽儔僢僋傾僂僩僉儍僲僺乕乿","仚","儖乕儈傾"],
	["乽僼僃僀僞儖僼儕乕僕傾乿","仚仚","僠儖僲"],
	["乽僩儕僢僉乕僌儕僢僪乿","仚仚","戝梔惛"],
	["乽尪憐愮杮嶗乿","仚仚","儕儕乕儂儚僀僩"],
	["乽僽儔僀儞僪儘乕儗儔僀乿","仚仚","儈僗僥傿傾丒儘乕儗儔僀"],
	["乽揤敀晽敍恘乿","仚仚","幩柦娵丂暥"],
	["乽傾儞僇乕儊僀儖僗僩儘儉乿","仚仚仚","懞幯丂悈枿"],
	["乽崘巰墌晳挶乿","仚仚仚仚仚","惣峴帥丂桯乆巕"],
	["乽僜僂儖僗僥傿乕儔乕乿","仚仚仚","彫埆杺"],
	["乽傾儔僂儞僪僼儗傾乿","仚仚仚","楈塆楬丂嬻"],
	["乽僇儚乕僪儕乕儔價僢僩乿","仚仚仚仚","場敠丂偰傤"],
	["乽惎扖濶濷乮傾乕儈儔儕僔僃僀僋乯乿","仚仚仚仚","楅愬丒桪撥壺堾丒僀僫僶"],

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

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

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

	texts=[];
	ascent(i in 0..length(SpellCardSetting))
	{
		SetSpellCardNumber(SpellCardSetting[i][0]);
		let C_Array=[];
		alternative(PlayerCharacter)
		case(0){C_Array=GetAreaCommonData("SPELL_RECORD","Reimu_Challenge",[]);}
		case(1){C_Array=GetAreaCommonData("SPELL_RECORD","Marisa_Challenge",[]);}

		if(C_Array[GetCommonData("SpellNumber",0)]>=1)
		{
		texts=texts~[SpellCardSetting[i][0]];
		}
		else
		{
		texts=texts~["挧愴幰曞廤拞"];
		}
	}

	ascent(i in 1..length(texts)+1)
	{
		SetSpellCardNumber(SpellCardSetting[i-1][0]);
		let AddStr;
		if(i<10)
		{
			AddStr="00"~IntToString(i);
		}
		else if(i<100)
		{
			AddStr="0"~IntToString(i);
		}
		else
		{
			AddStr=IntToString(i);
		}
		AddStr=AddStr~" ";

		texts[i-1]=AddStr~texts[i-1];

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


		texts[i-1]=texts[i-1]~SpellCardSetting[i-1][1];


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

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

		AddStr=IntToString(G_Array[GetCommonData("SpellNumber",0)]);
		texts[i-1]=texts[i-1]~AddStr;

		AddStr=" / ";
		texts[i-1]=texts[i-1]~AddStr;
/*
		while(length(texts[i-1])<68)
		{
			texts[i-1]=texts[i-1]~"丂";
		}
*/
		let C_Array=[];
		alternative(PlayerCharacter)
		case(0){C_Array=GetAreaCommonData("SPELL_RECORD","Reimu_Challenge",[]);}
		case(1){C_Array=GetAreaCommonData("SPELL_RECORD","Marisa_Challenge",[]);}

		AddStr=IntToString(C_Array[GetCommonData("SpellNumber",0)]);
		texts[i-1]=texts[i-1]~AddStr;
	}

	var countMenu = 12;
	ascent(var iText in 0 .. length(texts))
	{
		SetSpellCardNumber(SpellCardSetting[iText][0]);

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

	//	TMenuItem(iText, mx, my+(iText%12)*25, texts[iText],G_Array[GetCommonData("SpellNumber",0)],C_Array[GetCommonData("SpellNumber",0)]);
		SpellCardInfo_Text=SpellCardInfo_Text~[texts[iText]];
		SpellCardInfo_Get=SpellCardInfo_Get~[G_Array[GetCommonData("SpellNumber",0)]];
		SpellCardInfo_Challenge=SpellCardInfo_Challenge~[C_Array[GetCommonData("SpellNumber",0)]];
	}
	TMenuItem;

	//僉乕忬懺偑儕僙僢僩偝傟傞傑偱懸婡
	while(GetVirtualKeyState(VK_OK) != KEY_FREE){yield;}

	SelectEffect;
	Explain;

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

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

			SetCommonData("Ori_SpellCardBonus",length(SpellCardSetting[selectIndex+selectPageIndex*12][1])/2*50000+100000);

			TStageScene("");
/*
			if(selectIndex == INDEX_START)
			{
				SetCommonData("SpellCardNo",selectIndex+1);
				TStageScene("");
			}
			else if(selectIndex == INDEX_REPLAY)
			{
				TReplaySelectScene();
			}
			else if(selectIndex == INDEX_QUIT)
			{
				ClosePackage();
			}
*/
			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(GetVirtualKeyState(VK_PAUSE) == KEY_PUSH)
		{
			PlayerCharacter++;
			PlayerCharacter=PlayerCharacter%2;
			SetAreaCommonData("TUNING","PlayerCharacter",PlayerCharacter);

			bEndTitleScene = true;
			yield;
			TTitleScene;
			break;

		}

		if(GetVirtualKeyState(VK_USER1) == KEY_PUSH)
		{
			yield;
			TReplaySelectScene();
			break;
		}

		if(selectIndex < 0) 
		{
			if(selectPageIndex==MaxselectPageIndex-1 && length(SpellCardSetting)%countMenu!=0)
			{
			selectIndex = length(SpellCardSetting)%countMenu-1;
			}
			else
			{
			selectIndex = countMenu - 1;
			}
		}
		else
		{
			if(selectPageIndex==MaxselectPageIndex-1 && length(SpellCardSetting)%countMenu!=0)
			{
				if(selectIndex > length(SpellCardSetting)%countMenu-1)
				{
					selectIndex = 0;
				}
			}
			selectIndex %= countMenu;
		}

		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>=60){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(0,16),rand(1,3),180+rand(-5,5));
			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 Band
		{

		}

		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)
			{
			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, 458,"");

		while(!bEndTitleScene)
		{
			SetSpellCardNumber(SpellCardSetting[selectIndex+selectPageIndex*12][0]);

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

			if(C_Array[GetCommonData("SpellNumber",0)]!=0)
			{
			//	ObjText_SetText(objText, "巊梡幰丗丂"~SpellCardSetting[GetCommonData("SpellNumber",0)-1][2]);
				ObjText_SetText(objText, "巊梡幰丗丂"~SpellCardSetting[selectIndex+selectPageIndex*12][2]);

			}
			else
			{
				ObjText_SetText(objText, "巊梡幰丗丂"~"Unknown");
			}
			if(F_Array[GetCommonData("SpellNumber",0)]!=0)
			{
			ObjText_SetText(objText2, "弶庢摼挧愴悢丂"~IntToString(F_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);
		//		ObjText_SetText(objText4, "嵟抁寕攋帪娫丂"~IntToString(E_Array[GetCommonData("SpellNumber",0)]));
			}
			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 obj = ObjText_Create();
		ObjText_SetText(obj, "Shot Decide  Pause CharacterChange  User1 Replay  Cansel_Hold Quit");
		ObjText_SetFontSize(obj, 12);
		ObjText_SetFontBold(obj, true);
//		ObjText_SetFontColorTop(obj, 128, 128, 255);
//		ObjText_SetFontColorBottom(obj, 64, 64, 255);
		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=0;
		while(!bEndTitleScene)
		{
			ObjRender_SetAlpha(obj,200+55*cos(AlphaAngle));
			AlphaAngle+=2;
			yield;
		}
		Obj_Delete(obj);
	}
}


//----------------------------------------------------
// 僗僥乕僕僔乕儞
//----------------------------------------------------
task TStageScene(let pathReplay)
{
	let dirCurrent = GetCurrentScriptDirectory();
	let dirModule = GetModuleDirectory();
	let pathMainScript = dirCurrent ~ "SpellCard/Stage.txt";
//	let pathPlayer = dirCurrent ~ "player/Marisa/Marisa.txt";
	let pathPlayer = dirCurrent ~ "player/Reimu/Reimu.txt";
	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);
	}

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

	//SetCommonData("SpellCardNo",1);

	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);
	if(GetCommonData("SpellCardID",0)<=20)
	{
	ObjSound_Load(StageMusicObject,GetCurrentScriptDirectory~"bgm/bgm1.ogg");
	}
	else if(GetCommonData("SpellCardID",0)<=41)
	{
	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;
	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,100*CurrentVulume);
			}
			else
			{
				ObjSound_SetVolumeRate(StageMusicObject,0);
			}
		}
		if(GetCommonData("BossDefeat",false))
		{
			loop(240)
			{
				CurrentVulume-=1/240;
				ObjSound_SetVolumeRate(StageMusicObject,100*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 = dirModule ~ "script/default_system/Default_EndScene.txt";
	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 = dirModule ~ "script/default_system/Default_EndScene.txt";
	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;
}


//----------------------------------------------------
// 夋柺慗堏僄僼僃僋僩
//----------------------------------------------------
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");
	}
}