#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 87;
life = 8000;

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

	TEnemyWork;
	ObjEnemy_Render(objEnemy);
}

task TEnemyWork
{
	EnemySpell = TYPE_GLOW;
	EnemyWave = true;	
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-135,20,5);
	loop(60){yield;}
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5,20,5);
	TLife(0,20,32,0,0,false);
	let spread = [8,[12,16,14][Dif],25+ceil(Dif/2)*4,1+Dif,7+Dif,3+Dif,24+floor(Dif/2)*16,26+(Dif%2)*10];

	SetSpellBack(SHOT_PURPLE);

	let dir = 1;
	let aM = 1/900;
	let aA = 0;
	let aB = 0.67;
	let c = 90;
	let time = 700;
	let nM = 0;
	let dirA = 1;
	while(nM < 5)
		{
		c+=1;
		aA+=3;
		if(aM <= 0)
			{
			aM = 1/900;
			dir = 1;
			nM+=1;
			c %= 6;
			c+=60;
			}
		else if(aM >= 1)
			{
			aM = 1-1/900;
			dir = -1;
			nM+=1;
			c %= 6;
			c+=60;
			}
		if(c >= 390)
			{
			aM+=dir/time;
			}
		if(c%6 == 0)
			{
			aB += (1-aB)/10;
			ascent(i in 0..spread[0])
				{
				i = i-spread[0]*0.5+0.5;
				ascent(n in 0..2)
					{
					let a = 270+aM*360+(i+0.125*sin(aA+n*180))*330*aB/spread[0];
					let shot = CreateBulletA2(enmx,enmy,2,a+rand(-1,1)*0.25,1/20,0,10,BALL_M,8,1,[SHOT_RED,SHOT_YELLOW,SHOT_PURPLE,SHOT_GREEN,SHOT_BLUE][nM%5]);
					ObjShot_SetSpellResist(shot,true);
					}
				}
			PlaySound("se_Shot4",35);
			}
		if(nM == 1 && c < 67)
			{
			SetSpellBack(SHOT_X+1);
			}
		else if(nM == 1 && c == 127)
			{
			SetSpellBack(SHOT_YELLOW);
			}
		if(nM == 0 && c >= 135 && c%(65+ceil(Dif/2)*25) == 0)
			{
			TShot1(dirA);
			dirA*=-1;
			}
		else if(nM == 1 && c >= 180 && c%75 == 0)
			{
			PlaySound("se_Shot1",80);
			ascent(i in 0..spread[1])
				{
				ascent(n in 0..2)
					{
					ascent(n1 in 0..1+Dif*2)
						{
						n1 = n1-(1+Dif*2)*0.5+0.5;
						let shot = CreateBulletA2(enmx,enmy,4,(i+n1*0.1+n*0.5)*360/spread[1]+90,-3/40,0,0.875-n*4.125,BALL_M,8,1,SHOT_GREEN);
						}
					}
				}	
			}
		else if(nM == 2 && c == 135)
			{
			TShot3;
			}
		else if(nM == 3 && c == 135)
			{
			TShot4;
			}
		else if(nM == 4 && c == 135)
			{
			TShot5;
			}
		yield;
		}
	task TShot1(dir)
		{
		ascent(n in 0..spread[3])
			{
			let aM = rand(0,360);
			ascent(i in 0..spread[2])
				{
				let a = i*360/spread[2]+aM;
				let s = 6+rand(0,0.25);
				let sM = 1.5-n*0.25/spread[3];
				CreateBulletA2(enmx,enmy,s,a,(sM-s)/20,dir*((n%2)*2-1)*135/(GetStgFrameWidth/sM),sM,BALL_M,8,1,SHOT_PURPLE);
				}	
			PlaySound("se_Shot1",80);
			loop((45-ceil(Dif/2)*21)/spread[3]){yield;}
			}
		}
	task TShot3
		{
		SetSpellBack(SHOT_X+1);
		let aM = 90;
		let c = -1;
		while(nM == 2)
			{
			c+=1;
			if(c == 60)
				{
				SetSpellBack(SHOT_RED);
				}
			if(c%5 == 0 && floor(c/5)%10 == 0)
				{
				TSpam(aM);
				aM+=180/spread[4];
				}
			yield;
			}
		task TSpam(a)
			{
			PlaySound("se_Shot1",80);
			descent(t in 0..spread[5])
				{
				let s = 0.75+t*(0.5+Dif*0.25)/spread[5];
				ascent(i in 0..spread[4])
					{
					CreateBulletA2(enmx,enmy,4,i*360/spread[4]+a,(s-4)/30,0,s,BALL_M,8,1,SHOT_RED);
					}
				loop(5){yield;}
				}
			}
		}
	task TShot4
		{
		SetSpellBack(SHOT_X+1);
		let c = -1;
		let t = 5+ceil(Dif/2)*3+floor(Dif/2);
		let aM = 90;
		while(nM == 3)
			{
			c+=1;
			if(c == 60)
				{
				SetSpellBack(SHOT_GREEN);
				}
			if(c%t == 0 && floor(c/t)%(14-ceil(Dif/2)*5) == 0)
				{
				TSpam(aM);
				aM+=180/spread[6];
				}
			yield;
			}
		task TSpam(a)
			{
			loop(6)
				{
				descent(i in 0..spread[6])
					{
					CreateBulletA2(enmx,enmy,1,i*360/spread[6]+a,1/30,0,2,BALL_M,8,1,SHOT_YELLOW);
					}
				PlaySound("se_Shot1",50);
				loop(t){yield;}
				}
			}
		}
	task TShot5
		{
		SetSpellBack(SHOT_X+1);
		let c = -1;
		let t = 15;
		let a = 90;
		while(nM == 4)
			{
			c+=1;
			if(c == 60)
				{
				SetSpellBack(SHOT_X);
				}
			if(c%t == 0 && floor(c/t)%(10-Dif*2) == 0)
				{
				TSpam;
				}
			yield;
			}
		task TSpam
			{
			loop(6)
				{
				descent(i in 0..spread[7])
					{
					CreateBulletA1(enmx,enmy,1.5,i*360/spread[7]+a,BALL_M,8,1,SHOT_BLUE);
					}
				PlaySound("se_Shot1",60);
				loop(t){yield;}
				a+=180/spread[7];
				}
			}
		}
}