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

let objScene=GetEnemyBossSceneObjectID();
let objBoss;
let frame=0;
let count=0;
let turn=0;
let time=0;
let waterpos;
let movesignal=0;
let charge;
let wsignal=0;
let GCSD=GetCurrentScriptDirectory~"";
let plusangle=-90;

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(2500)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(50)
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(10000)
	}
}

@MainLoop{
	frame++;
	plusangle++;
	if(ObjMove_GetSpeed(objBoss)>0&&movesignal==1){
		wsignal=1;
	}else{
		wsignal=0;
	}
	if(wsignal!=1){
		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,150);
	wait(30);
	PlaySound(objBoss,23,100,0);
	ObjEnemyBossScene_StartSpell(objScene);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/3,10,5);
	Cutin(objBoss,"Liquid Salt \"Artificial Brine Current -Dense-\"");
	TMovement;
	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 TWater{
	waterpos=TWaterHide(objBoss,"LARGE",true);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0);
	while(!Obj_IsDeleted(waterpos)&&wsignal==1){
		ObjRender_SetPosition(waterpos,ObjRender_GetX(objBoss),ObjRender_GetY(objBoss),0);
		yield;
	}
	Obj_Delete(waterpos);
	ObjRender_SetAlpha(objBoss,255);
	loop(5){
		TCallBubbles(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss));
	}
	while(wsignal!=1){ yield; }
	TWater;
}

task TMovement{
	wait(60);
	if(turn==0){
		ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth-100,100,15,3.5);
	}
	if(turn==1){
		ObjMove_SetDestAtWeight(objBoss,100,GetStgFrameHeight-100,15,3.5);
	}
	if(turn==2){
		ObjMove_SetDestAtWeight(objBoss,100,100,15,3.5);
	}
	if(turn==3){
		ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth-100,GetStgFrameHeight-100,15,3.5);
	}
	TShoot01;
	turn++;
	if(turn==4){
		turn=0;
	}
}

task TShoot01{
	let count=0;
	let laserc=0;
	let stangle;
	let angle;
	if(turn==0){
		stangle=135;
		angle=180;
	}
	if(turn==1){
		stangle=315;
		angle=0;
	}
	if(turn==2){
		stangle=45;
		angle=0;
	}
	if(turn==3){
		stangle=225;
		angle=180;
	}
	let spin=0;
	let speed=[0.25,0.5,0.75];
	let graph=1;
	let radius=100;
	let t=7;
	while(ObjMove_GetSpeed(objBoss)>0){
		if(time!=0){
			let angle=0;
			while(angle<360){
				ascent(i in 0..length(speed)){
					let g;
					if(graph==1){
						g=71;
					}else{
						g=69;
					}
					CreateShotA1(ObjMove_GetX(objBoss)+radius*cos(spin*t+angle),ObjMove_GetY(objBoss)+radius*sin(spin*t+angle),speed[i],spin*t+angle+rand(-135,135),g,15);
				}
				angle+=360/3;
			}
			graph=-graph;
			radius-=2.55;
			spin++;
			PlaySound(objBoss,12,100,0);
		}
		wait(4);
	}
	let gap=5;
	let s=7;
	loop(7){
		let angle2=-gap;
		while(angle2<gap){
			let angle3=0;
			while(angle3<360){
				CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0,GetAngleToPlayer(objBoss)+angle2+2.5+angle3,0.1,s,246,15);
				angle3+=360/4;
			}
			angle2+=5;
		}
		gap+=2.5;
		s-=0.75;
	}
	time++;
	TCheckHp(objBoss);
	PlaySound(objBoss,22,100,0);
	let charge=TCharge01(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),3,255,255,255);
	ObjMove_SetAngle(objBoss,angle);
	wait(60);
	let a=GetAngleToPlayer(objBoss);
	while(!Obj_IsDeleted(objBoss)&&count!=150){
		if(laserc==0){
			if(turn==0||turn==1){
			let obj=CreateStraightLaserA1(ObjMove_GetX(objBoss)-15,ObjMove_GetY(objBoss),a,700,16,120,ST_LASER_BLUE,0);
			ObjShot_SetIntersectionEnable(obj,false);
			PlaySound(objBoss,2,100,0);
			}
			if(turn==2||turn==3){
			let obj=CreateStraightLaserA1(ObjMove_GetX(objBoss)+15,ObjMove_GetY(objBoss),a,700,16,120,ST_LASER_BLUE,0);
			ObjShot_SetIntersectionEnable(obj,false);
			PlaySound(objBoss,2,100,0);
			}
			PlaySound(objBoss,22,100,0);
			charge=TCharge01(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),3,255,255,255);
			wait(60);
			laserc++;
		}
	if(turn==0){
		let shot=CreateLooseLaserA1(ObjMove_GetX(objBoss)-15,ObjMove_GetY(objBoss)+2,15,a+rand(-5,5),0,0,G_LARGE_BLUE,5);
		PlaySound(objBoss,9,100,0);
		TIncrease(shot);
	}
	if(turn==1){
		let shot=CreateLooseLaserA1(ObjMove_GetX(objBoss)-15,ObjMove_GetY(objBoss)+2,15,a+rand(-5,5),0,0,G_LARGE_BLUE,5);
		PlaySound(objBoss,9,100,0);
		TIncrease(shot);
	}
	if(turn==2){
		let shot=CreateLooseLaserA1(ObjMove_GetX(objBoss)+15,ObjMove_GetY(objBoss)+2,15,a+rand(-5,5),0,0,G_LARGE_BLUE,5);
		PlaySound(objBoss,9,100,0);
		TIncrease(shot);
	}
	if(turn==3){
		let shot=CreateLooseLaserA1(ObjMove_GetX(objBoss)+15,ObjMove_GetY(objBoss)+2,15,a+rand(-5,5),0,0,G_LARGE_BLUE,5);
		PlaySound(objBoss,9,100,0);
		TIncrease(shot);
	}
		count+=1.5;
		wait(1);
	}
	TMovement;
	task TIncrease(obj){
		let w=0;
		let l=0;
		loop(20){
			ObjLaser_SetLength(obj,l);
			ObjLaser_SetRenderWidth(obj,w);
			ObjLaser_SetIntersectionWidth(obj,w/1.5);
			w+=5;
			l+=10;
			yield;
		}
	}
}