#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Nonspell 3"]
#Text[""]
#BGM["./../bgm/Boss.ogg"]
#Background["./../BG.txt"]
#Player["./../player/Ultima's DDC Reimu B/ReimuH.dnh", "./../player/gt_medicine/PracticeP.txt"]
#System["./../System/Default_System.txt"]

#include "./../lib/Shot/ZUNShot/ZUNShot_Const.txt"

#include "./../lib/Render/EnemyMark.txt"
#include "./../FxnSFX.dnh"
#include "./../lib/Funcs.txt"
#include "./../lib/GoodLib.txt"
#include "./../lib/Render/MagicCircle.txt"

#include "./../lib/Render/Gizmo's Touhou Boss Sprite Library/GizmoSpriteLibrary.txt"
#include "./../lib/Render/Gizmo's Touhou Boss Sprite Library/GizmoFSpriteLibrary.txt"

let objBoss;
let pass;

let count = 0;
let countx = 0;

let shot = SoundSFX(GetCurrentScriptDirectory~"./../lib/se/shot1.wav");
let shot2 = SoundSFX(GetCurrentScriptDirectory~"./../lib/se/shot2.wav");
let charge = SoundSFX(GetCurrentScriptDirectory~"./../lib/se/charge.wav");
let discharge = SoundSFX(GetCurrentScriptDirectory~"./../lib/se/famdie.wav");
let laa = SoundSFX(GetCurrentScriptDirectory~"./../lib/se/laser1.wav");

@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(4000);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(42);
    }
    case(EV_REQUEST_SPELL_SCORE){
        SetScriptResult(1000000);
    }
}
@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	
	SetCommonData("Cold", false);
	SetCommonData("Pretty", true);
	BFX_Init();
	
    ObjMove_SetDestAtFrame(objBoss, gx(), 70, 30);
	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){
        round_bullets;
        count = 0;
    } 
    count++;
    yield;
}

task TDrawLoop{
	renderAlicePCB(objBoss);
	DrawName("Alice Margatroid", "YELLOW");
	EnemyMarker(objBoss);
	magiccircle(objBoss, 1.75, 0, 150, 100, true);
}

task Enemy(type, x, y, frame, gg){
	let objEnemy;
	objEnemy = ObjEnemy_Create(OBJ_ENEMY);
    ObjEnemy_Regist(objEnemy);
	
	ObjMove_SetX(objEnemy, bx);
	ObjMove_SetY(objEnemy, by);
	
	hitbox(objEnemy, 20);
	
	pass = 0;
	Obj_SetValue(pass, "die", 1);
	
	let ex = 0;
	let ey = 0;
	let ang = 0;
	let rang = 90;
	
	let b = 2;
	
	//1=B; 2=R
	renderTinyGoreF(objEnemy, type);
	magiccircle(objEnemy, 0.5, 191, 64, 64, false);
	
	ObjMove_SetX(objEnemy, bx);
	ObjMove_SetY(objEnemy, by);
	ObjMove_SetDestAtFrame(objEnemy, x, y, frame);
	
	wait(frame);
		ex = ObjMove_GetX(objEnemy);
		ey = ObjMove_GetY(objEnemy);
	DelayLaser(ex, ey, gg, 500, 50, 1, 24, 60*3);
	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		if(Obj_GetValueD(pass, "die", 1) == 0){Obj_Delete(objEnemy);}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
		
		rang = GetAngleToPlayer(objEnemy);
		ascent(A in 0..1){
			if(Obj_GetValueD(pass, "die", 1) == 0){Obj_Delete(objEnemy);}
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}

			if(!Obj_IsDeleted(objEnemy)){
				ObjMove_AddPatternA2(objEnemy, 60*1, 0, gg, 1/10, 0, 6);
				
				while(!Obj_IsDeleted(objEnemy)){
					if(ObjMove_GetX(objEnemy) <= 1 || ObjMove_GetX(objEnemy) >= stgw-1){spawn(objEnemy);}
					if(ObjMove_GetY(objEnemy) <= 1 || ObjMove_GetY(objEnemy) >= stgh-1){spawn(objEnemy);}
					yield;
				}
			}
		}

		wait(60*8);
	}
	
	Obj_Delete(objEnemy);
	
	task hitbox(obj, rad){
		wait(frame);
		while(!Obj_IsDeleted(obj)){
			ObjEnemy_SetIntersectionCircleToShot(objEnemy, ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), rad); 
			ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), rad);
			yield;
		}
	}
}

