#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#Title["Stage"]
#System["./../system/Default_System.txt"]
#include "./../system/SpellName.txt"
#include "./functions/functions_stage.txt"

@Initialize
{
	TStage();
}

@MainLoop
{
	yield;
}

@Event
{

}

@Finalize
{
}

task TStage
{
	let stage = GetCommonData("STAGE",1);
	let type = GetCommonData("STAGE_TYPE","");
	let numb = 0;
	alternative(type)
		case("TYPE_MAIN")
			{
			numb = 1;
			}
		case("TYPE_EXTRA","TYPE_SPELL_PRACTICE","TYPE_STAGE_PRACTICE")
			{
			numb = 1;
			}
	let unlock = GetAreaCommonData("STAGE_UNLOCK","STAGE_UNLOCK",[]);
	TPOC;
	loop(numb)
		{
		unlock[stage-1] = true;
		SetAreaCommonData("STAGE_UNLOCK","STAGE_UNLOCK",unlock);
		SaveCommonDataAreaA2("STAGE_UNLOCK",GetCurrentScriptDirectory ~ "../system/data/SCORE");
		let Plural = LoadScript(GetCurrentScriptDirectory ~ "Stage" ~ IntToString(stage) ~ "/Stage.txt");
		TInitializeSpellName(stage);
		StartScript(Plural);
		while(!IsCloseScript(Plural)){yield;}
		if(GetPlayerLife >=0 && stage < 6 && GetCommonData("STAGE_TYPE","") == "TYPE_MAIN")
			{
			stage+=1;
			SetCommonData("STAGE",stage);
			SetCommonData("INIT_SPELL",0);
			SetCommonData("CURRENT_SPELL",GetCommonData("INIT_SPELL",0));
			}
		else
			{
			break;
			}
		}
	CloseStgScene;
	CloseScript(GetOwnScriptID);
}