#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 60;

@Initialize
{
	Obj_SetValue(GetEnemyBossSceneObjectID,"INIT",false);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	bspell = true;

	TEnemyWork;
	ObjEnemy_Render;
	TBackground;
	TLife(210,20,32,1,0.5);
	TEnd(false);
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(2000/spellLife);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(10^10);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
}
task TEnemyWork
{
	TXY;
	TSpellName("\"These Games Are But Books in My Library\"");
	loop(75){yield;}
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-105,20,5);
	loop(30){yield;}
	let delay = 20;
	let spread = [2,3+Dif*2,3,24,15];
	let dirM = 1;
	loop
		{
		ascent(t in 0..2)
			{
			descent(i0 in 0..2+t)
				{
				let dir = dirM*(t*2-1)*((i0%2)*2-1);
				ascent(i1 in 0..spread[1]+1)
					{
					let iA = i1;
					i1 = i1-spread[1]*0.5+0.5-(i0%2)*0.5;
					TShot(GetStgFrameWidth*(0.5+dir*i1*2*0.5/spread[1]),3.5,dir,iA);
					loop(3-Dif){yield;}
					}
				loop(120-t*30){yield;}
				}
			loop(150+t*30){yield;}
			}
		dirM*=-1;
		}
	task TShot(x,speed,dirA,type)
		{
		PlaySound("se_LaserDelay",100);
		let shot = CreateBulletA1(x,GetStgFrameHeight-8,-0.25,270,BUBBLE,0,0,SHOT_ORANGE);
		Obj_SetRenderPriority(shot,0.49);
		Obj_SetValue(shot,"SKIP_TIME_STOP",true);
		ObjShot_SetIntersection(shot,false);
		ObjShot_SetSpellResist(shot,true);
		ObjShot_SetDeathResist(shot,true);
		ascent(t in 0..delay)
			{
			ObjShot_SetScale(shot,(1)*(2-t/(delay-1)));
			ObjRender_SetAlpha(shot,255*t/delay);
			yield;
			}
		ObjShot_SetIntersection(shot,true);
		loop(15){yield;}
		ObjMove_SetAcceleration(shot,speed/75);
		ObjMove_SetMaxSpeed(shot,speed);
		let counter = trunc(rand(0,30));
		let angle = rand(0,360);
		while(!Obj_IsDeleted(shot) && counter < 160)
			{
			counter+=1;
			angle+=6;
			if(counter%(13+Dif) == 0)
				{
				PlaySound("se_Shot2",30);
				ascent(i in 0..spread[0])
					{
					TSpam(angle+i*360/spread[0],(i%2)*2-1);
					}
				}
			yield;
			}
		if(type != 0 || Obj_IsDeleted(shot)){return;}
		let dirB = rand(-1,1)*0.5;
		PlaySound("se_Shot1",60);
		PlaySound("se_Die",80);
		ascent(i2 in 0..spread[2])
			{
			let speed = (0.25+i2*0.75/spread[2])*6;
			ascent(i3 in 0..spread[3])	
				{
				i3 = i3-spread[3]*0.5+0.5+dirB;
				let spam = CreateBulletA1(ObjMove_GetX(shot),ObjMove_GetY(shot),speed,270-dirA*45+i3*240/spread[3],ARROWHEAD,8,1.125,SHOT_BLUE);
				ObjShot_SetGraphic(spam,ARROWHEAD_PURPLE+2-min(i2,1)*2);
				}
			}
		ascent(i4 in 0..spread[4])
			{
			i4 = i4-spread[4]*0.5+0.5+dirB;
			ascent(dir in 0..2)
				{
				dir = dir*2-1;
				let spam = CreateBulletA2(ObjMove_GetX(shot),ObjMove_GetY(shot),0,270-dirA*45+180+i4*180/spread[4]+dir*65,1/30,-dir*90/120,1,BL_SMALL,8,1.125,SHOT_BLUE);
				ObjShot_SetGraphic(spam,BL_SMALL_PURPLE+1.5+dirA*dir*1.5);
				ObjMove_AddPatternA2(spam,120,NO_CHANGE,NO_CHANGE,0,0,0);
				}
			}
		ObjMove_SetSpeed(shot,0);
		ObjMove_SetAcceleration(shot,0);
		ObjShot_ScaleDelete(shot,15);
		task TSpam(angle,dir)
			{
			let spam = CreateBulletA1(ObjMove_GetX(shot)+24*cos(angle),ObjMove_GetY(shot)+24*sin(angle),0,90+dirA*dir*90,AMULET,8,0,SHOT_PURPLE);
			ObjShot_SetGraphic(spam,AMULET_PURPLE+dir);
			ObjShot_SetIntersection(spam,false);
			if(GetObjectDistance(spam,GetPlayerObjectID) < 48){Obj_Delete(spam);return;}
			ascent(t in 0..10)
				{
				if(Obj_IsDeleted(spam)){return;}
				ObjShot_SetScale(spam,1.25*(2-t/9));
				ObjRender_SetAlpha(spam,t*255/9);
				yield;
				}
			ObjShot_SetIntersection(spam,true);
			loop(20){yield;if(Obj_IsDeleted(spam)){return;}}
			ObjMove_SetAcceleration(spam,1/105);
			ObjMove_SetMaxSpeed(spam,10);
			loop(165){yield;if(Obj_IsDeleted(spam)){return;}}
			ObjShot_ScaleDelete(spam,10);
			}
		}
}