#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["St1_S5"]
#Text["Survival Spell"]
#System[".\system\Default_System.txt"]

#include"./lib/WormShot_Const.dnh"
#include"./lib/TheFunctions.txt"
#include"./BGTask.txt"
#include"./Animation_lib_Kaori.txt"
#include"./Animation_lib_subRoka.txt"
#include"./nuclear.txt"

let Enemy = ID_INVALID;
let Roka = ID_INVALID;
let EnemyX = 0;
let EnemyY = 0;
let RokaX = 0;
let RokaY = 0;
let CPattern = 0;
//those Min and Max x y are taken from Shadow's Raimoo, Edited for full screen by Me(i am too honest :))
//if(ObjEnemy_GetInfo(Enemy, INFO_LIFE) == 0){break;}  my god "function"
let MinX = 0;
let MaxX = 640;
let MinY = 0;
let MaxY = GetStgFrameHeight*1.8;
let cutinImg = KaoriCutin;
let Diff = ["none","Easy","Normal","Hard","Abnormal"];
let Spellname = ["no","Tag-Team Attack \"Energy-Sucking Ball\""];

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_SPELL_SCORE){SetScriptResult(10000000);}
	case(EV_REQUEST_LIFE){SetScriptResult(1500);}
	case(EV_REQUEST_TIMER){SetScriptResult(90);}
	case(EV_REQUEST_IS_DURABLE_SPELL){SetScriptResult(true);}

}

@Initialize{

	Enemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	Roka = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(Enemy);
	ObjEnemy_Regist(Roka);
SetShotAutoDeleteClip(150,150,150,150);
SetAutoDeleteObject(true);

SetDamageRateAtFrame(Enemy,0,0,240);
name("Kaori Mystique",0,10);
name("Enzaki Roka",0,20);
//LifeCircleBar_A(Enemy,1,0.0,false);
//LifeCircleBar_B(Enemy,0.2,0.0,false);
//LifeCircleBar_Marker(Enemy,0.8);
//LifeCircleBar_MarkerEx(Enemy,0.8,1200);
//LifeStock(0,2);
TWaveCircle_Shadertask(Enemy,6);
TAura(Enemy,6);
LoadSounds;
PlaySFX(spell);
EnemyMarker(Enemy,Enemy);
EnemyMarker(Roka,Enemy);
SpellBGoverlay;
st1SpellBG2;	
st3SpellBG1;
//st1SpellBG3;				
ObjCutin_SetSpellcardS4(Spellname[GetLanguage], cutinImg, NAZRIN, 240, 104, 104);
ObjCutin_LaunchS3(YABUSAME, cutinImg, Diff[GetDifficulty]);
ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
BossImage;
BossImage_Roka;
//InitializeAction(Enemy,MichaelDot);
//DrawBoss_Task(Enemy);
//SetAction(Enemy,ACT_MOVE,70);
	ObjMove_SetPosition(Enemy, GetStgFrameWidth/2, 100);
	ObjMove_SetPosition(Roka, GetStgFrameWidth/2, GetStgFrameHeight/2);
	BossActions;
}

@MainLoop{

	yield;
}



task BossActions{

	ShootBullets;
//Texto;
	while(ObjEnemy_GetInfo(Enemy, INFO_LIFE) > 0){
		EnemyX = ObjMove_GetX(Enemy);
		EnemyY = ObjMove_GetY(Enemy);
		RokaX = ObjMove_GetX(Roka);
		RokaY = ObjMove_GetY(Roka);
		//ObjEnemy_SetIntersectionCircleToShot(Enemy, EnemyX, EnemyY, 40);
		//ObjEnemy_SetIntersectionCircleToPlayer(Enemy, EnemyX, EnemyY, 30);
		yield;
	}
DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	
	Obj_Delete(Enemy);
	Obj_Delete(Roka);
	//loop(60){yield;}
	
		if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_PLAYER_SHOOTDOWN_COUNT) +ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_PLAYER_SPELL_COUNT) == 0)
		{AddScore(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID,INFO_SPELL_SCORE));}
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID(), INFO_PLAYER_SHOOTDOWN_COUNT) == 0 && ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID(), INFO_PLAYER_SPELL_COUNT) == 0 ){
	SetCommonData("Perfects",GetCommonData("Perfects",perfects)+1);
	}
	if(GetCommonData("Perfects",perfects) == 5){SetPlayerLife(GetPlayerLife+1); SetCommonData("Perfects",0);NotifyEventAll(EV_USER+999,0)}
		loop(60){yield;}
	CloseScript(GetOwnScriptID);
}
function wait(t){loop(t){yield;}}

