001    package net.minecraft.src;
002    
003    import java.util.List;
004    
005    public class EntityAIAvoidEntity extends EntityAIBase
006    {
007        /** The entity we are attached to */
008        private EntityCreature theEntity;
009        private float farSpeed;
010        private float nearSpeed;
011        private Entity closestLivingEntity;
012        private float distanceFromEntity;
013        private PathEntity field_75374_f;
014    
015        /** The PathNavigate of our entity */
016        private PathNavigate entityPathNavigate;
017    
018        /** The class of the entity we should avoid */
019        private Class targetEntityClass;
020    
021        public EntityAIAvoidEntity(EntityCreature par1EntityCreature, Class par2Class, float par3, float par4, float par5)
022        {
023            this.theEntity = par1EntityCreature;
024            this.targetEntityClass = par2Class;
025            this.distanceFromEntity = par3;
026            this.farSpeed = par4;
027            this.nearSpeed = par5;
028            this.entityPathNavigate = par1EntityCreature.getNavigator();
029            this.setMutexBits(1);
030        }
031    
032        /**
033         * Returns whether the EntityAIBase should begin execution.
034         */
035        public boolean shouldExecute()
036        {
037            if (this.targetEntityClass == EntityPlayer.class)
038            {
039                if (this.theEntity instanceof EntityTameable && ((EntityTameable)this.theEntity).isTamed())
040                {
041                    return false;
042                }
043    
044                this.closestLivingEntity = this.theEntity.worldObj.getClosestPlayerToEntity(this.theEntity, (double)this.distanceFromEntity);
045    
046                if (this.closestLivingEntity == null)
047                {
048                    return false;
049                }
050            }
051            else
052            {
053                List var1 = this.theEntity.worldObj.getEntitiesWithinAABB(this.targetEntityClass, this.theEntity.boundingBox.expand((double)this.distanceFromEntity, 3.0D, (double)this.distanceFromEntity));
054    
055                if (var1.isEmpty())
056                {
057                    return false;
058                }
059    
060                this.closestLivingEntity = (Entity)var1.get(0);
061            }
062    
063            if (!this.theEntity.getEntitySenses().canSee(this.closestLivingEntity))
064            {
065                return false;
066            }
067            else
068            {
069                Vec3 var2 = RandomPositionGenerator.findRandomTargetBlockAwayFrom(this.theEntity, 16, 7, Vec3.getVec3Pool().getVecFromPool(this.closestLivingEntity.posX, this.closestLivingEntity.posY, this.closestLivingEntity.posZ));
070    
071                if (var2 == null)
072                {
073                    return false;
074                }
075                else if (this.closestLivingEntity.getDistanceSq(var2.xCoord, var2.yCoord, var2.zCoord) < this.closestLivingEntity.getDistanceSqToEntity(this.theEntity))
076                {
077                    return false;
078                }
079                else
080                {
081                    this.field_75374_f = this.entityPathNavigate.getPathToXYZ(var2.xCoord, var2.yCoord, var2.zCoord);
082                    return this.field_75374_f == null ? false : this.field_75374_f.isDestinationSame(var2);
083                }
084            }
085        }
086    
087        /**
088         * Returns whether an in-progress EntityAIBase should continue executing
089         */
090        public boolean continueExecuting()
091        {
092            return !this.entityPathNavigate.noPath();
093        }
094    
095        /**
096         * Execute a one shot task or start executing a continuous task
097         */
098        public void startExecuting()
099        {
100            this.entityPathNavigate.setPath(this.field_75374_f, this.farSpeed);
101        }
102    
103        /**
104         * Resets the task
105         */
106        public void resetTask()
107        {
108            this.closestLivingEntity = null;
109        }
110    
111        /**
112         * Updates the task
113         */
114        public void updateTask()
115        {
116            if (this.theEntity.getDistanceSqToEntity(this.closestLivingEntity) < 49.0D)
117            {
118                this.theEntity.getNavigator().setSpeed(this.nearSpeed);
119            }
120            else
121            {
122                this.theEntity.getNavigator().setSpeed(this.farSpeed);
123            }
124        }
125    }