// Welcome to my main library, AKA the messiest thing in The Universe.

/////////////////////////////////////////////////////////////////////////////////////

// Include directories. (Obviously :v)

#include "./se_lib.dnh"
#include "./shot_lib.dnh"
#include "./lib_effects.dnh"
#include "./lib_pointFilter.dnh"
#include "./Boss_Anim.dnh"

/////////////////////////////////////////////////////////////////////////////////////

// Very Important Global Stuff (Without these, the whole thing falls apart.)

let objBoss; // Global boss object variable. (yuyuko)
let objScene = GetEnemyBossSceneObjectID(); // Pretty self-explanatory.
let start = false; // This is the boolean that starts the danmaku patterns.
function MyStageID { return GetCommonData("Main", 0); } // This is for linking the Singles with their Plural scripts.

/////////////////////////////////////////////////////////////////////////////////////

// Handy variables and functions.

let CSD = GetCurrentScriptDirectory;
let cx = GetStgFrameWidth() / 2;
let cy = GetStgFrameHeight() / 2;

function wait(n){loop(n){yield;}}

function stop{loop{yield;}}

function rand_int(min, max){
  return round(rand(min, max));
}

function GetCenterX(){
    return GetStgFrameWidth() / 2;
}
function GetCenterY(){
    return GetStgFrameHeight() / 2;
}

function GetBX{
	return ObjMove_GetX(objBoss);
}
function GetBY{
	return ObjMove_GetY(objBoss);
}

/////////////////////////////////////////////////////////////////////////////////////

// Magic Circle Animation.

task MagicCircle(enemy){
	let eff = CSD ~ "../img/circle3.png";
	let spin = 0;
	let inc = 0;	

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, eff);
	ObjSprite2D_SetSourceRect(obj, 0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 45);
	ObjRender_SetScaleXYZ(obj, 1.25, 1.25, 0);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	ObjRender_SetColor(obj, 255, 0, 150);

	while(!Obj_IsDeleted(enemy) && Obj_IsVisible(enemy) == true){ 
		ObjRender_SetPosition(obj, ObjMove_GetX(enemy), ObjMove_GetY(enemy), 0);
		ObjRender_SetAngleXYZ(obj, 0, 0, spin*3);
		ObjRender_SetScaleXYZ(obj, 1+cos(inc)*0.25, 1+cos(inc)*0.25, 1);
	//ObjRender_SetAlpha(obj, 200+sin(inc)*40);
		ObjRender_SetColor(obj, 90-120*sin(inc*2), 0, 180-120*sin(inc*1));
		spin++;
		inc += 2;
		yield;
	}
	Obj_Delete(obj);	
}

////////////////////////////////////////////////////////////////////////////////////

// Boss Marker.

task BossMarker{
	let img = CSD ~ "../img/marker.png";
	let inc = 0;

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img);
	ObjSprite2D_SetSourceRect(obj, 0,0,32,16);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriority(obj, 0.01);
	ObjRender_SetScaleXYZ(obj, 1.5, 1, 0);
	ObjRender_SetY(obj, 472);
	//ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	ObjRender_SetAlpha(obj, 200);
	while(!Obj_IsDeleted(objBoss)){
		ObjRender_SetX(obj, ObjMove_GetX(objBoss)+35);
		ObjRender_SetColorHSV(obj, 255, 100-75*sin(inc*5), 255);
		inc++;
		yield;
	}
	Obj_Delete(obj);
}

/////////////////////////////////////////////////////////////////////////////////////

// Boss Spell Cut-in.

task Cutin{
	let chara = CSD ~ "../img/cutins/cutin2.png";
	let fade = 230;	
	let moveX = 575;
	let moveY = -50;
	// let moveX = 460;
	let count = 0;
	
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, chara);
	ObjSprite2D_SetSourceRect(obj, 0,0,653,1000);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 70);
	ObjRender_SetAngleY(obj, 0);
	ObjRender_SetScaleXYZ(obj, 0.48, 0.48, 0);

	while(!Obj_IsDeleted(obj)){
		ObjRender_SetPosition(obj, moveX, -moveY, 0);
		fade=fade-0.5;
		ObjRender_SetAlpha(obj, fade);
		if(count < 35){
			moveX=moveX-5*2;
			moveY=moveY-2.5*2;
		}
		if(count > 35 && count < 100){
			moveX=moveX-1;
			moveY=moveY-0.5;
		}
		if(count > 100){
			moveX=moveX-20;
			moveY=moveY-10;
		}
		count++;
		yield;
	}	
}

