#include "./lib_common.txt"
#include "./lib_effect.txt"
 
let debug_mode = false;

let screenloc = 0;

let spellstate = 0;
let stepstate_end = 0;
let count_dea = 0;

let grid_pos = [0,-1];
let grid_player_pos = 0;

@Initialize{

	CreateCommonDataArea("Boss");
	
	SetAreaCommonData("Boss","BossName","");
	SetAreaCommonData("Boss","SpellCutin",512);
	SetAreaCommonData("Boss","TimerActive",true);
	SetAreaCommonData("Boss","LifeActive",true);
	SetAreaCommonData("Boss","Score",10000000);
	SetAreaCommonData("Boss","AuraEnable",true);
	SetAreaCommonData("Boss","AuraScale",2);
	
	TInit();
	
	TMaxScore();
	TScore();
	TGraze();
	TPoints();

	TPlayerLife();
	TPlayerBomb();
	TPlayerPower();

	ascent(a in 0..3){TBossLife(a);}
	TBossLife2;
	TBossName();
	
	TCurrentFps();
	
	SetStgFrame(0,0,SCREEN_WIDTH,SCREEN_HEIGHT,20,80);
	SetPlayerClip(128-16,16-16,512+16,464+16);
	
	StartItemScript(GetCurrentScriptDirectory ~ "lib_itemscript.txt");
	StartShotScript(GetCurrentScriptDirectory ~ "lib_shotscript.txt");

	InstallFont(GetMainStgScriptDirectory ~ "font/MonoSpatial.ttf");
	InstallFont(GetMainStgScriptDirectory ~ "font/Russell Square Regular.ttf");
}

@MainLoop{

	SetPlayerClip(128,16,512,464);
	
	yield;
}

@Event{
	alternative(GetEventType())
	
	case(EV_START_BOSS_SPELL){
		spellstate = 0;
		TSpellResult;
		TSpellBackground;
	}
	
	case(EV_START_BOSS_STEP){TBossTimer();}
	
	case(EV_END_BOSS_STEP){
		if(spellstate == 0 && count_dea > 0){spellstate = 1;}
		stepstate_end++;}
	
	case(EV_GAIN_SPELL){spellstate = 2;}
}

task TInit(){
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj,GetMainStgScriptDirectory ~ "./img/system/Package_Fond.png");
	Obj_SetRenderPriority(obj, 0.9);

	let obj2 = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj2,GetMainStgScriptDirectory ~ "./img/system/Package_Fond.png");
	Obj_SetRenderPriority(obj2, 0.9);

	let t = 0;
	
	while(true){
		
		ObjSpriteList2D_ClearVertexCount(obj);
		ObjSpriteList2D_ClearVertexCount(obj2);
		
		ObjSpriteList2D_SetDestRect(obj, 0,16,128,464);
		ObjSpriteList2D_SetSourceRect(obj, 0,16,128,464);
		ObjSpriteList2D_AddVertex(obj);
		
		ObjSpriteList2D_SetDestRect(obj, 512,16,640,464);
		ObjSpriteList2D_SetSourceRect(obj, 512,16,640,464);
		ObjSpriteList2D_AddVertex(obj);
		
		ObjSpriteList2D_SetDestRect(obj, 0,0,640,16);
		ObjSpriteList2D_SetSourceRect(obj, 0,0,640,16);
		ObjSpriteList2D_AddVertex(obj);
		
		ObjSpriteList2D_SetDestRect(obj, 0,464,640,480);
		ObjSpriteList2D_SetSourceRect(obj, 0,464,640,480);
		ObjSpriteList2D_AddVertex(obj);

		ObjSpriteList2D_SetDestRect(obj2, 125,16,128,464);
		ObjSpriteList2D_SetSourceRect(obj2, 639,0,640,1);
		ObjSpriteList2D_AddVertex(obj2);

		ObjSpriteList2D_SetDestRect(obj2, 512,16,515,464);
		ObjSpriteList2D_SetSourceRect(obj2, 639,0,640,1);
		ObjSpriteList2D_AddVertex(obj2);

		ObjSpriteList2D_SetDestRect(obj2, 125,13,515,16);
		ObjSpriteList2D_SetSourceRect(obj2, 639,0,640,1);
		ObjSpriteList2D_AddVertex(obj2);

		ObjSpriteList2D_SetDestRect(obj2, 125,464,515,467);
		ObjSpriteList2D_SetSourceRect(obj2, 639,0,640,1);
		ObjSpriteList2D_AddVertex(obj2);

		ObjSpriteList2D_CloseVertex(obj);
		ObjSpriteList2D_CloseVertex(obj2);

		t = min(t+1,50);
		
		yield;
	}
}

