#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["S2"]
#Text["."]
#System["./System.txt"]
#include"./Functions/Index.txt"

let objScene=GetEnemyBossSceneObjectID();
let objBoss;
let objMud;
let objMarsh;
let frame=0;
let wsignal=1;
let mSignal=1;
let bossSignal=0;
let GCSD=GetCurrentScriptDirectory~"";
let plusangle=-90;

@Initialize{
	mainTask;
}

@Event{
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){
		SetScriptResult(3500)
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(105)
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(10000)
	}
}

@MainLoop{
	frame++;
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),24);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),26);
	yield;
}

task mainTask{
	TLoadSounds;
	objBoss=TDrawBoss;
	ObjEnemy_SetDamageRate(objBoss,0,0);
	wait(30);
	PlaySound(objBoss,23,100,0);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,GetStgFrameHeight/2,10,5);
	ObjEnemyBossScene_StartSpell(objScene);
	Cutin(objBoss,"Summon \"Mudfishes From The Atchafalaya Basin\"");
	TShoot01;
	TBossShoot01;
	TEnd;
	yield;
}

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		if(!Obj_IsDeleted(objMud)){
			TExplosionA(ObjMove_GetX(objMud),ObjMove_GetY(objMud),10,0.5);
			Obj_Delete(objMud);
		}
		if(!Obj_IsDeleted(objMarsh)){
			TExplosionA(ObjMove_GetX(objMarsh),ObjMove_GetY(objMarsh),10,0.5);
			Obj_Delete(objMarsh);
		}
		TExplosionA(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),10,0.5);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		Obj_Delete(objBoss);
		loop(120){
			DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);
			yield;
			}
		CloseScript(GetOwnScriptID());
		return;
	}

task TWater(obj){
	let waterpos;
	waterpos=TWaterHide(obj,"LARGE",false);
	while(!Obj_IsDeleted(waterpos)&&wsignal==1){
		ObjRender_SetPosition(waterpos,ObjRender_GetX(obj),ObjRender_GetY(obj),0);
		yield;
	}
	Obj_Delete(waterpos);
	ObjRender_SetAlpha(obj,255);
	loop(5){
		TCallBubbles(ObjMove_GetX(obj),ObjMove_GetY(obj));
	}
	PlaySoundNR(0,100,0);
}

task TShoot01{
	wait(120);
	let k=1;
	let aObj=[];
	let bObj=[];
	let a=4;
	let b=3;
	let angle=0;
	let xAngle=0;
	while(angle<360){
		let obj=CreateShotA2(ObjMove_GetX(objBoss)+50*cos(angle),ObjMove_GetY(objBoss)+50*sin(angle),5,angle,-0.1,0,202,5);
		ObjMove_SetAngularVelocity(obj,15);
		aObj=aObj~[obj];
		TAction(obj);
		angle+=360/45;
		PlaySound(objBoss,12,100,0);
		yield;
	}
	ascent(i in 0..length(aObj)){
		while(ObjMove_GetSpeed(aObj[i])>0){ yield; }
	}
	k=2;
	let sAngle=0;
	ascent(i in 0..length(aObj)){
		if(!Obj_IsDeleted(aObj[i])){
			let angle2=0;
			while(angle2<360){
				let obj=CreateShotA2(ObjMove_GetX(aObj[i]),ObjMove_GetY(aObj[i]),0,sAngle*150+angle2+rand(-45,45),0.05,1.5,204,0);
				angle2+=360/2;
			}
		}
		sAngle++;
	}
	task TAction(obj){
		while(k==1){ yield; }
		ObjShot_SetGraphic(obj,195);
		ObjMove_SetAngularVelocity(obj,0);
		ObjMove_SetAcceleration(obj,0.05);
		ObjMove_SetMaxSpeed(obj,3);
		ObjMove_SetAngle(obj,atan2(ObjMove_GetY(objBoss)-ObjMove_GetY(obj),ObjMove_GetX(objBoss)-ObjMove_GetX(obj)));
		while(ObjMove_GetSpeed(obj)<3){ yield; }
		ObjMove_SetAcceleration(obj,-0.075);
		ObjMove_SetMaxSpeed(obj,0);
		while(ObjMove_GetSpeed(obj)>0){ yield; }
		if(!Obj_IsDeleted(obj)){
			let angle2=0;
			while(angle2<360){
				CreateShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0,GetAngleToPlayer(objBoss)+angle2+(xAngle*120),0.075,2,204,0);
				xAngle++;
				angle2+=360/4;
			}
		}
		ObjMove_SetAcceleration(obj,0.025);
		ObjMove_SetMaxSpeed(obj,3);
	}
	wait(30);
	PlaySound(objBoss,22,100,0);
	TCharge01(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),3,255,255,255);
	wait(90);
	ObjMove_SetDestAtWeight(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss)-525,5,15);
//mud and marsh appearance
	objMud=TDrawMudkip(GetStgFrameWidth/3*2,-200);
	TWater(objMud);
	objMarsh=TDrawMarsh(GetStgFrameWidth/3,-200);
	TWater(objMarsh);
	TMudShoot01;
	TMarshShoot01;
}

