#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["\"Gift to the Courageous Oath\""]

#include"./Cutin.txt"
#include"./../../Functions/Effects/Effects.txt"
#include"./LoadGraphics.txt"
#include"./LoadSounds.txt"

let bossObj;
let bossX;
let bossY;
let damage = 0;

#include"./MiyakoDrawBoss.txt"

@Initialize{	
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);
	SetAutoDeleteObject(true);
	SetCommonData("bossAURA",3);
	SetCommonData("ForceLifebar",0);

	animateBossM;
	LoadShotData;
	TEnemyMarker;
	mainTask;}

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){SetScriptResult(3000);}
	case(EV_REQUEST_SPELL_SCORE){SetScriptResult(10000000);}
	case(EV_START_BOSS_SPELL){
		let path = GetCurrentScriptDirectory ~ "./../../System/Miyako_System_MagicCircle.txt";
		let id = LoadScript(path);
		StartScript(id);yield;}}

@MainLoop{
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);

	if(damage==0){ObjEnemy_SetDamageRate(bossObj, 0, 0);}
	if(damage==1){ObjEnemy_SetDamageRate(bossObj, 15, 0);}

	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,32);
	yield;}

@Finalize{}

task mainTask{
	
	if(GetCommonData("Replay",0) == 0){
	ObjMove_SetDestAtFrame(bossObj,192,224,30);
	yield;
	SE(se_enep2);
	MakeExplosionA1(bossObj,"SAKURA",255,255,255);
	MakeExplosionA1(bossObj,"SAKURA",255,255,255);
	MakeExplosionA1(bossObj,"SAKURA",255,255,255);

	let s = 2;
	let n = 1;
	let r = 60;
	let ang = 90;
	TEtama;	loop(20){
	let obj1 = CreateLooseLaserA1(bossX+r*cos(rand(0,360)),bossY+r*cos(rand(0,360)),s,80,90,20,1019,0);
	ObjMove_SetAngle(obj1,ang+atan2(ObjMove_GetY(obj1)-bossY,ObjMove_GetX(obj1)-bossX));}
	wait(n);loop(20){
	let obj2 = CreateLooseLaserA1(bossX+r*cos(rand(0,360)),bossY+r*cos(rand(0,360)),s,80,90,20,1031,0);
	ObjMove_SetAngle(obj2,ang+atan2(ObjMove_GetY(obj2)-bossY,ObjMove_GetX(obj2)-bossX));}
	wait(n);loop(20){
	let obj3 = CreateLooseLaserA1(bossX+r*cos(rand(0,360)),bossY+r*cos(rand(0,360)),s,80,90,20,1030,0);
	ObjMove_SetAngle(obj3,ang+atan2(ObjMove_GetY(obj3)-bossY,ObjMove_GetX(obj3)-bossX));}
	wait(n);loop(20){
	let obj4 = CreateLooseLaserA1(bossX+r*cos(rand(0,360)),bossY+r*cos(rand(0,360)),s,80,90,20,1027,0);
	ObjMove_SetAngle(obj4,ang+atan2(ObjMove_GetY(obj4)-bossY,ObjMove_GetX(obj4)-bossX));}
	wait(n);loop(20){
	let obj5 = CreateLooseLaserA1(bossX+r*cos(rand(0,360)),bossY+r*cos(rand(0,360)),s,80,90,20,1025,0);
	ObjMove_SetAngle(obj5,ang+atan2(ObjMove_GetY(obj5)-bossY,ObjMove_GetX(obj5)-bossX));}
	wait(n);loop(20){
	let obj6 = CreateLooseLaserA1(bossX+r*cos(rand(0,360)),bossY+r*cos(rand(0,360)),s,80,90,20,1022,0);
	ObjMove_SetAngle(obj6,ang+atan2(ObjMove_GetY(obj6)-bossY,ObjMove_GetX(obj6)-bossX));}
	wait(n);loop(20){
	let obj7 = CreateLooseLaserA1(bossX+r*cos(rand(0,360)),bossY+r*cos(rand(0,360)),s,80,90,20,1020,0);
	ObjMove_SetAngle(obj7,ang+atan2(ObjMove_GetY(obj7)-bossY,ObjMove_GetX(obj7)-bossX));}}

	
	wait(90);SE(se_spell);
	if(GetCommonData("Replay",0) == 2){
	ObjCutin_SetSpellcardS4("\"Gift to the Courageous Oath\"", cutin, NAZRIN, 240, 104, 104);
	ObjCutin_LaunchS3(MOKOU, cutin, "Practice");}
	else{
	ObjCutin_SetSpellcardS4("\"Gift to the Courageous Oath\"", cutin, NAZRIN, 240, 104, 104);
	ObjCutin_LaunchS3(NAZRIN, cutin, "Extra");}
	ObjMove_SetDestAtWeight(bossObj,CenterX,120,8,3);
	TSpellBG;SetCommonData("BG",1);
	wait(90);damage=1;
	
	let time = 300;
	let amount = 1;
	
	let objw = CreateShotA1(bossX,bossY,2,90,380,30); wait(35);
	ObjShot_SetIntersectionEnable(objw,false);
	effect(objw,255,255,255);bulletManage(objw);SE(se_tan2);
	ascent(i in -87..88){CreateShotA1(bossX,bossY,2,270+i*2,32,0);}
	while(GetObjectDistance(GetPlayerObjectID,objw) > 25){yield;}
	Obj_Delete(objw);
	wait(120);damage=1;
	
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE) > 2000){
		time = 320;
			loop(5){
				if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) < 2000){break;}
				TSpecialPresent(amount,time,round(rand(1,7)));
				wait(64);
				time-=64;}
			if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) < 2000){break;}
			wait(240);}
			
	DeleteShotAll(TYPE_ALL,TYPE_FADE); SE(se_ch2);
	MakeConcentrationB1(bossObj,"SAKURA",255,64,64,75);
	MakeConcentrationB1(bossObj,"SAKURA",64,255,64,75);
	MakeConcentrationB1(bossObj,"SAKURA",64,64,255,75);
	wait(90);
	
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE) > 1000){
		time = 320;
			loop(8){
				if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) < 1000){break;}
				TSpecialPresent(amount,time,round(rand(1,7)));
				wait(40);
				time-=40;}
			if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) < 1000){break;}
			wait(240);}

	DeleteShotAll(TYPE_ALL,TYPE_FADE); SE(se_ch2);
	MakeConcentrationB1(bossObj,"SAKURA",255,64,64,75);
	MakeConcentrationB1(bossObj,"SAKURA",64,255,64,75);
	MakeConcentrationB1(bossObj,"SAKURA",64,64,255,75);
	wait(90);
	
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE) > 0){
		time = 320;
			loop(10){
				if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) < 0){break;}
				TSpecialPresent(amount,time,round(rand(1,7)));
				wait(32);
				time-=32;}
			if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) < 0){break;}
			wait(240);}
			
	SetCommonData("BG",0);
		yield;
	StartSlow(TARGET_ALL,30);
		MakeExplosionA1(bossObj,"SAKURA",64,64,255);SE(se_enep3);wait(30);
		MakeExplosionA1(bossObj,"SAKURA",255,64,64);SE(se_enep3);wait(30);
		MakeExplosionA1(bossObj,"SAKURA",64,255,64);SE(se_enep3);
	SetCommonData("Item",1);wait(90);
		Obj_Delete(bossObj);
		yield;
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	CloseScript(GetOwnScriptID());}

