#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 03 - Empty Cut \"Desiring Spirits\""]
#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/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 fam = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Special04.wav");
let shot = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Shot01.wav");

let wuon = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Special05.wav");
let slash = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Slash.wav");
let charge = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Charge01.wav");
let charged = SoundSFX(GetCurrentScriptDirectory ~ "./../lib/SFX/Charge02.wav");

@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(3600);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(62);
    }
	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", 75); SetCommonData("music", true);}
	
    ObjMove_SetDestAtFrameWeight(objBoss, gx(), 100, 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, "Empty Cut \"Desiring Spirits\"", false, 0, 204, 0);
}

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

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

	red(); 
	squares();
}

task squares{
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let en = [];
		
		let x; let y;
		let a; 
		
		let v = 0;
	//}

	ascent(i in 0..5){ en = en ~ [CreateEnemy(80 + i*120, 150, 30)];} PlaySFX_V(fam, 62);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		wait(60*1.7);
		
		PlaySFX_V(charged, 65); DelayedPlaySFX_V(shot, 60, 70);
		ascent(j in 0..length(en)){
			x = getx(en[j]); y = gety(en[j]); a = angtoP(en[j]);
			Concentration(x, y, 40, 50, 1, 2, 8, BLEND_ADD_RGB, 102, 102, 102, 4);
			
			let B = PolygonShot(x, y, 1.5, a, 8, 8, 3, 21, 3);
			ascent(i in 0..length(B)){
				if(!Obj_IsDeleted(B[i])){
					let sp = Obj_GetValue(B[i], "sv");
					
					ObjMove_AddPatternA2(B[i], 60, 6*sp, NO_CHANGE, -1/10*sp, v, 2*sp);
				}
			}
			
			v *= -1;
		}
		
		wait(60*1.23);
		ascent(i in 0..length(en)){
			x = 80 + i*120; y = 150;
			ObjMove_SetDestAtFrameWeight(en[i], rand(x-100, x+100), rand(y-60, y+60), 60);
			yield;
		}
	}
}

task red{
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
	//{
		let x = 40;
		let v = 2.75; 
	//}

	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		PlaySFX_V(wuon, 75);
		let B = CreateShotA1(x, 2, 2.6, rand(90-v, 90+v), 270, 5);
		trail(B, 10, +v, 1.5);
		wait(60*2);
		
		PlaySFX_V(wuon, 75);
		B = CreateShotA1(stgw - x, 2, 2.6, rand(90-v, 90+v), 270, 5);
		trail(B, 10, -v, 1.5);
		
		
		v *= -1; x = rand(20, 60);
		wait(60*2.5);
	}
	
	task trail(eu, num, vo, time){
		//{
			let B = [];
			
			let a; let r = geta(eu) + 90;
			
			let l = 0; let mc = 0;
		//}
		
		ObjShot_SetSpellResist(eu, true); ObjShot_SetAutoDelete(eu, false);
		while(!Obj_IsDeleted(eu) && gety(eu) < stgh+400){
			r += sin(l)*11;
			
			PlaySFX_V(slash, 25);
			let x = getx(eu); let y = gety(eu);
			ascent(i in 0..num){
				a = r + 360/num*i;
				
				x = getx(eu); y = gety(eu);
				B = B ~ [CreateShotA1(x, y, 17, a, 806, 0)]; set(B[mc]);
				ObjMove_AddPatternA3(B[mc], 15, -1/50, NO_CHANGE, 0, 0, 0, 276);
				ObjShot_SetSpellResist(B[mc], true); ObjShot_SetAutoDelete(B[mc], false);
				mc++;	
			}

			
			l += vo;
			wait(time);
		}
		
		Obj_Delete(eu);
		
		task set(le){
			ObjRender_SetBlendType(le, BLEND_INV_DESTRGB);
			ObjRender_SetAlpha(le, 160);
			wait(15);
			
			ObjRender_SetBlendType(le, BLEND_ADD_ARGB);
			ObjRender_SetAlpha(le, 255);
			wait(12);
			
			ObjRender_SetBlendType(le, BLEND_ALPHA);
			ObjRender_SetScaleXYZ(le, 0.65, 0.65, 0);
			ObjShot_FadeDelete(le);
		}
	}
}

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));
    }
	
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
    return;
}