#include "./CommonDataStuff.dnh"

let dirCurrent = GetCurrentScriptDirectory();

@Initialize
{
	InitializeCommonData;
	
	
	InitFrame();
	TScore();
	TGraze();
	TPlayerLife();
	TPlayerSpell();
	//TPlayerPower();
	//TPointValue();
	//TIchor();
	//TSpooks();
	TBossLife();
	TBossTimer();
	TCurrentFps();
	TReplayFps();

	CreateScreenCapture();
	
	StartShotScript(GetCurrentScriptDirectory ~ "../LIB/ShotScript.dnh");
	StartItemScript(GetCurrentScriptDirectory ~ "../LIB/ItemScript.dnh");
	StartScript(LoadScript(GetCurrentScriptDirectory ~ "./../LIB/SoundLib.dnh")); //Handles uh...sounds?
	
	SetPauseScriptPath(GetCurrentScriptDirectory ~ "Default_Pause.dnh");
	SetEndSceneScriptPath(GetCurrentScriptDirectory ~ "Default_EndScene.dnh");
	SetReplaySaveSceneScriptPath(GetCurrentScriptDirectory ~ "Default_ReplaySaveScene.dnh");

	CreateCommonDataArea("System");
	InstallFont(GetCurrentScriptDirectory ~ "font/Russell Square Regular.ttf"); //Russell Square Regular
	InstallFont(GetCurrentScriptDirectory ~ "font/monofonto.ttf"); //Russell Square Regular
}

@MainLoop
{
	if(GetPlayerSpell > 8){SetPlayerSpell(8);}
	if(GetPlayerSpell > 8){SetPlayerSpell(8);}
	if(GetPlayerPower > 400){
		SetPlayerPower(400);
	}
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_START_BOSS_SPELL)
	{
		let path = dirCurrent ~ "Default_System_MagicCircle.dnh";
		let id = LoadScript(path);
		StartScript(id);
	}
	case(EV_TIMEOUT)
	{
		let objScene = GetEnemyBossSceneObjectID();
		if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL)){
			if(!ObjEnemyBossScene_GetInfo(objScene, INFO_IS_DURABLE_SPELL) && ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) == 0 && ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0){
				NotifyEvent(GetCommonData("SoundScript", GetOwnScriptID), EV_USER, [36, 100]);
				TFailSpellTimeout;
			}
		}
		
	}
	case(EV_END_BOSS_STEP){
		let objScene = GetEnemyBossSceneObjectID();
		if(objScene != ID_INVALID && ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL)){
			if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) > 0 || ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) > 0){
				NotifyEvent(GetCommonData("SoundScript", GetOwnScriptID), EV_USER, [36, 100]);
				TFailSpellRegular;
			}
		}
	}
	case(EV_GAIN_SPELL){
		let objScene = GetEnemyBossSceneObjectID();
		let score = ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE);
		NotifyEvent(GetCommonData("SoundScript", GetOwnScriptID), EV_USER, [12, 100]);
			
		if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_LAST_SPELL)){TGainSpell(score, 1);}
		else if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_DURABLE_SPELL)){TGainSpell(score, 2);}
		else{TGainSpell(score, 0);}
	}
}

@Loading{
}

