#TouhouDanmakufu[Package]
#Title["Shinki"]
#Player["./Player/pl0/player.txt"]

#include "./system/functions/functions_general.txt"
#include "./system/functions/functions_package_sound.txt"

@Initialize
{
	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(VK_FLASH,KEY_X,KEY_INVALID);
	AddVirtualKey(VK_SKIP,KEY_LCONTROL,KEY_INVALID);
	
	LoadCommonDataAreaA2("CONFIG",pathConfig);
	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",pathConfig);
		}
	InitializeSound;
	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;
}

task TTitleScene
{
	TBackground;
	return;
	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);
}
function TBackground
{
	loop(30){yield;}
	TStageScene("");
}
task TStageScene(replayIndexS)
{
	loop(5){yield;}
	let pathPlayer = GetCurrentScriptDirectory ~ "Player/pl0/player.txt";
	let stageStart = 0;
	let stageEnd = 1;

	let pathStage = GetCurrentScriptDirectory ~ "Stage/";

	RenderSceneToTransitionTexture();
	TTransition();

	InitializeStageScene();

	if(replayIndexS != "")
		{
		LoadReplayList;
		let i = StringToInt(replayIndexS);
		let replay = GetValidReplayIndices[i-1];

		let stage = GetReplayInfo(i,REPLAY_STAGE_INDEX_LIST);
		let score = GetReplayInfo(i,REPLAY_STAGE_START_SCORE_LIST);
			
		stageStart = stage[0]-1;
		stageEnd = stage[length(stage)-1];

		SetStageReplayFile(GetReplayInfo(i,REPLAY_FILE_PATH));
		}
	else
		{
		stageStart = 0;
		stageEnd = 1;
		}

	ascent(i in stageStart..stageEnd)
		{
		i+=1;
		SetStagePlayerScript(pathPlayer);
		SetStageMainScript(pathStage ~ "Stage" ~ IntToString(1) ~ "/Stage.txt");
		SetStageIndex(i);
		StartStageScene();
		let val = StageRunning(i);
		if(val == "end")
			{
			break;
			}	
		}
	
	function StageRunning(Index)
		{
		loop
			{
			if(GetVirtualKeyState(VK_PAUSE) == KEY_PUSH)
				{
				let resPause = RunPauseScene();
				alternative(resPause)
				case("RESTART")
					{
					ClearInvalidRenderPriority;
					TMusicCurrentStop;
					TerminateStageScene();
					TStageScene(replayIndexS);
					break;
					}
				case("TITLE")
					{
					ClearInvalidRenderPriority;
					TerminateStageScene();
					TMusicCurrentStop;
					TTitleScene();
					break;
					}
				case("CLOSE")
					{
					ClearInvalidRenderPriority;
					TMusicCurrentStop;
					ClosePackage;
					}
				}
			if(!IsReplay() && GetKeyState(KEY_BACK) == KEY_PUSH)
				{
				TerminateStageScene();
				TStageScene("");
				break;
				}
			if(GetStageSceneState == STAGE_STATE_FINISHED)
				{
				EndCurrentMusic;
				if(Index == stageEnd)
					{
					TEndScene();
					}
				return "";
				}
			yield;
			}	
		TTransition();
		return "end";
		}
}
task TEndScene()
{
	if(IsReplay())
		{
		TTitleScene();
		return;
		}

	TerminateStageScene;
	FinalizeStageScene();

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

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

	let result = GetScriptResult(idScript);
	alternative(result)
	case("RESTART")
		{
		TerminateStageScene();
		yield;
		SetCommonData("CURRENT_SPELL",GetCommonData("INIT_SPELL",0));
		TStageScene("");
		}
	case("TITLE")
		{
		TerminateStageScene();
		yield;
		TTitleScene();
		}
	case("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);
}
	