
#ScriptVersion[3]
#Title["YaorochiS3"]
#Text[""]
#Player[DEFAULT]
#System["./system/Default_System.txt"]
#Background["./bg-klong.txt"]
#include"./Functions/Index.txt"
#include"./SetBoss.txt"
#include"./Shot/JINXShot_Const.txt"
#include"./system/Default_Effect.Txt"

let objBoss;
//let objBossIyo;
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 dir=GetCurrentScriptDirectory();
let obj=ObjEnemyBossScene_Create();
let color = [276,277,278];
let color2 = [51,53,55];
let dcolor = [308,309,310];
@Initialize
{
SetBoss("Doremy",true);
//SetBossIyo("Doremy",true);
SetCommonData("Life",1);
SetCommonData("Bomb",1);
GetCommonData("SPback",0);
SetCommonData("LenenSpellcut",[70,100,70]);
SetCommonData("SpellBarcl",[70,100,70]);
SetCommonData("Boss",0);
SetCommonData("CutChar",GCSD ~ "./Functions/booss/senri_09.png");
mainTask;
}

@MainLoop
{
	//ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),18);
	//ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),26);
	yield;

}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(2500);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(60);
	}
	case(EV_REQUEST_IS_DURABLE_SPELL)
	{
		SetScriptResult(true);
	}
	case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(500000);
	}
}

//----------------------------------------------------
task mainTask{
	SetCommonData("ForceLifebar",1);
	//SetCommonData("FORCE_HITBOX",true);
	StartCountDown;
	PlaySound("SpellStart",100,0);
	wait( 120 );
	ObjMove_SetDestAtWeight(objBoss,384/2,120,10,4);
	Cutin("Zen \"Sky High\"",20,"SHINKI");
	ObjEnemyBossScene_StartSpell(objScene);
	wait(210);
	ObjMove_AddPatternA2( objBoss, 0, 0, 270, 0.08, 0, 10 );
	ObjMove_AddPatternA2( objBoss, 120, 0, 90, 0, 0, 0 );
	TShot1;
	DamageRateAtFrame(80,20,0);
	}
	
