#東方弾幕風[Single]
#ScriptVersion[3]
#Title["SP4"]
#Text["Blep"]
#System["./default_system/Default_System.txt"]
#include "./default_system/MS_Index.txt"

let objBoss;
let BossX;
let BossY;
let player = GetCommonData("Chara", "sanae");

@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	
	HitSound(objBoss, 50, 450);
	if(player == "reimu"){
	RenderReimu(objBoss);
	}
	if(player == "marisa"){
	RenderMarisa(objBoss);
	}
	if(player == "sanae"){
	RenderSanae(objBoss);
	}
	DamageReduction(objBoss, 120);
	EnemyMarker(objBoss);
	
	
	SetCommonData("ForceLifebar", 0);
	
	if(GetCommonData("IsStage", false)==false){
	LoadAssets;}
	
	Main;
	end;
} 

@Event { 
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(4000);
     	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(75);
	}
	/*case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}*/
} 

@MainLoop {
	BossX = ObjMove_GetX(objBoss);
	BossY = ObjMove_GetY(objBoss);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),21);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),24);
	yield;
} 

@Finalize {
} 

task Main { 
wait(90);


	if(player == "reimu"){
	ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2, GetStgFrameHeight/2-80, 5, 4);
	let path = GetCurrentScriptDirectory ~ "./default_system/img/Ingame/rei1.png";
	let path2 = GetCurrentScriptDirectory ~ "./default_system/img/Ingame/rei2.png";
	SpellCard(160, "Trickery &quot;Fantasy Seal -1000 Ryo Bankrupcy-&quot;", [255, 15, 15], path, path2, 5, 150, 0.4, "Reimu");
	SpellReimu;
	}
	if(player == "marisa"){
	ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2, GetStgFrameHeight/2-120, 5, 4);
	let path = GetCurrentScriptDirectory ~ "./default_system/img/Ingame/mar1.png";
	let path2 = GetCurrentScriptDirectory ~ "./default_system/img/Ingame/mar2.png";
	SpellCard(160, "Trickery &quot;A Very Bright Witch&quot;", [255, 255, 5], path, path2, 5, 150, 0.4, "Marisa");
	SpellMarisa;
	}	
	if(player == "sanae"){
	ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2, GetStgFrameHeight/2-80, 5, 4);
	let path = GetCurrentScriptDirectory ~ "./default_system/img/Ingame/san1.png";
	let path2 = GetCurrentScriptDirectory ~ "./default_system/img/Ingame/san2.png";
	SpellCard(160, "Trickery &quot;Wind Good Girl&quot;", [5, 5, 122], path, path2, 5, 150, 0.4, "Sanae");
	SpellSanae;
	}	
}

task SpellMarisa{
SplitStar(BossX, BossY, 3, 90, -0.06, 0, 3);
loop{
wait(60*4);
ascent(i in 0..12){
SplitStar(BossX, BossY, 3, 90+i*360/12, -0.06, 0, 3);
}
wait(60*4);
ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2-100, GetStgFrameHeight/2-120, 5, 4);
wait(60);
ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2+100, GetStgFrameHeight/2-80, 5, 6);
	ascent(i in 0..12){
	SplitStar(BossX, BossY, 3, 180-i*8, -0.06, 0, 3);
	wait(4);
	}
wait(60*5);
ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2+100, GetStgFrameHeight/2-120, 5, 4);
wait(60);
ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2-100, GetStgFrameHeight/2-80, 5, 6);
	ascent(i in 0..12){
	SplitStar(BossX, BossY, 3, 0+i*8, -0.06, 0, 3);
	wait(4);
	}
wait(60*2);
ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2, GetStgFrameHeight/2-120, 5, 4);
wait(60*5);
ascent(j in 0..3){
	ascent(i in 0..6){
	SplitStar(BossX, BossY, 3+j/2, 90+i*360/3, -0.06, 0, 3);
	}
wait(20);
}
wait(60*6);
ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2, GetStgFrameHeight/2, 5, 4);
wait(60);
	ascent(i in 0..6){
	SplitStar(BossX, BossY, 4, 0+i*360/6, -0.06, 0, 3);
	}
