#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell 2 H"]
#Text["Vermillion Card \"Descent of the Crane's Wings\""]
#Background["./Background.txt"] 

let bossObj;
let bossX = 0;
let bossY = 0;

#include "./Expanded CTC (Darkness1)/Expanded_ShotConst.txt"
#include"./FX/Bloom VFX darkwalker247/BloomFX.txt"
#include"./lib.txt"
#include"./FX/Effects.cs"
#include"./Cutin/cutin.txt"

@Initialize {
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);
	ObjMove_SetDestAtSpeed(bossObj,GetStgFrameWidth/2,GetStgFrameHeight/4,5);
	SFXload;
	mainTask;
} 

@Event { 
	alternative(GetEventType()) 
	case(EV_REQUEST_LIFE) { 
		SetScriptResult(3250);
	}
	case(EV_REQUEST_TIMER) { 
		SetScriptResult(60);
	}
} 

@MainLoop {
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);

	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,32);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,20);
	
	yield;
} 

@Finalize {
} 

task mainTask {
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	ObjEnemy_SetDamageRate(bossObj,1,1);
	ObjCutin_Name("Vermillion Card \"Descent of the Crane's Wings\"");
	BossRender;
	movement;
	end;
} 


task movement {
ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
//Spam
let F1_time = 60;
let F1_bull_wave = 4;
let F1_time2 = 0;
let F1_bull_wave2 = 1;
let F1_bull_num = 11;
let F1_min_spd = 1.5;
let F1_max_spd = 3;

//wings
let F2_time = 120;
let F2_prox = 57;
let F2_prox2 = 2.25;
let F2_spread = 60/5;

//Balls
let F3_XPos = GetCenterX;
let F3_YPos = GetStgFrameHeight/4;
let F3_XRand = 100;
let F3_YRand = 20;
let F3_bull_num = 5;
let F3_time = F2_time;
let F3_bullring = 8; 
let F3_min_spd = 1;
let F3_max_spd = 2;
let F3_trailnum = 3;

//wall
let F4_time = F2_time; 
let F4_num = 3; 
let F4_wav = 3;
let F4_circ_wav = 15;
let F4_dist = 3;
let F4_spd1 = 6;
let F4_spd2 = 10;
let F4_CW_spd1 = 5;
let F4_dec1 = -0.1;
let F4_acc1 = 0.05;

//stream
let F5_Bul_num = 5;
let F5_Bul_length = 5;
let F5_Bul_Ring = 7;
let F5_Bul_MaxSpd = 5; 
let F5_Bul_Acc = 0.1;
let F5_time_1 = 30;
let F5_time_2 = 60;
	
wait(180);	
	
if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	PlaySE(charge4);
	BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
	
	wait(60);
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	
Fire1(false, F1_time, F1_time2, F1_bull_wave, F1_bull_wave2, F1_bull_num, F1_min_spd, F1_max_spd);

wait(F1_time);
if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	PlaySE(charge4);
	BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
	
	wait(60);
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}

Fire2(F2_time, F2_prox, F2_prox2, F2_spread);
Fire3(F3_XPos, F3_YPos, F3_XRand, F3_YRand, F3_bull_num, F3_time, F3_bullring, F3_min_spd, F3_max_spd, F3_trailnum);
Fire4(F4_time, F4_num, F4_wav,F4_circ_wav,F4_dist,F4_spd1,F4_spd2,F4_CW_spd1,F4_dec1,F4_acc1);

wait(F2_time);
if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}

	PlaySE(charge4);
	BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
	
	wait(60);
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}

Fire5(false, F5_Bul_num, F5_Bul_length, F5_Bul_Ring, F5_Bul_MaxSpd, F5_Bul_Acc, F5_time_1, F5_time_2);	

ObjEnemy_SetDamageRate(bossObj,45,25);
	
loop{


wait(180);

if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	PlaySE(charge4);
	BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
	
	wait(60);
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	
Fire1(true, F1_time, F1_time2, F1_bull_wave, F1_bull_wave2, F1_bull_num, F1_min_spd, F1_max_spd);

wait(F1_time);
if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	PlaySE(charge4);
	BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
	
	wait(60);
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}

Fire2(F2_time, F2_prox, F2_prox2, F2_spread);
Fire3(F3_XPos, F3_YPos, F3_XRand, F3_YRand, F3_bull_num, F3_time, F3_bullring, F3_min_spd, F3_max_spd, F3_trailnum);
Fire4(F4_time, F4_num, F4_wav,F4_circ_wav,F4_dist,F4_spd1,F4_spd2,F4_CW_spd1,F4_dec1,F4_acc1);

