| 1 | package net.minecraft.entity.ai;
|
| 2 |
|
| 3 | import com.google.common.base.Predicate;
|
| 4 | import com.google.common.base.Predicates;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.entity.Entity;
|
| 7 | import net.minecraft.entity.EntityCreature;
|
| 8 | import net.minecraft.pathfinding.PathEntity;
|
| 9 | import net.minecraft.pathfinding.PathNavigate;
|
| 10 | import net.minecraft.util.EntitySelectors;
|
| 11 | import net.minecraft.util.Vec3;
|
| 12 |
|
| 13 | public class EntityAIAvoidEntity<T extends Entity> extends EntityAIBase
|
| 14 | {
|
| 15 | private final Predicate<Entity> canBeSeenSelector;
|
| 16 |
|
| 17 | /** The entity we are attached to */
|
| 18 | protected EntityCreature theEntity;
|
| 19 | private double farSpeed;
|
| 20 | private double nearSpeed;
|
| 21 | protected T closestLivingEntity;
|
| 22 | private float avoidDistance;
|
| 23 |
|
| 24 | /** The PathEntity of our entity */
|
| 25 | private PathEntity entityPathEntity;
|
| 26 |
|
| 27 | /** The PathNavigate of our entity */
|
| 28 | private PathNavigate entityPathNavigate;
|
| 29 | private Class<T> classToAvoid;
|
| 30 | private Predicate <? super T > avoidTargetSelector;
|
| 31 |
|
| 32 | public EntityAIAvoidEntity(EntityCreature theEntityIn, Class<T> classToAvoidIn, float avoidDistanceIn, double farSpeedIn, double nearSpeedIn)
|
| 33 | {
|
| 34 | this(theEntityIn, classToAvoidIn, Predicates.<T>alwaysTrue(), avoidDistanceIn, farSpeedIn, nearSpeedIn);
|
| 35 | }
|
| 36 |
|
| 37 | public EntityAIAvoidEntity(EntityCreature theEntityIn, Class<T> classToAvoidIn, Predicate <? super T > avoidTargetSelectorIn, float avoidDistanceIn, double farSpeedIn, double nearSpeedIn)
|
| 38 | {
|
| 39 | this.canBeSeenSelector = new Predicate<Entity>()
|
| 40 | {
|
| 41 | public boolean apply(Entity p_apply_1_)
|
| 42 | {
|
| 43 | return p_apply_1_.isEntityAlive() && EntityAIAvoidEntity.this.theEntity.getEntitySenses().canSee(p_apply_1_);
|
| 44 | }
|
| 45 | };
|
| 46 | this.theEntity = theEntityIn;
|
| 47 | this.classToAvoid = classToAvoidIn;
|
| 48 | this.avoidTargetSelector = avoidTargetSelectorIn;
|
| 49 | this.avoidDistance = avoidDistanceIn;
|
| 50 | this.farSpeed = farSpeedIn;
|
| 51 | this.nearSpeed = nearSpeedIn;
|
| 52 | this.entityPathNavigate = theEntityIn.getNavigator();
|
| 53 | this.setMutexBits(1);
|
| 54 | }
|
| 55 |
|
| 56 | /**
|
| 57 | * Returns whether the EntityAIBase should begin execution.
|
| 58 | */
|
| 59 | public boolean shouldExecute()
|
| 60 | {
|
| 61 | List<T> list = this.theEntity.worldObj.<T>getEntitiesWithinAABB(this.classToAvoid, this.theEntity.getEntityBoundingBox().expand((double)this.avoidDistance, 3.0D, (double)this.avoidDistance), Predicates.and(new Predicate[] {EntitySelectors.NOT_SPECTATING, this.canBeSeenSelector, this.avoidTargetSelector}));
|
| 62 |
|
| 63 | if (list.isEmpty())
|
| 64 | {
|
| 65 | return false;
|
| 66 | }
|
| 67 | else
|
| 68 | {
|
| 69 | this.closestLivingEntity = list.get(0);
|
| 70 | Vec3 vec3 = RandomPositionGenerator.findRandomTargetBlockAwayFrom(this.theEntity, 16, 7, new Vec3(this.closestLivingEntity.posX, this.closestLivingEntity.posY, this.closestLivingEntity.posZ));
|
| 71 |
|
| 72 | if (vec3 == null)
|
| 73 | {
|
| 74 | return false;
|
| 75 | }
|
| 76 | else if (this.closestLivingEntity.getDistanceSq(vec3.xCoord, vec3.yCoord, vec3.zCoord) < this.closestLivingEntity.getDistanceSqToEntity(this.theEntity))
|
| 77 | {
|
| 78 | return false;
|
| 79 | }
|
| 80 | else
|
| 81 | {
|
| 82 | this.entityPathEntity = this.entityPathNavigate.getPathToXYZ(vec3.xCoord, vec3.yCoord, vec3.zCoord);
|
| 83 | return this.entityPathEntity == null ? false : this.entityPathEntity.isDestinationSame(vec3);
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | /**
|
| 89 | * Returns whether an in-progress EntityAIBase should continue executing
|
| 90 | */
|
| 91 | public boolean continueExecuting()
|
| 92 | {
|
| 93 | return !this.entityPathNavigate.noPath();
|
| 94 | }
|
| 95 |
|
| 96 | /**
|
| 97 | * Execute a one shot task or start executing a continuous task
|
| 98 | */
|
| 99 | public void startExecuting()
|
| 100 | {
|
| 101 | this.entityPathNavigate.setPath(this.entityPathEntity, this.farSpeed);
|
| 102 | }
|
| 103 |
|
| 104 | /**
|
| 105 | * Resets the task
|
| 106 | */
|
| 107 | public void resetTask()
|
| 108 | {
|
| 109 | this.closestLivingEntity = null;
|
| 110 | }
|
| 111 |
|
| 112 | /**
|
| 113 | * Updates the task
|
| 114 | */
|
| 115 | public void updateTask()
|
| 116 | {
|
| 117 | if (this.theEntity.getDistanceSqToEntity(this.closestLivingEntity) < 49.0D)
|
| 118 | {
|
| 119 | this.theEntity.getNavigator().setSpeed(this.nearSpeed);
|
| 120 | }
|
| 121 | else
|
| 122 | {
|
| 123 | this.theEntity.getNavigator().setSpeed(this.farSpeed);
|
| 124 | }
|
| 125 | }
|
| 126 | }
|
| 127 |
|