task TMaxScore(){
	let objText = ObjText_Create();
	ObjText_SetText(objText, "High Score");
	ObjText_SetFontSize(objText, 14);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 100, 100, 100);
	Obj_SetRenderPriority(objText, 0.9);
	ObjText_SetFontType(objText, "MonoSpatial");
	ObjRender_SetY(objText, 240-15*5);
	ObjText_SetFontBorderWidth(objText, 2);
	ObjText_SetFontBorderColor(objText, 0,0,0);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetHorizontalAlignment(objText,ALIGNMENT_CENTER);
	ObjRender_SetX(objText, 0);
	ObjText_SetMaxWidth(objText,128);
	ObjRender_AlphaUp(objText);

	let pathDigit = GetMainStgScriptDirectory ~ "./img/system/number.png";
	
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriority(obj, 0.9);
	ObjRender_SetY(obj, 240-15*4);
	ObjRender_AlphaUp(obj);

	while(true)
	{
		let score = max(GetScore(),GetAreaCommonData("Data","Results",[0])[0]);
		let count = 12;
		score = min(score, 999999999999);
		let listNum = DigitToArray(score, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[count-1-iObj];
			ObjRender_SetX(obj, 64 - (iObj-(count-1)/2) * (128-10)/13);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16/4*3, 24/4*3);
			ObjSpriteList2D_AddVertex(obj);
		}
		
		yield;
	}
}

task TScore(){
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Score");
	ObjText_SetFontSize(objText, 14);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 100, 100, 100);
	Obj_SetRenderPriority(objText, 0.9);
	ObjText_SetFontType(objText, "MonoSpatial");
	ObjRender_SetY(objText, 240-15*2);
	ObjText_SetFontBorderWidth(objText, 2);
	ObjText_SetFontBorderColor(objText, 0,0,0);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetHorizontalAlignment(objText,ALIGNMENT_CENTER);
	ObjRender_SetX(objText, 0);
	ObjText_SetMaxWidth(objText,128);
	ObjRender_AlphaUp(objText);

	let pathDigit = GetMainStgScriptDirectory ~ "./img/system/number.png";
	
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriority(obj, 0.9);
	ObjRender_SetY(obj, 240-15);
	ObjRender_AlphaUp(obj);

	while(true)
	{
		let score = GetScore();
		let count = 12;
		score = min(score, 999999999999);
		let listNum = DigitToArray(score, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[count-1-iObj];
			ObjRender_SetX(obj, 64 - (iObj-(count-1)/2) * (128-10)/13);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16/4*3, 24/4*3);
			ObjSpriteList2D_AddVertex(obj);
		}
		
		yield;
	}
}

task TGraze(){
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Thrill");
	ObjText_SetFontType(objText, "MonoSpatial");
	ObjText_SetFontSize(objText, 14);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 100, 100, 100);
	Obj_SetRenderPriority(objText, 0.9);
	ObjRender_SetY(objText, 240+15);
	ObjText_SetFontBorderWidth(objText, 2);
	ObjText_SetFontBorderColor(objText, 0,0,0);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetHorizontalAlignment(objText,ALIGNMENT_CENTER);
	ObjRender_SetX(objText, 0);
	ObjText_SetMaxWidth(objText,128);
	ObjRender_AlphaUp(objText);

	let pathDigit = GetMainStgScriptDirectory ~ "./img/system/number.png";
	let count = 5;
	
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriority(obj, 0.9);
	ObjRender_SetY(obj, 240+15*2);
	ObjRender_AlphaUp(obj);
	
	while(true)
	{
		let score = GetGraze();
		score = min(score, 999999);
		count = GetNumSize(999999);
		let listNum = DigitToArray(score, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[count-1-iObj];
			ObjRender_SetX(obj, 64 - (iObj-(count-1)/2) * (128-10)/13);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16/4*3, 24/4*3);
			ObjSpriteList2D_AddVertex(obj);
		}
		
		yield;
	}
}

task TPoints(){
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Point");
	ObjText_SetFontType(objText, "MonoSpatial");
	ObjText_SetFontSize(objText, 14);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 100, 100, 100);
	Obj_SetRenderPriority(objText, 0.9);
	ObjRender_SetY(objText, 240+15*4);
	ObjText_SetFontBorderWidth(objText, 2);
	ObjText_SetFontBorderColor(objText, 0,0,0);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetHorizontalAlignment(objText,ALIGNMENT_CENTER);
	ObjRender_SetX(objText, 0);
	ObjText_SetMaxWidth(objText,128);
	ObjRender_AlphaUp(objText);

	let pathDigit = GetMainStgScriptDirectory ~ "./img/system/number.png";
	let count = 5;
	
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriority(obj, 0.9);
	ObjRender_SetY(obj, 240+15*5);
	ObjRender_AlphaUp(obj);

	while(true)
	{
		let score = GetPoint();
		score = min(score, 9999999);
		count = GetNumSize(9999999);
		let listNum = DigitToArray(score, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[count-1-iObj];
			ObjRender_SetX(obj, 64 - (iObj-(count-1)/2) * (128-10)/13);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16/4*3, 24/4*3);
			ObjSpriteList2D_AddVertex(obj);
		}
		
		yield;
	}
}

