//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["kernel"]
#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 = 3800;
let limit = 0.6*life;
let time= 80;
let scene = GetEnemyBossSceneObjectID();

let path = GetCurrentScriptDirectory~"./../../SFX/";
let charge = LoadEffect(path~ "Charge.WAV", 80);
let horn = LoadEffect(path~ "Horn.WAV", 80);
let shot4 = LoadEffect(path~ "ATTACK4.WAV", 50);
let lenen = LoadEffect(path~ "LenenShot.WAV", 80);
let shot5 = LoadEffect(path~ "ATTACK5.WAV", 70);


@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);
    ObjEnemyBossScene_StartSpell(scene);
	
	SetCommonData("non1", "yes");
	SetCommonData("spell", "kernel");
	
	animSpell(objBoss, 1);
	TFinalizeSpell(scene, objBoss, false, false);
	
	Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
	
	ObjMove_SetDestAtFrame(objBoss, width/2, 80, 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);
	Spell("Ring theory:[r]Endomorphism kernels", objBoss, 1);
    wait(200);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	wait(60); 
	
	wall(0.5, -1, 18, 72, 72, limit);
	
	wait(40);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	wait(60);
	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>2*time/3){
	    ObjSound_Play(shot5);
	    let bulCancel = CreateShotA1(X(objBoss), Y(objBoss), 2.5, GetAngleToPlayer(objBoss), 399, 5); cancel(bulCancel, 15);
		loop(3){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=limit || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=2*time/3){return;}
		    let obj=CreateShotA1(X(objBoss), Y(objBoss), rand(2, 2.5), GetAngleToPlayer(objBoss)+rand(-10,10), 240, 5); 
			cancel(obj, 2);
		}
		loop(10){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=limit || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=2*time/3){return;}
			let obj = CreateShotA1(X(objBoss), Y(objBoss), rand(1.2,2.2), GetAngleToPlayer(objBoss)+rand(-26,26), 240, 5); 
			cancel(obj, 2);
		}
		loop(140){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>2*time/3){yield;}}
		yield;
	}
	ObjSound_Play(LoadEffect(path~ "ATTACK3.WAV", 90));
	loop(5){DeleteShotAll(TYPE_ALL, TYPE_FADE); yield;}
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	ObjMove_SetDestAtFrame(objBoss, width/2, 0.2*height, 40);
	wait(55);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	ObjSound_Play(LoadEffect(path~"CAUTIONSHORT.WAV", 100));
	wait(50);
	ObjSound_Play(horn);
	wait(50);
	
	move(objBoss, width/3, 2*width/3, 0.25*height,0.35* height, 0, 80, 60, objBoss, 0, scene, 0);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	shotSound(lenen, -1, 60, 8, objBoss, false, 0, scene, 0);
	wall(1.2, 1, 10,60,1, 0);
	wait(60);
	spiral(3, 2.8, 18, 28, 60);
	
	
}


task spiral(numBul, speed, delay, inc, period){
    let ang = 45 + rand(-15,15);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    let t=0; 
		let x = width/2- inc/absolute(inc) *width/4 +rand(-40,40); let y = rand(height/4, height/3);
		let obj = CreateShotA1(x, y, 0, 0, 362, 10); ObjShot_SetSpellResist(obj, true);
	    while(t<period){
		    ObjSound_Play(shot4);
			ascent(i in 0.. numBul){
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				let bulCancel = CreateShotA1(x, y, speed, ang+360/numBul *i +rand(-2,2), 300, 10);
				cancel(bulCancel, 12);
			}
			wait(delay); ang+=inc; t+=delay;
		}
		Obj_Delete(obj);
		inc*=-1;
	}
}
			



task wall(speed, dir, delayBul, period, pause, lim){
    let num = 40; let timeLim = 2*time/3; if(lim==0){timeLim=0; num =35;}
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>timeLim){
	    let t=0; let factor = width/num;
		if(lim>0){shotSound(lenen, period, 60, delayBul, objBoss, false, lim, scene, 2*time/3);}
		while(t<period){
			let y = height+rand(-2,2); if(dir==1){y= rand(-4,4);}
			ascent(i in 0.. num){
				if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=lim || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=timeLim){return;}
				let obj = CreateShotA1(factor * i, y, 0, rand(0,360), rand(90,105),  0);
				Comm(obj, dir, t, period, speed, pause);
			}
			t+= delayBul; wait(delayBul);
		}
		wait(pause * 2);
	}
}


task Comm(obj, dir, t, period, speed, pause){
    while(!Obj_IsDeleted(obj)){
	    while(t< period + pause){
			ObjMove_SetY(obj, Y(obj)+dir*speed);
			yield; t++;
		}
		wait(pause); t=0;
	}
}
	
	
task cancel(bulCancel, rad){
    ObjShot_SetSpellResist(bulCancel, true);
	while(!Obj_IsDeleted(bulCancel)){
		DeleteShotInCircle(TYPE_SHOT, TYPE_IMMEDIATE, X(bulCancel), Y(bulCancel), rad);
		yield;
	}
}
			
			
			
			
			
			
			
	
	
	
	
	
	