task TSpecialPresent(prison,border,type){
	let gr = 0;
	let ang = 0;
	let r = rand(0,360);
		let shot = CreateShotA1(bossX, bossY, 2, r, 380, 30);
		ObjShot_SetIntersectionEnable(shot,false);
		tManage(shot); TEtReflection(shot);
		bulletManage(shot);
			if(type == 1){effect(shot,255,64,64);	gr = 19;}
			if(type == 2){effect(shot,255,125,64);	gr = 31;}
			if(type == 3){effect(shot,255,255,64);	gr = 30;}
			if(type == 4){effect(shot,64,255,64);	gr = 27;}
			if(type == 5){effect(shot,64,255,255);	gr = 24;}
			if(type == 6){effect(shot,64,64,255);	gr = 23;}
			if(type == 7){effect(shot,255,64,255);	gr = 20;}
		wait(31);SE(se_tan2);
		ascent(i in 0..30){
			CreateShotA1(bossX,bossY,2,i*12+r,gr,0);}
		
	task tManage(obj){
		let color = 255;
		let time = 0;
		while(!Obj_IsDeleted(obj)){
			if(GetObjectDistance(GetPlayerObjectID,obj) <= 25){
				Obj_Delete(obj);break;}
			if(time >= border){
				ObjRender_SetColor(obj,255,color,color);
				color-=1.5;
				
			if(color <= 0){
				let x = ObjMove_GetX(obj);
				let y = ObjMove_GetY(obj);
				loop(prison){ SE(se_kira);
					if(type == 1){
						ascent(i in 0..45){CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),1,i*8,332,0);}
						ascent(i in 0..30){CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),2,i*12,332,0);}}
					if(type == 2){
						ascent(a in 0..2){ ascent(i in 0..10){
						CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),rand(0.6,1.4)+a,i*36+rand(0,18),829,0);}}}
					if(type == 3){
						ascent(i in 0..60){CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),0.5+i/30,i*15,797,0);}}
					if(type == 4){
						ascent(i in 0..8){ ascent(a in 2..8){
						CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),a/5,GetAngleToPlayer(obj)+i*45,107,0);}}}
					if(type == 5){
						ascent(a in 0..2){ ascent(i in 0..15){
						let s = CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),rand(0.5,1)+a,i*24,343,0);
						ObjMove_AddPatternA3(s,0,NO_CHANGE,NO_CHANGE,0,rand(-0.6,0.6),0,NO_CHANGE);}}}
					if(type == 6){
						ascent(a in 0..8){ ascent(i in -1..2){
						CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),1+a/6,GetAngleToPlayer(obj)+i*15,302,0);}}}
					if(type == 7){
						ascent(a in 0..3){ ascent(i in -4..5){
						let b = CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),1+a/2,0,237,0);
						ObjMove_SetAngle(b,GetAngleToPlayer(b)+180+i*15);
						TReflection(b);}}}
						}
					Obj_Delete(obj);
					break;}
				}
		time++;
		yield;}
		}
	}

task effect(object,r,g,b){
	while(!Obj_IsDeleted(object)){
		ShotAura(object,r,g,b);
		wait(2);}}

task bulletManage(object){
	let move = 0;
	while(!Obj_IsDeleted(object)){
		ObjRender_SetAngleZ(object,15*sin(move));
		move+=0.6;yield;}}
			
task TReflection(obj){
	while(!Obj_IsDeleted(obj)){
		if(ObjMove_GetX(obj) > 384){
			ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));break;}
		if(ObjMove_GetX(obj) < 0){
			ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));break;}
		if(ObjMove_GetY(obj) > 448){
			ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj));break;}
		if(ObjMove_GetY(obj) < 0){
			ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj));break;}
	yield;}}

task TEtReflection(obj){
	while(!Obj_IsDeleted(obj)){
		if(ObjMove_GetX(obj) > 384){
			ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));}
		if(ObjMove_GetX(obj) < 0){
			ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));}
		if(ObjMove_GetY(obj) > 448){
			ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj));}
		if(ObjMove_GetY(obj) < 0){
			ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj));}
	yield;}}