#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["ReimuS4"]
#Text["."]
#System["./System.txt"]
#include"./Functions/Index.txt"

let frame=0;
let sAngle=0;
let signal=1;
let objScene=GetEnemyBossSceneObjectID;
let GCSD=GetCurrentScriptDirectory~"";

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(2500)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(70)
	}
}

@MainLoop{
	frame++;
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),24);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),26);
	yield;
	
}

task mainTask{
	objBoss=TDrawBoss;
	ObjEnemy_SetDamageRate(objBoss,0,0);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/3,7.5,5);
	while(IsPlayerSpellActive==true||ObjMove_GetSpeed(objBoss)>0){ yield; }
	TEnemyHitboxStart(objBoss,360);
	wait(30);
	ObjEnemyBossScene_StartSpell(objScene);
	Cutin(objBoss,"Amulet \"One Hundred Cleansing Charms\"");
	TEnd;
	wait(120);
	TShoot01;
	TShoot02;
}

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
	TExplosionA(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),10,0.5);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	Obj_Delete(objBoss);
	loop(120){
		DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
		yield;
		}
	CloseScript(GetOwnScriptID());
	return;
}

task TShoot01{
	let bS=[1,2,3,4,5];
	let p=1;
	signal=2;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let obj=[];
		let S=[];
		let TYPE=[];
		TCharge(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss));
		wait(60);
		signal=1;
		ascent(i in 0..length(bS)){
			let angle=0;
			while(angle<360){
				let angle2=-2.5;
				while(angle2<=2.5){
					let o=CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),bS[i],angle+angle2+sAngle+GetAngleToPlayer(objBoss),-(bS[i]/25),0,AMULET_RED,10);
					obj=obj~[o];
					S=S~[ObjMove_GetSpeed(o)];
					TYPE=TYPE~[p];
					o=CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),bS[i],angle+angle2+-sAngle+GetAngleToPlayer(objBoss),-(bS[i]/25),0,AMULET_RED,10);
					obj=obj~[o];
					S=S~[ObjMove_GetSpeed(o)];
					TYPE=TYPE~[p];
					angle2+=5;
					p=-p;
				}
				angle+=120;
			}
			sAngle+=90;
		}
		PlaySound(GCSD~"se/tan01.wav");
		TMovement;
		ascent(i in 0..length(obj)){
			while(ObjMove_GetSpeed(obj[i])>0){ yield; }
		}
		ascent(i in 0..length(obj)){
			if(!Obj_IsDeleted(obj[i])){
				ObjMove_SetSpeed(obj[i],S[i]);
				ObjMove_SetAcceleration(obj[i],-(S[i]/25));
				ObjMove_SetAngle(obj[i],ObjMove_GetAngle(obj[i])+(TYPE[i]*50));
				ObjShot_SetGraphic(obj[i],SMALL_RED);
			}
		}
		PlaySound(GCSD~"se/kira00.wav");
		TMovement;
		ascent(i in 0..length(obj)){
			while(ObjMove_GetSpeed(obj[i])>0){ yield; }
		}
		ascent(i in 0..length(obj)){
			if(!Obj_IsDeleted(obj[i])){
				ObjMove_SetSpeed(obj[i],S[i]);
				ObjMove_SetAcceleration(obj[i],-(S[i]/25));
				ObjMove_SetAngle(obj[i],ObjMove_GetAngle(obj[i])-(TYPE[i]*150));
				ObjShot_SetGraphic(obj[i],SMALL_PURPLE);
			}
		}
		PlaySound(GCSD~"se/kira00.wav");
		ascent(i in 0..length(obj)){
			while(ObjMove_GetSpeed(obj[i])>0){ yield; }
		}
		ascent(i in 0..length(obj)){
			if(!Obj_IsDeleted(obj[i])){
				let A=ObjMove_GetAngle(obj[i])+(TYPE[i]*235);
				let Sp=S[i]/2;
				if(S[i]!=3&&S[i]!=1){
					let angle=0;
					loop(8){
						CreateShotA1(ObjMove_GetX(obj[i]),ObjMove_GetY(obj[i]),Sp,angle+A,AMULET_PURPLE,0);
						angle+=45;
					}
				}
				ObjShot_FadeDelete(obj[i]);
			}
		}
		PlaySound(GCSD~"se/kira00.wav");
		signal=2;
		wait(60);
	}
}

task TShoot02{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		if(signal==2){
			loop(15){
				CreateShotA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),rand(0.75,2.75),rand(0,360)+GetAngleToPlayer(objBoss),SMALL_RED,5);
			}
			PlaySound(GCSD~"se/tan00.wav");
			wait(10);
		}else{
			yield;
		}
	}
}

sub TMovement{
	let resultx=rand(0,1);
	resultx=round(resultx);
	let resulty=rand(0,1);
	resulty=round(resulty);
	let posx=rand(100,-100);
	if(posx<50&&posx>-50){
		if(resultx==1){
			posx=50;
		}else{
			posx=-50;
		}
	}
	if((ObjMove_GetX(objBoss)+posx)<=50||(ObjMove_GetX(objBoss)+posx)>=(GetStgFrameWidth()-50)){
		posx=-posx;
	}
	let posy=rand(25,-25);
	if(posy<25&&posy>-25){
		if(resulty==1){
			posy=10;
		}else{
			posy=-10;
		}
	}
	if((ObjMove_GetY(objBoss)+posy)<=32||(ObjMove_GetY(objBoss)+posy)>=GetStgFrameHeight()/3){
		posy=-posy;
	}
	ObjMove_SetDestAtWeight(objBoss,ObjMove_GetX(objBoss)+posx,ObjMove_GetY(objBoss)+posy,10,10);
}