#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 94;
life = 12000/3;

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

	TEnemyWork;
	TTime;
	ObjEnemy_Render(objEnemy);
}

task TEnemyWork
{
	loop(15){yield;}
	if(Dif <= 1)
		{
		TSpellName("Divinity \"Kuramitsuha of the Mountain Rain\"");
		}
	else if(Dif == 2)
		{
		TSpellName("Divinity \"Takaokami of the Waterfall\"");
		}		
	else
		{
		TSpellName("Divinity \"Fudoo Dragon Kurikara\"");
		}
	SetBossAura(5,1,1,40);
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-60,20,5);
	loop(90){yield;}
	TLife(0,12,12,120/3,120/3,false);
	let spread = [[4,4,4,5][Dif],1];
	let distM = [90,90,70,60][Dif];;
	let sM = [1.75,2,90,[1.5,1.625,2,1.25][Dif],[4,3,3,4][Dif],7,[1.25,0.85,1,1][Dif]];
	let aM = [390,90];
	let g = [SHOT_GREEN,SHOT_GREEN,SHOT_BLUE,SHOT_RED][Dif];
	let g2 = [[SHOT_AQUA,SHOT_AQUA,SHOT_AQUA,SHOT_ORANGE][Dif],[SHOT_BLUE,SHOT_BLUE,SHOT_BLUE,SHOT_RED][Dif]];
	TFall;
	loop(90){yield;}
	loop
		{
		descent(t in 0..3)
			{
			ascent(dir in 0..2)
				{
				dir = dir*2-1;
				TDragon(90+dir*(135-t*135/2),dir);
				loop(ceil(GetStgFrameHeight*sM[6]/sM[1])){yield;}
				}
			}
		}
	task TDragon(a,dir)
		{
		let y = -16;
		let c = -1;;
		let x = GetStgFrameWidth*0.5+cos(a)*distM;
		let xA = x;
		let yA = y;
		let n = 0;
		while(!Obj_IsDeleted(objEnemy) && y < GetStgFrameHeight+32)
			{
			c+=1;
			a+=dir*sM[0];
			x = GetStgFrameWidth*0.5+cos(a)*distM;
			y += 0.5+sM[1]*(absolute(cos(a))^2);
			if(c%sM[4] == 0)
				{
				PlaySound("se_Shot1",50);
				n = (n+1)%sM[5];
				let aP = atan2(y-yA,x-xA);
				ascent(i0 in 0..spread[0])
					{
					i0 = i0-spread[0]*0.5+0.5;
					let a0 = aP+i0*135/spread[0];
					let a1 = aP+i0*aM[0]/spread[0];
					let head = CreateBulletA1(x+16*cos(a0),y+16*sin(a0),0.25,(a0-aP)*0.67+aP,SCALE,0,1.25,SHOT_GREEN);
					Obj_DelayDelete(head,sM[4]+1);
					ObjShot_SetGraphic(head,SCALE[g]-1);
					TDelA(head,6);
					ascent(i1 in 0..spread[1])
						{
						i1 = i1-spread[1]*0.5+0.5;
						let shot = CreateBulletA1(x+6*cos(a0),y+6*sin(a0),0.25,a1+i1*aM[1]/spread[1],SCALE,0,1.125,SHOT_GREEN);
						if(n > 1)
							{
							ObjShot_SetGraphic(shot,SCALE[g]-1);
							ObjMove_AddPatternA2(shot,75,NO_CHANGE,NO_CHANGE,sM[3]/sM[2],0,sM[3]);
							}
						else if(n == 0 && Dif >= 2)
							{
							ObjShot_SetGraphic(shot,BL_SMALL[g2[0]]);
							ObjMove_SetAngle(shot,ObjMove_GetAngle(shot)+rand(-1,1)*30);
							ObjShot_SetScale(shot,1.125);
							ObjMove_AddPatternB2(shot,75,NO_CHANGE,NO_CHANGE,0,-1/90,0,-0.75);
							ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
							}
						else
							{
							Obj_Delete(shot);
							}
						TDelA(shot,25);
						}
					}
				}
			xA = x;
			yA = y;
			yield;
			}
		}
	task TFall	
		{	
		let dist = GetStgFrameWidth*0.5;
		let spread = 6;
		loop
			{
			ascent(i1 in 0..2)
				{
				i1 = i1*2-1;
				ascent(i0 in 0..spread)	
					{
					let x = GetStgFrameWidth/2+i1*(dist+16+i0*(GetStgFrameWidth/2-dist)/spread)+i1*rand(0,1)*(GetStgFrameWidth/2-dist+16)/spread;
					let shot = CreateBulletA2(x,-32,2,90+rand(-2,2),rand(1/25,1/15),0,10,FIRE,0,1,[g2[0],g2[1]][trunc(rand(0,2))]);
					ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
					if(x >= GetStgFrameWidth*0.5-distM && x <= GetStgFrameWidth*0.5+distM)
						{
						TDel(shot);
						}
					}
				//PlaySound(se_CuteShotLong,77.5);		
				loop(round(rand(2,4)))
					{
					if(dist > distM)
						{
						dist-=(GetStgFrameWidth*0.5-distM)/60;
						}
					yield;
					}	
				}
			}
		}
	task TDel(shot)
		{
		loop(35){yield;}
		ObjShot_FadeDelete(shot);
		}
	task TDelA(shot,time)
		{	
		ObjShot_SetIntersection(shot,false);
		let aP = atan2(ObjMove_GetY(shot)-enmy,ObjMove_GetX(shot)-enmx);
		if((IsBeyondClip(shot,8) && cos(ObjMove_GetAngle(shot) - aP) < 0.5) || Obj_IsDeleted(objEnemy))
			{
			return;		
			}	
		let sc = ObjShot_GetScale(shot);
		descent(t in 0..time)
			{
			ObjRender_SetAlpha(shot,255*(1-t/time));
			t = (1+2*t/time);
			ObjRender_SetScaleXYZ(shot,sc*t,sc*t,0);
			if(Obj_IsDeleted(objEnemy)){break;}
			yield;
			}
		ObjRender_SetScaleXYZ(shot,sc,sc,0);
		ObjShot_SetIntersection(shot,true);
		}
}