#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#Title["Ultra-Stage"]
#Background["./../Functions/background.txt"]
#System["./../System/Miyako_System.txt"]

#include"./../Functions/Enemy_Functions.txt"
#include"./../Functions/Effects/Effects.txt"
#include"./Script/LoadGraphics.txt"

@Initialize{
	SetAreaCommonData("LIFE","Number",0);
	SetAreaCommonData("SPELL","Number",0);
	SetAreaCommonData("COINS","Number",0);
	
	SetCommonData("Word",1);
	SetCommonData("Boss",0);
	SetCommonData("MiyakoBG",0);
	SetCommonData("soundScriptID",GetOwnScriptID);
	SetCommonData("Main Stage Script ID", GetOwnScriptID());
	
	SetPlayerClip(11,11,GetStgFrameWidth-11,GetStgFrameHeight-18);
	InstallFont(GetCurrentScriptDirectory ~ "./Fonts/Blogger Sans-Light.otf");
	StartItemScript(GetCurrentScriptDirectory ~ "./../Functions/item.txt");
	SetPlayerLife(2);
	TStage;}

@Event{
	alternative(GetEventType())
	case(EV_USER_STAGE){TNon9Effect;}
	
	case(EV_PAUSE_ENTER){
		StopMusic(GetAreaCommonData("MUSIC","Song",0));}
	case(EV_PAUSE_LEAVE){
		musicRestartEnable(GetAreaCommonData("MUSIC","Song",0),1);
		PlayMusic(GetAreaCommonData("MUSIC","Song",0));}
	if(GetEventType() == EV_USER+100) {
		let tempArgument = GetEventArgument(0);
		let isRestartEnable;
		if(tempArgument[1] == 0) { isRestartEnable = false; } 
		if(tempArgument[1] == 1) { isRestartEnable = true; } 
		ObjSound_SetRestartEnable(musicARRAY[tempArgument[0]],isRestartEnable);}
	if(GetEventType() == EV_USER+104){
		ObjSound_Play(musicARRAY[GetEventArgument(0)] );}
	if(GetEventType() == EV_USER+1040) {
		ObjSound_SetFade(GetEventArgument(0),0); 
		ObjSound_Stop(musicARRAY[GetEventArgument(0)] );}}

@MainLoop{
	CollectItemsInCircle(ObjMove_GetX(GetPlayerObjectID),ObjMove_GetY(GetPlayerObjectID),70);
	yield;}
	
@Finalize{}

