script_enemy_main {
	#include_function ".\includeKoishiSurvivalKoishi.txt"
	task TMain {
		SetDamageRate(0, 0);
		SetMovePosition03(GetCenterX, GetCenterY-100, 10, 9);
		SetPlayerMovePosition(GetCenterX, GetClipMaxY-64, 60);
		SetInvincibility(300);
		while(OnPlayerMissed || OnBomb) { SetInvincibility(300); yield; }
		wait(360);
		AllowMovement;
		wait(120);
		TCharge(GetPlayerX, GetPlayerY);
		PlaySE2("weird");
		wait(60);
		attack1;
	}
	task SetPlayerMovePosition ( xd, yd, f ) {
		let xo = GetPlayerX; let yo = GetPlayerY;
		descent(i in 0..f) {
			SetPlayerX(xo+(xd-xo)*(1+cos(i*180/f))/2);
			SetPlayerY(yo+(yd-yo)*(1+cos(i*180/f))/2);
			yield;
		}
	}
	task AllowMovement {
		function v { return [1.5,4][GetKeyState(VK_SLOWMOVE)==KEY_FREE]; }
		let canmove = 0;
		task DeleteShots {
			AddBomb(2-GetPlayerBomb);
			ascent(i in 0..30) { DeleteEnemyShotToItemInCircle(SHOT, GetX, GetY, i*30); yield; }
		}
		loop {
			SetCollisionB(GetPlayerX, GetPlayerY, 10);
			if(canmove == 0) {
				let a = 0;
				let v2 = 0;
				if(GetKeyState(VK_RIGHT)!=KEY_FREE) { 
					v2 = 1; a = 0;
					if(GetKeyState(VK_DOWN)!=KEY_FREE) { a = 45; }
					else if(GetKeyState(VK_UP)!=KEY_FREE) { a = -45; }
				} else if(GetKeyState(VK_LEFT)!=KEY_FREE) { 
					v2 = 1; a = 180;
					if(GetKeyState(VK_DOWN)!=KEY_FREE) { a = 180-45; }
					else if(GetKeyState(VK_UP)!=KEY_FREE) { a = 180+45; }
				} else if(GetKeyState(VK_DOWN)!=KEY_FREE) { v2 = 1; a = 90; }
				else if(GetKeyState(VK_UP)!=KEY_FREE) { v2 = 1; a = -90; }
				SetMovePosition02(GetX, GetY, 1);
				SetX(GetX+v*v2*cos(a)); SetY(GetY+v*v2*sin(a));
			} else { canmove--; }
			if(GetCommonDataDefault("JustDied", false)) {
				SetCommonData("JustDied", false);
				SetMovePosition03(GetX+rand(-100, 100), GetY+rand(-90, -60), 13, 4);
				canmove = 75;
			}
			if(canmove == 60) { DeleteShots; }
			if(GetX < GetClipMinX+16) { SetX(GetClipMinX+16); }
			if(GetX > GetClipMaxX-16) { SetX(GetClipMaxX-16); }
			if(GetY < GetClipMinY+16) { SetY(GetClipMinY+16); }
			if(GetY > GetClipMaxY-16) { SetY(GetClipMaxY-16); }
			yield;
		}
	}
	function GetAngleToKoishi { return atan2(GetY-GetPlayerY,GetX-GetPlayerX); }
	task attack1 {
		let ang = 0;
		loop(12) {
			loop(6) {
				PlaySE2("tan00");
				let n = [9,11,11,13][GetDifficulty];
				ascent(i in 0..n) { ascent(j in 0..2) {
					CreateShotA(0, GetPlayerX, GetPlayerY, 10);
					SetShotDataA(0, 0, 3, -90+30*sin(ang)+i*360/n, 0, 0, 0, 49);
					SetShotDataA(0, 30, NULL, NULL, [80,60,60,50][GetDifficulty]*((-1)^j), 0, 0, NULL);
					SetShotDataA(0, 31, 0, NULL, 0, 1/10, 3, NULL);
					FireShot(0);
				} }
				ang+=5;
				wait(12);
			}
			ascent(j in 0..[1,2,3,3][GetDifficulty]) { let a = GetAngleToKoishi+90+rand(-20, 20); ascent(i in 0..25+GetDifficulty*3) {
				CreateShotA(0, GetPlayerX, GetPlayerY, 10+j*8);
				SetShotDataA(0, 0, (4-j/2), a-i*180/(25+GetDifficulty*3), 0, -(4-j/2)/60, 0, 64);
				SetShotDataA(0, 60, 0, NULL, 0, 1/10, 2, 38);
				FireShot(0);
			} }
		}
		wait(36);
		DeleteEnemyShotToItem(ALL);
		attack2;
		attack2b;
	}
	task attack2 {
		loop(12) {
			PlaySE2("tan00"); let a = rand(0,360);
			ascent(j in 0..[1,1,2,3][GetDifficulty]) { ascent(i in 0..22) {
				CreateShot01(GetPlayerX, GetPlayerY, 2+j/2, a+i*360/22+j*180/22, 72, 10);
			} }
			wait(45);
		}
		wait(60);
		DeleteEnemyShotToItem(ALL);
		attack3;
	}
	task HakureiAmulet(v, ang) {
		let obj = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj, GetPlayerX, GetPlayerY);
		Obj_SetAngle(obj, ang);
		ObjShot_SetGraphic(obj, 56);
		Obj_SetSpeed(obj, 10);
		ObjShot_SetBombResist(obj, true);
		Obj_SetAutoDelete(obj, false);
		wait(10);
		descent(i in 0..60) {
			Obj_SetSpeed(obj, v*i/60);
			wait(1);
		}
		if(GetDifficulty >= 2) {
			ObjShot_SetGraphic(obj, 49);
			Obj_SetAngle(obj, atan2(GetY-Obj_GetY(obj), GetX-Obj_GetX(obj)));
			Obj_SetSpeed(obj, 3); wait(30);
			descent(i in 0..45) {
				Obj_SetSpeed(obj, 3*i/45);
				wait(1);
			}
			ObjShot_SetGraphic(obj, 55);
			Obj_SetAngle(obj, atan2(GetY-Obj_GetY(obj), GetX-Obj_GetX(obj)));
			Obj_SetSpeed(obj, 2);
		} else {
			ObjShot_SetGraphic(obj, 55);
			Obj_SetAngle(obj, atan2(GetY-Obj_GetY(obj), GetX-Obj_GetX(obj)));
			Obj_SetSpeed(obj, 2);
		}
		Obj_SetAutoDelete(obj, true);
	}
	task attack2b {
		loop(5) {
			SetPlayerMovePosition(GetCenterX+rand(-75, 75), GetCenterY+192+rand(-15, 30), 45);
			wait(15);
			loop(2) {
				let ang = -90+rand(-45, 45);
				ascent(i in -7..8) { ascent(j in 0..2) { HakureiAmulet(2+j, ang+i*6); } }
				wait(45);
			}
		}
	}
	task attack3 {
		let side = 1;
		let y = 150;
		loop(10) {
			SetPlayerMovePosition(GetCenterX+(100+rand(-40, 40))*side, GetCenterY+y+rand(-20,20), 30);
			PlaySE2("tan00"); let a = rand(0,360);
			let n = [17, 23, 23, 29][GetDifficulty];
			ascent(j in 0..[1, 1, 2, 2][GetDifficulty]) { ascent(i in 0..n) {
				CreateShot01(GetPlayerX, GetPlayerY, 3/2+j/5, a+i*360/n+j*180/n, 54, 10);
			} }
			y-=20;
			side*=-1;
			wait(30);
		}
		SetPlayerMovePosition(GetCenterX, GetCenterY+128, 30);
		wait(60);
		DeleteEnemyShotToItem(ALL);
		attack4;
	}
	task attack4 {
		task HakureiAmulet(ang) {
			let obj = Obj_Create(OBJ_SHOT);
			Obj_SetPosition(obj, GetPlayerX, GetPlayerY);
			Obj_SetAngle(obj, ang);
			ObjShot_SetGraphic(obj, 49);
			Obj_SetSpeed(obj, 4);
			ObjShot_SetBombResist(obj, true);
			let m = 35;
			while(!Obj_BeDeleted(obj)) {
				PlaySE2("tan00");
				CreateShot01(Obj_GetX(obj), Obj_GetY(obj), 7/4, ang+180+m, 50, 10);
				CreateShot01(Obj_GetX(obj), Obj_GetY(obj), 7/4, ang+180-m, 50, 10);
				m+=[5, 4, 4, 3][GetDifficulty];
				wait([8, 6, 5, 4][GetDifficulty]);
			}
		}
		loop(4) {
			PlaySE2("kira00");
			let ang = 90+rand(-10, 10);
			ascent(i in 0..5) { HakureiAmulet(ang+i*360/5); }
			wait(112);
		}
		wait(92);
		DeleteEnemyShotToItem(ALL);
		attack5;
	}
	task attack5 {
		let g = true;
		task MikoOrb(x, y, movang) {
			let obj = Obj_Create(OBJ_EFFECT);
			ObjEffect_SetTexture(obj,shots); ObjEffect_SetRenderState(obj,SUBTRACT);
			ObjEffect_CreateVertex(obj,4); ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN); ObjEffect_SetLayer(obj, 3);
			ObjEffect_SetVertexUV(obj,0,160,320); ObjEffect_SetVertexUV(obj,1,256,320); ObjEffect_SetVertexUV(obj,2,256,416); ObjEffect_SetVertexUV(obj,3,160,416);
			ObjEffect_SetVertexXY(obj,0,-48,-48); ObjEffect_SetVertexXY(obj,1,48,-48); ObjEffect_SetVertexXY(obj,2,48,48); ObjEffect_SetVertexXY(obj,3,-48,48);
			Obj_SetPosition(obj, x, y);
			let init = 90;
			task Scale {
				let an = rand(0,360); let rt = 0;
				while(!Obj_BeDeleted(obj)) {
					ObjEffect_SetScale(obj, (5/2+1/6*cos(an))*cos(init), (5/2+1/6*cos(an))*cos(init));
					ObjEffect_SetAngle(obj, 0, 0, rt);
					an+=9; an%=360; rt+=49; rt%=360;
					Obj_SetPosition(obj, Obj_GetX(obj)+2*cos(movang), Obj_GetY(obj)+2*sin(movang));
					yield;
				}
			} Scale;
			task col {
				while(g) { 
					SetCollisionB(Obj_GetX(obj), Obj_GetY(obj), 28);
					if(init > 0) { init-=5; }
					yield;
				}
				while(init < 90) { init+=5; yield; } Obj_Delete(obj);
			} col;
		}
		let ang = rand(0,360); let ang2 = 43;
		let n = [0, 1, 2, 2][GetDifficulty];
		loop(7) {
			PlaySE2("weird");
			ascent(i in -n..n+1) {
				if(GetDifficulty >= 2) {
					let x = GetCenterX+105*sin(ang)+i*168;
					loop([0, 0, 4, 10][GetDifficulty]) { CreateShot01(x, GetClipMaxY, rand(1,2), -rand(0, 180), 97+rand_int(0,7), 0); }
					MikoOrb(x, GetClipMaxY, -90);
				} else {
					loop([5, 10, 0, 0][GetDifficulty]) { CreateShot01(GetPlayerX, GetPlayerY, rand(1,2), -rand(0, 180), 97+rand_int(0,7), 0); }
					MikoOrb(GetPlayerX, GetPlayerY, GetAngleToKoishi+i*60);
				}
			}
			ang+=ang2; ang%=360; ang2+=2;
			SetPlayerMovePosition(GetCenterX+rand(-125, 125), GetClipMaxY-64+rand(-32, 48), 60);
			wait(60);
		}
		attack6;
		wait(60);
		g = false;
		DeleteEnemyShotToItem(SHOT);
	}
	task attack6 {
		task HakureiAmulet(ang, gra, v) {
			let obj = Obj_Create(OBJ_SHOT);
			Obj_SetPosition(obj, GetPlayerX, GetPlayerY);
			Obj_SetAngle(obj, ang);
			ObjShot_SetGraphic(obj, gra);
			Obj_SetSpeed(obj, v);
			let m = 0;
			while(!Obj_BeDeleted(obj)) {
				wait(1);
				if(m == 0) {
					if(Obj_GetX(obj) < GetClipMinX) {
						ObjShot_SetDelay(obj, 15);
						Obj_SetX(obj, GetClipMaxX-(GetClipMinX-Obj_GetX(obj)));
						Obj_SetY(obj, Obj_GetY(obj)-GetCenterY);
						m+=1;
					} else if(Obj_GetX(obj) > GetClipMaxX) {
						ObjShot_SetDelay(obj, 15);
						Obj_SetX(obj, GetClipMinX+(Obj_GetX(obj)-GetClipMaxX));
						Obj_SetY(obj, Obj_GetY(obj)-GetCenterY);
						m+=1;
					} else if(Obj_GetY(obj) < GetCenterY) {
						ObjShot_SetDelay(obj, 15);
						Obj_SetY(obj, GetClipMinY+GetCenterY-Obj_GetY(obj));
						Obj_SetAngle(obj, -Obj_GetAngle(obj));
						m+=1;
					} else if(Obj_GetY(obj) > GetClipMaxY) {
						ObjShot_SetDelay(obj, 15);
						Obj_SetY(obj, GetCenterY+GetClipMaxY-Obj_GetY(obj));
						Obj_SetAngle(obj, -Obj_GetAngle(obj));
						m+=1;
					}
				} else if(m == 1) {
					if(Obj_GetX(obj) < GetClipMinX) {
						ObjShot_SetDelay(obj, 10);
						m+=1;
					} else if(Obj_GetX(obj) > GetClipMaxX) {
						ObjShot_SetDelay(obj, 10);
						m+=1;
					} else if(Obj_GetY(obj) > GetCenterY) {
						ObjShot_SetDelay(obj, 10);
						m+=1;
					} else if(Obj_GetY(obj) < GetClipMinY) {
						ObjShot_SetDelay(obj, 10);
						m+=1;
					}
				} else { break; }
			} loop(7) { yield; } Obj_Delete(obj);
		}
		CreateLaserA(0, -16, GetCenterY, 0, 16, 225, 9000);
		SetLaserDataA(0, 0, 0, 0, 70, 0, 0);
		SetLaserDataA(0, 60, 0, 0, 0, 0, 0);
		FireShot(0);
		SetPlayerMovePosition(GetCenterX, (GetClipMaxY+GetCenterY)/2, 30);
		wait(60);
		DeleteEnemyShotToItem(SHOT);
		let num = [4, 8, 10, 14][GetDifficulty];
		let a = 180/num; let b = 0; let f = 0;
		loop {
			PlaySE2("tan00");
			if(f%8 < 5) { ascent(i in 0..num) {
				HakureiAmulet(a+i*360/num, 49, 2);
			} }
			a+=b; if(b < 1) { b+=1/12; } f++; f%=8;
			HakureiAmulet(-90+rand(-150,150), 16, 3/2);
			loop(6) {
				if(GetY > GetCenterY) { HakureiAmulet(GetAngleToKoishi+rand(-40, 40), 16, rand(5, 7)); }
			wait(1); }
		}
	}
}