//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Chaos sign: instability"]
#include "./../../ZUNShot/ZUNShot_Const.txt"
#include "./../sharedFuncDave.dnh"



let objBoss;
let width = 384;
let height = 448;
let life = 3200;
let time= 60;
let scene = GetEnemyBossSceneObjectID();

@Event{
    alternative(GetEventType())
    case(EV_REQUEST_LIFE){
        SetScriptResult(life);
    }
    case(EV_REQUEST_TIMER){
        SetScriptResult(time);
    }
}


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
    ObjEnemy_Regist(objBoss);
	ObjEnemyBossScene_StartSpell(scene);
	SetCommonData("non1", "yes");
	SetCommonData("spell", "Unstable forms");
	
	
	animSpell(objBoss, 1);
	TFinalizeSpell(scene, objBoss, false, false);
	
	
    ObjMove_SetDestAtFrame(objBoss, width/2, 2*height/5, 60);
	Main(200);
}

@MainLoop{
    ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 32); 
    ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 28);
    yield;
}


task Main(numBul){
    Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/SPELLCARD.WAV", 100));
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~ "./../../SFX/CAUTIONSHORT.WAV", 100));
	Spell("Chaos sign:[r]Unstable forms", objBoss, 1);
    wait(240);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Charge.WAV", 80));
	wait(60);
	deleteBul;
	pattern(numBul, false);
	check(numBul);
}

task check(numBul){
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    let lifeIni= GetPlayerLife; let spellIni= GetPlayerSpell;
		while(GetPlayerLife== lifeIni && GetPlayerSpell== spellIni){yield;}
		DeleteShotAll(TYPE_ALL, TYPE_FADE);
		ObjEnemy_SetDamageRate(objBoss, 20, 10);
		wait(180);
		pattern(numBul, true);
		yield;
	}
}

task deleteBul{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    DeleteShotInCircle(TYPE_SHOT, TYPE_IMMEDIATE, width/2, height/2, 120); yield;
	}
}


task pattern(numBul, retry){
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
    let lifeIni= GetPlayerLife; let spellIni= GetPlayerSpell;
    if(!retry){ObjEnemy_SetDamageRate(objBoss, 10, 0);}
    ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK3.WAV", 70));
	ascent(i in 1..5){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;} let obj= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0,0, 285, 0); ObjShot_SetSpellResist(obj, true); cont(obj, i);}
	wait(60);
    let r=0; if(rand(0,1)>=1/2){r=1;}
	if(GetPlayerLife< lifeIni || GetPlayerSpell< spellIni){return;}
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK4.WAV", 70));
	ascent(i in 0..numBul/4){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		let obj = CreateShotA1(384, 448-i*4*448/numBul, 0, 0, 318, 0); astroid(obj, i, 0, numBul, 150, 120, 100, 180, r, retry); ObjShot_SetSpellResist(obj, true);
	}
	ascent(i in 0..numBul/4){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		let obj = CreateShotA1(384+32-i*4 *448/numBul, 448, 0, 0, 318, 0); astroid(obj, i, 1, numBul, 150, 120, 100, 180,r, retry); ObjShot_SetSpellResist(obj, true);
	}
    ascent(i in 0..numBul/4){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		let obj = CreateShotA1(0, i*4*448/numBul, 0, 0, 318, 0); astroid(obj, i, 2, numBul, 150, 120, 100, 180,r, retry); ObjShot_SetSpellResist(obj, true);
	}
    ascent(i in 0..numBul/4){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		let obj = CreateShotA1(-32+i*4 *448/numBul, 0, 0, 0, 318, 0); astroid(obj, i, 3, numBul, 150, 120, 100, 180,r, retry); ObjShot_SetSpellResist(obj, true);
	}
	wait(210);
	if(GetPlayerLife< lifeIni || GetPlayerSpell< spellIni){return;}
	move(140, 240, height/5, 200, 0, 60, 120, objBoss, 0);
	if(!retry){ObjEnemy_SetDamageRate(objBoss, 60, 20);}
	fixedPoint(retry);
	wait(90);
	if(retry){ObjEnemy_SetDamageRate(objBoss, 60, 20);}
	if(GetPlayerLife< lifeIni || GetPlayerSpell< spellIni){return;}
	circles(retry);
}



task cont(obj,i){
    let shot= LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK4.WAV", 30);
    alternative(i)
	case(1){ObjMove_SetDestAtFrame(obj,width-30, 20, 60);}
	case(4){ObjMove_SetDestAtFrame(obj, 30, 20, 60);}
	case(2){ObjMove_SetDestAtFrame(obj,width-30, height-20, 60);}
	case(3){ObjMove_SetDestAtFrame(obj, 30, height-20, 60);}
	wait(120);
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Charge.WAV", 80));
	wait(60);
	shotSound(shot, -1, 50, 3, obj, true, 0);
	ascent(j in 0..60){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0 || Obj_IsDeleted(obj)){return;}
		let obja = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 7, 90*j+45 +rand(-10,10), 325, 0); yield; ObjRender_SetAlpha(obja, 140); Obj_SetRenderPriorityI(obja, 50);
	}
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Horn.WAV", 90));
	ascent(j in 0..60){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0 || Obj_IsDeleted(obj)){return;}
		let obja= CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 7, 90*j+45 +rand(-10,10), 325, 0); yield; ObjRender_SetAlpha(obja, 140); Obj_SetRenderPriorityI(obja, 50);
	}
	ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK3.WAV", 80));
	ascent(j in 0..120){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0|| Obj_IsDeleted(obj)){return;}
	    let obja= CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 3, GetAngleToPlayer(obj) +rand(-10,10), 325, 0); yield; ObjRender_SetAlpha(obja, 140); Obj_SetRenderPriorityI(obja, 50);
	}
	while(!Obj_IsDeleted(obj)){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0 || Obj_IsDeleted(obj)){return;}
	    let obja= CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 3, GetAngleToPlayer(obj) +rand(-10,10), 325, 0); 
		ObjRender_SetAlpha(obja, 140); Obj_SetRenderPriorityI(obja, 50);
		yield;
	}	
}
	




