#TouhouDanmakufu[Package]
#Title["Popular Popularity"]
#Text["Reisen Udonge Inaba"]
#Player["./Player/pl0/player.txt"]

#include "./Menu/MenuResult.txt"
#include "./system/Sound.txt"

@Initialize
{
	SetSkipModeKey(KEY_INVALID);
	AddVirtualKey(KEY_BACK,KEY_INVALID,KEY_INVALID);
	AddVirtualKey(VK_USER1,KEY_LCONTROL,KEY_INVALID);
	InstallFont(GetCurrentScriptDirectory ~ "./system/img/GOTHIC.ttf");
	InstallFont(GetCurrentScriptDirectory ~ "./system/img/GOTHICB.ttf");
	InstallFont(GetCurrentScriptDirectory ~ "./system/img/GOTHICBI.ttf");
	InstallFont(GetCurrentScriptDirectory ~ "./system/img/GOTHICI.ttf");

	LoadCommonDataAreaA2("CONFIG",GetCurrentScriptDirectory ~ "system/data/CONFIG");
	if(!IsCommonDataAreaExists("CONFIG"))
		{
		CreateCommonDataArea("CONFIG");
		SetAreaCommonData("CONFIG", "MUSIC_VOLUME", 1);
		SetAreaCommonData("CONFIG", "SOUND_VOLUME", 0.85);
		SetAreaCommonData("CONFIG", "EFFECT", 0);
		SetAreaCommonData("CONFIG", "TEXT_ENABLED",true);
		SetAreaCommonData("CONFIG", "CHEAT_ENABLED", false);
		SaveCommonDataAreaA2("CONFIG",GetCurrentScriptDirectory ~ "system/data/CONFIG");
		}
	TInitializeSpellName;
	TTitleScene;
}

@MainLoop
{
	yield;
}

@Finalize
{
}

task TTitleScene
{
	SetCommonData("PRACTICE",false);
	SetCommonData("REI_SPELL",false);
	SetCommonData("INIT_SPELL",0);
	SetCommonData("CURRENT_SPELL",GetCommonData("INIT_SPELL",0));
	SetCommonData("MIDBOSS",false);

	DeleteCommonData("PLAYER_LIFE");
	DeleteCommonData("PLAYER_BOMB");
	DeleteCommonData("PLAYER_FLASH");
	DeleteCommonData("BACKGROUND_END");

	SetCommonData("EFFECT",GetAreaCommonData("CONFIG","EFFECT",0));
	SetCommonData("TEXT_ENABLED",GetAreaCommonData("CONFIG","TEXT_ENABLED",true));
	SetCommonData("CHEAT_ENABLED",GetAreaCommonData("CONFIG","CHEAT_ENABLED",false));
	let flash = GetAreaCommonData("CONFIG","FLASH_KEY",false);
	SetCommonData("FLASH_KEY",flash);

	if(flash == true)
		{
		AddVirtualKey(VK_USER2,KEY_C,KEY_INVALID);
		}	
	else
		{
		AddVirtualKey(VK_USER2,KEY_X,KEY_INVALID);
		}
	

	let Title = LoadScript(GetCurrentScriptDirectory ~ "Menu/Title.txt");
	StartScript(Title);
	while(!IsCloseScript(Title)){yield;}
	let array = GetScriptResult(Title);
	let mainSelect = array[0];
	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/StageM.txt";
	let pathPlayer = GetCurrentScriptDirectory ~ "Player/pl0/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)
				{
				if(!IsReplay())
					{
					SetCommonData("CURRENT_SPELL",GetCommonData("INIT_SPELL",0));
					DeleteCommonData("PLAYER_LIFE");
					DeleteCommonData("PLAYER_BOMB");
					TerminateStageScene();
					TStageScene("");
					return;
					}
				}
			case(RESULT_TITLE)
				{
				TerminateStageScene();
				TTitleScene();
				break;
				}
			case(RESULT_CLOSE)
				{
				ClosePackage;
				}
			}
		if(!IsReplay() && GetKeyState(KEY_BACK) == KEY_PUSH)
			{
			TerminateStageScene();
			TStageScene("");
			return;
			}
		if(GetStageSceneState == STAGE_STATE_FINISHED)
			{
			TerminateStageScene();
			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)
		{
		SetCommonData("CURRENT_SPELL",GetCommonData("INIT_SPELL",0));
		TStageScene("");
		}
	case(RESULT_TITLE)
		{
		TTitleScene();
		}
	case(RESULT_CLOSE)
		{
		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 TInitializeSpellName
{
	let spellName = [];
	ascent(t in 0..10)
		{
		ascent(i in 0..3)
			{
			let name = [];
			alternative(t)
				case(0)
					{
					name = ["Tewi 1"];
					}
				case(1)
					{
					name = ["Rabbit Sign \"Two Rabbits Chasing Youkai\""];
					}
				case(2)
					{
					name = ["Reisen 1"];
					}
				case(3)
					{
					name = ["Red Eyes \"Eyesight Clearing \(Lunatic Stare\)\""];
					}
				case(4)
					{
					name = ["Reisen 2"];
					}
				case(5)
					{
					name = ["Illusion Sign \"Violet Eye \(Shadow Cloning\)\""];
					}
				case(6)
					{
					name = ["Reisen 3"];
					}
				case(7)
					{
					name = ["Rabbit Sign \"Mind Break \(Inaba Shooter\)\""];
					}
				case(8)
					{
					name = ["\"Red Moon Delusion \(\Hyper Red Moon)\""];
					}
			if(t == 7 && i == 0)
				{
				name = ["Rabbit Sign \"Mind Bend \(Ravi Training\)\""];
				}
			if(t == 7 && i == 2)
				{
				name = ["Rabbit Shot \"Mind Bend \(Earth Inaba Training\)\""];
				}
			spellName = spellName ~ name;
			}
		}
	SetCommonData("SPELL_NAMES",spellName);
	LoadCommonDataAreaA2("SCORE",GetCurrentScriptDirectory ~ "system/data/SCORE");
	if(!IsCommonDataAreaExists("SCORE"))
		{
		CreateCommonDataArea("SCORE");
		let unlock = [];
		ascent(i in 0..length(spellName))
			{
			unlock = unlock ~ [true];
			}
		SetAreaCommonData("SCORE", "SPELL_UNLOCK",unlock);
		SaveCommonDataAreaA2("SCORE",GetCurrentScriptDirectory ~ "system/data/SCORE");
		}
}