  #include_function ".\playerfunctions.txt"
  #include_function ".\effects.txt"
  #include_function ".\nhimor_functions.txt"

//  let active=[0,0,0,0,0,0,0];
//  let x=[0,0,0,0,0,0,0];
//  let y=[0,0,0,0,0,0,0];
//  let dir=[0,0,0,0,0,0,0];

  let active=[0,0,0,0,0,0,0,0,0,0];
  let x=[0,0,0,0,0,0,0,0,0,0];
  let y=[0,0,0,0,0,0,0,0,0,0];
  let dir=[0,0,0,0,0,0,0,0,0,0];

  let playernum=2;

  let i=0;
  let i2=0;

  let temparrayx=[];
  let temparrayy=[];
  let temparrayd=[];

  function SetCloneData(id,nx,ny){
	let temp=x[id];
	if(active[id]==0){
		temp=nx;
	}
	active[id]=1;
	x[id]=nx;
	y[id]=ny;
	if(nx>temp){dir[id]=2;}
	else if(nx<temp){dir[id]=1;}
	else{dir[id]=0;}
  }

  task run{
    SE("bubble");
    SpellCardEffect;
    SetCommonData("DrawNhineeClones",1);
    DeleteCommonData("DeleteNhimorShots");
    PlayerForbidShotOnN;

    let angle=0;
    i2=0;
    loop(10){
	//CreateClone(i2,GetPlayerX+cos(angle)*70,GetPlayerY+sin(angle)*70);
	angle=prand(0,360);
	CreateClone(i2,GetPlayerX+prand(-100,100),GetPlayerY+prand(-100,100));
	StandardShots;
	loop(4){yield;}
	i2++;
    }

    loop(60){
	StandardShots;
	if(i2%2==0){
		CloneShots;
	}
	loop(4){yield;}
	i2++;
    }
    SetCommonData("DrawNhineeClones",0);

    StopEffect=1;
    PlayerForbidShotOffN;
    SetCommonData("DeleteNhimorShots",1);
    loop(30){
      yield;
    }
    SetCommonData("DeleteNhimorShots",1);
    yield;
    End();
  }


  task CreateClone(id,x,y){
	SE("nhinee1");
	let obj=Obj_Create(OBJ_SPELL);	//dummy object so collision can be used

	x=center(44,x,404);
	y=center(40,y,448);
	loop(5){
//		CloneEffect(GetPlayerX,GetPlayerY,x+prand(-10,10),y+prand(-10,10),30,0);
		CloneEffect(GetPlayerX,GetPlayerY,x,y,30,0);
	}
	loop(30){yield;}
	let angle;;
	loop(3){
		angle=prand(0,360);
		Goo(x+cos(angle)*5,y+sin(angle)*5,0,0,1,255,30);
	}

	let dir=rand_int(0,7);
	let move=0;
	let timer1=prand_int(10,30);
	let timer2=timer1+prand_int(5,10);
	let currenttime=timer1;
	let totaltime=0;
	loop(180){
		if(currenttime<timer1){
			x+=cos(dir*45)*4.5;
			y+=sin(dir*45)*4.5;
			x=center(44,x,404);
			y=center(40,y,448);
		}else if(currenttime>=timer2){
			timer1=prand_int(5,40);
			timer2=timer1+prand_int(10,30);
			currenttime=0;
			dir=round(atan2(GetPlayerY-y,GetPlayerX-x)/45)+prand_int(-1,1);
		}

		currenttime++;
		SetCloneData(id,x,y);
		ObjSpell_SetIntersecrionCircle(obj,x,y,25,0,true);
		yield;
	}
	SE("nhinee2");
	active[id]=0;
	loop(5){
		CloneEffect(x+prand(-10,10),y+prand(-10,10),GetPlayerX,GetPlayerY,30,1);
	}
	loop(5){
		angle=prand(0,360);
		Goo(x+cos(angle)*5,y+sin(angle)*5,0,0,1,255,30);
	}
	Obj_Delete(obj);
  }
  task CloneEffect(x1,y1,x2,y2,time,track){
	let obj=Obj_Create(OBJ_EFFECT);
	Obj_SetSpeed(obj,0);
	Obj_SetAngle(obj,0);
	ObjEffect_SetLayer(obj,4);
	ObjEffect_SetTexture(obj,img_nhimor);
	ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLEFAN);
	ObjEffect_CreateVertex(obj,4);
	ObjEffect_SetVertexXY(obj,0,-18,-18);
	ObjEffect_SetVertexXY(obj,1, 18,-18);
	ObjEffect_SetVertexXY(obj,2, 18, 18);
	ObjEffect_SetVertexXY(obj,3,-18, 18);
	ObjEffect_SetScale(obj,0.7,0.7);
	Pisclet_SetGraphic(obj,prand_int(0,3));

	let lastx=GetPlayerX;
	let lasty=GetPlayerY;

	let xv=(x2-x1)/time;
	let yv=(y2-y1)/time;
	let graphicangle=0;
	let offset=0;
	let frame=0;
	let offsetangle=prand(0,360);
	loop(time){
		offset=cos(frame*(180/time)-90);
		ObjEffect_SetAngle(obj,0,0,graphicangle);
		Obj_SetPosition(obj,x1+cos(offsetangle)*offset*15,y1+sin(offsetangle)*offset*15);
		x1+=xv;
		y1+=yv;
		if(track==1){
			x1+=GetPlayerX-lastx;
			y1+=GetPlayerY-lasty;
			lastx=GetPlayerX;
			lasty=GetPlayerY;
		}
		graphicangle+=20;
		frame++;
		yield;
	}

	Obj_Delete(obj);
  }
  sub StandardShots{
	CreatePlayerShot01Obj(GetPlayerX   ,GetPlayerY,12,-90,1.75,1,prand_int(5,8),255);
  }
  sub CloneShots{
    if(!IsForbidShot){
	i=0;
	loop(length(GetCommonData("NhineeCloneX"))){
		CreatePlayerShot01Obj(GetCommonData("NhineeCloneX")[i]-10,GetCommonData("NhineeCloneY")[i],12,-95,2.4,1,prand_int(5,8),255);
		CreatePlayerShot01Obj(GetCommonData("NhineeCloneX")[i]   ,GetCommonData("NhineeCloneY")[i],12,-90,2.4,1,prand_int(5,8),255);
		CreatePlayerShot01Obj(GetCommonData("NhineeCloneX")[i]+10,GetCommonData("NhineeCloneY")[i],12,-85,2.4,1,prand_int(5,8),255);

		i++;
	}
    }
  }
  @Initialize{
    SetPlayerInvincibility(300);
    run();
  }

  @MainLoop{
    CollectItems();
    yield;
    i=0;
    temparrayx=[];
    temparrayy=[];
    temparrayd=[];
    loop(10){
	if(active[i]==1){
		temparrayx=temparrayx~[x[i]];
		temparrayy=temparrayy~[y[i]];
		temparrayd=temparrayd~[dir[i]];
	}
	i++;
    }
    SetCommonData("NhineeCloneX",temparrayx);
    SetCommonData("NhineeCloneY",temparrayy);
    SetCommonData("NhineeCloneDir",temparrayd);
  }
  @Finalize{

  }