/*#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Toyasatomimi no Miko (Spell 2)"]
#Text["knock knock [r] get the door [r] it's religion"]
#System["./system/system/Awalys_System.txt"]
#Background["./system/background/back_miko.dnh"]
#Player["./system/player/Byakuren/Byakuren_A.txt"]*/

#include"./system/shot/CrispBright4/Shotconstants.dnh"
#include"./system/lib/lib_main.dnh"
#include"./system/lib/miko_aura.dnh"

let frame = 0;
let diff = 3;
let time = 0;
let timer = 0;
let move = 0;
let angleInc = 0;
let inc = 0;
let count = 0;
let count2 = 0;
let count3 = 0;

let angleT = 45;

let objFuto;
let objTojiko;

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(5500/4);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(47);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(999000000/3);
	}
}

@Initialize{
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
    ObjMove_SetDestAtWeight(objBoss, cx, 120, 15,6);

	TFinalizeSpell;
	StartSlow(TARGET_ALL, 60);
	BossRender;
	BossMarker;
	MagicCircle(objBoss);
	Name("Toyasatomimi no Miko");
	Pattern1;
	//Movement;
}

@MainLoop{
	let ex = ObjMove_GetX(objBoss);
	let ey = ObjMove_GetY(objBoss);

	ObjEnemy_SetIntersectionCircleToShot(objBoss, ex, ey, 32); 
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ex, ey, 24);

	SetShotAutoDeleteClip(128, 128, 128, 128);

	ObjEnemy_SetDamageRate(objBoss,1,1);
	if(start){
		ObjEnemy_SetDamageRate(objBoss,25,25);
		angleInc+=1;
	}
	
	Start;

	frame++;

	yield;
}

task Pattern1{
	loop{
		if(start){
			loop(5){
				FireFuto(rand_int(1,5));
				wait(rand_int(20,40));
				FireTojiko(rand_int(1,5));
				wait(rand_int(80,90));
			}
			nTestCharge(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),90,255,200,0);
			PlaySE(ChargeSE);
			wait(60);
			Attack=true;
			FireMiko;
			wait(240);
			Attack=false;
			//break;
		}
		yield;
	}
}

task Start{
	if(frame == 60){
		Spell("Summon \"Royal Clan's Harmonic Shot\"");
		ObjEnemyBossScene_StartSpell(objScene);
		Cutin;
		wait(60);
		//Aura;
		nTestCharge(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),90,255,200,0);
		objFuto = CreateFamiliarA1(objBoss, 60, 120, 0);
    	objTojiko = CreateFamiliarA1(objBoss, 380, 120, 0);
    	TojikoRender(objTojiko);
    	FutoRender(objFuto);
		PlaySE(ChargeSE);
		Attack = true;
		wait(100);
		EShockwave(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 0.09);
		PlaySE(ExpSE2);
		start=true;
		Attack=false;
		//MagicCircle2;
	}
}