task TPlayerLife{
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Life");
	ObjText_SetFontType(objText, "MonoSpatial");
	ObjText_SetFontSize(objText, 14);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 100, 100, 100);
	Obj_SetRenderPriority(objText, 0.9);
	ObjRender_SetX(objText, 516);
	ObjRender_SetY(objText, 480-20-48);
	ObjText_SetFontBorderWidth(objText, 2);
	ObjText_SetFontBorderColor(objText, 0,0,0);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjRender_AlphaUp(objText);
	let pathDigit = GetMainStgScriptDirectory ~ "./img/system/Default_System.png";

	
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetColor(obj,255,120,120);
	Obj_SetRenderPriority(obj, 0.9);
	ObjRender_SetY(obj, 480-19-48);
	ObjRender_SetScaleXYZ(obj,0.6,0.6,0);
	ObjRender_AlphaUp(obj);
	
	while(true)
	{
		let point = GetPlayerLife();
		if(point < 15){
			ObjText_SetText(objText, "Life");
			point = min(point, 8);
			point = max(point, 0);
			
			ObjSpriteList2D_ClearVertexCount(obj);
			ascent(iObj in 0 .. truncate(point)){
				ObjRender_SetX(obj, 506-(iObj+1) * (128-30*2)/8);
				ObjSpriteList2D_SetSourceRect(obj,  48, 4, 64, 19);
				ObjSpriteList2D_SetDestRect(obj, 0, 0, 22, 22);
				ObjSpriteList2D_AddVertex(obj);
				}
			}
		else{
			ObjText_SetText(objText, "Infinite[r]Lifes");
		}
		
		if(GetPlayerX > 512-80){ObjRender_SetAlpha(obj,150);}
		else{ObjRender_SetAlpha(obj,255);}

		yield;
	}
}

task TPlayerBomb{
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Bomb");
	ObjText_SetFontType(objText, "MonoSpatial");
	ObjText_SetFontSize(objText, 14);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 100, 100, 100);
	Obj_SetRenderPriority(objText, 0.9);
	ObjRender_SetX(objText, 516);
	ObjRender_SetY(objText, 480-20-32);
	ObjText_SetFontBorderWidth(objText, 2);
	ObjText_SetFontBorderColor(objText, 0,0,0);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjRender_AlphaUp(objText);

	let pathDigit = GetMainStgScriptDirectory ~ "./img/system/Default_System.png";

	
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetColor(obj,120,255,120);
	Obj_SetRenderPriority(obj, 0.9);
	ObjRender_SetY(obj, 480-19-32);
	ObjRender_SetScaleXYZ(obj,0.6,0.6,0);
	ObjRender_AlphaUp(obj);
	
	while(true)
	{
		let point;
		if(GetAreaCommonData("Data","Bomb",0) == 0){point = GetPlayerSpell();}
		else{point = floor(GetPlayerPower()/100);}
		point = min(point, 8);
		point = max(point, 0);
			
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. truncate(point)){
			ObjRender_SetX(obj, 506-(iObj+1) * (128-30*2)/8);
			ObjSpriteList2D_SetSourceRect(obj,  48, 4, 64, 19);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 22, 22);
			ObjSpriteList2D_AddVertex(obj);
		}
		
		if(GetPlayerX > 512-80){ObjRender_SetAlpha(obj,150);}
		else{ObjRender_SetAlpha(obj,255);}

		yield;
	}
}

task TPlayerPower{
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Power");
	ObjText_SetFontType(objText, "MonoSpatial");
	ObjText_SetFontSize(objText, 14);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 100, 100, 100);
	Obj_SetRenderPriority(objText, 0.9);
	ObjRender_SetX(objText, 516);
	ObjRender_SetY(objText, 480-20-16);
	ObjText_SetFontBorderWidth(objText, 2);
	ObjText_SetFontBorderColor(objText, 0,0,0);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjRender_AlphaUp(objText);

	let pathDigit = GetMainStgScriptDirectory ~ "./img/system/number.png";
	let count = 3;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriority(obj, 0.9);
	ObjRender_SetY(obj, 480-19-16);
	ObjRender_AlphaUp(obj);

	while(true)
	{
		let score = GetPlayerPower();
		score = min(score, 400);
		let listNum = DigitToArray(score, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[count-1-iObj];
			ObjRender_SetX(obj, 509-(iObj+1+floor(iObj/2)/2) * (128-30*2)/8);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16/4*3, 24/4*3);
			ObjSpriteList2D_AddVertex(obj);
		}
		
		yield;
	}
}

task TPlayerFlash{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, GetMainStgScriptDirectory ~ "./img/system/flash_gauge_00.png");
	Obj_SetRenderPriority(obj, 0.9);
	ObjSprite2D_SetSourceRect(obj,0,0,15,260);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,512+64, 240, 0);
	ObjRender_AlphaUp(obj);
}

