//#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Nonspell #1"]
#Background["./BG/BG.dnh"]
#Player["./Player/AoCF_YukariReimu/AoCF_YukariReimu.dnh"]
#System["./System/System.dnh"]
#include "./LIB/Library_Index.dnh"

let bossObj;
let bossX = 0;
let bossY = 0;
let bosslife;

let diff = GetCommonData("Difficulty", 0);

@Initialize{//Declare the boss and assign the texture
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);

	SetAreaCommonData("System", "CurrentActiveScript", GetOwnScriptID);

	//Move the boss onto the game field
	ObjMove_SetDestAtWeight(bossObj, GetCenterX, 96, 10, 10);
	ObjEnemy_SetDamageRate(bossObj, 0, 0);
	mainTask;
	
	if(GetCommonData("Boss", "usami") == "doremy"){Tag;} //switch to Sumireko
	else{
		SetCommonData("Boss", "usami");
		AnimateBoss_Usami(bossObj);
	}

	NotifyEventAll(EV_USER+2, 0);
	NotifyEventAll(EV_USER+3, 0); //activate magic circle
	SetShotAutoDeleteClip(96, 96, 96, 96);
}

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(12000);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(60);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(6000000 + diff*100000);
	}
}

@Loading{
}

@MainLoop{
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);

	ObjEnemy_SetIntersectionCircleToShot(bossObj, bossX, bossY, 40);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj, bossX, bossY, 20);
	yield;
}

@Finalize{

}

task mainTask{
	EnemyName(bossObj, "Sumireko & Doremy", 255, 0, 192);

	FlameAura(bossObj, [255, 0, 192], 2);
	BottomEnemyMarker(bossObj, bossObj);
	wait(120);

	alternative(diff)
	case(0){
		fireE;
	}
	case(1){
		fireN;
	}
	case(2){
		fireH;
	}
	case(3){
		fireL;
	}


	endTask;
	wait(180);

	ObjEnemy_SetDamageRate(bossObj, 240, 100);
	ObjEnemy_PlayHitSound(bossObj);
}

task Tag{
	if(GetCommonData("Boss", "usami") == "usami"){
		PossessionTag(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj));
		ObjAnim_ForceStop(bossObj);
		Obj_SetValue(bossObj, "anim_forcestop", false);

		SetCommonData("Boss", "doremy");
		AnimateBoss_Doremy(bossObj);
	}else if(GetCommonData("Boss", "usami") == "doremy"){
		PossessionTag(ObjMove_GetX(bossObj), ObjMove_GetY(bossObj));
		ObjAnim_ForceStop(bossObj);
		Obj_SetValue(bossObj, "anim_forcestop", false); 

		SetCommonData("Boss", "usami");
		AnimateBoss_Usami(bossObj);
	}
}


task fireE{
	let dir = 0;
	let dir2 = 0;
	let radsize = 64;

	loop{
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		PlaySoundA1(17, 70, 0);
		ConcentrationEffect(bossObj, "Cherry", 60, 4, 128, [255, 0, 192], BLEND_ADD_ARGB);
		wait(60);

		radsize = 64;
		loop(14){
			if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
			PlaySoundA1(95, 50, 0);
			
			loop(3){
				ascent(r in 1 .. 4){
					let pos = [bossX + (r*radsize)*cos(dir), bossY + (r*radsize)*sin(dir)];
					if(GetDistance(GetPlayerX, GetPlayerY, pos[0], pos[1]) > 24){
						ShotA2(pos[0], pos[1], 6, dir + 90, -0.25, 1.5, AMULET_PURPLE, 10);
					}
				}
				dir += 360/3;
			}

			dir += 18;
			radsize -= 64/14;
			wait(10);
		}
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		Tag;

		loop(28){
			if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
			PlaySoundA1(95, 50, 0);
			
			loop(2){
				loop(6){
					ShotA2(bossX + radsize*cos(dir) + (radsize/2)*cos(dir2), bossY + radsize*sin(dir) + (radsize/2)*sin(dir2), 0, dir2, 0.1, 2, BL_SMALL_PINK, 10);
					dir2 += 360/6;
				}
				dir += 360/2;
			}

			dir += 11;
			dir2 -= 18;
			radsize += 64/28;
			wait(10);
		}

		wait(60);
		moveDeceleration(bossObj, GetCenterX + rand(-64, 64), GetCenterY + rand(-64, -128), 90, true);
		wait(120);
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		Tag;
		wait(30);
	}
}

