#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 50;
life = 3000;

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

	TEnemyWork;
	ObjEnemy_Render(objEnemy);
}

task TEnemyWork
{
	SetSpellBack(SHOT_RED);
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-135,20,5);
	loop(45){yield;}
	TLife(240,20,32,52.5,52.5,false);
	
	let spreadM = 4;
	let spread = 3;
	let dir = trunc(rand(0,2))*2-1;
	let time = [45,45,360];
	EnemySpell = TYPE_FIRE;
	TSpam1;
	let c = -1;
	loop
		{
		descent(t0 in 0..2)
			{
			descent(n in 0..2)
				{
				if(t0 == 0 && n == 0)
					{
					dir *= -1;
					}
				ascent(t in 0..time[1])
					{
					if(n == 1)
						{
						t = time[1]-t;
						}	
					if(t >= floor(time[1]*0.4) && (t0 == 1 || n == 1) && t%((Dif+1)%2+1) == 0)
						{
						let a = GetAngleToPlayer(objEnemy)+dir*(-90+(t-floor(time[1]*0.4))*180/ceil(time[1]*0.6));
						let s = 1.75+rand(0,1);
						ascent(i in 0..1)
							{
							CreateBulletA1(enmx,enmy,s*(1-i*1/20),a,BALL_M,8,1.125,SHOT_RED);
							}
						}
					ascent(dir in 0..2)
						{
						dir = dir*2-1;
						ascent(i in 0..5)
							{
							let d = (5-i)*4;
							let a = GetAngleToPlayer(objEnemy)+dir*(11.5-floor(Dif/2)*10.25+i*(10+floor(Dif/2)*10)/4+(t+(n*2-1)*rand(0,1))*165/time[1]);
							let sA = i+rand(0,1);
							let s = 3+sA*2/5;
							let sM = 2.75-floor(Dif/2)*1.5+sA*(3.75+floor(Dif/2)*1.25)/5;
							let shot = CreateBulletA2(enmx+d*cos(a),enmy+d*sin(a),s,a,(sM-s)/75,0,sM,FIRE,0,1,SHOT_RED);
							if(i == 3 || i == 0)	
								{
								ObjShot_SetGraphic(shot,FIRE[SHOT_YELLOW]);
								}
							}
						}
					yield;
					}
				}
			ObjEnemy_Move(objEnemy,20,3,50+t0*50,rand(120,135),100,2);
			}
		}
	function TSpam1
		{
		let dir = trunc(rand(0,2))*2-1;
		let xM = enmx;
		let yM = enmy;
		let aP = 90;
		let aA = 0;
		ascent(t in 0..time[0]+20)
			{
			if(t == time[0])
				{
				ObjEnemy_Move(objEnemy,20,3,60,rand(120,135),100,2);
				}
			let spread = spreadM+floor(max(0,spreadM-t*spreadM*4/time[0]));
			let aM = aP+dir*(t*60/time[0]);
			let dM = t*120/time[0];
			let d = t*16/time[0];
			let x = xM+dM*cos(aM);
			let y = yM+dM*sin(aM);
			if(t < 8 || (t%(9-Dif*3) == 0 && t < time[0]+20-10))
				{
				let a = 0;
				if(t >= time[0]-20)
					{
					a = rand(-1,1)*10;
					}	
				PlaySound("se_Shot1",80);
				let shot = CreateBulletA2(enmx,enmy,6,GetAngleToPlayer(objEnemy)+a,-6/120,0,1,BALL_L,8,1,SHOT_BLUE);
				Obj_SetRenderPriority(shot,0.49);
				}
			aA+=(480/spread)/(time[0]-1);
			ascent(i in 0..spread)
				{
				let a = i*360/spread+aP-dir*135/spread+dir*aA;
				if(t >= time[0])
					{
					a+=dir*(t-time[0])*30/20;
					}
				let s = (0.875+(time[0]-min(time[0],t))*4.125/time[0])+rand(0,1/40);
				ascent(n in 0..2)
					{
					CreateBulletA1(x+d*cos(a),y+d*sin(a),s*(1-n*1/32),a,FIRE,8,1,SHOT_RED);
					}
				}
			dir*=-1;
			loop(max(3,35-t*4)){yield;}
			}
		TSpam2;
		task TSpam2
			{
			let dM = (time[0]+20)*120/time[0];
			let d = (time[0]+20)*16/time[0];
			let c = -1;
			loop
				{
				let aM = aP+dir*(time[0]+20)*60/time[0];
				let x = xM+dM*cos(aM);
				let y = yM+dM*sin(aM);
				let spread = floor(spreadM*(1+min(c,150)*2/150));
	
				aA+=((540-Dif*60)/spreadM)/(time[0]-1);
				ascent(i in 0..spread)
					{
					let a = i*360/spread+dir*(20)*30/20+aP-dir*135/spread+dir*aA;
					let s = 0.875+min(c,150*150/210)*1.75/150+rand(0,1/40);
					ascent(n in 0..2)
						{
						CreateBulletA1(x+d*cos(a),y+d*sin(a),s*(1-n*1/32),a,FIRE,8,1,SHOT_RED);
						}
					}
				dir*=-1;
				c+=1;
				if(c%9 == 0)
					{
					PlaySound("se_Shot1",80);
					CreateBulletA2(enmx,enmy,6,GetAngleToPlayer(objEnemy),-6/120,0,1,BALL_L,8,1,SHOT_BLUE);
					}
				loop(14-(Dif%2)*3){yield;}
				}
			}
		}
}            