//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Homotopy Spell"]
#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 = 3400;
let time= 60;
let scene = GetEnemyBossSceneObjectID();
let path = GetCurrentScriptDirectory~"./../../SFX/";
let len = LoadEffect(path~"LenenShot.WAV", 90);
let horn = LoadEffect(path~"Horn.WAV", 80);
let explosion = LoadEffect(path~ "SmallExplosion.WAV", 100);

@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", "Homotopy");
	
	animSpell(objBoss, 1);
	TFinalizeSpell(scene, objBoss, false);
	
	ObjMove_SetDestAtFrame(objBoss, width/2, height/4, 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{
	Name(1, objBoss);
    Indicator(objBoss, 1);
    takeDamage(objBoss, scene, life);
    ObjEnemy_SetDamageRate(objBoss, 0, 0);
	Spell("Mind twist: Homotopic graph", objBoss, 1);
	let shot = LoadEffect(path~"ATTACK5.WAV", 70);
    wait(240);
    let dir=1; if(rand(0,1)>1/2){dir=-1;}
	ObjEnemy_SetDamageRate(objBoss, 10, 0);
	arrowLines(5, 8, 2, dir, 2.7, 3.9, 14);
	wait(240);
	ObjEnemy_SetDamageRate(objBoss, 60, 20);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>4*life/5 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>40){yield;}
	ObjSound_Play(LoadEffect(path~"ATTACK3.WAV", 100));
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/3 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>20){
		ObjSound_Play(LoadEffect(path~"Charge.WAV", 80));
		loop(60){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)> life/3 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>20){ yield;}};
		ascent(i in 0..3){
		    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/3 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>20){
				ObjSound_Play(shot);
				let obj= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss),0,0, 407,5);
				con(obj, i, dir, 1); ObjShot_SetSpellResist(obj, true);
				loop(30){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/3 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>20){yield;}};
			}
			else{yield;}
		}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/2 + 400){loop(300){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/3 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>20){yield;}}} 
		else if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)> life/3){loop(220){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/3 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>20){yield;}}} 
		else{yield;}
	}
	ObjSound_Play(LoadEffect(path~"ATTACK3.WAV", 100));
	wait(100);
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		loop(2){
			ascent(i in 0..3){
			    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){return;}
				ObjSound_Play(shot);
				let obj= CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss),0,0, 400,5);
				con(obj, i, dir, 2); ObjShot_SetSpellResist(obj, true); dir*=-1;
			}
			wait(40);
		}
		wait(80); dir*=-1;
	}
}


task con(obj, i, dir, cas){
    wait(5); let y = 5*height/6 - i*height/3 + rand(-20,20);
	if(cas==1){
	    del(obj);
	    ObjMove_SetDestAtFrame(obj, width/2 + dir*width/2, y, 120-30*i);
		wait(120-30*i);
	}
	if(cas==2){
	    ObjMove_SetDestAtFrame(obj, width/2 + dir*width/2, y, 100-20*i);
		wait(100-20*i);
	}
    shoot(obj, i, dir, cas);
}

task del(obj){
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>life/3 && ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)>20){yield;}
	Obj_Delete(obj);
}


task shoot(obj, i, dir, cas){
    if(cas ==1){
		loop(60){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=life/3 || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=20){ return;} yield;}
		let x = ObjMove_GetX(obj); let y= ObjMove_GetY(obj); let shot =LoadEffect(path~"ATTACK4.WAV", 70);
		DelayLaser(x, y, 90+90*dir, 500, 50, 25, 60);
		ObjSound_Play(horn);
		loop(60){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=life/3 || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=20){ return;} yield;}
		shotSound(shot, 60, 50, 5, objBoss, false, 0, scene, 0);
		helix(x,y, 9, 90+90*dir, 40, width/4 +rand(-20,50), 12, 5, 283, -1, objBoss, 0);
		loop(90){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=life/3 || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=20){ return;} yield;}
		ObjShot_SetGraphic(obj, 400);
		ObjSound_Play(horn);
		loop(70){if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=life/3 || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=20){ return;} yield;}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=life/3 || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=20){ return;}
		ObjSound_Play(explosion);
		loop(35){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= life/3 || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=20 || Obj_IsDeleted(obj)){return;}
			let obja = CreateShotA2(x,y, 4, 90+90*dir + dir*rand(-80,80), rand(-0.08, -0.04), rand(0.6,1.4), 241,0); del(obja);
		}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0 || ObjEnemyBossScene_GetInfo(scene, INFO_TIMER)<=20 || Obj_IsDeleted(obj)){return;}
		let obja = CreateShotA1(x,y, 4.2,GetAngleToPlayer(obj), 376,0); del(obja);
		Obj_Delete(obj);
	}
	else{
		let x = ObjMove_GetX(obj); let y= ObjMove_GetY(obj); let shot =LoadEffect(path~"ATTACK4.WAV", 70);
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0 || Obj_IsDeleted(obj)){return;}
		ObjSound_Play(explosion);
		loop(8){
			if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0 || Obj_IsDeleted(obj)){return;}
			CreateShotA2(x,y, 4, 90+90*dir +dir*rand(-80,80), rand(-0.08, -0.05), rand(0.5,1), 241,0);
		}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0 || Obj_IsDeleted(obj)){return;}
		Obj_Delete(obj);
	}   
}	
    



task arrowLines(numLanes, xincrement, maxdiff, dir, speedmin, speedmax, delay){
    let gap = width/(numLanes+1);
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0){
		arrowLine(xincrement, maxdiff, dir, speedmin, speedmax, delay);
		wait(gap*delay/xincrement);
	}
}
    


task arrowLine(xincrement, maxdiff, dir, speedmin, speedmax, delay){
    let xini= width/2 - dir*width/2 +dir*3;
	while(ObjEnemy_GetInfo(objBoss, INFO_LIFE)>0 && xini< width){
	    ObjSound_Play(len);
	    spawn(-90, xini, rand(speedmin, speedmax), maxdiff);
		wait(delay); xini+= dir*xincrement;
	}
}
    
    

task spawn(ang, t, speed, maxdiff){
    if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<= 0){return;}
	CreateShotA1(t +rand(-maxdiff, maxdiff), height, speed, ang, 412, 5);
}