task TBossLife(numberaaa)
{
	let path = GetMainStgScriptDirectory ~ "./img/system/Default_System.png";
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D); //lifebar base
	let objb = ObjPrim_Create(OBJ_PRIMITIVE_2D); //actual life bar
	let obj3 = ObjPrim_Create(OBJ_SPRITE_LIST_2D); //life bar knobs
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetPrimitiveType(objb, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetTexture(obj, path);
	ObjPrim_SetTexture(objb, path);
	ObjPrim_SetTexture(obj3, path);
	Obj_SetRenderPriority(obj, 0.67);
	Obj_SetRenderPriority(objb, 0.68);
	Obj_SetRenderPriority(obj3, 0.69);
	let lastRemStep = -1;
	let lifeRateRender = 0;

	let vertexcount = 64;
	let innerrad = 60; //inner radius of the circle
	let outerrad = 68; //outer radius of the circle

	let objScene = ID_INVALID;
	let bossEnemy;


	ObjPrim_SetVertexCount(obj, vertexcount);
	ObjPrim_SetVertexCount(objb, vertexcount);
	loop{
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj3);
		if(objScene != ID_INVALID){
			let countRemStep = ObjEnemyBossScene_GetInfo(objScene, INFO_REMAIN_STEP_COUNT);
			if(lastRemStep != countRemStep){lifeRateRender = 0;}

			if(length(GetEnemyBossObjectID) > 0){
				bossEnemy = (GetCommonData("BossList",[ID_INVALID,ID_INVALID,ID_INVALID]))[numberaaa];

				if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_DURABLE_SPELL) || bossEnemy == ID_INVALID){
					ObjRender_SetAlpha(obj, 0);
					ObjRender_SetAlpha(objb, 0);
					ObjRender_SetAlpha(obj3, 0);
				}else{
					ObjRender_SetAlpha(obj, 256);
					ObjRender_SetAlpha(objb, 256);
					ObjRender_SetAlpha(obj3, 256);
				}
			}else{
				ObjRender_SetAlpha(obj, 0);
				ObjRender_SetAlpha(objb, 0);
				ObjRender_SetAlpha(obj3, 0);
			}

			let lifeM = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_MAX_LIFE); //life at the start of the step
			let life = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_LIFE); //Life remaining in the step
			ascent(i in 0..vertexcount/2){
		        let indexvert = i*2;
		        let angle = i*360/(vertexcount*0.5-1) - 90;
		        let angle2 = -90-(life/lifeM)*i*sin(lifeRateRender*90)*360/(vertexcount*0.5-1);
		        ObjPrim_SetVertexPosition(obj, indexvert+0, ObjMove_GetX(bossEnemy)+innerrad*cos(angle), ObjMove_GetY(bossEnemy)+innerrad*sin(angle), 0);
		        ObjPrim_SetVertexPosition(obj, indexvert+1, ObjMove_GetX(bossEnemy)+outerrad*cos(angle), ObjMove_GetY(bossEnemy)+outerrad*sin(angle), 0);

		        ObjPrim_SetVertexUVT(obj, indexvert+0, 8, 0);
		        ObjPrim_SetVertexUVT(obj, indexvert+1, 16, 32);


		        ObjPrim_SetVertexPosition(objb, indexvert+0, ObjMove_GetX(bossEnemy)+innerrad*cos(angle2), ObjMove_GetY(bossEnemy)+innerrad*sin(angle2), 0);
		        ObjPrim_SetVertexPosition(objb, indexvert+1, ObjMove_GetX(bossEnemy)+outerrad*cos(angle2), ObjMove_GetY(bossEnemy)+outerrad*sin(angle2), 0);

		        ObjPrim_SetVertexUVT(objb, indexvert+0, 0, 0);
		        ObjPrim_SetVertexUVT(objb, indexvert+1, 8, 32);
		    }

		    ObjSpriteList2D_SetSourceRect(obj3, 16, 0, 23,18);
			let listLifeDiv = [0] ~ ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_LIFE_RATE_LIST);
			ascent(iDiv in 0 .. length(listLifeDiv)){
				let rate = listLifeDiv[iDiv];

				let angle = 360*(listLifeDiv[iDiv]);
				let axe = ObjMove_GetX(bossEnemy)+(innerrad + ((outerrad - innerrad)/2))*cos(angle-90);
				let why = ObjMove_GetY(bossEnemy)+(innerrad + ((outerrad - innerrad)/2))*sin(angle-90);

				ObjSpriteList2D_SetDestCenter(obj3);
				ObjRender_SetPosition(obj3, axe, why, 0);
				ObjRender_SetScaleXYZ(obj3, 0.75, 0.75, 0);
				ObjRender_SetAngleZ(obj3, angle);
				ObjSpriteList2D_AddVertex(obj3);
			}

			lifeRateRender += 1/80;
			lifeRateRender = min(lifeRateRender, 1);
			lastRemStep = countRemStep;
		}else{
			ObjRender_SetAlpha(obj, 0);
			ObjRender_SetAlpha(objb, 0);
			ObjRender_SetAlpha(obj3, 0);
		}
		yield;
	}
}

