#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["YukariS9"]
#Text["Great Hakurei Barrier -Shining Star-, Yukari's PCB Spell Card, but totally adapted version."]
#Player["./Hecatia/Hecatia.txt"]
#System["./system/Default_System.txt"]
#Background["./BG.txt"]
#include"./Functions/Index.txt"
#include"./SetBoss.txt"
#include"./Shot/AllStarShot_Const.dnh"
#include"./system/Default_Effect.Txt"


let objBoss;
let GCSD=GetCurrentScriptDirectory~"";
let enemy;
let bSpell=false;
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
let idle = true;
let atk = false;
let fog = 0;
let action = false;
let objScene=GetEnemyBossSceneObjectID();
let color = [276,277,278];
let color2 = [51,53,55];
let dcolor = [308,309,310];
@Initialize
{
SetBoss("Yukari",true);
}

@MainLoop
{
	yield;

}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(1);
	}

                case(EV_REQUEST_IS_DURABLE_SPELL)
	{
		SetScriptResult(true);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(152 );
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(90000000);
	}
}

//----------------------------------------------------
task mainTask{
	StartCountDown;
	wait( 150 );
	SetCommonData("ForceLifebar",1);
	ObjMove_SetDestAtWeight(objBoss,stgx,stgy,10,7);
	Cutin("Great Hakurei Barrier -Shining Star-",15,"SHINKI");
	ObjEnemyBossScene_StartSpell(objScene);
	Sound(GCSD ~ "Butterfly.mp3");
	wait(105);
	TShot1;
	wait(1200);
	}
//----------------------------------------------------