wait(F2_time);
if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}

	PlaySE(charge4);
	BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
	
	wait(60);
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}

Fire5(true, F5_Bul_num, F5_Bul_length, F5_Bul_Ring, F5_Bul_MaxSpd, F5_Bul_Acc, F5_time_1, F5_time_2);

wait(180);	
	
if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	PlaySE(charge4);
	BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
	
	wait(60);
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	
Fire1(false, F1_time, F1_time2, F1_bull_wave, F1_bull_wave2, F1_bull_num, F1_min_spd, F1_max_spd);

wait(F1_time);
if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	PlaySE(charge4);
	BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
	
	wait(60);
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}

Fire2(F2_time, F2_prox, F2_prox2, F2_spread);
Fire3(F3_XPos, F3_YPos, F3_XRand, F3_YRand, F3_bull_num, F3_time, F3_bullring, F3_min_spd, F3_max_spd, F3_trailnum);
Fire4(F4_time, F4_num, F4_wav,F4_circ_wav,F4_dist,F4_spd1,F4_spd2,F4_CW_spd1,F4_dec1,F4_acc1);

wait(F2_time);
if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}

	PlaySE(charge4);
	BFX_FocusA1(bossX,bossY,100,VFX_Smoke,VFX_Beams,45);
	
	wait(60);
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}

Fire5(false, F5_Bul_num, F5_Bul_length, F5_Bul_Ring, F5_Bul_MaxSpd, F5_Bul_Acc, F5_time_1, F5_time_2);
}
}

task Fire1(rev, time, time2, bull_wave, bull_wave2, bull_num, min_spd, max_spd){
	let ang = rand(70,110);
	let bullwave_true = 0;
	
	ascent(LOL_FILLER_STUFF in -bull_wave..bull_wave+1){
		bullwave_true++;
		}
	
	if(rev == false){	
		ascent(angI in -bull_wave..bull_wave+1){
			if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
			PlaySE(shot1);
			Fire1_Shot(ang+(angI*30), time2, bull_wave2, bull_num, min_spd, max_spd);
		wait(time/bullwave_true);
			}
	}
	
	else if(rev == true){	
		descent(angI in -bull_wave..bull_wave+1){
			if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
			PlaySE(shot1);
			Fire1_Shot(ang+(angI*30), time2, bull_wave2, bull_num, min_spd, max_spd);
		wait(time/bullwave_true);
			}
	}
		
		
}

task Fire1_Shot(angle, time2, bull_wave2, bullnum, min_spd, max_spd){
	let randang = 30;
	let randang2 = randang;
	let spd = min_spd;
	loop(bullnum){
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		loop(bull_wave2){
			CreateShotA2(bossX, bossY, spd, angle + rand(-randang, randang), -(spd*0.01), spd*0.3, round(rand(121,122)), 10);
		}
		randang -= randang2/bullnum;
		spd += max_spd/(min_spd*bullnum);
		wait(time2/bullnum)
	}
}

task Fire2(time, prox, prox2, spread){
	let bullnum = 30;
	let angadd = prox/bullnum;
	
	
	let angA = GetAngleToPlayer(bossObj)-90;
	let angB = GetAngleToPlayer(bossObj)+90;
	loop(bullnum){
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		
		ascent (i in -2 .. 3){
		CreateShotA1(bossX, bossY, 5, angA + spread * i, 284, 0);
		CreateShotA1(bossX, bossY, 5, angB + spread * i, 284, 0);
		}
		
		angA -= angadd;		
		angB += angadd;		
		wait(time/(bullnum + (bullnum * 2) + (bullnum /2) ) );
	}
	
	loop(bullnum * prox2){
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		
		ascent (i in -2 .. 3){
		CreateShotA1(bossX, bossY, 5, angA + spread * i, 284, 0);
		CreateShotA1(bossX, bossY, 5, angB + spread * i, 284, 0);
		}
		
		angA += angadd;		
		angB -= angadd;		
		
		spread-=(spread/bullnum);
		wait(time/(bullnum + (bullnum * 2) + (bullnum /2) ) );
	}
	
	loop(bullnum /2){
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		
		ascent (i in -2 .. 3){
		CreateShotA1(bossX, bossY, 5, angA + spread * i, 284, 0);
		CreateShotA1(bossX, bossY, 5, angB + spread * i, 284, 0);
		}
		
		wait(time/(bullnum + (bullnum * 2) + (bullnum /2)));
	}
	
}

