BloodMagic/src/main/java/WayofTime/bloodmagic/entity/ai/EntityAIAttackStealthMelee.java

143 lines
5.3 KiB
Java
Raw Normal View History

package WayofTime.bloodmagic.entity.ai;
2017-08-15 21:30:48 -07:00
import WayofTime.bloodmagic.entity.mob.EntityCorruptedChicken;
import net.minecraft.entity.EntityLivingBase;
2016-09-22 19:30:07 -04:00
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.pathfinding.Path;
import net.minecraft.util.EnumHand;
2016-09-22 19:30:07 -04:00
import net.minecraft.world.World;
2017-08-15 21:30:48 -07:00
public class EntityAIAttackStealthMelee extends EntityAIBase {
protected final int attackInterval = 20;
protected EntityCorruptedChicken chicken;
2016-09-22 19:30:07 -04:00
/**
* An amount of decrementing ticks that allows the entity to attack once the
* tick reaches 0.
*/
protected int attackTick;
2017-08-15 21:30:48 -07:00
World worldObj;
/**
* The speed with which the mob will approach the target
*/
2016-09-22 19:30:07 -04:00
double speedTowardsTarget;
/**
* When true, the mob will continue chasing its target, even if it can't
* find a path to them right now.
*/
boolean longMemory;
2017-08-15 21:30:48 -07:00
/**
* The PathEntity of our entity.
*/
2016-09-22 19:30:07 -04:00
Path entityPathEntity;
private int delayCounter;
private double targetX;
private double targetY;
private double targetZ;
private int failedPathFindingPenalty = 0;
private boolean canPenalize = false;
2017-08-15 21:30:48 -07:00
public EntityAIAttackStealthMelee(EntityCorruptedChicken creature, double speedIn, boolean useLongMemory) {
2016-09-22 19:30:07 -04:00
this.chicken = creature;
2016-12-12 19:56:36 -08:00
this.worldObj = creature.getEntityWorld();
2016-09-22 19:30:07 -04:00
this.speedTowardsTarget = speedIn;
this.longMemory = useLongMemory;
this.setMutexBits(3);
}
@Override
2017-08-15 21:30:48 -07:00
public boolean shouldExecute() {
if (chicken.attackStateMachine != 1) {
2016-09-22 19:30:07 -04:00
return false;
}
EntityLivingBase entitylivingbase = this.chicken.getAttackTarget();
2017-08-15 21:30:48 -07:00
if (entitylivingbase == null) {
2016-09-22 19:30:07 -04:00
return false;
2017-08-15 21:30:48 -07:00
} else if (!entitylivingbase.isEntityAlive()) {
2016-09-22 19:30:07 -04:00
return false;
2017-08-15 21:30:48 -07:00
} else {
if (canPenalize) {
if (--this.delayCounter <= 0) {
2016-09-22 19:30:07 -04:00
this.entityPathEntity = this.chicken.getNavigator().getPathToEntityLiving(entitylivingbase);
this.delayCounter = 4 + this.chicken.getRNG().nextInt(7);
return this.entityPathEntity != null;
2017-08-15 21:30:48 -07:00
} else {
2016-09-22 19:30:07 -04:00
return true;
}
}
this.entityPathEntity = this.chicken.getNavigator().getPathToEntityLiving(entitylivingbase);
return this.entityPathEntity != null;
}
}
@Override
2017-08-15 21:30:48 -07:00
public boolean shouldContinueExecuting() {
return chicken.attackStateMachine == 1 && super.shouldContinueExecuting();
}
@Override
2017-08-15 21:30:48 -07:00
public void resetTask() {
if (chicken.attackStateMachine == 1) {
chicken.attackStateMachine = 0;
}
}
@Override
2017-08-15 21:30:48 -07:00
public void updateTask() {
2016-09-22 19:30:07 -04:00
EntityLivingBase entitylivingbase = this.chicken.getAttackTarget();
this.chicken.getLookHelper().setLookPositionWithEntity(entitylivingbase, 30.0F, 30.0F);
double d0 = this.chicken.getDistanceSq(entitylivingbase.posX, entitylivingbase.getEntityBoundingBox().minY, entitylivingbase.posZ);
--this.delayCounter;
2017-08-15 21:30:48 -07:00
if ((this.longMemory || this.chicken.getEntitySenses().canSee(entitylivingbase)) && this.delayCounter <= 0 && (this.targetX == 0.0D && this.targetY == 0.0D && this.targetZ == 0.0D || entitylivingbase.getDistanceSq(this.targetX, this.targetY, this.targetZ) >= 1.0D || this.chicken.getRNG().nextFloat() < 0.05F)) {
2016-09-22 19:30:07 -04:00
this.targetX = entitylivingbase.posX;
this.targetY = entitylivingbase.getEntityBoundingBox().minY;
this.targetZ = entitylivingbase.posZ;
this.delayCounter = 4 + this.chicken.getRNG().nextInt(7);
2017-08-15 21:30:48 -07:00
if (this.canPenalize) {
2016-09-22 19:30:07 -04:00
this.delayCounter += failedPathFindingPenalty;
2017-08-15 21:30:48 -07:00
if (this.chicken.getNavigator().getPath() != null) {
2016-09-22 19:30:07 -04:00
net.minecraft.pathfinding.PathPoint finalPathPoint = this.chicken.getNavigator().getPath().getFinalPathPoint();
if (finalPathPoint != null && entitylivingbase.getDistanceSq(finalPathPoint.x, finalPathPoint.y, finalPathPoint.z) < 1)
2016-09-22 19:30:07 -04:00
failedPathFindingPenalty = 0;
else
failedPathFindingPenalty += 10;
2017-08-15 21:30:48 -07:00
} else {
2016-09-22 19:30:07 -04:00
failedPathFindingPenalty += 10;
}
}
2017-08-15 21:30:48 -07:00
if (d0 > 1024.0D) {
2016-09-22 19:30:07 -04:00
this.delayCounter += 10;
2017-08-15 21:30:48 -07:00
} else if (d0 > 256.0D) {
2016-09-22 19:30:07 -04:00
this.delayCounter += 5;
}
2017-08-15 21:30:48 -07:00
if (!this.chicken.getNavigator().tryMoveToEntityLiving(entitylivingbase, this.speedTowardsTarget)) {
2016-09-22 19:30:07 -04:00
this.delayCounter += 15;
}
}
this.attackTick = Math.max(this.attackTick - 1, 0);
this.attackEntity(entitylivingbase, d0);
}
2017-08-15 21:30:48 -07:00
protected void attackEntity(EntityLivingBase attacked, double distance) {
double d0 = this.getAttackReachSqr(attacked);
2017-08-15 21:30:48 -07:00
if (distance <= d0 && this.attackTick <= 0) {
this.attackTick = 20;
2016-09-22 19:30:07 -04:00
this.chicken.swingArm(EnumHand.MAIN_HAND);
this.chicken.attackEntityAsMob(attacked);
chicken.attackStateMachine = 2;
}
}
2016-09-22 19:30:07 -04:00
2017-08-15 21:30:48 -07:00
protected double getAttackReachSqr(EntityLivingBase attackTarget) {
2016-09-22 19:30:07 -04:00
return (double) (this.chicken.width * 2.0F * this.chicken.width * 2.0F + attackTarget.width);
}
}