script_enemy_main {

	#include_function ".\..\ShotSheet\shot_replace.dnh"
	#include_function ".\..\functions.txt"
	#include_function ".\..\other\enemy_functions_exchikke.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=[100,100,1];
	let expsize=[2,2,2];
	let expexpand=[0.2,0.4,0.5];
	let bomb=[100,100,50];

	let size=          [0,10,20];
	let ScoreItem=     [1,2,4];
	let MultiplierItem=[0,1,4];

	let ScoreKill=[3,5,10]; //divided by 100
	let ScoreDamage=[1.5,2,3]; //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;

    @Initialize {
	Randomize;
	shotinit;
	if(type==2){
		Life[2]=Life[2]*arg4;
	}
        SetLife(Life[type]+10000);
	prevlife=Life[type];
	totallife=Life[type];
	DamageRate;

	ExchikkeInit;
	if(type==2){
		DeathFairyInit(4,7,4);
	}

	if(type==0){Move0;}
	if(type==1){Move1;}
	if(type==2){Move2;}
    }
task DamageRate{
	let time=GetCommonDataDefault("EnemyInvincibilityA",0);
	SetDamageRate(0,0);
	Wait(time);
	SetDamageRate(255,bomb[type]*2);
}

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

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

	SetCollisionA(GetX,GetY, 20);
	SetCollisionB(GetX,GetY, 12);

	ExchikkeMainLoop;
	if(type==2){
		DeathFairyLogic(rand(200,280),rand(0.2,0.8),rand(200,280),rand(0.2,0.8));
	}

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

	if(GetLife<10000){VanishEnemy;}	


	yield;
    }
// Type, X, Y, V, Angle, a1, a2, a3, a4

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

//0=spread aim | [time,turntime,angleturn,shotfrequency],[boolet1,v1,v2,deceltime,anglevariation1],[boolet2,finalv,acceltime,anglevariation2] //-------------------------------------------------------------------------------------------------  
task Move0{
	let time=arg1[0];
	let angleturn=arg1[2];
	let turntime=arg1[1];
	let shotfrequency=arg1[3];
	let deceltime=arg2[3];
	let acceltime=arg3[2];
	let v1=arg2[1];
	let v2=arg2[2];

	let anglevariation1=arg2[4];
	let anglevariation2=arg3[3];
	let boolet1=arg2[0];
	let boolet2=arg3[0];

	let finalv=arg3[1];

	if(diff==0){
		shotfrequency=round(shotfrequency*3);
		anglevariation1*=0.5;
		anglevariation2*=0.5;
	}
	if(diff==1){
		shotfrequency=round(shotfrequency*1.7);
		anglevariation1*=0.5;
	}

	let soundfreq=0;
	let v;
	while(time>0){
		v=rand(v1,v2);
		CreateShotA(0,GetX,GetY,0);
		SetShotDataA(0,0,v,GetAngle+rand(-anglevariation1,anglevariation1),0,-v/deceltime,0,boolet1);
		SetShotDirectionType(PLAYER);
		SetShotDataA(0,deceltime,0,rand(-anglevariation2,anglevariation2),0,finalv/acceltime,finalv,boolet2);
		SetShotDirectionType(ABSOLUTE);
		AddShotDelay(0,deceltime,boolet2,10);
		FireShot(0);
		if(soundfreq==0){
			SE("shot1");
			soundfreq=2;
		}else{
			soundfreq--;
		}
		time-=shotfrequency;
		Wait(shotfrequency);
	}
	loop(turntime){
		SetAngle(GetAngle+angleturn);
		yield;
	}
}
//1=split | [time,angleshot,frequency],[[v1,v2],[v1,v2],[v1,v2],[v1,v2]],[g,g,g,g],null //-------------------------------------------------------------------------------------------------  
task Move1{
	let time=arg1[0];
	let angleshot=arg1[1];
	let frequency=arg1[2];

	let v=arg2;
	let graphic=arg3;

	let tempv;
	let a=0;
	let a2=0;
	let a3=0;

	if(diff==0){
		frequency*=2;
	}
	if(diff==1){
		frequency*=1.5;
	}
	loop{
		Wait(frequency);
		CreateShotA(0,GetX,GetY,0);
		tempv=rand(v[0][0],v[0][1]);
		SetShotDataA(0,0,tempv,angleshot,0,-tempv/time,0, graphic[0]);
		loop(4){
			tempv=rand(v[1][0],v[1][1]);
			CreateShotA(1,0,0,10);
			SetShotDataA(1,0,tempv,a+angleshot+45,0,-tempv/time,0, graphic[1]);
			loop(4){
				tempv=rand(v[2][0],v[2][1]);
				CreateShotA(2,0,0,10);
				SetShotDataA(2,0,tempv,a2+angleshot,0,-tempv/time,0, graphic[2]);

				loop(4){
					tempv=rand(v[3][0],v[3][1]);
					CreateShotA(3,0,0,10);
					SetShotDataA(3,0,tempv,a3+angleshot+45,0,0,0, graphic[3]);

					AddShot(time,2,3,0);
					a3+=90;
				}
				SetShotKillTime(2,time+1);
				AddShot(time,1,2,0);
				a2+=90;
			}

			SetShotKillTime(1,time+1);
			AddShot(time,0,1,0);
			a+=90;
		}
		SetShotKillTime(0,time+1);
		FireShot(0);
		SE("shot2");
		SE("shot3");

	}
}

