#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Magic Lantern \"Jewel of Life Among the People\""]

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

let bossObj;
let bossX;
let bossY;
let position = 1;

let step1 = 0;
let step2 = 0;
let step3 = 0;
let step4 = 0;
let step5 = 0;
let step6 = 0;
let step7 = 0;
let step8 = 0;
let step9 = 0;

#include"./MiyakoDrawBoss.txt"

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

	animateBossM;
	LoadShotData;
	TEnemyMarker;
	mainTask;
	TEnd9;}

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){SetScriptResult(500);}
	case(EV_REQUEST_TIMER){SetScriptResult(138);}
	case(EV_REQUEST_SPELL_SCORE){SetScriptResult(100000000);}
	case(EV_GAIN_SPELL){SetScriptResult(100000000);}
	case(EV_REQUEST_IS_DURABLE_SPELL){SetScriptResult(true);}
	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);

	ObjEnemy_SetDamageRate(bossObj, 0, 0);
	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
	if(position == 0){
		ObjMove_SetDestAtWeight(bossObj,GetPlayerX,GetPlayerY,5,3);}
	yield;}

@Finalize{}

task mainTask{
	ObjMove_SetDestAtWeight(bossObj,CenterX,CenterY,8,3);
	TEtama;
	if(GetCommonData("Replay",0) == 2){Obj_SetVisible(bossObj,false); wait(90);}
	else{wait(90); Obj_SetVisible(bossObj,false);}
	wait(90);SE(se_spell);
	if(GetCommonData("Replay",0) == 2){
	ObjCutin_SetSpellcardS4("Magic Lantern \"Jewel of Life Among the People\"", cutin, NAZRIN, 240, 104, 104);
	ObjCutin_LaunchS3(MOKOU, cutin, "Practice");}
	else{
	ObjCutin_SetSpellcardS4("Magic Lantern \"Jewel of Life Among the People\"", cutin, NAZRIN, 240, 104, 104);
	ObjCutin_LaunchS3(NAZRIN, cutin, "Extra");}
	TSpellBG;SetCommonData("BG",1);TSurvival;position=0;wait(90);
	
	SE(se_ch2);MakeConcentrationB1(bossObj,"SAKURA",255,255,255,75);
	wait(75);SE(se_enep3);MakeExplosionA1(bossObj,"SAKURA",255,255,255);
	
	let obj1 = CreateShotA1(bossX,bossY,0,0,380,0);
	effect(obj1,255,64,64);bulletManage(obj1);
	ObjMove_SetDestAtWeight(obj1,CenterX,CenterY-120,12,3);
	
	wait(60);
	TChaoticMove(obj1);
	let time = 40;
	let ring = 40;
	let r = 0;
	loop(8){ SE(se_tan2);
		ascent(i in 0..ring){
			CreateShotG1(ObjMove_GetX(obj1),ObjMove_GetY(obj1),1,i*360/ring+r,210,332,0);}
			if(time<=30){time=30;}
			if(ring>=45){ring=45;}
		r = rand(0,50);
		wait(time);time-=1;ring+=1;}
	loop(12){ SE(se_tan2);
		ascent(i in 0..45){
			CreateShotG1(ObjMove_GetX(obj1),ObjMove_GetY(obj1),1,i*8+r,210,332,0);}
		ascent(i in 0..25){
			CreateShotG1(ObjMove_GetX(obj1),ObjMove_GetY(obj1),2,i*360/25+r,210,332,0);}
		r = rand(0,60);
		wait(30);}
	wait(180);SE(se_ch2);MakeConcentrationB1(bossObj,"SAKURA",255,255,255,75);
	wait(75);SE(se_enep3);MakeExplosionA1(bossObj,"SAKURA",255,255,255);	
	
	let obj2 = CreateShotA1(bossX,bossY,0,0,380,0);
	effect(obj2,255,128,64);bulletManage(obj2);
	ObjMove_SetDestAtWeight(obj2,CenterX+rand(-60,60),CenterY-120,12,3);
	wait(60);
	TChaoticMove(obj2);
	let angle = rand(0,60);
	loop(130){ SE(se_tan2);
			ascent(i in 0..2){
				CreateShotG1(ObjMove_GetX(obj1),ObjMove_GetY(obj1),2+i,angle,223,829,0);
				CreateShotG1(ObjMove_GetX(obj2),ObjMove_GetY(obj2),2+i,angle+180,223,829,0);}
			wait(5); angle+=35;}
	wait(90);SE(se_ch2);MakeConcentrationB1(bossObj,"SAKURA",255,255,255,75);
	wait(75);SE(se_enep3);MakeExplosionA1(bossObj,"SAKURA",255,255,255);
	step1=1;
	let obj3 = CreateShotA1(bossX,bossY,0,0,380,0);
	effect(obj3,255,255,64);bulletManage(obj3);
		TCircularMove(obj1,step3,0);
		TCircularMove(obj2,step3,120);
		TCircularMove(obj3,step3,240);
	wait(120);
	let boss = GetAngleToPlayer(bossObj);
	loop(120){SE(se_tan2);
		ascent(i in 0..2){
			CreateShotG1(ObjMove_GetX(obj1),ObjMove_GetY(obj1),1,boss+angle+i*180,221,797,0);
			CreateShotG1(ObjMove_GetX(obj2),ObjMove_GetY(obj2),1,boss+angle+i*180,221,797,0);
			CreateShotG1(ObjMove_GetX(obj3),ObjMove_GetY(obj3),1,boss+angle+i*180,221,797,0);}
		angle+=16;wait(6);}
	wait(90);SE(se_ch2);MakeConcentrationB1(bossObj,"SAKURA",255,255,255,75);
	wait(75);SE(se_enep3);MakeExplosionA1(bossObj,"SAKURA",255,255,255);
	step3=3;
	let obj4 = CreateShotA1(bossX,bossY,0,0,380,0);
	effect(obj4,64,255,64);bulletManage(obj4);
		TCircularMove(obj1,step4,0);
		TCircularMove(obj2,step4,90);
		TCircularMove(obj3,step4,180);
		TCircularMove(obj4,step4,270);
	wait(150);
	loop(8){SE(se_tan2);
		ascent(i in 0..8){ ascent(a in 2..8){
			CreateShotG1(ObjMove_GetX(obj1),ObjMove_GetY(obj1),a/5,GetAngleToPlayer(obj1)+i*45,218,107,0);
			CreateShotG1(ObjMove_GetX(obj2),ObjMove_GetY(obj2),a/5,GetAngleToPlayer(obj2)+i*45,218,107,0);
			CreateShotG1(ObjMove_GetX(obj3),ObjMove_GetY(obj3),a/5,GetAngleToPlayer(obj3)+i*45,218,107,0);
			CreateShotG1(ObjMove_GetX(obj4),ObjMove_GetY(obj4),a/5,GetAngleToPlayer(obj4)+i*45,218,107,0);}}
		wait(90);}
	wait(30);SE(se_ch2);MakeConcentrationB1(bossObj,"SAKURA",255,255,255,75);
	wait(75);SE(se_enep3);MakeExplosionA1(bossObj,"SAKURA",255,255,255);
	step4=4;
	let c = 270;
	let obj5 = CreateShotA1(bossX,bossY,0,0,380,0);
	effect(obj5,64,255,255);bulletManage(obj5);
		TCircularMove(obj1,step5,c);
		TCircularMove(obj2,step5,72+c);
		TCircularMove(obj3,step5,144+c);
		TCircularMove(obj4,step5,216+c);
		TCircularMove(obj5,step5,288+c);
	wait(60);
	loop(4){
		CreateNotesCircle(ObjMove_GetX(obj1),ObjMove_GetY(obj1)); wait(40);
		CreateNotesCircle(ObjMove_GetX(obj2),ObjMove_GetY(obj2)); wait(40);
		CreateNotesCircle(ObjMove_GetX(obj3),ObjMove_GetY(obj3)); wait(40);
		CreateNotesCircle(ObjMove_GetX(obj4),ObjMove_GetY(obj4)); wait(40);
		CreateNotesCircle(ObjMove_GetX(obj5),ObjMove_GetY(obj5)); wait(40);}
	SE(se_ch2);MakeConcentrationB1(bossObj,"SAKURA",255,255,255,75);
	wait(75);SE(se_enep3);MakeExplosionA1(bossObj,"SAKURA",255,255,255);
	step5=5;c=120;
	let obj6 = CreateShotA1(bossX,bossY,0,0,380,0);
	effect(obj6,64,64,255);bulletManage(obj6);
		TCircularMove(obj1,step6,c);
		TCircularMove(obj2,step6,60+c);
		TCircularMove(obj3,step6,120+c);
		TCircularMove(obj4,step6,180+c);
		TCircularMove(obj5,step6,240+c);
		TCircularMove(obj6,step6,300+c);
	wait(120);
	loop(5){ SE(se_tan2); ascent(a in 0..8){
		let bub1 = CreateShotG1(ObjMove_GetX(obj1),ObjMove_GetY(obj1),0.5+a/6,0,214,302,0);
		ObjMove_SetAngle(bub1,GetAngleToPlayer(bub1));} wait(24);
		
		SE(se_tan2); ascent(a in 0..8){
		let bub2 = CreateShotG1(ObjMove_GetX(obj2),ObjMove_GetY(obj2),0.5+a/6,0,214,302,0);
		ObjMove_SetAngle(bub2,GetAngleToPlayer(bub2));} wait(24);
		
		SE(se_tan2); ascent(a in 0..8){
		let bub3 = CreateShotG1(ObjMove_GetX(obj3),ObjMove_GetY(obj3),0.5+a/6,0,214,302,0);
		ObjMove_SetAngle(bub3,GetAngleToPlayer(bub3));} wait(24);
		
		SE(se_tan2); ascent(a in 0..8){
		let bub4 = CreateShotG1(ObjMove_GetX(obj4),ObjMove_GetY(obj4),0.5+a/6,0,214,302,0);
		ObjMove_SetAngle(bub4,GetAngleToPlayer(bub4));} wait(24);
		
		SE(se_tan2); ascent(a in 0..8){
		let bub5 = CreateShotG1(ObjMove_GetX(obj5),ObjMove_GetY(obj5),0.5+a/6,0,214,302,0);
		ObjMove_SetAngle(bub5,GetAngleToPlayer(bub5));} wait(24);
		
		SE(se_tan2); ascent(a in 0..8){
		let bub6 = CreateShotG1(ObjMove_GetX(obj6),ObjMove_GetY(obj6),0.5+a/6,0,214,302,0);
		ObjMove_SetAngle(bub6,GetAngleToPlayer(bub6));} wait(24);}
		
	wait(60);SE(se_ch2);MakeConcentrationB1(bossObj,"SAKURA",255,255,255,75);
	wait(75);SE(se_enep3);MakeExplosionA1(bossObj,"SAKURA",255,255,255);
	step6=6;
	let obj7 = CreateShotA1(bossX,bossY,0,0,380,0);
	effect(obj7,255,64,255);bulletManage(obj7);
		TCentralMove(obj1,step7,(360/7)*1);
		TCentralMove(obj2,step7,(360/7)*2);
		TCentralMove(obj3,step7,(360/7)*3);
		TCentralMove(obj4,step7,(360/7)*4);
		TCentralMove(obj5,step7,(360/7)*5);
		TCentralMove(obj6,step7,(360/7)*6);
		TCentralMove(obj7,step7,360);
	wait(120);
	loop(55){ SE(se_tan2);
		let h1 = CreateShotG1(ObjMove_GetX(obj1),ObjMove_GetY(obj1),3,0,214,237,0);
		let h2 = CreateShotG1(ObjMove_GetX(obj2),ObjMove_GetY(obj2),3,0,214,237,0);
		let h3 = CreateShotG1(ObjMove_GetX(obj3),ObjMove_GetY(obj3),3,0,214,237,0);
		let h4 = CreateShotG1(ObjMove_GetX(obj4),ObjMove_GetY(obj4),3,0,214,237,0);
		let h5 = CreateShotG1(ObjMove_GetX(obj5),ObjMove_GetY(obj5),3,0,214,237,0);
		let h6 = CreateShotG1(ObjMove_GetX(obj6),ObjMove_GetY(obj6),3,0,214,237,0);
		let h7 = CreateShotG1(ObjMove_GetX(obj7),ObjMove_GetY(obj7),3,0,214,237,0);
		ObjMove_SetAngle(h1,GetAngleToPlayer(h1)+180); Reflection(h1);
		ObjMove_SetAngle(h2,GetAngleToPlayer(h2)+180); Reflection(h2);
		ObjMove_SetAngle(h3,GetAngleToPlayer(h3)+180); Reflection(h3);
		ObjMove_SetAngle(h4,GetAngleToPlayer(h4)+180); Reflection(h4);
		ObjMove_SetAngle(h5,GetAngleToPlayer(h5)+180); Reflection(h5);
		ObjMove_SetAngle(h6,GetAngleToPlayer(h6)+180); Reflection(h6);
		ObjMove_SetAngle(h7,GetAngleToPlayer(h7)+180); Reflection(h7);
		wait(18);}
		position = 1;
	ObjMove_SetDestAtWeight(bossObj,CenterX,CenterY,5,3);
		
	wait(45);SE(se_ch2);MakeConcentrationB1(bossObj,"SAKURA",255,255,255,75);
	wait(45);SE(se_ch2);MakeConcentrationB1(bossObj,"SAKURA",255,255,255,75);
	wait(45);SE(se_ch2);MakeConcentrationB1(bossObj,"SAKURA",255,255,255,75);
	wait(75);SE(se_enep3);
		MakeExplosionA1(bossObj,"SAKURA",255,255,255);
		MakeExplosionA1(bossObj,"SAKURA",255,255,255);
		MakeExplosionA1(bossObj,"SAKURA",255,255,255);

	step7 = 7;
	let obj8 = CreateShotA1(bossX,bossY,0,0,380,0);
	effect(obj8,255,255,255);bulletManage(obj8);
	ObjRender_SetScaleXYZ(obj8,1.5,1.5,0);
		TReflectionMove(obj8,obj1);
		TReflectionMove(obj8,obj2);
		TReflectionMove(obj8,obj3);
		TReflectionMove(obj8,obj4);
		TReflectionMove(obj8,obj5);
		TReflectionMove(obj8,obj6);
		TReflectionMove(obj8,obj7);
	TSaw(obj1,1019);
	TSaw(obj2,1031);
	TSaw(obj3,1030);
	TSaw(obj4,1027);
	TSaw(obj5,1025);
	TSaw(obj6,1022);
	TSaw(obj7,1020);
	wait(600); step8=8;
		TReflectionMoveEnd(obj1);
		TReflectionMoveEnd(obj2);
		TReflectionMoveEnd(obj3);
		TReflectionMoveEnd(obj4);
		TReflectionMoveEnd(obj5);
		TReflectionMoveEnd(obj6);
		TReflectionMoveEnd(obj7);}

