#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 120;
let lifeM = 2000;
life = lifeM;

@Initialize
{
	ObjShot_DeleteAll(TARGET_ENEMY);

	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);

	TEnemyWork;
	ObjEnemy_Render(objEnemy);
}

task TEnemyWork
{
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-75,20,5);
	loop(60){yield;}
	TPhase1(0);
	while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) >= lifeM*0.75){yield;}
	loop(30){yield;}
	TPhase2(0);
	task TPhase1(r)
		{
		let spread = [5,3+Dif,32+Dif*10];
		let dM = [100];
		dM = dM ~ [GetDistance(dM[0]*cos(0),dM[0]*sin(0),dM[0]*cos(72*2),dM[0]*sin(72*2))];
		let sM = 4;
		let dir = -1;
		let aP = 90;
		let time = 30;
		let aC = 10;
	
		TLife(time*4,20,32,100,100,false);
		local
			{
			let timeA = 1+2^(2-Dif);
			ascent(t in 0..time*4)
				{
				let d = min(time,t)*dM[1]/time;
				if(t%timeA == 0)
					{
					ascent(i0 in 0..spread[0])
						{
						let a = i0*360/spread[0]+aP+180;
						let x = enmx+dM[0]*cos(a)+d*cos(a+dir*72*2.25);
						let y = enmy+dM[0]*sin(a)+d*sin(a+dir*72*2.25);
						ascent(i1 in 0..spread[1])
							{	
							let aA = a+180+dir*72*2.25;
							if(t >= 30)
								{
								aA += dir*(t-time)*(360+13)/time+dir*i1*((t-time)*aC/(time*2))/spread[1];
								}
							let s = i1*sM*0.125/spread[1]+(time-min(time,t))*sM*0.875/time;
							let shot = CreateBulletA2(x,y,s,aA,sM/(120-Dif*30+t),0,sM,BALL_M,8,1,[SHOT_PURPLE,SHOT_YELLOW,SHOT_GREEN,SHOT_BLUE][i1%4]);
							ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
							ObjRender_SetColorHSV(shot,[300,60,120,240][i1%4],125,255);
							}
						}
					}
				wait(1);
				}	
			}
		loop(60){wait(1);}
		let aB = 0;
		let c = 0;
		while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) >= lifeM*r)
			{
			ascent(i0 in 0..spread[0])
				{
				loop(5-Dif*2)
					{	
					c+=1;
					if(c%180 == 0)
						{
						ObjEnemy_Move(objEnemy,30,1,30,rand(90,120),100,2);
						}
					if(c >= 150 && (c-150)%120 == 0)
						{
						ascent(i in 0..spread[2])
							{
							let shot = CreateBulletA2(enmx,enmy,1,i*360/spread[2]+GetAngleToPlayer(objEnemy),1/30,0,10,BALL_M,8,1,[SHOT_YELLOW,SHOT_BLUE,SHOT_RED][Dif]);
							ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
							ObjRender_SetColorHSV(shot,[60,240,0][Dif],125,255);
							}
						}
					wait(1);
					}
				dir*=-1;
				let a = i0*360/spread[0]+aP+180;
				let x = enmx+dM[0]*cos(a)+dM[1]*cos(a+dir*72*2.25);
				let y = enmy+dM[0]*sin(a)+dM[1]*sin(a+dir*72*2.25);
				ascent(i1 in 0..spread[1])
					{	
					let aA = a+i0*360/spread[0]+aB+180+dir*72*2.25+dir*i1*aC/spread[1];
					let s = i1*sM*0.125/spread[1];
					let shot = CreateBulletA2(x,y,s,aA,sM/(120-Dif*30+time*2),0,sM,BALL_M,8,1,[SHOT_PURPLE,SHOT_YELLOW,SHOT_GREEN,SHOT_BLUE][i1%4]);
					ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
					ObjRender_SetColorHSV(shot,[300,60,120,240][i1%4],125,255);
					}
				aB-=3.5;
				aP+=0.75;
				}
			}
		
		function wait(time)
			{
			loop(time)
				{
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) < lifeM*r)
				{
				break;
				}
				yield;
				}
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) < lifeM*r)
				{
				break;
				}
			}
		}
	task TPhase2(r)
		{
		let sA = 0;
		TSpam(9-Dif*2,0);
		while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) >= lifeM*0.45){yield;}
		TSpam(6+Dif,1);
		loop(90){yield;}
		while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) >= lifeM*0.2){yield;}
		let dir = 1;
		let spread = 9;
		let c = -1;
		while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) >= lifeM*r)
			{
			ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*(0.5+rand(0,0.2)*cos(dir)+rand(-0.1,0.1)*sin(dir)),rand(90,135),15,6);
			loop(90)
				{
				c+=1;
				if(c%105 == 0)
					{
					TShot;
					}
				wait(1);
				}
			dir+=90;
			}
		task TSpam(count,n)
			{
			let dir = 1;
			let aM = -2;
			let c = -1;
			let aA = 0;
			let dirM = 1;
			while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) >= lifeM*r)
				{
				if(aM >= 1)
					{
					aM = 1;
					dir = -1;
					dirM*=-1;
					}
				else if(aM <= -1)
					{
					aM = -1;
					dir = 1;
					dirM*=-1;
					}
				aA+=[5,3][n];
				c+=1;
				aM+=dir*1/20;
				if(c%count == 0)
					{
					TShot(aM);
					}
				wait(1);
				}
			task TShot(aM)
				{
				let aP = GetAngleToPlayer(objEnemy)+aM*105;
				let x = enmx+24*cos(aP);
				let y = enmy+24*sin(aP);
				let spread = 4+Dif*(1+n)-n;	
				let dir = dirM;
				ascent(t in 0..spread)
					{
					let s = [2.5,1.5][n]+t*[1,1.5+Dif*0.5][n]/6+sA;
					let a = aP+dir*n*t*5;
					let shot = CreateBulletA2(x,y,2,a,(s-2)/60,-dir*(n*2-1)*[67.5,0][n]/(GetStgFrameWidth/s),s,[BEAM,BALL_M][n],8,1,[SHOT_GREEN,[SHOT_PURPLE,SHOT_YELLOW][t%2]][n]);
					ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
					ObjRender_SetColorHSV(shot,[120,[300,60][t%2]][n],125,255);
					loop(3){wait(1);}
					}
				}
			}
		task TShot
			{
			let x = GetPlayerX;
			let y = GetPlayerY;
			ascent(t in 0..7)
				{
				ascent(i in 0..spread)
					{
					i = i-spread*0.5+0.5;
					let a = atan2(y-enmy,x-enmx)+i*(180-Dif*45)/spread;
					let shot = CreateBulletA2(enmx+16*cos(a),enmy+16*sin(a),0.75+sA,a,1/35,0,10,BEAM,8,1,[SHOT_GREEN,SHOT_YELLOW,SHOT_PURPLE][t%3]);
					ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
					ObjRender_SetColorHSV(shot,[120,60,300][t%3],125,255);
					}
				loop(3){wait(1);}
				}
			
			}
		
		function wait(time)
			{
			loop(time)
				{
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) < lifeM*r)
				{
				break;
				}
				yield;
				}
			if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) < lifeM*r)
				{
				break;
				}
			}
		}
}