package game.ai; import java.util.List; import java.util.function.Predicate; import game.entity.Entity; import game.entity.types.EntityLiving; import game.pathfinding.PathEntity; import game.pathfinding.PathNavigate; import game.util.Predicates; import game.world.Vec3; public class EntityAIAvoidEntity extends EntityAIBase { private final Predicate canBeSeenSelector; /** The entity we are attached to */ protected EntityLiving theEntity; private double farSpeed; private double nearSpeed; protected T closestLivingEntity; private float avoidDistance; /** The PathEntity of our entity */ private PathEntity entityPathEntity; /** The PathNavigate of our entity */ private PathNavigate entityPathNavigate; private Class classToAvoid; private Predicate avoidTargetSelector; public EntityAIAvoidEntity(EntityLiving theEntityIn, Class classToAvoidIn, float avoidDistanceIn, double farSpeedIn, double nearSpeedIn) { this(theEntityIn, classToAvoidIn, Predicates.alwaysTrue(), avoidDistanceIn, farSpeedIn, nearSpeedIn); } public EntityAIAvoidEntity(EntityLiving theEntityIn, Class classToAvoidIn, Predicate avoidTargetSelectorIn, float avoidDistanceIn, double farSpeedIn, double nearSpeedIn) { this.canBeSeenSelector = new Predicate() { public boolean test(Entity p_apply_1_) { return p_apply_1_.isEntityAlive() && EntityAIAvoidEntity.this.theEntity.getEntitySenses().canSee(p_apply_1_); } }; this.theEntity = theEntityIn; this.classToAvoid = classToAvoidIn; this.avoidTargetSelector = avoidTargetSelectorIn; this.avoidDistance = avoidDistanceIn; this.farSpeed = farSpeedIn; this.nearSpeed = nearSpeedIn; this.entityPathNavigate = theEntityIn.getNavigator(); this.setMutexBits(1); } /** * Returns whether the EntityAIBase should begin execution. */ public boolean shouldExecute() { List list = this.theEntity.worldObj.getEntitiesWithinAABB(this.classToAvoid, this.theEntity.getEntityBoundingBox().expand((double)this.avoidDistance, 3.0D, (double)this.avoidDistance), Predicates.and(new Predicate[] {this.canBeSeenSelector, this.avoidTargetSelector})); if (list.isEmpty()) { return false; } else { this.closestLivingEntity = list.get(0); Vec3 vec3 = RandomPositionGenerator.findRandomTargetBlockAwayFrom(this.theEntity, 16, 7, new Vec3(this.closestLivingEntity.posX, this.closestLivingEntity.posY, this.closestLivingEntity.posZ)); if (vec3 == null) { return false; } else if (this.closestLivingEntity.getDistanceSq(vec3.xCoord, vec3.yCoord, vec3.zCoord) < this.closestLivingEntity.getDistanceSqToEntity(this.theEntity)) { return false; } else { this.entityPathEntity = this.entityPathNavigate.getPathToXYZ(vec3.xCoord, vec3.yCoord, vec3.zCoord); return this.entityPathEntity == null ? false : this.entityPathEntity.isDestinationSame(vec3); } } } /** * Returns whether an in-progress EntityAIBase should continue executing */ public boolean continueExecuting() { return !this.entityPathNavigate.noPath(); } /** * Execute a one shot task or start executing a continuous task */ public void startExecuting() { this.entityPathNavigate.setPath(this.entityPathEntity, this.farSpeed); } /** * Resets the task */ public void resetTask() { this.closestLivingEntity = null; } /** * Updates the task */ public void updateTask() { if (this.theEntity.getDistanceSqToEntity(this.closestLivingEntity) < 49.0D) { this.theEntity.getNavigator().setSpeed(this.nearSpeed); } else { this.theEntity.getNavigator().setSpeed(this.farSpeed); } } }