/////////////////////////////////////////////////////////////////////////////////////

// Stage clear score bonus. Use this in your Stage script right before you terminate it.

task STGClearScore(score){
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Stage Cleared!");
	ObjText_SetFontType(objText, "Russell Square Regular");
	ObjText_SetFontSize(objText, 32);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 64, 64, 64);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,255, 255, 255);
	ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriority(objText, 0.6);
	ObjRender_SetX(objText, 105);
	ObjRender_SetY(objText, 98);

	let Score = "+" ~ IntToString(score);
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, Score);
	ObjText_SetFontType(objScore, "Russell Square Regular");
	ObjText_SetFontSize(objScore, 32);
	ObjText_SetFontBold(objScore, true);
	ObjText_SetFontColorTop(objScore, 160, 0, 80);
	ObjText_SetFontColorBottom(objScore, 160, 0, 0);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore,255, 255, 255);
	ObjText_SetFontBorderWidth(objScore, 1);
	Obj_SetRenderPriority(objScore, 0.6);
	ObjRender_SetX(objScore, 120);
	ObjRender_SetY(objScore, 140);

	AddScore(score);

	loop(120)
	{
		yield;
	}
	Obj_Delete(objText);
	Obj_Delete(objScore);
}

/////////////////////////////////////////////////////////////////////////////////////

// Boss name. Put it in the @Initialize of your Singles.

task Name(name){
	let objText = ObjText_Create();
	ObjText_SetText(objText, name);
    ObjText_SetFontSize(objText, 12);
    ObjText_SetFontType(objText, "Russell Square Regular");
    ObjText_SetFontBold(objText, false);
    ObjText_SetFontColorTop(objText, 0, 220, 80);
    ObjText_SetFontColorBottom(objText, 0,170,200);
    ObjText_SetFontBorderType(objText, BORDER_FULL);
    ObjText_SetHorizontalAlignment(objText, ALIGNMENT_LEFT);
	ObjText_SetMaxWidth(objText, 123);
    ObjText_SetFontBorderColor(objText, 50, 50, 50);
    ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriorityI(objText, 82);
	ObjRender_SetPosition(objText, 32, 17, 0);
}

/////////////////////////////////////////////////////////////////////////////////////

// Sets the power of an enemy shot.

task SetDamage(obj, amount){ // WELCOME TO INFINITE LOOP HELL.
	while(!Obj_IsDeleted(obj)){
		let playerlife = GetPlayerLife;
		if(ObjCol_IsIntersected(GetPlayerObjectID) && ObjCol_IsIntersected(obj)){
			while(!Obj_IsDeleted(obj)){
				if(GetPlayerState == STATE_DOWN){
					let damage = amount;
					Number(damage);
					DeleteShotInCircle(TYPE_SHOT, TYPE_IMMEDIATE, GetPlayerX, GetPlayerY, 50);
					if(playerlife <= damage){
						SetPlayerLife(0);
						Obj_Delete(GetPlayerObjectID);
						KillPlayer;
					}
					else{
						SetPlayerLife(playerlife-damage);
					}
					break;
				}
				yield;
			}
			break;
		}
		yield;
	}
}
task Number(damage){
	let objText = ObjText_Create();
	ObjText_SetText(objText, IntToString(damage));
    ObjText_SetFontSize(objText, 14);
    ObjText_SetFontType(objText, "Russell Square Regular");
    ObjText_SetFontBold(objText, false);
    ObjText_SetFontColorTop(objText, 255, 255, 255);
    ObjText_SetFontColorBottom(objText, 255, 50, 50);
    ObjText_SetFontBorderType(objText, BORDER_FULL);
    ObjText_SetFontBorderColor(objText, 50, 50, 50);
    ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriorityI(objText, 82);
	ObjRender_SetPosition(objText, GetPlayerX, GetPlayerY, 0);
	NumMove(objText);
	wait(90);
	Obj_Delete(objText);
}
task NumMove(objText){
	let inc = 0;
	while(!Obj_IsDeleted(objText)){
		inc++;
		ObjRender_SetPosition(objText, GetPlayerX-inc*0.1, GetPlayerY-cos(inc)*10, 0);
		yield;
	}
}
task KillPlayer{
	wait(120);
	CloseStgScene();
}

/////////////////////////////////////////////////////////////////////////////////////

// Spell Card Activation Task.