task TSaw(obj,gr){
	ascent(i in 0..20){
		let l = CreateStraightLaserA1(0,0,0,60,22,1400,gr,180);
		TRotation(l,i*18);}
		wait(180); SE(se_lazer);

		task TRotation(shot,ang){
			let move = 0;
			while(!Obj_IsDeleted(obj)){
				let x = ObjMove_GetX(obj);
				let y = ObjMove_GetY(obj);
				ObjStLaser_SetAngle(shot,90+ang+move);
				ObjMove_SetPosition(shot,x+40*cos(move+ang),y+40*sin(move+ang));
			move+=3; yield;}}}

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

task TReflectionMove(objA,objB){
	ObjMove_SetAngle(objB,GetAngleToPlayer(objB) + 180);
	ObjMove_SetSpeed(objB,2.5);
	while(step8 == 0){
		if(ObjMove_GetY(objB) <= 0 || ObjMove_GetY(objB) >= 448){
			ObjMove_SetAngle(objB,-ObjMove_GetAngle(objB)); yield;}
		if(ObjMove_GetX(objB) <= 0 || ObjMove_GetX(objB) >= 384){
			ObjMove_SetAngle(objB,180-ObjMove_GetAngle(objB));yield;}
		yield;}}
		
task TReflectionMoveEnd(objB){
	while(step9 == 0){
		if(ObjMove_GetY(objB) <= 0 || ObjMove_GetY(objB) >= 448 || ObjMove_GetX(objB) <= 0 || ObjMove_GetX(objB) >= 384){
			ObjMove_SetDestAtWeight(objB,192,224,2,2); yield;}
		yield;}}

