Files
CustomNPC-Plus/src/main/java/noppes/npcs/ai/EntityAIAvoidTarget.java
T

137 lines
4.8 KiB
Java
Raw Normal View History

2018-09-13 12:03:28 -04:00
package noppes.npcs.ai;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.pathfinding.PathEntity;
import net.minecraft.pathfinding.PathNavigate;
import net.minecraft.util.Vec3;
import noppes.npcs.constants.AiMutex;
import noppes.npcs.constants.EnumNavType;
import noppes.npcs.entity.EntityNPCInterface;
2023-01-12 21:33:06 -05:00
import java.util.List;
2025-03-13 17:14:45 -04:00
public class EntityAIAvoidTarget extends EntityAIBase {
/**
* The entity we are attached to
*/
2018-09-13 12:03:28 -04:00
private EntityNPCInterface theEntity;
private Entity closestLivingEntity;
private float distanceFromEntity;
2025-02-10 21:32:55 -05:00
2018-09-13 12:03:28 -04:00
private float health;
2025-03-13 17:14:45 -04:00
/**
* The PathEntity of our entity
*/
2018-09-13 12:03:28 -04:00
private PathEntity entityPathEntity;
2025-03-13 17:14:45 -04:00
/**
* The PathNavigate of our entity
*/
2018-09-13 12:03:28 -04:00
private PathNavigate entityPathNavigate;
2025-02-10 21:32:55 -05:00
2025-03-13 17:14:45 -04:00
/**
* The class of the entity we should avoid
*/
2018-09-13 12:03:28 -04:00
private Class targetEntityClass;
2025-03-13 17:14:45 -04:00
public EntityAIAvoidTarget(EntityNPCInterface par1EntityNPC) {
2018-09-13 12:03:28 -04:00
this.theEntity = par1EntityNPC;
this.distanceFromEntity = this.theEntity.stats.aggroRange;
this.health = this.theEntity.getHealth();
this.entityPathNavigate = par1EntityNPC.getNavigator();
this.setMutexBits(AiMutex.PASSIVE + AiMutex.LOOK);
}
/**
* Returns whether the EntityAIBase should begin execution.
*/
2025-03-13 17:14:45 -04:00
public boolean shouldExecute() {
EntityLivingBase target = this.theEntity.getAttackTarget();
2025-02-10 21:32:55 -05:00
2025-03-13 17:14:45 -04:00
if (target == null) {
2018-09-13 12:03:28 -04:00
return false;
}
2025-02-10 21:32:55 -05:00
2025-03-13 17:14:45 -04:00
targetEntityClass = target.getClass();
2025-02-10 21:32:55 -05:00
2025-03-13 17:14:45 -04:00
if (this.targetEntityClass == EntityPlayer.class) {
this.closestLivingEntity = this.theEntity.worldObj.getClosestPlayerToEntity(this.theEntity, (double) this.distanceFromEntity);
2018-09-13 12:03:28 -04:00
2025-03-13 17:14:45 -04:00
if (this.closestLivingEntity == null) {
2018-09-13 12:03:28 -04:00
return false;
}
2025-03-13 17:14:45 -04:00
} else {
List var1 = this.theEntity.worldObj.getEntitiesWithinAABB(this.targetEntityClass, this.theEntity.boundingBox.expand((double) this.distanceFromEntity, 3.0D, (double) this.distanceFromEntity));
2018-09-13 12:03:28 -04:00
2025-03-13 17:14:45 -04:00
if (var1.isEmpty()) {
2018-09-13 12:03:28 -04:00
return false;
}
2025-03-13 17:14:45 -04:00
this.closestLivingEntity = (Entity) var1.get(0);
2018-09-13 12:03:28 -04:00
}
2025-03-13 17:14:45 -04:00
if (!this.theEntity.getEntitySenses().canSee(this.closestLivingEntity) && this.theEntity.ais.directLOS) {
2018-09-13 12:03:28 -04:00
return false;
2025-03-13 17:14:45 -04:00
} else {
2018-09-13 12:03:28 -04:00
Vec3 var2 = RandomPositionGeneratorAlt.findRandomTargetBlockAwayFrom(this.theEntity, 16, 7, Vec3.createVectorHelper(this.closestLivingEntity.posX, this.closestLivingEntity.posY, this.closestLivingEntity.posZ));
2025-02-10 21:32:55 -05:00
boolean var3 = this.theEntity.inventory.getProjectile() == null || this.theEntity.ais.useRangeMelee == 2;
2018-09-13 12:03:28 -04:00
boolean var4 = var3 ? this.health == this.theEntity.getHealth() : this.theEntity.getRangedTask() != null && !this.theEntity.getRangedTask().hasFired();
2025-02-10 21:32:55 -05:00
2025-03-13 17:14:45 -04:00
if (var2 == null) {
2018-09-13 12:03:28 -04:00
return false;
2025-03-13 17:14:45 -04:00
} else if (this.closestLivingEntity.getDistanceSq(var2.xCoord, var2.yCoord, var2.zCoord) < this.closestLivingEntity.getDistanceSqToEntity(this.theEntity)) {
2018-09-13 12:03:28 -04:00
return false;
2025-03-13 17:14:45 -04:00
} else if (this.theEntity.ais.tacticalVariant == EnumNavType.HitNRun && var4) {
return false;
} else {
2018-09-13 12:03:28 -04:00
this.entityPathEntity = this.entityPathNavigate.getPathToXYZ(var2.xCoord, var2.yCoord, var2.zCoord);
return this.entityPathEntity == null ? false : this.entityPathEntity.isDestinationSame(var2);
}
}
}
/**
* Returns whether an in-progress EntityAIBase should continue executing
*/
2025-03-13 17:14:45 -04:00
public boolean continueExecuting() {
2018-09-13 12:03:28 -04:00
return !this.entityPathNavigate.noPath();
}
/**
* Execute a one shot task or start executing a continuous task
*/
2025-03-13 17:14:45 -04:00
public void startExecuting() {
2018-09-13 12:03:28 -04:00
this.entityPathNavigate.setPath(this.entityPathEntity, 1.0D);
}
/**
* Resets the task
*/
2025-03-13 17:14:45 -04:00
public void resetTask() {
2018-09-13 12:03:28 -04:00
this.closestLivingEntity = null;
this.theEntity.setAttackTarget(null);
}
/**
* Updates the task
*/
2025-03-13 17:14:45 -04:00
public void updateTask() {
if (this.theEntity.getDistanceSqToEntity(this.closestLivingEntity) < 49.0D) {
2018-09-13 12:03:28 -04:00
this.theEntity.getNavigator().setSpeed(1.2D);
2025-03-13 17:14:45 -04:00
} else {
2018-09-13 12:03:28 -04:00
this.theEntity.getNavigator().setSpeed(1.0D);
}
2025-03-13 17:14:45 -04:00
if (this.theEntity.ais.tacticalVariant == EnumNavType.HitNRun) {
float dist = this.theEntity.getDistanceToEntity(this.closestLivingEntity);
if (dist > this.distanceFromEntity || dist < this.theEntity.ais.tacticalRadius) {
this.health = this.theEntity.getHealth();
}
2018-09-13 12:03:28 -04:00
}
}
2025-02-10 21:32:55 -05:00
}