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

let objScene=GetEnemyBossSceneObjectID();
let objBoss;
let frame=0;
let sAngle=0;
let lBullets=[];
let GCSD=GetCurrentScriptDirectory~"";
let plusangle=-90;

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(4000)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(105)
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(10000)
	}
}

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

task mainTask{
	TLoadSounds;
	objBoss=TDrawBoss;
	TEnemyHitboxStart(objBoss,480);
	wait(30);
	PlaySound(objBoss,23,100,0);
	ObjEnemyBossScene_StartSpell(objScene);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/3,10,5);
	Cutin(objBoss,"Untold \"Sea's Transparent Antiprisim\"");
	TShoot01;
	TEnd;
	yield;
}

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 TDelete{
	loop(120){
		DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
		yield;
	}
}

task TShoot01{
	wait(60);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/2,10,5);
	while(ObjMove_GetSpeed(objBoss)>0){ yield; }
	PlaySound(objBoss,22,100,0);
	TCharge01(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),3,255,255,255);
	wait(90);
	let angle=0;
	while(angle<360){
		let obj=CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0,angle,0.1,8,320,5);
		ObjShot_SetSpellResist(obj,true);
		lBullets=lBullets~[obj];
		TStop(obj);
		angle+=360/8;
		PlaySound(objBoss,12,100,0);
		wait(3);
	}
	TLasers;
}

task TStop(obj){
	while(!Obj_IsDeleted(obj)){
		if(ObjMove_GetX(obj)<=0||ObjMove_GetX(obj)>=GetStgFrameWidth||ObjMove_GetY(obj)<=0||ObjMove_GetY(obj)>=GetStgFrameHeight){
			ObjMove_SetSpeed(obj,0);
			ObjMove_SetAcceleration(obj,0);
			TExplosionA(ObjMove_GetX(obj),ObjMove_GetY(obj),25,0.6);
			PlaySound(objBoss,13,100,0);
			break;
		}
		yield;
	}
}

task TLasers{
	let num=3;
	ascent(i in 0..length(lBullets)){
		while(ObjMove_GetSpeed(lBullets[i])>0){
			yield;
		}
	}
	wait(15);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/3,10,5);
	wait(30);
	ascent(i in 0..length(lBullets)){
		let angle=0;
		while(angle<360){
			let obj=TCreateLaser(ObjMove_GetX(lBullets[i]),ObjMove_GetY(lBullets[i]),angle,10,9,60);
			TAngle2(obj,angle,75);
			PlaySound(objBoss,1,100,0);
			PlaySound(objBoss,2,100,60);
			angle+=360/num;
		}
	}
	TMovement;
	wait(75);
	TShoot02;
	TShoot03;
	TShoot04;
	TShoot05;
	TShoot06;
	sAngle=0;
	wait(15);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		ascent(i in 0..length(lBullets)){
			let angle=0;
			while(angle<360){
				let obj=CreateStraightLaserA1(ObjMove_GetX(lBullets[i]),ObjMove_GetY(lBullets[i]),sAngle+angle,650,10,30,9,60);
				PlaySound(objBoss,2,100,60);
				TAngle(obj,angle);
				angle+=360/num;
			}
		}
		wait(90);
	}
}

task TShoot02{
	let speed=2;
	while(!Obj_IsDeleted(objBoss)){
		loop(3){
			let angleplayer=GetAngleToPlayer(objBoss);
			loop(3){
				let angle=-5;
				while(angle<=5){
					CreateShotA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),speed,angle+angleplayer,39,5);
					angle+=5;
				}
				PlaySound(objBoss,13,100,0);
				wait(3);
			}
			wait(10);
			if(speed<2.75){
				speed+=0.25;
			}
		}
		wait(90);
	}
}

task TAngle(obj,p){
	while(!Obj_IsDeleted(obj)){
		ObjStLaser_SetAngle(obj,sAngle/7+p);
		yield;
	}
}

task TAngle2(obj,p,w){
	wait(w);
	sAngle=0;
	while(!Obj_IsDeleted(obj)){
		ObjStLaser_SetAngle(obj,sAngle/7+p);
		yield;
	}
}

function TCreateLaser(x,y,dir,w,g,d){
	let length=0;
	let obj=CreateStraightLaserA1(x,y,dir,0,w,30,g,d);
	T;
	return obj;
	task T{
		loop(755){
			ObjLaser_SetLength(obj,length);
			length+=650/75;
			yield;
		}
	}
}

