#TouhouDanmakufu
#Title["Clash of Souls" -Hard-]
#Text[Genner Onisque pugnant.]
#Image[snapshot\clashofsoulshard.bmp]
#Player[player\ProcellaPlayers\EmilyA\EmilyA.dnh, player\ProcellaPlayers\EmilyB\EmilyB.dnh, player\ProcellaPlayers\FeliciaA\FeliciaA.dnh, player\ProcellaPlayers\FeliciaB\FeliciaB.dnh]
#PlayLevel[Hard]
#BGM[bgm\Night Falls ~ Evening Star.mp3]
#ScriptVersion[2]

script_enemy_main {
	#include_function "lib\SHOT_REPLACE\shot_replace.dnh"
	#include_function "p_config.txt"
	let ImgBoss = "script\img\stg8enm.png";
	let ImgFamiliar = "script\img\circle6_s.png";
	let LastSpellBgm = "bgm\Soul Battle ~ Clash of Souls.dnh";
	let frame = -120;
	@Initialize {
		SetEffectForZeroLife(66, 255, 2);
		SetLife(6666);
		SetDamageRate(0, 0);
		SetTimer(61);
		SetScore(666666);
		LastSpell;
		SetDurableSpellCard;
		CutIn(KOUMA, ""\""Clash of Souls"\", "script\img\gennerandonis.png", 0, 0, 0, 0);

		LoadGraphic(ImgBoss);
		SetTexture(ImgBoss);
		SetGraphicRect(6,195,70,285);
		LoadGraphic(ImgFamiliar);
		if(bgm_on) {
			LoadMusic(LastSpellBgm);
			PlayMusic(LastSpellBgm);
		}
		psrand(747030);
		shotinit;

		SetMovePosition03(GetClipMaxX - 40, GetCenterY, 10, 3);
	}
	function IsOffScreen {
		return GetX < GetClipMinX || GetX > GetClipMaxX || GetY < GetClipMinY || GetY > GetClipMaxY;
	}
	@MainLoop {
		SetCollisionA(GetCenterX, GetCenterY, 500);
		if(frame%420 == 61) {
			SpinningWallA(GetClipMinX + 40, GetCenterY, 2, 0, RED50, 10);
			SpinningWallB(GetClipMinX + 40, GetCenterY, 2, 0, RED50, 10);
		}
		if(frame%600 == 361) {
			Explosion(GetCenterX, GetClipMinY, 2, 2.25, 144, ORANGE57, RED57, 0);
			Explosion(GetCenterX, GetClipMinY, 2.5, 2.75, 144, ORANGE57, RED57, 0);
		}
		if(frame%600 == 1) {
			CannonA(GetClipMinX, GetClipMinY + 32, GetClipMaxY - 32, 2, 4, 0, 5, 30);
		}
		else if(frame%300 == 1) {
			CannonB(GetClipMinX, GetClipMinY + 32, GetClipMaxY - 32, 2, 4, 0, 5, 30);
		}
		if(frame%450 == 361) {
			OnisBlastB;
		}
		else if(frame%90 == 31) {
			OnisBlastA;
		}
		yield;
		frame++;
	}
	task OnisBlastB {
		let x = rand(GetClipMinX, GetClipMaxX);
		CreateLaser01(x, GetClipMinY, 4, 90, 150, 15, WHITE50, 60);
		ascent(a in 0..36) {
			CreateShot01(x, GetClipMinY, 3 - (absolute(9 - a)) / 6, a * 10, WHITEI23, 60);
			CreateShot01(x, GetClipMinY, 1.5 - (absolute(9 - a)) / 12, a * 10, WHITEI23, 60);
		}
	}
	task OnisBlastA {
		let y = rand(GetClipMinY, GetClipMaxY);
		CreateLaser01(GetClipMaxX, y, 4, 180, 150, 15, WHITE50, 60);
		ascent(a in 0..36) {
			CreateShot01(GetClipMaxX, y, 3 - (absolute(18 - a)) / 6, a * 10, WHITEI23, 60);
			CreateShot01(GetClipMaxX, y, 1.5 - (absolute(18 - a)) / 12, a * 10, WHITEI23, 60);
		}
	}
	task CannonA(x, y1, y2, v1, v2, angle, num, delay) {
		let i = (y2 - y1) / (num - 1);
		ascent(y in 0..num) {
			TrailShot(x, y1 + y * i, v1, v2, angle, PURPLE03, PURPLE02, PURPLE01, delay);
			loop(7) { yield; }
		}
	}
	task CannonB(x, y1, y2, v1, v2, angle, num, delay) {
		let i = (y2 - y1) / num;
		descent(y in 0..num + 1) {
			TrailShot(x, y1 + y * i, v1, v2, angle, PURPLE03, PURPLE02, PURPLE01, delay);
			loop(7) { yield; }
		}
	}
	function TrailShot(x, y, v1, v2, angle, headshot, bodyshot, tailshot, delay) {
		CreateShot01(x, y, v2, angle, headshot, delay);
		loop(5) {
			CreateShot01(x, y, rand(v1 + 0.5, v2), angle + rand(-2.5, 2.5), bodyshot, delay);
			CreateShot01(x, y, rand(v1, v2 - 0.5), angle + rand(-2.5, 2.5), tailshot, delay);
		}
	}
	task SpinningWallA(x, y, v, angle, graphic, delay) {
		loop(36) {
			CreateLaser01(x + cos(angle + 90) * 45, y + sin(angle + 90) * 45, v, angle, 100, 10, graphic, delay);
			CreateLaser01(x + cos(angle - 90) * 45, y + sin(angle - 90) * 45, v, angle, 100, 10, graphic, delay);
			CreateShot01(x, y, v, angle, RED02, delay);
			angle += 10;
			yield;
		}
	}
	task SpinningWallB(x, y, v, angle, graphic, delay) {
		loop(36) {
			CreateLaser01(x + cos(angle + 90) * 45, y + sin(angle + 90) * 45, v, angle, 100, 10, graphic, delay);
			CreateLaser01(x + cos(angle - 90) * 45, y + sin(angle - 90) * 45, v, angle, 100, 10, graphic, delay);
			CreateShot01(x, y, v, angle, RED02, delay);
			angle -= 10;
			yield;
		}
	}
	task Explosion(x, y, v1, v2, num, graphic1, graphic2, delay) {
		ascent(a in 0..num) {
			CreateShot01(x, y, v1, a * 360 / num, graphic1, delay);
			if(a%2 == 0) {
				CreateShot01(x, y, v2, a * 360 / num, graphic2, delay);
			}
		}
	}
	@DrawLoop {
		SetTexture(ImgBoss);
		DrawGraphic(GetClipMinX + (GetClipMaxX - GetX), GetY);
		DrawGraphic(GetX, GetY);
	}
	@Finalize {
		loop(18) { CreateItem(ITEM_SCORE, GetX + rand(-50, 50), GetY - rand(20, 100)); }
		DeleteGraphic(ImgBoss);
		DeleteGraphic(ImgFamiliar);
	}
	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;
		}
	}
}