task astroid(obj, posi, segment, numBul, delay, changeSizeSpeed, rad1, rad2, r, retry){
    let lifeIni= GetPlayerLife; let spellIni= GetPlayerSpell;
    if(!retry){wait(180);} else{wait(120);} let t=1; let warp = LoadEffect(GetCurrentScriptDirectory~"./../../SFX/Wave.WAV", 90);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    if(GetPlayerLife< lifeIni || GetPlayerSpell< spellIni){return;}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(warp);
		squareToAstroid(obj, posi, numBul, t, segment, changeSizeSpeed, width/2);
		wait(delay);
		if(GetPlayerLife< lifeIni || GetPlayerSpell< spellIni){return;}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(warp);
		rotate(obj, 45, 90, -1, width/2, height/2, true, true);
		wait(120);
		if(GetPlayerLife< lifeIni || GetPlayerSpell< spellIni){return;}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(warp);
		if(r ==1){ObjMove_SetDestAtFrame(obj, width/2 + rad1 * cos(90*posi*4/numBul +90*segment), height/2 + rad2 * sin(90*posi*4/numBul +90*segment), 120);}
		else{ObjMove_SetDestAtFrame(obj, width/2 + rad2 * cos(90*posi*4/numBul +90*segment), height/2 + rad1 * sin(90*posi*4/numBul +90*segment), 120);}
		wait(delay);
		if(GetPlayerLife< lifeIni || GetPlayerSpell< spellIni){return;}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(warp);
		rotate(obj, 45, 90, 1, width/2, height/2, true, true);
		wait(120);
		segment= (segment+1)%4;
		t= (t+1)%2; r=(r+1)%2;
	}
}
	

    

task squareToAstroid(obj, posi, numBul, cas, segment, changeSizeSpeed, radius){
    if(cas==1){ObjMove_SetDestAtFrame(obj, width/2+ radius* (cos(90*posi*4/numBul + 90*segment))^3, height/2 + radius* (sin(90*posi*4/numBul +90*segment))^3,changeSizeSpeed);}
	else{ObjMove_SetDestAtFrame(obj, width/2 + radius* (cos(90*posi*4/numBul + 90*segment +45))^3, height/2 + radius* (sin(90*posi*4/numBul +90*segment +45))^3,changeSizeSpeed);}
}


task fixedPoint(retry){
    let shot= LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK5.WAV", 60);
    let lifeIni= GetPlayerLife; let spellIni= GetPlayerSpell;
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    if(GetPlayerLife< lifeIni || GetPlayerSpell< spellIni){return;}
	    let ang= rand(0,360);
		ObjSound_Play(shot);
	    ascent(t in 0..11){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		    let obj = CreateShotA1(width/2 + 10*cos(ang+t*360/11), height/2+ 10*sin(ang+360*t/11), 0,ang+ 360*t/11, 412, 10);
			ObjShot_SetSpellResist(obj, true); Obj_SetRenderPriorityI(obj, 70);
			ObjMove_AddPatternA2(obj, 60, 0, NO_CHANGE, 0.02, 0, 3);
		}
	    wait(60);
	}
}

task circles(retry){
    let i=0; let shot= LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK4.WAV", 80);
	let lifeIni= GetPlayerLife; let spellIni= GetPlayerSpell;
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    if(GetPlayerLife< lifeIni || GetPlayerSpell< spellIni){return;}
		shotSound(shot, 20, 40, 1, objBoss, true, 0);
		let ang= rand(0,360); let coord;
		if(i%4 ==0){coord=[rand(0.1*width, 0.25*width), rand(0.1*height, 0.25*height)];}
		else if(i%4 ==1){coord=[rand(0.75*width, 0.9*width), rand(0.1*height, 0.25*height)];}
		else if(i%4 ==2){coord=[rand(0.1*width, 0.25*width), rand(0.75*height, 0.8*height)];}
		else{coord=[rand(0.75*width, 0.9*width), rand(0.75*height, 0.8*height)];}
		ascent(t in 0..20){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			let obj = CreateShotA1(coord[0] + 30*cos(ang+t*360/20), coord[1]+ 30*sin(ang+360*t/20), 0,ang+ 360*t/20, 414, 0); yield;
			Obj_SetRenderPriorityI(obj, 70);
			ObjShot_SetSpellResist(obj, true); ObjMove_AddPatternA2(obj, 90, 0, NO_CHANGE, rand(0.005, 0.02),0, 3);
		}
		if(i<=3 && !retry){wait(40);} else{wait(20);} 
		i++; if(i==3){ObjSound_Play(LoadEffect(GetCurrentScriptDirectory~"./../../SFX/ATTACK3.WAV", 80));}
	}
}