task CreateNotesCircle(x,y){
	SE(se_tan2);
	ascent(i in 0..20){
		let note = CreateShotG1(x,y,4,i*18,216,343,0);
		ObjMove_AddPatternA3(note,20,0,NO_CHANGE,0,0,0,NO_CHANGE);
		ObjMove_AddPatternA3(note,90,1,NO_CHANGE,0,rand(-2,2),0,NO_CHANGE);
		ObjMove_AddPatternA3(note,150,1,NO_CHANGE,0,rand(-1,1),0,NO_CHANGE);
		ObjMove_AddPatternA3(note,210,1,NO_CHANGE,0,rand(-0.5,0.5),0,NO_CHANGE);
		ObjMove_AddPatternA3(note,270,1,NO_CHANGE,0,rand(-0.1,0.1),0,NO_CHANGE);}
	wait(90); SE(se_kira);}
		
function CreateShotG1(x, y, speed, angle, graphic, graphic2, delay){
	let shot = CreateShotA1(x, y, speed, angle, graphic, delay);
	task tManage{
		while(!Obj_IsDeleted(shot)){
			if(GetObjectDistance(shot,GetPlayerObjectID()) <= 80){
			ObjShot_SetGraphic(shot, graphic2);}
			else{ObjShot_SetGraphic(shot, graphic);}
		yield;}}
	tManage;
	return shot;}

