| 1 | package net.minecraft.world.entity.ai.targeting;
|
| 2 |
|
| 3 | import net.minecraft.server.level.ServerLevel;
|
| 4 | import net.minecraft.world.Difficulty;
|
| 5 | import net.minecraft.world.entity.LivingEntity;
|
| 6 | import net.minecraft.world.entity.Mob;
|
| 7 | import org.jspecify.annotations.Nullable;
|
| 8 |
|
| 9 | public class TargetingConditions {
|
| 10 | public static final TargetingConditions DEFAULT = forCombat();
|
| 11 | private static final double MIN_VISIBILITY_DISTANCE_FOR_INVISIBLE_TARGET = 2.0;
|
| 12 | private final boolean isCombat;
|
| 13 | private double range = -1.0;
|
| 14 | private boolean checkLineOfSight = true;
|
| 15 | private boolean testInvisible = true;
|
| 16 | private TargetingConditions.@Nullable Selector selector;
|
| 17 |
|
| 18 | private TargetingConditions(final boolean isCombat) {
|
| 19 | this.isCombat = isCombat;
|
| 20 | }
|
| 21 |
|
| 22 | public static TargetingConditions forCombat() {
|
| 23 | return new TargetingConditions(true);
|
| 24 | }
|
| 25 |
|
| 26 | public static TargetingConditions forNonCombat() {
|
| 27 | return new TargetingConditions(false);
|
| 28 | }
|
| 29 |
|
| 30 | public TargetingConditions copy() {
|
| 31 | TargetingConditions clone = this.isCombat ? forCombat() : forNonCombat();
|
| 32 | clone.range = this.range;
|
| 33 | clone.checkLineOfSight = this.checkLineOfSight;
|
| 34 | clone.testInvisible = this.testInvisible;
|
| 35 | clone.selector = this.selector;
|
| 36 | return clone;
|
| 37 | }
|
| 38 |
|
| 39 | public TargetingConditions range(final double range) {
|
| 40 | this.range = range;
|
| 41 | return this;
|
| 42 | }
|
| 43 |
|
| 44 | public TargetingConditions ignoreLineOfSight() {
|
| 45 | this.checkLineOfSight = false;
|
| 46 | return this;
|
| 47 | }
|
| 48 |
|
| 49 | public TargetingConditions ignoreInvisibilityTesting() {
|
| 50 | this.testInvisible = false;
|
| 51 | return this;
|
| 52 | }
|
| 53 |
|
| 54 | public TargetingConditions selector(final TargetingConditions.@Nullable Selector selector) {
|
| 55 | this.selector = selector;
|
| 56 | return this;
|
| 57 | }
|
| 58 |
|
| 59 | public boolean test(final ServerLevel level, final @Nullable LivingEntity targeter, final LivingEntity target) {
|
| 60 | if (targeter == target) {
|
| 61 | return false;
|
| 62 | }
|
| 63 |
|
| 64 | if (!target.canBeSeenByAnyone()) {
|
| 65 | return false;
|
| 66 | }
|
| 67 |
|
| 68 | if (this.selector != null && !this.selector.test(target, level)) {
|
| 69 | return false;
|
| 70 | }
|
| 71 |
|
| 72 | if (targeter == null) {
|
| 73 | if (this.isCombat && (!target.canBeSeenAsEnemy() || level.getDifficulty() == Difficulty.PEACEFUL)) {
|
| 74 | return false;
|
| 75 | }
|
| 76 | } else {
|
| 77 | if (this.isCombat && (!targeter.canAttack(target) || targeter.isAlliedTo(target))) {
|
| 78 | return false;
|
| 79 | }
|
| 80 |
|
| 81 | if (this.range > 0.0) {
|
| 82 | double modifier = this.testInvisible ? target.getVisibilityPercent(level, targeter) : 1.0;
|
| 83 | double visibilityDistance = Math.max(this.range * modifier, 2.0);
|
| 84 | double distanceToSqr = targeter.distanceToSqr(target.getX(), target.getY(), target.getZ());
|
| 85 | if (distanceToSqr > visibilityDistance * visibilityDistance) {
|
| 86 | return false;
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | if (this.checkLineOfSight && targeter instanceof Mob mob && !mob.getSensing().hasLineOfSight(target)) {
|
| 91 | return false;
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | return true;
|
| 96 | }
|
| 97 |
|
| 98 | @FunctionalInterface
|
| 99 | public interface Selector {
|
| 100 | boolean test(LivingEntity target, ServerLevel level);
|
| 101 | }
|
| 102 | }
|
| 103 |
|