task Spell(name, id){
	if(!LoadCommonDataAreaA2("SpellHistoryCaps", CSD ~ "../scriptdata/SpellHistoryCaps.dat")){
		CreateCommonDataArea("SpellHistoryCaps");
		SetAreaCommonData("SpellHistoryCaps", id, 0);
	}
	if(!LoadCommonDataAreaA2("SpellHistoryTries", CSD ~ "../scriptdata/SpellHistoryTries.dat")){
		CreateCommonDataArea("SpellHistoryTries");
		SetAreaCommonData("SpellHistoryTries", id, 0);
	}

	if(!IsReplay){
		SetAreaCommonData("SpellHistoryTries", id, GetAreaCommonData("SpellHistoryTries", id, 0)+1);
		SaveCommonDataAreaA2("SpellHistoryTries", CSD ~ "../scriptdata/SpellHistoryTries.dat");
	}

	let splText = CSD ~ "../img/effect3.png"; // Directory for spell text graphic.

	let objText = ObjText_Create();
	ObjText_SetText(objText, name);
    ObjText_SetFontSize(objText, 12);
    ObjText_SetFontType(objText, "Trajan Pro Regular");
    ObjText_SetFontBold(objText, false);
    ObjText_SetFontColorTop(objText, 255, 255, 255);
    ObjText_SetFontColorBottom(objText, 175, 175, 175);
    ObjText_SetFontBorderType(objText, BORDER_FULL);
    ObjText_SetFontBorderColor(objText, 50, 50, 50);
    ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriorityI(objText, 62);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(objText, 430);
	//ObjRender_SetX(objText, 300);
	
	let textBonus = ObjText_Create();
	ObjText_SetText(textBonus, "Bonus: " ~ "A Gold Star");
    ObjText_SetFontSize(textBonus, 10);
    ObjText_SetFontType(textBonus, "Trajan Pro Regular");
    ObjText_SetFontBold(textBonus, false);
    ObjText_SetFontColorTop(textBonus, 255, 255, 255);
    ObjText_SetFontColorBottom(textBonus, 175, 175, 175);
    ObjText_SetFontBorderType(textBonus, BORDER_FULL);
    ObjText_SetFontBorderColor(textBonus, 50, 50, 50);
    ObjText_SetFontBorderWidth(textBonus, 1);
	Obj_SetRenderPriorityI(textBonus, 62);
	// ObjRender_SetX(textBonus, 520);

	let caps = IntToString(GetAreaCommonData("SpellHistoryCaps", id, 0));
	let tries = IntToString(GetAreaCommonData("SpellHistoryTries", id, 0));

	let textHistory1 = ObjText_Create();
	ObjText_SetText(textHistory1, "History: " ~ rtos("#00", caps) ~ "/" ~ rtos("#00", tries));
    ObjText_SetFontSize(textHistory1, 10);
    ObjText_SetFontType(textHistory1, "Trajan Pro Regular");
    ObjText_SetFontBold(textHistory1, false);
    ObjText_SetFontColorTop(textHistory1, 255, 255, 255);
    ObjText_SetFontColorBottom(textHistory1, 175, 175, 175);
    ObjText_SetFontBorderType(textHistory1, BORDER_FULL);
    ObjText_SetFontBorderColor(textHistory1, 50, 50, 50);
    ObjText_SetFontBorderWidth(textHistory1, 1);
	Obj_SetRenderPriorityI(textHistory1, 62);
	// ObjRender_SetX(textHistory1, 480);
	
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, splText);
	ObjSprite2D_SetSourceRect(obj, 0,0,256,45);
	ObjSprite2D_SetDestRect(obj, 0,0,256,45);
	Obj_SetRenderPriorityI(obj, 50);
	// ObjRender_SetX(obj, 410);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	
	let spellname = [objText, textBonus, obj, textHistory1];
	
	TextMove(objText,spellname);
	
	let bonusget = true;
	let bonus = ObjEnemyBossScene_GetInfo(objScene,INFO_SPELL_SCORE);
	while(bonusget){
		bonusget = (ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT)
		+ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT)) == 0;
		bonus = ObjEnemyBossScene_GetInfo(objScene,INFO_SPELL_SCORE);
		ObjText_SetText(textBonus, "Bonus: " ~ IntToString(bonus));
		yield;
	}
	ObjText_SetText(textBonus, "Bonus: " ~ "Failed");

	task TextMove(objText,spellname){
		let ypos1 = 332+15;
		let ypos2 = 343+15;
		let ypos3 = 305+15;
		
		let acc = 0.01;
		while(!Obj_IsDeleted(objText)){
			ObjRender_SetPosition(spellname[0], 0, ypos1, 0);
			ObjRender_SetPosition(spellname[1], 270, ypos2, 0);
			ObjRender_SetPosition(spellname[2], 180, ypos3, 0);
			ObjRender_SetPosition(spellname[3], 360, ypos2, 0);
			
			ypos1-=acc;
			ypos2-=acc;
			ypos3-=acc;
			
			if(ypos1 >= 120 && ypos2 >= 130 && ypos3 >= 95){
				acc+=0.05;
			}
			else{
				acc-=0.12;
			}
			if(acc <= 0){
				acc = 0;
			}
			
			yield;
		}
	}
}