//----------------------------------------------------
//??????ó
//----------------------------------------------------
task InitFrame()
{
	//Frame
	let path = GetCurrentScriptDirectory() ~ "img/HUD_frame.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, path);
	Obj_SetRenderPriority(obj, 0);
	ObjSprite2D_SetSourceRect(obj, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
	ObjSprite2D_SetDestRect(obj, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

	SetStgFrame(128, 16, 512, 464, 20, 80);
}

//----------------------------------------------------
//OOA?ó
//----------------------------------------------------
task TScore()
{
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	let img = GetCurrentScriptDirectory()~"img/HUD_new.png";
	ObjPrim_SetTexture(obj2, img);
	ObjSprite2D_SetSourceRect(obj2, 0, 32, 128, 64);
	ObjSprite2D_SetDestCenter(obj2);
	Obj_SetRenderPriorityI(obj2, 100);
	ObjRender_SetPosition(obj2, GetScreenWidth - 64, 24, 0);
	ObjRender_SetScaleXYZ(obj2, 0.6, 0.6, 0);
	
	let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj3, img);
	ObjSprite2D_SetSourceRect(obj3, 0, 224, 128, 256);
	ObjSprite2D_SetDestCenter(obj3);
	Obj_SetRenderPriorityI(obj3, 100);
	ObjRender_SetPosition(obj3, GetScreenWidth - 64, 34, 0);
	ObjRender_SetScaleXYZ(obj3, 0.9, 1, 0);

	//??
	let pathDigit = GetCurrentScriptDirectory() ~ "img/number.png";
	let count = 12;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	Obj_SetRenderPriorityI(obj, 100);
	ObjRender_SetY(obj, 36);

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

		let extra = 0;
		ObjSpriteList2D_ClearVertexCount(obj);

		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			if(iObj % 3 == 0){
				extra += 2;
			}
			ObjRender_SetX(obj, GetScreenWidth - 131 + iObj * 10 + extra);
			ObjRender_SetScaleXYZ(obj, 0.85, 0.85, 0);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 0, (num + 1) * 16, 20);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
			ObjSpriteList2D_AddVertex(obj);
		}

		ObjRender_SetX(obj, GetScreenWidth - 131 + 3*10 + 2);
		ObjRender_SetScaleXYZ(obj, 0.85, 0.85, 0);
		ObjSpriteList2D_SetSourceRect(obj, 224, 0, 240, 20);
		ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
		ObjSpriteList2D_AddVertex(obj);

		ObjRender_SetX(obj, GetScreenWidth - 131 + 6*10 + 4);
		ObjRender_SetScaleXYZ(obj, 0.85, 0.85, 0);
		ObjSpriteList2D_SetSourceRect(obj, 224, 0, 240, 20);
		ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
		ObjSpriteList2D_AddVertex(obj);

		ObjRender_SetX(obj, GetScreenWidth - 131 + 9*10 + 6);
		ObjRender_SetScaleXYZ(obj, 0.85, 0.85, 0);
		ObjSpriteList2D_SetSourceRect(obj, 224, 0, 240, 20);
		ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
		ObjSpriteList2D_AddVertex(obj);

		yield;
	}
}

//----------------------------------------------------
//Graze?ó
//----------------------------------------------------
task TGraze()
{
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	let img = GetCurrentScriptDirectory()~"img/HUD_new.png";
	ObjPrim_SetTexture(obj2, img);
	ObjSprite2D_SetSourceRect(obj2, 0, 128, 128, 160);
	ObjSprite2D_SetDestCenter(obj2);
	Obj_SetRenderPriority(obj2, 1);
	ObjRender_SetPosition(obj2, GetScreenWidth - 64, 72, 0);
	ObjRender_SetScaleXYZ(obj2, 0.6, 0.6, 0);

	let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj3, img);
	ObjSprite2D_SetSourceRect(obj3, 0, 224, 128, 256);
	ObjSprite2D_SetDestCenter(obj3);
	Obj_SetRenderPriorityI(obj3, 100);
	ObjRender_SetPosition(obj3, GetScreenWidth - 64, 82, 0);
	ObjRender_SetColor(obj3, 128, 128, 128);
	ObjRender_SetScaleXYZ(obj3, 0.9, 1, 0);
		
	//??
	let pathDigit = GetCurrentScriptDirectory() ~ "img/number.png";
	let count = 6;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	Obj_SetRenderPriority(obj, 100);
	ObjRender_SetY(obj, 84);

	while(true)
	{
		let graze = GetGraze();
		graze = min(graze, 99999);
		let listNum = DigitToArray(graze, count);
		count = 6;

		let extra = 0;
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			if(iObj % 3 == 0){
				extra += 2;
			}

			ObjRender_SetX(obj, GetScreenWidth - 131 + iObj * 10 + 32 + extra);
			ObjRender_SetScaleXYZ(obj, 0.85, 0.85, 0);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 0, (num + 1) * 16, 20);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
			ObjSpriteList2D_AddVertex(obj);
		}

		ObjRender_SetX(obj, GetScreenWidth - 131 + 6*10 + 4);
		ObjRender_SetScaleXYZ(obj, 0.85, 0.85, 0);
		ObjSpriteList2D_SetSourceRect(obj, 224, 0, 240, 20);
		ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
		ObjSpriteList2D_AddVertex(obj);
		yield;
	}
}

