#TouhouDanmakufu[Stage]
#Title["Reflection Cave [Lunatic]"]
#Text["A Nice Cruise to Kalos[r]LOCAA 7 Contest Entry[r]By Meowstic"]
#Player["./Player/Orin/Orin.dnh"]
#ScriptVersion[3]

#include "./EverythingElse/Functions/Index.txt"
#include "./EverythingElse/Functions/BGStage.txt"
let objBoss; //Because errors.
let PlayX;
let PlayY;

let bgm1path = GetCurrentScriptDirectory ~ "EverythingElse/Functions/se/bgmstage.ogg";
let bgm2path = GetCurrentScriptDirectory ~ "EverythingElse/Functions/se/bgmboss.ogg";
let paths = [bgm1path,bgm2path];

@Initialize{
SetPlayerLife(5);
SetCommonData("musicswap",0);
STAGE;
BGM;
PoC;
}

@MainLoop{
if(GetPlayerState()==STATE_END){
	wait(120);
	CloseStgScene();
}

yield;
}

@Finalize{}

task PoC{
loop{
CollectItemsInCircle(ObjMove_GetX(GetPlayerObjectID), ObjMove_GetY(GetPlayerObjectID), 50);
if(GetPlayerY<120){CollectAllItems;}
yield;
}
}

task BGM{
let a = ObjSound_Create;
let b = ObjSound_Create;
let bgm = [a,b];
ascent(i in 0..2){ObjSound_Load(bgm[i],paths[i]); 
ObjSound_SetLoopEnable(bgm[i],true); 
ObjSound_SetVolumeRate(bgm[i],90);}
ObjSound_SetLoopTime(bgm[0], 4.992, 60+12.270);
ObjSound_SetLoopTime(bgm[1], 10.389, 60+28.244);
ObjSound_Play(bgm[0]);
while(GetCommonData("musicswap",0) < 1){yield;}
ObjSound_Stop(bgm[0]);
ObjSound_Play(bgm[1]);
while(GetCommonData("musicswap",0) < 2){yield;}
let volume = ObjSound_GetVolumeRate(bgm[1]);
while(volume>0){
	volume--;
	ObjSound_SetVolumeRate(bgm[1],volume);
	wait(2);
}
}

/*
let obj = ObjSound_Create();
ObjSound_Load(obj, GetCurrentScriptDirectory ~ "EverythingElse/Functions/se/bgmstage.ogg");
ObjSound_SetSoundDivision(obj, SOUND_BGM);
ObjSound_SetLoopEnable(obj, true);
ObjSound_SetLoopTime(obj, 4.992, 60+12.270);
ObjSound_Play(obj);
*/

