#TouhouDanmakufu[Package]
#Title["End of the Ordinary Witch"]
#Text["Totally not re-used content <_< >_>"]
#Player["./Player/pl0/player.txt"]

#include "./Menu/MenuResult.txt"
#include "./system/package/package_sound.txt"
#include "./system/package/functions_score.txt"

@Initialize
{
	pathScore = GetCurrentScriptDirectory ~ "system/data/SCORE";
	SetCommonData("PATHSCORE",pathScore);
	InstallFont(GetCurrentScriptDirectory ~ "./system/img/fonts/GOTHIC.ttf");
	InstallFont(GetCurrentScriptDirectory ~ "./system/img/fonts/GOTHICB.ttf");
	InstallFont(GetCurrentScriptDirectory ~ "./system/img/fonts/GOTHICBI.ttf");
	InstallFont(GetCurrentScriptDirectory ~ "./system/img/fonts/GOTHICI.ttf");
	InstallFont(GetCurrentScriptDirectory ~ "./system/img/fonts/ARLRDBD.ttf");
	InstallFont(GetCurrentScriptDirectory ~ "./system/img/fonts/Cirno.ttf");

	SetSkipModeKey(KEY_INVALID);
	AddVirtualKey(KEY_BACK,KEY_INVALID,KEY_INVALID);
	AddVirtualKey(VK_USER1,KEY_LCONTROL,KEY_INVALID);

	LoadCommonDataAreaA2("CONFIG",GetCurrentScriptDirectory ~ "./system/data/CONFIG");
	if(!IsCommonDataAreaExists("CONFIG"))
		{
		CreateCommonDataArea("CONFIG");
		SetAreaCommonData("CONFIG", "MUSIC_VOLUME", 100);
		SetAreaCommonData("CONFIG", "SOUND_VOLUME", 85);
		SetAreaCommonData("CONFIG", "BACKGROUND_TYPE", 2);
		SetAreaCommonData("CONFIG", "TEXT_ENABLED",0);
		SetAreaCommonData("CONFIG", "CHEAT_ENABLED", 0);
		SaveCommonDataAreaA2("CONFIG",GetCurrentScriptDirectory ~ "./system/data/CONFIG");
		}

	InitializeStageUnlock;
	InitializeSound;
	InitializeMusic;
	TTitleScene;
}

@Event
{
	alternative(GetEventType())
		case(EV_SOUND_PLAY)
			{
			TSound(GetEventArgument(0)[0],GetEventArgument(0)[1]);
			}
		case(EV_SOUND_STOP)
			{
			TSoundStop(GetEventArgument(0)[0]);
			}
		case(EV_MUSIC_PLAY)
			{
			TMusic(GetEventArgument(0)[0]);
			}
		case(EV_MUSIC_STOP)
			{
			TMusicStop(GetEventArgument(0)[0]);
			}
		case(EV_MUSIC_FADE)
			{
			TMusicFade(GetEventArgument(0)[0],GetEventArgument(0)[1]);
			}
		case(EV_MUSIC_VOL)
			{
			TMusicVol(GetEventArgument(0)[0],GetEventArgument(0)[1]);
			}
		case(EV_MUSIC_C_STOP)
			{
			TMusicCurrentStop;
			}
		case(EV_MUSIC_C_FADE)
			{
			TMusicCurrentFade(GetEventArgument(0)[0]);
			}
		case(EV_MUSIC_PAUSE)
			{
			TMusicPause;
			}
		case(EV_MUSIC_RESUME)
			{
			TMusicResume;
			}
}

@MainLoop
{
	let se = GetCommonData("EV_SOUND_PLAY",[]);
	if(length(se) > 0)
		{
		ascent(i in 0..length(se))
			{
			TSound(se[i],GetCommonData("EV_SOUND_VOL",[])[i]);
			}
		SetCommonData("EV_SOUND_PLAY",[]);
		SetCommonData("EV_SOUND_VOL",[]);
		}
	yield;
}

@Finalize
{
}

