#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["MI LIBRO"]
#Text["Dross S4"]
#System[".\system\Default_System.txt"]

#include"./lib/AllStarShot_Const.dnh"
#include"./lib/TheFunctions.txt"
#include"./BGTask.txt"
#include"./Animation_lib_satori.txt"

let Enemy = ID_INVALID;
let EnemyX = 0;
let EnemyY = 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 = DrossCutin;
let Diff = ["none","Easy","Normal","Hard","Abnormal"];
let Spellname = ["Number 1 -My Book, Luna de Pluton-","Shape Magic -Magical Oval-"];

let Mouv = 0;
let Mouv2 = 0;
let rango = 180;

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_SPELL_SCORE){SetScriptResult(10000000);}
	case(EV_REQUEST_LIFE){SetScriptResult(3000);}
	case(EV_REQUEST_TIMER){SetScriptResult(60);}

}

@Initialize{

	Enemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(Enemy);
SetShotAutoDeleteClip(50,50,50,50);
SetAutoDeleteObject(true);
ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID);
SetDamageRateAtFrame(Enemy,25,6,120);
name("Dross Rotzank",0,10);
//LifeCircleBar_A(Enemy,0.2,0.0,false);
//LifeCircleBar_B(Enemy,0.2,0.0,false);
//LifeCircleBar_Marker(Enemy,0.8);
//LifeCircleBar_MarkerEx(Enemy,0.8,1200);
//LifeStock(1,1);
TWaveCircle_Shadertask(Enemy,0);
TAura(Enemy,0);
LoadSounds;
PlaySFX(spell);
EnemyMarker(Enemy,Enemy);
ObjCutin_SetSpellcardS4(Spellname[GetLanguage], cutinImg, NAZRIN, 104, 104, 240);
ObjCutin_LaunchS3(BYAKUREN, cutinImg, Diff[GetDifficulty]);
SpellBGoverlay;
InitializeAction(Enemy,DrossDot);
DrawBoss_Task(Enemy);
SetAction(Enemy,ACT_MOVE,70);
	ObjMove_SetDestAtWeight(Enemy, GetStgFrameWidth/2, 110, 20,4);
	BossActions;
}

@MainLoop{

	yield;
}



task BossActions{

	ShootBullets;
//Texto;
	while(ObjEnemy_GetInfo(Enemy, INFO_LIFE) > 0){
		EnemyX = ObjMove_GetX(Enemy);
		EnemyY = ObjMove_GetY(Enemy);
		ObjEnemy_SetIntersectionCircleToShot(Enemy, EnemyX, EnemyY, 40);
		ObjEnemy_SetIntersectionCircleToPlayer(Enemy, EnemyX, EnemyY, 30);
		if(GetCommonData("Pluton Esta Vivo",false) == true){
		ObjEnemy_SetIntersectionCircleToShot(Enemy, GetStgFrameWidth/2, 0, 310);
		ObjEnemy_SetIntersectionCircleToPlayer(Enemy, GetStgFrameWidth/2, 0, 310);
		}
		yield;
	}
DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	
	Obj_Delete(Enemy);
	//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)}
		ExtendScreen2;
		loop(60){yield;}
	CloseScript(GetOwnScriptID);
}
function wait(t){loop(t){yield;}}

task ShootBullets{
let angul = rand(0,360);
let angul2 = 0;
wait(150);
ExtendScreen;

st3SpellBG2F;	
st3SpellBG1F;
st3SpellBG3F;			
PlaySFX(D1);
let Count1 = [0,17,23,28,32];
let Count2 = [0,3,4,5,5];
let as = 0;
let taim = 120;
let posx = 0;
let posy = 0;

wait(60);
PlaySFX(PlutonShort);
Luna(GetStgFrameWidth/2,0);

ObjMove_SetDestAtWeight(Enemy, GetStgFrameWidth/2, 110, 20,4);
//attack2;
wait(60);
SetCommonData("Pluton Esta Vivo",true);
DefenseActions;
let C1 = [0,8,9,10];
loop{
if(Obj_IsDeleted(Enemy)){break;}
PlaySFX(tan00);
as = rand(0,360);
ascent(i in 0..C1[GetDifficulty]){
if(Obj_IsDeleted(Enemy)){break;}
 CreateShotA1(GetStgFrameWidth/2,0,3,rand(0,360)+i*360/10,SMALL_GRAY,0);
 }
 wait(5);
  yield;
  }
}

function angleToPlayer{
let angle=atan2(GetPlayerY-EnemyY,GetPlayerX-EnemyX);
return angle;
}


task ExtendScreen{//credits to kirbio ;) thx
ascent(i in 0..30){
let s = sin((i+1)*90/30)^2;
let pastRatioX = GetPlayerX/GetStgFrameWidth;
let pastRatioY = GetPlayerY/GetStgFrameHeight;
SetStgFrame(32-32*s, 16-16*s, 416+224*s, 464+16*s, 20, 80);
SetPlayerClip(16, 16, 384+256*s-16, 448+32*s-16);
//ObjRender_SetAlpha(obj,stgalphastgalpha);
//stgalphastgalpha -= 10;
ObjMove_SetPosition(GetPlayerObjectID, GetStgFrameWidth*pastRatioX, GetStgFrameHeight*pastRatioY);
yield;
}
}