task TStage{
	let path = GetCurrentScriptDirectory ~ "Ultra_Mid_Plural.txt";
	let idScript = LoadScriptInThread(path);
	StartScript(idScript);
	
	let path2 = GetCurrentScriptDirectory ~ "Ultra_Plural.txt";
	let idScript2 = LoadScriptInThread(path2);
	StartScript(idScript2);

	FairyCheck;
	TAutoDeath;
	ObjMove_SetX(GetPlayerObjectID,GetStgFrameWidth/2);
	ObjMove_SetY(GetPlayerObjectID,GetStgFrameHeight-150);
	PlayMusic(1);
	BlackBackground;
	ItemBorderText;
	ItemBorderLine;
	TextBGM(275,430,"BGM: Lit Road of the Seven-Coloured Light");
	loop(120){yield;}
	
		TFairyU1;
	loop(500){yield;}
		TStageTitle;
	loop(360){yield;}

	TBlueFire;
	loop(2){
		TFairyU2(-10,50,100,80,500,100);
		loop(240){yield;}
		TFairyU2(394,50,284,80,-100,100);
		loop(240){yield;}}
	task TBlueFire{
		loop(16){TSpiritU2(rand(84,300),-50,rand(80,100));
		loop(60){yield;}}}
		loop(300){yield;}

		loop(10){TFairyU3(-50,30);
			TFairyU3(434,150);
		loop(80){yield;}}
		loop(60){yield;}

		TFairyU4(334,-500);
		TFairyU4(50,500);
		loop(640){yield;}

		TFairyU5(192);
		loop(360){yield;}
		TFairyU5(340);
		loop(360){yield;}
		TFairyU5(44);
		loop(360){yield;}

		waitForNoEnemy;

	SetCommonData("Start",1);
		while(!IsCloseScript(idScript)){
		if(GetCommonData("Item",0) == 1){
			let radius = 5;
			let shotarray = [];
			let bossarray = GetEnemyBossObjectID();
			let objScene = GetEnemyBossSceneObjectID();
			ascent(i in 0..length(bossarray)){
			if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) +
			ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0){
				LifeP(ObjMove_GetX(bossarray[i]),ObjMove_GetY(bossarray[i]));}
			else{SpellP(ObjMove_GetX(bossarray[i]),ObjMove_GetY(bossarray[i]));}
			ascent(count in 0..33){
				let r = 100;
				PowerS(ObjMove_GetX(bossarray[i])+rand(-r,r),ObjMove_GetY(bossarray[i])+rand(-r,r));
				Point(ObjMove_GetX(bossarray[i])+rand(-r,r),ObjMove_GetY(bossarray[i])+rand(-r,r));}}
			loop(12){
			shotarray = GetShotIdInCircleA2( 192, 210, radius, TARGET_ENEMY);
			ascent( i in 0..length(shotarray)){
				if(ObjMove_GetX(shotarray[i])>0 && ObjMove_GetX(shotarray[i])<384 && ObjMove_GetY(shotarray[i])>0 && ObjMove_GetY(shotarray[i])<448){
					Item(ObjMove_GetX(shotarray[i]),ObjMove_GetY(shotarray[i]));}
				DeleteShotInCircle(TYPE_SHOT,TYPE_IMMEDIATE, 192, 210, radius/1.2);}
				radius+=20;yield;}
				loop(30){yield;}
				let r = 60;
				loop(40){
					CollectItemsInCircle(GetPlayerX, GetPlayerY, r);
					r+=12; yield;}
			SetCommonData("Item",0);}
		yield;}
	MakeExplosionA2(192,192,"HEART",255,64,64);
		
	SE(se_enep2);
	loop(60){yield;}
		TFairyU8(250,-15,192,80,1,19);
	loop(80){yield;}
		TFairyU8(0,-15,150,50,-1,31);
	loop(80){yield;}
		TFairyU8(384,-15,284,50,1,30);
	loop(80){yield;}
		TFairyU8(102,-15,52,50,-1,27);
	loop(80){yield;}
		TFairyU8(50,-15,150,20,1,24);
	loop(80){yield;}
		TFairyU8(250,-15,290,50,-1,23);
	loop(80){yield;}
		TFairyU8(192,-15,192,40,1,20);
	loop(120){yield;}
		TFairyU6;
		loop(7){
			TSpiritU6(-50,20,rand(40,80),rand(40,80));
			loop(30){yield;}
			TSpiritU6(434,20,384-rand(40,80),rand(40,80));
			loop(30){yield;}}
	loop(60){yield;}
		waitForNoEnemy;
	TFairyU10("red",823);waitForNoEnemy;w(90);
	TFairyU10("green",825);waitForNoEnemy;w(90);
	TFairyU10("blue",824);waitForNoEnemy;w(90);
	loop(120){yield;}
		MiyakoAppearance;
		SetForbidPlayerShot(true);
		SetForbidPlayerSpell(true);
	SetCommonData("MiyakoBG",2);
	loop(120){yield;}
	SetCommonData("Boss",1);
	loop(120){yield;}
	
	TextBGM(320, 410,"BGM: Crystal Flower Lantern's [r]Mysterious Guide ~ Crystal Lantern");

	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);

		while(!IsCloseScript(idScript2)){
			if(GetCommonData("Item",0) == 1){
				let radius = 5;
				let shotarray = [];
				let bossarray = GetEnemyBossObjectID();
				let objScene = GetEnemyBossSceneObjectID();
				ascent(i in 0..length(bossarray)){
				if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) +
				ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0){
					LifeP(ObjMove_GetX(bossarray[i]),ObjMove_GetY(bossarray[i]));}
				else{SpellP(ObjMove_GetX(bossarray[i]),ObjMove_GetY(bossarray[i]));}
				ascent(count in 0..33){
					let r = 100;
					PowerS(ObjMove_GetX(bossarray[i])+rand(-r,r),ObjMove_GetY(bossarray[i])+rand(-r,r));
					Point(ObjMove_GetX(bossarray[i])+rand(-r,r),ObjMove_GetY(bossarray[i])+rand(-r,r));}}
				loop(12){
				shotarray = GetShotIdInCircleA2( 192, 210, radius, TARGET_ENEMY);
				ascent( i in 0..length(shotarray)){
					if(ObjMove_GetX(shotarray[i])>0 && ObjMove_GetX(shotarray[i])<384 && ObjMove_GetY(shotarray[i])>0 && ObjMove_GetY(shotarray[i])<448){
						Item(ObjMove_GetX(shotarray[i]),ObjMove_GetY(shotarray[i]));}
						DeleteShotInCircle(TYPE_SHOT,TYPE_IMMEDIATE, 192, 210, radius/1.2);}
						radius+=24;yield;}
				loop(30){yield;}
				let r = 60;
				loop(40){
					CollectItemsInCircle(GetPlayerX, GetPlayerY, r);
					r+=12; yield;}
				SetCommonData("Item",0);}
			yield;}

	loop(200){yield;}
	TExtraClear;
	loop(120){yield;}
	let score = GetScore;
	loop(100){
		AddScore(100000000);
		yield;}
	loop(240){yield;}
	StopMusic(GetAreaCommonData("MUSIC","Song",0));
	CloseStgScene();}

