#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",0);
let Stage = GetCommonData("STAGE",1);	
let Practice = GetCommonData("PRACTICE",false);
let mid = true;
let BossScene;
let MBossScene;
let PlayerHit = false;	

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

@MainLoop
{
	if(GetPlayerLife < 0)
		{	
		EndCurrentMusic;
		CloseStgScene;
		}
	yield;
}

@Event
{
	alternative(GetEventType())

		case(EV_PAUSE_ENTER)
			{
			PauseMusic;
			}
		case(EV_PAUSE_LEAVE)
			{
			ResumeMusic;
			}
	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);
					}
				}
			}
		}

}


task TMain
{
	let EffectPath = LoadScript(GetCurrentScriptDirectory ~ "../../system/package/package_effect.txt");
	StartScript(EffectPath);

	let texture = [];
	mid = true;
	if(Practice == true && floor(GetCommonData("CURRENT_SPELL",0)/3) >= 2)
		{
		mid = false;
		}
	if(Practice == true && floor(GetCommonData("CURRENT_SPELL",0)/3) >= 2)
		{
		SetCommonData("BACKGROUND_COLD",true);
		}
	else
		{
		SetCommonData("BACKGROUND_COLD",false);
		}
	TBackground;

	loop(1)
		{
		texture = texture ~ [LoadTexture(SpritePathBoss)];
		texture = texture ~ [LoadTexture(SpritePathMidboss)];
		texture = texture ~ [LoadTexture(Nue_Face)];
		texture = texture ~ [LoadTexture(Hecatia_Face)];
		texture = texture ~ [LoadTexture(GetCurrentScriptDirectory ~ "../../system/package/img/Aura1.png")];
		texture = texture ~ [LoadTexture(GetCurrentScriptDirectory ~ "../../system/package/img/Aura2.png")];
		texture = texture ~ [LoadTexture(GetCurrentScriptDirectory ~ "../../system/package/img/Aura3.png")];
		texture = texture ~ [LoadTexture(GetCurrentScriptDirectory ~ "img/Background/Back_Lite.png")];
		texture = texture ~ [LoadTexture(GetCurrentScriptDirectory ~ "img/Background/Back_Lite2.png")];
		texture = texture ~ [LoadTexture(GetCurrentScriptDirectory ~ "img/Background/Back_Cloud.png")];
		texture = texture ~ [LoadTexture(GetCurrentScriptDirectory ~ "img/Background/Back_Snow.png")];
		texture = texture ~ [LoadTexture(GetCurrentScriptDirectory ~ "img/Background/Back_Flower.png")];
		texture = texture ~ [LoadTexture(GetCurrentScriptDirectory ~ "img/Background/Back_Flower2.png")];
		texture = texture ~ [LoadTexture(GetCurrentScriptDirectory ~ "Boss/img/Back_Main.png")];
		texture = texture ~ [LoadTexture(GetCurrentScriptDirectory ~ "Boss/img/Back_Snow.png")];
		texture = texture ~ [LoadTexture(GetCurrentScriptDirectory ~ "boss/img/Effect_Cloud.png")];
		LoadBoss;
		}
	loop(60){yield;}
	SetCommonData("CURRENT_SPELL",GetCommonData("CURRENT_SPELL",0)+6);
	mid = false;
	if(Practice == false || mid == true)
		{
		PlayMusic(10);
		TSongName("Diva's Device");
		}
	if(Practice == false)
		{
		TStage;
		}
	StartBoss;
	SetCommonData("PLAYER_LIFE",GetPlayerHealth);
	SetCommonData("PLAYER_BOMB",GetPlayerSpell);
	SetCommonData("PLAYER_FLASH",GetPlayerFlash);
	ascent(i in 0..length(texture))
		{
		RemoveTexture(texture[i]);
		}
	CloseScript(EffectPath);
	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 ~ "Base4.txt");
		ObjEnemyBossScene_Add(BossScene, 3, dir ~ "Spell4.txt");
		ObjEnemyBossScene_Add(BossScene, 4, dir ~ "Spell5.txt");
		ObjEnemyBossScene_Add(BossScene, 4, dir ~ "End.txt");
		ObjEnemyBossScene_LoadInThread(BossScene);		
		}
	else
		{
		BossScene = ObjEnemyBossScene_Create();
		let numb = GetCommonData("CURRENT_SPELL",0)+6;
		ObjEnemyBossScene_Add(BossScene, 0, dir ~ "PracticeInit.txt");
		alternative(floor(numb/3))
			case(2)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Base1.txt");
				}
			case(3)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Spell1.txt");
				}
			case(4)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Base2.txt");
				}
			case(5)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Spell2.txt");
				}
			case(6)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Base3.txt");
				}
			case(7)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Spell3.txt");
				}
			case(8)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Base4.txt");
				}
			case(9)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Spell4.txt");
				}
			case(10)	
				{
				ObjEnemyBossScene_Add(BossScene, 0, dir ~ "Spell5.txt");
				}
		ObjEnemyBossScene_Add(BossScene, 0, dir ~ "PracticeEnd.txt");
		ObjEnemyBossScene_LoadInThread(BossScene);
		}
	}