task TMudShoot01{
	ObjMove_SetDestAtWeight(objMud,GetStgFrameWidth/3,GetStgFrameHeight/3+25,10,15);
	while(ObjMove_GetSpeed(objMud)>0){ yield; }
	ObjMove_SetDestAtWeight(objMud,GetStgFrameWidth/3,GetStgFrameHeight/3,10,15);
	while(ObjMove_GetSpeed(objMud)>0){ yield; }
	wsignal=0;
	wait(30);
	let t=0;
	TMovement(objMud);
	while(!Obj_IsDeleted(objMud)){
		CreateShotA2(ObjMove_GetX(objMud)+rand(-50,50),ObjMove_GetY(objMud)+rand(-50,50),10,t*6,-0.55,2,DOT_BLUE,15);
		if(bossSignal==1){
			if(t%4==0){
				if(ObjMove_GetSpeed(objMud)==0){
					let angle=-45;
					while(angle<=45){
						let obj=CreateShotA2(ObjMove_GetX(objMud),ObjMove_GetY(objMud),10,angle+t*5.1,-0.5,2,PELLET_BLUE,15);
						TCheck(obj);
						angle+=10;
					}
					PlaySound(objBoss,12,100,0);
				}
			}
		}
		t++;
		wait(1);
	}
	ObjEnemy_AddLife(objMarsh,500);
	bossSignal+=1;
	task TCheck(obj){
		let signal=1;
		while(!Obj_IsDeleted(obj)){
			if(ObjMove_GetX(obj)<0||ObjMove_GetX(obj)>GetStgFrameWidth&&signal==1){
				ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));
				signal++;
			}
			if(ObjMove_GetY(obj)<0&&signal==1){
				ObjMove_SetAngle(obj,360-ObjMove_GetAngle(obj));
				signal++;
			}
			yield;
		}
	}
}

task TMarshShoot01{
	ObjMove_SetDestAtWeight(objMarsh,GetStgFrameWidth/3*2,GetStgFrameHeight/3+25,10,15);
	while(ObjMove_GetSpeed(objMarsh)>0){ yield; }
	ObjMove_SetDestAtWeight(objMarsh,GetStgFrameWidth/3*2,GetStgFrameHeight/3,10,15);
	while(ObjMove_GetSpeed(objMarsh)>0){ yield; }
	wait(30);
	let t=0;
	TMovement(objMarsh);
	TI;
	TShot01;
	while(!Obj_IsDeleted(objMarsh)){
		PlaySound(objBoss,9,100,0);
		loop(5){
			let angle=0;
			while(angle<360){
				let x=ObjMove_GetX(objMarsh)+50*cos(angle+sin(t)*90);
				let y=ObjMove_GetY(objMarsh)+50*sin(angle+sin(t)*90);
				let obj=CreateShotA2(x,y,10,angle+cos(t)*90,-0.75,1,FIRE_BLUE,5);
				ObjMove_AddPatternA2(obj,60,NO_CHANGE,NO_CHANGE,0.0475,0,5);
				angle+=60;
			}
		wait(2);
		}
		wait(50);
	}
	ObjEnemy_AddLife(objMud,250);
	bossSignal+=1;
	task TI{
		while(!Obj_IsDeleted(objMarsh)){
			if(bossSignal==1){
				if(t%45==0){
					let angle=0;
					while(angle<360){
						let obj=CreateShotA2(ObjMove_GetX(objMarsh),ObjMove_GetY(objMarsh),10,angle+GetAngleToPlayer(objMarsh),-0.5,3,G_LARGE_BLUE,15);
						TCheck(obj);
						angle+=24;
					}
				}
			}
			t+=0.5;
			yield;
		}
	}
	task TShot01{
		while(!Obj_IsDeleted(objMarsh)){
			let x=ObjMove_GetX(objMarsh);
			let y=ObjMove_GetY(objMarsh);
			let dir=GetAngleToPlayer(objMarsh);
			let gap=360/10;
			while(gap<360){
				let angle=-gap;
				while(angle<gap){
					let obj=CreateShotA2(x,y,0,dir+angle,0.15,5,ARROWHEAD_BLUE,25);
					angle+=12;
				}
				gap+=360/10;
				PlaySound(objBoss,8,100,0);
				wait(2);
			}
			let angle=0;
			while(angle<360){
				let obj=CreateShotA2(x,y,0,dir+angle,0.15,5,ARROWHEAD_BLUE,25);
				TLaser01(obj,ObjMove_GetX(obj),ObjMove_GetY(obj),ObjMove_GetAngle(obj));
				angle+=12;
			}
			PlaySound(objBoss,18,100,0);
			wait(105);
		}
		task TLaser01(obj,x,y,dir){
			let shot=CreateStraightLaserA1(x,y,dir,700,10,0,ST_LASER_PURPLE,120);
			while(!Obj_IsDeleted(obj)){
				ObjMove_SetPosition(shot,ObjMove_GetX(obj),ObjMove_GetY(obj));
				if(Obj_IsDeleted(obj)){
					break;
					Obj_Delete(shot);
				}
				yield;
			}
		}
	}
	task TCheck(obj){
		let signal=1;
		while(!Obj_IsDeleted(obj)){
			if(ObjMove_GetX(obj)<=0||ObjMove_GetX(obj)>=GetStgFrameWidth&&signal==1){
				ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));
				signal++;
			}
			if(ObjMove_GetY(obj)<=0&&signal==1){
				ObjMove_SetAngle(obj,360-ObjMove_GetAngle(obj));
				signal++;
			}
			yield;
		}
	}
}