task Fire3(XPos, YPos, XRand, YRand, bull_num, time, bullring, min_spd, max_spd, trailnum){
	loop(bull_num){
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		let ang = rand(0,360);//GetAngleToPlayer(bossObj);
		let XRng = rand(-XRand, XRand);
		let YRng = rand(-YRand, YRand);
		PlaySE(shot9);
		loop(bullring){
			let spd = min_spd;
		loop(trailnum){
		CreateShotA2(XPos+XRng, YPos+YRng, 0, ang, spd*0.01, spd, 9, 30);
		spd += (max_spd/min_spd)/trailnum;
		}
		CreateShotA2(XPos+XRng, YPos+YRng, 0, ang, spd* 0.01, spd, 25, 30);
			ang += 360/bullring;
		}
		wait(time/bull_num);
	}
}

task Fire4(time, num, wav,circ_wav,dist,spd1,spd2,CW_spd1,dec1,acc1){
	
	loop(num){
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	PlaySE(laser4);
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	let ang = GetAngleToPlayer(bossObj);
	
	loop(circ_wav){
	let circ = CreateShotA2(bossX,bossY,spd1,(dist*wave)+ang,dec1,0,285,10);
	ObjMove_AddPatternA2(circ,45,NO_CHANGE,NO_CHANGE,acc1,0,spd2);
	ang+=360/circ_wav;
	}
	
	ang = GetAngleToPlayer(bossObj);
	
	ascent(wave in -(wav-1)..wav){
	let stream = CreateShotA2(bossX,bossY,spd1,(dist*wave)+ang,dec1,0,285,10);
	ObjMove_AddPatternA2(stream,45,NO_CHANGE,NO_CHANGE,acc1,0,spd2);
	}
	
	wait(time/num);
	}
	
}
/*	let Bul_num = 8;
	let Bul_length = 10;//----
	let Bul_Ring = 10;//----
	let Bul_MaxSpd = 5; 
	let Bul_Acc = 0.5;
	let time_1 = 30;
	let time_2 = 180;*/
	
task Fire5(rev, Bul_num, Bul_length, Bul_Ring, Bul_MaxSpd, Bul_Acc, time_1, time_2){
	if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
	let ang = 0;
	if(rev == true){ang += 180;}
	PlaySE(shot4);
	
	let angadd = rand(0,360);
	let shot_ang = 0;
	let colorA = 165;
	let colorB = 9;
	loop(2){
		let F5_Shot = CreateShotA2(bossX,bossY,10,ang,-1,0.5,colorA,0);
		ObjRender_SetBlendType(F5_Shot,BLEND_ALPHA);
		ObjShot_SetSpellResist(F5_Shot,true);
		Fire5_Shot(F5_Shot, shot_ang, angadd, Bul_num, Bul_length, Bul_Ring, Bul_MaxSpd, Bul_Acc, time_1, time_2, colorB);
		ang+=180;
		colorA++;
		colorB++;
		shot_ang+=180;
		angadd*=-1;
	}
}

task Fire5_Shot(obj, shot_ang, angadd, bul_num, Bul_length, Bul_Ring, Bul_MaxSpd, Bul_Acc, time_1, time_2, ColorB){
	wait(30);
	shot_ang = GetAngleToPlayer(obj);
	loop(bul_num){
		if(Obj_IsDeleted(obj)){return;}
			let posX = ObjMove_GetX(obj);
			let posY = ObjMove_GetY(obj);
		loop(Bul_length){
			if(Obj_IsDeleted(obj)){return;}
			PlaySE(shot2);
					
			loop(Bul_Ring){
			CreateShotA2(posX + 20*cos(shot_ang), posY + 20*sin(shot_ang), 0.001, shot_ang, Bul_Acc, Bul_MaxSpd, ColorB, 20);
			shot_ang+=360/Bul_Ring;
			}
			
		
		wait(time_1/Bul_length);
		}
		shot_ang += (360/shot_ang)/2; 
		wait(time_2/(bul_num*time_1/Bul_length));
		}
}

task end {
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}
	PlaySE(SpellOff);
	SetAutoDeleteObject(true);
	loop(5){
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);	
	yield;
	}
	Obj_Delete(bossObj);
	wait(30);	
	CloseScript(GetOwnScriptID());
}