#TouhouDanmakufu
#Title["Scorched Earth"]
#Text[Onis arcanum.]
#Image[snapshot\scorchedearth.bmp]
#Player[player\ProcellaPlayers\EmilyA\EmilyA.dnh, player\ProcellaPlayers\EmilyB\EmilyB.dnh, player\ProcellaPlayers\FeliciaA\FeliciaA.dnh, player\ProcellaPlayers\FeliciaB\FeliciaB.dnh]
#PlayLevel[Normal]
#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 ImgFamiliar = "script\img\circle6_s.png";
	let frame = -120;
	@Initialize {
		SetLife(800);
		SetInvincibility(330);
		SetDamageRate(15, 0);
		SetTimer(61);
		SetScore(666666);
		CutIn(KOUMA, ""\""Scorched Earth"\", "script\img\gennerandonis.png", 0, 0, 0, 0);

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

		SetMovePosition02(GetCenterX, GetClipMinY + 120, 120);
	}
	function IsOffScreen {
		return GetX < GetClipMinX || GetX > GetClipMaxX || GetY < GetClipMinY || GetY > GetClipMaxY;
	}
	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);
		if(frame%60 == 1) {
			Familiar(GetClipMinX + 32, GetClipMinY - 32, 2, 90, 0, 40, 0);
		}
		else if(frame%60 == 31) {
			Familiar(GetClipMaxX - 32, GetClipMinY - 32, 2, 90, 180, 40, 0);
		}
		if(frame%540 >= 121 && frame%540 <= 181 && frame%10 == 1) {
			let lasang = rand(60, 120);
			let lasx = rand_int(GetClipMinX + 1, GetClipMaxX - 1);
			CreateLaserA(0, lasx, GetClipMinY + 1, 600, 20, RED60, 10);
			SetLaserDataA(0, 0, lasang, 0, 0, 0, 0);
			SetShotKillTime(0, 10);
			FireShot(0);
			BurstLaser(lasx, GetClipMinY + 1, 10, lasang, 10, 20, RED55, SP01, 90);
		}
		yield;
		frame++;
	}
	task Familiar(x, y, v, angle, shotangle, freq, delay) {
		let core = Obj_Create(OBJ_SHOT);
		let obj = Obj_Create(OBJ_EFFECT);
		let ga = 0;
		Obj_SetPosition(obj, x, y);
		Obj_SetPosition(core, x, y);
		Obj_SetAngle(core, angle);
		Obj_SetAutoDelete(core, false);
		ObjShot_SetGraphic(core, REDI02);
		ObjShot_SetBombResist(core, true);
		Obj_SetAngle(obj, angle);
		ObjEffect_SetTexture(obj, ImgFamiliar);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexUV(obj, 0, 128, 64);
		ObjEffect_SetVertexUV(obj, 1, 192, 64);
		ObjEffect_SetVertexUV(obj, 2, 192, 128);
		ObjEffect_SetVertexUV(obj, 3, 128, 128);
		loop(delay) {
			yield;
		}
		while(Obj_BeDeleted(obj) == false) {
			if(frame%freq == 1) {
				CreateShot01(Obj_GetX(obj), Obj_GetY(obj), 1.5, shotangle, REDI01, delay);
			}
			ObjEffect_SetVertexXY(obj, 0, -32, -32);
			ObjEffect_SetVertexXY(obj, 1, 32, -32);
			ObjEffect_SetVertexXY(obj, 2, 32, 32);
			ObjEffect_SetVertexXY(obj, 3, -32, 32);
			ObjEffect_SetAngle(obj, 0, 0, ga);
			Obj_SetPosition(obj, Obj_GetX(obj) + cos(angle) * v, Obj_GetY(obj) + sin(angle) * v);
			Obj_SetPosition(core, Obj_GetX(obj), Obj_GetY(obj));
			ga += 2;
			yield;
		}
	}
	function TrailShot(x, y, v1, v2, angle, headshot, bodyshot, tailshot, delay) {
		CreateShot01(GetX, GetY, v2, angle, headshot, delay);
		loop(8) {
			CreateShot01(GetX, GetY, rand(v1 + 0.5, v2), angle + rand(-2.5, 2.5), bodyshot, delay);
			CreateShot01(GetX, GetY, rand(v1, v2 - 0.5), angle + rand(-2.5, 2.5), tailshot, delay);
		}
	}
	task BurstLaser(x, y, v, angle, l, w, graphic1, graphic2, delay) {
		let obj = Obj_Create(OBJ_SINUATE_LASER);
		let boom = false;
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, v);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, graphic1);
		ObjShot_SetDelay(obj, delay);
		ObjSinuateLaser_SetLength(obj, l);
		ObjSinuateLaser_SetWidth(obj, w);
		while(delay > 0) {
			delay--;
			yield;
		}
		while(Obj_BeDeleted(obj) == false) {
			if((Obj_GetX(obj) < GetClipMinX || Obj_GetX(obj) > GetClipMaxX || Obj_GetY(obj) < GetClipMinY || Obj_GetY(obj) > GetClipMaxY) && boom == false) {
				ascent(m in 0..20) {
					CreateShotA(0, Obj_GetX(obj), Obj_GetY(obj), 0);
					SetShotDataA(0, 0, 1, m * 18, 5, 0, 1, graphic2);	
					SetShotDataA(0, 90, NULL, NULL, 0, 0, 1, graphic2);
					FireShot(0);
					CreateShotA(0, Obj_GetX(obj), Obj_GetY(obj), 0);
					SetShotDataA(0, 0, 1, m * 18, -5, 0, 0.5, graphic2);	
					SetShotDataA(0, 90, NULL, NULL, 0, -0.05, 0.5, graphic2);
					FireShot(0);
				}
				boom = true;
			}
			yield;
		}
		yield;
	}
	@DrawLoop {
		SetTexture(ImgBoss);
		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;
		}
	}
}