//----------------------------------------------------
//ð??ó
//----------------------------------------------------
task TPlayerLife
{
	Text;
	
	ascent(i in 0..8){
		ActualSpell(GetScreenWidth - 88 + i*16, i);
	}
	
	task ActualSpell(x, num){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let img = GetCurrentScriptDirectory ~ "img/HUD_new.png";
		let y = 140;

		if(num >= 4){
			x -= 64;
			y += 16;
		}
		
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj, 240, 0, 256, 16);
		ObjSprite2D_SetDestCenter(obj);
		Obj_SetRenderPriority(obj, 1);
		ObjRender_SetPosition(obj, x, y, 0);
		
		loop{
			let spell = GetPlayerLife();
			let fragnum = GetAreaCommonData("System", "LifeFragmentNum", 0);
			let fragva = GetAreaCommonData("System", "LifeFragmentValue", [1/5, 5]);
			
			let FragmentRate = GetPlayerLife - trunc(GetPlayerLife);
			let DisplayRate = trunc(5 * FragmentRate);
			
			if(spell >= num){
				ObjRender_SetAlpha(obj, 255);
				if(spell < num+1){
					ObjSprite2D_SetSourceRect(obj, 160 + 16*DisplayRate, 0, 176 + 16*DisplayRate, 16);
					ObjRender_SetColor(obj, 155, 155, 155);
				}else{
					ObjSprite2D_SetSourceRect(obj, 240, 0, 256, 16);
					ObjRender_SetColor(obj, 255, 255, 255);
				}
			}
			else{ObjRender_SetColor(obj, 155, 155, 155); ObjSprite2D_SetSourceRect(obj, 160, 0, 176, 16);}
			yield;
		}
	}
	
	task Text{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let img = GetCurrentScriptDirectory ~ "img/HUD_new.png";
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj, 0, 96, 128, 128);
		ObjSprite2D_SetDestCenter(obj);
		Obj_SetRenderPriority(obj, 1);
		ObjRender_SetPosition(obj, GetScreenWidth - 64, 120, 0);
		ObjRender_SetScaleXYZ(obj, 0.6, 0.6, 0);
		
		let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj3, img);
		ObjSprite2D_SetSourceRect(obj3, 0, 224, 128, 256);
		ObjSprite2D_SetDestCenter(obj3);
		Obj_SetRenderPriority(obj3, 1);
		ObjRender_SetPosition(obj3, GetScreenWidth - 64, 130, 0);
		ObjRender_SetColor(obj3, 192, 0, 256);
		ObjRender_SetScaleXYZ(obj3, 0.9, 1, 0);
	}
}
//----------------------------------------------------
//ðOuE?ó
//----------------------------------------------------
task TPlayerSpell
{
	Text;
	
	ascent(i in 0..8){
		ActualSpell(GetScreenWidth - 88 + i*16, i);
	}
	
	task ActualSpell(x, num){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let img = GetCurrentScriptDirectory ~ "img/HUD_new.png";
		let y = 200;

		if(num >= 4){
			x -= 64;
			y += 16;
		}
		
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj, 240, 16, 256, 32);
		ObjSprite2D_SetDestCenter(obj);
		Obj_SetRenderPriority(obj, 1);
		ObjRender_SetPosition(obj, x, y, 0);
		
		loop{
			let spell = GetPlayerSpell();
			let fragnum = GetAreaCommonData("System", "SpellFragmentNum", 0);
			let fragva = GetAreaCommonData("System", "SpellFragmentValue", [1/5, 5]);
			
			let FragmentRate = GetPlayerSpell - trunc(GetPlayerSpell);
			let DisplayRate = trunc(5 * FragmentRate);
			
			if(spell >= num){
				ObjRender_SetAlpha(obj, 255);
				if(spell < num+1){
					ObjSprite2D_SetSourceRect(obj, 160 + 16*DisplayRate, 16, 176 + 16*DisplayRate, 32);
					ObjRender_SetColor(obj, 155, 155, 155);
				}else{
					ObjSprite2D_SetSourceRect(obj, 240, 16, 256, 32);
					ObjRender_SetColor(obj, 255, 255, 255);
				}
			}
			else{ObjRender_SetColor(obj, 155, 155, 155); ObjSprite2D_SetSourceRect(obj, 160, 16, 176, 32);}
			yield;
		}
	}
	
	task Text{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let img = GetCurrentScriptDirectory ~ "img/HUD_new.png";
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj, 0, 64, 128, 96);
		ObjSprite2D_SetDestCenter(obj);
		Obj_SetRenderPriority(obj, 1);
		ObjRender_SetPosition(obj, GetScreenWidth - 64, 180, 0);
		ObjRender_SetScaleXYZ(obj, 0.6, 0.6, 0);
		
		let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj3, img);
		ObjSprite2D_SetSourceRect(obj3, 0, 224, 128, 256);
		ObjSprite2D_SetDestCenter(obj3);
		Obj_SetRenderPriority(obj3, 1);
		ObjRender_SetPosition(obj3, GetScreenWidth - 64, 190, 0);
		ObjRender_SetColor(obj3, 0, 192, 0);
		ObjRender_SetScaleXYZ(obj3, 0.9, 1, 0);
	}
}
//----------------------------------------------------
//----------------------------------------------------
//----------------------------------------------------
task TPlayerPower{
	Text;
	
	let pathDigit = GetCurrentScriptDirectory() ~ "img/number.png";
	let count = 5;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, 144);
	ObjRender_SetColor(obj, 255, 100, 0);

	while(true)
	{
		let power = GetPlayerPower();
		power = min(power, GetAreaCommonData("System", "PlayerMaxPower", 400));
		let listNum = DigitToArray(power, count);
		count = GetNumSize(GetAreaCommonData("System", "PlayerMaxPower", 400));

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, GetStgFrameWidth/2+292 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 0, (num + 1) * 16, 20);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
			ObjSpriteList2D_AddVertex(obj);
		}
		ObjRender_SetX(obj, GetStgFrameWidth/2+292 + 3 * 14); //slash
		ObjSpriteList2D_SetSourceRect(obj, 160, 0, 176, 20);
		ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
		ObjSpriteList2D_AddVertex(obj);

		//400
		ObjRender_SetX(obj, GetStgFrameWidth/2+292 + 4 * 14);
		ObjSpriteList2D_SetSourceRect(obj, 4*16, 0, 5*16, 20);
		ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
		ObjSpriteList2D_AddVertex(obj);

		ObjRender_SetX(obj, GetStgFrameWidth/2+292 + 5 * 14);
		ObjSpriteList2D_SetSourceRect(obj, 0, 0, 16, 20);
		ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
		ObjSpriteList2D_AddVertex(obj);

		ObjRender_SetX(obj, GetStgFrameWidth/2+292 + 6 * 14);
		ObjSpriteList2D_SetSourceRect(obj, 0, 0, 16, 20);
		ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
		ObjSpriteList2D_AddVertex(obj);
		yield;
	}
	
	task Text{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let img = GetCurrentScriptDirectory ~ "img/HUD.png";
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj, 0, 51, 96, 68);
		ObjSprite2D_SetDestCenter(obj);
		Obj_SetRenderPriority(obj, 1);
		ObjRender_SetPosition(obj, GetStgFrameWidth/2+260, 154, 0);
	}
}
//----------------------------------------------------
//----------------------------------------------------
//----------------------------------------------------
task TPointValue{
	Text;
	
	let pathDigit = GetCurrentScriptDirectory() ~ "img/number.png";
	let count = 12;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, 166);
	ObjRender_SetColor(obj, 130, 221, 240);

	while(true)
	{
		let value = GetAreaCommonData("System", "PointValue", 10000);
		value = min(value, GetAreaCommonData("System", "MaxPointValue", 250000));
		let listNum = DigitToArray(value, count);
		count = GetNumSize(GetAreaCommonData("System", "PointValue", 250000));

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, GetStgFrameWidth/2+292 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 0, (num + 1) * 16, 20);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
	
	task Text{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let img = GetCurrentScriptDirectory ~ "img/HUD.png";
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj, 96, 51, 192, 68);
		ObjSprite2D_SetDestCenter(obj);
		Obj_SetRenderPriority(obj, 1);
		ObjRender_SetPosition(obj, GetStgFrameWidth/2+258, 176, 0);
	}
}
//----------------------------------------------------
//uO?EAo?ó
//----------------------------------------------------
task TBossLife
{
	let path = GetCurrentScriptDirectory() ~ "img/Default_System.png";
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D); //lifebar base
	let objb = ObjPrim_Create(OBJ_PRIMITIVE_2D); //actual life bar
	let obj2 = ObjPrim_Create(OBJ_SPRITE_LIST_2D); //boss stars
	let obj3 = ObjPrim_Create(OBJ_SPRITE_LIST_2D); //life bar knobs
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetPrimitiveType(objb, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory() ~ "img/lifebar.png");
	ObjPrim_SetTexture(objb, GetCurrentScriptDirectory() ~ "img/lifebar.png");
	ObjPrim_SetTexture(obj2, path);
	ObjPrim_SetTexture(obj3, GetCurrentScriptDirectory() ~ "img/lifebar.png");
	Obj_SetRenderPriority(obj, 0.68);
	Obj_SetRenderPriority(objb, 0.69);
	Obj_SetRenderPriority(obj2, 0.7);
	Obj_SetRenderPriority(obj3, 0.7);

	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(obj2);
		ObjSpriteList2D_ClearVertexCount(obj3);
		if(objScene != ID_INVALID){
			if(length(GetEnemyBossObjectID) > 0){
				bossEnemy = GetEnemyBossObjectID();

				if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_DURABLE_SPELL) || GetAreaCommonData("System", "HideBoss", false)){
					ObjRender_SetAlpha(obj, 0);
					ObjRender_SetAlpha(objb, 0);
					ObjRender_SetAlpha(obj2, 0);
					ObjRender_SetAlpha(obj3, 0);
				}else{
					ObjRender_SetAlpha(obj, 256);
					ObjRender_SetAlpha(objb, 256);
					ObjRender_SetAlpha(obj2, 256);
					ObjRender_SetAlpha(obj3, 256);
				}
			}else{
				ObjRender_SetAlpha(obj, 0);
				ObjRender_SetAlpha(objb, 0);
				ObjRender_SetAlpha(obj2, 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
			let lifeRate = min(life / lifeM, lifeRateRender);
			ascent(i in 0..vertexcount/2){
		        let indexvert = i*2;
		        let angle = i*360/(vertexcount*0.5-1) - 90;
		        let angle2 = -90-lifeRate*i*360/(vertexcount*0.5-1);
		        ObjPrim_SetVertexPosition(obj, indexvert+0, ObjMove_GetX(bossEnemy[0])+innerrad*cos(angle), ObjMove_GetY(bossEnemy[0])+innerrad*sin(angle), 0);
		        ObjPrim_SetVertexPosition(obj, indexvert+1, ObjMove_GetX(bossEnemy[0])+outerrad*cos(angle), ObjMove_GetY(bossEnemy[0])+outerrad*sin(angle), 0);

		        ObjPrim_SetVertexUVT(obj, indexvert+0, 10, 0);
		        ObjPrim_SetVertexUVT(obj, indexvert+1, 20, 32);


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

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

		    ObjSpriteList2D_SetSourceRect(obj3, 22, 0, 32, 12);
			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[0])+(innerrad + ((outerrad - innerrad)/2))*cos(angle-90);
				let why = ObjMove_GetY(bossEnemy[0])+(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 += 0.01;
			lifeRateRender = min(lifeRateRender, 1);

			RenderBossSteps;
		}else{
			ObjRender_SetAlpha(obj, 0);
			ObjRender_SetAlpha(objb, 0);
			ObjRender_SetAlpha(obj2, 0);
			ObjRender_SetAlpha(obj3, 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){ //Boss's extra lives (stars)
			ObjSpriteList2D_SetSourceRect(obj2, 246, 0, 256, 10);
			ObjSpriteList2D_SetDestCenter(obj2);
			ObjSpriteList2D_AddVertex(obj2);
			ObjRender_SetPosition(obj2, 6 + iStep*10, 16, 0);
		}
	}
}

//----------------------------------------------------
//TAy[?ó
//----------------------------------------------------
task TBossTimer{
	let pathDigit = GetCurrentScriptDirectory() ~ "img/number.png";

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	Obj_SetRenderPriority(obj, 0.75);
	ObjRender_SetY(obj, 0);
	let count = 2;

	let objScene = ID_INVALID;
	let c = 0;

	MoveTimer;
	loop{
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj);
		if(objScene != ID_INVALID){
			RenderTimer();
			if(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) <= 10 && ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) > 5 && c%60 == 0){
				NotifyEvent(GetCommonData("SoundScript", GetOwnScriptID), EV_USER, [99, 100]);
				TimerPulse(0.5);
			}
			if(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) <= 5 && ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) > 0 && c%60 == 0){
				NotifyEvent(GetCommonData("SoundScript", GetOwnScriptID), EV_USER, [100, 100]);
				TimerPulse(1);
			}
		}
		c++;
		yield;
	}

	function RenderTimer(){
		let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER);
		timer = min(timer, 99);
		let listNum = DigitToArray(timer, count);
		let c = 0;
		
		if(timer < 10 && timer != -1){
			ObjRender_SetColor(obj, 255, 0, 0);
		}else{
			ObjRender_SetColor(obj, 255, 255, 255);
		}

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count){
			let num = listNum[iObj];
			ObjRender_SetX(obj, GetStgFrameWidth/2 - 14 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 0, (num + 1) * 16, 20);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
			ObjSpriteList2D_AddVertex(obj);
		}
	}

	task MoveTimer{
		loop{
			while(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL)){
				if(ObjRender_GetY(obj) < 40){
					ObjRender_SetY(obj, ObjRender_GetY(obj) + 0.5);
				}
				yield;
			}
			if(ObjRender_GetY(obj) > 0){
				ObjRender_SetY(obj, ObjRender_GetY(obj) - 0.5);
			}
			yield;
		}
	}

	task TimerPulse(sincrease){
		let s = 1;
		loop(5){
			ObjRender_SetScaleXYZ(obj, s, s, s);
			s += sincrease/5;
			yield;
		}
		loop(10){
			ObjRender_SetScaleXYZ(obj, s, s, s);
			s -= sincrease/10;
			yield;
		}
	}
}