task TChaoticMove(object){
	let move = 0;
	let move2 = 0;
	while(step1 == 0){
		if(step7 == 7){break;}
		ObjMove_SetDestAtSpeed(object,192+90*cos(move),CenterY-120+60*sin(move),0.8);
	move+=3*sin(move2);move2+=0.6;yield;} return;}

task TCircularMove(object,stepTYPE,ang){
	let move = ang;
	while(stepTYPE == 0){
		if(step7 == 7){break;}
		ObjMove_SetDestAtSpeed(object,GetPlayerX+240*cos(move),GetPlayerY+240*sin(move),3);
	move++;yield;}
	yield; return;}
	
task TCentralMove(object,stepTYPE,ang){
	let move = ang;
	while(stepTYPE == 0){
		if(step7 == 7){break;}
		ObjMove_SetDestAtSpeed(object,CenterX+240*cos(move),CenterY+240*sin(move),3);
	move+=0.85;yield;} return;}

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

task bulletManage(object){
	let move = 0;
	ObjShot_SetSpellResist(object,true);
	ObjShot_SetAutoDelete(object,false);
	ObjShot_SetIntersectionEnable(object,false);
	while(!Obj_IsDeleted(object)){
		ObjRender_SetAngleZ(object,15*sin(move));
		move+=2;yield;}}