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

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

@Initialize
{
	TStage();
}

@MainLoop
{
	yield;
}

@Event
{

}

@Finalize
{
}

function LoadStuff
{
	let dir = GetCurrentScriptDirectory ~ "functions/img/";
	LoadTexture(dir~"fairy_black.png");
	LoadTexture(dir~"fairy_blue.png");
	LoadTexture(dir~"fairy_green.png");
	LoadTexture(dir~"fairy_red.png");
	LoadTexture(dir~"fairy_white.png");
	LoadTexture(dir~"fairy_yellow.png");
	LoadTexture(dir~"flame.png");
	LoadTexture(dir~"orb.png");

	dir = GetCurrentScriptDirectory ~ "enemies/events/";
	LoadTexture(dir~"Frog.png");
	LoadTexture(dir~"Snake.png");
	ascent(i in 1..10+1)
		{
		LoadTexture(dir~"Division" ~ IntToString(i) ~ ".png");
		LoadTexture(dir~"Rank" ~ IntToString(i) ~ ".png");
		}
	ascent(i in 1..22+1)
		{
		LoadTexture(dir~"event_" ~ IntToString(i) ~ ".png");
		}
}

task TStage
{
	LoadStuff;
	let EffectPath = LoadScript(GetCurrentScriptDirectory ~ "../system/package/package_effect.txt");
	StartScript(EffectPath);

	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",1));
		SetCommonData("CHEAT_ENABLED",GetAreaCommonData("REPLAY","CHEAT_ENABLED",0));
		SetCommonData("FLASH_KEY",GetAreaCommonData("REPLAY","FLASH_KEY",1));
		SetCommonData("DIFFICULTY",GetAreaCommonData("REPLAY","DIFFICULTY",0));	
		}
	else
		{	
		if(!IsCommonDataAreaExists("REPALY"))
			{
			CreateCommonDataArea("REPLAY");
			}
		let type = GetCommonData("STAGE_TYPE","TYPE_MAIN");
		let stage = GetWorldX+1;
		let spell = GetCommonData("CURRENT_SPELL",0);
		let text = GetCommonData("TEXT_ENABLED",1);
		let cheat = GetCommonData("CHEAT_ENABLED",0);
		let flash = GetCommonData("FLASH_KEY",0);	
		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");

		SetWorldX(0);
		SetWorldY(0);
		}

	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;
			}
	TPOC;
	
	loop(numb)
		{
		SetStageX(0);
		SetStageY(0);

		let path = LoadScript(GetCurrentScriptDirectory ~ "Stage.txt");
		StartScript(path);
		while(!IsCloseScript(path)){yield;}

		if(GetPlayerLife >=0 && GetWorldY < 5 && GetCommonData("STAGE_TYPE","") == "TYPE_MAIN")
			{
			let width = -1+(2+(GetWorldY+1)*3)*2;
			let dir = 0;
			if(GetStageX < trunc(-width/6))
				{
				dir = -1;
				}
			else if(GetStageX > trunc(width/6))
				{
				dir = 1;
				}
			AddWorldX(dir);
			AddWorldY(1);
			}
		else
			{
			break;
			}

		}
	CloseScript(EffectPath);
	CloseStgScene;
	CloseScript(GetOwnScriptID);
}