script_enemy_main {

	#include_function ".\..\ShotSheet\shot_replace.dnh"
	#include_function ".\..\functions.txt"

	let GCSD=GetCurrentScriptDirectory;
	let imgEnemy=GPSD~"img\minion.png";

	let type=GetCommonData("E");
	let arg1=GetArgument;
	let arg2=GetCommonData("A2");
	let arg3=GetCommonData("A3");
	let arg4=GetCommonData("A4");
	let arg5=GetCommonData("A5");
	let diff=GetCommonDataDefault("Diff",1);

	let Life=[250*arg2,250*arg2,250*arg2,250*arg2];
	let expsize=[2,2,2,2];
	let expexpand=[0.2,0.2,0.6,0.2];
	let bomb=[100,100,100,100];

	let ScoreItem=     [1,1,1,1];
	let MultiplierItem=[1,1,1,1];

	let ScoreKill=[3,3,5,3]; //divided by 100
	let ScoreDamage=[1.5,1.5,3,1.5]; //divided by 100 (Score per kill, but calculated per hit)

	let BeDeleted=1;
	let MaxGraze=0;
	let prevlife=0;
	let totallife=0;
	let ii=0;

	let xs=0;
	let ys=0;

	let color=[0,0,0];

    @Initialize {
	Randomize;
	shotinit;
        SetLife(Life[type]+10000);
	prevlife=Life[type];
	totallife=Life[type];
	DamageRate;

	color=[Rainbow(0,arg5[0])*arg5[1]+(255-arg5[1]),Rainbow(1,arg5[0])*arg5[1]+(255-arg5[1]),Rainbow(2,arg5[0])*arg5[1]+(255-arg5[1])];

	Move;
	if(type==0){Shoot0;}
	if(type==1){Shoot1;}
	if(type==3){Shoot3;}
    }

let glowframe=0;
    @MainLoop {
	DrawLogic;
	AddScore(((prevlife - (upper(GetLife-10000,0)) )/totallife)*(ScoreDamage[type]*100));
	prevlife=upper(GetLife-10000,0);

	if((GetX<GetClipMinX-64||GetX>GetClipMaxX+64||GetY<GetClipMinY-64||GetY>GetClipMaxY+64)&&BeDeleted==1){
		VanishEnemy;
	}

	SetCollisionA(GetX,GetY, 18*arg2);
	SetCollisionB(GetX,GetY, 12*arg2);

	if(GetLife<10000){VanishEnemy;}	

	glowframe++;
	if(glowframe%5==0){
		Glow;
	}
	yield;
    }
// Type, X, Y, V, Angle, a1, a2, a3, a4

function Wait(frames){loop(frames){yield;}}


task DamageRate{
	let time=GetCommonDataDefault("EnemyInvincibilityA",0);
	SetDamageRate(0,0);
	Wait(time);
	SetDamageRate(215,bomb[type]*2);
	loop{
		if(rand_int(0,4)==0){
			CheckForExplosions;
		}
		yield;
	}
}

// first 2 parameters are always the same:
// [[numsteps], [frames], [speeds], [angles], [times]],size

//-------------------------------------------------------------------------------------------------  // common movement routine

task Move{
	let phase=0;
	let frame=0;
	let speedadd;
	let angleadd;
	loop(arg1[0][0]){
		speedadd=(GetSpeed-arg1[2][phase])/arg1[4][phase];
		angleadd=(GetAngle-arg1[3][phase])/arg1[4][phase];
		loop(arg1[4][phase]){
			SetSpeed(GetSpeed-speedadd);
			SetAngle(GetAngle-angleadd);
			frame++;
			yield;
		}
		while(frame!=arg1[1][phase]){
			frame++;
			yield;
		}
		phase++;
	}
}


//0=ring random	|	[type,v1,v2,acceltime,number],[frequency,max] //-------------------------------------------------------------------------------------------------  
task Shoot0{
	if(arg4[0]>0){
		if(diff==0){
			arg4[0]=round(arg4[0]*2);
			arg4[1]=round(arg4[1]/2);
			arg3[4]=round(arg3[4]*0.5);
		}
		if(diff==1){
			arg4[0]=round(arg4[0]*1.5);
			arg4[1]=round(arg4[1]/1.5);
			arg3[4]=round(arg3[4]*0.8);
		}
		let k=0;
		loop(arg4[1]){
			k=rand(0,360);
			loop(arg3[4]){
				CreateShot02(GetX,GetY,arg3[1],k,(arg3[2]-arg3[1])/arg3[3],arg3[2],arg3[0],0);
				k+=360/arg3[4];
			}
			SE("shot3");
			SE("shot1");
			Wait(arg4[0]);
		}
	}
}

//1=aimed delay	|	[type1,type2,v1,v2,accel1,accel2],[frequency,max] //-------------------------------------------------------------------------------------------------  
task Shoot1{
	if(arg4[0]>0){
		if(diff==0){
			arg4[0]=round(arg4[0]*2);
			arg4[1]=round(arg4[1]/2);
			arg3[2]=arg3[2]*0.5;
			arg3[3]=arg3[3]*0.5;
		}
		if(diff==1){
			arg4[0]=round(arg4[0]*1.5);
			arg4[1]=round(arg4[1]/1.5);
			arg3[2]=arg3[2]*0.8;
			arg3[3]=arg3[3]*0.8;
		}
		loop(arg4[1]){
			Shoot1B(GetX,GetY);

			if(arg3[4]>1){
				DelaySound(arg3[4]);
			}
			SE("shot3");
			SE("shot1");
			Wait(arg4[0]);
		}
	}
}
task Shoot1B(x,y){
	let angle=rand(0,360);
	let delay=EnemyShotDelay(x,y,angle,32,10,arg3[0],20,20);
	CreateShotA(0,x,y,0);
	SetShotDataA(0,0,arg3[2],angle,0,-(arg3[2])/arg3[4],0,arg3[0]);
	SetShotDirectionType(PLAYER);
	SetShotDataA(0,arg3[4],0,0,0,(arg3[3])/arg3[5],arg3[3],arg3[1]);
	AddShotDelay(0,arg3[4],arg3[1],20);
	SetShotDirectionType(ABSOLUTE);
	FireShot(0);
}
//2=explode	|	[type,a,b,velocity modifier,number,angle],frequency //-------------------------------------------------------------------------------------------------  
task Shoot2{
	if(arg4>0){
		if(diff==0){
			arg4*=2;
			arg3[3]=arg3[3]*0.7;
			arg3[4]=round(arg3[4]*0.5);
		}
		if(diff==1){
			arg4*=1.5;
			arg3[4]=round(arg3[4]*0.5);
		}
		let k=0;
		let kint=0;
		let ellipsepoints=EllipsePoints(arg3[1],arg3[2],arg3[4]);

		loop(arg3[4]){
//			CreateShot02(GetX,GetY,ellipsepoints[kint]*arg3[1],k+arg3[6],(ellipsepoints[kint]*(1-arg3[1]))),ellipsepoints[kint],arg3[0],0);
			CreateShot01(GetX,GetY,ellipsepoints[kint]*arg3[3],k+arg3[5],arg3[0],0);
//			CreateShot01(GetX,GetY,ellipsepoints[kint]*arg3[3]*0.5,k+arg3[5],arg3[0],0);
			k+=360/arg3[4];
			kint++;
		}
		SE("shot1");
	}
}


//1=fall random delay	|	[type,yv1,yv2,minxv,maxxv,acceltime],[minfrequency,maxfrequency] //-------------------------------------------------------------------------------------------------  
task Shoot3{
	if(arg4[0]>0){
		if(diff==0){
			arg3[1]=arg3[1]*0.5;
			arg3[2]=arg3[2]*0.5;
			arg4[0]=arg4[0]*1.5;
			arg4[1]=arg4[1]*1.5;
		}
		if(diff==1){
			arg3[1]=arg3[1]*0.8;
			arg3[2]=arg3[2]*0.8;
			arg4[1]=arg4[1]*1.3;
		}
		loop{
			Shoot3B(GetX,GetY);
			SE("shot1");
			Wait(rand_int(arg4[0],arg4[1]));
		}
	}
}
task Shoot3B(x,y){
	let delay=EnemyShotDelay(x,y,0,50,20,arg3[0],15,15);
	if(GetPlayerY>GetY){
		CreateShot12(x,y,rand(arg3[3],arg3[4]),arg3[1],0,(arg3[2]-arg3[1])/arg3[5],0,arg3[2],arg3[0],delay);
	}else{
		CreateShot12(x,y,rand(arg3[3],arg3[4]),-arg3[1],0,-(arg3[2]-arg3[1])/arg3[5],0,-arg3[2],arg3[0],delay);
	}
}

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------

function EllipsePoints(a,b,steps){
	let points=[];
	let i=0;
	loop(steps){
		points=points~[((cos(i)^2)*a+(sin(i)^2)*b)^0.5];
		i+=360/steps;
	}
	return points;
}

  let glowx=[0,0,0,0,0];
  let glowy=[0,0,0,0,0];
  let glowt=[-1,-1,-1,-1,-1];
  let glowd=[0,0,0,0,0];


  let imgrotation=[rand(0,360),rand(0,360),rand(0,360)];
  let imgrotspeed=[rand(0.5,1)*RandDir,rand(0.5,1)*RandDir,rand(0.5,1)*RandDir];
  let starrotation=0;

    sub DrawLogic{
	starrotation++;
    }





  let glownum=0;
  function Glow{
	glowx[glownum%5]=GetX;
	glowy[glownum%5]=GetY;
	glowt[glownum%5]=starrotation;
	glowd[glownum%5]=rand(0,360);
	glownum++;
  }



	let gi=0;

	let dir=0;
	let th=30;
    @DrawLoop {
        SetTexture(imgEnemy);
	SetColor(color[0],color[1],color[2]);

	SetGraphicRect(64, 0, 128, 64);
	SetGraphicScale(arg2*0.5,arg2*0.5);
	SetAlpha(100);
	SetGraphicAngle(0,0,starrotation*9);
	DrawGraphic(GetX,GetY);
	SetGraphicAngle(0,0,-starrotation*10);
	DrawGraphic(GetX,GetY);


	if(arg2>=1){
		SetGraphicRect(128, 96, 160, 128);

		SetAlpha(255);
		SetGraphicAngle(0,0,0);
		let k=0;
		loop(5){
			if((starrotation-glowt[k])>=0&&(starrotation-glowt[k])<20){
				SetGraphicScale(arg2*(20-(starrotation-glowt[k]))*0.02,arg2*(20-(starrotation-glowt[k]))*0.02);
				DrawGraphic(glowx[k]+cos(glowd[k])*(starrotation-glowt[k])*arg2,glowy[k]+sin(glowd[k])*(starrotation-glowt[k])*arg2);
			}
			k++;
		}
	}

	SetGraphicRect(160, 0, 160+64, 64);
	SetAlpha(255);
	gi=0;
	loop(3){
		SetGraphicAngle(0,0,imgrotation[gi]+imgrotspeed[gi]*7*starrotation);
		SetGraphicScale(arg2*graphicscale[gi],arg2*graphicscale[gi]);
		DrawGraphic(GetX,GetY);
		gi++;
	}

}
let graphicscale=[0.3,0.4,0.5];

    @Finalize {
	deleteobjects=true;
	yield;
	deleteobjects=false;
	if(GetLife<10000){
		if(type==2){
			Shoot2;
		}
		AddScore(ScoreKill[type]*100);

		CreateItems(GetX,GetY,10,ScoreItem[type],MultiplierItem[type],arg5[2],arg5[3]);

		SE("enemyexplode");


		CreateDamagingExplosion(GetX,GetY,40*arg2,60,color);

		CreateExplosion(0,GetX,GetY,expsize[type],[color[0]-prand(0,100),color[1]-prand(0,100),color[2]-prand(0,100)],expexpand[type]);
//		CreateExplosion(0,GetX,GetY,expsize[type],[color[0]-prand(0,100),color[1]-prand(0,100),color[2]-prand(0,100)],expexpand[type]/2);
	}
    }

function CreateDamagingExplosion(x,y,maxsize,time,color){
	let i=0;
	while(GetCommonDataDefault("DExx"~IntString(i),-1000)>-1000&&i<50){
		i++;
	}
	SetCommonData("DExx"~IntString(i),x);
	SetCommonData("DExy"~IntString(i),y);
	SetCommonData("DExs"~IntString(i),maxsize);
	SetCommonData("DExt"~IntString(i),time);
	SetCommonData("DExc"~IntString(i),color);
}


	let timer=0;
	let i=0;
	let i2=0;
	let i3=0;

sub CheckForExplosions{
	let i=0;
	loop(GetCommonDataDefault("NumDamageExplosions",0)){
		while(GetCommonDataDefault("DamageExplosionX"~IntString(i),-1000)==-1000){
			i++;			
		}
		if(distance(GetX,GetY,GetCommonData("DamageExplosionX"~IntString(i)),GetCommonData("DamageExplosionY"~IntString(i)))^0.5<GetCommonData("DamageExplosionS"~IntString(i))){
			AddLife(-100);
		}
		i++;
	}
}

}