task fireN{
	let dir = 0;
	let dir2 = 0;
	let radsize = 64;

	loop{
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		PlaySoundA1(17, 70, 0);
		ConcentrationEffect(bossObj, "Cherry", 60, 4, 128, [255, 0, 192], BLEND_ADD_ARGB);
		wait(60);

		radsize = 64;
		loop(20){
			if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
			PlaySoundA1(95, 50, 0);
			
			loop(4){
				ascent(r in 1 .. 4){
					let pos = [bossX + (r*radsize)*cos(dir), bossY + (r*radsize)*sin(dir)];
					if(GetDistance(GetPlayerX, GetPlayerY, pos[0], pos[1]) > 24){
						ShotA2(pos[0], pos[1], 6, dir + 90, -0.25, 1.5, AMULET_PURPLE, 10);
					}
				}
				dir += 360/4;
			}

			dir += 13;
			radsize -= 64/20;
			wait(7);
		}
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		Tag;

		loop(40){
			if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
			PlaySoundA1(95, 50, 0);
			
			loop(2){
				loop(6){
					ShotA2(bossX + radsize*cos(dir) + (radsize/2)*cos(dir2), bossY + radsize*sin(dir) + (radsize/2)*sin(dir2), 0, dir2, 0.1, 2.5, BL_SMALL_PINK, 10);
					dir2 += 360/6;
				}
				dir += 360/2;
			}

			dir += 11;
			dir2 -= 13;
			radsize += 64/40;
			wait(7);
		}

		wait(60);
		moveDeceleration(bossObj, GetCenterX + rand(-64, 64), GetCenterY + rand(-64, -128), 90, true);
		wait(120);
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		Tag;
		wait(30);
	}
}

task fireH{
	let dir = 0;
	let dir2 = 0;
	let radsize = 64;

	loop{
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		PlaySoundA1(17, 70, 0);
		ConcentrationEffect(bossObj, "Cherry", 60, 4, 128, [255, 0, 192], BLEND_ADD_ARGB);
		wait(60);

		radsize = 64;
		loop(20){
			if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
			PlaySoundA1(95, 50, 0);
			
			loop(5){
				ascent(r in 1 .. 4){
					let pos = [bossX + (r*radsize)*cos(dir), bossY + (r*radsize)*sin(dir)];
					if(GetDistance(GetPlayerX, GetPlayerY, pos[0], pos[1]) > 24){
						ShotA2(pos[0], pos[1], 6, dir + 90, -0.25, 1.5, AMULET_PURPLE, 10);
					}
				}
				dir += 360/5;
			}

			dir += 13;
			radsize -= 64/20;
			wait(7);
		}
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		Tag;

		loop(40){
			if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
			PlaySoundA1(95, 50, 0);
			
			loop(2){
				loop(8){
					ShotA2(bossX + radsize*cos(dir) + (radsize/2)*cos(dir2), bossY + radsize*sin(dir) + (radsize/2)*sin(dir2), 0, dir2, 0.1, 3, BL_SMALL_PINK, 10);
					dir2 += 360/8;
				}
				dir += 360/2;
			}

			dir += 11;
			dir2 -= 13;
			radsize += 64/40;
			wait(7);
		}

		wait(60);
		moveDeceleration(bossObj, GetCenterX + rand(-64, 64), GetCenterY + rand(-64, -128), 90, true);
		wait(120);
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		Tag;
		wait(30);
	}
}

task fireL{
	let dir = 0;
	let dir2 = 0;
	let dir3 = 0;
	let radsize = 48;

	loop{
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		PlaySoundA1(17, 70, 0);
		ConcentrationEffect(bossObj, "Cherry", 60, 4, 128, [255, 0, 192], BLEND_ADD_ARGB);
		wait(60);

		radsize = 64;
		loop(20){
			if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
			PlaySoundA1(95, 50, 0);
			
			loop(3){
				ascent(r in 1 .. 5){
					let pos = [bossX + (r*radsize)*cos(dir), bossY + (r*radsize)*sin(dir)];
					if(GetDistance(GetPlayerX, GetPlayerY, pos[0], pos[1]) > 24){
						ShotA2(pos[0], pos[1], 6, dir + 90, -0.25, 1.5, AMULET_PURPLE, 10);
						ShotA2(pos[0], pos[1], 6, dir - 90, -0.25, 1.5, AMULET_DPURPLE, 10);
					}
				}
				dir += 360/3;
			}

			dir += 17;
			radsize -= 64/20;
			wait(7);
		}
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		Tag;

		loop(40){
			if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
			PlaySoundA1(95, 50, 0);
			
			loop(3){
				loop(6){
					ShotA2(bossX + radsize*cos(dir) + (radsize/2)*cos(dir2), bossY + radsize*sin(dir) + (radsize/2)*sin(dir2), 0, dir2, 0.1, 3.5, BL_SMALL_PINK, 10);
					dir2 += 360/6;
				}
				dir += 360/3;
			}

			dir += 11;
			dir2 -= 17;
			radsize += 64/40;
			wait(7);
		}

		wait(60);
		moveDeceleration(bossObj, GetCenterX + rand(-64, 64), GetCenterY + rand(-64, -128), 90, true);
		wait(120);
		if(ObjEnemy_GetInfo(bossObj, INFO_LIFE) <= 0){return;}
		Tag;
		wait(30);
	}
}

task endTask{
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0) {
		yield;
	}
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	SetAutoDeleteObject(true);
	Obj_Delete(bossObj);
	wait(180);
	CloseScript(GetOwnScriptID());
}