function waitForNoEnemy{
	while(length(GetAllEnemyID) > 0){yield;}}

function BlackBackground{
	let object = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(object,black);
	Obj_SetRenderPriorityI(object, 100);
	ObjSprite2D_SetSourceRect(object, 0, 0, 640, 480);
	ObjSprite2D_SetDestRect(object, 0, 0, 640, 480);
	loop(60){yield;}
	let alpha = 0;
	loop(85){
	ObjRender_SetAlpha(object,255-alpha);
		alpha+=3; yield;}
	Obj_Delete(object);
	return;}

task TAutoDeath{
	while(GetPlayerState != STATE_END){yield;}
	loop(180){yield;}
	StopMusic(GetAreaCommonData("MUSIC","Song",0));
	yield;
	CloseStgScene;}

task TExtraClear{
	let alpha = 0;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let end = GetCurrentScriptDirectory() ~ "./../Image/extraclear.png";
	ObjPrim_SetTexture(obj,end);
	Obj_SetRenderPriorityI(obj, 60);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 563, 165);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,0.6,0.6,0);
	ObjRender_SetX(obj,GetStgFrameWidth/2);
	ObjRender_SetY(obj,180);
	TScore;
		loop(52){
			ObjRender_SetAlpha(obj,alpha);
			alpha+=5;yield;}
		loop(160){yield;}
		loop(52){
			ObjRender_SetAlpha(obj,alpha);
			alpha-=5;yield;}
		Obj_Delete(obj);
		
		task TScore{
			Number(130-15,190,1);
			loop(10){yield;}
			ascent(i in 0..10){
				Number(130+i*15,190,0);
				loop(10){yield;}
			}
			task Number(x,y,type){
				let alpha = 0;
				let move = 0;
				let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
				let end = GetCurrentScriptDirectory() ~ "./../Image/ending.png";
				ObjPrim_SetTexture(obj1,end);
				Obj_SetRenderPriorityI(obj1, 60);
				if(type == 1){ObjSprite2D_SetSourceRect(obj1, 18, 30, 42, 60);}
				if(type == 0){ObjSprite2D_SetSourceRect(obj1, 98, 30, 122, 60);}
				ObjSprite2D_SetDestCenter(obj1);
				ObjRender_SetScaleXYZ(obj1,1.6,1.6,0);
				loop(50){
					ObjRender_SetX(obj1,x);
					ObjRender_SetY(obj1,y-20*sin(move));
					ObjRender_SetAlpha(obj1,alpha);
					alpha+=5;move+=5;yield;}
				loop(120){yield;}
				loop(52){
					ObjRender_SetAlpha(obj1,alpha);
					alpha-=5;yield;}
				Obj_Delete(obj1);
			}
		}
		
		return;}

