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