//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Quotient space(cylinder)"]
#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 "./../sharedFuncSaul.dnh"


let objBoss;
let width = 444;
let height = 448;
let life = 3800;
let time= 60;
let scene = GetEnemyBossSceneObjectID();
let path = GetCurrentScriptDirectory~"./../../SFX/";
let len = LoadEffect(path~"LenenShot.WAV", 70);
let warp = LoadEffect(path~"Warp 1.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);
	ObjEnemyBossScene_StartSpell(scene);
	SetCommonData("non1", "yes");
	SetCommonData("spell", "Quotient");
	
	animSpell(objBoss, 1);
	TFinalizeSpell(scene, objBoss, false);
	
	ObjMove_SetDestAtFrame(objBoss, width/2, height/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{
    SetForbidPlayerSpell(true);
    Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	Spell("Mind twist: Quotient[r]space (cylinder)", objBoss, 1);
	let shot = LoadEffect(path~"ATTACK5.WAV", 70);
	let shot2 = LoadEffect(path~"ATTACK4.WAV", 70);
	wait(240); let t=0;
	ObjEnemy_SetDamageRate(objBoss, 10,0);
	while(t<90){
	    ObjSound_Play(len);
		ascent(j in 0..50){
			let obj =CreateShotA1(0, 5+j*10, 6, 0, 416,0); ObjRender_SetAlpha(obj, 150);
			ObjShot_SetIntersectionEnable(obj, false);
		}
		wait(5); t+=5;
	}
	SetForbidPlayerSpell(false);
	t=0;
	wait(20);
	associate;
	wait(30);
	ObjSound_Play(LoadEffect(path~"Charge.WAV", 80));
	wait(60);
	ascent(i in 0..4){
	    ObjSound_Play(shot);
	    let ang= rand(15, 25);
	    ascent(j in 0.. i +2){
		    let obj = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 6, 135-90*i +ang+ 15*j, -0.2, 4.5, 292, 0);
			quotient(obj, true, 293, true, life/2 + 200, false);
		}
		wait(60-20*i);
	}
	ObjEnemy_SetDamageRate(objBoss, 60,20);
	while(t<400){
	    ObjSound_Play(shot);
	    let obj = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(4.5,6), rand(0,360), rand(-0.1, -0.01), rand(3.5,4.3), 292, 0);
		quotient(obj, true, 293, false, life/2 + 200, false);
		wait(15-0.0275*t); t+= 15-0.0275*t;
	}
	move(0.3*width, 0.7*width, height/5, 2*height/5, 0, 140, 200, objBoss, life/2 + 200, scene, time/2);
	circles(5);
	shotSound(shot, -1, 60, 5, objBoss, false, life/2 +200, scene, time/2); 
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)> life/2 +200 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)> time/2){
	    let obj = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), rand(3.5,4.5), rand(0,360), rand(-0.3, -0.2), rand(2.2,3), 292, 0);
		quotient(obj, true, 293, false, life/2 + 200, false);
		wait(5);
	}
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	ObjSound_Play(LoadEffect(path~"ATTACK3.WAV", 100));
	loop(30){DeleteShotAll(TYPE_SHOT, TYPE_FADE); yield;}
	wait(30);
	ObjMove_SetDestAtFrame(objBoss, width/2, height/2, 60);
	wait(120);
	ObjSound_Play(LoadEffect(path~"Horn.WAV", 80));
	ObjSound_Play(LoadEffect(path~"Charge.WAV", 80));
	ascent(i in 0..2){
	    DelayLaser(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), -30+60*i, 500, 25, 25, 90);
		DelayLaser(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 150+60*i, 500, 25, 25, 90);
	}
	wait(60);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	shotSound(shot2, 300, 60, 4,objBoss, false, 0, scene, 0);
	cage(1);
	wait(150);
	spir(9, 50, 14);
	wait(90);
	ascent(i in 0..2){
	    DelayLaser(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 60+60*i, 500, 25, 25, 90);
		DelayLaser(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 240+60*i, 500, 25, 25, 90);
	}
	ObjSound_Play(LoadEffect(path~"Horn.WAV", 80));
	ObjSound_Play(LoadEffect(path~"Charge.WAV", 80));
	wait(60);
	shotSound(shot2, -1, 60, 2,objBoss, false, 0, scene, 0);
	cage(2);
	wait(60);
}


