//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["bulletSplit"]
#System["./../../system/NSystem.dnh"]
#Player["./../../player/Reimu by plasmaflame22/Reimu_A.txt", "./../../player/Reimu by plasmaflame22/Reimu_C.txt"]
#include "./../../ZUNShot/ZUNShot_Const.txt"
#include "./../sharedFuncSamir.dnh"


let objBoss;
let width = 420;
let height = 440;
let life = 4800;
let limit = life*0.4;
let time= 70;
let scene = GetEnemyBossSceneObjectID();
let path = GetCurrentScriptDirectory~"./../../SFX/";

let charge = LoadEffect(path~ "Charge.WAV", 80);
let horn = LoadEffect(path~ "Horn.WAV", 80);
let shot3 = LoadEffect(path~ "ATTACK3.WAV", 90);
let shot4 = LoadEffect(path~ "ATTACK4.WAV", 65);
let shot5 = LoadEffect(path~ "ATTACK5.WAV", 70);
let explosion = LoadEffect(path~ "SmallExplosion.WAV", 90);

@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);
	
	SetPlayerClip(5,5, 415, 435);
	SetCommonData("non1", "yes");
	
	Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
	
	animNon(objBoss, 1);
	TFinalizeNon(scene, objBoss);
	
	ObjMove_SetDestAtFrame(objBoss, width/2, height*0.3, 60);
	Main;
}

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




task Main{
    SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	wait(120);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	ObjSound_Play(LoadEffect(path ~"Charge.WAV", 80)); 
    wait(60);
	ObjEnemy_SetDamageRate(objBoss, 100, 60);
	move(objBoss, width/4, 3*width/4, height/4 ,height*0.4, 0, 120, 30, objBoss, limit, scene, time/2);	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time/2){
	    ObjSound_Play(shot5);
		ascent(i in 0..4){
			let obj = CreateShotA2(X(objBoss), Y(objBoss), 11, 45*i+rand(-30,30), rand(-0.9,-0.4), 0, 293, 5);
			ObjShot_SetSpellResist(obj, true);
			split(obj, [3, 3, 7], [0.6, 6], 60, limit);
		}
		let obja = CreateShotA2(X(objBoss), Y(objBoss), 11, rand(180,360), rand(-0.9,-0.4), 0, 293, 5);
		ObjShot_SetSpellResist(obja, true);
		split(obja, [3, 3, 7], [0.6, 6], 60, limit);
		loop(110){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time/2){yield;}}
		loop(4){
			let obj = CreateShotA2(X(objBoss), Y(objBoss), 11, rand(-70,40), rand(-0.9,-0.4), 0, 293, 5);
			ObjShot_SetSpellResist(obj, true);
			split(obj, [3, 3, 1], [0.6, 2], 30, limit);
		}
		loop(4){
			let obj = CreateShotA2(X(objBoss), Y(objBoss), 11, rand(150,250), rand(-0.9,-0.4), 0, 293, 5);
			ObjShot_SetSpellResist(obj, true);
			split(obj, [3, 3, 1], [0.6, 2], 30, limit);
		}
		loop(120){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time/2){yield;}}
		yield;
	}
	yield;
	ObjSound_Play(LoadEffect(path~ "ATTACK3.WAV", 90));
	DeleteShotAll(TYPE_ALL, TYPE_FADE);
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjMove_SetDestAtFrame(objBoss, width/2, height*0.45, 60);
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 20, 0);
	ObjSound_Play(charge);
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 100, 60);
	move(objBoss, width/3, 2*width/3, height/4 ,height*0.38, 0, 80, 30, objBoss, 0, scene, 0);	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
		ObjSound_Play(shot5);
	    loop(4){
			let obj = CreateShotA2(X(objBoss), Y(objBoss), 11, rand(0,360), rand(-0.9,-0.4), 0, 294, 5);
			ObjShot_SetSpellResist(obj, true);
			split(obj, [3, 2, 1], [0.4, 2], 30, 0);
		}
		wait(40);
	}
}


/* numBulArray[0] is number of bullets the bullet first splits in. Each splits in numBulArray[1] bullets,
which each explode in lines of numBulArray[2] bullets. */


task split(obj, numBulArray, speed, delay, lim ){
    let ang = rand(0,360); let timeLim = time/2; if(lim==0){timeLim =0;}
	while(ObjMove_GetSpeed(obj)>0){yield;}
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=lim || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=timeLim){return;}
	ObjSound_Play(shot5);
	ascent(i in 0..numBulArray[0]){
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=lim || Obj_IsDeleted(obj)){return;}
		let obja = CreateShotA2(X(obj), Y(obj), 6, ang + 360/numBulArray[0] *i, rand(-0.6,-0.2), 0, 249, 10);
		ObjShot_SetSpellResist(obja, true);
		finalSplit(obja, [numBulArray[1], numBulArray[2]], speed, delay, lim);
	}
	Obj_Delete(obj);
}
	
	
task finalSplit(obja, numBulArray, speed, delay, lim){
    let ang = rand(0,360); let timeLim = time/2; if(lim==0){timeLim =0;}
	while(ObjMove_GetSpeed(obja)>0){yield;}
	if(numBulArray[1]>1){
		ObjSound_Play(horn);
	    ascent(i in 0..numBulArray[0]){DelayLaser(obja, X(obja), Y(obja), ang+ 360/numBulArray[0] *i, 600, 30, 23, delay);}
	}
	wait(delay); let inc = speed[0];
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=lim || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=timeLim){return;}
	ObjSound_Play(shot4);
	if(numBulArray[1]>1){
		inc = (speed[1]-speed[0])/numBulArray[1];
		ascent(i in 0..numBulArray[0]){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=lim){return;}
			ascent(j in 0..numBulArray[1]){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=lim || Obj_IsDeleted(obja)){return;}
				let obj = CreateShotA1(X(obja), Y(obja), speed[1]-inc*j, ang + 360/numBulArray[0] *i, 142, 10);
				ObjShot_SetSpellResist(obj, true);
			}
		}
	}
	else{
		let graphic = 262; if(lim==0){graphic= truncate(rand(262, 266));}
		ascent(i in 0..numBulArray[0]){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=lim || Obj_IsDeleted(obja)){return;}
			CreateShotA1(X(obja), Y(obja), rand(speed[0], speed[1]), ang + 360/numBulArray[0] *i, graphic, 10);
		}
	}
	Obj_Delete(obja);
}


	
	
	
	
	