task FireFuto1{
	loop(64){
		while(Obj_IsDeleted(objBoss)){yield;}
		let shot1 = CreateShotA2(ObjMove_GetX(objFuto), ObjMove_GetY(objFuto), 8, rand(0,360), -0.5, 0, ARROW_BLUE, 15);
		let shot2 = CreateShotA2(ObjMove_GetX(objFuto), ObjMove_GetY(objFuto), 8, rand(0,360), -0.5, 0, ARROW_BLUE, 15);
		AimToPlayer(shot1, 20); AimToPlayer(shot2, -20);
		PlaySE(ShotSE1);
		yield;
	}
}
task AimToPlayer(shot, type){wait(29); ObjMove_AddPatternA2(shot, 1, 0, GetAngleToPlayer(shot)+type, 0.2, 0, 8);}
task FireTojiko1{
	loop(64){
		while(Obj_IsDeleted(objBoss)){yield;}
		let shot1 = CreateShotA2(ObjMove_GetX(objTojiko), ObjMove_GetY(objTojiko), 8, rand(0,360), -0.5, 0, ARROW_ORANGE, 15);
		AimToPlayer(shot1, rand(-5,5));
		PlaySE(ShotSE1);
		yield;
	}
}
task FireTojiko2{
	let angleT = rand(0,360);
	loop(16){
		ascent(i in 1..10){
			let shot = CreateShotA1(ObjMove_GetX(objTojiko), ObjMove_GetY(objTojiko), 3+i/10, angleT, ICE_ORANGE, 15);
			ObjRender_SetBlendType(shot, BLEND_ADD_ARGB);
			ReflectSides(shot, 2);
		}
		angleT += 360/16;
	}
	PlaySE(BurstSE);
}
task FireTojiko3{
	let angleT = rand(0,360);
	loop(6){
		ascent(i in 1..10){
			let shot = CreateShotA1(ObjMove_GetX(objTojiko), ObjMove_GetY(objTojiko), 1, angleT+i*3, BALL_M_RED, 15);
		}
		angleT += 360/6;
	}
	PlaySE(ShotSE2);
}
task FireFuto4{
	let angleT = rand(0,360);
	loop(42){
		CreateShotA1(ObjMove_GetX(objFuto), ObjMove_GetY(objFuto), 1, angleT, BALL_M_PURPLE, 15);
		CreateShotA1(ObjMove_GetX(objFuto), ObjMove_GetY(objFuto), 1.1, angleT+5, BALL_M_PURPLE, 15);
		angleT += 360/42;
	}
	PlaySE(ShotSE2);
}
task FireTojiko4{
	let angleT = 90+rand(-40,40);
	loop(3){
		let shot = CreateCurveLaserA1(ObjMove_GetX(objTojiko), ObjMove_GetY(objTojiko), 6, angleT, 30, 25, LIGHTNING_YELLOW, 0);
		ObjMove_SetAngularVelocity(shot, 1.25);
		ObjCrLaser_SetTipDecrement(shot, 0.25);
		BulletCommandExplosionsSide(shot);
		angleT = 90+rand(-40,40);
		PlaySE(LaserSE1);
	}
}
task FireFuto5{
	let angleT = 285;
	loop(12){
		while(Obj_IsDeleted(objBoss)){yield;}
		let shot = CreateGravityShotA1(ObjMove_GetX(objFuto), ObjMove_GetY(objFuto), 5, 0.02, angleT, BALL_M_BLUE, 0);
		BulletCommandExplosionsSide(shot);
		angleT += 10;
		PlaySE(ShotSE2);
		wait(5);
	}
}
task FireTojiko5{
	FireTojiko55(190); FireTojiko55(230); FireTojiko55(260); FireTojiko55(290); FireTojiko55(320); FireTojiko55(350);
}
task FireTojiko55(angle){
	let shot = CreateShotA2(ObjMove_GetX(objTojiko), ObjMove_GetY(objTojiko), 5.5, angle, -0.15, 0, VORTEX_ORANGE, 15);
	wait(rand_int(60,120));
	let angleR = 90+rand(-30,30);
	while(Obj_IsDeleted(shot)){yield;}
	DelayLaser(ObjMove_GetX(shot), ObjMove_GetY(shot), angleR, 1000, 100, 1, LASER_ORANGE, 100);
	PlaySE(LaserSE2);
	wait(60);
	while(Obj_IsDeleted(shot)){yield;}
	CreateLooseLaserA1(ObjMove_GetX(shot), ObjMove_GetY(shot), 50, angleR, 1000, 40, LASER_ORANGE, 20);
	wait(20);
	PlaySE(ShotSE3);
	Obj_Delete(shot);
}
task BulletCommandExplosionsSide(shot){
	while(ObjMove_GetX(shot) > 2 && ObjMove_GetX(shot) < GetStgFrameWidth-2){yield;}
	let rx = ObjMove_GetX(shot);
	let ry = ObjMove_GetY(shot);
	let angleR = rand(0,360);
	if(rx <= 2&&!Obj_IsDeleted(shot)){
	PlaySE(ExpSE3);
	loop(32){
		CreateShotA2(rx, ry, rand(0.1,0.5), angleR, 0.01,  rand(3,4), BALL_S_YELLOW, 5);
		ObjShot_SetDeleteFrame(shot, 60);
		angleR += 360/32;}
	}
	if(rx >= GetStgFrameWidth-2&&!Obj_IsDeleted(shot)){
	PlaySE(ExpSE3);
	loop(12){
		CreateShotA2(rx, ry, rand(0.1,0.5), angleR, 0.01, rand(5,6), BALL_BS_BLUE, 5);
		ObjShot_SetDeleteFrame(shot, 60);
		angleR += 360/12;}
	}
}
task FireFuto2{
	let x1 = 0;
	let x2 = 0;
	loop(30){
		while(Obj_IsDeleted(objBoss)){yield;}
		let shot1 = CreateShotA1(ObjMove_GetX(objFuto)+x1, ObjMove_GetY(objFuto)+rand(-50,40), 0.25, rand(75,105), FIREBALL_RED, 20);
		let shot2 = CreateShotA1(ObjMove_GetX(objFuto)+x2, ObjMove_GetY(objFuto)+rand(-50,40), 0.25, rand(75,105), FIREBALL_RED, 20);
		ObjMove_AddPatternA2(shot1, 90, 0, GetAngleToPlayer(shot1)+rand(-2,2), 0.1, 0, 10);
		ObjMove_AddPatternA2(shot2, 90, 0, GetAngleToPlayer(shot2)+rand(-2,2), 0.1, 0, 10);
		x1 += 10;
		x2 -= 10;
		PlaySE(PhaseSE1);
		yield;
	}
	PlaySE(ShotSE2);
}
task FireFuto3{
	let angleT = GetAngleToPlayer(objFuto);
	loop(9){
		ascent(i in 0..7){
			CreateShotA2(ObjMove_GetX(objFuto), ObjMove_GetY(objFuto), 4, angleT+i, -0.1, 4, ARROW_BLUE, 15+i*5);
			CreateShotA2(ObjMove_GetX(objFuto), ObjMove_GetY(objFuto), 4, angleT-i, -0.1, 4, ARROW_BLUE, 15+i*5);

			CreateShotA2(ObjMove_GetX(objFuto), ObjMove_GetY(objFuto), 2, angleT+i, -0.1, 1, ARROW_BLUE, 15+i*5);
			CreateShotA2(ObjMove_GetX(objFuto), ObjMove_GetY(objFuto), 2, angleT-i, -0.1, 1, ARROW_BLUE, 15+i*5);
		}
		angleT += 360/9;
	}
	PlaySE(ShotSE3);
}

