#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 75;

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

	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	bspell = true;

	TEnemyWork;
	TBackground;
	ObjEnemy_Render; 
	TLife(490,30,20,0,0,100,0);
	TEnd(false);
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(5000/spellLife);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(timer);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
}

task TEnemyWork
{
	TXY;
	TSpellName;
	loop(105){yield;}
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-65,20,5);
	loop(90){yield;}

	let spread = [5,1+Dif,9+Dif*2];
	let graph = [ARROWHEAD_BLUE,ARROWHEAD_GREEN-1,ARROWHEAD_PURPLE,ARROWHEAD_YELLOW-1,ARROWHEAD_ORANGE];
	let count = [4,4,5];
	let dir = 1;
	
	loop
		{
		ObjMove_SetDestAtWeight(objEnemy,enmx-dir*20,GetStgFrameHeight/2-rand(55,65),40,1);
		loop(20){yield;}
		ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2+dir*80,GetStgFrameHeight/2-rand(75,85),15,3);
		
		let timeA = floor(rand(0,30));
		let g = graph[0];
		ascent(t in 0..30)
			{
			if((t+timeA)%count[0] == 0)
				{
				ascent(i0 in 0..spread[0])
					{
					ascent(i1 in 0..2)
						{
						TShot((spread[0]*0.25+i0*0.75)*3.5/spread[0],270-dir*(-30+t*180/30+(i0-spread[0]*0.5+0.5)*540/(30/count[0])/spread[0])+i1*180,dir,g);
						}
					}
				}
			yield;
			}
		loop(135){yield;}
		let y = enmy;
		ascent(t0 in 0..4)
			{
			let timeA = floor(rand(0,30));
			let dirA = dir*((t0%2)*2-1);
			let x = dir*(80-(t0+1)*160/3);
			let g = graph[t0+1];
			y -= 5+((t0%2)*2-1)*20;
			if(t0 == 3)
				{
				x = 0;
				y = GetStgFrameHeight*0.5-rand(85,95);
				}
			ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2+x,y,15,3);
			ascent(t in 0..30)
				{
				if((t+timeA)%count[1] == 0)
					{
					ascent(i0 in 0..spread[0])
						{
						ascent(i1 in 0..2)
							{
							TShot((spread[0]*0.25+i0*0.75)*3.5/spread[0],270-dirA*(-30+t*180/30+(i0-spread[0]*0.5+0.5)*540/(30/count[1])/spread[0])+i1*180,dirA,g);
							}
						}
					}
				yield;
				}
			if(t0 == 3)
				{
				loop(40){yield;}
				}
			else
				{
				loop(70-Dif*5){yield;}
				}
			}
		ObjMove_SetDestAtFrame(objEnemy,enmx,enmy+20,180);
		let angleA = 180;
		let angleM = 270+dir*rand(50,60);
		ascent(t in 0..180)
			{
			angleA+=180/180;
			let angleB = 1.33+cos(angleA);
			angleM+=dir*angleB;
			if(t%count[2] == 0)
				{
				ascent(i in 0..spread[2])
					{
					let dist = 8+i*16/spread[2];
					let angle = angleM-dir*i*angleB*count[2]*4/spread[2];
					let shot = CreateBulletA1(enmx+dist*cos(angle),enmy+dist*sin(angle),0.5+i*2.5/spread[2],angle,BL_SMALL_BLUE,8,1);
					ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
					}
				}
			yield;
			}
		loop(120){yield;}
		dir*=-1;
		}

	task TShot(speed,angle,dir,graph)
		{
		let x = 16*cos(angle);
		let y = 48*sin(angle);
		let dist = GetDistance(0,0,x,y);
		let angleA = atan2(y,x);
		x = dist*cos(angleA+dir*30);
		y = dist*sin(angleA+dir*30);
		let obj = CreateBulletA2(enmx+x,enmy+y,speed,angle,(0.25-speed)/60,0,0.25,graph,8,1.175);

		ascent(i in 0..spread[1])
			{
			i = (i-spread[1]/2+0.5)*2;
			let speed = 1.5+rand(-1,1)*0.2;
			let shot = CreateBulletA2(enmx+x,enmy+y,1.5,angle+i*120/spread[1]+rand(-1,1)*3,(speed-1.5)/60,0,speed,DROP_GRAY+floor(floor(rand(0,3))/2)*6,8,1.125);
			ObjMove_AddPatternB2(shot,60,NO_CHANGE,NO_CHANGE,0,1/45,0,1.125+0.375*sin(ObjMove_GetAngle(shot)));
			}

		loop(105){yield;}
		if(Obj_IsDeleted(obj)){return;}
		let shot = CreateBulletA1(ObjMove_GetX(obj),ObjMove_GetY(obj),ObjMove_GetSpeed(obj),ObjMove_GetAngle(obj),ARROWHEAD_BLUE-1+floor(rand(0,2))*10,8,1.175);
		ObjMove_AddPatternA2(shot,45,NO_CHANGE,NO_CHANGE,1/15,0,3);
		Obj_Delete(obj);
		}
}	