wait(60*4);
ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2, GetStgFrameHeight/2-120, 5, 4);
}
}

	task SplitStar(x, y, spd, ang, acc, mspd, del){
		let obj = FireShotA2(x, y, spd, ang, acc, mspd, STAR_M_ORANGE_ADD, del);
		ObjShot_SetSpellResist(obj, 1);
		ObjShot_SetIntersectionEnable(obj, 0);
		Split(obj);
		PlaySound(GetCurrentScriptDirectory ~ "default_system/se/spirit.wav", 65, 0);
		
		task Split(obj){
		wait(del+1);
		while(ObjMove_GetSpeed(obj) != 0){yield;}
		let random = rand(0,360);
		if(!Obj_IsDeleted(obj)){
		ascent(i in 0..5){
		FireShotA2(GetX(obj), GetY(obj), 0, ang+i*360/5, 0.01, 2, STAR_S_YELLOW, 10);
		}
		ascent(i in 0..3){
		Newlaser(GetX(obj), GetY(obj), ang+i*360/3, 500, 25, 45, LASER_C_YELLOW_ADD, 120);
		}
		PlaySound(GetCurrentScriptDirectory ~ "default_system/se/laser1.wav", 65, 120);
		SplitStar2(GetX(obj), GetY(obj), spd, ang+90, -0.06, 0, del);
		SplitStar2(GetX(obj), GetY(obj), spd, ang-90, -0.06, 0, del);
		}
		Obj_Delete(obj);
		}
	}
	task SplitStar2(x, y, spd, ang, acc, mspd, del){
		let obj = FireShotA2(x, y, spd, ang, acc, mspd, STAR_M_ORANGE_ADD, del);
		ObjShot_SetSpellResist(obj, 1);
		ObjShot_SetIntersectionEnable(obj, 0);
		Split(obj);
		PlaySound(GetCurrentScriptDirectory ~ "default_system/se/spirit.wav", 65, 0);
		
		task Split(obj){
		wait(del+1);
		while(ObjMove_GetSpeed(obj) != 0){yield;}
		let random = rand(0,360);
		if(!Obj_IsDeleted(obj)){
		ascent(i in 0..5){
		FireShotA2(GetX(obj), GetY(obj), 0, ang+i*360/5, 0.01, 2, STAR_S_YELLOW, 10);
		}
		ascent(i in 0..3){
		Newlaser(GetX(obj), GetY(obj), ang+i*360/3, 500, 25, 45, LASER_C_YELLOW_ADD, 120);
		}
		PlaySound(GetCurrentScriptDirectory ~ "default_system/se/laser1.wav", 65, 120);
		SplitStar3(GetX(obj), GetY(obj), spd, ang+90, -0.06, 0, del);
		SplitStar3(GetX(obj), GetY(obj), spd, ang-90, -0.06, 0, del);
		}
		Obj_Delete(obj);
		}
	}
	task SplitStar3(x, y, spd, ang, acc, mspd, del){
		let obj = FireShotA2(x, y, spd, ang, acc, mspd, STAR_M_ORANGE_ADD, del);
		ObjShot_SetSpellResist(obj, 1);
		ObjShot_SetIntersectionEnable(obj, 0);
		Split(obj);
		PlaySound(GetCurrentScriptDirectory ~ "default_system/se/spirit.wav", 65, 0);
		
		task Split(obj){
		wait(del+1);
		while(ObjMove_GetSpeed(obj) != 0){yield;}
		if(!Obj_IsDeleted(obj)){
		ascent(i in 0..5){
		FireShotA2(GetX(obj), GetY(obj), 0, ang+i*360/5, 0.01, 2, STAR_S_YELLOW, 10);
		}
		PlaySound(GetCurrentScriptDirectory ~ "default_system/se/smbullet.wav", 65, 0);
		}
		Obj_Delete(obj);
		}
	}