task FireFuto(p){
	if(p == 1){nTestCharge(ObjMove_GetX(objFuto),ObjMove_GetY(objFuto),30,0,0,255); PlaySE(ChargeSE); Movement(objFuto);	FireFuto1;}
	else if(p == 2){nTestCharge(ObjMove_GetX(objFuto),ObjMove_GetY(objFuto),30,255,0,150); PlaySE(ChargeSE); Movement(objFuto);	FireFuto2;}
	else if(p == 3){nTestCharge(ObjMove_GetX(objFuto),ObjMove_GetY(objFuto),30,0,0,175); PlaySE(ChargeSE); Movement(objFuto);	FireFuto3;}
	else if(p == 4){nTestCharge(ObjMove_GetX(objFuto),ObjMove_GetY(objFuto),30,175,0,255); PlaySE(ChargeSE); Movement(objFuto);	FireFuto4;}
	else if(p == 5){nTestCharge(ObjMove_GetX(objFuto),ObjMove_GetY(objFuto),30,200,200,255); PlaySE(ChargeSE); Movement(objFuto);	FireFuto5;}
}

task FireTojiko(p){
	if(p == 1){nTestCharge(ObjMove_GetX(objTojiko),ObjMove_GetY(objTojiko),30,255,0,0); PlaySE(ChargeSE); Movement(objTojiko);	FireTojiko1;}
	else if(p == 2){nTestCharge(ObjMove_GetX(objTojiko),ObjMove_GetY(objTojiko),30,255,0,150); PlaySE(ChargeSE); Movement(objTojiko);	FireTojiko2;}
	else if(p == 3){nTestCharge(ObjMove_GetX(objTojiko),ObjMove_GetY(objTojiko),30,255,0,0); PlaySE(ChargeSE); Movement(objTojiko);	FireTojiko3;}
	else if(p == 4){nTestCharge(ObjMove_GetX(objTojiko),ObjMove_GetY(objTojiko),30,255,255,255); PlaySE(ChargeSE); Movement(objTojiko);	FireTojiko4;}
	else if(p == 5){nTestCharge(ObjMove_GetX(objTojiko),ObjMove_GetY(objTojiko),30,255,255,0); PlaySE(ChargeSE); Movement(objTojiko);	FireTojiko5;}
}

task FireMiko{
	let angleT = GetAngleToPlayer(objBoss);
	ascent(i in 0..8){
		CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), angleT+i*2, 500, 20, 60, LASER_ORANGE, 90);
		CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), angleT-i*2, 500, 20, 60, LASER_ORANGE, 90);
		PlaySE(LaserSE1);
		yield;
	}
	ascent(i in 0..24/2){
		CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), angleT+180-i*7*2, 500, 15, 60, LASER_ORANGE, 80);
		CreateStraightLaserA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), angleT-180+i*7*2, 500, 15, 60, LASER_ORANGE, 80);
		PlaySE(LaserSE2);
		yield;
	}
}

task Movement(obj){
	if(ObjMove_GetX(obj) < 50){
		ObjMove_SetDestAtWeight(obj, ObjMove_GetX(obj)+50, ObjMove_GetY(obj)+rand(-10,5), 15,6);
	}
	else if(ObjMove_GetX(obj) > GetStgFrameWidth-50){
		ObjMove_SetDestAtWeight(obj, ObjMove_GetX(obj)-50, ObjMove_GetY(obj)+rand(-10,5), 15,6);
	}
	else{
		ObjMove_SetDestAtWeight(obj, ObjMove_GetX(obj)+rand(-50,50), ObjMove_GetY(obj)+rand(-10,5), 15,6);
	}
}