//----------------------------------------------------
//OuEE[eó??ó
//----------------------------------------------------
task TGainSpell(score, type)
{
	//NotifyEvent(GetCommonData("SoundScript", GetOwnScriptID), EV_USER, [10, 100]);
	
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, dirCurrent ~ "img/boni.png");
	
	alternative(type)
	case(0){ //regular spell
		ObjSprite2D_SetSourceRect(obj, 0, 0, 512, 64);
		ObjSprite2D_SetDestCenter(obj);
	}
	case(1){ //last spell
		ObjSprite2D_SetSourceRect(obj, 0, 64, 512, 128);
		ObjSprite2D_SetDestCenter(obj);
	}
	case(2){ //timeout spell
		ObjSprite2D_SetSourceRect(obj, 0, 128, 512, 192);
		ObjSprite2D_SetDestCenter(obj);
	}
	
	
	ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);
	ObjRender_SetAngleX(obj, 90);
	Obj_SetRenderPriority(obj, 0.8);
	ObjRender_SetX(obj, GetStgFrameWidth/2);
	ObjRender_SetY(obj, 98);

	let strScore = IntToString(score);
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, strScore);
	ObjText_SetFontSize(objScore, 20);
	ObjText_SetFontBold(objScore, true);
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 204, 204, 204);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore, 0, 0, 0);
	ObjText_SetFontBorderWidth(objScore, 2);
	Obj_SetRenderPriority(objScore, 0.8);
	ObjRender_SetX(objScore, GetStgFrameWidth/2 - (length(strScore)*10)/2);
	ObjRender_SetY(objScore, 120);
	ObjRender_SetAngleX(objScore, 90);
	
	let anx = 90;
	while(anx > 0){
		ObjRender_SetAngleX(obj, anx);
		ObjRender_SetAngleX(objScore, anx);
		anx -= 6;
		yield;
	}
	
	AddScore(score);

	loop(60)
	{
		yield;
	}
	while(anx < 90){
		ObjRender_SetAngleX(obj, anx);
		ObjRender_SetAngleX(objScore, anx);
		anx += 6;
		yield;
	}
	Obj_Delete(obj);
	Obj_Delete(objScore);
}