/////////////////////////////////////////////////////////////////////////////////////

// Screen shake!

task ScreenShake(frames, inten){
	loop(frames) {
		Set2DCameraFocusX(GetStgFrameWidth/2+rand(-inten,inten));
		Set2DCameraFocusY(GetStgFrameHeight/2+rand(-inten,inten));
		yield;
	}
	Reset2DCamera;
}

/////////////////////////////////////////////////////////////////////////////////////

// RNG function.

function RNGpercent(chance){
		let random = rand_int(0, 100);
		if(random <= chance){return true;}
		else{return false;}
}

/////////////////////////////////////////////////////////////////////////////////////

// Charge Effect. (By Neito.)

task nTestCharge(x,y,flame,r1,g1,b1) // This is the Task youll be using for the effect!!
{
	ascent(i in 0..5){
		lotemasLite(x+rand(-5,5),y+rand(-15,15),1*i,flame,r1,g1,b1,(1*i)/1); //Creates the Color Charge effect
		lotemas(x+rand(-5,5),y+rand(-15,15),1*i,flame,250,250,250,(1*i)); //Creates the Black charge effect
	}
	loop(flame-30){ //No dot effects will be spawned if the charge times is less then or equal to 30 frames.
		loop(rand(1,3)){
			oltahmas(x+rand(-5,5),y+rand(-5,5),30,r1,g1,b1); //Creates Dots of black and the designated colors randomly that fly in at the given position.
		}
		yield;
	}
}

task lotemas(x,y,Edscale,flame,r1,g1,b1,fractal)
{
	let imgEff = GetCurrentScriptDirectory~"../img/eltmas1.png";
	let scale = Edscale;
	let alpha = 0;
	let angle = rand(0,360);
	let angleR = rand(-3,3); 

	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_SUBTRACT);
	ObjPrim_SetTexture(obj, imgEff);
	Obj_SetRenderPriority(obj, 0.30);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 256, 256);
	ObjSprite2D_SetDestRect(obj, -128, -128, 128, 128);
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetPosition(obj, x, y, 0);
	ascent(i in 0 .. flame){
		//if(Obj_IsDeleted(bossObj)==true){Obj_Delete(obj);}
		ObjRender_SetColor(obj, alpha*r1, alpha*g1, alpha*b1);
		ObjRender_SetScaleXYZ(obj,scale,scale,1.0);
		ObjRender_SetAngleXYZ(obj,0,0,angle);
		angle+=angleR;
		scale-= Edscale/flame;
		alpha+= (1/fractal)/(flame/Edscale);
		yield;
	}
	Obj_Delete(obj);
}
task lotemasLite(x,y,Edscale,flame,r1,g1,b1,fractal)
{
	let imgEff = GetCurrentScriptDirectory~"../img/eltmas1.png";
	let scale = Edscale;
	let alpha = 0;
	let angle = rand(0,360);
	let angleR = rand(-3,3); 

	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	ObjPrim_SetTexture(obj, imgEff);
	Obj_SetRenderPriority(obj, 0.30);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 256, 256);
	ObjSprite2D_SetDestRect(obj, -128, -128, 128, 128);
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetPosition(obj, x, y, 0);
	ascent(i in 0 .. flame){
		//if(Obj_IsDeleted(bossObj)==true){Obj_Delete(obj);}
		ObjRender_SetColor(obj, alpha*r1, alpha*g1, alpha*b1);
		ObjRender_SetScaleXYZ(obj,scale,scale,1.0);
		ObjRender_SetAngleXYZ(obj,0,0,angle);
		angle+=angleR;
		scale-= Edscale/flame;
		alpha+= (1/fractal)/(flame/Edscale);
		yield;
	}
	Obj_Delete(obj);
}
task oltahmas(x,y,flame,r1,g1,b1)
{
	let imgEff = GetCurrentScriptDirectory~"../img/dot.png";
	let scaleStandard = rand(0.25,1);
	let scale = scaleStandard;
	let alpha = 0;
	let alphaDun = rand(0.1,1);

	let accer = rand(0.3, 0.4);
	let angle = rand(0, 360);
	let r = accer*(flame^2)/2;
	x += r*cos(angle);
	y += r*sin(angle);

	let ToF=0;
	ToF=rand(-1,1);

	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	if(ToF>0){
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	}else if(ToF<0){
		ObjRender_SetBlendType(obj, BLEND_SUBTRACT);
	}
	ObjPrim_SetTexture(obj, imgEff);
	Obj_SetRenderPriority(obj, 0.30);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 64, 64);
	ObjSprite2D_SetDestRect(obj, -32, -32, 32, 32);
	ObjRender_SetPosition(obj, x, y, 0);
	ascent(i in 0 .. flame){
		ObjRender_SetPosition(obj, x, y, 0);
		ObjRender_SetAlpha(obj, 255*alpha);
		//if(Obj_IsDeleted(bossObj)==true){Obj_Delete(obj);}
		ObjRender_SetColor(obj, alpha*r1, alpha*g1, alpha*b1);
		ObjRender_SetScaleXYZ(obj,scale,scale,1.0);
		x -= accer*i*cos(angle);
		y -= accer*i*sin(angle);
		if(alpha<alphaDun){
			alpha+= alphaDun/5;
		}
		scale-=scaleStandard/flame;
		yield;
	}
	Obj_Delete(obj);
}

