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

147 lines
5.7 KiB
Java
Raw Normal View History

2018-09-13 12:03:28 -04:00
package noppes.npcs.ai;
2025-01-29 13:20:04 -05:00
import kamkeel.npcs.addon.DBCAddon;
2018-09-13 12:03:28 -04:00
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
2018-09-13 12:03:28 -04:00
import net.minecraft.entity.ai.EntityAIBase;
2024-04-11 22:18:23 -04:00
import net.minecraft.entity.player.EntityPlayer;
2018-09-13 12:03:28 -04:00
import net.minecraft.pathfinding.PathEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
2023-01-07 00:21:08 -05:00
import noppes.npcs.config.ConfigMain;
2018-09-13 12:03:28 -04:00
import noppes.npcs.constants.AiMutex;
import noppes.npcs.entity.EntityNPCInterface;
2025-03-13 17:14:45 -04:00
public class EntityAIAttackTarget extends EntityAIBase {
private World world;
private EntityNPCInterface npc;
private EntityLivingBase entityTarget;
2018-09-13 12:03:28 -04:00
/**
* An amount of decrementing ticks that allows the entity to attack once the tick reaches 0.
*/
private int attackTick;
2018-09-13 12:03:28 -04:00
2025-03-13 17:14:45 -04:00
/**
* The PathEntity of our entity.
*/
private PathEntity entityPathEntity;
2018-09-13 12:03:28 -04:00
private int field_75445_i;
2025-03-13 17:14:45 -04:00
private boolean navOverride = false;
2024-04-11 22:18:23 -04:00
2025-03-13 17:14:45 -04:00
public EntityAIAttackTarget(EntityNPCInterface par1EntityLiving) {
2018-09-13 12:03:28 -04:00
this.attackTick = 0;
this.npc = par1EntityLiving;
this.world = par1EntityLiving.worldObj;
2025-03-13 17:14:45 -04:00
this.setMutexBits(this.navOverride ? AiMutex.PATHING : AiMutex.LOOK + AiMutex.PASSIVE);
2018-09-13 12:03:28 -04:00
}
/**
* Returns whether the EntityAIBase should begin execution.
*/
2025-03-13 17:14:45 -04:00
public boolean shouldExecute() {
EntityLivingBase entitylivingbase = this.npc.getAttackTarget();
2018-09-13 12:03:28 -04:00
2025-03-13 17:14:45 -04:00
if (entitylivingbase == null) {
2018-09-13 12:03:28 -04:00
return false;
2025-03-13 17:14:45 -04:00
} else if (!entitylivingbase.isEntityAlive()) {
return false;
} else if (this.npc.inventory.getProjectile() != null && this.npc.ais.useRangeMelee == 0) {
2018-09-13 12:03:28 -04:00
return false;
}
2024-04-11 22:18:23 -04:00
double var2 = this.npc.getDistanceSq(entitylivingbase.posX, entitylivingbase.boundingBox.minY, entitylivingbase.posZ);
2025-02-10 21:32:55 -05:00
double var3 = this.npc.ais.distanceToMelee * this.npc.ais.distanceToMelee;
2024-04-11 22:18:23 -04:00
2025-03-13 17:14:45 -04:00
if (this.npc.ais.useRangeMelee == 1 && var2 > var3) {
return false;
} else {
this.entityTarget = entitylivingbase;
this.npc.getEntityAttribute(SharedMonsterAttributes.followRange).setBaseValue(((EntityNPCInterface) this.npc).stats.aggroRange);
this.entityPathEntity = this.npc.getNavigator().getPathToEntityLiving(entitylivingbase);
this.npc.getEntityAttribute(SharedMonsterAttributes.followRange).setBaseValue(ConfigMain.NpcNavRange);
2018-09-13 12:03:28 -04:00
return this.entityPathEntity != null;
}
}
/**
* Returns whether an in-progress EntityAIBase should continue executing
*/
2025-03-13 17:14:45 -04:00
public boolean continueExecuting() {
this.entityTarget = this.npc.getAttackTarget();
2024-04-11 22:18:23 -04:00
2025-03-13 17:14:45 -04:00
if (entityTarget == null || !entityTarget.isEntityAlive())
return false;
2024-04-11 22:18:23 -04:00
2025-03-13 17:14:45 -04:00
if (entityTarget instanceof EntityPlayer && DBCAddon.instance.isKO(npc, (EntityPlayer) this.entityTarget))
2024-04-11 22:18:23 -04:00
return false;
2025-03-13 17:14:45 -04:00
if (npc.getDistanceToEntity(entityTarget) > npc.stats.aggroRange)
return false;
if (this.npc.ais.useRangeMelee == 1 && npc.getDistanceSqToEntity(entityTarget) > (this.npc.ais.distanceToMelee * this.npc.ais.distanceToMelee))
return false;
2024-04-11 22:18:23 -04:00
2025-03-13 17:14:45 -04:00
return this.npc.isWithinHomeDistance(MathHelper.floor_double(entityTarget.posX), MathHelper.floor_double(entityTarget.posY), MathHelper.floor_double(entityTarget.posZ));
2018-09-13 12:03:28 -04:00
}
/**
* Execute a one shot task or start executing a continuous task
*/
2025-03-13 17:14:45 -04:00
public void startExecuting() {
if (!navOverride)
this.npc.getNavigator().setPath(this.entityPathEntity, 1.3D);
2018-09-13 12:03:28 -04:00
this.field_75445_i = 0;
2025-03-13 17:14:45 -04:00
if (this.npc.getRangedTask() != null && this.npc.ais.useRangeMelee == 2) {
this.npc.getRangedTask().navOverride(true);
2018-09-13 12:03:28 -04:00
}
}
/**
* Resets the task
*/
2025-03-13 17:14:45 -04:00
public void resetTask() {
this.entityPathEntity = null;
this.entityTarget = null;
this.npc.setAttackTarget(null);
this.npc.getNavigator().clearPathEntity();
2025-03-13 17:14:45 -04:00
if (this.npc.getRangedTask() != null && this.npc.ais.useRangeMelee == 2) {
this.npc.getRangedTask().navOverride(false);
2018-09-13 12:03:28 -04:00
}
}
/**
* Updates the task
*/
2025-03-13 17:14:45 -04:00
public void updateTask() {
this.npc.getLookHelper().setLookPositionWithEntity(this.entityTarget, 30.0F, 30.0F);
2025-03-13 17:14:45 -04:00
if (!navOverride && --this.field_75445_i <= 0) {
this.field_75445_i = 4 + this.npc.getRNG().nextInt(7);
this.npc.getNavigator().tryMoveToEntityLiving(this.entityTarget, 1.3f);
2018-09-13 12:03:28 -04:00
}
this.attackTick = Math.max(this.attackTick - 1, 0);
double distance = this.npc.getDistanceSq(this.entityTarget.posX, this.entityTarget.boundingBox.minY, this.entityTarget.posZ);
double range = npc.stats.attackRange * npc.stats.attackRange + entityTarget.width;
double minRange = this.npc.width * 2.0F * this.npc.width * 2.0F + entityTarget.width;
2025-03-13 17:14:45 -04:00
if (minRange > range)
range = minRange;
if (distance <= range && (npc.canSee(this.entityTarget) || distance < minRange)) {
if (this.attackTick <= 0) {
this.attackTick = this.npc.stats.attackSpeed;
2025-03-13 17:14:45 -04:00
if (this.npc.stats.swingWarmUp == 0)
this.npc.attackEntityAsMob(this.entityTarget);
this.npc.swingItem();
} else if (this.npc.stats.swingWarmUp > 0 && this.attackTick == this.npc.stats.attackSpeed - this.npc.stats.swingWarmUp) {
// Perform the actual attack that deals damage
this.npc.attackEntityAsMob(this.entityTarget);
2018-09-13 12:03:28 -04:00
}
}
}
2025-03-13 17:14:45 -04:00
public void navOverride(boolean nav) {
this.navOverride = nav;
2018-09-13 12:03:28 -04:00
this.setMutexBits(this.navOverride ? AiMutex.PATHING : AiMutex.LOOK + AiMutex.PASSIVE);
}
}