task TShot1
{
                   //ObjPlayer_ClearIntersection(GetPlayerObjectID);
	if( ObjEnemy_GetInfo( objBoss, INFO_LIFE ) < 1 ){ DeleteShotAll( TYPE_ALL, TYPE_IMMEDIATE ); }
	attack(150);
	let r = 70;
	let angle = rand(0,80);
	let dir = 0;
	let s = 1;
	wait( 80 );
	maple;
	wait( 75 );
	ObjRender_SetAlpha( objBoss, 0 );

	PatternBorder;
	wait( 480 );
	PlaySound("Shot1",92,0);

	PatternYear1;
	Year1Move;
	wait( 1900 );

	PlaySound("Shot1",92,0);
	NotBorderAllDelete;
	wait( 45 );

	PatternYear2;
	Year2Move;
	wait( 1840 );

	PlaySound("Shot1",92,0);
	NotBorderAllDelete;
	wait( 45 );

	PatternYear3;
	wait( 700 );
	Year3Move;
	wait( 2550 );
	
	PlaySound("Shot1",92,0);
	NotBorderAllDelete;
	wait( 45 );

	PatternYear4;
}

               task Year1Move{
               wait( 380 );
               PlaySound("Boon1",92,0);
               ObjMove_SetDestAtWeight( objBoss, 80,120,3,0.32 );
               wait( 425 );
               PlaySound("Boon1",92,0);
               ObjMove_SetDestAtWeight( objBoss, 310,120,1,0.5 );
               wait( 450 );
               PlaySound("Boon1",92,0);
               ObjMove_SetDestAtWeight( objBoss, stgx,400,1,0.5 );
	}

               task Year2Move{
               wait( 330 );
               PlaySound("Boon1",92,0);
               ObjMove_SetDestAtWeight( objBoss, GetStgFrameWidth - 50,60,3,0.7 );
               wait( 540 );
               PlaySound("Boon1",92,0);
               ObjMove_SetDestAtWeight( objBoss, 30,stgy + 100,1,0.6 );
               wait( 620 );
               PlaySound("Boon1",92,0);
               ObjMove_SetDestAtWeight( objBoss, stgx,stgy,1,0.6 );
	}

               task Year3Move{
               wait( 350 );
               PlaySound("Boon1",92,0);
               ObjMove_SetDestAtWeight( objBoss, GetStgFrameWidth - 20,stgy,3,0.4 );
               wait( 460 );
               PlaySound("Boon1",92,0);
               ObjMove_SetDestAtWeight( objBoss, GetStgFrameWidth / 5, GetStgFrameHeight - 30,1,0.6 );
               wait( 600 );
               PlaySound("Boon1",92,0);
               ObjMove_SetDestAtWeight( objBoss, stgx, 10,1,0.6 );
               wait( 720 );
               PlaySound("Boon1",92,0);
               ObjMove_SetDestAtWeight( objBoss, stgx,stgy,1,0.6 );
	}

              task PatternBorder{ 
              loop( 1 ){

              			let angle = 0;
	                                	let way = 8;
                                                	let shote1 = 856;
                                                	let shote2 = 858;
			let shote3 = 504;
			let shote4 = 507;
			let delay = 0;

			loop( way ){

				let sht1;
				let sht2;

                      	               	 	let sx = ObjMove_GetX( objBoss );
                        			let sy = ObjMove_GetY( objBoss );
				
					sht1 = TShotA2( sx, sy, 0, angle, 0, 0, shote1, delay );
					ObjShot_SetSpellResist( sht1, true );
					ObjShot_SetAutoDelete( sht1, false );

					let sht5 = TShotA2( sx, sy, 0, angle, 0, 0, shote2, delay );
					ObjShot_SetSpellResist( sht5, true );
					ObjShot_SetAutoDelete( sht5, false );

					sht2 = TShotA2( sx, sy, 0, angle, 0, 0, shote2, delay );
					ObjShot_SetSpellResist( sht2, true );
					ObjShot_SetAutoDelete( sht2, false );

					let sht3 = CreateStraightLaserA1( sx, sy, angle, 210, 28, 150000, shote3, 60 );
					let sht4 = CreateStraightLaserA1( sx, sy, angle, 210, 28, 150000, shote4, 60 );

					Obj_SetValue( sht1, "border" ,1 );
					Obj_SetValue( sht2, "border" ,1 );
					Obj_SetValue( sht3, "border" ,1 );
					Obj_SetValue( sht4, "border" ,1 );
					Obj_SetValue( sht5, "border" ,1 );

					ObjShot_SetSpellResist( sht3, true );
					ObjShot_SetAutoDelete( sht3, false );

					ObjShot_SetSpellResist( sht4, true );
					ObjShot_SetAutoDelete( sht4, false );

					ObjectRotation( sht1, objBoss, 350, 100, angle, -0.6, 0.8 , 0, 0 );
					ObjectRotation( sht5, objBoss, 350, 100, angle, -0.6, 0.8 , 0, 0 );
					ObjectRotation( sht2, objBoss, 350, 100, angle, -0.6, -0.8 , 0, 0 );

					ObjectRotation( sht3, objBoss, 350, 100, angle, -0.6, 0.8 , 0, 0 );
					ObjectRotation( sht4, objBoss, 350, 100, angle, -0.6, -0.8 , 0, 0 );

				NotifyEventAll( EV_USER + 40, [ sht1, delay ] );
				NotifyEventAll( EV_USER + 40, [ sht2, delay ] );
				NotifyEventAll( EV_USER + 40, [ sht3, delay ] );
				NotifyEventAll( EV_USER + 40, [ sht4, delay ] );

				angle += 360 / way;

			}

			PlaySound("Boon1",92,0);
                       		if( wait( 1 ) ){ return; }


	}

   }

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

              task PatternYear1{ 
              loop( 41 ){
              let sx = 0;
              let gap = 48;
              loop( GetStgFrameWidth / gap + 1 ){
              			let angle = 90;
                                                	let shote = 99;
			x( sx, angle, shote );

			sx += gap;
			PlaySound("LenenShot1",92,0);
			wait( 5 );

	}

                        if( wait( 0 ) ){ return; }

	}

   }
              task x( sx, angle, shote ){
              loop( 6 ){

              			let speed = 2;
	                                	let way = 1;
			let delay = 20;

			loop( way ){

                        		let sy = 0;
				
				let sht1 = TShotA2( sx, sy, speed, angle, 0, speed, shote, delay );
				ObjShot_SetSpellResist( sht1, true );
				NotifyEventAll( EV_USER + 40, [ sht1, delay ] );

				angle += 360 / way;
			
			}
			wait( 5 );
			}
			}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

              task PatternYear2{ 
              loop( 38 ){
              let sy1 = 0;
              let sy2 = 44.8;
              let gap = 44.8*2;
              loop( GetStgFrameHeight / gap + 1 ){
                        		let sx = 0;
              			let angle = 0;
                                                	let shote = 106;
			y( sy1, angle, shote, sx );

                        		let sx2 = GetStgFrameWidth;
              			let angle2 = 180;
                                                	let shote2 = 103;
			y( sy2, angle2, shote2, sx2 );

			sy1 += gap;
			sy2 += gap;
			PlaySound("LenenShot1",92,0);
			wait( 7 );

	}

                        if( wait( 5 ) ){ return; }

	}

   }
              task y( sy, angle, shote, sx ){
              loop( 6 ){

              			let speed = 2;
	                                	let way = 1;
			let delay = 20;

			loop( way ){
				
				let sht1 = TShotA2( sx, sy, speed, angle, 0, speed, shote, delay );
				ObjShot_SetSpellResist( sht1, true );
				NotifyEventAll( EV_USER + 40, [ sht1, delay ] );

				angle += 360 / way;
			
			}
			wait( 5 );
			}
			}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

              task PatternYear3{ 
              let setangle = 270;
              loop( 6 ){

              			let speed = 0.3;
	                                	let way = 28;
                                                	let shote = 110;
			let delay = 20;
			let r = 150;
                      	                	let sx = ObjMove_GetX( objBoss ) + r*cos( setangle );
                        		let sy = ObjMove_GetY( objBoss ) + r*sin( setangle );
              			let angle = GetDotAngle( ObjMove_GetX( objBoss ), ObjMove_GetY( objBoss ), sx, sy );

			loop( way ){

				let sht1 = TShotA2( sx, sy, 0, angle, 0.005, speed, shote, delay );
				ObjShot_SetSpellResist( sht1, true );
				NotifyEventAll( EV_USER + 40, [ sht1, delay ] );
				EdgeWarpingA( sht1, 10 );
				EdgeWarpingB( sht1, 10 );

				angle += 360 / way;

			}
			setangle += 360 / 6;
			PlaySound("LenenShot1",92,0);

	}
	wait( 800 );
              let setangle2 = 300;
              loop( 6 ){

              			let speed = 0.3;
	                                	let way = 28;
                                                	let shote = 99;
			let delay = 20;
			let r = 150;
                      	                	let sx = ObjMove_GetX( objBoss ) + r*cos( setangle2 );
                        		let sy = ObjMove_GetY( objBoss ) + r*sin( setangle2 );
              			let angle = GetDotAngle( ObjMove_GetX( objBoss ), ObjMove_GetY( objBoss ), sx, sy );

			loop( way ){

				let sht1 = TShotA2( sx, sy, 0, angle, 0.005, speed, shote, delay );
				ObjShot_SetSpellResist( sht1, true );
				NotifyEventAll( EV_USER + 40, [ sht1, delay ] );
				EdgeWarpingA( sht1, 10 );
				EdgeWarpingB( sht1, 10 );

				angle += 360 / way;

			}
			setangle2 += 360 / 6;
			PlaySound("LenenShot1",92,0);

	}

   }

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

	function GetDotAngle(x1,y1,x2,y2){
	let angle=atan2(y1-y2,x1-x2);
	return angle;
	}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

              task PatternYear4{
              let c = 0;
              while( !Obj_IsDeleted( objBoss )){

              			let speed = 0.8;
	                                	let way = 10;
                                                	let shote = [ 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112 ];
			let delay = 30;

			let r = 70;

                      	                	let x1 = 0 + rand( -20, 20 );
                      	                	let x2 = GetStgFrameWidth / 2  + rand( -20, 20 );
                      	                	let x3 = GetStgFrameWidth  + rand( -20, 20 );

                        		let y1 = 0  + rand( -20, 20 );
                        		let y2 = GetStgFrameHeight / 2  + rand( -20, 20 );
                        		let y3 = GetStgFrameHeight  + rand( -20, 20 );

              			let angle = rand( 0, 360 );

			loop( way ){

                      	                	let sx1 = x1 + r*cos( angle );
                      	                	let sx2 = x2 + r*cos( angle );
                      	                	let sx3 = x3 + r*cos( angle );

                        		let sy1 = y1 + r*sin( angle );
                        		let sy2 = y2 + r*sin( angle );
                        		let sy3 = y3 + r*sin( angle );

				let sht1 = TShotA2( sx1, sy1, speed, angle, 0, speed, shote[c%length( shote )], delay );
				let sht2 = TShotA2( sx2, sy1, speed, angle, 0, speed, shote[c%length( shote )], delay );
				let sht3 = TShotA2( sx3, sy1, speed, angle, 0, speed, shote[c%length( shote )], delay );
				let sht4 = TShotA2( sx1, sy2, speed, angle, 0, speed, shote[c%length( shote )], delay );
				let sht5 = TShotA2( sx3, sy2, speed, angle, 0, speed, shote[c%length( shote )], delay );
				let sht6 = TShotA2( sx1, sy3, speed, angle, 0, speed, shote[c%length( shote )], delay );
				let sht7 = TShotA2( sx2, sy3, speed, angle, 0, speed, shote[c%length( shote )], delay );
				let sht8 = TShotA2( sx3, sy3, speed, angle, 0, speed, shote[c%length( shote )], delay );

				ObjShot_SetSpellResist( sht1, true );
				ObjShot_SetSpellResist( sht2, true );
				ObjShot_SetSpellResist( sht3, true );
				ObjShot_SetSpellResist( sht4, true );
				ObjShot_SetSpellResist( sht5, true );
				ObjShot_SetSpellResist( sht6, true );
				ObjShot_SetSpellResist( sht7, true );
				ObjShot_SetSpellResist( sht8, true );

				NotifyEventAll( EV_USER + 40, [ sht1, delay ] );
				NotifyEventAll( EV_USER + 40, [ sht2, delay ] );
				NotifyEventAll( EV_USER + 40, [ sht3, delay ] );
				NotifyEventAll( EV_USER + 40, [ sht4, delay ] );
				NotifyEventAll( EV_USER + 40, [ sht5, delay ] );
				NotifyEventAll( EV_USER + 40, [ sht6, delay ] );
				NotifyEventAll( EV_USER + 40, [ sht7, delay ] );
				NotifyEventAll( EV_USER + 40, [ sht8, delay ] );

				angle += 360 / way;

			}
			c += 1;
			PlaySound("LenenShot1",92,0);
                       		if( wait( 40 ) ){ return; }

	}
	}

             task NotBorderAllDelete{
             loop( 1 ){	
            	let shotarray = GetShotIdInCircleA2( ObjMove_GetX( objBoss ), ObjMove_GetY( objBoss ), 1000, TARGET_ENEMY );
             	ascent( i in 0..length( shotarray )){
             	if( Obj_GetValueD( shotarray[ i ], "border", 0 ) == 0 ){ 
		ObjShot_SetSpellResist( shotarray[ i ], false );
		DeleteShotAll( TYPE_SHOT, TYPE_ITEM );	
                                                                   }
	              }
	}
}

