#TouhouDanmakufu
#Title["Ashen Sky"]
#Text[Onis arcanum.]
#Image[snapshot\ashensky.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;
	let xl = false;
	let nhf = false;
	let mc = 0;
	let trig = 0;
	let delay = 0;
	@Initialize {
		SetLife(800);
		SetInvincibility(180);
		SetDamageRate(20, 0);
		SetTimer(61);
		SetScore(666666);
		CutIn(KOUMA, ""\""Ashen Sky"\", "script\img\gennerandonis.png", 0, 0, 0, 0);

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

		SetMovePosition03(GetCenterX, GetClipMinY + 120, 10, 3);
	}
	function IsOffScreen {
		return GetX < GetClipMinX || GetX > GetClipMaxX || GetY < GetClipMinY || GetY > GetClipMaxY;
	}
	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);
		if(frame > 0) {
			trig += 2.3;
		}
		if(GetSpeed == 0 && nhf) {
			ascent(a in 0..20) {
				CreateShot02(GetX, GetY, 3, a * 18, -0.01, 1.5, YELLOW03, 30);
			}
			nhf = false;
		}
		if(GetY > GetClipMaxY) {
			xl = false;
			SetY(GetClipMinY - 64);
			SetMovePosition03(GetCenterX, GetClipMinY + 150, 27, 6);
		}
		if(xl) {
			SetSpeed(GetSpeed + 0.02);
			nhf = true;
		}
		if(frame == -120) {
			ascent(a in 0..3) {
				Familiar(GetCenterX, GetClipMinY + 40, a, 2, 60);
			}
		}
		if(frame%420 == 1) {
			SetAngle(GetAngleToPlayer);
			xl = true;
		}
		yield;
		frame++;
	}
	task PerfectFreezeGlobal(x, y, sv, decel, accel, ev, graphic1, graphic2) {
		let obj = Obj_Create(OBJ_SHOT);
		let thaw = false;
		let dc = 30;
		Obj_SetPosition(obj, x, y);	
		Obj_SetSpeed(obj, sv);
		Obj_SetAngle(obj, rand(0, 360));
		ObjShot_SetGraphic(obj, graphic1);
		ObjShot_SetDelay(obj, dc);
		while(Obj_BeDeleted(obj) == false) {
			while(dc > 0) {
				dc--;
				yield;
			}
			while(Obj_GetSpeed(obj) > 0) {
				Obj_SetSpeed(obj, Obj_GetSpeed(obj) + decel);
				yield;
			}
			while(thaw == false) {
				if(delay <= 0) {
					thaw = true;
				}
				yield;
			}
			CreateShot02(Obj_GetX(obj), Obj_GetY(obj), 0, rand(0, 360), accel, ev, graphic2, 0);
			Obj_Delete(obj);
			yield;
		}
	}
	task ThundercloudShot(x, y, v, angle, maxr, minr, rv, tv, headgraphic, tailgraphic, delay) {
		let obj = Obj_Create(OBJ_SHOT);
		let r = minr;
		let ta = 0;
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, v);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, headgraphic);
		ObjShot_SetDelay(obj, delay);
		loop(delay) { yield; }
		while(Obj_BeDeleted(obj) == false) {
			r += rv;
			ta += tv;
			if(r > maxr || r < minr) {
				rv *= -1;
				tv *= -1;
			}
			ascent(a in 0..13) {
				CreateShotA(0, Obj_GetX(obj) + cos(ta + a * 360 / 13) * r, Obj_GetY(obj) + sin(ta + a * 360 / 13) * r, 0);
				SetShotDataA(0, 0, 0, 0, 0, 0, 0, tailgraphic);
				SetShotKillTime(0, 1);
				FireShot(0);
				CreateShotA(0, Obj_GetX(obj) + cos(180 - ta + a * 360 / 13) * r / 2, Obj_GetY(obj) + sin(180 - ta + a * 360 / 13) * r / 2, 0);
				SetShotDataA(0, 0, 0, 0, 0, 0, 0, tailgraphic);
				SetShotKillTime(0, 1);
				FireShot(0);
			}
			yield;
		}
	}
	task Familiar(x, y, angle, turndir, freq) {
		let core = Obj_Create(OBJ_SHOT);
		let obj = Obj_Create(OBJ_EFFECT);
		let ga = 0;
		let sa = angle * 120;
		ObjShot_SetGraphic(core, PURPLEI02);
		ObjShot_SetBombResist(core, true);
		Obj_SetAutoDelete(core, false);
		ObjEffect_SetTexture(obj, ImgFamiliar);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexUV(obj, 0, 64, 64);
		ObjEffect_SetVertexUV(obj, 1, 128, 64);
		ObjEffect_SetVertexUV(obj, 2, 128, 128);
		ObjEffect_SetVertexUV(obj, 3, 64, 128);
		ObjEffect_SetVertexXY(obj, 0, -32, -32);
		ObjEffect_SetVertexXY(obj, 1, 32, -32);
		ObjEffect_SetVertexXY(obj, 2, 32, 32);
		ObjEffect_SetVertexXY(obj, 3, -32, 32);
		loop(120) {
			yield;
		}
		while(Obj_BeDeleted(obj) == false) {
			Obj_SetPosition(obj, GetX + cos(sa) * 60, GetY + sin(sa) * 60);
			Obj_SetPosition(core, GetX + cos(sa) * 60, GetY + sin(sa) * 60);
			Obj_SetAngle(core, atan2(Obj_GetY(obj) - GetY, Obj_GetX(obj) - GetX));
			ObjEffect_SetAngle(obj, 0, 0, ga);
			ga += turndir * -1;
			sa += turndir;
			if(frame%5 == 1 && GetSpeed > 0.5) {
				ascent(a in 0..3) {
					CreateShotA(0, Obj_GetX(obj), Obj_GetY(obj), 30);
					SetShotDataA(0, 0, 5, Obj_GetAngle(core) + a * 120, 0, -0.25, 0, PURPLE04);
					SetShotDataA(0, 120, NULL, Obj_GetAngle(core) - 90 + a * 120, 0, 0.01, 1, YELLOW12);
					FireShot(0);
				}
			}
			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);
		}
	}
	@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;
		}
	}
}