| 1 | package net.minecraft.entity.ai;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Random;
|
| 5 | import net.minecraft.entity.EntityAgeable;
|
| 6 | import net.minecraft.entity.item.EntityXPOrb;
|
| 7 | import net.minecraft.entity.passive.EntityAnimal;
|
| 8 | import net.minecraft.entity.passive.EntityCow;
|
| 9 | import net.minecraft.entity.player.EntityPlayer;
|
| 10 | import net.minecraft.stats.AchievementList;
|
| 11 | import net.minecraft.stats.StatList;
|
| 12 | import net.minecraft.util.EnumParticleTypes;
|
| 13 | import net.minecraft.world.World;
|
| 14 |
|
| 15 | public class EntityAIMate extends EntityAIBase
|
| 16 | {
|
| 17 | private EntityAnimal theAnimal;
|
| 18 | World theWorld;
|
| 19 | private EntityAnimal targetMate;
|
| 20 |
|
| 21 | /**
|
| 22 | * Delay preventing a baby from spawning immediately when two mate-able animals find each other.
|
| 23 | */
|
| 24 | int spawnBabyDelay;
|
| 25 |
|
| 26 | /** The speed the creature moves at during mating behavior. */
|
| 27 | double moveSpeed;
|
| 28 |
|
| 29 | public EntityAIMate(EntityAnimal animal, double speedIn)
|
| 30 | {
|
| 31 | this.theAnimal = animal;
|
| 32 | this.theWorld = animal.worldObj;
|
| 33 | this.moveSpeed = speedIn;
|
| 34 | this.setMutexBits(3);
|
| 35 | }
|
| 36 |
|
| 37 | /**
|
| 38 | * Returns whether the EntityAIBase should begin execution.
|
| 39 | */
|
| 40 | public boolean shouldExecute()
|
| 41 | {
|
| 42 | if (!this.theAnimal.isInLove())
|
| 43 | {
|
| 44 | return false;
|
| 45 | }
|
| 46 | else
|
| 47 | {
|
| 48 | this.targetMate = this.getNearbyMate();
|
| 49 | return this.targetMate != null;
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | /**
|
| 54 | * Returns whether an in-progress EntityAIBase should continue executing
|
| 55 | */
|
| 56 | public boolean continueExecuting()
|
| 57 | {
|
| 58 | return this.targetMate.isEntityAlive() && this.targetMate.isInLove() && this.spawnBabyDelay < 60;
|
| 59 | }
|
| 60 |
|
| 61 | /**
|
| 62 | * Resets the task
|
| 63 | */
|
| 64 | public void resetTask()
|
| 65 | {
|
| 66 | this.targetMate = null;
|
| 67 | this.spawnBabyDelay = 0;
|
| 68 | }
|
| 69 |
|
| 70 | /**
|
| 71 | * Updates the task
|
| 72 | */
|
| 73 | public void updateTask()
|
| 74 | {
|
| 75 | this.theAnimal.getLookHelper().setLookPositionWithEntity(this.targetMate, 10.0F, (float)this.theAnimal.getVerticalFaceSpeed());
|
| 76 | this.theAnimal.getNavigator().tryMoveToEntityLiving(this.targetMate, this.moveSpeed);
|
| 77 | ++this.spawnBabyDelay;
|
| 78 |
|
| 79 | if (this.spawnBabyDelay >= 60 && this.theAnimal.getDistanceSqToEntity(this.targetMate) < 9.0D)
|
| 80 | {
|
| 81 | this.spawnBaby();
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | /**
|
| 86 | * Loops through nearby animals and finds another animal of the same type that can be mated with. Returns the first
|
| 87 | * valid mate found.
|
| 88 | */
|
| 89 | private EntityAnimal getNearbyMate()
|
| 90 | {
|
| 91 | float f = 8.0F;
|
| 92 | List<EntityAnimal> list = this.theWorld.<EntityAnimal>getEntitiesWithinAABB(this.theAnimal.getClass(), this.theAnimal.getEntityBoundingBox().expand((double)f, (double)f, (double)f));
|
| 93 | double d0 = Double.MAX_VALUE;
|
| 94 | EntityAnimal entityanimal = null;
|
| 95 |
|
| 96 | for (EntityAnimal entityanimal1 : list)
|
| 97 | {
|
| 98 | if (this.theAnimal.canMateWith(entityanimal1) && this.theAnimal.getDistanceSqToEntity(entityanimal1) < d0)
|
| 99 | {
|
| 100 | entityanimal = entityanimal1;
|
| 101 | d0 = this.theAnimal.getDistanceSqToEntity(entityanimal1);
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | return entityanimal;
|
| 106 | }
|
| 107 |
|
| 108 | /**
|
| 109 | * Spawns a baby animal of the same type.
|
| 110 | */
|
| 111 | private void spawnBaby()
|
| 112 | {
|
| 113 | EntityAgeable entityageable = this.theAnimal.createChild(this.targetMate);
|
| 114 |
|
| 115 | if (entityageable != null)
|
| 116 | {
|
| 117 | EntityPlayer entityplayer = this.theAnimal.getPlayerInLove();
|
| 118 |
|
| 119 | if (entityplayer == null && this.targetMate.getPlayerInLove() != null)
|
| 120 | {
|
| 121 | entityplayer = this.targetMate.getPlayerInLove();
|
| 122 | }
|
| 123 |
|
| 124 | if (entityplayer != null)
|
| 125 | {
|
| 126 | entityplayer.triggerAchievement(StatList.animalsBredStat);
|
| 127 |
|
| 128 | if (this.theAnimal instanceof EntityCow)
|
| 129 | {
|
| 130 | entityplayer.triggerAchievement(AchievementList.breedCow);
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | this.theAnimal.setGrowingAge(6000);
|
| 135 | this.targetMate.setGrowingAge(6000);
|
| 136 | this.theAnimal.resetInLove();
|
| 137 | this.targetMate.resetInLove();
|
| 138 | entityageable.setGrowingAge(-24000);
|
| 139 | entityageable.setLocationAndAngles(this.theAnimal.posX, this.theAnimal.posY, this.theAnimal.posZ, 0.0F, 0.0F);
|
| 140 | this.theWorld.spawnEntityInWorld(entityageable);
|
| 141 | Random random = this.theAnimal.getRNG();
|
| 142 |
|
| 143 | for (int i = 0; i < 7; ++i)
|
| 144 | {
|
| 145 | double d0 = random.nextGaussian() * 0.02D;
|
| 146 | double d1 = random.nextGaussian() * 0.02D;
|
| 147 | double d2 = random.nextGaussian() * 0.02D;
|
| 148 | double d3 = random.nextDouble() * (double)this.theAnimal.width * 2.0D - (double)this.theAnimal.width;
|
| 149 | double d4 = 0.5D + random.nextDouble() * (double)this.theAnimal.height;
|
| 150 | double d5 = random.nextDouble() * (double)this.theAnimal.width * 2.0D - (double)this.theAnimal.width;
|
| 151 | this.theWorld.spawnParticle(EnumParticleTypes.HEART, this.theAnimal.posX + d3, this.theAnimal.posY + d4, this.theAnimal.posZ + d5, d0, d1, d2, new int[0]);
|
| 152 | }
|
| 153 |
|
| 154 | if (this.theWorld.getGameRules().getBoolean("doMobLoot"))
|
| 155 | {
|
| 156 | this.theWorld.spawnEntityInWorld(new EntityXPOrb(this.theWorld, this.theAnimal.posX, this.theAnimal.posY, this.theAnimal.posZ, random.nextInt(7) + 1));
|
| 157 | }
|
| 158 | }
|
| 159 | }
|
| 160 | }
|
| 161 |
|