001    package net.minecraft.src;
002    
003    public class EntityAIVillagerMate extends EntityAIBase
004    {
005        private EntityVillager villagerObj;
006        private EntityVillager mate;
007        private World worldObj;
008        private int matingTimeout = 0;
009        Village villageObj;
010    
011        public EntityAIVillagerMate(EntityVillager par1EntityVillager)
012        {
013            this.villagerObj = par1EntityVillager;
014            this.worldObj = par1EntityVillager.worldObj;
015            this.setMutexBits(3);
016        }
017    
018        /**
019         * Returns whether the EntityAIBase should begin execution.
020         */
021        public boolean shouldExecute()
022        {
023            if (this.villagerObj.getGrowingAge() != 0)
024            {
025                return false;
026            }
027            else if (this.villagerObj.getRNG().nextInt(500) != 0)
028            {
029                return false;
030            }
031            else
032            {
033                this.villageObj = this.worldObj.villageCollectionObj.findNearestVillage(MathHelper.floor_double(this.villagerObj.posX), MathHelper.floor_double(this.villagerObj.posY), MathHelper.floor_double(this.villagerObj.posZ), 0);
034    
035                if (this.villageObj == null)
036                {
037                    return false;
038                }
039                else if (!this.checkSufficientDoorsPresentForNewVillager())
040                {
041                    return false;
042                }
043                else
044                {
045                    Entity var1 = this.worldObj.findNearestEntityWithinAABB(EntityVillager.class, this.villagerObj.boundingBox.expand(8.0D, 3.0D, 8.0D), this.villagerObj);
046    
047                    if (var1 == null)
048                    {
049                        return false;
050                    }
051                    else
052                    {
053                        this.mate = (EntityVillager)var1;
054                        return this.mate.getGrowingAge() == 0;
055                    }
056                }
057            }
058        }
059    
060        /**
061         * Execute a one shot task or start executing a continuous task
062         */
063        public void startExecuting()
064        {
065            this.matingTimeout = 300;
066            this.villagerObj.setMating(true);
067        }
068    
069        /**
070         * Resets the task
071         */
072        public void resetTask()
073        {
074            this.villageObj = null;
075            this.mate = null;
076            this.villagerObj.setMating(false);
077        }
078    
079        /**
080         * Returns whether an in-progress EntityAIBase should continue executing
081         */
082        public boolean continueExecuting()
083        {
084            return this.matingTimeout >= 0 && this.checkSufficientDoorsPresentForNewVillager() && this.villagerObj.getGrowingAge() == 0;
085        }
086    
087        /**
088         * Updates the task
089         */
090        public void updateTask()
091        {
092            --this.matingTimeout;
093            this.villagerObj.getLookHelper().setLookPositionWithEntity(this.mate, 10.0F, 30.0F);
094    
095            if (this.villagerObj.getDistanceSqToEntity(this.mate) > 2.25D)
096            {
097                this.villagerObj.getNavigator().tryMoveToEntityLiving(this.mate, 0.25F);
098            }
099            else if (this.matingTimeout == 0 && this.mate.isMating())
100            {
101                this.giveBirth();
102            }
103    
104            if (this.villagerObj.getRNG().nextInt(35) == 0)
105            {
106                this.worldObj.setEntityState(this.villagerObj, (byte)12);
107            }
108        }
109    
110        private boolean checkSufficientDoorsPresentForNewVillager()
111        {
112            int var1 = (int)((double)((float)this.villageObj.getNumVillageDoors()) * 0.35D);
113            return this.villageObj.getNumVillagers() < var1;
114        }
115    
116        private void giveBirth()
117        {
118            EntityVillager var1 = new EntityVillager(this.worldObj);
119            this.mate.setGrowingAge(6000);
120            this.villagerObj.setGrowingAge(6000);
121            var1.setGrowingAge(-24000);
122            var1.setProfession(this.villagerObj.getRNG().nextInt(5));
123            var1.setLocationAndAngles(this.villagerObj.posX, this.villagerObj.posY, this.villagerObj.posZ, 0.0F, 0.0F);
124            this.worldObj.spawnEntityInWorld(var1);
125            this.worldObj.setEntityState(var1, (byte)12);
126        }
127    }