task TTitleScene
{
	DeleteCommonData("PLAYER_LIFE");
	DeleteCommonData("PLAYER_BOMB");
	DeleteCommonData("PLAYER_FLASH");
	DeleteCommonData("BACKGROUND_END");
	DeleteCommonData("STAGE_TYPE");
	SetCommonData("INIT_SPELL",0);
	SetCommonData("CURRENT_SPELL",GetCommonData("INIT_SPELL",0));

	SetCommonData("EFFECT",GetAreaCommonData("CONFIG","EFFECT",0));
	SetCommonData("TEXT_ENABLED",GetAreaCommonData("CONFIG","TEXT_ENABLED",1));
	SetCommonData("CHEAT_ENABLED",GetAreaCommonData("CONFIG","CHEAT_ENABLED",0));
	SetCommonData("BACKGROUND_TYPE",GetAreaCommonData("CONFIG","BACKGROUND_TYPE",2));

	SetCommonData("FLASH_KEY",0);
	AddVirtualKey(VK_USER2,KEY_X,KEY_INVALID);

	let Title = LoadScript(GetCurrentScriptDirectory ~ "Menu/Title.txt");
	StartScript(Title);
	while(!IsCloseScript(Title)){yield;}
	let array = GetScriptResult(Title);

	alternative(array[0])
		case("START")
			{
			TStageScene("");
			}
		case("REPLAY")
			{
			TStageScene(array[1]);
			}
		case("EXIT")
			{
			ClosePackage;
			}
	Obj_Delete(Title);
	
}


task TStageScene(let pathReplay)
{
	let pathMainScript = GetCurrentScriptDirectory ~ "./Stage/World.txt";
	let pathPlayer = GetCurrentScriptDirectory ~ "./Player/pl" ~ IntToString(GetCommonData("PLAYER_1",0)) ~ "/player.txt";

	RenderSceneToTransitionTexture();
	TTransition();

	InitializeStageScene();
	if(length(pathReplay) > 0)
		{
		SetStageReplayFile(pathReplay);
		}

	SetStageMainScript(pathMainScript);
	SetStagePlayerScript(pathPlayer);
	StartStageScene();

	loop
		{
		if(GetVirtualKeyState(VK_PAUSE) == KEY_PUSH)
			{
			let resPause = RunPauseScene();
			alternative(resPause)
			case(RESULT_RESTART)
				{
				ClearInvalidRenderPriority;
				TMusicCurrentStop;
				if(!IsReplay())
					{
					SetCommonData("CURRENT_SPELL",GetCommonData("INIT_SPELL",0));
					DeleteCommonData("PLAYER_LIFE");
					DeleteCommonData("PLAYER_BOMB");
					TerminateStageScene();
					TStageScene("");
					return;
					}
				}
			case(RESULT_TITLE)
				{
				ClearInvalidRenderPriority;
				TerminateStageScene();
				TMusicCurrentStop;
				TTitleScene();
				break;
				}
			case(RESULT_CLOSE)
				{
				ClearInvalidRenderPriority;
				TMusicCurrentStop;
				ClosePackage;
				}
			}
		if(!IsReplay() && GetKeyState(KEY_BACK) == KEY_PUSH)
			{
			TerminateStageScene();
			TStageScene("");
			return;
			}
		if(GetStageSceneState == STAGE_STATE_FINISHED)
			{
			TEndScene();
			break;
			}
		yield;
		}	
	TTransition();
}


task TEndScene()
{
	if(IsReplay())
		{
		TTitleScene();
		return;
		}

	FinalizeStageScene();

	let idScript = LoadScript(GetCurrentScriptDirectory ~ "Menu/EndScene.txt");
	StartScript(idScript);

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

	let result = GetScriptResult(idScript);
	alternative(result)
	case(RESULT_RESTART)
		{
		TerminateStageScene();
		yield;
		SetCommonData("CURRENT_SPELL",GetCommonData("INIT_SPELL",0));
		TStageScene("");
		}
	case(RESULT_TITLE)
		{
		TerminateStageScene();
		yield;
		TTitleScene();
		}
	case(RESULT_CLOSE)
		{
		TerminateStageScene();
		yield;
		ClosePackage;
		}
}

function RunPauseScene()
{
	RenderSceneToTransitionTexture();
	PauseStageScene(true);

	let idScript = LoadScript(GetCurrentScriptDirectory ~ "Menu/Pause.txt");
	StartScript(idScript);

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

	PauseStageScene(false);

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

function RenderSceneToTransitionTexture()
{
	let textureName = GetTransitionRenderTargetName();
	RenderToTextureA1(textureName, 0, 100, true);
}

task TTransition
{
	let objImage = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objImage, 100);
	ObjPrim_SetTexture(objImage, GetTransitionRenderTargetName());
	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);
}

task InitializeStageUnlock
{	LoadCommonDataAreaA2("SCORE",pathScore);
	if(!IsCommonDataAreaExists("SCORE"))
		{
		CreateCommonDataArea("SCORE");
		}
	SaveCommonDataAreaA2("SCORE",pathScore);		
}		