task TShoot03{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>3250&&ObjEnemyBossScene_GetInfo(objScene,INFO_TIMER)>46){ yield; }
	while(!Obj_IsDeleted(objBoss)){
		let a1=[];
		let angle=0;
		while(angle<360){
			let obj=CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),3.5,angle,-0.125,0,249,15);
			a1=a1~[obj];
			angle+=360/10;
		}
		PlaySound(objBoss,11,100,0);
		ascent(i in 0..length(a1)){
			TSpeed(a1[i]);
		}
		wait(135);
	}
	task TSpeed(obj){
		let signal=1;
		let x=ObjMove_GetX(objBoss);
		let y=ObjMove_GetY(objBoss);
		while(ObjMove_GetSpeed(obj)>0){ yield; }
		signal=0;
		ObjMove_SetAngularVelocity(obj,0);
		let length=GetGapLength(x,y,ObjMove_GetX(objPlayer),ObjMove_GetY(objPlayer));
		let f=truncate(length/(4.4+0.8/20));
		x=ObjMove_GetX(obj);
		y=ObjMove_GetY(obj);
		let speed=GetGapLength(x,y,ObjMove_GetX(objPlayer),ObjMove_GetY(objPlayer))/f;
		let angle=GetGapAngle(x,y,ObjMove_GetX(objPlayer),ObjMove_GetY(objPlayer));
		ObjMove_SetSpeed(obj,speed);
		ObjMove_SetAngle(obj,angle);
		ObjMove_SetAcceleration(obj,0);
	}
}

task TShoot04{
	let s=[1.5,1.75,2,2.25,2.5];
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>2500&&ObjEnemyBossScene_GetInfo(objScene,INFO_TIMER)>19){ yield; }
	while(!Obj_IsDeleted(objBoss)){
		let angle=0;
		while(angle<360){
			ascent(i in 0..length(s)){
				CreateShotA1(ObjMove_GetX(objBoss)+(s[i]*25)*cos(angle),ObjMove_GetY(objBoss)+(s[i]*25)*sin(angle),s[i],180+angle,67,15);
			}
			angle+=360/10;
		}
		PlaySound(objBoss,14,100,0);
		wait(150);
	}
}

task TShoot05{
	let spin=0;
	let speed=[2.5,2.6,2.7,2.8,2.9,3];
	let turn=1;
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>1500&&ObjEnemyBossScene_GetInfo(objScene,INFO_TIMER)>6){ yield; }
	while(!Obj_IsDeleted(objBoss)){
		let angle=0;
		while(angle<360){
			ascent(i in 0..length(speed)){
				let obj=CreateShotA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0.75,spin*7.11+angle,83,5);
				if(turn==1){
					ObjMove_AddPatternA1(obj,90,speed[i],ObjMove_GetAngle(obj)+90);
				}else{
					ObjMove_AddPatternA1(obj,90,speed[i],ObjMove_GetAngle(obj)-90);
				}
			}
			angle+=360/4;
		}
		spin++;
		turn=-turn;
		wait(10);
	}
}

task TShoot06{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>750&&ObjEnemyBossScene_GetInfo(objScene,INFO_TIMER)>6){ yield; }
	while(!Obj_IsDeleted(objBoss)){
		CreateShotA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),rand(0.5,2),rand(0,360),round(rand(119,132)),5);
		wait(2);
	}
}

function GetGapLength(xA,yA,xB,yB){
	return((xB-xA)^2+(yB-yA)^2)^0.5;
}

function GetGapAngle(xA,yA,xB,yB){
	return atan2(yB-yA,xB-xA);
}

function GravityShot(x,y,speed,angle,grvSpeed,grvAngle,graphic,delay){
	let xSpeed = speed * cos( angle );
	let ySpeed = speed * sin( angle );
	let accXSpeed = grvSpeed * cos( grvAngle );
	let accYSpeed = grvSpeed * sin( grvAngle );
	let maxXSpeed = [ 99, - 99 ][ accXSpeed < 0 ];
	let maxYSpeed = [ 99, - 99 ][ accYSpeed < 0 ];
	let gs=CreateShotB2(x,y,xSpeed,ySpeed,accXSpeed,accYSpeed,maxXSpeed,maxYSpeed,graphic,delay);
	return gs;
}

task TMovement{
	wait(120);
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){
		let resultx=rand(0,1);
		resultx=round(resultx);
		let resulty=rand(0,1);
		resulty=round(resulty);
		let posx=rand(75,-75);
		if(posx<=15&&posx>=-15){
			if(resultx==1){
				posx=20;
			}else{
				posx=-20;
			}
		}
		if((ObjMove_GetX(objBoss)+posx)<=32||(ObjMove_GetX(objBoss)+posx)>=(GetStgFrameWidth()-32)){
			posx=-posx;
		}
		
		let posy=rand(25,-25);
		if(posy<=10&&posy>=-10){
			if(resulty==1){
				posy=15;
			}else{
				posy=-15;
			}
		}
		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,5);
		wait(90);
	}
}