task STAGE{
    let dir=GetCurrentScriptDirectory();
    let obj=ObjEnemyBossScene_Create();
    let obj2=ObjEnemyBossScene_Create();
	PlaytestInvincibility;
    ObjEnemyBossScene_Add(obj2,0,dir~"./EverythingElse/MNS1L.txt");
    ObjEnemyBossScene_Add(obj2,0,dir~"./EverythingElse/MSP1L.txt");
	ObjEnemyBossScene_Add(obj,0,dir~"./EverythingElse/Dialogue.txt");
    ObjEnemyBossScene_Add(obj,0,dir~"./EverythingElse/NS1L.txt");
    ObjEnemyBossScene_Add(obj,0,dir~"./EverythingElse/SP1L.txt");
    ObjEnemyBossScene_Add(obj,1,dir~"./EverythingElse/NS2L.txt");
    ObjEnemyBossScene_Add(obj,1,dir~"./EverythingElse/SP2L.txt");
    ObjEnemyBossScene_Add(obj,2,dir~"./EverythingElse/NS3L.txt");
	ObjEnemyBossScene_Add(obj,2,dir~"./EverythingElse/SP3L.txt");
    ObjEnemyBossScene_Add(obj,3,dir~"./EverythingElse/NS4L.txt");
	ObjEnemyBossScene_Add(obj,3,dir~"./EverythingElse/SP4L.txt");
	ObjEnemyBossScene_Add(obj,4,dir~"./EverythingElse/NS5L.txt");
	ObjEnemyBossScene_Add(obj,4,dir~"./EverythingElse/SP5L.txt");
	ObjEnemyBossScene_Add(obj,4,dir~"./EverythingElse/DialogueEnd.txt");
	ObjEnemyBossScene_LoadInThread(obj2);
    ObjEnemyBossScene_LoadInThread(obj);
let modifier = 0;
wait(280);
loop(2){
SolosisSpawn(-25,50,0, 1, 1);
wait(120);
SolosisSpawn(GetStgFrameWidth+25,50,180, 1, -1);
wait(120)
}
SolosisSpawn(-25,50,0, 1, 1);
SolosisSpawn(GetStgFrameWidth+25,50,180, 1, -1);
wait(350);
RoggenSpawn(GetCenterX-100,0,90, 2, 1,450); 
RoggenSpawn(GetCenterX+100,0,90, 2, 2,450);
wait(320);
RoggenSpawn(GetCenterX-150,0,90, 2, 1,450);
RoggenSpawn(GetCenterX,0,90, 2, 3,450);
RoggenSpawn(GetCenterX+150,0,90, 2, 2,450);
wait(400);
ChingSpawn(GetCenterX+60,0,90, 2, 2,500);
ChingSpawn(GetCenterX-60,0,90, 2, 1,500);
wait(500);
loop(5){
RoggenSpawn(GetCenterX-150+modifier,0,90, 2, 1, 400);
wait(40);
modifier += 50;
}
modifier = 0;
wait(200);
loop(5){
RoggenSpawn(GetCenterX+150-modifier,0,90, 2, 2, 400);
wait(40);
modifier += 50;
}
wait(400);
ObjEnemyBossScene_Regist(obj2);
    while(!Obj_IsDeleted(obj2)){
        yield;
    }
	CreateItemA1(ITEM_1UP , GetCenterX, 150, 500);	
	loop(35){
	CreateItemA1(ITEM_POINT_S, GetCenterX+rand(-75,75), 150+rand(-75,75), 500);
	}
	loop(10){
	CreateItemA1(ITEM_POINT, GetCenterX+rand(-75,75), 150+rand(-75,75), 5000);
	}
SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/death.wav", 90);
wait(300);
SolosisSpawn(-25,50,0, 1, 1);
SolosisSpawn(GetStgFrameWidth+25,50,180, 1, -1);
wait(250);
SolosisSpawn(-25,50,0, 1, 1);
SolosisSpawn(GetStgFrameWidth+25,50,180, 1, -1);
wait(350);
RoggenSpawn(GetCenterX-100,0,90, 2, 1, 400);
RoggenSpawn(GetCenterX+100,0,90, 2, 2, 400);
ChingSpawn(GetCenterX,0,90, 2, 3, 400);
wait(450);
MimeSpawn(GetCenterX-100,0,90, 2, 1, 400);
MimeSpawn(GetCenterX+100,0,90, 2, 2, 400);
WobbaSpawn(GetCenterX,0,90, 2, 3, 800);
wait(700);
MimeSpawn(GetCenterX-130,0,90, 2, 1, 400);
MimeSpawn(GetCenterX+130,0,90, 2, 1, 400);
ChingSpawn(GetCenterX,0,90, 2, 3, 400);
wait(500);
MimeSpawn(GetCenterX-130,0,90, 2, 1, 700);
wait(30);
MimeSpawn(GetCenterX+130,0,90, 2, 2, 700);
wait(30);
MimeSpawn(GetCenterX,0,90, 2, 3, 700);
wait(120);
SolosisSpawn(-25,50,0, 1, 1);
SolosisSpawn(GetStgFrameWidth+25,50,180, 1, -1);
wait(420);
RoggenSpawn(GetCenterX-150,0,90, 2, 1, 400);
RoggenSpawn(GetCenterX,0,90, 2, 3, 400);
RoggenSpawn(GetCenterX+150,0,90, 2, 2, 400);
wait(400);
SableyeSpawn(GetCenterX,0,90, 2, 3,1000);
wait(1200);
ObjEnemyBossScene_Regist(obj);
    while(!Obj_IsDeleted(obj)){
        yield;
    }
wait(350);
CloseStgScene;
}

