| 1 | package net.minecraft.entity.ai;
|
| 2 |
|
| 3 | import net.minecraft.entity.EntityCreature;
|
| 4 | import net.minecraft.entity.EntityLiving;
|
| 5 | import net.minecraft.entity.EntityLivingBase;
|
| 6 | import net.minecraft.entity.IEntityOwnable;
|
| 7 | import net.minecraft.entity.SharedMonsterAttributes;
|
| 8 | import net.minecraft.entity.ai.attributes.IAttributeInstance;
|
| 9 | import net.minecraft.entity.player.EntityPlayer;
|
| 10 | import net.minecraft.pathfinding.PathEntity;
|
| 11 | import net.minecraft.pathfinding.PathPoint;
|
| 12 | import net.minecraft.scoreboard.Team;
|
| 13 | import net.minecraft.util.BlockPos;
|
| 14 | import net.minecraft.util.MathHelper;
|
| 15 | import org.apache.commons.lang3.StringUtils;
|
| 16 |
|
| 17 | public abstract class EntityAITarget extends EntityAIBase
|
| 18 | {
|
| 19 | /** The entity that this task belongs to */
|
| 20 | protected final EntityCreature taskOwner;
|
| 21 |
|
| 22 | /**
|
| 23 | * If true, EntityAI targets must be able to be seen (cannot be blocked by walls) to be suitable targets.
|
| 24 | */
|
| 25 | protected boolean shouldCheckSight;
|
| 26 |
|
| 27 | /**
|
| 28 | * When true, only entities that can be reached with minimal effort will be targetted.
|
| 29 | */
|
| 30 | private boolean nearbyOnly;
|
| 31 |
|
| 32 | /**
|
| 33 | * When nearbyOnly is true: 0 -> No target, but OK to search; 1 -> Nearby target found; 2 -> Target too far.
|
| 34 | */
|
| 35 | private int targetSearchStatus;
|
| 36 |
|
| 37 | /**
|
| 38 | * When nearbyOnly is true, this throttles target searching to avoid excessive pathfinding.
|
| 39 | */
|
| 40 | private int targetSearchDelay;
|
| 41 |
|
| 42 | /**
|
| 43 | * If @shouldCheckSight is true, the number of ticks before the interuption of this AITastk when the entity does't
|
| 44 | * see the target
|
| 45 | */
|
| 46 | private int targetUnseenTicks;
|
| 47 |
|
| 48 | public EntityAITarget(EntityCreature creature, boolean checkSight)
|
| 49 | {
|
| 50 | this(creature, checkSight, false);
|
| 51 | }
|
| 52 |
|
| 53 | public EntityAITarget(EntityCreature creature, boolean checkSight, boolean onlyNearby)
|
| 54 | {
|
| 55 | this.taskOwner = creature;
|
| 56 | this.shouldCheckSight = checkSight;
|
| 57 | this.nearbyOnly = onlyNearby;
|
| 58 | }
|
| 59 |
|
| 60 | /**
|
| 61 | * Returns whether an in-progress EntityAIBase should continue executing
|
| 62 | */
|
| 63 | public boolean continueExecuting()
|
| 64 | {
|
| 65 | EntityLivingBase entitylivingbase = this.taskOwner.getAttackTarget();
|
| 66 |
|
| 67 | if (entitylivingbase == null)
|
| 68 | {
|
| 69 | return false;
|
| 70 | }
|
| 71 | else if (!entitylivingbase.isEntityAlive())
|
| 72 | {
|
| 73 | return false;
|
| 74 | }
|
| 75 | else
|
| 76 | {
|
| 77 | Team team = this.taskOwner.getTeam();
|
| 78 | Team team1 = entitylivingbase.getTeam();
|
| 79 |
|
| 80 | if (team != null && team1 == team)
|
| 81 | {
|
| 82 | return false;
|
| 83 | }
|
| 84 | else
|
| 85 | {
|
| 86 | double d0 = this.getTargetDistance();
|
| 87 |
|
| 88 | if (this.taskOwner.getDistanceSqToEntity(entitylivingbase) > d0 * d0)
|
| 89 | {
|
| 90 | return false;
|
| 91 | }
|
| 92 | else
|
| 93 | {
|
| 94 | if (this.shouldCheckSight)
|
| 95 | {
|
| 96 | if (this.taskOwner.getEntitySenses().canSee(entitylivingbase))
|
| 97 | {
|
| 98 | this.targetUnseenTicks = 0;
|
| 99 | }
|
| 100 | else if (++this.targetUnseenTicks > 60)
|
| 101 | {
|
| 102 | return false;
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | return !(entitylivingbase instanceof EntityPlayer) || !((EntityPlayer)entitylivingbase).capabilities.disableDamage;
|
| 107 | }
|
| 108 | }
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | protected double getTargetDistance()
|
| 113 | {
|
| 114 | IAttributeInstance iattributeinstance = this.taskOwner.getEntityAttribute(SharedMonsterAttributes.followRange);
|
| 115 | return iattributeinstance == null ? 16.0D : iattributeinstance.getAttributeValue();
|
| 116 | }
|
| 117 |
|
| 118 | /**
|
| 119 | * Execute a one shot task or start executing a continuous task
|
| 120 | */
|
| 121 | public void startExecuting()
|
| 122 | {
|
| 123 | this.targetSearchStatus = 0;
|
| 124 | this.targetSearchDelay = 0;
|
| 125 | this.targetUnseenTicks = 0;
|
| 126 | }
|
| 127 |
|
| 128 | /**
|
| 129 | * Resets the task
|
| 130 | */
|
| 131 | public void resetTask()
|
| 132 | {
|
| 133 | this.taskOwner.setAttackTarget((EntityLivingBase)null);
|
| 134 | }
|
| 135 |
|
| 136 | /**
|
| 137 | * A static method used to see if an entity is a suitable target through a number of checks.
|
| 138 | */
|
| 139 | public static boolean isSuitableTarget(EntityLiving attacker, EntityLivingBase target, boolean includeInvincibles, boolean checkSight)
|
| 140 | {
|
| 141 | if (target == null)
|
| 142 | {
|
| 143 | return false;
|
| 144 | }
|
| 145 | else if (target == attacker)
|
| 146 | {
|
| 147 | return false;
|
| 148 | }
|
| 149 | else if (!target.isEntityAlive())
|
| 150 | {
|
| 151 | return false;
|
| 152 | }
|
| 153 | else if (!attacker.canAttackClass(target.getClass()))
|
| 154 | {
|
| 155 | return false;
|
| 156 | }
|
| 157 | else
|
| 158 | {
|
| 159 | Team team = attacker.getTeam();
|
| 160 | Team team1 = target.getTeam();
|
| 161 |
|
| 162 | if (team != null && team1 == team)
|
| 163 | {
|
| 164 | return false;
|
| 165 | }
|
| 166 | else
|
| 167 | {
|
| 168 | if (attacker instanceof IEntityOwnable && StringUtils.isNotEmpty(((IEntityOwnable)attacker).getOwnerId()))
|
| 169 | {
|
| 170 | if (target instanceof IEntityOwnable && ((IEntityOwnable)attacker).getOwnerId().equals(((IEntityOwnable)target).getOwnerId()))
|
| 171 | {
|
| 172 | return false;
|
| 173 | }
|
| 174 |
|
| 175 | if (target == ((IEntityOwnable)attacker).getOwner())
|
| 176 | {
|
| 177 | return false;
|
| 178 | }
|
| 179 | }
|
| 180 | else if (target instanceof EntityPlayer && !includeInvincibles && ((EntityPlayer)target).capabilities.disableDamage)
|
| 181 | {
|
| 182 | return false;
|
| 183 | }
|
| 184 |
|
| 185 | return !checkSight || attacker.getEntitySenses().canSee(target);
|
| 186 | }
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | /**
|
| 191 | * A method used to see if an entity is a suitable target through a number of checks. Args : entity,
|
| 192 | * canTargetInvinciblePlayer
|
| 193 | */
|
| 194 | protected boolean isSuitableTarget(EntityLivingBase target, boolean includeInvincibles)
|
| 195 | {
|
| 196 | if (!isSuitableTarget(this.taskOwner, target, includeInvincibles, this.shouldCheckSight))
|
| 197 | {
|
| 198 | return false;
|
| 199 | }
|
| 200 | else if (!this.taskOwner.isWithinHomeDistanceFromPosition(new BlockPos(target)))
|
| 201 | {
|
| 202 | return false;
|
| 203 | }
|
| 204 | else
|
| 205 | {
|
| 206 | if (this.nearbyOnly)
|
| 207 | {
|
| 208 | if (--this.targetSearchDelay <= 0)
|
| 209 | {
|
| 210 | this.targetSearchStatus = 0;
|
| 211 | }
|
| 212 |
|
| 213 | if (this.targetSearchStatus == 0)
|
| 214 | {
|
| 215 | this.targetSearchStatus = this.canEasilyReach(target) ? 1 : 2;
|
| 216 | }
|
| 217 |
|
| 218 | if (this.targetSearchStatus == 2)
|
| 219 | {
|
| 220 | return false;
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | return true;
|
| 225 | }
|
| 226 | }
|
| 227 |
|
| 228 | /**
|
| 229 | * Checks to see if this entity can find a short path to the given target.
|
| 230 | *
|
| 231 | * @param target the entity to find a path to
|
| 232 | */
|
| 233 | private boolean canEasilyReach(EntityLivingBase target)
|
| 234 | {
|
| 235 | this.targetSearchDelay = 10 + this.taskOwner.getRNG().nextInt(5);
|
| 236 | PathEntity pathentity = this.taskOwner.getNavigator().getPathToEntityLiving(target);
|
| 237 |
|
| 238 | if (pathentity == null)
|
| 239 | {
|
| 240 | return false;
|
| 241 | }
|
| 242 | else
|
| 243 | {
|
| 244 | PathPoint pathpoint = pathentity.getFinalPathPoint();
|
| 245 |
|
| 246 | if (pathpoint == null)
|
| 247 | {
|
| 248 | return false;
|
| 249 | }
|
| 250 | else
|
| 251 | {
|
| 252 | int i = pathpoint.xCoord - MathHelper.floor_double(target.posX);
|
| 253 | int j = pathpoint.zCoord - MathHelper.floor_double(target.posZ);
|
| 254 | return (double)(i * i + j * j) <= 2.25D;
|
| 255 | }
|
| 256 | }
|
| 257 | }
|
| 258 | }
|
| 259 |
|