task TBossLife2{
	let path = GetMainStgScriptDirectory ~ "./img/system/Default_System.png";
	let obj2 = ObjPrim_Create(OBJ_SPRITE_LIST_2D); //boss stars
	ObjPrim_SetTexture(obj2, path);
	Obj_SetRenderPriority(obj2, 0.9);
	let lastRemStep = -1;
	let lifeRateRender = 0;

	let objScene = ID_INVALID;
	let bossEnemy;
	loop{
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj2);
		if(objScene != ID_INVALID){
			let countRemStep = ObjEnemyBossScene_GetInfo(objScene, INFO_REMAIN_STEP_COUNT);
			if(lastRemStep != countRemStep){lifeRateRender = 0;}

			if(length(GetEnemyBossObjectID) > 0){
				bossEnemy = (GetCommonData("BossList",[ID_INVALID]))[0];

				if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_DURABLE_SPELL) || bossEnemy == ID_INVALID){
					ObjRender_SetAlpha(obj2, 0);
				}else{
					if(GetPlayerY < 80){ObjRender_SetAlpha(obj2,150);}
					else{ObjRender_SetAlpha(obj2,255);}
				}
			}else{
				ObjRender_SetAlpha(obj2, 0);
			}
			RenderBossSteps;
		}else{
			ObjRender_SetAlpha(obj2, 0);
		}
		yield;
	}

	function RenderBossSteps{
		let countRemStep = ObjEnemyBossScene_GetInfo(objScene, INFO_REMAIN_STEP_COUNT); //Remaining boss's extra lives (stars)
		if(lastRemStep != countRemStep){lifeRateRender = 0;}
		ascent(iStep in 0 .. countRemStep)
		{
			ObjSpriteList2D_SetSourceRect(obj2, 48, 3, 64, 19);
			ObjSpriteList2D_SetDestRect(obj2, 132 + iStep%6 * 10, 28+(floor(iStep/6))*10,
				132 + (iStep%6) * 10+16, 30+10*(floor(iStep/6))+16);
			ObjSpriteList2D_AddVertex(obj2);
		}
		ObjRender_SetColor(obj2,CR("green")[0],CR("green")[1],CR("green")[2]);
	}
}

task TBossTimer{
	
	let pathDigit = GetMainStgScriptDirectory ~ "./img/system/number.png";
	let objScene = GetEnemyBossSceneObjectID();
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	let count = 4;
	let stepstate = stepstate_end;
	let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER)*100;
	
	
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriority(obj, 0.75);
	ObjRender_SetY(obj, 16);
	ObjSpriteList2D_ClearVertexCount(obj);
	
	Timer;
	
	while(stepstate_end == stepstate){
		timer-=100/60;
		timer = max(min(timer, 99999),0);
		if(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) > 99){count = 5;}
		else{count = 4;}
		let listNum = DigitToArray(timer, count);
		
		ObjSpriteList2D_ClearVertexCount(obj);
		
		if(GetAreaCommonData("Boss","TimerActive",0)){
			if(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) > 99){
				ascent(iObj in -1 .. 2){
					let num = listNum[iObj+1];
					ObjRender_SetX(obj, 497 + (iObj-2) * 14);
					ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
					ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
					ObjSpriteList2D_AddVertex(obj);
				}
				ascent(iObj in 2 .. 4){
					let num = listNum[iObj+1];
					ObjRender_SetX(obj, 497 + (iObj-2) * 7);
					ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
					ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
					ObjSpriteList2D_SetDestRect(obj, 0, 24/2, 16/2, 24);
					ObjSpriteList2D_AddVertex(obj);
				}
			}
			else{
				ascent(iObj in 0 .. 2){
					let num = listNum[iObj];
					ObjRender_SetX(obj, 497 + (iObj-2) * 14);
					ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
					ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
					ObjSpriteList2D_AddVertex(obj);
				}
				ascent(iObj in 2 .. 4){
					let num = listNum[iObj];
					ObjRender_SetX(obj, 497 + (iObj-2) * 7);
					ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
					ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
					ObjSpriteList2D_SetDestRect(obj, 0, 24/2, 16/2, 24);
					ObjSpriteList2D_AddVertex(obj);
				}
			}
		}
		if(GetPlayerY < 80){ObjRender_SetAlpha(obj,150);}
		else{ObjRender_SetAlpha(obj,255);}
		yield;
	}
	
	Obj_Delete(obj);
	
	task Timer{
		let TIMER = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER);
		while(stepstate_end == stepstate){
			TIMER = max(TIMER-1,-1);
			if(TIMER <= 10 && TIMER != -1){_PlaySE(1);}
			loop(60){yield;}
		}
	}
}

task TBossName{
	let objText = ObjText_Create();
	ObjText_SetFontType(objText, "MonoSpatial");
	ObjText_SetFontSize(objText, 14);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 120, 255, 120);
	ObjText_SetFontColorBottom(objText, 60, 255, 60);
	Obj_SetRenderPriority(objText, 0.9);
	ObjText_SetFontBorderWidth(objText, 1);
	ObjText_SetFontBorderColor(objText, 0,0,0);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
		ObjRender_SetX(objText, 132);
		ObjRender_SetY(objText, 16);
	
	while(true){
		let objScene = GetEnemyBossSceneObjectID();
		let bossEnemy = GetEnemyBossObjectID();
		
		if(objScene != ID_INVALID && length(bossEnemy) > 0){ObjText_SetText(objText, GetAreaCommonData("Boss","BossName",0));}
		else{ObjText_SetText(objText, "");}

		if(GetPlayerY < 80){ObjRender_SetAlpha(objText,150);}
		else{ObjRender_SetAlpha(objText,255);}
		
		yield;
	}
}

