//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["ideal"]
#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 "./../../ZUNShot/shotMathsCons.txt"
#include "./../sharedFuncSamir.dnh"


let objBoss; let bulCancel; let bulCancel2;
let width = 420;
let height = 440;
let life = 3400;
let limit = life*0.35;
let time= 75;
let scene = GetEnemyBossSceneObjectID();

let path = GetCurrentScriptDirectory~"./../../SFX/";
let charge = LoadEffect(path~ "Charge.WAV", 80);
let horn = LoadEffect(path~ "Horn.WAV", 80);
let shot2 = LoadEffect(path~ "ATTACK2.WAV", 70);
let shot4 = LoadEffect(path~ "ATTACK4.WAV", 80);
let shot5 = LoadEffect(path~ "ATTACK5.WAV", 70);
let wave = LoadEffect(path~ "Wave.WAV", 70);
let explosion = LoadEffect(path~ "SmallExplosion.WAV", 95);


@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", "ideal");
	
	animSpell(objBoss, 1);
	TFinalizeSpell(scene, objBoss, false, false);
	
	Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
	
	ObjMove_SetDestAtFrame(objBoss, width/2, 90, 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{
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	Spell("Ring theory:[r]Ideal absorption", objBoss, 1);
    wait(200);
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	ObjSound_Play(charge);
	wait(60);
	ring(8,65,[76, 83]);
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	wait(60);
	halfCircles(40, 453);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>limit && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time/3){yield;}
	yield;
	ObjSound_Play(LoadEffect(path~"ATTACK3.WAV", 90));
	DeleteShotAll(TYPE_SHOT, TYPE_FADE);
	ObjEnemy_SetDamageRate(objBoss, 0, 0);
	wait(60); 
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	ObjSound_Play(LoadEffect(path~"POWER UP.WAV", 100));
	wait(100);
	ObjSound_Play(LoadEffect(path~"LenenBomb.WAV", 100));
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	let numBul = 12; let ang= rand(0,360); let dir=1;
	move(objBoss, width/2-50, width/2+50, 80,81, 0, 120, 60, objBoss, 0, scene, 0);
	shotSound(shot4, -1, 45, 3, objBoss, false, 0, scene, 0);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    let t = 0;
		while(absolute(t)<90){
		    let wvel = -dir*rand(4,5); let pause = rand(10,25);
			ascent(i in 0..numBul){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
			    let obj= CreateShotA1(X(objBoss), Y(objBoss)-15, 10, ang+t+ 360/numBul *i, 152, 15);
				slowRing(obj, 72, [0.7, 1.2], 0, 0, wvel,pause, 0.82, false);
				wavy(obj, wvel, pause);
			}
			t+=5*dir; wait(2);
		}
		yield; dir*=-1;
	}	    
	
}

// task move(xmin, xmax, ymin, ymax, iniDel, time, delay, objBoss, limit, scene, timeLimit){
// task wavy(obj, wvel, pause){


task halfCircles(numBul, graphic){
    ObjSound_Play(shot5);
    ascent(i in 0..2){
		let obj3 = CreateShotA1(X(objBoss), Y(objBoss), 0,0, graphic, 0);
		ObjShot_SetSpellResist(obj3, true);
		ObjMove_SetDestAtFrame(obj3, width/4 * (2*i+1), height-16, 60);
		shoot(obj3, 1, numBul*2.5, -90, 0, 1);
	}
	
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>4*life/5 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time-15){yield;}
	ObjSound_Play(LoadEffect(path~"ATTACK3.WAV", 90));
	wait(30);
	ObjSound_Play(shot5);
	let obj = CreateShotA1(X(objBoss), Y(objBoss), 0,0, graphic, 0);
	ObjRender_SetAlpha(obj, 210);
	ObjShot_SetSpellResist(obj, true);
	ObjMove_SetDestAtFrame(obj, 16, 0.4*height, 30);
	shoot(obj, 0, numBul, 0, 0, 0.77);
	
	let obj2 = CreateShotA1(X(objBoss), Y(objBoss), 0,0, graphic, 0);
	ObjRender_SetAlpha(obj2, 210);
	ObjShot_SetSpellResist(obj2, true);
	ObjMove_SetDestAtFrame(obj2, width-16, 0.4*height, 30);
	shoot(obj2, 0, numBul, 180, 70, 0.77);
	wait(60);
	ObjSound_Play(horn);
	
}


task shoot(obj, n, numBul, ang, iniDel, probAbsorb){
    wait(60);
	let anga = ObjMove_GetAngle(obj);
	loop(20){ObjMove_SetAngle(obj, ObjMove_GetAngle(obj) - anga/20); yield;}
	wait(40); 
	let lim = 0; if(n==0){lim = limit;}
	let timeLim = 0; if(n==0){timeLim = time/3;}
	wait(iniDel); let graph = 57; if(probAbsorb ==1){graph = 42;}
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>lim && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>timeLim){
	    ObjSound_Play(shot2);
		ascent(i in 0..numBul){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=lim){return;}
			let obja = CreateShotA2(X(obj), Y(obj), 0, ang-120 + 240/numBul *i, 0.05, 2.5, graph, 5);
			ObjRender_SetAlpha(obja, 210);
			slowRing(obja, 70, [0.7, 1.1], 0.05, 2.5, 0,0, probAbsorb, true);
		}
		if(n==1){wait(100);} else{wait(45);}
	}
	Obj_Delete(obj);
}
	
    