task ShootBullets{
let angul = 0;
let angul2 = 0;
wait(150);

let as = 0;
let taim = 120;
let FCount = [0,10,14,17,20];
let PCount = [0,4,2,2,1];
let Tiem = [0,20,40,40,80];
let ang = 270;
let px = ObjMove_GetX(Player);
let py = ObjMove_GetY(Player);
PlaySFX(tan04);
NuclearShot(2,EnemyX,EnemyY,-3,0,0.5,0,0,"PURPLE",90+(130*5),true);
ObjMove_SetDestAtWeight(Enemy, GetStgFrameWidth/2, 200, 20,5);
wait(90);
Roka_Pattern;

loop(5){
	px = ObjMove_GetX(Player);
    py = ObjMove_GetY(Player);
	if(Obj_IsDeleted(Enemy)){break;}
	PlaySFX(laser);
	CreateStraightLaserA1(EnemyX,EnemyY ,getAtanAngle(Enemy,Player) ,GetDistance(EnemyX,EnemyY,px,py) ,10 ,0 ,ST_LASER_PURPLE ,30 );
    wait(30);
    ascent(i in 0..FCount[GetDifficulty]){
    	PlaySFX(tan04);
    	FlowerShot(EnemyX,EnemyY,getAtanAngle(Enemy,Player)+i*360/FCount[GetDifficulty],5,SMALL_PURPLE,PELLET_PURPLE,10);
    }
    ObjMove_SetDestAtWeight(Enemy, px, py, 20,5);

    wait(100);
    }

    CPattern = 1;
    NuclearShot(2,EnemyX,EnemyY,0,0,0.8,0,0,"PURPLE",90+(130*6),true);
    wait(90);

    loop(6){
	px = ObjMove_GetX(Player);
    py = ObjMove_GetY(Player);
    let angloP = getAtanAngle(Enemy,Player);
	if(Obj_IsDeleted(Enemy)){break;}
	PlaySFX(laser);
	CreateStraightLaserA1(EnemyX,EnemyY ,angloP,GetDistance(EnemyX,EnemyY,px,py) ,10 ,0 ,ST_LASER_PURPLE ,30 );
    wait(30);
    ascent(i in 0..FCount[GetDifficulty]){
    	PlaySFX(tan04);
    	FlowerShot(EnemyX,EnemyY,getAtanAngle(Enemy,Player)+i*360/FCount[GetDifficulty],5,SMALL_PURPLE,PELLET_PURPLE,10);
    }
    Pilar(Enemy,angloP,Tiem[GetDifficulty],PCount[GetDifficulty],SMALL_BLUE,PELLET_BLUE);

    ObjMove_SetDestAtWeight(Enemy, px, py, 20,5);

    wait(100);
    }

    CPattern = 2;
    NuclearShot(2,EnemyX,EnemyY,0,0,1,0,0,"PURPLE",90+(130*8),true);
    wait(90);

    loop(8){
	px = ObjMove_GetX(Player);
    py = ObjMove_GetY(Player);
    let angloP = getAtanAngle(Enemy,Player);
	if(Obj_IsDeleted(Enemy)){break;}
	PlaySFX(laser);
	CreateStraightLaserA1(EnemyX,EnemyY ,angloP,GetDistance(EnemyX,EnemyY,px,py) ,10 ,0 ,ST_LASER_PURPLE ,30 );
    wait(30);
    ascent(i in 0..FCount[GetDifficulty]){
    	PlaySFX(tan04);
    	FlowerShot(EnemyX,EnemyY,getAtanAngle(Enemy,Player)+i*360/FCount[GetDifficulty],5,SMALL_PURPLE,PELLET_PURPLE,10);
    }
    Pilar(Enemy,angloP,Tiem[GetDifficulty],PCount[GetDifficulty],SMALL_BLUE,PELLET_BLUE);
    CheetoLaser(EnemyX,EnemyY,4,angloP+180,50,10,SMALL_RED,3);
	CheetoLaser(EnemyX,EnemyY,4,angloP+180,50,10,SMALL_RED,-3);

    ObjMove_SetDestAtWeight(Enemy, px, py, 20,5);

    wait(100);
    }

    CPattern = 3;
    let BSize = [0,0.25,0.3,0.35,0.4];
    ObjMove_SetDestAtWeight(Enemy, GetStgFrameWidth/2, GetStgFrameHeight/2, 20,4);
    wait(120);
    let getmu = 0;
    PlaySFX(tan04);
    ascent(u in 0..3){
        NuclearShot(2,EnemyX,EnemyY,0+u*360/3,1,BSize[GetDifficulty],220,0,"PURPLE",99999999,true);
        NuclearShot(2,EnemyX,EnemyY,0+u*360/3,-1,BSize[GetDifficulty],110,0,"BLUE",99999999,true);
    }
    loop{
    	if(Obj_IsDeleted(Enemy)){break;}
    	PlaySFX(tan00);
    	ascent(i in 0..5){
    	    let a = CreateShotA2(RokaX,RokaY ,5 ,getmu+i*360/5 ,-0.05 ,-5 ,SMALL_RED ,0 );
    	    ObjShot_SetDeleteFrame(a,200 );
        }
        getmu+=2;
    	wait(5);
    }
    }
