#TouhouDanmakufu[Single]
#Title[Koishi - Spell #6]
#BackGround[User(.\bgNon.png, 0, 0)]
#BGM[.\music0.mp3]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main {

	#include_function ".\Include2.~ath"

	task codeInit {
		SetLife(10000); SetTimer(51);
		SetMovePosition03(GetX+rand(-100,100), GetY+rand(-75,0), 20, 7); wait(120);
		ascent(i in 0..40) { ascent(j in 0..4) { CreateShot01(GetX, GetY, 3+j/2, GetAngleToPlayer+i*360/40, BLUE03, 0); } }
		SetMovePosition03(GetX, GetClipMinY-300, 10, 4);
		cutin("Distant Past "\""Escape From Your Ancient Guilt"\","",0,0,255,511); SetScore(1000000); SetDurableSpellCard;
		SetEnemyMarker(false);
		SetInvincibility(120);
		wait(120);
		attack1;
		attack2;
	}
	let red = []; let blue = []; let explode = 0;
	let ev = 2;
	task attack1 {
		let a = 0; let r = 300; let r2 = 100;
		loop {
			r2-=50/(45*60/2);
			if(r > r2) { r-=2; } else { r = r2; }
			ascent(i in 0..3) {
				CreateShotA(0, GetPlayerX+r*cos(a+i*360/3), GetPlayerY+r*sin(a+i*360/3), 5);
				SetShotDataA(0, 0, 1, a+i*360/3, 2, -1/60, 0, [RED30, BLUE30][explode]);
				SetShotKillTime(0, 90);
					CreateShotA(1, 0, 0, 10);
					SetShotDataA(1, 0, 0, 0, 0, 0, 0, [RED30, BLUE30][explode]);
					SetShotKillTime(1, 0);
					AddShot(89, 0, 1, 0);
				FireShot(0);
			}
			a+=7; a%=360;
			wait(2);
		}
	}
	task attack2 {
		let time = 24;
		let c = 6;
		loop {
			loop([360, 120][c>5]/time) {
				wait(time);
				let x = GetCenterX+rand(-50,50); ascent(i in -10..11) { CreateBullet(x+i*34, [1,0][rand_int(0,floor(c))==floor(c)]); }
			} 
			loop(30) {
				ascent(i in 0..length(red)) { Obj_SetSpeed(red[i], Obj_GetSpeed(red[i])-ev/30); }
				ascent(i in 0..length(blue)) { Obj_SetSpeed(blue[i], Obj_GetSpeed(blue[i])-ev/30); }
				wait(1);
			}
			wait(30);
			if(explode == 0) {
				ascent(i in 0..length(red)) { Obj_SetSpeed(red[i], 0); ObjShot_SetDelay(red[i], 11); ObjShot_SetGraphic(red[i], RED35); }
				ascent(i in 0..length(blue)) { Obj_SetSpeed(blue[i], 0); ObjShot_SetDelay(blue[i], 61); ObjShot_SetGraphic(blue[i], BLUE35); }
				wait(10);
				ascent(i in 0..length(red)) { Obj_Delete(red[i]); }
				wait(110);
				ascent(i in 0..length(blue)) { ObjShot_SetDelay(blue[i], 31); }
				wait(30);
				ascent(i in 0..length(blue)) { Obj_Delete(blue[i]); }
			} else {
				ascent(i in 0..length(red)) { Obj_SetSpeed(red[i], 0); ObjShot_SetDelay(red[i], 61); ObjShot_SetGraphic(red[i], RED35); }
				ascent(i in 0..length(blue)) { Obj_SetSpeed(blue[i], 0); ObjShot_SetDelay(blue[i], 11); ObjShot_SetGraphic(blue[i], BLUE35); }
				wait(10);
				ascent(i in 0..length(blue)) { Obj_Delete(blue[i]); }
				wait(110);
				ascent(i in 0..length(red)) { ObjShot_SetDelay(red[i], 31); }
				wait(30);
				ascent(i in 0..length(red)) { Obj_Delete(red[i]); }
			}
			wait(90);
			explode++; explode%=2; time-=4; if(ev < 4) { ev+=3/4; } c-=1/2;
		}
	}
	task CreateBullet(x, type) {
		let obj = Obj_Create(OBJ_SHOT);
		alternative((type+explode)%2)
		case(0) { red = red ~ [obj]; ObjShot_SetGraphic(obj, RED01); }
		case(1) { blue = blue ~ [obj]; ObjShot_SetGraphic(obj, BLUE01); }
		Obj_SetPosition(obj, x, 0); Obj_SetSpeed(obj, ev+rand(-1/20, 1/20)); Obj_SetAngle(obj, 90);
		ObjShot_SetBombResist(obj, true); Obj_SetAutoDelete(obj, false);
		while(!Obj_BeDeleted(obj)) {
			wait(1);
		}
		alternative((type+explode)%2)
		case(0) { ascent(i in 0..length(red)) { if(red[i] == obj) { red = erase(red, i); break; } } }
		case(1) { ascent(i in 0..length(blue)) { if(blue[i] == obj) { blue = erase(blue, i); break; } } }
	}
}