#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Toyasatomimi no Miko (Spell 4)"]
#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 phase1 = true;
let phase2 = false;
let phase3 = false;
let phase4 = false;

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(10000/4);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(120);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(999000000/1);
	}
}

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


	TFinalizeFinal;
	StartSlow(TARGET_ALL, 60);
	BossRender;
	BossMarker;
	MagicCircle(objBoss);
	Name("Toyasatomimi no Miko");
	
	Pattern1;
	Pattern2;
	Pattern3;
	Pattern4;
	Pattern5;
	SetPhases;
	PhaseExplosion;
	//Movement;

	DamageRate;
}

@MainLoop{
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) != 0){ ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 24); }
	else{ ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0); }

	SetShotAutoDeleteClip(256, 256, 256, 256);

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

	frame++;

	yield;
}

task SetPhases{
	loop{
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 7500/4){phase2 = true; phase1 = false;}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 5000/4){phase3 = true;}
		if(ObjEnemy_GetInfo(objBoss, INFO_LIFE) < 2500/4){phase4 = true;}
		if(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) < 20){phase2 = true; phase3 = true; phase4 = true; phase1 = true;}
		yield;
	}
}

task PhaseExplosion{
	loop{
		while(!phase2){yield;}
			DeleteShotAll(TYPE_ALL, TYPE_ITEM);
			PlaySE(PhaseSE1);
			start = false;
			FireShot1;
			wait(120);
			start = true;
		while(!phase3){yield;}
			DeleteShotAll(TYPE_ALL, TYPE_ITEM);
			PlaySE(PhaseSE1);
			start = false;
			FireShot1;
			wait(120);
			start = true;
		break;
	}
}

task Pattern1{
	loop{
		if(start){
			//nTestCharge(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),90,175,0,165);
			wait(90);
			//FireShot1;
			wait(60*10);
		}
		yield;
	}
}

task Pattern2{
	loop{
		if(start){
			FireShot2;
			wait(45*2);
		}
		yield;
	}
}

task Pattern3{
	loop{
		if(start && phase3){
			FireShot3;
			wait(5);
		}
		yield;
	}
}

task Pattern4{
	loop{
		if(start && phase2){
			loop(7){
				FireShot4;
				wait(1);
			}
			wait(30);
		}
		yield;
	}
}

task Pattern5{
	loop{
		if(start && phase4){
			FireShot5;
			wait(2);
		}
		yield;
	}
}

task Start{
	if(frame == 60){
		nTestCharge(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),90,255,200,0);
		PlaySE(ChargeSE);
		Attack = true;
		wait(100);
		AuraFull;
		FireShot1;
		wait(60);
		Spell("Law \"The Seventeen Articles\"");
		ObjEnemyBossScene_StartSpell(objScene);
		Cutin;
		wait(90);
		EShockwave(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss), 0.09);
		PlaySE(ExpSE2);
		start=true;
		//Attack=false;
		//MagicCircle2;
	}
}	

task FireShot1{
	let r = 0;
	let angle = 10;
	loop(60*2/2){
		r = r+rand(-0.5,0.5);
		ascent(i in 0.5..9){
			CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, angle+i*20+r, 0.3, 1000, MIKO_ORB_ORANGE, 15);
			CreateShotA2(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, angle-i*20+r, 0.3, 1000, MIKO_ORB_ORANGE, 15);
		}
		PlaySE(ShotSE2);
		wait(2);
	}
}

task FireShot2{
	let angleT = GetAngleToPlayer(objBoss);
	let randx1 = ObjMove_GetX(objBoss)+rand(75,150);
	let randx2 = ObjMove_GetX(objBoss)+rand(-75,-150);
	let randy = ObjMove_GetY(objBoss)+rand(-20,20);
	let t = 0;
	if(phase1){t = 3;}
	else{t = 2;}
	loop(12){
		if(start){
			ascent(i in 0..4){
				let shot1 = CreateShotA2(randx1, randy, 8, angleT+i*t, -0.1, 4, RICE_M_BLUE, 15+i*5);
				let shot2 = CreateShotA2(randx1, randy, 8, angleT-i*t, -0.1, 4, RICE_M_BLUE, 15+i*5);
				//ObjMove_AddPatternA2(shot1, 30, 0, NO_CHANGE, 0.1, 0, 8);
				//ObjMove_AddPatternA2(shot2, 30, 0, NO_CHANGE, 0.1, 0, 8);
			}
		}
		angleT += 360/12;
	}
	PlaySE(ShotSE1);
	wait(45);
	PlaySE(ShotSE1);
	loop(12){
		if(start){
			ascent(i in 0..4){
			let shot1 = CreateShotA2(randx2, randy, 8, angleT+i*t, -0.1, 4, RICE_M_BLUE, 15+i*5);
			let shot2 = CreateShotA2(randx2, randy, 8, angleT-i*t, -0.1, 4, RICE_M_BLUE, 15+i*5);
			//ObjMove_AddPatternA2(shot1, 30, 0, NO_CHANGE, 0.1, 0, 8);
			//ObjMove_AddPatternA2(shot2, 30, 0, NO_CHANGE, 0.1, 0, 8);
			}
		}
		angleT += 360/12;
	}
}

task FireShot3{
	let angleT = angleInc*0.95;
	let randx = ObjMove_GetX(objBoss)+rand(-150,150);
	let randy = ObjMove_GetY(objBoss)+rand(-20,20);
	PlaySE(ShotSE1);
	loop(4){
		ascent(i in 1..2){
			let shot1 = CreateShotA2(ObjMove_GetX(objBoss)+100, 30, 8+i/2, angleT, -0.1, 3+i/2, BILL_RED, 15);
			let shot2 = CreateShotA2(ObjMove_GetX(objBoss)-100, 30, 8+i/2, -angleT, -0.1, 3+i/2, BILL_GRAY, 15);
		}
		angleT += 360/32;
	}
}

task FireShot4{
	let angleT = angleInc*1.72;
	loop(9){
		let shot1 = CreateShotA2(ObjMove_GetX(objBoss), 20, 8, angleT, -0.1, 3, SWORD_PURPLE, 15);
		//ObjRender_SetBlendType(shot1, BLEND_ADD_ARGB);
		ObjMove_AddPatternA2(shot1, 90, 3, NO_CHANGE, 0.1, 0, 8);
		PlaySE(ShotSE1);
		angleT += 360/9;
	}
}

task FireShot5{
	let angleT = 30;
	let wave = 5*sin(angleInc*2);
	loop(6){
		CreateShotA1(ObjMove_GetX(objBoss)-100, ObjMove_GetY(objBoss)+20, 8, angleT+wave, KUNAI_M_RED, 15);
		CreateShotA1(ObjMove_GetX(objBoss)+100, ObjMove_GetY(objBoss)+20, 8, angleT-wave, KUNAI_M_BLUE, 15);
		PlaySE(ShotSE1);
		angleT += 360/6;
	}
}

task DamageRate{
	loop{
		if(start){
			if(IsPermitPlayerSpell == false){ ObjEnemy_SetDamageRate(objBoss, 1, 1); ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0); } // This is here because Byakuren player's bomb is actually a regular player shot.
			else{ ObjEnemy_SetDamageRate(objBoss, 25, 1); ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 32); }
		}
		yield;
	}
}