function angleToPlayer{
let angle=atan2(GetPlayerY-EnemyY,GetPlayerX-EnemyX);
return angle;
}


task Roka_Pattern{
let anglel = 0;
let Count = [0,10,15,20,25];
	while(CPattern == 0){
		anglel = rand(0,360);
		PlaySFX(tan02);
		ascent(i in 0..Count[GetDifficulty]){
			CreateShotA2(RokaX,RokaY ,1 ,anglel+i*360/Count[GetDifficulty] ,0.05 ,5 ,OVAL_RED ,10 );
		}
		wait(90);
	}

	while(CPattern == 1){
		anglel = rand(0,360);
		PlaySFX(tan02);
		ascent(i in 0..Count[GetDifficulty]){
			CreateShotA2(RokaX,RokaY ,1 ,anglel+i*360/Count[GetDifficulty] ,0.05 ,5 ,OVAL_RED ,10 );
		}
		wait(80);
	}

	while(CPattern == 2){
		anglel = rand(0,360);
		PlaySFX(tan02);
		ascent(i in 0..Count[GetDifficulty]){
			CreateShotA2(RokaX,RokaY ,1 ,anglel+i*360/Count[GetDifficulty] ,0.05 ,5 ,OVAL_RED ,10 );
		}
		wait(70);
	}
	wait(1200);
    Attack3;
	while(!Obj_IsDeleted(Enemy)){
		PlaySFX(tan01);
		CreateShotA1(RokaX,RokaY ,2 ,rand(0,360) ,SMALL_BLUE ,10 );
		wait(5);
	}


}

task Attack3{
	wait(600);
	let Count = [0,10,15,20,25];
	while(!Obj_IsDeleted(Enemy)){
		ascent(i in 0..Count[GetDifficulty]){
			CreateShotA1(RokaX,RokaY ,2 ,0+i*360/Count[GetDifficulty] ,PELLET_GREEN ,10 );
		}
		wait(60);
	}
}