task SolosisSpawn(x,y,ang,spd, mult){
let sol = ObjEnemy_Create(OBJ_ENEMY);
ObjEnemy_Regist(sol);
ObjEnemy_SetLife(sol,350);
ObjMove_SetPosition(sol,x,y);
rendersolosis(sol);
SolosisBehavior(sol,ang,spd, mult);
Hitboxes(sol);
BossShotSound(sol, 50, 0);
task SolosisBehavior(tar,ang,spd,mult){
	ObjMove_SetAngle(tar, ang);
	ObjMove_SetSpeed(tar, spd);
	let angle = ObjMove_GetAngle(tar);
	Deletion(tar);
		while(ObjEnemy_GetInfo(tar,INFO_LIFE) > 0) {
	let X = ObjMove_GetX(tar);
	let Y = ObjMove_GetY(tar);
	ascent(i in 0..4){
	let obj = CreateShotA1(X+25*cos(angle),Y+25*sin(angle),2.5,angle+(90*i),BALL_S_MAGENTA,5);
		AngVel(obj,mult);
	}
	angle+=15*mult;
	SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/smbullet.wav", 65);
	wait(5);
}
}
task AngVel(tar,add){
ObjMove_SetAngularVelocity(tar, -2*add);
wait(60);
ObjMove_SetAngularVelocity(tar, 0);
}
task Deletion(tar){
wait(60*8.5);
Obj_Delete(tar);
}
}

task RoggenSpawn(x,y,ang,spd,mult,tim){
let rog = ObjEnemy_Create(OBJ_ENEMY);
ObjEnemy_Regist(rog);
ObjEnemy_SetLife(rog,400);
ObjMove_SetPosition(rog,x,y);
renderroggen(rog);
RoggenBehavior(rog,ang,spd, mult);
Hitboxes(rog);
MoveDespawn(rog, mult,tim );
BossShotSound(rog, 50, 0);
task RoggenBehavior(tar,ang,spd,mult){
	ObjMove_SetAngle(tar, ang);
	ObjMove_SetDestAtWeight(tar,ObjMove_GetX(tar),ObjMove_GetY(tar)+100,25,3);
		wait(60);;
	let speed = 2;
	while(ObjEnemy_GetInfo(tar,INFO_LIFE) > 0) {
	let X = ObjMove_GetX(tar);
	let Y = ObjMove_GetY(tar);
	let angle = GetAngleToPlayer(tar)-90;
	let def = GetAngleToPlayer(tar)-90;
	loop(8){
	loop(5){
	CreateShotA1(X+25*cos(angle),Y+25*sin(angle),speed,angle,SCALE_ORANGE,25);
	angle += 45;
	}
	angle = def;
	speed += 0.2;
	SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/smbullet.wav", 65);
	wait(2);
	}
	speed = 2;
	loop(9){
	CreateShotA1(X+25*cos(angle),Y+25*sin(angle),speed,angle,BALL_M_YELLOW,15);
	angle += 22.5;
	}
	angle = GetAngleToPlayer(tar)-90;
	SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/labullet.wav", 65);
	wait(60);
}
}
}

