#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#ID["Main"]
#Title["Toyasatomimi no Miko REMASTERED"]
#Text["knock knock [r] get the door [r] it's religion"]
#System["./system/system/Awalys_System.txt"]
#Background["./system/background/back_miko.dnh"]
#Player["./system/player/Byakuren/Byakuren_A.txt"]

#include"./system/shot/CrispBright4/Shotconstants.dnh"
#include"./system/lib/lib_main.dnh"
#include"./system/lib/Boss_Anim.dnh"
#include"./system/lib/dialogue.dnh"
#include"./system/lib/miko_aura.dnh"

// Huge kudos to Sparen, his code taught me a whole lot.

let dir = GetCurrentScriptDirectory();
let end = false;
let bgm = ObjSound_Create;
let bgm2 = ObjSound_Create;

@Event{
	alternative(GetEventType)
	case(EV_USER_STAGE) {
		let arg = GetEventArgument(0); // X and Y coordinates.
		CreateItemA1(ITEM_SPELL, arg[0], arg[1], 1500);
	}
	case(EV_USER_STAGE+1) {
		PlaySE(ExpSE3);
	}
	case(EV_USER_STAGE+2) {
		PlaySE(SpellGetSE);
	}
	case(EV_USER_STAGE+3) {
		ObjSound_Play(bgm);
	}
	case(EV_USER_STAGE+4) {
		let arg = GetEventArgument(0); // Spell score.
    	AddScore(arg);
	}
	case(EV_PAUSE_ENTER){
		ObjSound_SetVolumeRate(bgm, 40);
	}
    case(EV_PAUSE_LEAVE){
		ObjSound_SetVolumeRate(bgm, 100);
	}
}

@Initialize{
	TStage;
	
	SetCommonData("Main", GetOwnScriptID());
	SetPlayerLife(6);
}

@MainLoop{
	if(GetPlayerState() == STATE_END){
		FinalizeStage;
	}
	yield;
}

task TStage{
	let pluralboss = dir ~ "./Plural1.dnh";
	let boss = LoadScriptInThread(pluralboss);
	
	wait(60);
	StartScript(boss);
	
	while(!IsCloseScript(boss)){yield;}

	STGClearScore(1000000000);

	FinalizeStage;
}

task FinalizeStage{
	wait(180);
	StopSound(BGM);
	if(GetScore > GetAreaCommonData("HighScore", "Score", 0)){ // Saves the high score.
		SetAreaCommonData("HighScore", "Score", GetScore);
		SaveCommonDataAreaA1("HighScore");
	}
	CloseStgScene();
}