    001    package net.minecraft.src;
    002
    003    import java.util.Iterator;
    004    import java.util.List;
    005
    006    public class EntityAIPlay extends EntityAIBase
    007    {
    008        private EntityVillager villagerObj;
    009        private EntityLiving targetVillager;
    010        private float field_75261_c;
    011        private int playTime;
    012
    013        public EntityAIPlay(EntityVillager par1EntityVillager, float par2)
    014        {
    015            this.villagerObj = par1EntityVillager;
    016            this.field_75261_c = par2;
    017            this.setMutexBits(1);
    018        }
    019
    020        /**
    021         * Returns whether the EntityAIBase should begin execution.
    022         */
    023        public boolean shouldExecute()
    024        {
    025            if (this.villagerObj.getGrowingAge() >= 0)
    026            {
    027                return false;
    028            }
    029            else if (this.villagerObj.getRNG().nextInt(400) != 0)
    030            {
    031                return false;
    032            }
    033            else
    034            {
    035                List var1 = this.villagerObj.worldObj.getEntitiesWithinAABB(EntityVillager.class, this.villagerObj.boundingBox.expand(6.0D, 3.0D, 6.0D));
    036                double var2 = Double.MAX_VALUE;
    037                Iterator var4 = var1.iterator();
    038
    039                while (var4.hasNext())
    040                {
    041                    EntityVillager var5 = (EntityVillager)var4.next();
    042
    043                    if (var5 != this.villagerObj && !var5.isPlaying() && var5.getGrowingAge() < 0)
    044                    {
    045                        double var6 = var5.getDistanceSqToEntity(this.villagerObj);
    046
    047                        if (var6 <= var2)
    048                        {
    049                            var2 = var6;
    050                            this.targetVillager = var5;
    051                        }
    052                    }
    053                }
    054
    055                if (this.targetVillager == null)
    056                {
    057                    Vec3 var8 = RandomPositionGenerator.findRandomTarget(this.villagerObj, 16, 3);
    058
    059                    if (var8 == null)
    060                    {
    061                        return false;
    062                    }
    063                }
    064
    065                return true;
    066            }
    067        }
    068
    069        /**
    070         * Returns whether an in-progress EntityAIBase should continue executing
    071         */
    072        public boolean continueExecuting()
    073        {
    074            return this.playTime > 0;
    075        }
    076
    077        /**
    078         * Execute a one shot task or start executing a continuous task
    079         */
    080        public void startExecuting()
    081        {
    082            if (this.targetVillager != null)
    083            {
    084                this.villagerObj.setPlaying(true);
    085            }
    086
    087            this.playTime = 1000;
    088        }
    089
    090        /**
    091         * Resets the task
    092         */
    093        public void resetTask()
    094        {
    095            this.villagerObj.setPlaying(false);
    096            this.targetVillager = null;
    097        }
    098
    099        /**
    100         * Updates the task
    101         */
    102        public void updateTask()
    103        {
    104            --this.playTime;
    105
    106            if (this.targetVillager != null)
    107            {
    108                if (this.villagerObj.getDistanceSqToEntity(this.targetVillager) > 4.0D)
    109                {
    110                    this.villagerObj.getNavigator().tryMoveToEntityLiving(this.targetVillager, this.field_75261_c);
    111                }
    112            }
    113            else if (this.villagerObj.getNavigator().noPath())
    114            {
    115                Vec3 var1 = RandomPositionGenerator.findRandomTarget(this.villagerObj, 16, 3);
    116
    117                if (var1 == null)
    118                {
    119                    return;
    120                }
    121
    122                this.villagerObj.getNavigator().tryMoveToXYZ(var1.xCoord, var1.yCoord, var1.zCoord, this.field_75261_c);
    123            }
    124        }
    125    }