task FlowerShot(x,y,angle,speed,graphic,graphic2,radius){
let f1 = CreateShotA2(x,y,0,angle,0.05,speed,graphic,10);
//ObjShot_SetDeleteFrame(f1,rand(80,100) );
let sub1= CreateShotA1(x+radius*cos(angle),y+radius*sin(angle),speed,angle,graphic2,10);
let sub2= CreateShotA1(x+radius*cos(angle+72),y+radius*sin(angle+72),speed,angle,graphic2,10);
let sub3= CreateShotA1(x+radius*cos(angle+144),y+radius*sin(angle+144),speed,angle,graphic2,10);
let sub4= CreateShotA1(x+radius*cos(angle+216),y+radius*sin(angle+216),speed,angle,graphic2,10);
let sub5= CreateShotA1(x+radius*cos(angle+288),y+radius*sin(angle+288),speed,angle,graphic2,10);
while(!Obj_IsDeleted(f1)){
ObjMove_SetPosition(sub1,ObjMove_GetX(f1)+radius*cos(angle),ObjMove_GetY(f1)+radius*sin(angle));
ObjMove_SetPosition(sub2,ObjMove_GetX(f1)+radius*cos(angle+72),ObjMove_GetY(f1)+radius*sin(angle+72));
ObjMove_SetPosition(sub3,ObjMove_GetX(f1)+radius*cos(angle+144),ObjMove_GetY(f1)+radius*sin(angle+144));
ObjMove_SetPosition(sub4,ObjMove_GetX(f1)+radius*cos(angle+216),ObjMove_GetY(f1)+radius*sin(angle+216));
ObjMove_SetPosition(sub5,ObjMove_GetX(f1)+radius*cos(angle+288),ObjMove_GetY(f1)+radius*sin(angle+288));

ObjMove_SetAngle(sub1,angle );
ObjMove_SetAngle(sub2,angle+72 );
ObjMove_SetAngle(sub3,angle+144 );
ObjMove_SetAngle(sub4,angle+216 );
ObjMove_SetAngle(sub5,angle+288 );

angle+=3;
yield;
}
Obj_Delete(sub1);
Obj_Delete(sub2);
Obj_Delete(sub3);
Obj_Delete(sub4);
Obj_Delete(sub5);
}



task Pilar(obj,angle,time,waita,graphic,graphic2){

let angol = angle+90;
let angol2 = angle-90;
let asd = 0;
let asd2 = 0;
let radius= 0;
loop(time){
 let t =CreateShotA2(ObjMove_GetX(obj)+radius*cos(angol),ObjMove_GetY(obj)+radius*sin(angol),0,(angle+90)+asd,0.01,3.5,graphic,10);
 ObjShot_SetGraphicAtFrame(t,graphic2,30);
 let t2 =CreateShotA2(ObjMove_GetX(obj)+radius*cos(angol2),ObjMove_GetY(obj)+radius*sin(angol2),0,(angle-90)+asd2,0.01,3.5,graphic,10);
 ObjShot_SetGraphicAtFrame(t2,graphic2,30);
 PlaySFX(tan05);	
 SEatFrame(kira00,30);
 radius = 0+sin(asd)*40;
 asd+= 10;
 asd2-= 10;
 wait(waita);
 }
}
task ObjShot_SetGraphicAtFrame(obj,graphic,frame){
wait(frame);
ObjShot_SetGraphic(obj,graphic);
}
task SEatFrame(se,frame){
wait(frame);
PlaySFX(se);	
}

task CheetoLaser(x,y,speed,angle,l,w,graphic,anv){
let obj = CreateCurveLaserA1(x,y,speed,angle,l,w,graphic,0);
ObjCrLaser_SetTipDecrement(obj,0);
ObjMove_SetAngularVelocity(obj,anv);
wait(60);
ObjMove_SetAngularVelocity(obj,0);
speed = 6;
	let a = 90;
	let a_spd = 3; // rate of angle change
	loop(40){
		let angle_to = GetAngleToPlayer(obj);

		// choose to increase or decrease angle depending on which is closer
		let da = [a_spd, -a_spd][((a - angle_to) % 360) < 180]; 

		// if the angles are close enough, just snap instead
		if( (|a - angle_to|) < a_spd){
			a = angle_to;
		}else{
			a = (a + da) % 360;
		}

		ObjMove_SetAngle(obj, a);

		// optionally scale speed so it slows down to adjust angle
		let scale = (|(a - angle_to) % 360 - 180|) / 180; // ranges [0,1]; is 1 when angles are equal
		ObjMove_SetSpeed(obj, speed * scale); // squaring also optional, increases slowdown
		yield;
	}
}