/////////////////////////////////////////////////////////////////////////////////////

// Terminating tasks. Put them in the @Initialize of your Singles.

task TFinalizeNonspell{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
    EShockwave(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 0.09);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	Obj_Delete(objBoss);
	NotifyEvent(MyStageID,EV_USER_STAGE+1,NULL);
    SetAutoDeleteObject(true);
    SetCommonData("isBirb", 0);
	CloseScript(GetOwnScriptID());
    return;
}

task TFinalizeSpell(id){
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
    EShockwave(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 0.09);
	NotifyEvent(MyStageID,EV_USER_STAGE+1,NULL);
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT)+ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0){
		let score = ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE);
		NotifyEvent(MyStageID,EV_USER_STAGE+2,NULL);
		NotifyEvent(MyStageID,EV_USER_STAGE+4,score);
		SetAreaCommonData("SpellHistoryCaps", id, GetAreaCommonData("SpellHistoryCaps", id, 0)+1);
		SaveCommonDataAreaA2("SpellHistoryCaps", CSD ~ "../scriptdata/SpellHistoryCaps.dat");
	}
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	Obj_Delete(objBoss);
    SetAutoDeleteObject(true);
    SetCommonData("isBirb", 0);
    CloseScript(GetOwnScriptID());
    return;
}

task TFinalizeFinal(id){
	let radius = 0;
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
	SetAutoDeleteObject(true);
	SetCommonData("isBirb", 0);
	start = false;
	//Attack = false;
	PlaySE(ExpSE1);
	ObjMove_SetDestAtSpeed(objBoss, ObjMove_GetX(objBoss)+rand(-40,40), ObjMove_GetY(objBoss)+rand(115,130), 0.2);
	EShockwave(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 0.05);
	NotifyEvent(MyStageID,EV_USER_STAGE+1,NULL);
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT)+ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0){
	    AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE));
	    if(!IsReplay){ 
	    	SetAreaCommonData("SpellHistoryCaps", id, GetAreaCommonData("SpellHistoryCaps", id, 0)+1);
	    	SaveCommonDataAreaA2("SpellHistoryCaps", CSD ~ "../scriptdata/SpellHistoryCaps.dat");
	    }
		NotifyEvent(MyStageID,EV_USER_STAGE+2,NULL);
	}
	loop(140){
		DeleteShotInCircle(TYPE_ALL, TYPE_FADE, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), radius);
		ObjEnemy_SetIntersectionCircleToPlayer(objBoss, 0, 0, 0);
		radius += 5;
		yield;
	}
	EExplosion(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 200);
	PlaySE(ExpSE1);
	Obj_SetVisible(objBoss, false);
	loop(120){
		DeleteShotAll(TYPE_ALL, TYPE_ITEM);
		ObjEnemy_SetIntersectionCircleToPlayer(objBoss, 0, 0, 0);
		yield;
	}
	Obj_Delete(objBoss);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	CloseScript(GetOwnScriptID());
    return;
}

/////////////////////////////////////////////////////////////////////////////////////

// Debug. (Nothing yet. :P)

/////////////////////////////////////////////////////////////////////////////////////