task TextBGM(x,y,text){
	let alpha= 0;
	let xpos = x;
	let objtext1 = ObjText_Create();
	ObjText_SetFontType(objtext1,"Blogger Sans Light");
	ObjText_SetText(objtext1, text);
	ObjText_SetFontSize(objtext1, 15);
	ObjText_SetFontBold(objtext1, true);
	ObjText_SetFontColorTop(objtext1, 255, 200, 200);
	ObjText_SetFontColorBottom(objtext1, 255, 120, 120);
	ObjText_SetFontBorderType(objtext1, BORDER_FULL);
	ObjText_SetFontBorderColor(objtext1,64, 0, 0);
	ObjText_SetFontBorderWidth(objtext1, 1);
	Obj_SetRenderPriorityI(objtext1, 80);
	ObjRender_SetPosition(objtext1,xpos,y,0);
	ObjRender_SetAlpha(objtext1,alpha);
	loop(20){yield;}
	loop(90){
		ObjRender_SetPosition(objtext1,xpos,y,0);
		ObjRender_SetAlpha(objtext1,alpha);
		alpha+=255/90;
		xpos-=2; yield;}
	loop(180){yield;}
	loop(90){
		ObjRender_SetPosition(objtext1,xpos,y,0);
		ObjRender_SetAlpha(objtext1,alpha);
		alpha-=255/90;
		xpos+=2; yield;}
	Obj_Delete(objtext1);}

task TNon9Effect{
	THalo(255,64,64,0);
	THalo(64,255,64,120);
	THalo(64,64,255,240);
	SetCommonData("Non9Effect",0);
	task THalo(r,g,b,vel){let v = 0;
		let circle = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(circle,halo);
		Obj_SetRenderPriorityI(circle,50);
		ObjRender_SetAlpha(circle,200);
		ObjRender_SetPosition(circle,384/2,448/2,0);
		ObjRender_SetBlendType(circle,BLEND_ADD_ARGB);
		ObjSprite2D_SetSourceRect(circle,0,0,256,256);
		ObjSprite2D_SetDestCenter(circle);
		ObjRender_SetColor(circle,r,g,b);
			while(GetCommonData("Non9Effect",0) < 2){
				ObjRender_SetPosition(circle,384/2+v*sin(vel),448/2+v*cos(vel),0);
				if(GetCommonData("Non9Effect",0) == 0){v+=0.6;}
				if(GetCommonData("Non9Effect",0) == 1){v-=1.8;}
				if(v > 200){v = 200;}
				vel+=0.5; yield;}
			DeleteCommonData("Non9Effect");}}

task ItemBorderText{
	let alpha = 0;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let item = GetCurrentScriptDirectory() ~ "./../Image/itemborderline.png";
	ObjPrim_SetTexture(obj,item);
	Obj_SetRenderPriorityI(obj, 30);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 180, 20);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetX(obj,GetStgFrameWidth/2);
	ObjRender_SetY(obj,120);
		loop(210){
			ObjRender_SetAlpha(obj,150+150*sin(270+alpha));
			alpha+=5;yield;}
		Obj_Delete(obj);
		return;}

task ItemBorderLine{
	let alpha = 0;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let item = GetCurrentScriptDirectory() ~ "./../Image/itemborderline.png";
	ObjPrim_SetTexture(obj,item);
	Obj_SetRenderPriorityI(obj, 30);
	ObjSprite2D_SetSourceRect(obj, 0, 25, 180, 40);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,2.5,0.5,0);
	ObjRender_SetX(obj,GetStgFrameWidth/2);
	ObjRender_SetY(obj,135);
		loop(210){
			ObjRender_SetAlpha(obj,150+150*sin(270+alpha));
			alpha+=5;yield;}
		Obj_Delete(obj);
		return;}

