script_enemy_main {
    let GCSD=GetCurrentScriptDirectory;
    #include_function ".\..\ShotSheet\shot_replace.dnh"
    #include_function ".\..\functions.txt"
    #include_function ".\iddyea_functions.txt"

    SpellID=505;

    @Initialize {
	Randomize;
        SetLife(5000);
        SetTimer2(77);
	SetDamageRate(80,40);
	MagicCircle(false);
	SetEnemyMarker(true);
	IddyeaInit;
	MainTask;
	shotinit;

	StartFunctions(1,0,0);
	SetBG(20);

	SpellCircle2(6,1,4,200);
    }


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

task MainTask{
	DelayBombTask;
	let pattern;
	let subpattern;

	let flag=0;
	let past=[0,0,0];
	let current=0;
	let i2=0;
	let timelife=0;

	let attacks=0;

	CutIn2(3,"Subconscious [Labyrinth of the Mind]");

	SetMovePositionNew(GetCenterX+rand(-50,50),100,60);
	Wait(100);

	while(GetLife>4000&&GetTimer2>60){
		if(attacks>0){
			SetMovePositionNew(GetCenterX+rand(-50,50),100,20);
		}
		Wait(20);
		timelife=lower(GetLife,GetTimer2*(200/3));
		subpattern=rand(5000-timelife,timelife-5000)/500;
		MainLaser(GetX,GetY,0,   1,subpattern,60,3,0, 100,10,5,100,10,RED23+attacks%7,200);
		MainLaser(GetX,GetY,180, 1,subpattern,60,3,0, 100,10,5,100,10,RED23+attacks%7,200);
		MainLaser(GetX,GetY,0,   1,subpattern,60,-3,0, 100,10,5,100,10,RED23+attacks%7,200);
		MainLaser(GetX,GetY,180, 1,subpattern,60,-3,0, 100,10,5,100,10,RED23+attacks%7,200);
		LaserSound;
		attacks++;
	}
	while(GetLife>3000&&GetTimer2>45){
		SetMovePositionNew(GetCenterX+rand(-50,50),rand(50,150),20);
		Wait(25);
		timelife=lower(GetLife,GetTimer2*(200/3));
		subpattern=rand(4000-timelife,timelife-4000)/500;
		MainLaser(GetCenterX+((attacks%2)-0.5)*380,rand(-100,0),90,   60,((attacks%2)-0.5)*-3,30,subpattern,0, 80,10,8,110,10,RED23+attacks%7,300);
		LaserSound;
		attacks++;
	}

	while(GetLife>2000&&GetTimer2>30){
		SetMovePositionNew(GetCenterX+rand(-90,90),rand(10,150),30);
		Wait(30);
//		subpattern=rand(4000-GetLife,GetLife-4000)/500;
		subpattern=attacks%2-0.5;
		MainLaser(GetX,GetY,GetAngleToPlayer+0  +subpattern*15,   1,0,1,0,subpattern*1, 90,10,5,100,20,RED23+attacks%7,220);
		MainLaser(GetX,GetY,GetAngleToPlayer+90 +subpattern*15,  1,0,1,0,subpattern*1, 90,10,5,100,20,RED23+attacks%7,220);
		MainLaser(GetX,GetY,GetAngleToPlayer+180+subpattern*15,  1,0,1,0,subpattern*1, 90,10,5,100,20,RED23+attacks%7,220);
		MainLaser(GetX,GetY,GetAngleToPlayer+270+subpattern*15,  1,0,1,0,subpattern*1, 90,10,5,100,20,RED23+attacks%7,220);
		LaserSound;

		attacks++;
		yield;
	}

	let timing=35;

	loop{

		flag=0;
		while(flag==0){
			pattern=rand_int(0,6);
			flag=1;
			i2=0;
			loop(3){
				if(pattern==past[i2]){flag=0;}
				i2++;
			}
		}
		past[current%3]=pattern;
		current++;

//		pattern=0;
		if(pattern==0){
			subpattern=rand(-1,1);
			SetMovePositionNew(GetCenterX+RD,100+RD,timing);
			Wait(timing);
			DoubleLaser(GetX,GetY,0,   60,subpattern,60,3,0, 100,10,5,100,10,RED23,200);
		}else if(pattern==1){
			subpattern=rand(-8,8);
			SetMovePositionNew(GetCenterX+120+RD,150+RD,timing);
			Wait(timing);
			DoubleLaser(GetX,GetY,20,   60,3*cos(110+subpattern),60,3*sin(110+subpattern),0, 100,10,6,100,10,AQUA23,200);
		}else if(pattern==2){
			subpattern=rand(-8,8);
			SetMovePositionNew(GetCenterX-120+RD,150+RD,timing);
			Wait(timing);
			DoubleLaser(GetX,GetY,-20,   60,3*cos(70+subpattern),60,3*sin(70+subpattern),0, 100,10,6,100,10,YELLOW23,200);
		}else if(pattern==3){
			subpattern=rand_int(0,1)-0.5;
			SetMovePositionNew(GetCenterX+RD,120+rand(-30,30),timing);
			Wait(timing);
			MainLaser(GetX,GetY,GetAngleToPlayer+0  +subpattern*10,   1,0,1,0,subpattern*1, 100,10,5,100,20,GREEN23,220);
			MainLaser(GetX,GetY,GetAngleToPlayer+90 +subpattern*10,  1,0,1,0,subpattern*1, 100,10,5,100,20,GREEN23,220);
			MainLaser(GetX,GetY,GetAngleToPlayer+180+subpattern*10,  1,0,1,0,subpattern*1, 100,10,5,100,20,GREEN23,220);
			MainLaser(GetX,GetY,GetAngleToPlayer+270+subpattern*10,  1,0,1,0,subpattern*1, 100,10,5,100,20,GREEN23,220);
		}else if(pattern==4){
			SetMovePositionNew(GetCenterX+rand(-60,60),120+rand(-30,30),timing);
			Wait(timing);
			MainLaser(GetX,GetY,GetAngleToPlayer-90,   1,0,1,0,0.5, 100,10,5,110,20,BLUE23,180);
			MainLaser(GetX,GetY,GetAngleToPlayer+90,   1,0,1,0,-0.5, 100,10,5,110,20,BLUE23,180);
		}else if(pattern==5){
			SetMovePositionNew(GetCenterX+rand(-30,30),120+rand(-30,30),timing);
			Wait(timing);
			subpattern=rand_int(0,1)-0.5;
			MainLaser(GetCenterX+subpattern*390,GetClipMaxY+rand(-50,50),-90+10*subpattern, 1,0,1,0,-1*subpattern, 100,10,5,120,10,PURPLE23,400);
		}else if(pattern==6){
			subpattern=rand_int(0,1)-0.5;
			SetMovePositionNew(GetCenterX+subpattern*300,150,timing);
			Wait(timing);
			MainLaser(GetCenterX+subpattern*380,rand(-100,0),90,   120,-4*subpattern,1,0,0, 100,10,5,120,10,ORANGE23,400);
		}
		LaserSound;
	}
}

function RD{return rand(-10,10);}

function DoubleLaser(x,y,a, xaccel,xv2,yaccel,yv2,spin, l,w,num,spacing,spawntime,graphic,deletetime){
	MainLaser(x,y,a,     xaccel,xv2,yaccel,yv2,spin, l,w,num,spacing,spawntime,graphic,deletetime);
	MainLaser(x,y,a+180, xaccel,xv2,yaccel,yv2,spin, l,w,num,spacing,spawntime,graphic,deletetime);
	MainLaser(x,y,a,     xaccel,-xv2,yaccel,-yv2,spin, l,w,num,spacing,spawntime,graphic,deletetime);
	MainLaser(x,y,a+180, xaccel,-xv2,yaccel,-yv2,spin, l,w,num,spacing,spawntime,graphic,deletetime);
}

@MainLoop {
        SetCollisionA(GetX, GetY, 40);
	SetCollisionB(GetX, GetY, 20);

	IddyeaDrawLogic;

	yield;
}

@DrawLoop {
	IddyeaDrawLoop;
}

@Finalize {
	IddyeaFinalize;
}


task MainLaser(x,y,a, xaccel,xv2,yaccel,yv2,spin, l,w,num,spacing,spawntime,graphic,deletetime){
    let addspacing=0.2;
    spacing+=spacing*addspacing;

    let obj = [];
    let frame=0;
    let size=100;
    let kk=0;

    loop(num){
	obj=obj~[InitLaser(x,y,a,l,w,spacing,spawntime,graphic,kk)];
	kk++;
    }
    let frame=0;
    let xv=0;
    let yv=0;
    let xa=xv2/xaccel;
    let ya=yv2/yaccel;
    loop(deletetime+30){
	kk=0;
	loop(num){
		if(frame>=kk*((l/spacing)*spawntime)){
			MoveLaser(obj[kk],trunc(x),trunc(y),a,spacing,kk);
		}
		kk++;
	}
	if((xv<xv2&&xv2>0) || (xv>xv2&&xv2<0)){xv+=xa;}
	if((yv<yv2&&yv2>0) || (yv>yv2&&yv2<0)){yv+=ya;}
	x+=xv;
	y+=yv;
	a+=spin;
	if(frame==deletetime){
		kk=0;
		loop(num){
			ObjShot_FadeDelete(obj[kk]);
			kk++;
		}
	}

	frame++;
	yield;
    }
}

function InitLaser(x,y,a,l,w,spacing,spawntime,graphic,index){
	let obj=Obj_Create(OBJ_LASER);
	InitLaser2(obj,x,y,a,l,w,spacing,spawntime,graphic,index);
	return obj;
}
task InitLaser2(obj,x,y,a,l,w,spacing,spawntime,graphic,index){
	let spawntime2=(l/spacing)*spawntime;
	ObjLaser_SetSource(obj,false);
	Obj_SetAutoDelete(obj,false);
	Obj_SetSpeed(obj, 0);
	ObjShot_SetGraphic(obj, graphic);
	ObjShot_SetDelay(obj, 0);
	Obj_SetAngle(obj, a);
	ObjShot_SetBombResist(obj,true);
	ObjLaser_SetWidth(obj,w);
	ObjLaser_SetLength(obj,l);
	Obj_SetPosition(obj,-100,-100);
	Wait(spawntime2*index);
	Obj_SetPosition(obj, x+cos(a)*spacing*index,y+sin(a)*spacing*index);
	let lengthadd=l/spawntime2;
	let l2=0;
	let loops=0;
	loop(spawntime){
		if(loops<spawntime2){
			ObjLaser_SetLength(obj,l2);
			l2+=lengthadd;
		}
		yield;
		if(loops%2==0){
			AppearEffect(Obj_GetX(obj)+cos(Obj_GetAngle(obj))* (loops*(spacing/spawntime)) ,Obj_GetY(obj)+sin(Obj_GetAngle(obj))* (loops*(spacing/spawntime)) ,10);
		}
		loops++;
	}
	ObjLaser_SetLength(obj,l);
}
function MoveLaser(obj,x,y,a,spacing,index){
	x-=cos(a)*(100-spacing)*0.5; //center the lazer
	y-=sin(a)*(100-spacing)*0.5;
	Obj_SetPosition   (obj, x+cos(a)*spacing*index,y+sin(a)*spacing*index);
	Obj_SetAngle(obj,a);
}
}