#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#Title["Stage"]
#System["./../system/Default_System.txt"]

#include "./../system/SpellName.txt"
#include "./functions/functions_stage.txt"
#include "./../system/music.txt"

@Initialize
{
	TStage();
}

@MainLoop
{
	yield;
}

@Event
{

}

@Finalize
{
}

task TStage
{
	if(IsReplay == true)
		{
		LoadCommonDataAreaFromReplayFile("REPLAY");
		let type = GetAreaCommonData("REPLAY","STAGE_TYPE","TYPE_MAIN");
		SetCommonData("STAGE_TYPE",type);
		if(type == "TYPE_SPELL_PRACTICE")
			{
			SetCommonData("CURRENT_SPELL",GetAreaCommonData("REPLAY","CURRENT_SPELL",0));
			}
		if(type == "TYPE_MAIN")
			{
			SetCommonData("STAGE",1);
			}
		else
			{
			SetCommonData("STAGE",GetAreaCommonData("REPLAY","STAGE",1));
			}
		SetCommonData("TEXT_ENABLED",GetAreaCommonData("REPLAY","TEXT_ENABLED",true));
		SetCommonData("CHEAT_ENABLED",GetAreaCommonData("REPLAY","CHEAT_ENABLED",true));
		SetCommonData("FLASH_KEY",GetAreaCommonData("REPLAY","FLASH_KEY",true));
		SetCommonData("DIFFICULTY",GetAreaCommonData("REPLAY","DIFFICULTY",0));	
		}
	else
		{	
		if(!IsCommonDataAreaExists("REPALY"))
			{
			CreateCommonDataArea("REPLAY");
			}
		let type = GetCommonData("STAGE_TYPE","TYPE_MAIN");
		let stage = GetCommonData("STAGE",1);
		let spell = GetCommonData("CURRENT_SPELL",0);
		let text = GetCommonData("TEXT_ENABLED",true);
		let cheat = GetCommonData("CHEAT_ENABLED",false);
		let flash = GetCommonData("FLASH_KEY",false);	
		let Dif = GetCommonData("DIFFICULTY",0);
		let pl1 = GetCommonData("PLAYER_1",0);	
		let pl2 = GetCommonData("PLAYER_2",0);	
		let pl3 = GetCommonData("PLAYER_3",0);	

		SetAreaCommonData("REPLAY","STAGE_TYPE",type);
		SetAreaCommonData("REPLAY","STAGE",stage);
		SetAreaCommonData("REPLAY","CURRENT_SPELL",spell);
		SetAreaCommonData("REPLAY","TEXT_ENABLED",text);
		SetAreaCommonData("REPLAY","CHEAT_ENABLED",cheat);
		SetAreaCommonData("REPLAY","FLASH_KEY",flash);
		SetAreaCommonData("REPLAY","DIFFICULTY",Dif);
		SetAreaCommonData("REPLAY","PLAYER_1",pl1);
		SetAreaCommonData("REPLAY","PLAYER_2",pl2);
		SetAreaCommonData("REPLAY","PLAYER_3",pl3);
		
		SaveCommonDataAreaToReplayFile("REPLAY");
		}

	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);
}