#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 150*60;
let lifeM = 5000/spellLife;
let life = lifeM;

@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	bspell = true;

	TEnemyStart;
	ObjEnemy_Render;
	TLife(225,30,20,0,0,22.5,0);
}

@MainLoop
{
	life = ObjEnemy_GetInfo(objEnemy,INFO_LIFE);
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(lifeM);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(200);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
}

task TEnemyStart
{
	TXY;
	loop(45){yield;}
	CreateBossAura(0);
	loop(75){yield;}
	TBackground; 
	TSpellName;
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-130,15,2);
	TEnemyWork;
	ObjEnemyBossScene_SetSpellTimer(objScene,timer);
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	TEnd(true);
	loop
		{
		timer = ObjEnemyBossScene_GetInfo(objScene,INFO_TIMERF);
		yield;
		}
}


task TEnemyWork
{
	loop(60){yield;}
	let timerm = 35*60;
	let phase = 0;
	let spreadM = [[2+Dif,1+Dif],[2+Dif,1+Dif],[2,1],[2+Dif,1]];
	let spreadA = [[6,7+Dif],[6,5+Dif],[6,4],[6,7+Dif*2]];
	let color = [240,270,240,345];
	let graph = [BL_SMALL_BLUE,BL_SMALL_AQUA,BL_SMALL_BLUE-1,BL_SMALL_WHITE];

	let dir = 1;
	let counter = -1;
	let count = [125];
	let angleM = 0;

	loop(1)
		{
		while(life > lifeM*0.775 && timer > timerm)
			{
			counter+=1;
			if(counter%count[0] == 0)
				{
				TPattern(rand(0,360),135+trunc(rand(0,90)));
				}
			yield;
			}
		}
	phase+=1;
	loop(1)
		{
		if(timer > timerm)
			{
			ClearScreen;
			loop(45){yield;}
			}
		counter = -1;
		count = [4,14,15];
	
		while(life > lifeM*0.525 && timer > timerm)
			{
			counter+=1;
			if(counter%count[2] == 0 && floor(counter/count[2])%count[1] == 0)
				{
				angleM = rand(0,360);
				}
			if(counter%count[2] == 0 && floor(counter/count[2])%count[1] < count[0])
				{
				TPattern(angleM,180);
				angleM+=(180+rand(-1,1)*90)/spreadA[phase][0];
				}
			yield;
			}
		}
	phase+=1;
	loop(1)
		{
		if(timer > timerm)
			{
			ClearScreen;
			loop(45){yield;}
			}
		counter = -1;
		count = [4+Dif*2,60,3];
	
		while(life > lifeM*0.275 && timer > timerm)
			{
			counter+=1;
			if(counter%count[2] == 0 && floor(counter/count[2])%count[1] == 0)
				{
				dir*=-1;
				angleM = GetAngleToPlayer(objEnemy)-dir*180/(spreadA[phase][0]*spreadA[phase][1]);
				}
			if(counter%count[2] == 0 && floor(counter/count[2])%count[1] < count[0])
				{
				TPattern(angleM,135+trunc(rand(0,90)));
				angleM+=dir*(90/spreadA[phase][0])/5;
				}
			yield;
			}
		}
	phase+=1;
	loop(1)
		{
		ClearScreen;
		loop(45){yield;}

		counter = -1;
		count = [65];
		let angleM = rand(0,360);
	
		while(life > 0)
			{
			counter+=1;
			if(counter%count[0] == 0)
				{
				TPattern(angleM,135+trunc(rand(0,90)));
				angleM+=180/spreadA[phase][0]+1.5;
				}
			if(timer == timerm)
				{
				ClearScreen;
				loop(45){yield;}
				counter = -1;
				count = [50];
				}
			yield;
			}
		}
	




	task TPattern(angleM,timeA)
		{
		PlaySound("se_Shot1",90);
		let spread = spreadA[phase];
		ascent(i0 in 0..spread[0])
			{
			let angle1 = i0*360/spread[0];
			let x1 = cos(angle1);
			let y1 = sin(angle1);
			let x2 = cos(angle1+360/spread[0]);
			let y2 = sin(angle1+360/spread[0]);
			let dist2 = GetDistance(x1,y1,x2,y2);
			let angle2 = atan2(y2-y1,x2-x1);
			ascent(i1 in 0..spread[1])
				{
				let x = x1+(i1/spread[1])*dist2*cos(angle2);
				let y = y1+(i1/spread[1])*dist2*sin(angle2);
				let angle = atan2(y,x)+angleM;
				let speed = GetDistance(0,0,x,y);
				TShot(enmx+speed*32*cos(angle),enmy+speed*32*sin(angle),speed*2.5,angle,timeA,spreadM[phase]);
				}
			}

		}
	task TShot(x,y,speed,angle,timeA,spreadM)
		{
		let shot = CreateBulletA2(x,y,0,angle,speed/90,0,speed,graph[phase],8,1);
		ObjShot_SetSpellResist(shot,true);
		while(!Obj_IsDeleted(shot) && ObjMove_GetX(shot) > -4 && ObjMove_GetX(shot) < GetStgFrameWidth+4 && ObjMove_GetY(shot) > -4){timeA-=1;yield;}
		if(Obj_IsDeleted(shot)){return;}
		x = ObjMove_GetX(shot);
		y = ObjMove_GetY(shot);
		if(y > GetStgFrameHeight+4){return;}
		speed = ObjMove_GetSpeed(shot);
		let angle2 = ObjMove_GetAngle(shot);
		let type = 0;
		if(y <= -4)
			{
			angle = -ObjMove_GetAngle(shot);
			type = 1;
			}
		else	
			{
			angle = 180-ObjMove_GetAngle(shot);
			}
		Obj_Delete(shot);
		let accel = -1/rand(15,50+type*40);	
		PlaySound("se_Shot8",100);
		shot = CreateBulletA1(x,y,0,angle,BL_SMALL_GRAY,8,1);
		ObjShot_SetSpellResist(shot,true);
		while(!Obj_IsDeleted(shot) && speed > 0)
			{
			x+=speed*cos(angle);
			y+=speed*sin(angle);
			speed+=accel;
			ObjMove_SetPosition(shot,x,y);
			timeA-=1;
			yield;
			}
		if(IsBeyondClip(shot,4)){Obj_Delete(shot);return;}
		loop(timeA){yield;if(Obj_IsDeleted(shot)){return;}}
		if(ObjMove_GetY(shot) < GetStgFrameHeight*0.5)
			{
			type = 1;
			}
		if(Obj_IsDeleted(shot)){return;}
		if(type == 1)
			{
			PlaySound("se_Shot4",50);
			}
		ascent(i in 0..spreadM[type])
			{
			let shot2 = CreateBulletA2(ObjMove_GetX(shot),ObjMove_GetY(shot),0,90+rand(-1,1)*3.5,(1+i/16)/(120-type*30),0,1+i*0.5+type*rand(0,0.5)+rand(0,0.25)+(1-type)*100,D_PELLET_GRAY,8,1.125);
			if(type == 0)
				{
				ObjMove_SetAngle(shot2,GetAngleToPlayer(shot2));
				}
			else
				{
				ObjShot_SetGraphic(shot2,PELLET_GRAY);
				ObjRender_SetColorHSV(shot2,color[phase],floor(rand(0,3))*(25+phase*2.5+floor(phase/3)*2.5),255);
				}
			}
		Obj_Delete(shot);
		}
}	

task ClearScreen
{

	let array = GetShotIdInCircleA2(GetStgFrameWidth/2,GetStgFrameHeight/2,GetStgFrameHeight,TARGET_ENEMY);
	ascent(i in 0..length(array))
		{
		let obj = array[i];
		ObjShot_ToItem(obj);
		}
	PlaySound("se_Die",80);
}