task TMovement(obj){
	wait(120);
	while(ObjEnemy_GetInfo(obj,INFO_LIFE)>0){
		ObjMove_SetDestAtWeight(obj,GetStgFrameWidth/2+rand(-150,150),GetStgFrameHeight/3+rand(-75,25),15,5);
		while(ObjMove_GetSpeed(obj)>0){ yield; }
		wait(120);
	}
}

task TBossShoot01{
	while(bossSignal<2&&ObjEnemyBossScene_GetInfo(objScene,INFO_TIMER)>=25){ yield; }
	if(bossSignal>=2){
		TEnemyHitboxStart(objBoss,150);
		ObjMove_SetDestAtWeight(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss)+525,10,15);
		while(ObjMove_GetSpeed(objBoss)>0){ yield; }
		ObjMove_SetDestAtWeight(objBoss,ObjMove_GetX(objBoss),GetStgFrameHeight/3,15,15);
		while(ObjMove_GetSpeed(objBoss)>0){ yield; }
		PlaySound(objBoss,22,100,0);
		TCharge01(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),3,255,255,255);
		wait(90);
		let t=0;
		let tt=0;
		let a=0;
		TI;
		TShoot01;
		TShoot02;
		TSound;
		while(!Obj_IsDeleted(objBoss)){
			let angle=0;
			while(angle<360){
				CreateShotA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),2,t*24.13+angle,DROP_BLUE,0);
				angle+=360/3;
			}
			wait(2);
		}
		task TI{
			while(!Obj_IsDeleted(objBoss)){
				t+=a;
				tt+=3;
				if(a<0.25){
					a+=0.0075;
				}
				yield;
			}
		}
		task TSound{
			while(!Obj_IsDeleted(objBoss)){
				PlaySound(objBoss,12,100,0);
				wait(20);
			}
		}
		task TShoot01{
			tt=0;
			while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>2500){ yield; }
			while(!Obj_IsDeleted(objBoss)){
				let angle=0;
				while(angle<360){
					CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),5,angle+tt*6.1,-0.5,sin(tt)*1+1.5,KUNAI_BLUE,5);
					angle+=360/3;
				}
				wait(3);
			}
		}
		task TShoot02{
			let bullets=[1.5,1.6,1.7,1.8,1.9,2.0,2.1,2.2,2.3,2.4,2.5];
			while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>1500){ yield; }
			while(!Obj_IsDeleted(objBoss)){
				let angle=0;
				while(angle<360){
					ascent(i in 0..length(bullets)){
						CreateShotA2(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),10,angle+GetAngleToPlayer(objBoss),-0.5,bullets[i],PELLET_AQUA,25);
					}
					angle+=360/15;
				}
				PlaySound(objBoss,9,100,0);
				wait(60);
			}
		}
	}else{
		TEnemyHitboxStart(objBoss,150);
		ObjMove_SetDestAtWeight(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss)+525,10,15);
		while(ObjMove_GetSpeed(objBoss)>0){ yield; }
		ObjMove_SetDestAtWeight(objBoss,ObjMove_GetX(objBoss),GetStgFrameHeight/3,15,15);
		while(ObjMove_GetSpeed(objBoss)>0){ yield; }
		PlaySound(objBoss,22,100,0);
		TCharge01(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),3,255,255,255);
		wait(90);
		let t=0;
		let a=0;
		TI;
		while(!Obj_IsDeleted(objBoss)){
			let angle=0;
			while(angle<360){
				CreateShotA1(ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),2,t*29.13+angle,DROP_BLUE,0);
				angle+=360/3;
			}
			wait(2);
		}
		task TI{
			while(!Obj_IsDeleted(objBoss)){
				t+=a;
				if(a<0.25){
					a+=0.0075;
				}
				yield;
			}
		}
	}
}

function GravityShot(x,y,speed,angle,grvSpeed,grvAngle,graphic,delay){
	let xSpeed=speed*cos(angle);
	let ySpeed=speed*sin(angle);
	let accXSpeed=grvSpeed*cos(grvAngle);
	let accYSpeed=grvSpeed*sin(grvAngle);
	let maxXSpeed=[99,-99][accXSpeed<0];
	let maxYSpeed=[99,-99][accYSpeed<0];
	let gs=CreateShotB2(x,y,xSpeed,ySpeed,accXSpeed,accYSpeed,maxXSpeed,maxYSpeed,graphic,delay);
	return gs;
}