#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell3H"]

#include"./functions1.txt"

timer = 90;

@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy,0,0);
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	
	TEnemyWork;
	ObjEnemy_Render;
	ObjEffect_Shadow(true);
	TLife(objEnemy,180,20,20,2.5,2.5,10,5);
	TEnd;
}

@MainLoop
{
	timer-=1/60;
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(500);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(timer);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
	case(EV_START_BOSS_SPELL)
		{
		SetScriptResult(true);
		}
}

task TEnemyWork
{
	TSpellName("Mystery \"Rekindling Divine Spirits\"");
	TXY;
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-70,15,5);
	loop(90){yield;}
	EnemySpell = true;
	let spread = [0,4];
	let time = 30;
	let angleA;
	let dir = 1;

	loop
		{
		spread[0] = 6;
		spread[1] = 4;
		angleA = GetAngleToPlayer(objEnemy)+75/spread[1];
		time = 30;
		loop(4)
			{
			loop(5)
				{
				TSpam(0);
				loop(time+15){yield;}
				angleA+=17;
				}	
			ObjMove_SetDestAtFrame(objEnemy,GetStgFrameWidth/2+dir*GetStgFrameWidth/3,GetStgFrameHeight/2+rand(-75,-50),3*(time));
			dir = dir*-1;
			}
		ObjMove_SetDestAtFrame(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-50,5*(time));
		loop(5)
			{
			TSpam(0);
			loop(time+15){yield;}
			angleA+=15;
			}
		loop(30){yield;}
		EnemySpell2 = true;
		loop(75){yield;}
		EnemySpell2 = false;
		loop(30){yield;}
		spread[0] = 4;
		spread[1] = 4;
		time = 45;
		loop(5)
			{
			TSpam(90);
			angleA+=(360/spread[0])/5;
			}
		loop(180){yield;}
		}

	task TSpam(angleM)
		{
		let dirA = 1;
		ascent(i0 in 0..spread[0])
			{
			ascent(i1 in 0..spread[1])
				{
				let dist = i1*(450+angleM*0.33)/spread[1]+50-sin(angleM)*25;
				let angle = i0*360/spread[0]+i1*(360/spread[0])/2+angleA;
				let graph = (i1)%4+2;
				TShot(enmx+dist*cos(angle),enmy+dist*sin(angle),graph,dirA*angleM);
				dirA = dirA*-1;
				}
			}
		PlaySound(se_DeepShotHigh,80);
		loop(time+15){yield;}
		PlaySound(se_CuteShotLong,80);
		}

	task TShot(x,y,graph,angleM)
		{
		let angle = atan2(y-enmy,x-enmx);
		let dist = GetDistance(enmx,enmy,x,y);
		let speed = dist/(time-1);
		let accel = -(speed/time);	
		while(graph > 8){graph-=8;}
		let obj = CreateShotOA2(objEnemy,speed,angle,accel,0,DS_BALL_L_WHITE+round(graph),15);
		loop(time+15){yield;}
		if(Obj_IsDeleted(obj)){return;}
		let graph2 = 7;
		alternative(round(graph))
			case(1)
				{
				graph2 = 1;
				}
			case(2)
				{
				graph2 = 9;
				}
			case(3)
				{
				graph2 = 5;
				}
			case(4)
				{
				graph2 = 2;
				}
			case(5)
				{
				graph2 = 3;
				}
			case(6)
				{
				graph2 = 6;
				}
			case(7)
				{
				graph2 = 8;
				}
		ascent(i in 0..2)
			{
			let bulletObj = CreateShotC1(ObjMove_GetX(obj),ObjMove_GetY(obj),0,angle+angleM,DS_LIGHT_D_WHITE+graph2,15);
			ObjMove_AddPatternA2(bulletObj,15,NO_CHANGE,NO_CHANGE,-accel*(45/time-absolute(sin(angleM))*0.75),0,10);
			ObjRender_SetBlendType(bulletObj,BLEND_ADD_ARGB);
			ObjRender_SetColorHSV(bulletObj,0,i*100,255-i*150);
			}
		ObjShot_SetSpellResist(obj,true);
		ObjShot_SetIntersectionEnable(obj,false);
		let alph = 255;
		let scale = 1;
		loop(15)
			{
			alph-=255/30;
			scale+=1/30;
			ObjRender_SetScaleXYZ(obj,scale,scale,0);
			ObjRender_SetAlpha(obj,alph);
			loop(1){yield;}
			}
		Obj_Delete(obj);		
		}
		
}		

task TMovement
{
	let distM = 25;
	let distm =distM/2;
	let numb = round(rand(0,1))*2-1;
		if(ObjMove_GetX(objEnemy) < 175)
			{
			numb =1;	
			}
		else if (ObjMove_GetX(objEnemy) > GetStgFrameWidth-175)
			{	
			numb = -1;
			}
	ObjMove_SetDestAtWeight(objEnemy,ObjMove_GetX(objEnemy)+numb*rand(distm,distM),rand(120,170),30,5);
		
}