task TFailSpellRegular
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, dirCurrent ~ "img/boni.png");
	ObjSprite2D_SetSourceRect(obj, 0, 384, 512, 448);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);
	ObjRender_SetAngleX(obj, 90);
	Obj_SetRenderPriority(obj, 0.6);
	ObjRender_SetX(obj, GetStgFrameWidth/2);
	ObjRender_SetY(obj, 98);
	
	let anx = 90;
	while(anx > 0){
		ObjRender_SetAngleX(obj, anx);
		anx -= 6;
		yield;
	}

	loop(60)
	{
		yield;
	}
	while(anx < 90){
		ObjRender_SetAngleX(obj, anx);
		anx += 6;
		yield;
	}
	Obj_Delete(obj);
}

task TFailSpellTimeout
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, dirCurrent ~ "img/boni.png");
	ObjSprite2D_SetSourceRect(obj, 0, 448, 512, 512);
	ObjSprite2D_SetDestCenter(obj);	
	ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0.5);
	ObjRender_SetAngleX(obj, 90);
	Obj_SetRenderPriority(obj, 0.6);
	ObjRender_SetX(obj, GetStgFrameWidth/2);
	ObjRender_SetY(obj, 98);
	
	let anx = 90;
	while(anx > 0){
		ObjRender_SetAngleX(obj, anx);
		anx -= 6;
		yield;
	}

	loop(60)
	{
		yield;
	}
	while(anx < 90){
		ObjRender_SetAngleX(obj, anx);
		anx += 6;
		yield;
	}
	Obj_Delete(obj);
}

