//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["quotient"]
#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 = 2000;
let time= 66;
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", 60);
let lenen = LoadEffect(path~ "LenenShot.WAV", 80);
let shot5 = LoadEffect(path~ "ATTACK5.WAV", 70);
let twinkle = LoadEffect(path~ "TWINKLE.WAV", 70);


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


@Initialize{
    objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	SetPlayerClip(5,5, 415, 435);
    ObjEnemyBossScene_StartSpell(scene);
	
	SetCommonData("non1", "no");
	SetCommonData("spell", "quotient");
	
	animSpell(objBoss, 1);
	TFinalizeSpell(scene, objBoss, true, 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 Com(obj, centre, incAng, rad){


task Main{
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	Spell("Ring theory:[r]Quotient structures", objBoss, 1);
    wait(210);
	
	
	ObjSound_Play(shot5);
	let obj = CreateShotA1(width/2, 8, 0,0, 292, 30);
	let obj2 = CreateShotA1(width/2, height-8, 0,0, 292, 30);
	ObjShot_SetSpellResist(obj, true); ObjShot_SetSpellResist(obj2, true);
	ObjRender_SetAlpha(obj, 160); ObjRender_SetAlpha(obj2, 160);
	
	wait(60);
	
	quotient(obj, [0, width], [height,height], 2.7, 188, [10,25], 3, [25,40], time-30);
	quotient(obj2, [0, width], [0,0], 2.7, 199, [10,25], 3, [25,40], time-30);
	wait(120);
	
	phase1(width/2, 0.15*height, 30, [1,2.5], 12); 
	wait(220);
	ascent(i in 0..6){
	    phase1(GetPlayerX, GetPlayerY, 70-2*i, [1, 2.5], 10+truncate(i/3));
		wait(230 - 20*i);
	}
	wait(120);
	
	
	// time-30.
	
	
	ObjSound_Play(LoadEffect(path~ "ATTACK3.WAV", 90));
	loop(40){DeleteShotAll(TYPE_ALL, TYPE_FADE); yield;}
	
	wait(90);
	
	
	
	ObjSound_Play(shot5);
	let obj5 = CreateShotA1(width/4, 8, 0,0, 292, 0);
	let obj6 = CreateShotA1(width/4, height-8, 0,0, 292, 0);
	let obj3 = CreateShotA1(3*width/4, 8, 0,0, 292, 0);
	let obj4 = CreateShotA1(3*width/4, height-8, 0,0, 292, 0);
	ObjShot_SetSpellResist(obj5, true); ObjShot_SetSpellResist(obj6, true);
	ObjShot_SetSpellResist(obj3, true); ObjShot_SetSpellResist(obj4, true);
	ObjRender_SetAlpha(obj5, 160); ObjRender_SetAlpha(obj6, 160);
	ObjRender_SetAlpha(obj3, 160); ObjRender_SetAlpha(obj4, 160);
	
	wait(60);
	
	quotient(obj5, [0, width], [height,height], 2.6, 188, [10,20], 2.5, [25,40], 0);
	quotient(obj6, [0, width], [0,0], 2.6, 188, [10,20], 2.5, [25,40], 0);
	quotient(obj3, [0, width], [height,height], 2.6, 199, [10,20], 2.5, [25,40], 0);
	quotient(obj4, [0, width], [0,0], 2.6, 199, [10,20], 2.5, [25,40], 0);
	
	wait(60);
	ObjSound_Play(shot5);
	bulletFollow(1.65,20, 20, 50, 60, time-60);
	wait(400);
	ObjSound_Play(shot5);
	let bul = CreateShotA1(width/2, height/2, 0, 0, 356, 20);
	ObjShot_SetSpellResist(bul, true);
	wait(30); let numBul = 8;
	while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time-60){
	    let ang = rand(0,360); 
		ObjSound_Play(shot4);
	    ascent(i in 0..numBul){
		    CreateShotA1(width/2, height/2, 1, ang+ 360/numBul *i, 57, 10);
		}
		loop(70){
			if(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time-60){yield;}
		}
		yield;
	}
	ObjSound_Play(LoadEffect(path~ "ATTACK3.WAV", 90));
	Obj_Delete(bul);
	loop(20){DeleteShotAll(TYPE_SHOT, TYPE_FADE); yield;}
	
	wait(60);
	
	ObjSound_Play(shot5);
	let obj7 = CreateShotA1(8, 8, 0, 45, 356, 40);
	let obj8 = CreateShotA1(width-8, 8, 0, 135, 356, 40);
	let obj9 = CreateShotA1(8, height-8, 0, -45, 356, 40);
	let obj10 = CreateShotA1(width-8, height-8, 0, -135, 356, 40);
	final(obj7); final(obj8); final(obj9); final(obj10);
	sound(obj7);
	
}

task sound(obj){
    wait(100);
	ObjSound_Play(LoadEffect(path~"LenenBomb.WAV", 100));
	shotSound(shot4, -1, 60, 3, obj, true, 0, scene, 0);
}


task final(obj){
    let ang = ObjMove_GetAngle(obj);
	ObjShot_SetSpellResist(obj, true);
	wait(40);
	ObjSound_Play(charge);
	wait(60);
	ObjMove_SetSpeed(obj, 1);
	while(!Obj_IsDeleted(obj)){
		loop(8){CreateShotA1(X(obj), Y(obj), rand(4,8), ang+rand(45, 315), 378, 0);}
	    yield;
	}
}


task phase1(x,y, delay, speed, delayBul){
    ObjSound_Play(shot5);
    let centre = CreateShotA1(x, y, 0,0, 368, 0);
	intersect(centre, delay);
	ObjShot_SetSpellResist(centre, true); let alpha=60;
	ObjRender_SetAlpha(centre, alpha);
	loop(delay){alpha+=(255-60)/delay; ObjRender_SetAlpha(centre, alpha); yield;}
	let numBul = 4; let rad = 50; let incAng = rand(3.5,5); let dir = 1; if(probaRoll(0.5)){dir=-1;}
	ascent(i in 0..numBul){
	    if(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=time-30){return;}
	    let obj = CreateShotA1(X(centre), Y(centre), 0,360/numBul *i, 305, 0);
		ObjShot_SetSpellResist(obj, true);
		control(obj, i, centre, numBul, rad, incAng*dir, speed, delayBul);
	}
	loop(59){DelayLaser(centre, X(centre), Y(centre), GetAngleToPlayer(centre), 600, 30,3, 2); yield;}
	let ang = GetAngleToPlayer(centre);
	DelayLaser(centre, X(centre), Y(centre), GetAngleToPlayer(centre), 600, 30,3, 2); 
	yield;
	ObjShot_SetGraphic(centre, 355);
	DelayLaser(centre, X(centre), Y(centre), ang, 600, 30,3, 30);
	ObjSound_Play(horn);
	ObjMove_AddPatternA2(centre, 30, 0.1, ang, 0.1 ,0, 3);
	while(X(centre)< width+70 && X(centre) >-70 && Y(centre)< height+70 && Y(centre)>-70){yield;} 
	Obj_Delete(centre);
}

task intersect(centre,delay){
    ObjShot_SetIntersectionEnable(centre, false);
	wait(delay+40);
	ObjShot_SetIntersectionEnable(centre, true);
}

task control(obj, i, centre, numBul, rad, incAng, speed, delayBul){
	ObjMove_SetDestAtFrame(obj, X(centre) + rad*cos(360/numBul *i), Y(centre)+rad*sin(360/numBul *i), 30);
	wait(30);
	Com(obj, centre, incAng, rad);
	wait(60);
	shotSound(lenen, -1, 70, delayBul, obj, true, 0, scene, 0);
	trail(obj, 10, speed, [262,268], 1, delayBul);
}
    
    


task bulletFollow(speed, periodFollow, periodStraight, pause, rad, lim){
    while(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>lim){
	    let bulFollow = CreateShotA1(width/2, 0.3*height, 0, 0, 355, 20);
		loop(40){
		    if(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=lim){Obj_Delete(bulFollow); return;} 
			yield;
		}
		
		ObjShot_SetSpellResist(bulFollow, true); ObjShot_SetAutoDelete(bulFollow, false);
		
		loop(60){
		    if(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=lim){Obj_Delete(bulFollow); return;} 
			yield;
		}
		followCts(bulFollow, speed, periodFollow, periodStraight, pause);
		shoot(bulFollow, rad); let life = GetPlayerLife;
		while(GetPlayerLife >= life && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>lim){yield;}
		Obj_Delete(bulFollow); 
		loop(60){
		    if(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=lim){return;} 
			yield;
		}
	}
}


task shoot(bulFollow, rad){
    while(! Obj_IsDeleted(bulFollow)){
	    if(absolute(GetPlayerX- X(bulFollow)) < rad && absolute(GetPlayerY- Y(bulFollow)) < rad){
		    while(absolute(GetPlayerX- X(bulFollow)) < rad && absolute(GetPlayerY- Y(bulFollow)) < rad){
			    ObjSound_Play(twinkle);
				let ra = rand(-5,5); let ang= rand(0,360);
				ascent(i in 0..5){
					CreateShotA1(X(bulFollow), Y(bulFollow), 2, ang +ra +72 *i, 57, 5);
				}
				wait(20);
			}
		}
		yield;
	}
}


task followCts(obj, speed, periodFollow, periodStraight, delay){
    while(!Obj_IsDeleted(obj)){
	    let t=0; ObjMove_SetSpeed(obj, speed);
	    while(t<periodFollow){
		    ObjMove_SetAngle(obj, GetAngleToPlayer(obj));
			yield; t++;
		}
		wait(periodStraight);
		t=0; ObjMove_SetSpeed(obj, 0);
		while(t<delay){t++; yield;}
		yield;
	}
}


task quotient(obj, xdest, ydest, speed, graphic, period, delay, pause, lim){
	let ang = findAngRad(xdest[0], X(obj), ydest[0], Y(obj))[0]; 
	let ang2 = findAngRad(xdest[1], X(obj), ydest[1], Y(obj))[0];
	ctsLaser(obj, ang, ang2);
	ObjSound_Play(charge);
	wait(60);
	while(!Obj_IsDeleted(obj)){
	    let t=0; let per = rand(period[0], period[1]);
		while(t<per){
		    if(ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=lim){return;}
			CreateShotA1(X(obj), Y(obj), speed, ang, graphic, 10);
			CreateShotA1(X(obj), Y(obj), speed, ang2, graphic, 10);
			wait(delay); t+=delay;
		}
		wait(rand(pause[0], pause[1]));
	}
}




task ctsLaser(obj, ang, ang2){
    while(! Obj_IsDeleted(obj)){
	    DelayLaser(obj, X(obj), Y(obj), ang, 600, 40, 17, 2);
		DelayLaser(obj, X(obj), Y(obj), ang2, 600, 40, 17, 2);
		yield;
	}
}




task trail(obj, spread, speed, graphic, numBul, delay){
    let s = spread/2; 
	while(! Obj_IsDeleted(obj)){
	    let ang = ObjMove_GetAngle(obj);
	    loop(numBul){
		    if(Obj_IsDeleted(obj)){return;}
			CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), rand(speed[0], speed[1]), ang+180+rand(-s,s), truncate(rand(graphic[0], graphic[1])), 0);
		}
		wait(delay);
	}
}	
	


	
	
	