#TouhouDanmakufu
#Title["Formless and Void"]
#Text[Emiliae Nihilique arcanum.]
#Image[snapshot\formlessandvoid.bmp]
#Player[player\ProcellaPlayers\EmilyA\EmilyA.dnh, player\ProcellaPlayers\EmilyB\EmilyB.dnh, player\ProcellaPlayers\FeliciaA\FeliciaA.dnh, player\ProcellaPlayers\FeliciaB\FeliciaB.dnh]
#PlayLevel[Last Word]
#BGM[bgm\Nantettatte Densetsu.mp3]
#ScriptVersion[2]

script_enemy_main {
	#include_function "lib\SHOT_REPLACE\shot_replace.dnh"
	let ImgBoss = "script\img\stg8enm.png";
	let SeShot = "script\se\tan00.wav";
	let SeFamiliar = "script\se\Shadow.wav";
	let frame = -120;
	let trig = 0;
	let turndir = 1;
	let movecounter = 0;
	let oa = 0;
	let firedcounter = 0;
	@Initialize {
		SetLife(666);
		SetDamageRate(0, 0);
		SetTimer(45);
		SetDurableSpellCard;
		SetScore(1111111);
		LastSpell;
		Expert;
		CutIn(KOUMA, ""\""Formless and Void"\", "script\img\emilycutin.png", 0, 0, 0, 0);

		LoadGraphic(ImgBoss);
		SetTexture(ImgBoss);
		SetGraphicRect(6,195,70,285);
		LoadSE(SeShot);
		LoadSE(SeFamiliar);
		psrand(747030);
		shotinit;
		NegationField;

		SetMovePosition02(GetCenterX, GetClipMinY + 180, 120);
	}
	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);
		if(frame%480 == 1 && firedcounter < 3) {
			ReHomerSpiral;
			firedcounter++;
		}
		if(frame%480 == 1) {
			if(movecounter%4 == 0) {
				SetMovePosition03(GetCenterX - 60, GetClipMinY + 100, 9, 3);
			}
			else if(movecounter%4 == 2) {
				SetMovePosition03(GetCenterX + 60, GetClipMinY + 100, 9, 3);
			}
			else {
				SetMovePosition03(GetCenterX, GetClipMinY + 180, 9, 3);
			}
			movecounter++;
		}
		yield;
		frame++;
	}
	task NegationField {
		yield;
		loop {
			if(frame%60 == 1) {
				PlaySE(SeShot);
			}
			ascent(a in 0..8) {
				let bulletx = GetPlayerX + cos(trig + a * 45) * 90;
				let bullety = GetPlayerY + sin(trig + a * 45) * 90;
				if(frame%60 == 1) {
					CreateShot01(bulletx, bullety, 0.5, atan2(GetPlayerY - bullety, GetPlayerX - bulletx), WHITE05, 10);
					CreateShot01(bulletx, bullety, 0.625, atan2(GetPlayerY - bullety, GetPlayerX - bulletx), WHITE05, 10);
					CreateShot01(bulletx, bullety, 0.75, atan2(GetPlayerY - bullety, GetPlayerX - bulletx), WHITE05, 10);
				}
				CreateShotA(0, bulletx, bullety, 0);
				SetShotDataA(0, 0, 0, 0, 0, 0, 0, WHITE03);
				SetShotKillTime(0, 1);
				FireShot(0);
			}
			trig ++;
			yield;
		}
	}
	function LemmingDelay(x, y) {
		CreateShotA(0, x, y, 30);
		SetShotDataA(0, 0, 0, 0, 0, 0, 0, WHITE57);
		SetShotKillTime(0, 0);	
		FireShot(0);
	}
	function ReshotRing(x, y) {
		ascent(a in 0..8) {
			CreateShot01(x, y, 0.5, a * 45, BLUE12, 0);
		}
	}
	task ReShooterSpiral {
		ascent(a in 0..12) {
			ReShooter(GetX, GetY, 3, a * 30, 0.05, WHITE03, WHITE01, 0);
		}
	}
	task ReHomerSpiral {
		if(turndir == 1) {
			ascent(a in 0..15) {
				PlaySE(SeFamiliar);
				ReHomer(GetX, GetY, 3, a * 24, 0.05, WHITE03, 0);
				loop(10) {
					yield;
				}
			}
		}
		else {
			descent(a in 0..15) {
				PlaySE(SeFamiliar);
				ReHomer(GetX, GetY, 3, a * 24, 0.05, WHITE03, 0);
				loop(10) {
					yield;
				}
			}
		}
	}
	task ReShooter(x, y, v, angle, accel, graphic1, graphic2, delay) {
		let obj = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, v);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, graphic1);
		ObjShot_SetDelay(obj, delay);
		while(Obj_GetSpeed(obj) > 0) {
			LemmingDelay(Obj_GetX(obj), Obj_GetY(obj));
			Obj_SetSpeed(obj, Obj_GetSpeed(obj) - accel);
			yield;
		}
		while(delay > 0) {
			LemmingDelay(Obj_GetX(obj), Obj_GetY(obj));
			Obj_SetSpeed(obj, 0);
			delay--;
			yield;
		}
		while(Obj_GetSpeed(obj) < v) {
			LemmingDelay(Obj_GetX(obj), Obj_GetY(obj));
			if(frame%17 == 1) {
				ReshotRing(Obj_GetX(obj), Obj_GetY(obj));
			}
			Obj_SetSpeed(obj, Obj_GetSpeed(obj) + accel);
			Obj_SetAngle(obj, Obj_GetAngle(obj) + turndir);
			yield;
		}
		while(Obj_BeDeleted(obj) == false) {
			LemmingDelay(Obj_GetX(obj), Obj_GetY(obj));
			if(frame%17 == 1) {
				ReshotRing(Obj_GetX(obj), Obj_GetY(obj));
			}
			yield;
		}
	}
	task ReHomer(x, y, v, angle, accel, graphic, delay) {
		let obj = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, v);
		Obj_SetAngle(obj, angle);
		Obj_SetAutoDelete(obj, false);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		while(Obj_BeDeleted(obj) == false) {
			while(Obj_GetSpeed(obj) > 0) {
				if(OnPlayerMissed) {
					ObjShot_FadeDelete(obj);
				}
				LemmingDelay(Obj_GetX(obj), Obj_GetY(obj));
				Obj_SetSpeed(obj, Obj_GetSpeed(obj) - accel);
				yield;
			}
			Obj_SetAngle(obj, atan2(GetPlayerY - Obj_GetY(obj), GetPlayerX - Obj_GetX(obj)));
			while(Obj_GetSpeed(obj) < v * 2) {
				if(OnPlayerMissed) {
					ObjShot_FadeDelete(obj);
				}
				LemmingDelay(Obj_GetX(obj), Obj_GetY(obj));
				Obj_SetSpeed(obj, Obj_GetSpeed(obj) + accel * 2);
				yield;
			}
		}
	}
	@DrawLoop {
		SetTexture(ImgBoss);
		DrawGraphic(GetX, GetY);
	}
	@Finalize {
		DeleteGraphic(ImgBoss);
	}
}