task ObjectRotation( obj, target, rstart, rmax, angle, ad, av, aad, aav ){

   let ox;
   let oy;
   let r = rstart;
   while(!Obj_IsDeleted(obj)){

      ox = ObjMove_GetX( target ) + r*cos( angle );
      oy = ObjMove_GetY( target ) + r*sin( angle );

      ObjMove_SetPosition(obj,ox,oy);
      ObjStLaser_SetAngle(obj,angle);

      if(r > rmax){
      r += ad;
      ad += aad; }

      angle += av;

      yield;
   }
}

	task StartCountDown
	{
		wait((152*60) - 50 - (10*60));
		loop(5)
		{
			PlaySound("time1",100,0);
			wait(60);
		}
		loop(5)
		{
			PlaySound("time2",100,0);
			wait(60);
		}
	}


task EdgeWarpingA(obj,time)
{
        let count = 0;
        while(!Obj_IsDeleted(obj) )
        {
            if(count==time){break;}
            
            if(ObjMove_GetX(obj)<-20){ObjMove_SetX(obj,GetStgFrameWidth+20);yield;count++;}
            if(ObjMove_GetX(obj)>GetStgFrameWidth+20){ObjMove_SetX(obj,-20);yield;count++;}
            yield;
        }
}
task EdgeWarpingB(obj,time)
{
        let count = 0;
        while(!Obj_IsDeleted(obj) )
        {
            if(count==time){break;}
            
            if(ObjMove_GetY(obj)>GetStgFrameHeight+20){ObjMove_SetY(obj,-20);yield;count++;}
            if(ObjMove_GetY(obj)<-20){ObjMove_SetY(obj,GetStgFrameHeight+20);yield;count++;}
            yield;
        }
}


//----------------------------------------------------

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		EndBoss(1);
		yield;
		CloseScript(GetOwnScriptID());
	}