//----------------------------------------------------
let tempg = [ 5, 6, 7 ];
task TShot1
{
	dummy;
	wait( 100 );
	wait( 40 );
	loop( 3 ){
		wait( 200 );
		KnifeSlash( GetPlayerX, GetPlayerY, 180 - 30 );
	}
	wait( 20 );
	loop( 4 ){
		wait( 160 );
		SlashDot( GetPlayerX, GetPlayerY );
	}
	wait( 150 );
	loop( 4 ){
		loop( 5 ){
			let x = rand( 80, GetStgFrameWidth - 80 );
			let y = rand( 100, GetStgFrameHeight - 150 );
			let angle = rand( 0, 360 );
			sub_slash( x, y, angle );
			wait( 6 );
		}
		wait( 80 );
	}
	mix;
	}

	task mix{
		let time = 110;
		let select = floor( rand( 0, 2 + 1 ));
		if( select == 0 ){
			Pattern1( time );
		}
		if( select == 1 ){
			Pattern2( time );
		}
		if( select == 2 ){
			Pattern3( time );
		}
	}
	
	task Pattern1( time ){
		let mul = [ -1, 1 ];
		let select_a = floor( rand( 0, 1 + 1 ));
		KnifeSlash( GetPlayerX, GetPlayerY, 60 + 90*mul[ select_a ] );
		
		wait( time );
		time -= 2;
		
		let select = floor( rand( 0, 1 + 1 ));
		if( select == 0 ){
			Pattern2( time );
		}
		if( select == 1 ){
			Pattern3( time );
		}
	}
		
	task Pattern2( time ){
		SlashDot( GetPlayerX, GetPlayerY );
		
		wait( time );
		time -= 2;
		
		let select = floor( rand( 0, 1 + 1 ));
		if( select == 0 ){
			Pattern1( time );
		}
		if( select == 1 ){
			Pattern3( time );
		}
	}
	
	task Pattern3( time ){
		S_Slash;
		
		wait( time );
		time -= 2;
		
		let select = floor( rand( 0, 1 + 1 ));
		if( select == 0 ){
			Pattern1( time );
		}
		if( select == 1 ){
			Pattern2( time );
		}
	}
		
		task S_Slash{
		loop( 3 ){
					let x = rand( 80, GetStgFrameWidth - 80 );
					let y = rand( 100, GetStgFrameHeight - 150 );
					let angle = rand( 0, 360 );
					sub_slash( x, y, angle );
					wait( 32 );
				}
		}

		task sub_slash( x, y, angle ){ 
	        let way = 2;
            let shote = 145;
			let delay = 50;

			loop( way ){
				let r = -60;
				let sx = x + r*cos( angle );
				let sy = y + r*sin( angle );
				
				HLaser( sx, sy, angle, 900, 20, 40, shote, delay );

				angle += 360 / way;
			}
			PlaySound("LenenShot1",92,0);
		}

			task KnifeSlash( x, y, angle ){ 
				PlaySound("Boon1",92,0);
				let sx1;
				let sy1;
				let rad_weff = 60;
				loop( 10 ){
					let way = 1;
					let shote = 153;
					let delay = 0;
					let length = 1600;
					let width = 4;
					loop( way ){
						let r = -length/2;
						
              			sx1 = x + r*cos( angle ) + rad_weff*cos( angle + 90 );
              			sy1 = y + r*sin( angle ) + rad_weff*sin( angle + 90 );
						let sx2 = x + r*cos( angle ) + rad_weff*cos( angle - 90 );
              			let sy2 = y + r*sin( angle ) + rad_weff*sin( angle - 90 );
				
						let sht1 = CreateStraightLaserA1( sx1, sy1, angle, length, width, 100, shote, delay );
						slash_eff01( sht1 );
						ObjMove_AddPatternA2( sht1, 0, 0, angle + 90 + 180, 0.185, 0, 20 );
						ObjMove_AddPatternA2( sht1, 25, 0, NO_CHANGE, 0, 0, 0 );
						ObjShot_SetIntersectionEnable( sht1, false );
						ObjShot_SetAutoDelete( sht1, false );
						
						let sht2 = CreateStraightLaserA1( sx2, sy2, angle, length, width, 100, shote, delay );
						slash_eff01( sht2 );
						ObjMove_AddPatternA2( sht2, 0, 0, angle + 90, 0.185, 0, 20 );
						ObjMove_AddPatternA2( sht2, 25, 0, NO_CHANGE, 0, 0, 0 );
						ObjShot_SetIntersectionEnable( sht2, false );
						ObjShot_SetAutoDelete( sht2, false );

						angle += 360 / way;
				}
				wait( 6 );
			}
			realslash( sx1 - rad_weff*cos( angle + 90 ), sy1 - rad_weff*sin( angle + 90 ), angle );
		}
		
		task realslash( sx, sy, angle ){
			wait( 2 );
			delaysoundeff02;
			let uspeed = 40;
			let slash = CreateLooseLaserA1( sx, sy, uspeed, angle, 760, 25*1.8, 89, 0 );
			ObjShot_SetAutoDelete( slash, false );
			DelayDelete( slash, 120 );
			wait( 16 );
			let slash02 = TShotA2( sx, sy, uspeed, angle, 0, uspeed, 1, 0 );
			//SlashSub( slash02, angle );
			ObjShot_SetAutoDelete( slash02, false );
			DelayDelete( slash02, 120 );
			ObjShot_SetSpellResist( slash02, true );
			ObjShot_SetIntersectionEnable( slash02, false );
			ObjRender_SetScaleXYZ( slash02, 5, 5, 0 );
			
			let len = 60;
			let slash3 = CreateStraightLaserA1( sx, sy, 180 + angle, 3500, len, 200, 145, 0 );
			close_eff( slash3, len );
			//ObjRender_SetBlendType( slash3, BLEND_ADD_ARGB );
			ObjShot_SetAutoDelete( slash3, false );
			ObjMove_AddPatternA2( slash3, 0, uspeed, angle, 0, 0, uspeed );
		}
		
		task delaysoundeff02{
		wait( 25 );
		PlaySound("Slash",92,0);
		}
		
	task close_eff( obj, len ){
		wait( 90 );
		ObjShot_SetIntersectionEnable( obj, false );
		descent( i in 0..len*(18/len)){
			ObjLaser_SetRenderWidth( obj, i/(18/len) );
			yield;
		}
		Obj_Delete( obj );
	}
		
	task slash_eff01( obj ){
		ascent( i in 0..255/9 ){
			ObjRender_SetAlpha( obj, i*9 );
			yield;
		}
	}
			
			
			task delayadd( obj ){
				wait( 60 );
				PlaySound("BulletWave",92,0);
				ObjMove_AddPatternA2( obj, 0, NO_CHANGE, NO_CHANGE, 0.05, 0, 20 );
			}
			
		task SlashSub( obj, w_angle ){ 
		let u_d = [ 0, 180 ];
		let select = round( rand( 0, 1 ));
		wait( 5 );
		let a = 4.2;
		let b = 3.8;
            while( !Obj_IsDeleted( obj )){
				let speed = absolute( rand( a, b ) );
				loop( 1 ){
	                let way = 1;
                    let shote = 49;
					let delay = 0;
					loop( way ){
					
              			let sx = ObjMove_GetX( obj );
              			let sy = ObjMove_GetY( obj );
				
						let w_r = 0;
						loop( 7 ){
							let angle = ObjMove_GetAngle( obj ) + rand( -90, 90 );
							let sht1 = TShotA2( sx + w_r*cos( w_angle ), sy + w_r*sin( w_angle ), speed, angle, 0, speed, shote, delay + w_r/20 );
							ObjShot_SetSpellResist( sht1, true );
							eff_scale02( sht1 );
							eff_alpha02( sht1 );
							w_r += 7.5;
						}

						//angle += 360 / way;
				}
			}
			a -= 0.13;
			b -= 0.13;
			select++;
			wait( 1 );
		}
		}
		
		task auto_dist( obj ){
			ObjShot_SetAutoDelete( obj, false );
			wait( 100 );
			ObjShot_SetAutoDelete( obj, true );
		}

	task SlashDot( x, y ){
		PlaySound("LenenLaserDelayShort",52,0);
		TargetEffect(x, y, 40, 80, 1);
		wait( 80 );
		let angle = rand( 0, 360 );
		let way = 20;
		let len = 90;
		loop( way/2 ){
			CreateSlash( x, y, rand( 0, 360 ), len, 14, 49 );
			len += 10;
			angle += 360/way;
			PlaySound("LenenLaserShort",52,0);
			wait( 2 );
		}
	}
		

	task Snake( obj, a, angle, pangle ){
		//wait( rand( 0, 40 ) );
		wait( 0 );
		while( !Obj_IsDeleted( obj )){
			loop( 5 ){
				let x = ObjMove_GetX( obj );
				let y = ObjMove_GetY( obj );
				PatternSnake( x, y, a, angle, pangle );
				wait( 6 );
			}
			wait( 2*6 );
		}
	}

    task PatternSnake( sx, sy, mul, angle, pangle ){ 
		let a = 0;
        loop( 1 ){
		
            let speed = 1.2;
	        let way = 1;
            let shote = 9;
			let delay = 10;

			loop( way ){
				
				let sht1 = TShotA2( sx + a, sy, speed, angle, 0, speed, shote, delay );
				DelayX( sht1 );
				SpeedUn( sht1 );
				ObjShot_SetAutoDelete( sht1, false );
				ObjShot_SetSpellResist( sht1, true );
				SnakeWave( sht1, angle, 60*mul, 7, pangle );

				angle += 360 / way;
			
			}
			a += 10;
			PlaySound("LenenShot3",92,0);
			wait( 3 );
	}
   }
   
	task DelayX( obj ){
		while( ObjMove_GetX( obj ) > -20 ){ wait( 100 ); }
		Obj_Delete( obj );
	}
   
	task SpeedUn( obj ){
		let a = 0;
		while( !Obj_IsDeleted( obj )){
			ObjMove_SetSpeed( obj, 2 + sin( a )/1.5 );
			a += 6;
			yield;
		}
	}

