#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Shokora Midboss Spell 2"]
#Background["./NamelessPath.dnh"]
#Player["./../lib/Okina_player_by_Python/Okina Matara/Okina.dnh"]

#include "../lib/UNL_Shot/lib_ShotConstants.dnh"
#include "./../lib/includes/BossIncludes.dnh"
#include "./../lib/includes/EffectFunctions.dnh"

@Event{
	alternative(GetEventType())
		case(EV_REQUEST_LIFE){ SetScriptResult(1000); }
		case(EV_REQUEST_TIMER){ SetScriptResult(999); }
		case(EV_REQUEST_SPELL_SCORE){ SetScriptResult(1000000); }
		case(EV_GAIN_SPELL){ AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE)); }
		case(EV_REQUEST_IS_DURABLE_SPELL) { SetScriptResult(true); }
}

@Initialize{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	ObjMove_SetPosition(objEnemy, -1500, -1500);
	SetAutoDeleteObject(true);
	delayedstart;
	End;
}

task End {
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0) { yield; }
	SetShotAutoDeleteClip(32, 40, 32, 32);
	NotifyEventAll(EV_SPELLBGEND, 1);
	CreateItems02(22, ITEM_POINT_S, GetStgFrameWidth/2, -40, 0, -50, 150, 35, 10000);
	//DeleteShotWave(1,GetX,GetY,20);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	//PlaySoundEffect("end",60,GetCenterX);
	//NotifyEventAll(EV_SPAWN_ENEMY,[99.1,GetEnemyX,GetEnemyY]);
	Obj_Delete(objEnemy);
	CloseScript(GetOwnScriptID);
}

task delayedstart{
	ObjEnemyBossScene_StartSpell(objScene);
	ObjEnemyBossScene_SetSpellTimer(objScene, 60*16.5);
	PlaySoundEffect("cardcall", 90, GetCenterX);
	
	let lbcut = GetCurrentScriptDirectory~"sprites/shokorasprite.png";
	ObjCutin_SetSpellcardS4("Thunder Tome &quot;Bolting&quot;", lbcut, "AYA", "255", "255", "0");
	ObjCutin_LaunchS3("AYA", "lbcut", "Hard");
	NotifyEventAll(EV_SPELLBG, 1);
	
	attack1;
}

task attack1{
	wait(90);
	let color = 0;
	angle = 270;
	let time = 115;
	let radius = 35;
	let offset = 0;
	let space = 80;
	let x = 0;
	let y = 0;
	switch = 1;
	let pattern = 0;
	loop(4){
		PlaySoundEffect("sagecast", 90, GetCenterX);
		y = rand(-5, 5);
		wait(45);
		PlaySoundEffect("boltingcast", 90, GetCenterX);
		if(pattern%2==0){
			ascent(i in -2..7){
				if(i*space+y>6 && i*space+y<GetStgFrameHeight-6){
				RecursiveLaser(GetCenterX+205*switch, i*space+y, 90+(90*switch), 100, 20, 30, CIRCLE_S[YELLOW], 105, 6);
				}
				switch*=-1;
			}
		}
		else{
			ascent(i in -2..7){
				if(i*space+y>6 && i*space+y<GetStgFrameWidth-6){
				RecursiveLaser(i*space+y, GetCenterY+224*switch, 180+(90*switch), 100, 20, 30, CIRCLE_S[YELLOW], 105, 6);
				}
				switch*=-1;
			}
		}
		pattern++;
		wait(105);
		PlaySoundEffect("boltinghit", 90, GetCenterX);
		wait(30);
	}
}

	task RecursiveLaser(x, y, dir, len, width, deletetime, graphic, delay, angleoffset){
		let angoff = rand(-angleoffset, angleoffset);
		let targetx = 0;
		let targety = 0;
		let time = 0;
		let switch = -1;
		let spawnpoint;
		let obj = CreateStraightLaserA1(x, y, dir+angoff, len, width, deletetime, graphic, delay);
		ObjStLaser_SetSource(obj, false);
		targetx = x+(len*0.6)*cos(dir+angoff);
		targety = y+(len*0.6)*sin(dir+angoff);
		if(!Obj_IsDeleted(obj)){
			if(targetx>0 && targetx<384 && targety>0 && targety<448){ RecursiveLaser(targetx, targety, dir, len, width, deletetime, graphic, delay, angleoffset); }
			else{
				spawnpoint = GetLineBorderPoint(x, y, x+len*cos(dir+angleoffset), y+len*sin(dir+angleoffset));
				AlertZone02(2, spawnpoint[0], spawnpoint[1], 105);
				ThunderBullets(spawnpoint[0], spawnpoint[1], dir+180);
			}
		}
		while(!Obj_IsDeleted(obj)){
			if(time%7==0 && time<105){ switch*=-1; }
			ObjRender_SetAlpha(obj, 255*switch);
			time++;
			yield;
		}
		
	}
	
	task ThunderBullets(x, y,dir){
		let dir2 = dir;
		if(x>384/2){ dir = 180; }
		loop(105){ 
		if(Obj_IsDeleted(objEnemy)){ return; } 
		yield; }
		ascent(i in 0..4){
			loop(10){
				CreateShotA2(x, y, 3, dir2+(i*360/10/2), -0.05, 0.75+(i/3), ARROWHEAD[YELLOW], 10);
				dir2+=360/10;
			}
		}
	}
		
		