#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 65;
life = 1850;

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

	TEnemyWork;
	ObjEnemy_Render(objEnemy);
}

task TEnemyWork
{
	SetSpellBack(SHOT_BLUE);
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-105,20,5);
	loop(60){yield;}
	TLife(180,20,32,33,33,false);

	let spread = [100,2+Dif*2,6,25,1+Dif,7,8];
	let dir = 1;
	EnemySpell = TYPE_ICE;
	EnemyWave = true;
	TSpin;
	loop
		{
		let aM = GetAngleToPlayer(objEnemy)+dir*90+dir*75/spread[2];
		let g = BALL_O;
		let n = -0.25;
		let c = floor(rand(0,spread[6]));
		descent(i0 in 0..spread[0])
			{
			PlaySound("se_Shot1",55);
			aM+=dir*(45/spread[2])/spread[0];
			if(i0 <= spread[0]*0.5)
				{
				n+=0.25;
				g = ICE;
				}
			if(i0 >= 7 && i0 <= spread[0]*0.5-27)
				{
				EnemyWave = false;
				}
			else
				{
				EnemyWave = true;
				}
			c+=1;
			ascent(i1 in 0..spread[1])
				{
				let d = ((1+i1)*64/spread[1])*(1-i0/spread[0]);
				ascent(i2 in 0..spread[2])
					{
					let s = (8+i1*8/spread[1]+rand(0,1/40))*i0/spread[0];
					let sM = 4+rand(0,1/80);
					let graph = SNOW;
					let v = dir*90/(GetStgFrameWidth/sM);
					let dirA = dir;
					if((i2*2+i1)%spread[2] <= floor(n))
						{
						graph = g;
						sM+=1;
						v=-dir*90/(GetStgFrameWidth/sM);
						dirA*=-1;
						}
					let a = (i2+dirA*i1*0.51)*360/spread[2]+aM;
					let shot = CreateBulletA2(enmx+d*cos(a),enmy+d*sin(a),s,a,(sM-s)/15,v,sM,graph,8,1,SHOT_BLUE);
					Obj_SetRenderPriority(shot,0.49);
					}
				}
			loop(5){yield;}
			}
		loop(20){yield;}
		dir*=-1;
		TSpam;
		}

	task TSpin
		{
		loop(5*30){yield;}
		let aM;
		let dirA = -dir;
		loop
			{
			PlaySound("se_Shot4",65);
			if(dirA != dir)
				{
				aM = rand(0,360);
				dirA = dir;
				}
			ascent(i3 in 0..spread[3])
				{
				let s = 16+rand(0,1/40);
				ascent(i4 in 0..spread[4])
					{
					let a = (i3-dir*i4*0.25)*360/spread[3]+aM;
					let sM = 2.75-i4*0.58+rand(0,1/80);
					let shot = CreateBulletA2(enmx,enmy,s,a,(sM-s)/15,dir*90/(GetStgFrameWidth/sM),sM,[BALL_O,BALL_S][min(i4,1)],8,1,SHOT_BLUE);
					Obj_SetRenderPriority(shot,0.48);
					}
				}
			loop(45){yield;}
			aM+=dir*3;
			}
		}
	task TSpam
		{
		loop(15){yield;}
		let time = spread[0];
		let tA = 8-ceil(Dif/2)*2;
		descent(t in 0..time)
			{
			if(t%tA == 0)
				{
				PlaySound("se_Shot3",50);
				ascent(i2 in 0..spread[2])
					{
					let s = 4+rand(0,1/40);
					let a = (i2+rand(0,1))*360/spread[2];
					let sM = rand(1.25,2.25)+t*0.5/time+rand(0,1/80);
					let shot = CreateBulletA2(enmx,enmy,s,a,(sM-s)/15,-dir*22.5/(GetStgFrameWidth/sM),sM,ICE,8,1,SHOT_PURPLE);
					Obj_SetRenderPriority(shot,0.52);
					}	
				}
			else if(t%tA == tA*0.5)
				{
				PlaySound("se_Shot3",50);
				ascent(i2 in 0..spread[2])
					{
					let s = 4+rand(0,1/40);
					let a = (i2+rand(0,1))*360/spread[2];
					let sM = rand(1.75,2.25)+t*0.5/time+rand(0,1/80);
					let shot = CreateBulletA2(enmx,enmy,s,a,(sM-s)/15,0,sM,SNOW,8,1,SHOT_BLUE);
					Obj_SetRenderPriority(shot,0.51);
					}	
				}
			yield;
			}
		}
}