task SpellReimu{
let c = 0;
	loop{
	let random = rand(0, 360);
	ascent(j in 0..8){
	ReimuSpread(j*360/8, 2.5, 4);
	wait(3);
	}
	if(c == 1){Rings;}
	c++;
	wait(60*7);
	}
}

task Rings{
let a = 0;
loop{
ascent(j in 0..18){
let obj = FireShotA2(BossX, BossY, 4, a+j*360/18, -0.07, 0.5, COIN_YELLOW, 10);
ObjMove_AddPatternB2(obj, 60, NO_CHANGE, NO_CHANGE, 0, 0.01, 10, 10);
}
a+=5;
PlaySound(GetCurrentScriptDirectory ~ "default_system/se/smbullet.wav", 65, 0);
wait(60);
}
}

task ReimuSpread(ang, spd, mult){
let rad = 5;
	PlaySound(GetCurrentScriptDirectory ~ "Default_System/se/labullet.wav", 90, 0);
	ascent(j in 1..8){
		ascent(k in 0..5){
		let obj = FireShotA2(BossX, BossY, spd+j/mult, ang+10-k*5, -0.05, 0, COIN_RED, 10);
		Redirect(obj, k, j);
		}
	}
	
	PlaySound(GetCurrentScriptDirectory ~ "default_system/se/labullet.wav", 65, 0);
	
	task Redirect(obj, k, j){
	let angle = ObjMove_GetAngle(obj);
	wait(60);
	ObjShot_SetGraphic(obj, COIN_WHITE);
	wait(60+24*j);
	if(!Obj_IsDeleted(obj)){CreateShotA2(GetX(obj), GetY(obj), 0, GetAngleToPlayer(obj), 0.04, 3, COIN_WHITE, 0);}
	ObjMove_AddPatternA2(obj, 0, 0, angle+180+(j*5)-k*(j*5/2), 0.02, 0, 2-j/6);
	PlaySound(GetCurrentScriptDirectory ~ "default_system/se/spirit.wav", 65, 0);
	ObjShot_SetGraphic(obj, COIN_RED);
	}
}

task SpellSanae{
wait(120);
let rad = 50;
let ang = 270;
let angle = 0;
Packets;
loop{
MoveStar(rad, ang, 1, angle, STAR_M_BLUE_ADD);
PlaySound(GetCurrentScriptDirectory ~ "default_system/se/spirit.wav", 65, 0);
angle+=360/24;
wait(90);
}
}

task Packets{
wait(120);
loop{
let a = 0;
descent(i in -1..2){
let obj = Packet(BossX, BossY);
ObjMove_SetDestAtWeight(obj, GetCenterX-150*i, GetCenterY-50*i, 10, 5);
Explode(obj, a);
a++;
wait(5);
}
a = 0;
wait(60*5);
descent(i in -1..2){
let obj = Packet(BossX, BossY);
ObjMove_SetDestAtWeight(obj, GetCenterX+150*i, GetCenterY-50*i, 10, 5);
Explode(obj, a);
a++;
wait(5);
}
wait(60*5);
}
}

