#TouhouDanmakufu
#Title["Border of Abstinence and Addiction -Lost Will-" -Lunatic-]
#Text[Invitae arcanum.]
#Image[snapshot\aaborderlostwilllunatic.bmp]
#Player[player\ProcellaPlayers\EmilyA\EmilyA.dnh, player\ProcellaPlayers\EmilyB\EmilyB.dnh, 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 outAngle = 90;
	let inAngle = 0;
	let ex1 = 0;
	let ey1 = 0;
	let ex2 = 0;
	let ey2 = 0;
	let ex3 = 0;
	let ey3 = 0;
	let ex4 = 0;
	let ey4 = 0;
	let ex5 = 0;
	let ey5 = 0;
	let ex6 = 0;
	let ey6 = 0;
	let ex7 = 0;
	let ey7 = 0;
	let ex8 = 0;
	let ey8 = 0;
	@Initialize {
		SetLife(800);
		SetInvincibility(180);
		SetDamageRate(20, 0);
		SetTimer(61);
		SetScore(555555);
		CutIn(KOUMA, ""\""Border of Abstinence and Addiction -Lost Will-"\", "script\img\desdemonaandinvita.png", 0, 0, 0, 0);

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

		SetMovePosition03(GetCenterX, GetCenterY, 9, 5);
	}
	function IsOffScreen {
		return GetX < GetClipMinX || GetX > GetClipMaxX || GetY < GetClipMinY || GetY > GetClipMaxY;
	}
	task Deler(x, y, v, angle, graphic, delay) {
		let obj = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj, x, y);
		Obj_SetAngle(obj, angle);
		Obj_SetSpeed(obj, v);
		Obj_SetCollisionToObject(obj, true);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		while(!Obj_BeDeleted(obj)) {
			if(Collision_Line_Circle(ex1, ey1, ex2, ey2, 15, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_Delete(obj);
			}
			else if(Collision_Line_Circle(ex3, ey3, ex4, ey4, 15, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_Delete(obj);
			}
			else if(Collision_Line_Circle(ex5, ey5, ex6, ey6, 15, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_Delete(obj);
			}
			else if(Collision_Line_Circle(ex7, ey7, ex8, ey8, 15, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_Delete(obj);
			}
			yield;
		}
	}
	function Flower(x, y, v1, accel, v2, num, sa, graphic) {
		loop(num) {
			CreateShot02(x, y, v1, sa, accel, v2, graphic, 0);
			CreateShot02(x, y, v1, 180 - sa, accel, v2, graphic, 0);
			sa += (360 / num);
		}
	}
	function DelerFlower(x, y, v, num, sa, graphic) {
		loop(num) {
			Deler(x, y, v, sa, graphic, 10);
			Deler(x, y, v, 180 - sa, graphic, 10);
			sa += (360 / num);
		}
	}
	@MainLoop {
		SetShotAutoDeleteClip(200, 200, 200, 200);
		if(!OnBomb) {
			SetCollisionA(GetX, GetY, 32);
			SetCollisionB(GetX, GetY, 16);
		}
		if(frame == 0) {
			ascent(a in 0..4) {
				AABorderPart(a);
				CreateLaserB(0, 250 / 2^0.5, 33, RED60, 60);
				SetLaserDataB(0, 0, 0, 125, 0, a * 90, -0.5, a * 90 - 135, -0.5);
				FireShot(0);
			}
		}
		if(frame%20 == 1) {
			DelerFlower(GetX, GetY, 1, 7, outAngle, RED31);
			outAngle += 5;
		}
		if(frame%240 == 61) {
			ascent(a in 0..36) {
				Aimer(GetX, GetY, 1.5, a * 10, WHITE57, PURPLE57, 0);
			}
		}
		if(frame%240 == 181) {
			ascent(a in 0..36) {
				Reverser(GetX, GetY, 1.5, a * 10 + 5, WHITE57, 0);
			}
		}
		trig += 0.5;
		yield;
		frame++;
	}
	task AABorderPart(param) {
		let obj = Obj_Create(OBJ_LASER);
		let l = 540 / 2^0.5;
		ObjShot_SetGraphic(obj, RED60);
		ObjShot_SetDelay(obj, 60);
		ObjLaser_SetWidth(obj, 33);
		Obj_SetCollisionToObject(obj, true);
		ObjLaser_SetLength(obj, l);
		while(Obj_BeDeleted(obj) == false) {
			Obj_SetPosition(obj, GetX + cos(trig + param * 90) * 270, GetY + sin(trig + param * 90) * 270);
			Obj_SetAngle(obj, atan2(GetY - Obj_GetY(obj), GetX - Obj_GetX(obj)) - 45);
			if(param == 0) {
				ex1 = Obj_GetX(obj);
				ey1 = Obj_GetY(obj);
				ex2 = ObjLaser_GetEndX(obj);
				ey2 = ObjLaser_GetEndY(obj);
			}
			else if(param == 1) {
				ex3 = Obj_GetX(obj);
				ey3 = Obj_GetY(obj);
				ex4 = ObjLaser_GetEndX(obj);
				ey4 = ObjLaser_GetEndY(obj);
			}
			else if(param == 2) {
				ex5 = Obj_GetX(obj);
				ey5 = Obj_GetY(obj);
				ex6 = ObjLaser_GetEndX(obj);
				ey6 = ObjLaser_GetEndY(obj);
			}
			else {
				ex7 = Obj_GetX(obj);
				ey7 = Obj_GetY(obj);
				ex8 = ObjLaser_GetEndX(obj);
				ey8 = ObjLaser_GetEndY(obj);
			}
			yield;
		}
	}
	task Reverser(x, y, v, angle, graphic, delay) {
		let obj = Obj_Create(OBJ_SHOT);
		let aim = false;
		Obj_SetPosition(obj, x, y);
		Obj_SetAngle(obj, angle);
		Obj_SetSpeed(obj, v);
		Obj_SetCollisionToObject(obj, true);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		while(!aim) {
			if(Collision_Line_Circle(ex1, ey1, ex2, ey2, 33, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_SetAngle(obj, Obj_GetAngle(obj) + 180);
				aim = true;
			}
			else if(Collision_Line_Circle(ex3, ey3, ex4, ey4, 33, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_SetAngle(obj, Obj_GetAngle(obj) + 180);
				aim = true;
			}
			else if(Collision_Line_Circle(ex5, ey5, ex6, ey6, 33, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_SetAngle(obj, Obj_GetAngle(obj) + 180);
				aim = true;
			}
			else if(Collision_Line_Circle(ex7, ey7, ex8, ey8, 33, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_SetAngle(obj, Obj_GetAngle(obj) + 180);
				aim = true;
			}
			if(aim == true) {
				Obj_SetSpeed(obj, v / 3);
			}
			yield;
		}
		loop(5) { yield; }
		while(Obj_BeDeleted(obj) == false) {
			if(Collision_Line_Circle(ex1, ey1, ex2, ey2, 15, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_Delete(obj);
			}
			else if(Collision_Line_Circle(ex3, ey3, ex4, ey4, 15, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_Delete(obj);
			}
			else if(Collision_Line_Circle(ex5, ey5, ex6, ey6, 15, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_Delete(obj);
			}
			else if(Collision_Line_Circle(ex7, ey7, ex8, ey8, 15, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_Delete(obj);
			}
			yield;
		}
	}
	task Aimer(x, y, v, angle, graphic, agraphic, delay) {
		let obj = Obj_Create(OBJ_SHOT);
		let aim = false;
		Obj_SetPosition(obj, x, y);
		Obj_SetAngle(obj, angle);
		Obj_SetSpeed(obj, v);
		Obj_SetCollisionToObject(obj, true);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		while(!aim) {
			if(Collision_Line_Circle(ex1, ey1, ex2, ey2, 33, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_SetAngle(obj, atan2(GetPlayerY - Obj_GetY(obj), GetPlayerX - Obj_GetX(obj)));
				ObjShot_SetGraphic(obj, agraphic);
				aim = true;
			}
			else if(Collision_Line_Circle(ex3, ey3, ex4, ey4, 33, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_SetAngle(obj, atan2(GetPlayerY - Obj_GetY(obj), GetPlayerX - Obj_GetX(obj)));
				ObjShot_SetGraphic(obj, agraphic);
				aim = true;
			}
			else if(Collision_Line_Circle(ex5, ey5, ex6, ey6, 33, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_SetAngle(obj, atan2(GetPlayerY - Obj_GetY(obj), GetPlayerX - Obj_GetX(obj)));
				ObjShot_SetGraphic(obj, agraphic);
				aim = true;
			}
			else if(Collision_Line_Circle(ex7, ey7, ex8, ey8, 33, Obj_GetX(obj), Obj_GetY(obj), 1)) {
				Obj_SetAngle(obj, atan2(GetPlayerY - Obj_GetY(obj), GetPlayerX - Obj_GetX(obj)));
				ObjShot_SetGraphic(obj, agraphic);
				aim = true;
			}
			if(aim == true) {
				Obj_SetSpeed(obj, v / 3);
			}
			yield;
		}
		loop(480) { yield; }
		ObjShot_FadeDelete(obj);
	}
	@DrawLoop {
		SetTexture(ImgBoss);
		if(OnBomb) {
			SetAlpha(127);
		}
		else {
			SetAlpha(255);
		}
		DrawGraphic(GetX, GetY);
	}
	@Finalize {
		DeleteGraphic(ImgBoss);
	}
	task CreateShot03(x, y, v, angle, graphic, delay) {
		let obj = Obj_Create(OBJ_SHOT);
		let lifecounter = 10;
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, v);
		Obj_SetAngle(obj, angle);
		Obj_SetAutoDelete(obj, false);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetBombResist(obj, true);
		ObjShot_SetDelay(obj, delay);
		while(Obj_BeDeleted(obj) == false) {
			lifecounter--;
			if(lifecounter <= 0) {
				Obj_SetAutoDelete(obj, true);
			}
			yield;
		}
	}
}