task ring(numBul, rad, graphic){
    ObjSound_Play(wave);
	bulCancel = CreateShotA1(X(objBoss), Y(objBoss), 0,0, 57, 0);
	ObjShot_SetSpellResist(bulCancel, true);
	ObjShot_SetIntersectionEnable(bulCancel, false);
	Obj_SetVisible(bulCancel, false);
	ascent(i in 0.. numBul){
		let obj= CreateShotA1(X(bulCancel)+ rad*cos(360/numBul *i), Y(bulCancel)+ rad*sin(360/numBul *i), 0, 360/numBul *i, graphic[0], 20);
		ObjShot_SetSpellResist(obj, true); ObjShot_SetAutoDelete(obj, false);
		Com(obj, bulCancel, 0.4, rad);
	}
	bulCancel2 = CreateShotA1(X(objBoss), Y(objBoss), 0,0, 57, 0);
	ObjShot_SetSpellResist(bulCancel2, true);
	ObjShot_SetIntersectionEnable(bulCancel2, false);
	Obj_SetVisible(bulCancel2, false);
	ascent(i in 0.. numBul){
		let obj= CreateShotA1(X(bulCancel2)+ rad*cos(360/numBul *i), Y(bulCancel2)+ rad*sin(360/numBul *i), 0, 360/numBul *i, graphic[1], 20);
		ObjShot_SetSpellResist(obj, true); ObjShot_SetAutoDelete(obj, false);
		Com(obj, bulCancel2, -0.4, rad);
	}
	
	ellipse(bulCancel, [80, 0.28*height], [width-80, 0.75*height], 10, 0.3);
	ellipse(bulCancel2, [80, 0.75*height], [width-80, 0.28*height], 10, 0.3);
}




task ellipse(obj, point1, point2, littleRad, inc){
	let angRad = findAngRad(point2[0], point1[0], point2[1], point1[1]); 
	let bigRad = angRad[1]/2; let ang = angRad[0]; let theta =90 ;
	let centre = [(point1[0]+point2[0])/2, (point1[1]+point2[1])/2];
	let x = centre[0] + bigRad*cos(theta) *cos(ang) -littleRad*sin(theta)*sin(ang);
	let y= centre[1] +bigRad*cos(theta)*sin(ang)+ littleRad * sin(theta) *cos(ang);
	ObjMove_SetDestAtFrame(obj, x,y, 40);
	wait(40);
	ObjSound_Play(horn);
	wait(50);
	while(true){
		x = centre[0] + bigRad*cos(theta) *cos(ang) -littleRad*sin(theta)*sin(ang);
		y= centre[1] +bigRad*cos(theta)*sin(ang)+ littleRad * sin(theta) *cos(ang);
		ObjMove_SetPosition(obj, x, y);
		theta+= inc; yield;
	}
}
	




task slowRing(obj, rad, newspeed, accel, maxspeed, wvel, pause, probAbsorb, phase1){
    if((absolute(X(obj)-X(bulCancel)) < rad && absolute(Y(obj)-Y(bulCancel)) < rad) || (absolute(X(obj)-X(bulCancel2)) < rad && absolute(Y(obj)-Y(bulCancel2))<rad)){
	    if(probaRoll(0.85)){Obj_Delete(obj); return;}
	}
    let speed1 = ObjMove_GetSpeed(obj);
	while(!Obj_IsDeleted(obj)){
		yield;
		let speed2 = ObjMove_GetSpeed(obj); 
		if((absolute(X(obj)-X(bulCancel)) > rad || absolute(Y(obj)-Y(bulCancel)) > rad) && (absolute(X(obj)-X(bulCancel2)) > rad || absolute(Y(obj)-Y(bulCancel2))>rad)){
			if(phase1){ObjMove_SetSpeed(obj, speed2);} else{ObjMove_SetSpeed(obj, speed1);}
			wavy(obj, wvel, pause); ObjMove_AddPatternA2(obj, 0, NO_CHANGE,NO_CHANGE, accel, 0, maxspeed);
			while((absolute(X(obj)-X(bulCancel)) > rad || absolute(Y(obj)-Y(bulCancel)) > rad) && (absolute(X(obj)-X(bulCancel2)) > rad || absolute(Y(obj)-Y(bulCancel2))>rad) && !Obj_IsDeleted(obj)){yield;}
		}
		else{
			if(probaRoll(probAbsorb)){Obj_Delete(obj); return;}
			ObjMove_SetAcceleration(obj, 0); ObjMove_SetSpeed(obj, rand(newspeed[0], newspeed[1]));
			while(((absolute(X(obj)-X(bulCancel)) <= rad && absolute(Y(obj)-Y(bulCancel)) <= rad) || (absolute(X(obj)-X(bulCancel2)) <= rad && absolute(Y(obj)-Y(bulCancel2)) <= rad)) && !Obj_IsDeleted(obj)){yield;}
		}
		yield;
	}
}
			
			

	
	
	
	
	
	
	