package hmggirlfront.entity;

import java.util.List;

import gvclib.entity.living.EntityGVCLivingBase;
import gvclib.entity.living.EntityVehicleBase;
import gvclib.entity.living.ISoldier;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.items.ItemDollAttachment;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.MoverType;
import net.minecraft.entity.ai.RandomPositionGenerator;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;

public abstract class DollAI_EntityMoveS_Squad
{
	public static void newmove(EntityDollBase entity, int id, float sp, float turnspeed, double max, double range1, double range2, double followrange) {
		entity.sneak = false;
		//boolean ta = false;
		double range = range1;
		if(entity.getMobMode() == 1){
			range = range2;
		}
		
		entity.on_meve = false;
		/*{
			List<Entity> llist2 = entity.world.getEntitiesWithinAABBExcludingEntity(entity, entity
					.getEntityBoundingBox().expand(entity.motionX, entity.motionY, entity.motionZ).grow(range));
			if (llist2 != null) {
				for (int lj = 0; lj < llist2.size(); lj++) {
					Entity entity1 = (Entity) llist2.get(lj);
					if (entity1.canBeCollidedWith()) {
						boolean flag = entity.getEntitySenses().canSee(entity1);
						if(!(entity.getMoveT() == 3)) {
							
						}
					}
				}
			}
		}*/
		
		boolean rf_mode = false;
		{
			if(entity.getattacktask()){
				if(entity.gun_class == 3
						|| entity.gun_class == 4) {
					entity.setSneak(true);
					rf_mode = true;
				}
			}else {
				entity.setSneak(false);
			}
		}
		
		
		{
			double x = entity.posX;
			double y = entity.posY;
			double z = entity.posZ;
			double han = range;
			AxisAlignedBB axisalignedbb2 = (new AxisAlignedBB((double) (x - han), (double) (y - han),
					(double) (z - han), (double) (x + han), (double) (y + han), (double) (z + han)))
							.grow(han);
			List<Entity> llist = entity.world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb2);
			if (llist != null) {
				//player
				for (int lj = 0; lj < llist.size(); lj++) {
					Entity entity1 = (Entity) llist.get(lj);
					if (entity1.canBeCollidedWith()) {
						if (entity.getMoveT() == 0)//follow
						{
							if (entity1 != null && entity1 instanceof EntityPlayerMP) {
								EntityPlayer player = (EntityPlayer) entity1;
								//System.out.println(String.format("m"));
								if (entity.isOwner(player) && !entity.isRiding()) {
									double d5 = entity1.posX - entity.posX;
									double d7 = entity1.posZ - entity.posZ;
									double d6 = entity1.posY - entity.posY;
									double d1 = entity.posY - (entity1.posY);
									double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
									float f11 = (float) (-(Math.atan2(d1, d3) * 180.0D / Math.PI));
									if (!entity.getattacktask()) {
										entity.rotationYawHead = entity.rotationYaw = entity.renderYawOffset = entity.rotation = entity.rote = -((float) Math
												.atan2(d5, d7)) * 180.0F / (float) Math.PI;
										entity.rotationp = entity.rotationPitch = -f11 + 0;
									}
									double ddx = Math.abs(d5);
									double ddz = Math.abs(d7);
									if ((ddx > followrange || ddz > followrange)) {
										entity.setPositionAndUpdate(player.posX, player.posY, player.posZ);
									} else {
										if ((ddx > entity.squad_followrange || ddz > entity.squad_followrange)) {
											if(!rf_mode)MoveS(entity, sp, 1, player.posX, player.posY, player.posZ, 0);
										}
									}
									//System.out.println(String.format("move"));
								}
							}
						}
						if (entity1 != null && (entity1 instanceof ISoldier && entity1 instanceof EntityGVCLivingBase) && entity.getMoveT() != 3) {
							EntityGVCLivingBase sold = (EntityGVCLivingBase) entity1;
							
							//System.out.println(String.format("en"));
							double d5 = entity1.posX - entity.posX;
							double d7 = entity1.posZ - entity.posZ;
							double d6 = entity1.posY - entity.posY;
							double d1 = entity.posY - (entity1.posY);
							double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
							double ddx = Math.abs(d5);
							double ddz = Math.abs(d7);
							if ((ddx < 2 && ddz < 2) && sold.getMoveT() != 3) {
								MoveS(entity, -sp, 1, entity1.posX, entity1.posY, entity1.posZ, 0);
							}
							//break;
						}
					}
				}
				//player
			}
		}
		{
			double x = entity.posX;
			double y = entity.posY;
			double z = entity.posZ;
			double han = range;
			AxisAlignedBB axisalignedbb2 = 
					(new AxisAlignedBB((double)(x-han), (double)(y-han), (double)(z-han), (double)(x + han), (double)(y + han), (double)(z+ han)))
            		.grow(han);
			//List<Entity> llist = entity.world.getEntitiesWithinAABBExcludingEntity(entity, entity
			//		.getEntityBoundingBox().expand(entity.motionX, entity.motionY, entity.motionZ).grow(range));
			List<Entity> llist = entity.world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb2);
			if (llist != null) {
				//mob
				for (int lj = 0; lj < llist.size(); lj++) {
					Entity entity1 = (Entity) llist.get(lj);
					if (entity1.canBeCollidedWith()) {
						boolean flag = entity.getEntitySenses().canSee(entity1);
						if(entity.targetentity == entity1 && entity1 != null && entity1 instanceof EntityLivingBase) {
							EntityLivingBase en = (EntityLivingBase) entity1;
							if(en.deathTime > 0) {
								entity.targetentity = null;
							}
						}
						if(entity.getMoveT() == 3) {//wait
							if(entity.targetentity == entity1 && entity1 != null) {
								double d5 = entity.targetentity.posX - entity.posX;
								double d7 = entity.targetentity.posZ - entity.posZ;
								double d6 = entity.targetentity.posY - entity.posY;
								double d1 = entity.posY - (entity.targetentity.posY);
								double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
								float f11 = (float) (-(Math.atan2(d1, d3) * 180.0D / Math.PI));
								entity.rotationYaw = entity.renderYawOffset = entity.rotationYawHead = entity.rotation = entity.rote
										= -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
								entity.rotationp = entity.rotationPitch = -f11 + 0;
								break;
							}
						}
						else if(entity.getMoveT() == 5) {//pointfire
							{
								double d5 = entity.getMoveX() - entity.posX;
								double d7 = entity.getMoveZ()- entity.posZ;
								double d6 = entity.getMoveY() - entity.posY;
								double d1 = entity.posY - (entity.getMoveY());
								double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
								float f11 = (float) (-(Math.atan2(d1, d3) * 180.0D / Math.PI));
								 
								{
									entity.rotationYawHead = entity.rotationYaw = entity.renderYawOffset = entity.rotation = entity.rote
											= -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
									entity.rotationp = entity.rotationPitch = -f11 + 0;
									double ddx = Math.abs(d5);
									double ddz = Math.abs(d7);
								}
								break;
							} 
						}
						else if(entity.getMoveT() == 0) {//follow
							if(entity.targetentity == entity1 && entity1 != null) {
								double d5 = entity.targetentity.posX - entity.posX;
								double d7 = entity.targetentity.posZ - entity.posZ;
								double d6 = entity.targetentity.posY - entity.posY;
								double d1 = entity.posY - (entity.targetentity.posY);
								double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
								float f11 = (float) (-(Math.atan2(d1, d3) * 180.0D / Math.PI));
								entity.rotationYaw = entity.renderYawOffset = entity.rotationYawHead = entity.rotation = entity.rote
										= -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
								entity.rotationp = entity.rotationPitch = -f11 + 0;
								if (flag) {
									entity.targetentity = (EntityLivingBase) entity1;
								}
							}
							
						}
						else if(entity.getMoveT() == 1) {//free
							boolean medic = false;
							ItemStack feet = entity.getItemStackFromSlot(EntityEquipmentSlot.FEET);
							if(!feet.isEmpty() &&  feet.getItem() instanceof ItemDollAttachment) {
								if(feet.getItem() == mod_GirlFront.dollam_class_medic) {
									if (entity1 instanceof EntityDoll && entity1 != entity && ((EntityLivingBase) entity1).getHealth() <= 0.0F)
			                        {
										EntityDoll em = (EntityDoll) entity1;
										double d5 = entity1.posX - entity.posX;
										double d7 = entity1.posZ - entity.posZ;
										double d6 = entity1.posY - entity.posY;
										double d1 = entity.posY - (entity1.posY);
										double ddx = Math.abs(d5);
										double ddz = Math.abs(d7);
										if ((ddx < 20 && ddz < 20) && (ddx > 1.5 || ddz > 1.5) && flag) {
											MoveS(entity, sp, 1, entity1.posX, entity1.posY, entity1.posZ, 0);
										}
										medic = true;
										break;
									}
								}
							}
							if (entity.CanAttack(entity1) && entity1 != null && !medic) {//target
								
								if (entity.targetentity == entity1
										&& ((EntityLivingBase) entity1).getHealth() > 0.0F) {
									double d5 = entity1.posX - entity.posX;
									double d7 = entity1.posZ - entity.posZ;
									double d6 = entity1.posY - entity.posY;
									double d1 = entity.posY - (entity1.posY);
									double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
									float f11 = (float) (-(Math.atan2(d1, d3) * 180.0D / Math.PI));
									
									if (flag) {
										entity.rotationYawHead = entity.rotationYaw = entity.renderYawOffset = entity.rotation = entity.rote
												= -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
										entity.rotationp = entity.rotationPitch = -f11 + 0;
										double ddx = Math.abs(d5);
										double ddz = Math.abs(d7);
										if ((ddx > max || ddz > max)) {
											if(!rf_mode)MoveS(entity, sp, 1, entity1.posX, entity1.posY, entity1.posZ, 0);
										}
										entity.noflag = 0;
									} else {
										++entity.noflag;
										if(entity.noflag > 100) {
											{
												if(!rf_mode)MoveS(entity, sp, 1, entity1.posX, entity1.posY, entity1.posZ, 0);
											}
										}else {
										}
									}
									if (flag) {
										entity.targetentity = (EntityLivingBase) entity1;
									}
									break;
								} 
							}//target
						}
						else if(entity.getMoveT() == 2) {//pointattack
							{//target
								{
									double d5 = entity.getMoveX() - entity.posX;
									double d7 = entity.getMoveZ()- entity.posZ;
									double d6 = entity.getMoveY() - entity.posY;
									double d1 = entity.posY - (entity.getMoveY());
									double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
									float f11 = (float) (-(Math.atan2(d1, d3) * 180.0D / Math.PI));
									 
									{
										entity.rotationYawHead = entity.rotationYaw = entity.renderYawOffset = entity.rotation = entity.rote
												= -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
										entity.rotationp = entity.rotationPitch = -f11 + 0;
										double ddx = Math.abs(d5);
										double ddz = Math.abs(d7);
										if ((ddx > 2 || ddz > 2)){
											//if(!rf_mode)MoveS(entity, sp, 1, entity.getMoveX(), entity.getMoveY(), entity.getMoveZ(), 1);
											MoveS(entity, sp, 1, entity.getMoveX(), entity.getMoveY(), entity.getMoveZ(), 1);
										}else {
											entity.setMoveT(3);
										}
										
										{
											double x4 = entity.posX;
											double y4 = entity.posY;
											double z4 = entity.posZ;
											double han4 = 20;
											AxisAlignedBB axisalignedbb4 = (new AxisAlignedBB((double) (x4 - han4), (double) (y4 - han4),
													(double) (z4 - han4), (double) (x4 + han4), (double) (y4 + han4), (double) (z4 + han4)))
															.grow(han4);
											List<Entity> llist4 = entity.world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb4);
											if (llist != null) {
												for (int lj4 = 0; lj4 < llist4.size(); lj4++) {
													Entity entity4 = (Entity) llist4.get(lj4);
													if (entity4.canBeCollidedWith()) {
														if (entity4 != null && entity4 instanceof EntityDoll) {
															EntityDoll doll = (EntityDoll) entity4;
			    	        								if (doll.getOwner() ==   entity.getOwner() && doll.getSquadID() == entity.getSquadID()) 
			    	        								{
			    	        									//System.out.println(String.format("en"));
																double d54 = entity4.posX - entity.posX;
																double d74 = entity4.posZ - entity.posZ;
																double d64 = entity4.posY - entity.posY;
																double d14 = entity.posY - (entity1.posY);
																double d34 = (double) MathHelper.sqrt(d54 * d54 + d74 * d74);
																double ddx4 = Math.abs(d54);
																double ddz4 = Math.abs(d74);
																if ((ddx4 > 16 || ddz4 > 16)) {
																	entity.setPositionAndUpdate(entity4.posX, entity4.posY, entity4.posZ);
																}
			    	        								}
														}
													}
												}
											}
										}
									}
									break;
								} 
							}//target
						}
						
						
						
						
					}
				}
				//mob
			}
		}
		if(!entity.getattacktask()){
			boolean item = false;
			item = stay_debug(entity, id);
			if(!item)stay(entity, id);
			//entity.setattacktask(false);
		}else{
			//entity.sneak = true;
			//entity.setattacktask(true);
		}
		if(entity.targetentity != null) {
			//entity.setattacktask(true);
		}else {
			//entity.setattacktask(false);
		}
		
		{
			if(entity.getattacktask()){
				if(entity.gun_class == 3
						|| entity.gun_class == 4) {
					if(!entity.on_meve) {
						entity.setSneak(true);
					}else {
						entity.setSneak(false);
					}
				}
			}else {
				entity.setSneak(false);
			}
		}
	}
	
	public static void MoveS(EntityDollBase entity, double speed, double han, double ex, double ey, double ez, int id){
		
		entity.on_meve = true;
		
		{// 1
			//if(!entity.world.isRemote)
			{
				double d5 = ex - entity.posX;
				double d7 = ez - entity.posZ;
				double root = Math.sqrt(d5 * d5 + d7 * d7);
				//entity.rotationYaw = entity.renderYawOffset = -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
				float yawset = -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
				float yaw = yawset * (2 * (float) Math.PI / 360);
				double mox = 0;
				double moy = entity.motionY;
				double moz = 0;
				mox -= MathHelper.sin(yaw) * speed * 1.5;
				moz += MathHelper.cos(yaw) * speed * 1.5;
				if (entity.onGround) {
					
				}
				entity.stepHeight = entity.height * 0.8F;
				
				boolean flag = true;
				{
					Vec3d lock = Vec3d.fromPitchYaw(entity.rotationPitch, entity.rotationYaw);//entity.getForward();
					BlockPos pos0 = new BlockPos(entity.posX + lock.x * 1, entity.posY - 0,entity.posZ + lock.z * 1);
					BlockPos pos1 = new BlockPos(entity.posX + lock.x * 1, entity.posY - 1,entity.posZ + lock.z * 1);
					BlockPos pos2 = new BlockPos(entity.posX + lock.x * 1, entity.posY - 2,entity.posZ + lock.z * 1);
					if (entity.world.getBlockState(pos0).getBlock() == Blocks.LAVA
							|| entity.world.getBlockState(pos0).getBlock() == Blocks.FLOWING_LAVA) {
						 flag = false;
					}
					if (entity.world.getBlockState(pos1).getBlock() == Blocks.LAVA
							|| entity.world.getBlockState(pos1).getBlock() == Blocks.FLOWING_LAVA) {
						 flag = false;
					}
					if (entity.world.getBlockState(pos2).getBlock() == Blocks.LAVA
							|| entity.world.getBlockState(pos2).getBlock() == Blocks.FLOWING_LAVA) {
						 flag = false;
					}
				}
				double ddx = Math.abs(d5);
				double ddz = Math.abs(d7);
				if ((ddx < han && ddz < han)) {
					flag = false;
				}
				//if(entity.world.rand.nextInt(4) == 0) 
				{
					//entity.getNavigator().tryMoveToXYZ(ex, ey, ez, speed * 10);
				//	if(id == 0 || !(entity.getHealth() > 0))
					if(flag){
						/*if(id == 1) {
							entity.getNavigator().tryMoveToXYZ(ex, ey, ez, speed * 15);
						}else */
						{
							entity.motionX = mox;
							entity.motionZ = moz;
							//entity.motionY = y;
							entity.move(MoverType.PLAYER, entity.motionX, entity.motionY, entity.motionZ);
						}
					}
					/*else 
					{
						if(entity.cooltime3 > 20) {
							entity.getNavigator().tryMoveToXYZ(ex, ey, ez, speed * 15);
							entity.cooltime3 = 0;
						}
						
					}*/
				}
			}
		} // 1
	}
	
	public static boolean stay_debug(EntityDollBase entity, int id) {
		boolean item = false;
		{
			double x = entity.posX;
			double y = entity.posY;
			double z = entity.posZ;
			double han = 10;
			AxisAlignedBB axisalignedbb2 = (new AxisAlignedBB((double) (x - han), (double) (y - han),
					(double) (z - han), (double) (x + han), (double) (y + han), (double) (z + han)))
							.grow(han);
			List<Entity> llist = entity.world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb2);
			if (llist != null) {
				//player
				for (int lj = 0; lj < llist.size(); lj++) {
					Entity entity1 = (Entity) llist.get(lj);
					if (entity1.canBeCollidedWith()) {
						if (entity.getMoveT() == 3)//follow
						{
							if (entity1 != null && entity1 instanceof EntityPlayer) {
								EntityPlayer player = (EntityPlayer) entity1;
								if (entity.isOwner(player) && !entity.isRiding()) {
									double d5 = entity1.posX - entity.posX;
									double d7 = entity1.posZ - entity.posZ;
									double d6 = entity1.posY - entity.posY;
									double d1 = entity.posY - (entity1.posY);
									double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
									float f11 = (float) (-(Math.atan2(d1, d3) * 180.0D / Math.PI));
									ItemStack itemstack = player.getHeldItemMainhand();
									//if (!itemstack.isEmpty() && itemstack.getItem() == Item.getItemFromBlock(Blocks.RED_FLOWER))
									if (!itemstack.isEmpty() && itemstack.getItem() == Items.DIAMOND)
									{
										entity.rotationYawHead = entity.rotationYaw = entity.renderYawOffset = entity.rotation = entity.rote = -((float) Math
												.atan2(d5, d7)) * 180.0F / (float) Math.PI;
										entity.rotationp = entity.rotationPitch = -f11 + 0;
										item = true;
									}
								}
							}
						}
					}
				}
				//player
			}
		}
		return item;
	}
	
	
	public static void stay(EntityDollBase entity, int id) {
		entity.setMobMode(0);
		double xPosition = 0;
	    double yPosition = 0;
	    double zPosition = 0;
		if (entity.getIdleTime() >= 100)
        {
        }
        else if (entity.getRNG().nextInt(120) != 0)
        {
        }
        else
        {
            Vec3d vec3 = RandomPositionGenerator.findRandomTarget(entity, 10, 7);

            if (vec3 == null)
            {
            }
            else
            {
                xPosition = vec3.x;
                yPosition = vec3.y;
                zPosition = vec3.z;
            }
            entity.rotation = entity.rotationYawHead = entity.getRNG().nextInt(120) - 60;
        }
		{
			if(entity.rote > 180F){
				entity.rote = -179F;
			}
			if(entity.rote < -180F){
				entity.rote = 179F;
			}
			float f3 = (float) (entity.rotationYawHead - entity.rote);
			 if(entity.rotationYawHead != entity.rote){
         		if(f3 > 1){
 					if(f3 > 180F){
 						entity.rotationYaw = entity.rotationYaw + 1;
 					}else{
 						entity.rotationYaw = entity.rotationYaw - 1;
 					}
 				}
 				else if(f3 < -1){
 					if(f3 < -180F){
 						entity.rotationYaw = entity.rotationYaw - 1;
 					}else{
 						entity.rotationYaw = entity.rotationYaw + 1;
 					}
 				}
	            }
		}
		if(id != 0 && entity.getMoveT() != 3) {
			entity.getNavigator().tryMoveToXYZ(xPosition, yPosition, zPosition, 1D);
		}
		if(entity.isRiding()) {
			/*if(entity.getAIType() == 3) 
			{
				if(entity.ontick <= 5) {
					entity.rotationPitch = entity.rotationPitch - 4;
				}else if(entity.ontick >= 5 && entity.ontick <= 15) {
					entity.rotationPitch = entity.rotationPitch + 4;
				}else {
					entity.rotationPitch = entity.rotationPitch - 4;
				}
			}*/
		}
	}
	public static void newmove_dummy(EntityDoll_Dummy entity, int id, float sp, float turnspeed, double max, double range1, double range2, double followrange) {
		entity.sneak = false;
		//boolean ta = false;
		double range = range1;
		if(entity.getMobMode() == 1){
			range = range2;
		}
		
		entity.on_meve = false;
		
		boolean rf_mode = false;
		{
			if(entity.getattacktask()){
				if(entity.gun_class == 3
						|| entity.gun_class == 4) {
					entity.setSneak(true);
					rf_mode = true;
				}
			}else {
				entity.setSneak(false);
			}
		}
		
		{
			EntityDoll doll = null;
			double x = entity.posX;
			double y = entity.posY;
			double z = entity.posZ;
			double han = range;
			AxisAlignedBB axisalignedbb2 = (new AxisAlignedBB((double) (x - han), (double) (y - han),
					(double) (z - han), (double) (x + han), (double) (y + han), (double) (z + han)))
							.grow(han);
			List<Entity> llist = entity.world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb2);
			if (llist != null) {
				//player
				for (int lj = 0; lj < llist.size(); lj++) {
					Entity entity1 = (Entity) llist.get(lj);
					if (entity1.canBeCollidedWith()) {
						if(entity.getMoveT() == 0 || (entity.getMoveT() == 3 && entity.getattacktask()) || entity.getMoveT() == 5) {//wait
							if (entity1 != null && entity1 instanceof EntityDoll){
								EntityDoll doll2 = (EntityDoll) entity1;
								if(entity.getCustomNameTag().equals(doll2.getCustomNameTag())) {
									doll = doll2;
									break;
								}
							}
						}
					}
				}
			}
			if(doll != null) {
				double d5 = doll.posX - entity.posX;
				double d7 = doll.posZ - entity.posZ;
				double d6 = doll.posY - entity.posY;
				double d1 = entity.posY - (doll.posY);
				double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
				float f11 = (float) (-(Math.atan2(d1, d3) * 180.0D / Math.PI));
				if (!entity.getattacktask()) {
					entity.rotationYawHead = entity.rotationYaw = entity.renderYawOffset = entity.rotation = entity.rote = -((float) Math
							.atan2(d5, d7)) * 180.0F / (float) Math.PI;
					entity.rotationp = entity.rotationPitch = -f11 + 0;
				}
				double ddx = Math.abs(d5);
				double ddz = Math.abs(d7);
				double ix = 0;
				double iz = 0;
				float f12 = doll.rotationYawHead * (2 * (float) Math.PI / 360);
				
				if(entity.getDummyID() == 0) {
					ix -= MathHelper.sin(f12) * -2;
					iz += MathHelper.cos(f12) * -2;
					ix -= MathHelper.sin(f12 + 1.57F) * 2;
					iz += MathHelper.cos(f12 + 1.57F) * 2;
					BlockPos pos = new BlockPos(doll.posX + ix, doll.posY,doll.posZ + iz);
					//if(entity.posX != pos.getX() && entity.posZ != pos.getZ())
						MoveS(entity, sp, 1, pos.getX(), pos.getY(), pos.getZ(), 0);
				}
				else if(entity.getDummyID() == 1) {
					ix -= MathHelper.sin(f12) * -2;
					iz += MathHelper.cos(f12) * -2;
					ix -= MathHelper.sin(f12 + 1.57F) * -2;
					iz += MathHelper.cos(f12 + 1.57F) * -2;
					BlockPos pos = new BlockPos(doll.posX + ix, doll.posY,doll.posZ + iz);
					//if(entity.posX != pos.getX() && entity.posZ != pos.getZ())
						MoveS(entity, sp, 1, pos.getX(), pos.getY(), pos.getZ(), 0);
				}
				else if(entity.getDummyID() == 2) {
					ix -= MathHelper.sin(f12) * -4;
					iz += MathHelper.cos(f12) * -4;
					ix -= MathHelper.sin(f12 + 1.57F) * 1;
					iz += MathHelper.cos(f12 + 1.57F) * 1;
					BlockPos pos = new BlockPos(doll.posX + ix, doll.posY,doll.posZ + iz);
					//if(entity.posX != pos.getX() && entity.posZ != pos.getZ())
						MoveS(entity, sp, 1, pos.getX(), pos.getY(), pos.getZ(), 0);
				}
				else{
					ix -= MathHelper.sin(f12) * -3;
					iz += MathHelper.cos(f12) * -3;
					ix -= MathHelper.sin(f12 + 1.57F) * 1;
					iz += MathHelper.cos(f12 + 1.57F) * 1;
					BlockPos pos = new BlockPos(doll.posX + ix, doll.posY,doll.posZ + iz);
					//if(entity.posX != pos.getX() && entity.posZ != pos.getZ())
						MoveS(entity, sp, 1, pos.getX(), pos.getY(), pos.getZ(), 0);
				}
			}
		}
		
		/*{
			double x = entity.posX;
			double y = entity.posY;
			double z = entity.posZ;
			double han = range;
			AxisAlignedBB axisalignedbb2 = (new AxisAlignedBB((double) (x - han), (double) (y - han),
					(double) (z - han), (double) (x + han), (double) (y + han), (double) (z + han)))
							.grow(han);
			List<Entity> llist = entity.world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb2);
			if (llist != null) {
				//player
				for (int lj = 0; lj < llist.size(); lj++) {
					Entity entity1 = (Entity) llist.get(lj);
					if (entity1.canBeCollidedWith()) {
						if(entity.getMoveT() == 3 || entity.getMoveT() == 0) {//wait
							if (entity1 != null && entity1 instanceof EntityDoll){
								EntityDoll doll = (EntityDoll) entity1;
								if(entity.getCustomNameTag().equals(doll.getCustomNameTag())) {
									
								break;
							}
						}
						if (entity.getMoveT() == 0)//follow
						{
						}
						if (entity1 != null && (entity1 instanceof ISoldier && entity1 instanceof EntityGVCLivingBase) && entity.getMoveT() != 3) {
							EntityGVCLivingBase sold = (EntityGVCLivingBase) entity1;
							
							//System.out.println(String.format("en"));
							double d5 = entity1.posX - entity.posX;
							double d7 = entity1.posZ - entity.posZ;
							double d6 = entity1.posY - entity.posY;
							double d1 = entity.posY - (entity1.posY);
							double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
							double ddx = Math.abs(d5);
							double ddz = Math.abs(d7);
							if ((ddx < 2 && ddz < 2) && sold.getMoveT() != 3) {
								MoveS(entity, -sp, 1, entity1.posX, entity1.posY, entity1.posZ, 0);
							}
							//break;
						}
					}
				}
				//player
			}
			}
		}*/
		{
			double x = entity.posX;
			double y = entity.posY;
			double z = entity.posZ;
			double han = range;
			AxisAlignedBB axisalignedbb2 = 
					(new AxisAlignedBB((double)(x-han), (double)(y-han), (double)(z-han), (double)(x + han), (double)(y + han), (double)(z+ han)))
            		.grow(han);
			//List<Entity> llist = entity.world.getEntitiesWithinAABBExcludingEntity(entity, entity
			//		.getEntityBoundingBox().expand(entity.motionX, entity.motionY, entity.motionZ).grow(range));
			List<Entity> llist = entity.world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb2);
			if (llist != null) {
				//mob
				for (int lj = 0; lj < llist.size(); lj++) {
					Entity entity1 = (Entity) llist.get(lj);
					if (entity1.canBeCollidedWith()) {
						boolean flag = entity.getEntitySenses().canSee(entity1);
						if(entity.targetentity == entity1 && entity1 != null && entity1 instanceof EntityLivingBase) {
							EntityLivingBase en = (EntityLivingBase) entity1;
							if(en.deathTime > 0) {
								entity.targetentity = null;
							}
						}
						if(entity.getMoveT() == 3) {//wait
							if(entity.targetentity == entity1 && entity1 != null) {
								double d5 = entity.targetentity.posX - entity.posX;
								double d7 = entity.targetentity.posZ - entity.posZ;
								double d6 = entity.targetentity.posY - entity.posY;
								double d1 = entity.posY - (entity.targetentity.posY);
								double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
								float f11 = (float) (-(Math.atan2(d1, d3) * 180.0D / Math.PI));
								entity.rotationYaw = entity.renderYawOffset = entity.rotationYawHead = entity.rotation = entity.rote
										= -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
								entity.rotationp = entity.rotationPitch = -f11 + 0;
								break;
							}
						}
						else if(entity.getMoveT() == 5) {//pointfire
							{
								double d5 = entity.getMoveX() - entity.posX;
								double d7 = entity.getMoveZ()- entity.posZ;
								double d6 = entity.getMoveY() - entity.posY;
								double d1 = entity.posY - (entity.getMoveY());
								double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
								float f11 = (float) (-(Math.atan2(d1, d3) * 180.0D / Math.PI));
								 
								{
									entity.rotationYawHead = entity.rotationYaw = entity.renderYawOffset = entity.rotation = entity.rote
											= -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
									entity.rotationp = entity.rotationPitch = -f11 + 0;
									double ddx = Math.abs(d5);
									double ddz = Math.abs(d7);
								}
								break;
							} 
						}
						else if(entity.getMoveT() == 0) {//follow
							if(entity.targetentity == entity1 && entity1 != null) {
								double d5 = entity.targetentity.posX - entity.posX;
								double d7 = entity.targetentity.posZ - entity.posZ;
								double d6 = entity.targetentity.posY - entity.posY;
								double d1 = entity.posY - (entity.targetentity.posY);
								double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
								float f11 = (float) (-(Math.atan2(d1, d3) * 180.0D / Math.PI));
								entity.rotationYaw = entity.renderYawOffset = entity.rotationYawHead = entity.rotation = entity.rote
										= -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
								entity.rotationp = entity.rotationPitch = -f11 + 0;
								if (flag) {
									entity.targetentity = (EntityLivingBase) entity1;
								}
							}
							
						}
						else if(entity.getMoveT() == 1) {//free
							boolean medic = false;
							ItemStack feet = entity.getItemStackFromSlot(EntityEquipmentSlot.FEET);
							
							if (entity.CanAttack(entity1) && entity1 != null && !medic) {//target
								
								if (entity.targetentity == entity1
										&& ((EntityLivingBase) entity1).getHealth() > 0.0F) {
									double d5 = entity1.posX - entity.posX;
									double d7 = entity1.posZ - entity.posZ;
									double d6 = entity1.posY - entity.posY;
									double d1 = entity.posY - (entity1.posY);
									double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
									float f11 = (float) (-(Math.atan2(d1, d3) * 180.0D / Math.PI));
									
									if (flag) {
										entity.rotationYawHead = entity.rotationYaw = entity.renderYawOffset = entity.rotation = entity.rote
												= -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
										entity.rotationp = entity.rotationPitch = -f11 + 0;
										double ddx = Math.abs(d5);
										double ddz = Math.abs(d7);
										/*if ((ddx > max || ddz > max)) {
											if(!rf_mode)MoveS(entity, sp, 1, entity1.posX, entity1.posY, entity1.posZ, 0);
										}*/
										entity.noflag = 0;
									} else {
										/*++entity.noflag;
										if(entity.noflag > 100) {
											{
												if(!rf_mode)MoveS(entity, sp, 1, entity1.posX, entity1.posY, entity1.posZ, 0);
											}
										}else {
										}*/
									}
									if (flag) {
										entity.targetentity = (EntityLivingBase) entity1;
									}
									break;
								} 
							}//target
						}
						else if(entity.getMoveT() == 2) {//pointattack
							{//target
								{
									double d5 = entity.getMoveX() - entity.posX;
									double d7 = entity.getMoveZ()- entity.posZ;
									double d6 = entity.getMoveY() - entity.posY;
									double d1 = entity.posY - (entity.getMoveY());
									double d3 = (double) MathHelper.sqrt(d5 * d5 + d7 * d7);
									float f11 = (float) (-(Math.atan2(d1, d3) * 180.0D / Math.PI));
									 
									{
										entity.rotationYawHead = entity.rotationYaw = entity.renderYawOffset = entity.rotation = entity.rote
												= -((float) Math.atan2(d5, d7)) * 180.0F / (float) Math.PI;
										entity.rotationp = entity.rotationPitch = -f11 + 0;
										double ddx = Math.abs(d5);
										double ddz = Math.abs(d7);
										if ((ddx > 2 || ddz > 2)){
											//if(!rf_mode)MoveS(entity, sp, 1, entity.getMoveX(), entity.getMoveY(), entity.getMoveZ(), 1);
											MoveS(entity, sp, 1, entity.getMoveX(), entity.getMoveY(), entity.getMoveZ(), 1);
										}else {
											entity.setMoveT(3);
										}
										
										{
											double x4 = entity.posX;
											double y4 = entity.posY;
											double z4 = entity.posZ;
											double han4 = 20;
											AxisAlignedBB axisalignedbb4 = (new AxisAlignedBB((double) (x4 - han4), (double) (y4 - han4),
													(double) (z4 - han4), (double) (x4 + han4), (double) (y4 + han4), (double) (z4 + han4)))
															.grow(han4);
											List<Entity> llist4 = entity.world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb4);
											if (llist != null) {
												for (int lj4 = 0; lj4 < llist4.size(); lj4++) {
													Entity entity4 = (Entity) llist4.get(lj4);
													if (entity4.canBeCollidedWith()) {
														if (entity4 != null && entity4 instanceof EntityDoll) {
															EntityDoll doll = (EntityDoll) entity4;
			    	        								if (doll.getOwner() ==   entity.getOwner() && doll.getSquadID() == entity.getSquadID()) 
			    	        								{
			    	        									//System.out.println(String.format("en"));
																double d54 = entity4.posX - entity.posX;
																double d74 = entity4.posZ - entity.posZ;
																double d64 = entity4.posY - entity.posY;
																double d14 = entity.posY - (entity1.posY);
																double d34 = (double) MathHelper.sqrt(d54 * d54 + d74 * d74);
																double ddx4 = Math.abs(d54);
																double ddz4 = Math.abs(d74);
																if ((ddx4 > 16 || ddz4 > 16)) {
																	entity.setPositionAndUpdate(entity4.posX, entity4.posY, entity4.posZ);
																}
			    	        								}
														}
													}
												}
											}
										}
									}
									break;
								} 
							}//target
						}
						
						
						
						
					}
				}
				//mob
			}
		}
		if(!entity.getattacktask()){
			boolean item = false;
			item = stay_debug(entity, id);
			if(!item)stay(entity, id);
			//entity.setattacktask(false);
		}else{
			//entity.sneak = true;
			//entity.setattacktask(true);
		}
		if(entity.targetentity != null) {
			//entity.setattacktask(true);
		}else {
			//entity.setattacktask(false);
		}
		
		{
			if(entity.getattacktask()){
				if(entity.gun_class == 3
						|| entity.gun_class == 4) {
					if(!entity.on_meve) {
						entity.setSneak(true);
					}else {
						entity.setSneak(false);
					}
				}
			}else {
				entity.setSneak(false);
			}
		}
	}
}