task TSpellBackground{
	if(GetAreaCommonData("Boss","BossName","") == "Koishi Komeiji"){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,GetMainStgScriptDirectory ~ "img/spellbg/spellbg_koishi_00.png");
		ObjSprite2D_SetSourceRect(obj, 0,0,384*3,448*3);
		ObjSprite2D_SetDestCenter(obj);ObjRender_SetPosition(obj,320,240,0);
		Obj_SetRenderPriority(obj,0.21);

		let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj2,GetMainStgScriptDirectory ~ "img/spellbg/spellbg_koishi_01.png");
		ObjRender_SetAlpha(obj2,120);
		ObjSprite2D_SetSourceRect(obj2, 0,0,1024,1024);
		ObjSprite2D_SetDestCenter(obj2);ObjRender_SetPosition(obj2,320,240,0);
		Obj_SetRenderPriority(obj2,0.21);
			
		let t = 0;

		ascent(a in 1..181){
			ObjRender_SetColor(obj,255/180*a,255/180*a,255/180*a);
			ObjRender_SetColor(obj2,255/180*a,255/180*a,255/180*a);
			
			ObjRender_SetAngleZ(obj2,-t/4);
			ObjRender_SetPosition(obj2,320+120*cos(-90+t/2),240+120*sin(-90+t/2),0);

			t++;

			yield;
		}
			
		while(spellstate == 0){
			ObjRender_SetAngleZ(obj2,-t/4);
			ObjRender_SetPosition(obj2,320+120*cos(-90+t/2),240+120*sin(-90+t/2),0);

			t++;

			yield;
		}

		descent(a in 1..31){
			ObjRender_SetAlpha(obj,255/30*a);
			ObjRender_SetAlpha(obj2,120/30*a);

			ObjRender_SetAngleZ(obj2,-t/4);
			ObjRender_SetPosition(obj2,320+120*cos(-90+t/2),240+120*sin(-90+t/2),0);

			t++;

			yield;
		}
			
		Obj_Delete(obj);
		return;
	}
}

task TSpellResult{
	let score = GetAreaCommonData("Boss","Score",0);
	count_dea = 0;
	ascent(a in 0..3){TSpellRing(a);}
	while(spellstate == 0){
		
		if(GetPlayerState != STATE_NORMAL || IsPlayerSpellActive){count_dea++;}
		
		yield;
	}
	if(spellstate == 0){spellstate = 2;}
	if(spellstate == 1){
		_PlaySE(3);
		TNotification("Spell Failed!",96+32,CR("blue"));
		return;
	}
	else if(spellstate == 2){
		_PlaySE(2);
		TNotification("Spell Captured![r]+" ~ IntToString(score),96+32,CR("red"));
		AddScore(truncate(score));
		return;
	}
}