function StartBoss
	{
	let scene = BossScene;
	if(mid == true && Practice == false)
		{
		scene = MBossScene;
		}
	let back;
	ObjEnemyBossScene_Regist(scene);
	if(mid == false)
		{
		TBossName("Letty Whiterock");
		}
	else
		{
		TBossName("Aki Sisters");
		}
	if(mid == false)
		{
		if(Practice == false)
			{
			while(Obj_GetValueD(GetPlayerObjectID,"MUSIC",true) == true){yield;}
			while(Obj_GetValueD(GetPlayerObjectID,"MUSIC",false) == false)
				{
				EndMusic(10);
				yield;
				}
			}
		EndMusic(10);
		PlayMusic(11);
		if(Practice == false)
			{
			while(Obj_GetValueD(GetPlayerObjectID,"MUSIC",true) == true){yield;}
			}
		TSongName("Snow Recollection");
		}
	while(!Obj_IsDeleted(scene)){yield;}
	if((mid == false && Practice == false) || Practice == true)	
		{
		SetCommonData("BACKGROUND_END",2);
		SetForbidPlayerShot(true);
		SetForbidPlayerSpell(true);	
		FadeCurrentMusic(150);
		loop(150){yield;}
		SetCommonData("PLAYER_WIN",true);
		}
	Obj_Delete(scene);
	mid = false;
	return;
	}

task TBackground
{

	let current = GetCurrentScriptDirectory ~ "img/Background/";

	SetCameraFocusX(0);
	SetCameraFocusY(30);
	SetCameraFocusZ(-100);
	SetCameraRadius(300);
	SetCameraElevationAngle(5);
	SetCameraAzimuthAngle(90);

	SetFogParam(0, 1000, 32, 32, 32);
	if(Effect > 3)
		{
		TBlank;
		}
	else 
		{
		TNormalBackground;
		}
	task TBlank
		{
		let alphaM = 1;
		let back = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(back,current ~ "Back_Normal.png");	
		let switch = false;
		ObjSprite2D_SetSourceRect(back,1,1,385,446);
		Obj_SetRenderPriority(back,0.2);
		ObjSprite2D_SetDestRect(back,GetStgFrameWidth/2-320,GetStgFrameHeight/2-240,GetStgFrameWidth/2+320,GetStgFrameHeight/2+240);
		loop
			{
			if(GetCommonData("BACKGROUND_END",0) == 2)
				{
				alphaM-=1/135;
				}
			if(GetCommonData("BACKGROUND_COLD",false) == true && switch == false)
				{		
				ObjPrim_SetTexture(back,current ~ "Back_Normal2.png");
				switch = true;
				}
			ObjRender_SetAlpha(back,255*alphaM);
			yield;
			}
		}
	task TNormalBackground
		{
		let alphaM = 1;
		let back = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(back,current ~ "Back_Normal.png");
		ObjSprite2D_SetSourceRect(back,1,1,385,446);
		Obj_SetRenderPriority(back,0.2);
		ObjSprite2D_SetDestRect(back,GetStgFrameWidth/2-320,GetStgFrameHeight/2-240,GetStgFrameWidth/2+320,GetStgFrameHeight/2+240);
		let switch = false;
		loop
			{
			if(GetCommonData("BACKGROUND_END",0) == 2)
				{
				alphaM-=1/135;
				}
			if(GetCommonData("BACKGROUND_COLD",false) == true && switch == false)
				{		
				ObjPrim_SetTexture(back,current ~ "Back_Normal2.png");
				switch = true;
				}
			ObjRender_SetAlpha(back,255*alphaM);
			yield;
			}
		}
}