#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 06 - Karma's Roulette \"Destiny\""]
#Text[""]
#Background["./../BG.txt"]
#Player["./../player/lenen/YabuKura.txt", "./../player/lenen/TenkaBami.txt", "./../player/gt_medicine/PracticeP.txt"]
#System["./../System/Default_System.txt"]

#include "./../lib/Shot/Shotconstants.dnh"
//#include "./../lib/Shot/LenenShot/JINXShot_Const.txt"

//#include "./../lib/Render/EnemyMark.txt"
#include "./../lib/FxnSFX.dnh"
#include "./../lib/Funcs.dnh"
#include "./../lib/FXLib.dnh"
#include "./../lib/Render/MagicCircle.txt"
#include "./../lib/Render/Spawner.txt"
#include "./../lib/Patterns.dnh"

//#include "./../lib/Render/Gizmo's Touhou Boss Sprite Library/GizmoSpriteLibrary.txt"
#include "./../lib/Render/LenenBossRender/RenderLenen.txt"
#include "./../lib/RMTCLenenCutinLib/RMTCLenenCutin.txt"

let objBoss;
let objScene = GetEnemyBossSceneObjectID();

let count = 0; let it = [];

let fam = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Special04.wav");
let die = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/BossDeath.wav");
let ass = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/BossAdd.wav");
let charge = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Charge01.wav");

@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(3600);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(82);
    }
	case(EV_REQUEST_IS_DURABLE_SPELL){
		SetScriptResult(true);
	}
    case(EV_REQUEST_SPELL_SCORE){
        SetScriptResult(1000000);
    }
}

@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	
	if(GetCommonData("music", false) == false){BGM_Play("Boss", 65); SetCommonData("music", true);}
	
	LoadKart();
    ObjMove_SetDestAtFrameWeight(objBoss, gx(), 170, 50);
	StartSpell;
	TDrawLoop;
    TFinalize;
}

@MainLoop{
    ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 32); 
    ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 24);
    if(count == 0){
        phases;
        count = 0;
    } 
	
    count++;
    yield;
}

task StartSpell{
wait(60*1);
	ObjEnemyBossScene_StartSpell(objScene);
	
	let objEnemy = GetEnemyBossObjectID[0];
	wait(30);
	
	let rumia = GetCurrentScriptDirectory() ~ "../img/save.png";
	TRMTCLenenCutin(rumia, "Karma's Roulette \"Destiny\"", false, 242, 242, 242);
}

task TDrawLoop{
	renderBoss(objBoss, "Haiji", 1.12, 60*1/2, true);
	magiccircle(objBoss, 1.6, false);
}

task phases{
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	wait(60*4.5); PlaySFX_V(charge, 65);
	Concentration(bx, by, 60, 300, 3, 2, 3, BLEND_ADD_RGB, 242, 242, 242, 1); 
	ChargeCircle(bx, by, 75, 242, 242, 242, BLEND_ADD_RGB);
	wait(65);
	renderAura(objBoss, "Haiji", 1.12, 60*1/2, 200, [242, 242, 242]);
	ObjEnemy_SetDamageRate(objBoss, 55, 20);

	egg();
}

task egg{
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		
	//}
	
	it = it ~ [spawner(bx, by, 1.0)]; PlaySFX_V(fam, 62); wait(5); 
	ObjRenderMove_SetDestAtFrameWeight(it[0], bx - 200, by, 36); wait(40); 
	randomer(it[0], 60*1); wait(60*1.5); selector(it[0]); wait(5);
	
	it = it ~ [spawner(bx, by, 1.0)]; PlaySFX_V(fam, 62); wait(5); 
	ObjRenderMove_SetDestAtFrameWeight(it[1], bx + 200, by, 36); wait(40); 
	randomer(it[1], 60*1); wait(60*1.5); selector(it[1]); 
	while((ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0) && GetCommonData("alive", true)){
		ascent(j in 0..99){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){break;}
			wait(110);
			 
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){break;}
			ascent(i in 0..2){ randomer(it[i], 60*1);}
			wait(60*1.5);
			
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){break;}
			ascent(i in 0..2){ selector(it[i]);}
			
		}
		wait(60*999);
	}
	
	

}

task TFinalize {
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
	
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) + ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) == 0){
		SetCommonData("extend", true);
		
		SetPlayerLife(GetPlayerLife() + 1);
		AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE));
    }
	
	SetCommonData("alive", false); PlaySFX_V(die, 70);
	ObjMove_SetDestAtSpeed(objBoss, bx + 40, rand(60,80), 1/3); 
	let x = bx; let y = by; StartSlow(TARGET_ALL, 15); 
	ascent(i in 0..20){
		x = bx; y = by;
		
		PlaySFX_V(ass, 75);
		let R = 255; let G = 255; let B = 255;
		ConcentrationExplosion(x, y, 40, 010 + i*390/(19/2), 20/4, +4, BLEND_ADD_ARGB, R, G, B, 1);
		ConcentrationExplosion(x, y, 40, 400 - i*390/(19/2), 20/4, +4, BLEND_ADD_ARGB, R, G, B, 1);
		wait(2); DeleteShotAll(TYPE_ALL, TYPE_ITEM); 
	}
	PlaySFX_V(die, 65); StopSlow(TARGET_ALL);
	wait(17);
	
    Obj_Delete(objBoss); ascent(i in 0..2){ Obj_Delete(it[i]);}
	wait(60*2);
	
	SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
    return;
}