task Explode(obj, a){
wait(90+a*60);
let random = truncate(rand(0, 100));
let X = GetX(obj);
let Y = GetY(obj);
if(random < 20){Explode0(X, Y); PacketExplode(X, Y, 0); Girl(X, Y, 0)}
if(random >= 20 && random < 70){Explode1(X, Y);PacketExplode(X, Y, 1); Girl(X, Y, 1)}
if(random >= 70 && random < 95){Explode2(X, Y);PacketExplode(X, Y, 2); Girl(X, Y, 2)}
if(random >= 95){Explode3(X, Y);PacketExplode(X, Y, 2); Girl(X, Y, 3)}
Obj_Delete(obj);

task Explode0(X, Y){
let random = rand(0,360);
	ascent(i in 0..8){
	FireShotA1(X, Y, 0.5, random+i*360/8, RICE_S_WHITE, 10);
	}
	PlaySound(GetCurrentScriptDirectory ~ "default_system/se/smbullet.wav", 65, 0);
}

task Explode1(X, Y){
let random = rand(0,360);
ascent(j in 0..3){
	ascent(i in 0..16){
	FireShotA1(X, Y, 1+1*j/2, random+i*360/16, RICE_S_WHITE, 10);
	}
}
PlaySound(GetCurrentScriptDirectory ~ "default_system/se/labullet.wav", 65, 0);
}

task Explode2(X, Y){
let random = rand(0,360);
ascent(j in 0..3){
	ascent(i in 0..20){
	FireShotA1(X, Y, 0.5+0.25*j/2, random+i*360/20, RICE_S_WHITE, 10);
	}
}
	ascent(j in 0..2){
		ascent(i in 0..32){
		FireShotA1(X, Y, 1+0.5*j/2, random+i*360/32+5.625*j, FIREBALL_RED_ADD, 10);
		}
	}
PlaySound(GetCurrentScriptDirectory ~ "default_system/se/smbullet.wav", 65, 0);
PlaySound(GetCurrentScriptDirectory ~ "default_system/se/explode.wav", 65, 0);
}

task Explode3(X, Y){
let random = rand(0,360);
	ascent(j in 0..6){
		ascent(i in 0..24){
		FireShotA1(X, Y, 1+1*j/2, random+i*360/24, RICE_S_WHITE, 10);
		}
	}
	PlaySound(GetCurrentScriptDirectory ~ "default_system/se/smbullet.wav", 65, 0);
	ascent(i in 0..64/4){
		ascent(j in 0..5){
		FireShotA1(X, Y, 3, GetAngleToPoint(X, Y, GetPlayerX, GetPlayerY)+j*360/5+72/2, ORB_M_BLUE_ADD, 10);
		}
		ascent(j in 0..5){
		FireShotA1(X, Y, 2, GetAngleToPoint(X, Y, GetPlayerX, GetPlayerY)+j*360/5+i*11.1122, ORB_R_BLUE, 10);
		}
	PlaySound(GetCurrentScriptDirectory ~ "default_system/se/spirit.wav", 65, 0);
	wait(8);
	}
}

}

let del = 4;

task MoveStar(rad, ang, spd, ang2, graph){
let obj = Enemee(BossX, BossY);
ObjMove_SetPosition(obj, BossX+rad*cos(ang), -100+BossY+rad*sin(ang));
Shoot(obj, spd, graph, ang2);
let points = 5;
let pointangle = 360/points+360/points;
	ascent(i in 1..points+1){
	ObjMove_SetDestAtFrame(obj, BossX+rad*cos(ang+(pointangle)*i), -100+BossY+rad*sin(ang+(pointangle)*i), del);
	wait(del);
	}
}

task Shoot(obj, spd, graph, ang2){
	ascent(j in 0..del*5){
		ascent(i in 0..8){
		let pointer = CreateShotA1(BossX, BossY-100, 0,0,0,0);
		let obj2 = FireShotA1(GetX(obj), GetY(obj), 0, 0, graph, 10);
		ObjMove_AddPatternA2(pointer, del*5+30-j, 0, 270+i*360/8+ang2, 0.015, 0,3);
		Follow(obj2, pointer, del*5+30-j);
		}
	yield;
	}
	
	task Follow(obj2, pointer, time){
	wait(time);
	let rad = GetObjectDistance(obj2, pointer);
	let angle = GetAngleToObject(obj2, pointer);
	while(!Obj_IsDeleted(pointer)){
	ObjMove_SetPosition(obj2, GetX(pointer)+rad*cos(angle), GetY(pointer)+rad*sin(angle));
	angle++;
	yield;
	}
	Obj_Delete(obj2);
	}
}

task end{
while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
	SetCommonData("bossx", BossX);
	SetCommonData("bossy", BossY);
	SetCommonData("RipSpell", 1);
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
	
} 