task TStageTitle{
	let scale = 0;
	let alpha = 0;
	let air = 0;

	let lantern1 = CreateLantern(290,215,0.5);
	let lantern2 = CreateLantern(310,205,0.3);
	let logo = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(logo,GetCurrentScriptDirectory ~ "./../Image/logo.png");
	Obj_SetRenderPriorityI(logo,32);
	ObjSprite2D_SetSourceRect(logo,0,0,330,30);
	ObjSprite2D_SetDestCenter(logo);
	ObjRender_SetBlendType(logo,BLEND_ALPHA);
	ObjRender_SetPosition(logo,192,192,0);
	ObjRender_SetScaleXYZ(logo,1,0,0);
	ObjRender_SetAlpha(logo,255);
	LanternManage(lantern1,0.8);
	LanternManage(lantern2,1);

	let objtext1 = ObjText_Create();
	ObjText_SetFontType(objtext1,"Blogger Sans Light");
	ObjText_SetText(objtext1, "Life of lit Kaguradsaka");
	ObjText_SetFontSize(objtext1, 25);
	ObjText_SetFontColorTop(objtext1, 255, 255, 255);
	ObjText_SetFontColorBottom(objtext1, 255, 200, 255);
	Obj_SetRenderPriorityI(objtext1, 38);
	ObjRender_SetPosition(objtext1,50,172,0);
	ObjRender_SetAlpha(objtext1,0);

	let objtext2 = ObjText_Create();
	ObjText_SetFontType(objtext2,"Blogger Sans Light");
	ObjText_SetText(objtext2, "Extra Stage");
	ObjText_SetFontSize(objtext2, 35);
	ObjText_SetFontColorTop(objtext2, 255, 255, 255);
	ObjText_SetFontColorBottom(objtext2, 200, 200, 255);
	Obj_SetRenderPriorityI(objtext2, 33);
	ObjRender_SetPosition(objtext2,50,135,0);
	ObjRender_SetAlpha(objtext2,0);

	let objtext3 = ObjText_Create();
	ObjText_SetFontType(objtext3,"Blogger Sans Light");
	ObjText_SetText(objtext3, "Near Hakurei Shrine");
	ObjText_SetFontSize(objtext3, 20);
	ObjText_SetFontColorTop(objtext3, 255, 255, 255);
	ObjText_SetFontColorBottom(objtext3, 200, 200, 255);
	Obj_SetRenderPriorityI(objtext3, 33);
	ObjRender_SetPosition(objtext3,190,205,0);
	ObjRender_SetAlpha(objtext3,0);

	while(ObjRender_GetScaleY(logo)<1){
		ObjRender_SetScaleY(logo,scale);
		scale+=0.01;yield;}
	loop(85){
		ObjRender_SetX(objtext1,ObjRender_GetX(objtext1)+0.3);
		ObjRender_SetAlpha(objtext1,alpha);
		ObjRender_SetAlpha(objtext2,alpha);
		ObjRender_SetAlpha(objtext3,alpha);
		ObjRender_SetAlpha(lantern1,alpha);
		ObjRender_SetAlpha(lantern2,alpha);
		alpha+=3;yield;}
	loop(120){
		ObjRender_SetX(objtext1,ObjRender_GetX(objtext1)+0.3);
		yield;}
	while(ObjRender_GetScaleY(logo)>0){
		ObjRender_SetX(objtext1,ObjRender_GetX(objtext1)+0.3);
		ObjRender_SetScaleY(logo,scale);
		ObjRender_SetAlpha(objtext1,alpha);
		ObjRender_SetAlpha(objtext2,alpha);
		ObjRender_SetAlpha(objtext3,alpha);
		ObjRender_SetAlpha(lantern1,alpha);
		ObjRender_SetAlpha(lantern2,alpha);
		alpha-=3;scale-=0.01;yield;}

	function CreateLantern(x,y,sc){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "./../Image/stage.png");
		Obj_SetRenderPriorityI(obj,32);
		ObjSprite2D_SetSourceRect(obj,0,0,78,150);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
		ObjRender_SetPosition(obj,x,y,0);
		ObjRender_SetScaleXYZ(obj,sc,sc,0);
		ObjRender_SetAlpha(obj,0);
		return obj;}

	task LanternManage(obj,move){
		while(!Obj_IsDeleted(obj)){
			ObjRender_SetAngleZ(obj,10*sin(air));
			air+=move;
			yield;}}
	Obj_Delete(logo);
	Obj_Delete(lantern1);
	Obj_Delete(lantern2);
	Obj_Delete(objtext1);
	Obj_Delete(objtext2);
	Obj_Delete(objtext3);
	return;}