task round_bullets{
ObjEnemy_SetDamageRate(objBoss, 0, 0);
wait(60);
PlaySFX(charge);
Concentration(bx, by, 60*1.5, 150, 2, 15/2, 1, BLEND_ADD_ARGB, 255, 102, 255, 2);
wait(60*1.25);
PlaySFX(discharge);
ConcentrationExplosion(bx, by, 60*1.5, 250, 7/4, 1, BLEND_ADD_ARGB, 217, 102, 255, 2);
ObjEnemy_SetDamageRate(objBoss, 80, 40);
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;} //Default kill to prevent (0,0) spawning
    let rang = 0;
	
	//{
		let ang;
		
		let l = 0;
		let b = 4;
	//}
	
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){
		Obj_SetValue(objBoss, "cast", 1);
		wait(60*1);
		
		ascent(i in 0..2){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
			
			PlaySFX(shot);
			ObjSound_SetVolumeRate(shot, 70);
			ascent(a in 0..b){
				ang = rang + 360/b*a;
				
				Enemy(2, px, py, 60*2, ang);
			}
			
			rang += 90;
			wait(60*2);
		}
		
		l = rand(10, 160);
		
		Obj_SetValue(objBoss, "cast", 0);
		wait(60 * 5);
    }
}

task spawn(obj){
ObjMove_AddPatternA1(obj, 1, 0, NO_CHANGE);
Obj_SetValue(objBoss, "cast", 0);
wait(60*1/60);
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
	let rang = ObjMove_GetAngle(obj);
	
		let ang = 0;
		let b = 1;
	
	task bitch(){
		wait(60 * 1/6);
		PlaySFX(laa);
	}
	
	while(!Obj_IsDeleted(obj)){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
		if(Obj_IsDeleted(obj)){return;}
			
		//PlaySFX(laa);
		PlaySFX(shot);
		ObjSound_SetVolumeRate(shot, 80);
		ascent(i in 0..1){
			ang = (ObjMove_GetAngle(obj)+180) + 360/1*i;
				
			CreateStraightLaserA1(ObjMove_GetX(obj), ObjMove_GetY(obj), ang, 1500, 60, 60*1, 20, 60*1/6);
		}
		bitch();
		ascent(i in 0..b){
			ang = (ObjMove_GetAngle(obj)+180) + 360/b*i;
				
			let c = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 0, ang, 402, 5);
			ObjMove_AddPatternA1(c, 60*1/60, 1, NO_CHANGE);
			
			go(c);
		}
		
		Obj_Delete(obj);
		yield;
	}
}

task go(obj){
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
	let rang = ObjMove_GetAngle(obj);
	
		let ang = 0;
		let b = 40;
		let sp = 1;
	
	task die(obj, time){
		wait(time);
		ObjShot_FadeDelete(obj);
	}
	
	while(!Obj_IsDeleted(obj)){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){return;}
		if(Obj_IsDeleted(obj)){return;}
	
		PlaySFX(shot);
		ObjSound_SetVolumeRate(shot, 80);
		
		ascent(AA in 0..1){
			if(Obj_IsDeleted(obj)){return;}
			ascent(i in 0..b){
				ang = rang + 360/b*i;
					
				let c = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), sp, ang, 46, 0);
				ObjMove_AddPatternA2(c, 60*1/10, 0, NO_CHANGE, 1/1000, 0, 4);
				//die(c, 60*6);
			}
			
			sp++;
			
			rang += 73;
			wait(60*1/500);
		}
		
		Obj_Delete(obj);
		yield;
	}
}

task DelayLaser(x, y, ang, l, w, dt, graph, delay){
	let objlaser = CreateStraightLaserA1(x, y , ang, l, w, dt, graph, delay);
	ObjStLaser_SetSource(objlaser, false);
	//ObjStLaser_SetAngle(objlaser, angtoP(objBoss));
	
	loop(delay-1){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) <= 0){Obj_Delete(objlaser); return;}
		yield;
	}
	Obj_Delete(objlaser);
}

task TFinalize {
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
	
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
    return;
}