//----------------------------------------------------
//FPS?ó
//----------------------------------------------------
task TCurrentFps()
{
	let objText = ObjText_Create();
	ObjText_SetFontSize(objText, 12);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 204, 204, 204);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText, 0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 2);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(objText, GetScreenWidth() - 8);
	Obj_SetRenderPriority(objText, 1.0);
	ObjRender_SetX(objText, 0);
	ObjRender_SetY(objText, GetScreenHeight() - 15);

	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, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 204, 204, 204);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText, 0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 2);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(objText, GetScreenWidth() - 8);
	Obj_SetRenderPriority(objText, 1.0);
	ObjRender_SetX(objText, 0);
	ObjRender_SetY(objText, GetScreenHeight() - 15);

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

//----------------------------------------------------
//A[aAEaA
//----------------------------------------------------
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 CreateScreenCapture{ //courtesy of Natashi
	let pathShot = GetModuleDirectory ~ "Screenshots/";
	CreateRenderTarget("RTARGET_SCREENSHOT");
	let texture = "RTARGET_SCREENSHOT";
	AddVirtualKey(VK_USER2,KEY_LCONTROL,KEY_INVALID);
	loop{
		if(GetKeyState(KEY_P)==KEY_PUSH){
			let count = GetFilePathList(pathShot ~ "0.bmp");
			let countLength = length(count);
			RenderToTextureA1(texture,0,100,true);
			SaveRenderedTextureA2(texture,pathShot~IntToString(countLength)~".png",0,0,GetScreenWidth,GetScreenHeight);
		}
		yield;
	}
}