task ChingSpawn(x,y,ang,spd, mult,tim){
let chi = ObjEnemy_Create(OBJ_ENEMY);
ObjEnemy_Regist(chi);
ObjEnemy_SetLife(chi,600);
ObjMove_SetPosition(chi,x,y);
renderchingie(chi);
ChingBehavior(chi,ang,spd, mult);
Hitboxes(chi);
MoveDespawn(chi, mult, tim);
BossShotSound(chi, 50, 0);
task ChingBehavior(tar,ang,spd,mult){
	ObjMove_SetAngle(tar, ang);
	ObjMove_SetDestAtWeight(tar,ObjMove_GetX(tar),ObjMove_GetY(tar)+100,25,3);
		wait(60);
		let count = 1;
		let multi = 0;
	while(ObjEnemy_GetInfo(tar,INFO_LIFE) > 0) {
	let X = ObjMove_GetX(tar);
	let Y = ObjMove_GetY(tar);
	ascent(i in 1..4){
	if(Obj_IsDeleted(tar)){break;}
	let angle = GetAngleToPlayer(tar)+i*-12.5;
	loop(i*6){
	let obj = CreateShotA1(X+25*cos(angle),Y+25*sin(angle),2,angle+(i*-multi),KUNAI_MINT,25);
	let obj2 = CreateShotA1(X+25*cos(angle),Y+25*sin(angle),2,angle+(i*-multi)+120,KUNAI_MINT,25);
	let obj3 = CreateShotA1(X+25*cos(angle),Y+25*sin(angle),2,angle+(i*-multi)+240,KUNAI_MINT,25);
	if(i==2){ObjShot_SetGraphic(obj, KUNAI_AZURE);ObjShot_SetGraphic(obj2, KUNAI_AZURE);ObjShot_SetGraphic(obj3, KUNAI_AZURE);}
	if(i==3){ObjShot_SetGraphic(obj, KUNAI_WATERMELON);ObjShot_SetGraphic(obj2, KUNAI_WATERMELON);ObjShot_SetGraphic(obj3, KUNAI_WATERMELON);}
	angle += 5;
	}
	multi =  0.9*3;
	if(i==2){multi+=0.7;}
	wait(15);
	if(i==1){SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/note1.wav", 65);}
	if(i==2){SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/note2.wav", 65);}
	if(i==3){SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/note3.wav", 65);}
	if(i==3){multi = 0;}
	}
	wait(30);
	}
}
}

task WobbaSpawn(x,y,ang,spd, mult, tim){
let wob = ObjEnemy_Create(OBJ_ENEMY);
ObjEnemy_Regist(wob);
ObjEnemy_SetLife(wob,1000);
ObjMove_SetPosition(wob,x,y);
renderwobba(wob);
WobbaBehavior(wob,ang,spd, mult);
Hitboxes(wob);
MoveDespawn(wob, mult, tim);
task WobbaBehavior(tar,ang,spd,mult){
	ObjMove_SetDestAtWeight(tar,ObjMove_GetX(tar),ObjMove_GetY(tar)+100,25,3);
	let X = ObjMove_GetX(tar);
	let Y = ObjMove_GetY(tar);
	BossShotWobbaffet(X,Y,tar,50)
}
}

task MimeSpawn(x,y,ang,spd, mult, tim){
let mr = ObjEnemy_Create(OBJ_ENEMY);
ObjEnemy_Regist(mr);
ObjEnemy_SetLife(mr,400);
ObjMove_SetPosition(mr,x,y);
rendermrmime(mr);
MimeBehavior(mr,ang,spd, mult);
Hitboxes(mr);
MoveDespawn(mr, mult, tim);
BossShotSound(mr, 50, 0);
task MimeBehavior(tar,ang,spd,mult){
	ObjMove_SetAngle(tar, ang);
	ObjMove_SetDestAtWeight(tar,ObjMove_GetX(tar),ObjMove_GetY(tar)+70,25,3);
		wait(60);;
	while(ObjEnemy_GetInfo(tar,INFO_LIFE) > 0) {
	let X = ObjMove_GetX(tar);
	let Y = ObjMove_GetY(tar);
	let offset = 0;
	loop(20){
	CreateShotA2(X-50+offset,Y+10,0,offset*2,0.04,3,BALL_M_PURPLE,35);
	offset += 5;
	}
	SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/barrage2.wav", 65);
	offset = GetAngleToPlayer(tar)-90;
	wait(90);
	}
}
}

task BossShotWobbaffet(x,y,tar,vol){
	while(!Obj_IsDeleted(tar)) {
	let angle = GetAngleToPlayer(tar);
	if(ObjEnemy_GetInfo(tar,INFO_SHOT_HIT_COUNT)>0){
	SE_Play(GetCurrentScriptDirectory ~ "sfx/damage1.wav", vol);
	let obj = CreateShotA1(x+rand(-25,25),y+rand(-25,25),rand(2,3.5),angle,PELLET_0_GRAY,15);
	loop(7){
	angle += 360/7;
	let obj2 = CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),rand(2,3.5),angle,PELLET_0_GRAY,15);
	}
	Obj_Delete(obj);
	SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/smbullet.wav", 65);
	yield;
	}
	x = ObjMove_GetX(tar);
	y = ObjMove_GetY(tar);
	yield;
	}
}

task SableyeSpawn(x,y,ang,spd,mult,tim){
let sab = ObjEnemy_Create(OBJ_ENEMY);
ObjEnemy_Regist(sab);
ObjEnemy_SetLife(sab,2000);
ObjMove_SetPosition(sab,x,y);
rendersableye(sab);
SableyeBehavior(sab,ang,spd, mult);
SableyeDie(sab);
SableyePattern2(sab);
MoveDespawn(sab, mult, tim);
BossShotSound(sab, 50, 500);
task SableyeBehavior(tar,ang,spd,mult){
	ObjMove_SetAngle(tar, ang);
	ObjMove_SetDestAtWeight(tar,ObjMove_GetX(tar),ObjMove_GetY(tar)+180,25,3);
	wait(60);
	SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/charge.wav", 100);
	ConcentrationA1(tar,"Nothing",70,7,120,[85,15,150]);
	wait(70);
	let speed = 1.5;
	let X = 0;
	let Y = 0;
	while(ObjEnemy_GetInfo(tar,INFO_LIFE) > 0) {
	let angle = GetAngleToPlayer(tar)-90;
	loop(24){
	if(Obj_IsDeleted(tar)){break;}
	X = ObjMove_GetX(tar);
	Y = ObjMove_GetY(tar);
	CreateShotA1(X+25*cos(angle),Y+25*sin(angle),speed,angle,RICE_M_PURPLE_INV,25);
	SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/labullet.wav", 65);
	angle += 8;
	wait(1);
	}
	wait(10);
	angle = GetAngleToPlayer(tar)+90;
	loop(24){
	if(Obj_IsDeleted(tar)){break;}
	X = ObjMove_GetX(tar);
	Y = ObjMove_GetY(tar);
	CreateShotA1(X+25*cos(angle),Y+25*sin(angle),speed,angle,RICE_M_PURPLE_INV,25);
	SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/labullet.wav", 65);
	angle -= 8;
	wait(1);
	}
	wait(10);
}
}
task SableyePattern2(tar){
	wait(190);
	let angle = GetAngleToPlayer(tar)-90;
	let angmod = 0;
	loop{
	if(Obj_IsDeleted(tar)){break;}
	let X = ObjMove_GetX(tar);
	let Y = ObjMove_GetY(tar);
	CreateShotA1(X+25*cos(angle),Y+25*sin(angle),2,-angle+180+angmod,RICE_PURPLE_INV,5);
	CreateShotA1(X+25*cos(angle),Y+25*sin(angle),2,-angle+angmod,RICE_PURPLE_INV,5);
	angle += 20;
	angmod += 1;
	wait(1);
	}
}
}


task Hitboxes(tar){
while(ObjEnemy_GetInfo(tar,INFO_LIFE) > 0) {
ObjEnemy_SetIntersectionCircleToShot(tar,ObjMove_GetX(tar),ObjMove_GetY(tar),24);
ObjEnemy_SetIntersectionCircleToPlayer(tar,ObjMove_GetX(tar),ObjMove_GetY(tar),16);
yield;
}
if(!Obj_IsDeleted(tar)){
SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/die.wav", 50);
shockwave(tar);
loop(15){
CreateItemA1(ITEM_POINT_S, ObjMove_GetX(tar)+rand(-35,35), ObjMove_GetY(tar)+rand(-35,35), 500);
}
}
kill = true;
Obj_Delete(tar);
}

task SableyeDie(tar){
while(ObjEnemy_GetInfo(tar,INFO_LIFE) > 0) {
ObjEnemy_SetIntersectionCircleToShot(tar,ObjMove_GetX(tar),ObjMove_GetY(tar),24);
ObjEnemy_SetIntersectionCircleToPlayer(tar,ObjMove_GetX(tar),ObjMove_GetY(tar),16);
yield;
}
if(!Obj_IsDeleted(tar)){
SE_Play(GetCurrentScriptDirectory ~ "EverythingElse/Functions/sfx/die.wav", 50);
shockwave(tar);
loop(25){
CreateItemA1(ITEM_POINT_S, ObjMove_GetX(tar)+rand(-55,55), ObjMove_GetY(tar)+rand(-55,55), 500);
}
CreateItemA1(ITEM_1UP , ObjMove_GetX(tar), ObjMove_GetY(tar), 500);
}
Obj_Delete(tar);
}

task MoveDespawn(tar, dir, tim){
	wait(tim);
	if(dir == 1){ObjMove_SetDestAtFrame(tar,-10,-10,75);}
	if(dir == 2){ObjMove_SetDestAtFrame(tar,GetStgFrameWidth+10,-10,75);}
	if(dir == 3){ObjMove_SetDestAtFrame(tar,GetStgFrameWidth/2,-10,75);}
	wait(76);
	Obj_Delete(tar);
}