//2=big | [deceltime,totalshots,leavetime],[boolet,v,numbullets,shotfrequency],[numdiamonds,diamondwidth,spacing,patternrotation],HPmod  //-------------------------------------------------------------------------------------------------  

task Move2{
	let deceltime=arg1[0];

	let boolet=arg2[0];
	let numbullets=arg2[2];
	let spacing=arg3[2];
	let v=arg2[1];
	let numdiamonds=arg3[0];
	let patternrotation=arg3[3];
	let diamondwidth=arg3[1];
	let shotfrequency=arg2[3];
	let totalshots=arg1[1];
	let leavetime=arg1[2];

	let decel=GetSpeed/deceltime;
	loop(deceltime){
		SetSpeed(GetSpeed-decel);
		yield;
	}

	if(diff==0){
		spacing*=2;
		shotfrequency*=2;
		totalshots=round(totalshots/2);
	}

	let angle=0;
	loop(totalshots){
		loop(numdiamonds){
			DiamondShot(GetX,GetY,v,angle,diamondwidth,boolet,numbullets,spacing);
			angle+=360/numdiamonds;
		}
		DiamondSound(numbullets,spacing);
		angle+=patternrotation;
		Wait(shotfrequency);
	}
	Wait(leavetime);
	loop(deceltime){
		SetSpeed(GetSpeed-decel);
		yield;
	}
}
task DiamondSound(num,spacing){
	SE("laser3");
	SE("laser2");
	SE("shot0");
	SE("shot3");
	loop(num){
		SE("shot0");
		Wait(spacing);
	}
}
task DiamondShot(x,y,v,a,a2,g,num,spacing){
	let anglechange=a2/num;
	let i=0;
	let angleadd=0;
	while(i<num){
		CreateShot01(x,y,v,a+angleadd,g,0);
		if(angleadd!=0){
			CreateShot01(x,y,v,a-angleadd,g,0);
		}
		i++;
		angleadd+=anglechange;
		Wait(spacing);
	}
	while(i>=0){
		CreateShot01(x,y,v,a+angleadd,g,0);
		if(angleadd!=0){
			CreateShot01(x,y,v,a-angleadd,g,0);
		}
		i--;
		angleadd-=anglechange;
		Wait(spacing);
	}
}

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



    @DrawLoop{
	DeathFairyEffect(6,70);
	ExchikkeDrawLoop;
}

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

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

		SE("enemyexplode");

		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);
		CreateExplosion(0,GetX,GetY,expsize[type],[color[0]-prand(0,100),color[1]-prand(0,100),color[2]-prand(0,100)],expexpand[type]/4);

		if(type==2){
			loop(7){
				CreateExplosionEX(3,GetX,GetY,rand(1,3),RainbowArray(rand(200,260),rand(100,150),100),rand(0.2,1),rand(20,40));
			}
		}

	}
    }

}
