#TouhouDanmakufu
#Title[SatoriNonSpell H]
#Text[Nonspell]
#BackGround[User(.\img\stage.png,0,-0.5)]
#PlayLevel[Extra]
#Player[FREE]
#BGM[.\yamabiko.mp3]
#ScriptVersion[2]

script_enemy_main
{
#include_function ".\Library.dnh"

let SatoriSprite = csd~"img/Satori/SatoriSprite.png";
let SatoriCutin = csd~"img/Satori/SatoriCutin.png";
let SpellBG01 = csd~"img/Satori/SpellBG01.png";
let SpellBG02 = csd~"img/Satori/SpellBG02.png";
let shot1=csd~"se/shot1.wav";
let shot2=csd~"se/shot2.wav";
let shot3=csd~"se/shot3.wav";
let shot4=csd~"se/shot4.wav";
let spell=csd~"se/spellcard.wav";
let laser=csd~"se/lazer.wav";
let charge=csd~"se/charge2.wav";

@Initialize
	{
	LoadUserShotData(csd~"Shot.txt");
	LoadGraphic(SatoriSprite);
	LoadGraphic(SpellBG01);
	LoadGraphic(SpellBG02);
	LoadSE(shot1);
	LoadSE(shot2);
	LoadSE(shot3);
	LoadSE(shot4);
	LoadSE(spell);
	LoadSE(laser);
	LoadSE(charge);
	SetLife(400);
	SetScore(100);
	SetEnemyMarker(true);
	SetDamageRate(5,0);
	SetTimer(71);
	CutIn(YOUMU,"Awakening Ceremonial -- Drum Rocks",SatoriCutin,0,0,256,512);
	//let a=0;
	SetEffectForZeroLife(60,0,0);	
	

MainTask;
Vulnerable;
Timer;
IdleSatori;
//MoveAbout;
SetMovePosition03(GetCenterX, GetCenterY - 40, 30,10);
}

task Vulnerable
{
Wait(600);
SetDamageRate(10,0);
}



task LaserWall(x,y,a,d,t){
	let obj=Obj_Create(OBJ_LASER);
	let obj2=Obj_Create(OBJ_LASER);
	Obj_SetCollisionToPlayer(obj,false);
	Obj_SetCollisionToPlayer(obj2,false);
	ObjLaser_SetWidth(obj,10);
	ObjLaser_SetLength(obj,500);
	ObjLaser_SetWidth(obj2,10);
	ObjLaser_SetLength(obj2,500);
	Obj_SetX(obj,x);
	Obj_SetY(obj,y);
	Obj_SetX(obj2,x);
	Obj_SetY(obj2,y);
	Obj_SetSpeed(obj,0);
	Obj_SetAngle(obj,a+90);
	Obj_SetSpeed(obj2,0);
	Obj_SetAngle(obj2,a-90);
	ObjShot_SetGraphic(obj,t);
	ObjShot_SetGraphic(obj2,t);
	ObjShot_SetDelay(obj,0);
	let ti=(0)*0.5;
	let u=0;
	let X=GetPlayerX;
	let Y=GetPlayerY;
	while(!Obj_BeDeleted(obj)){
		yield;
		ti=ti+0.5;
		u++;
		Obj_SetX(obj,x+ti*cos(a));
		Obj_SetY(obj,y+ti*sin(a));
		Obj_SetX(obj2,x+ti*cos(a));
		Obj_SetY(obj2,y+ti*sin(a));
		if(u==d-30){Obj_Delete(obj);Obj_Delete(obj2);}
		
	}
}

task Exlaser(x,y,a,n,find,gn,t){
	let obj=Obj_Create(OBJ_LASER);
	ObjLaser_SetSource(obj,false);
	Obj_SetCollisionToPlayer(obj,true);
	ObjLaser_SetWidth(obj,10);
	ObjLaser_SetLength(obj,500);
	Obj_SetX(obj,x+10*cos(a));
	Obj_SetY(obj,y+10*sin(a));
	Obj_SetSpeed(obj,0);
	Obj_SetAngle(obj,a);
	ObjShot_SetGraphic(obj,7);
	ObjShot_SetDelay(obj,30);
	let u=0;
	let X=x;
	let Y=y;
	while(X>24 && X<424 && Y>24 && Y<466){X=X+cos(a);Y=Y+sin(a);}
	while(!Obj_BeDeleted(obj)){
		yield;
		u++;
		if(u==60){ascent(i in 0..n){Shard(X,Y)};
		if(find==0){Gold(X,Y,gn,t);}
		Obj_Delete(obj);}
		
	}
}

task Gold(x,y,gn,t){
	let obj=Obj_Create(OBJ_SHOT);
	Obj_SetX(obj,x);
	Obj_SetY(obj,y);
	Obj_SetSpeed(obj,0);
	ObjShot_SetGraphic(obj,207);
	ObjShot_SetDelay(obj,0);
	let u=t;
	let v=0;
	let ph=0;
	let k=1;
	while(!Obj_BeDeleted(obj)){
		yield;
		u++;
		v=v+1/(1+4*sin(u)^2)*sin(u/3.4);
		if(ph==0 && u==600){ph=1; u=0; v=0;}
		if(ph==1){Obj_SetX(obj,k*x+(1-k)*(GetX+60*cos(3*u+360/5*gn)));
				Obj_SetY(obj,k*y+(1-k)*(GetY+60*sin(3*u+360/5*gn)));
				k=0.998*(k)^1.04;
				if(u==300){ph=2;u=0;}
				}
		if(ph==2){Obj_SetX(obj,(1)*(GetX+(60+180*sin(u))*cos(3*v+360/5*gn)));
				Obj_SetY(obj,(1)*(GetY+(60+180*sin(u))*sin(3*v+360/5*gn)));
				if(u%7==0){CreateShot01(Obj_GetX(obj),Obj_GetY(obj),1,3*v+360/5*gn,94,5);
				CreateShot01(Obj_GetX(obj),Obj_GetY(obj),1,180+(3*v+360/5*gn),94,5)};
				}
				
	}
}


task Shard(x,y){
	let obj=Obj_Create(OBJ_SHOT);
	Obj_SetX(obj,x);
	Obj_SetY(obj,y);
	Obj_SetSpeed(obj,0);
	ObjShot_SetGraphic(obj,82);
	ObjShot_SetDelay(obj,0);
	let u=0;
	let X=rand(-2,2);
	let Y=rand(-2,2);
	while(!Obj_BeDeleted(obj)){
		yield;
		u++;
		Obj_SetX(obj,x+X*u);
		Obj_SetY(obj,y+Y*u+0.02*u*u);
		Obj_SetAngle(obj,X^2+5*u);
	}

}


task Rip(x,y,R,a,s,t){
	let obj=Obj_Create(OBJ_SHOT);
	Obj_SetX(obj,x+R*cos(a));
	Obj_SetY(obj,y+R*sin(a));
	Obj_SetSpeed(obj,0);
	ObjShot_SetGraphic(obj,82);
	ObjShot_SetDelay(obj,30);
	Obj_SetAngle(obj,a);
	let u=0;
	let v=0;
	let w=0;
	let ut=0;
	let ph=0;
	let r=R;
	let X=rand(-2,2);
	let Y=rand(-2,2);
	while(!Obj_BeDeleted(obj)){
		yield;
		u++;
		v++;
		w++;
		ut++;
		Obj_SetX(obj,x+r*cos(a));
	Obj_SetY(obj,y+r*sin(a));
		if(ph==0 && u>=200+30*cos(a)){ph=1;u=0}
		if(ph==1){r=R-R/30*sin(6*u);}
		if(ph==1 && v>=400+30*cos(a-60)){ph=2;v=0;}
		if(ph==2){r=R-R/30*sin(6*u)-R/30*sin(12*(v));}
		if(ph==2 && w>=600+30*cos(a-120)){ph=3;w=0;}
		if(ph==3){r=R-R/30*sin(6*u)-R/30*sin(12*(v))-R/30*sin(18*(w));}
		if(ut==800){CreateShot01(Obj_GetX(obj),Obj_GetY(obj),rand(1,2),a+rand(-10,10),84,40);
		CreateShot01(Obj_GetX(obj),Obj_GetY(obj),rand(3,4),180+a+rand(-10,10),94,40);
		Obj_Delete(obj);}
	}

}


task Boom(x,y,let s,a,acc,ms,t,d,R,s2,n2){
let obj=Obj_Create(OBJ_SHOT);
	Obj_SetX(obj,x);
	Obj_SetY(obj,y);
	Obj_SetSpeed(obj,s);
	ObjShot_SetGraphic(obj,t);
	ObjShot_SetDelay(obj,d);
	Obj_SetAngle(obj,a);
	let u=0;
	while(!Obj_BeDeleted(obj)){
		yield;
		if(s<ms){s+=acc;}
		Obj_SetSpeed(obj,s);
		if((Obj_GetX(obj)-GetX)^2+(Obj_GetY(obj)-GetY)^2 < R*R){
		let ra=rand(0,360);
		ascent(i in 0..n2){CreateShot01(Obj_GetX(obj),Obj_GetY(obj),rand(0.5,1.5)*s2,rand(0,360),229,15)}
		ascent(i in 0..n2){CreateShot01(Obj_GetX(obj),Obj_GetY(obj),rand(0.5,1.5)*s2*0.8,rand(0,360),202,30)}
		Obj_Delete(obj);}
		}


}


task Sonar(x,y,s,a,t,da,n){
	ascent(j in 0..n){ascent(i in -3..4){Bouncy(x,y,s,a+i*da,t,j*5)}}
	let d=0;
	let e=0;
	let ctr=0;
	loop{
	ctr++;
	d=(GetPlayerX-x)^2+(GetPlayerY-y)^2;
	let d2=(48-GetPlayerX-x)^2+(GetPlayerY-y)^2;
	let d3=(848-GetPlayerX-x)^2+(GetPlayerY-y)^2;
	e=atan2((GetPlayerY-y),GetPlayerX-x);
	let e2=atan2((GetPlayerY-y),48-GetPlayerX-x);
	let e3=atan2((GetPlayerY-y),848-GetPlayerX-x);
	if(d<ctr*ctr*s*s && d>(ctr*s-(n-1)*5)^2 && e>a-3*da && e<a+4*da){
	Bouncy(x,y,s,a,230,30);break; 
	}
	if(d2<ctr*ctr*s*s && d2>(ctr*s-(n-1)*5)^2 && e2>a-3*da && e2<a+4*da){
	Bouncy(x,y,s,a,230,30);break; 
	}
	if(d3<ctr*ctr*s*s && d3>(ctr*s-(n-1)*5)^2 && e3>a-3*da && e3<a+4*da){
	Bouncy(x,y,s,a,230,30);break; 
	}
	if(ctr==600){break}
	yield;}
}

task Bouncy(x,y,s,a,t,d){
let obj=Obj_Create(OBJ_SHOT);
	Obj_SetX(obj,x);
	Obj_SetY(obj,y);
	Obj_SetSpeed(obj,s);
	ObjShot_SetGraphic(obj,t);
	ObjShot_SetDelay(obj,d);
	Obj_SetAngle(obj,a);
	let u=-d;;
	let X=x;
	let Y=y;
	while(!Obj_BeDeleted(obj)){
		yield;
		u++;
		if(u>=0){X=x+u*s*cos(a);
		Y=y+u*s*sin(a);
		if(X<24){X=48-X;Obj_SetAngle(obj,180-a);}
		else{if(X>424){X=848-X;Obj_SetAngle(obj,180-a);}}
		if(X<24 || X>424 || Y<24 || Y>466){Obj_Delete(obj)}
		Obj_SetX(obj,X);
	Obj_SetY(obj,Y);
		}
}

}

task Vajra(t1,t2,angle,charge){
let flag=0;
let a=0;
let obj=Obj_Create(OBJ_LASER);
ObjLaser_SetLength(obj,600);
ObjLaser_SetWidth(obj,5);
Obj_SetX(obj,GetX);
	Obj_SetY(obj,GetY);
	Obj_SetSpeed(obj,0);
	ObjShot_SetGraphic(obj,t1);
	ObjShot_SetDelay(obj,60);
	Obj_SetAngle(obj,angle);
//let obja=Obj_Create(OBJ_LASER);
//ObjLaser_SetLength(obja,600);
//ObjLaser_SetWidth(obja,5);
//Obj_SetX(obja,GetX);
	//Obj_SetY(obja,GetY);
	//Obj_SetSpeed(obja,0);
	//ObjShot_SetGraphic(obja,4);
	//ObjShot_SetDelay(obja,60);
	//Obj_SetAngle(obja,180);
loop(60){yield;}
loop{
let x=rand(80,370);
let y=rand(120,360);
let X=Obj_GetX(obj);
let Y=Obj_GetY(obj);
let t=0;
loop(120){
	t=t+1/120;
	Obj_SetAngle(obj,Obj_GetAngle(obj)+1/2);
	Obj_SetX(obj,t*x+(1-t)*X);
	Obj_SetY(obj,t*y+(1-t)*Y);
	//Obj_SetAngle(obja,Obj_GetAngle(obja)+1/2);
	//Obj_SetX(obja,t*x+(1-t)*X);
	//Obj_SetY(obja,t*y+(1-t)*Y);
	yield;
	}
loop(120){Obj_SetAngle(obj,Obj_GetAngle(obj)+1/2);
//Obj_SetAngle(obja,Obj_GetAngle(obja)+1/2);
if(a%2==0){Amp(obj,224,240,1.5,360*sin(2*a)+angle,t2);}
a++;
yield;}
if(ObjLaser_GetWidth(obj)>40){
	
	loop{yield;if((Obj_GetAngle(obj)-angle)%360==0){break}
	x=GetX+100*cos(angle);
	y=GetY+100*sin(angle);
	X=Obj_GetX(obj);
	Y=Obj_GetY(obj);
	Obj_SetX(obj,0.01*x+0.99*X);
	Obj_SetY(obj,0.01*y+0.99*Y);
	Obj_SetAngle(obj,Obj_GetAngle(obj)+1/2);
	}
	loop(60){yield;
	x=GetX+100*cos(angle);
	y=GetY+100*sin(angle);
	X=Obj_GetX(obj);
	Y=Obj_GetY(obj);
	Obj_SetX(obj,0.02*x+0.98*X);
	Obj_SetY(obj,0.02*y+0.98*Y);}
	Obj_SetAngle(charge,Obj_GetAngle(charge)+1);
	loop{yield;
	x=GetX+100*cos(angle);
	y=GetY+100*sin(angle);
	X=Obj_GetX(obj);
	Y=Obj_GetY(obj);
	Obj_SetX(obj,0.02*x+0.98*X);
	Obj_SetY(obj,0.02*y+0.98*Y);
	if(Obj_GetAngle(charge)>=2){break;}
	}
	ascent(i in 0..100){Obj_SetX(obj,GetX+(100-i+i*(100-i)/25)*cos(angle));
	Obj_SetY(obj,GetY+(100-i)*sin(angle));
	yield;}
	yield;
	ascent(i in -2..3){
		ascent(j in 0..101){
			CreateShot02(Obj_GetX(obj)+12*j*cos(Obj_GetAngle(obj)),Obj_GetY(obj)+12*j*sin(Obj_GetAngle(obj)),0,Obj_GetAngle(obj)+90+27*j+360/5*i,0.015,0.5+absolute(i)/3,t2,30);
						}	
						}
ObjLaser_SetWidth(obj,5);
//ObjLaser_SetWidth(obja,5);
loop(300){yield;}
Obj_SetAngle(charge,0);						
}
}	
loop{yield;}
}

task Amp(vaj,x,y,s,a,t){
let obj=Obj_Create(OBJ_SHOT);

	Obj_SetX(obj,x);
	Obj_SetY(obj,y);
	Obj_SetSpeed(obj,s);
	ObjShot_SetGraphic(obj,t);
	ObjShot_SetDelay(obj,0);
	Obj_SetAngle(obj,a);
	let u=0;
	while(!Obj_BeDeleted(obj)){
		yield;
		u++;
		let A=Obj_GetAngle(vaj);
		let X=Obj_GetX(vaj);
		let Y=Obj_GetY(vaj);
		if(absolute((Obj_GetX(obj)-X)*sin(A)-(Obj_GetY(obj)-Y)*cos(A))<2*s && ((Obj_GetX(obj)-X)*cos(A)+(Obj_GetY(obj)-Y)*sin(A)) >0){ObjLaser_SetWidth(vaj,ObjLaser_GetWidth(vaj)+0.6);
		//ObjLaser_SetWidth(vaja,ObjLaser_GetWidth(vaja)+4);
		Obj_Delete(obj);}
}
	
	
}

task Bouncers(let t){
		let obj = [0,0,0,0,0,0,0,0];
		let coll = [0,0,0,0,0,0,0,0];
		
		let a=0;
		
		let n=6;
		
		ascent(i in 0..n){
			obj[i]=Obj_Create(OBJ_SHOT);
			ObjShot_SetBombResist(obj[i], true);
			Obj_SetX(obj[i], 80+40*i);
			Obj_SetY(obj[i], rand(20,90));
        			ObjShot_SetGraphic(obj[i], 230);
			Obj_SetAngle(obj[i], 50-20*i+rand(-4,4));
			Obj_SetSpeed(obj[i],2);}
				while(t>0){
				yield;
				t--;
				ascent(i in 0..n){
				Obj_SetSpeed(obj[i],2+(Obj_GetSpeed(obj[i])-2)*0.90);
				if(Obj_GetY(obj[i])<20){Obj_SetY(obj[i],1+Obj_GetY(obj[i]));Obj_SetAngle(obj[i],-Obj_GetAngle(obj[i]))}
				if(Obj_GetY(obj[i])>460){Obj_SetY(obj[i],-1+Obj_GetY(obj[i]));Obj_SetAngle(obj[i],-Obj_GetAngle(obj[i]))}
				if(Obj_GetX(obj[i])<20){Obj_SetX(obj[i],1+Obj_GetX(obj[i]));Obj_SetAngle(obj[i],180-Obj_GetAngle(obj[i]))}
				if(Obj_GetX(obj[i])>430){Obj_SetX(obj[i],-1+Obj_GetX(obj[i]));Obj_SetAngle(obj[i],180-Obj_GetAngle(obj[i]))}
				if(coll[i]>0){coll[i]=coll[i]-1;}
					ascent(j in i+1..n){
					if((((Obj_GetY(obj[i])-Obj_GetY(obj[j]))^2+(Obj_GetX(obj[i])-Obj_GetX(obj[j]))^2)^0.5)<40){
						if(coll[j]==0){coll[j]=30;
						;a=Obj_GetAngle(obj[i]);
						Obj_SetAngle(obj[i],Obj_GetAngle(obj[j]));
						Obj_SetAngle(obj[j],a);
						Obj_SetSpeed(obj[i],5);Obj_SetSpeed(obj[j],5);PlaySE(shot2);
						ascent(k in 0..4){CreateShot01((Obj_GetX(obj[i])+Obj_GetX(obj[j]))/2,(Obj_GetY(obj[i])+Obj_GetY(obj[j]))/2,rand(1,3),rand(0,360),205,30)}
						ascent(k in 0..8){CreateShot01((Obj_GetX(obj[i])+Obj_GetX(obj[j]))/2,(Obj_GetY(obj[i])+Obj_GetY(obj[j]))/2,rand(1,3),rand(0,360),48,30)}
						ascent(k in 0..12){CreateShot01((Obj_GetX(obj[i])+Obj_GetX(obj[j]))/2,(Obj_GetY(obj[i])+Obj_GetY(obj[j]))/2,rand(1,3),rand(0,360),163,30)}}
						}
					}
				
			}
			}
			loop(75){t--;yield;ascent(i in 0..n){Obj_SetSpeed(obj[i],0);Obj_SetX(obj[i], Obj_GetX(obj[i])+5*(-1)^t);}}
		ascent(i in 0..n){let j=i;	PlaySE(shot2);		
						ascent(k in 0..6){CreateShot01((Obj_GetX(obj[i])),(Obj_GetY(obj[i])+Obj_GetY(obj[j]))/2,rand(1,3),rand(0,360),205,0)}
						ascent(k in 0..6){CreateShot01((Obj_GetX(obj[i])+Obj_GetX(obj[j]))/2,(Obj_GetY(obj[i])+Obj_GetY(obj[j]))/2,rand(1,3),rand(0,360),48,0)}
						ascent(k in 0..9){CreateShot01((Obj_GetX(obj[i])+Obj_GetX(obj[j]))/2,(Obj_GetY(obj[i])+Obj_GetY(obj[j]))/2,rand(1,3),rand(0,360),163,0)}
						Obj_Delete(obj[i]);}	
	}

task MainTask
{PlaySE(spell);
Wait(120);
let p1=0;
let p2=0;
let p3=0;
let p4=0;
let a=0;
let t=0;
let cj=1;
let d=200;
let w=0;
let v=180;
let r=0;
let R=200;
let charge=Obj_Create(OBJ_SHOT);
Obj_SetAngle(charge,0);

loop{r=(r+50)%360;Bouncers(600);
	loop(660){yield};
	
	}
}

task MoveAbout
{
SetMovePosition02(GetCenterX,GetCenterY-120,60);
MoveSatori;
loop
	{
	Wait(250);
	SetMovePositionRandom01(64,64,1,GetClipMinX+64,GetClipMinY+64,GetClipMaxX-64,GetClipMinY+128);
	MoveSatori;
	Wait(150);
	}
}

@MainLoop
	{
	SetCollisionA(GetX,GetY,32);
	SetCollisionB(GetX,GetY,32);
	yield;
	}



@DrawLoop
	{
	SetAlpha(255);
	if (bossflip==0){SetGraphicScale(1,1);}
	if (bossflip==1){SetGraphicScale(-1,1);}
	SetColor(255,255,255);
	SetGraphicAngle(0,0,0);
	SetTexture(SatoriSprite);
	SetGraphicRect(BR[0],BR[1],BR[2],BR[3]);
	DrawGraphic(GetX,GetY);
	}

	let bgalpha = 0;
let bgangle = 0;
let bgscroll = 0;

@BackGround
	{
	if (bgalpha < 255){bgalpha += 5;}
	bgangle += 0.3;
	bgscroll += 1;

	SetAlpha(bgalpha);
	SetGraphicScale(1,1);
	SetColor(255,255,255);

	SetTexture(SpellBG01);
SetGraphicRect(0,0,512+bgscroll,512);

	DrawGraphic(GetCenterX,GetCenterY);

	SetTexture(SpellBG02);
	SetGraphicRect(0,0,256,256);
	SetGraphicAngle(0,0,bgangle);
	DrawGraphic(GetCenterX,GetCenterY);

	}
	
@Finalize
	{
	if(GotSpellCardBonus==true){
		
			CreateItem(ITEM_SCORE,GetX(),GetY());
			
		}
	DeleteGraphic(SatoriSprite);
	}

}