task circles(numBul){
    let shot = LoadEffect(path~"ATTACK4.WAV", 70);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/2 +200 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>time/2){
	    ObjSound_Play(shot);
	    ascent(i in 0..numBul){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= life/2 + 200){return;}
		    let obj = CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2, GetAngleToPlayer(objBoss)+ i* 360/numBul, 0.1, 5, 288, 0);
		    quotient(obj, true, 291, false, life/2 + 200, false);
		}
		wait(50);
	}
}


task cage(n){
    if(n==1){
	    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
			ascent(i in 0..2){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
				let obj =CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 9, -30+60*i, 332,0);
				ObjShot_SetSpellResist(obj, true);
				let obj2 =CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 9,  150+60*i, 332,0);
				quotient(obj, true, 333, false, 0, true); quotient(obj2, true, 333, false,0, true);
				ObjShot_SetSpellResist(obj2, true);
			}
			wait(4);
		}
	}
	else{
	    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
			ascent(i in 0..2){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
				let obj =CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 9, 60+60*i, 332,0);
				ObjShot_SetSpellResist(obj, true);
				let obj2 =CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 9, 240+60*i, 332,0);
				quotient(obj, true, 333, false,0, true); quotient(obj2, true, 333, false,0, true);
				ObjShot_SetSpellResist(obj2, true);
			}
			wait(4);
		}
	}
}


task spir(delay, pause, increment){
    let ang; let dir = 1; if(rand(0,1)>1/2){dir=-1;} let t=0; let twinkle = LoadEffect(path~"TWINKLE.WAV", 70);
	shotSound(twinkle, -1, 70, 11, objBoss, false, 0, scene, 0);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    ang= rand(10,40);
	    while(t<300){
			ascent(i in 0..4){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
				let obj =CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3, ang+rand(-5,5) + i*90, 318,0); 
				quotient(obj, true, 321, false,0, false);
			}
		    wait(delay); t+=delay; ang+=dir*increment;
		}
		dir*=-1; wait(pause); t=0;
	}
}


task associate{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
	    ObjSound_Play(len);
	    ascent(i in 0..2){
		    ascent(j in 0..3){
			    let obj =CreateShotA1(6+j*10 +410*i, height, 6, -90, 416,0); ObjRender_SetAlpha(obj, 150);
				ObjShot_SetSpellResist(obj, true);
				ObjShot_SetIntersectionEnable(obj, false);
			}
			ascent(j in 0..2){
				let obj =CreateShotA1(0, 4+j*10+426*i, 6, 0, 416,0); ObjRender_SetAlpha(obj, 150);
				ObjShot_SetSpellResist(obj, true);
				ObjShot_SetIntersectionEnable(obj, false);
			}
		}
		wait(5);
	}
}


task quotient(obj, cylinder, graphic, begin, change, spellRes){
    while(ObjMove_GetX(obj)>0 && ObjMove_GetX(obj)<width && ObjMove_GetY(obj)>0 && ObjMove_GetY(obj)<height){yield;}
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= change){return;}
	let x = ObjMove_GetX(obj); let y = ObjMove_GetY(obj); if(begin){ObjSound_Play(warp);}
	if(x<=0){
		let obja=CreateShotA1(width, y, ObjMove_GetSpeed(obj), ObjMove_GetAngle(obj), graphic, 0);
		if(spellRes){ObjShot_SetSpellResist(obja, true);}
	}
	if(x>=width){
		let obja=CreateShotA1(0, y, ObjMove_GetSpeed(obj), ObjMove_GetAngle(obj), graphic, 0);
		if(spellRes){ObjShot_SetSpellResist(obja, true);}
	}
	if(cylinder){
	    if(y<=0){let obja=CreateShotA1(x, height, ObjMove_GetSpeed(obj), ObjMove_GetAngle(obj), graphic, 0); if(spellRes){ObjShot_SetSpellResist(obja, true);}}
		if(y>=height){let obja=CreateShotA1(x, 0, ObjMove_GetSpeed(obj), ObjMove_GetAngle(obj), graphic, 0); if(spellRes){ObjShot_SetSpellResist(obja, true);}}
	}
	else{
		if(y<=0){CreateShotA1(width-x, height, ObjMove_GetSpeed(obj), ObjMove_GetAngle(obj), graphic, 0);}
		if(y>=height){CreateShotA1(width-x, 0, ObjMove_GetSpeed(obj), ObjMove_GetAngle(obj), graphic, 0);}
	}
}
	
	
	
	
	
	
	
	
	