#TouhouDanmakufu
#Title["Sanctuary Field" -Lunatic-]
#Text[Nihili arcanum.]
#Image[snapshot\sanctuaryfieldlunatic.bmp]
#Player[player\ProcellaPlayers\FeliciaA\FeliciaA.dnh, player\ProcellaPlayers\FeliciaB\FeliciaB.dnh]
#PlayLevel[Lunatic]
#BGM[bgm\Night Falls ~ Evening Star.mp3]
#ScriptVersion[2]

script_enemy_main {
	#include_function "lib\SHOT_REPLACE\shot_replace.dnh"
	let ImgBoss = "script\img\stg8enm.png";
	let frame = -120;
	let trig = 0;
	let turndir = 1;
	let movecounter = 0;
	let oa = 0;
	let firedcounter = 0;
	let r = 0;
	let rm = 0.75;
	let dest = 120;
	let tm = 1.82;
	let tmd = tm;
	@Initialize {
		SetLife(800);
		SetDamageRate(20, 5);
		SetTimer(45);
		SetInvincibility(180);
		SetScore(444444);
		CutIn(KOUMA, ""\""Sanctuary Field"\", "script\img\emilycutin.png", 0, 0, 0, 0);

		LoadGraphic(ImgBoss);
		SetTexture(ImgBoss);
		SetGraphicRect(6,195,70,285);
		psrand(747030);
		shotinit;
		SanctuaryField;

		SetMovePosition02(GetCenterX, GetClipMinY + 180, 120);
	}
	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);
		SetShotAutoDeleteClip(16, 16, 16, 16);
		if(r < dest) {
			r+=rm;
		}
		else if(r > dest) {
			r -= rm;
		}
		if(tm > tmd) {
			tm -= 0.05;
		}
		else if(tm < tmd) {
			tm += 0.05;
		}
		if(frame%45 == 1) {
			movecounter++;
		}
		if(frame%180 == 179) {
			if(dest == 120) {
				dest = 60;
			}
			else {
				dest = 120;
			}
			tmd = tmd * -1;
		}
		yield;
		frame++;
	}
	task SanctuaryField {
		yield;
		loop {
			ascent(a in 0..30) {
				let bulletx = GetX + cos(trig + a * 12) * r;
				let bullety = GetY + sin(trig + a * 12) * r;
				if(frame%45 == 1) {
					CreateShot01(bulletx, bullety, 0.5, atan2(GetY - bullety, GetX - bulletx), WHITE12, 0);
					CreateShot01(bulletx, bullety, 1, atan2(GetY - bullety, GetX - bulletx), PURPLE12, 0);
					CreateShot01(bulletx, bullety, 1.5, atan2(GetY - bullety, GetX - bulletx), BLUE12, 0);
					CreateShot01(bulletx, bullety, 2, atan2(GetY - bullety, GetX - bulletx), AQUA12, 0);
					CreateShot01(bulletx, bullety, 0.5, atan2(GetY - bullety, GetX - bulletx) + (movecounter%2 * 2 - 1) * 18.2 + 180, WHITE12, 0);
					CreateShot01(bulletx, bullety, 1, atan2(GetY - bullety, GetX - bulletx) + (movecounter%2 * 2 - 1) * 18.2 + 180, PURPLE12, 0);
					CreateShot01(bulletx, bullety, 1.5, atan2(GetY - bullety, GetX - bulletx) + (movecounter%2 * 2 - 1) * 18.2 + 180, BLUE12, 0);
					CreateShot01(bulletx, bullety, 2, atan2(GetY - bullety, GetX - bulletx) + (movecounter%2 * 2 - 1) * 18.2 + 180, AQUA12, 0);
				}
				CreateShotA(0, bulletx, bullety, 0);
				SetShotDataA(0, 0, 0, 0, 0, 0, 0, AQUA02);
				SetShotKillTime(0, 1);
				FireShot(0);
			}
			ascent(a in 0..30) {
				let bulletx = GetX + cos(trig * -2 + a * 12) * r / 2;
				let bullety = GetY + sin(trig * -2 + a * 12) * r / 2;
				if(frame%45 == 1) {
					CreateShot01(bulletx, bullety, 0.5, atan2(GetY - bullety, GetX - bulletx), WHITE12, 0);
					CreateShot01(bulletx, bullety, 1, atan2(GetY - bullety, GetX - bulletx), PURPLE12, 0);
					CreateShot01(bulletx, bullety, 1.5, atan2(GetY - bullety, GetX - bulletx), BLUE12, 0);
					CreateShot01(bulletx, bullety, 2, atan2(GetY - bullety, GetX - bulletx), AQUA12, 0);
					CreateShot01(bulletx, bullety, 0.5, atan2(GetY - bullety, GetX - bulletx) + (movecounter%2 * 2 - 1) * 18.2 + 180, WHITE12, 0);
					CreateShot01(bulletx, bullety, 1, atan2(GetY - bullety, GetX - bulletx) + (movecounter%2 * 2 - 1) * 18.2 + 180, PURPLE12, 0);
					CreateShot01(bulletx, bullety, 1.5, atan2(GetY - bullety, GetX - bulletx) + (movecounter%2 * 2 - 1) * 18.2 + 180, BLUE12, 0);
					CreateShot01(bulletx, bullety, 2, atan2(GetY - bullety, GetX - bulletx) + (movecounter%2 * 2 - 1) * 18.2 + 180, AQUA12, 0);
				}
				CreateShotA(0, bulletx, bullety, 0);
				SetShotDataA(0, 0, 0, 0, 0, 0, 0, BLUE02);
				SetShotKillTime(0, 1);
				FireShot(0);
			}
			trig+=tm;
			yield;
		}
	}
	function LemmingDelay(x, y) {
		CreateShotA(0, x, y, 10);
		SetShotDataA(0, 0, 0, 0, 0, 0, 0, BLUE57);
		SetShotKillTime(0, 0);	
		FireShot(0);
	}
	@DrawLoop {
		SetTexture(ImgBoss);
		DrawGraphic(GetX, GetY);
	}
	@Finalize {
		DeleteGraphic(ImgBoss);
	}
}