| 1 | package net.minecraft.entity.ai;
|
| 2 |
|
| 3 | import net.minecraft.entity.Entity;
|
| 4 | import net.minecraft.entity.EntityCreature;
|
| 5 | import net.minecraft.entity.EntityLivingBase;
|
| 6 | import net.minecraft.pathfinding.PathEntity;
|
| 7 | import net.minecraft.util.BlockPos;
|
| 8 | import net.minecraft.world.World;
|
| 9 |
|
| 10 | public class EntityAIAttackOnCollide extends EntityAIBase
|
| 11 | {
|
| 12 | World worldObj;
|
| 13 | protected EntityCreature attacker;
|
| 14 |
|
| 15 | /**
|
| 16 | * An amount of decrementing ticks that allows the entity to attack once the tick reaches 0.
|
| 17 | */
|
| 18 | int attackTick;
|
| 19 |
|
| 20 | /** The speed with which the mob will approach the target */
|
| 21 | double speedTowardsTarget;
|
| 22 |
|
| 23 | /**
|
| 24 | * When true, the mob will continue chasing its target, even if it can't find a path to them right now.
|
| 25 | */
|
| 26 | boolean longMemory;
|
| 27 |
|
| 28 | /** The PathEntity of our entity. */
|
| 29 | PathEntity entityPathEntity;
|
| 30 | Class <? extends Entity > classTarget;
|
| 31 | private int delayCounter;
|
| 32 | private double targetX;
|
| 33 | private double targetY;
|
| 34 | private double targetZ;
|
| 35 |
|
| 36 | public EntityAIAttackOnCollide(EntityCreature creature, Class <? extends Entity > targetClass, double speedIn, boolean useLongMemory)
|
| 37 | {
|
| 38 | this(creature, speedIn, useLongMemory);
|
| 39 | this.classTarget = targetClass;
|
| 40 | }
|
| 41 |
|
| 42 | public EntityAIAttackOnCollide(EntityCreature creature, double speedIn, boolean useLongMemory)
|
| 43 | {
|
| 44 | this.attacker = creature;
|
| 45 | this.worldObj = creature.worldObj;
|
| 46 | this.speedTowardsTarget = speedIn;
|
| 47 | this.longMemory = useLongMemory;
|
| 48 | this.setMutexBits(3);
|
| 49 | }
|
| 50 |
|
| 51 | /**
|
| 52 | * Returns whether the EntityAIBase should begin execution.
|
| 53 | */
|
| 54 | public boolean shouldExecute()
|
| 55 | {
|
| 56 | EntityLivingBase entitylivingbase = this.attacker.getAttackTarget();
|
| 57 |
|
| 58 | if (entitylivingbase == null)
|
| 59 | {
|
| 60 | return false;
|
| 61 | }
|
| 62 | else if (!entitylivingbase.isEntityAlive())
|
| 63 | {
|
| 64 | return false;
|
| 65 | }
|
| 66 | else if (this.classTarget != null && !this.classTarget.isAssignableFrom(entitylivingbase.getClass()))
|
| 67 | {
|
| 68 | return false;
|
| 69 | }
|
| 70 | else
|
| 71 | {
|
| 72 | this.entityPathEntity = this.attacker.getNavigator().getPathToEntityLiving(entitylivingbase);
|
| 73 | return this.entityPathEntity != null;
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | /**
|
| 78 | * Returns whether an in-progress EntityAIBase should continue executing
|
| 79 | */
|
| 80 | public boolean continueExecuting()
|
| 81 | {
|
| 82 | EntityLivingBase entitylivingbase = this.attacker.getAttackTarget();
|
| 83 | return entitylivingbase == null ? false : (!entitylivingbase.isEntityAlive() ? false : (!this.longMemory ? !this.attacker.getNavigator().noPath() : this.attacker.isWithinHomeDistanceFromPosition(new BlockPos(entitylivingbase))));
|
| 84 | }
|
| 85 |
|
| 86 | /**
|
| 87 | * Execute a one shot task or start executing a continuous task
|
| 88 | */
|
| 89 | public void startExecuting()
|
| 90 | {
|
| 91 | this.attacker.getNavigator().setPath(this.entityPathEntity, this.speedTowardsTarget);
|
| 92 | this.delayCounter = 0;
|
| 93 | }
|
| 94 |
|
| 95 | /**
|
| 96 | * Resets the task
|
| 97 | */
|
| 98 | public void resetTask()
|
| 99 | {
|
| 100 | this.attacker.getNavigator().clearPathEntity();
|
| 101 | }
|
| 102 |
|
| 103 | /**
|
| 104 | * Updates the task
|
| 105 | */
|
| 106 | public void updateTask()
|
| 107 | {
|
| 108 | EntityLivingBase entitylivingbase = this.attacker.getAttackTarget();
|
| 109 | this.attacker.getLookHelper().setLookPositionWithEntity(entitylivingbase, 30.0F, 30.0F);
|
| 110 | double d0 = this.attacker.getDistanceSq(entitylivingbase.posX, entitylivingbase.getEntityBoundingBox().minY, entitylivingbase.posZ);
|
| 111 | double d1 = this.func_179512_a(entitylivingbase);
|
| 112 | --this.delayCounter;
|
| 113 |
|
| 114 | if ((this.longMemory || this.attacker.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.attacker.getRNG().nextFloat() < 0.05F))
|
| 115 | {
|
| 116 | this.targetX = entitylivingbase.posX;
|
| 117 | this.targetY = entitylivingbase.getEntityBoundingBox().minY;
|
| 118 | this.targetZ = entitylivingbase.posZ;
|
| 119 | this.delayCounter = 4 + this.attacker.getRNG().nextInt(7);
|
| 120 |
|
| 121 | if (d0 > 1024.0D)
|
| 122 | {
|
| 123 | this.delayCounter += 10;
|
| 124 | }
|
| 125 | else if (d0 > 256.0D)
|
| 126 | {
|
| 127 | this.delayCounter += 5;
|
| 128 | }
|
| 129 |
|
| 130 | if (!this.attacker.getNavigator().tryMoveToEntityLiving(entitylivingbase, this.speedTowardsTarget))
|
| 131 | {
|
| 132 | this.delayCounter += 15;
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | this.attackTick = Math.max(this.attackTick - 1, 0);
|
| 137 |
|
| 138 | if (d0 <= d1 && this.attackTick <= 0)
|
| 139 | {
|
| 140 | this.attackTick = 20;
|
| 141 |
|
| 142 | if (this.attacker.getHeldItem() != null)
|
| 143 | {
|
| 144 | this.attacker.swingItem();
|
| 145 | }
|
| 146 |
|
| 147 | this.attacker.attackEntityAsMob(entitylivingbase);
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | protected double func_179512_a(EntityLivingBase attackTarget)
|
| 152 | {
|
| 153 | return (double)(this.attacker.width * 2.0F * this.attacker.width * 2.0F + attackTarget.width);
|
| 154 | }
|
| 155 | }
|
| 156 |
|