task ExtendScreen2{//credits to kirbio ;) thx
descent(i in 0..30){
let s = sin((i+1)*90/30)^2;
let pastRatioX = GetPlayerX/GetStgFrameWidth;
let pastRatioY = GetPlayerY/GetStgFrameHeight;
SetStgFrame(32-32*s, 16-16*s, 416+224*s, 464+16*s, 20, 80);
SetPlayerClip(16, 16, 384+256*s-16, 448+32*s-16);
//ObjRender_SetAlpha(obj,stgalphastgalpha);
//stgalphastgalpha -= 10;
ObjMove_SetPosition(GetPlayerObjectID, GetStgFrameWidth*pastRatioX, GetStgFrameHeight*pastRatioY);
yield;
}
}

task Luna(x,y){

 let sbobj2 = ObjPrim_Create(OBJ_SPRITE_2D);
    let sbimg = moon;
     
    let sbalpha = 0;
    let sbscroll = 0;
	let huehuehue = 0;
     
    LoadTexture(sbimg);
    ObjPrim_SetTexture(sbobj2,sbimg);
	 ObjSprite2D_SetSourceRect(sbobj2,0,0,256,256);
    ObjSprite2D_SetDestCenter(sbobj2);
    //ObjSprite2D_SetDestRect(sbobj2,-256,-256-100,256,256+100);
    Obj_SetRenderPriorityI(sbobj2,GetShotRenderPriorityI+1);
    //ObjRender_SetPosition(sbobj2,cx,cy,0);
    
	
	while(!Obj_IsDeleted(Enemy)){
	if(sbalpha<255){sbalpha+=6;}
if(sbalpha>255){sbalpha=255;}
if(sbscroll<3){sbscroll+=0.2;}
if(sbscroll>3){sbscroll=3;}
ObjRender_SetScaleXYZ(sbobj2,sbscroll,sbscroll,sbscroll);
ObjRender_SetAlpha(sbobj2,sbalpha);
    ObjRender_SetPosition(sbobj2,x,y,0);
	yield;
	}
	Obj_Delete(sbobj2);
}

task DefenseActions{
Attack2;
while(ObjEnemy_GetInfo(Enemy, INFO_LIFE) > 2000){yield;}
Attack3;
while(ObjEnemy_GetInfo(Enemy, INFO_LIFE) > 1000){yield;}
Attack4;
}

task Attack2{
let mot = 0;
loop{
if(Obj_IsDeleted(Enemy)){break;}
PlaySFX(laser);
 ascent(i in 0..6){ 
 LaserGrowth(GetStgFrameWidth/2,0,mot+i*360/6,600,30,60,ST_LASER_RED,60,55);
 }
 mot+=17.123456789;
 wait(30);
 }
}

task LaserGrowth(x,y,angle,w,l,d,g,dl,t){
 let oi =CreateStraightLaserA1(x,y,angle,0,l,d,g,dl);
 let dm = 0;
 loop(t){
 ObjLaser_SetLength(oi,dm);
 dm+= w/t;
 yield;
 }
 
}

task Attack3{
let pee = 0;
let angul = 0;
let angul2 = 0;
let C2 = [0,11.1234,9.1234,7.1234];
loop{
if(Obj_IsDeleted(Enemy)){break;}
PlaySFX(tan05);
loop(3){
if(Obj_IsDeleted(Enemy)){break;}
let a =CreateShotA2(GetStgFrameWidth/2,0,4,angul,-0.1,0,ARROW_GREEN,10);
MultiShot(a,angul,4);
//let b =CreateShotA2(EnemyX,EnemyY,4,angul2,-0.1,0,AMULET_BLUE,10);
//MultiShot(b,angul2);
angul+= 360/3;
angul2+= 360/3;
}
angul-=C2[GetDifficulty];
angul2-=7.1234;
 
wait(6);
}

}

task MultiShot(obj,angle,count){
wait(60);
let speed = 1.6;
if(!Obj_IsDeleted(obj)){
PlaySFX(kira00);	
ascent(b in 0..count){
CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),speed,angle+b*15,PELLET_RED,5);
speed+= 0.4;
}
}
Obj_Delete(obj);
}

task Attack4{
let C3 = [0,2,3,4];
loop{
if(Obj_IsDeleted(Enemy)){break;}
let ss = atan2(ObjMove_GetY(Player) - 0,ObjMove_GetX(Player) - GetStgFrameWidth/2);
ascent(i in 0..C3[GetDifficulty]){
PlaySFX(laser2);
if(Obj_IsDeleted(Enemy)){break;}
LaserToShot(GetStgFrameWidth/2,0,ss+i*2,ST_LASER_GREEN,OVAL_GREEN);
LaserToShot(GetStgFrameWidth/2,0,ss-i*2,ST_LASER_GREEN,OVAL_GREEN);
wait(5);
}
wait(240);
}
}

task LaserToShot(x,y,angle,graphic1,graphic2){
let tabla = CreateStraightLaserA1(x,y,angle,700,20,0,graphic1,90);
wait(90);
ascent(i in 0..8){
CreateShotA1(x,y,3+i,angle,graphic2,0);
}
}