task TSpellRing(ass){
	let boss = (GetCommonData("BossList",[ID_INVALID]))[ass];
	if(boss == ID_INVALID){return;}
	let timer = ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID(), INFO_TIMER)*60;
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	let obj2 = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	let r1 = 160;
	let r2 = r1+16;
	let r3 = r1-12;
	let r4 = r1+4;

	let xa = ObjMove_GetX(boss);
	let ya = ObjMove_GetY(boss);

	let t = 0;
	let obj_st = 0;
	let obj_st2 = 0;

	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetTexture(obj,GetMainStgScriptDirectory ~ "img/system/eff_line.png");
	Obj_SetRenderPriorityI(obj, 40);
	ObjPrim_SetVertexCount(obj, 128);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);

	ObjPrim_SetPrimitiveType(obj2, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetTexture(obj2,GetMainStgScriptDirectory ~ "img/system/eff_line.png");
	Obj_SetRenderPriorityI(obj2, 40);
	ObjPrim_SetVertexCount(obj2, 128);
	ObjRender_SetBlendType(obj2,BLEND_ADD_ARGB);

	ObjRender_SetPosition(obj,ObjMove_GetX(boss),ObjMove_GetY(boss),0);
	ObjRender_SetPosition(obj2,ObjMove_GetX(boss),ObjMove_GetY(boss),0);

	ObjRender_SetAlpha(obj,150);
	ObjRender_SetAlpha(obj2,150);

	while(spellstate == 0){

		xa = ObjMove_GetX(boss);
		ya = ObjMove_GetY(boss);

		ascent(i in 0..64){
			let angle = i*360/63;

		    ObjPrim_SetVertexPosition(obj, i*2+0, r1*cos(angle)*obj_st, r1*sin(angle)*obj_st, 0);
		    ObjPrim_SetVertexPosition(obj, i*2+1, r2*cos(angle)*obj_st, r2*sin(angle)*obj_st, 0);

		    ObjPrim_SetVertexUVT(obj, i*2+0, 82, i*16);
		    ObjPrim_SetVertexUVT(obj, i*2+1, 95, i*16);

		    ObjPrim_SetVertexPosition(obj2, i*2+0, r4*cos(angle)*sin(min(obj_st2,90)), r4*sin(angle)*sin(min(obj_st2,90)), 0);
		    ObjPrim_SetVertexPosition(obj2, i*2+1, r3*cos(angle)*sin(max(obj_st2-90,0)), r3*sin(angle)*sin(max(obj_st2-90,0)), 0);

		    ObjPrim_SetVertexUVT(obj2, i*2+0, 49, i*16);
		    ObjPrim_SetVertexUVT(obj2, i*2+1, 64, i*16);
		}

		ObjRender_AddPatternA2(obj,ObjMove_GetX(boss),ObjMove_GetY(boss),50);
		ObjRender_AddPatternA2(obj2,ObjMove_GetX(boss),ObjMove_GetY(boss),50);	

		ObjRender_SetAngleZ(obj,ObjRender_GetAngleZ(obj)+3);
		ObjRender_SetAngleZ(obj2,ObjRender_GetAngleZ(obj2)-3);

		if(t < 45){obj_st = 2.5*sin(t*2);}
		else if (t < 90){obj_st = 1+1.5*sin(90-(t-45)*2)}
		obj_st2 = min(t*3,180);

		if(t >= 90){
			r1-= 144/timer;
		}

		r2 = r1 + 16;
		r3 = r1 - 12;
		r4 = r1 + 4;

		t++;

		yield;	
	}

	if(spellstate == 2){
		let smalliness = 0;
		loop(30){
			ascent(i in 0..64){
				let angle = i*360/63;

			    ObjPrim_SetVertexPosition(obj, i*2+0, r1*cos(angle)*obj_st, r1*sin(angle)*obj_st, 0);
			    ObjPrim_SetVertexPosition(obj, i*2+1, r2*cos(angle)*obj_st, r2*sin(angle)*obj_st, 0);

			    ObjPrim_SetVertexUVT(obj, i*2+0, 82, i*16);
			    ObjPrim_SetVertexUVT(obj, i*2+1, 95, i*16);

			    ObjPrim_SetVertexPosition(obj2, i*2+0, r4*cos(angle)*sin(min(obj_st2,90)), r4*sin(angle)*sin(min(obj_st2,90)), 0);
			    ObjPrim_SetVertexPosition(obj2, i*2+1, r3*cos(angle)*sin(max(obj_st2-90,0)), r3*sin(angle)*sin(max(obj_st2-90,0)), 0);

			    ObjPrim_SetVertexUVT(obj2, i*2+0, 49, i*16);
			    ObjPrim_SetVertexUVT(obj2, i*2+1, 64, i*16);
			}

			ObjRender_AddPatternA2(obj,GetPlayerX,GetPlayerY,10);
			ObjRender_AddPatternA2(obj2,GetPlayerX,GetPlayerY,10);	

			ObjRender_SetAngleZ(obj,ObjRender_GetAngleZ(obj)+3);
			ObjRender_SetAngleZ(obj2,ObjRender_GetAngleZ(obj2)-3);

			yield;
		}
		loop(20){
			smalliness += 1/20;
			ascent(i in 0..64){
				let angle = i*360/63;

			    ObjPrim_SetVertexPosition(obj, i*2+0, r1*cos(angle)*(smalliness*cos(angle*5)*2+2), r1*sin(angle)*(smalliness*cos(angle*5)*2+1), 0);
			    ObjPrim_SetVertexPosition(obj, i*2+1, r2*cos(angle)*(smalliness*cos(angle*5)*2+2), r2*sin(angle)*(smalliness*cos(angle*5)*2+1), 0);

			    ObjPrim_SetVertexUVT(obj, i*2+0, 82, i*16);
			    ObjPrim_SetVertexUVT(obj, i*2+1, 95, i*16);

			    ObjPrim_SetVertexPosition(obj2, i*2+0, r4*cos(angle)*(smalliness*cos(angle*5)*2+1), r4*sin(angle)*(smalliness*cos(angle*5)*2+1), 0);
			    ObjPrim_SetVertexPosition(obj2, i*2+1, r3*cos(angle)*(smalliness*cos(angle*5)*2+1), r3*sin(angle)*(smalliness*cos(angle*5)*2+1), 0);

			    ObjPrim_SetVertexUVT(obj2, i*2+0, 49, i*16);
			    ObjPrim_SetVertexUVT(obj2, i*2+1, 64, i*16);
			}

			ObjRender_AddPatternA2(obj,GetPlayerX,GetPlayerY,10);
			ObjRender_AddPatternA2(obj2,GetPlayerX,GetPlayerY,10);	

			ObjRender_SetAngleZ(obj,ObjRender_GetAngleZ(obj)+3);
			ObjRender_SetAngleZ(obj2,ObjRender_GetAngleZ(obj2)-3);

			ObjRender_SetAlpha(obj,255-smalliness*255);
			ObjRender_SetAlpha(obj2,255-smalliness*255);

			yield;
		}
	}
	else{
		xa = ObjRender_GetX(obj);
		ya = ObjRender_GetY(obj);

		t = 0;
		let rax = rand(-1,1);
		let ray = rand(-1,1);
		let rax2 = rand(-1,1);
		let ray2 = rand(-1,1);
		while(ya < 640+200){
			ascent(i in 0..64){
				let angle = i*360/63;

			    ObjPrim_SetVertexPosition(obj, i*2+0, r1*cos(angle), r1*sin(angle), 0);
			    ObjPrim_SetVertexPosition(obj, i*2+1, r2*cos(angle), r2*sin(angle), 0);

			    ObjPrim_SetVertexUVT(obj, i*2+0, 82, i*16);
			    ObjPrim_SetVertexUVT(obj, i*2+1, 95, i*16);

			    ObjPrim_SetVertexPosition(obj2, i*2+0, r4*cos(angle), r4*sin(angle), 0);
			    ObjPrim_SetVertexPosition(obj2, i*2+1, r3*cos(angle), r3*sin(angle), 0);

			    ObjPrim_SetVertexUVT(obj2, i*2+0, 49, i*16);
			    ObjPrim_SetVertexUVT(obj2, i*2+1, 64, i*16);
			}

			ObjRender_AddPatternA2(obj,xa,ya,10);
			ObjRender_AddPatternA2(obj2,xa,ya,10);	

			ya+=t/9;
			t+=3;

			yield;
		}
	}

	Obj_Delete(obj);
	Obj_Delete(obj2);

	task ObjRender_AddPatternA2(obj,x2,y2,t){
		let x = ObjRender_GetX(obj);
		let y = ObjRender_GetY(obj);
		let r = GetDis(x,y,x2,y2);
		let ang = GetAngA3(x,y,x2,y2);
		ascent(a in 1..t+1){
			ObjRender_SetPosition(obj,x+r*a/t*cos(ang),y+r*a/t*sin(ang),0);
			yield;
		}
	}
}