task SnakeWave2(obj,angle,amplitude,omega,pangle)
{
   let count = 0;
   while(!Obj_IsDeleted(obj))
   {
         ObjMove_SetAngle(obj,angle+amplitude*sin((count*omega)+pangle));
         count += 1;
         wait( 2 );
   }
}

	task dummy{ 
		let a = 0;
		let select = [ -1, 1, 1, -1 ];
		let pangle = 0;
		while( !Obj_IsDeleted( objBoss )){
		
			let way = 1;
			let speed = 0.7;
			let shote = 1;
			let delay = 0;
			let angle = 270;

			loop( way ){

				let sx = 384 + 0;
				let sy = 448 + 0;
				
				let sht = TShotA2( sx, sy, speed, angle, 0, speed, shote, delay );
				Snake( sht, select[ a%length( select ) ], 180, pangle );
				ObjShot_SetSpellResist( sht, true );
				ObjShot_SetAutoDelete( sht, false );
				DelayDelete( sht, 1200 );
				ObjShot_SetIntersectionEnable( sht, false );
				ObjRender_SetAlpha( sht, 0 );

				angle += 360 / way;
			}
			pangle += 20;
			//a++;
			//PlaySound("LenenShot3",92,0);
			wait( 177 );
		}
	}

task CreateSlash(s_x, s_y, s_angle, s_len, s_wid, s_graphic){
	let objLaser = CreateStraightLaserA1(0, 0, s_angle, 0, 0, 99999, s_graphic, 0);
	
	ObjShot_SetAutoDelete(objLaser, false);
	ObjShot_SetSpellResist(objLaser, true);
	
	ObjStLaser_SetSource(objLaser, false);
	
	let s_timer = 30;	//Starting phase duration (adjustable)
	let e_timer = 20;	//Ending phase duration (adjustable)
	
	let c = cos(s_angle);
	let s = sin(s_angle);
	
	ascent(i in 0..s_timer){
		let tmp = sin((i+1)/s_timer*90);
		
		Update(2.5 - tmp * 1.5, tmp);
	}
	descent(i in 0..e_timer){
		let tmp = (i + 1) / e_timer;
		
		Update(1, tmp);
	}
	
	Obj_Delete(objLaser);
	
	function Update(rl, rw){
		let tmp_l = rl * s_len;
		let tmp_w = rw * s_wid;
		
		let inv_l = max(0, rl - 1) * 50 + 10;
		
		ObjLaser_SetInvalidLength(objLaser, inv_l, inv_l);
		
		ObjMove_SetPosition(objLaser, s_x - c * tmp_l / 2, s_y - s * tmp_l / 2);
		
		ObjLaser_SetLength(objLaser, tmp_l);
		ObjLaser_SetRenderWidth(objLaser, tmp_w);
		ObjLaser_SetIntersectionWidth(objLaser, tmp_w * 0.8);
	
		yield;
	}
}
	
	task eff_scale02( obj ){
		descent( i in 4..3*4 ){ 
			ObjRender_SetScaleXYZ( obj, i/4, i/4, 0 );
			yield;
		}
	}
	
	task eff_alpha02( obj ){
		ascent( i in 0..255/25 ){
			ObjRender_SetAlpha( obj, i*25 );
			yield;
			}
		//Obj_Delete( obj );
	}
	
	task eff_scale01( obj ){
	ascent( i in 10..3*10 ){
		if( Obj_IsDeleted( obj )){ break; }
		ObjRender_SetScaleXYZ( obj, i/10, i/10, 0 );
		yield;
		}
	}
	
	task eff_alpha01( obj ){
	descent( i in 0..255/25 ){
		ObjRender_SetAlpha( obj, i*25 );
		yield;
		}
	Obj_Delete( obj );
	}

	task TrapOperate{
		conrad;
		PlaySound("LenenLaserDelayLong",92,0);
		let barrier = MakeBarrierEffect_Ring( objBoss, 80, 200, 140 );
		ObjBarrier_SetInnerRadius( barrier, 0 );
		setrad( barrier );
		wait( 80 );
		Trap;
	}
	task setrad( barrier ){
		while( !Obj_IsDeleted( objBoss )){
			ObjBarrier_SetOuterRadius( barrier, GetCommonData( "radi", 0 ) );
			yield;
		}
	}
	
	task conrad{
		SetCommonData( "radi", 330 );
		wait( 80 );
		let a = 0.25;
		let b = 0;
		while( GetCommonData( "radi", 0 ) >= 180 ){
			SetCommonData( "radi", 330 - b );
			b += a;
			a -= 0.000208;
			yield;
		}
		SetCommonData( "radi", 180 );
	}
	
	task Trap{ 
        while( !Obj_IsDeleted( objBoss )){
			while( GetDistance( GetPlayerX, GetPlayerY, ObjMove_GetX( objBoss ), ObjMove_GetY( objBoss )) > GetCommonData( "radi", 0 ) ){ yield; }
			while( GetDistance( GetPlayerX, GetPlayerY, ObjMove_GetX( objBoss ), ObjMove_GetY( objBoss )) < GetCommonData( "radi", 0 ) ){

				let shote = 49;
				let way = 1;
				let delay = 5;
				let angle = rand( 0, 360 );
				let angle2 = angle + 90 + rand( -60, 60 );

				loop( way ){

					let r = -500;
					let r2 = -150;
					let sx = GetPlayerX + r*cos( angle );
					let sy = GetPlayerY + r*sin( angle );
					let sx2 = GetPlayerX + r2*cos( angle2 );
					let sy2 = GetPlayerY + r2*sin( angle2 );
				
					ProperLaser( sx, sy, angle, 1000, 20, 20, shote );
					ProperLaser( sx2, sy2, angle2, 300, 15, 20, shote );

					angle += 360 / way;

				}
				PlaySound("trap",92,0);
				wait( 30 );
			}
			yield;
		}
	}
	
	task HLaser( sx, sy, angle, length, width, duration, shote, delay ){
		ProperLaserDelay( sx, sy, angle, length, delay, shote );
		wait( delay );
		PlaySound("LenenLaserShort",92,0);
		ProperLaser( sx, sy, angle, length, width, duration, shote );
	}

	task ProperLaserDelay( sx, sy, angle, length, delay, shote ){
		let sht0 = CreateStraightLaserA1( sx, sy, angle, length, 4, delay, shote, 0 );
		Opening2( sht0, length );
		ObjShot_SetIntersectionEnable( sht0, false );
		wait( delay );
		Obj_Delete( sht0 );
	}

	task Opening2( obj, length ){
		ascent( i in 0..length/20 ){
			ObjLaser_SetLength( obj, i*20 );
			yield;
		}
	}

	task ProperLaser( sx, sy, angle, length, width, duration, shote ){
		let sht1 = CreateStraightLaserA1( sx, sy, angle, length, width, duration + 120, shote, 0 );
		//ObjRender_SetBlendType( sht1, BLEND_INV_DESTRGB );
		ObjLaser_SetGrazeInvalidFrame( sht1, 5 );
		wait( duration );

		ObjShot_SetIntersectionEnable( sht1, false );
		descent( i in 0..width/3 ){
			ObjLaser_SetRenderWidth( sht1, i*3 );
			wait( 2 );
		}
		Obj_Delete( sht1 );
	}
	   
   	function GetDotAngle( x1, y1, x2, y2 ){
		let angle=atan2( y1 - y2, x1 - x2 );
		return angle;
	}

	task StartCountDown
	{
		wait((60*60) - (10*60));
		loop(10)
		{
			PlaySound("time",100,0);
			wait(60);
		}

	}

task SnakeWave(obj,angle,amplitude,omega,pangle)
{
   let count = 0;
   while(!Obj_IsDeleted(obj))
   {
         ObjMove_SetAngle(obj,angle+amplitude*sin(count*omega));
         count += 1;
         wait( 2 );
   }
}
	
	task Rotate( obj, ang, t ){
	ObjMove_SetAngularVelocity( obj, ang );
	wait( t );
	ObjMove_SetAngularVelocity( obj, 0 );
	}
	
	task RotateMT( obj, ang, ang2, t ){
	ObjMove_SetAngularVelocity( obj, ang );
	wait( t );
	ObjMove_SetAngularVelocity( obj, ang2 );
	}
	
//----------------------------------------------------

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		EndBoss(2);
		DeleteShotAll(TYPE_ALL,TYPE_FADE);
		yield;
		CloseScript(GetOwnScriptID());
	}