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

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

let Dif = GetCommonData("DIFFICULTY",1);
let Stage = GetCommonData("STAGE",1);
let Practice = GetCommonData("PRACTICE",false);
let mid = false;
let BossScene;
let MBossScene;
let Background = [];
let PlayerHit = false;	

@Initialize
{
	if(GetCommonData("CURRENT_SPELL",0) < 0)
		{
		mid = true;
		}
	SetCommonData("MIDBOSS",mid);
	DeleteCommonData("BACKGROUND_END");
	if(GetCommonData("STAGE_TYPE","") == "TYPE_SPELL_PRACTICE")
		{
		Practice = true;	
		}
	TMain;
}

@MainLoop
{
	if(GetPlayerLife < 0)
		{	
		ObjSound_Stop(musicObj);
		Obj_Delete(musicObj);
		CloseStgScene;
		}
	yield;
}

@Event
{
	alternative(GetEventType())

	case(EV_PAUSE_ENTER)
		{
		ObjSound_Stop(musicObj);
		}
	case(EV_PAUSE_LEAVE)
		{
		ObjSound_Play(musicObj);
		}
	case(EV_PLAYER_SHOOTDOWN)
		{
		PlayerHit = true;
		}
	case(EV_START_BOSS_STEP)
		{
		PlayerHit = false;
		}
	case(EV_END_BOSS_STEP)
		{
		let scene = GetEnemyBossSceneObjectID;
		if(scene != ID_INVALID && Obj_GetValueD(scene,"INIT",false) == false && Practice == false)
			{	
			let x = Obj_GetValueD(scene,"ENMX",GetStgFrameWidth/2);
			let y = Obj_GetValueD(scene,"ENMY",GetStgFrameHeight/2-110);
			if(PlayerHit == false)
				{
				if(ObjEnemyBossScene_GetInfo(scene,INFO_IS_SPELL) == true)
					{
					CreateItem1(x,y,TYPE_LIFE);
					}
				else
					{
					CreateItem1(x,y,TYPE_SPELL);
					}
				}
			ascent(i in 0..50)
				{
				let angle = (i+rand(0,1))*360/50;
				let dist = rand(10,50);
				CreateItem1(x+dist*cos(angle),y+dist*sin(angle),TYPE_POINT);
				}
			}
		}

}


task TMain
{
	let path = GetCurrentScriptDirectory ~ "Background/";
	let Background = LoadScriptInThread(path ~ "Background.txt");
	StartScript(Background);

	let EffectPath = LoadScriptInThread(GetCurrentScriptDirectory ~ "../../system/package/package_effect.txt");
	StartScript(EffectPath);

	let texture = [];
	loop(1)
		{
		texture = texture ~ [LoadTexture(SpritePathBoss)];
		texture = texture ~ [LoadTexture(SpritePathMidboss)];
		texture = texture ~ [LoadTexture(Nue_Face)];
		texture = texture ~ [LoadTexture(Hecatia_Face)];
		texture = texture ~ [LoadTexture(GetCurrentScriptDirectory ~ "background/img/Back_Normal.png")];
		texture = texture ~ [LoadTexture(GetCurrentScriptDirectory ~ "boss/img/Back_Plaid.png")];
		texture = texture ~ [LoadTexture(GetCurrentScriptDirectory ~ "boss/img/Back_Plaid2.png")];
		LoadBoss;
		}
	StartBoss;
	SetCommonData("PLAYER_LIFE",GetPlayerHealth);
	SetCommonData("PLAYER_BOMB",GetPlayerSpell);
	SetCommonData("PLAYER_FLASH",GetPlayerFlash);
	ascent(i in 0..length(texture))
		{
		RemoveTexture(texture[i]);
		}
	CloseScript(EffectPath);
	CloseScript(Background);
	CloseStgScene;
	CloseScript(GetOwnScriptID);
}

function LoadBoss
	{
	let dir = GetCurrentScriptDirectory ~ "boss/";
	if(Practice == false)
		{
		MBossScene = ObjEnemyBossScene_Create();
		ObjEnemyBossScene_Add(MBossScene, 0, dir ~ "Init.txt");
		ObjEnemyBossScene_Add(MBossScene, 0, dir ~ "PracticeEnd.txt");
		ObjEnemyBossScene_LoadInThread(MBossScene);
	
		BossScene = ObjEnemyBossScene_Create();
		ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Init.txt");
		ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Base1.txt");
		ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Spell1.txt");
		ObjEnemyBossScene_Add(BossScene, 1, dir ~ "Base2.txt");
		ObjEnemyBossScene_Add(BossScene, 1, dir ~ "Spell2.txt");
		ObjEnemyBossScene_Add(BossScene, 2, dir ~ "Base3.txt");
		ObjEnemyBossScene_Add(BossScene, 2, dir ~ "Spell3.txt");
		ObjEnemyBossScene_Add(BossScene, 3, dir ~ "Med.txt");
		ObjEnemyBossScene_Add(BossScene, 3, dir ~ "Spell4.txt");
		ObjEnemyBossScene_Add(BossScene, 3, dir ~ "End.txt");
		ObjEnemyBossScene_LoadInThread(BossScene);		
		}
	else
		{
		BossScene = ObjEnemyBossScene_Create();
		let numb = GetCommonData("CURRENT_SPELL",0);
		ObjEnemyBossScene_Add(BossScene, 0, dir ~ "PracticeInit.txt");
		alternative(floor(numb/3))
			case(0)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Base1.txt");
				}
			case(1)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Spell1.txt");
				}
			case(2)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Base2.txt");
				}
			case(3)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Spell2.txt");
				}
			case(4)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Base3.txt");
				}
			case(5)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Spell3.txt");
				}
			case(6)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Spell4.txt");
				}
		ObjEnemyBossScene_Add(BossScene, 0, dir ~ "PracticeEnd.txt");
		ObjEnemyBossScene_LoadInThread(BossScene);
		}
	}
function StartBoss
	{
	let scene = BossScene;
	let back;
	ObjEnemyBossScene_Regist(scene);
	if(mid == false)
		{
		TBossName("Sagume Kishin");
		}
	else
		{
		TBossName("Clownpiece");
		if(Practice == false)
			{
			scene = MBossScene;
			}
		}
	if(mid == false)
		{
		if(Practice == false)
			{
			while(Obj_GetValueD(GetPlayerObjectID,"MUSIC",true) == true){yield;}
			while(Obj_GetValueD(GetPlayerObjectID,"MUSIC",false) == false)
				{
				ObjSound_Stop(musicObj);
				yield;
				}
			}
		ObjSound_Stop(musicObj);
		PlayMusic(GetCurrentScriptDirectory ~ "bgm_Boss",3.1,115.4);
		TSongName("Desperate Situation ~ Inevitable Checkmate");
		}
	while(!Obj_IsDeleted(scene)){yield;}
	if((mid == false && Practice == false) || Practice == true)	
		{
		SetCommonData("BACKGROUND_END",2);
		SetForbidPlayerShot(true);
		SetForbidPlayerSpell(true);
		let vol = ObjSound_GetVolumeRate(musicObj);
		descent(i in -1..149)
			{
			ObjSound_SetVolumeRate(musicObj,i*vol/150);
			yield;
			}
		SetCommonData("PLAYER_WIN",true);
		ObjSound_Stop(musicObj);
		Obj_Delete(musicObj);
		}
	Obj_Delete(scene);
	}