task TNotification(text,y,color){
	let objText = ObjText_Create();
	ObjText_SetText(objText, text);
	ObjText_SetFontType(objText, "MonoSpatial");
	ObjText_SetFontSize(objText, 20);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255,255,255);
	ObjText_SetFontColorBottom(objText, color[0], color[1], color[2]);
	ObjText_SetFontBorderWidth(objText, 3);
	ObjText_SetFontBorderColor(objText, 0,0,0);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	Obj_SetRenderPriority(objText, 0.95);
	ObjText_SetHorizontalAlignment(objText,ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(objText, 640);
	ObjRender_SetX(objText, 0);
	ObjRender_SetY(objText, y);
	
	ascent(a in 1..21){
		ObjRender_SetAngleX(objText,-90+a*90/20);
		yield;
	}
	
	loop(80){yield;}
	
	descent(a in 1..21){
		ObjRender_SetAngleX(objText,-90+a*90/20);
		yield;
	}
	
	Obj_Delete(objText);
}

task TCurrentFps()
{
	let objText = ObjText_Create();
	ObjText_SetFontSize(objText, 14);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 100, 100, 100);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 1);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(objText, GetScreenWidth() - 8);
	Obj_SetRenderPriority(objText, 1.0);
	ObjRender_SetX(objText, 0);
	ObjRender_SetY(objText, GetScreenHeight() - 20);

	loop
	{
		let fps = GetCurrentFps();
		let text = vtos("1.2f", fps) ~ "fps";
		ObjText_SetText(objText, text);
		yield;
	}
}

task TReplayFps()
{
	if(!IsReplay()){return;}

	let objText = ObjText_Create();
	ObjText_SetFontSize(objText, 12);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 128, 128, 255);
	ObjText_SetFontColorBottom(objText, 64, 64, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,255, 255, 255);
	ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriority(objText, 1.0);

	let px = GetStgFrameLeft() + GetStgFrameWidth() - 18;
	let py = GetStgFrameTop() + GetScreenHeight() - 14;
	ObjRender_SetX(objText, px);
	ObjRender_SetY(objText, py);

	loop
	{
		let fps = GetReplayFps();
		let text = vtos("02d", fps);
		ObjText_SetText(objText, text);
		yield;
	}
}

function DigitToArray(let digit,let count)
{
	let res = [];
	digit = truncate(digit);

	loop
	{
		let tnum = truncate(digit % 10);
		digit /= 10;
		res = [tnum] ~ res;
		if(truncate(digit) == 0){break;}
	}

	loop(max(0, count - length(res)))
	{
		res = [0] ~ res;
	}

	return res;
}
	
function GetNumSize(value){
	if(value<=1){return 1;}
	else{return truncate(log10(value))+1;}
}

task ObjRender_AddPatternA1(obj,x2,y2,t){
	let x = ObjRender_GetX(obj);
	let y = ObjRender_GetY(obj);
	let r = GetDis(x,y,x2,y2);
	let ang = GetAngA3(x,y,x2,y2);
	ascent(a in 1..t+1){
		ObjRender_SetPosition(obj,x+r*cos(-90+a/t*90)*cos(ang),y+r*cos(-90+a/t*90)*sin(ang),0);
		yield;
	}
}

task ObjRender_AlphaUp(obj){
	ascent(a in 1..121){
		ObjRender_SetAlpha(obj,255*a/120);
		yield;
	}
}