| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import java.util.Optional;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.world.entity.Mob;
|
| 8 | import net.minecraft.world.entity.PathfinderMob;
|
| 9 | import net.minecraft.world.entity.ai.Brain;
|
| 10 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 11 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 12 | import net.minecraft.world.entity.ai.memory.WalkTarget;
|
| 13 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 14 | import net.minecraft.world.entity.ai.util.DefaultRandomPos;
|
| 15 | import net.minecraft.world.level.pathfinder.Path;
|
| 16 | import net.minecraft.world.phys.Vec3;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | public class MoveToTargetSink extends Behavior<Mob> {
|
| 20 | private static final int MAX_COOLDOWN_BEFORE_RETRYING = 40;
|
| 21 | private int remainingCooldown;
|
| 22 | private @Nullable Path path;
|
| 23 | private @Nullable BlockPos lastTargetPos;
|
| 24 | private float speedModifier;
|
| 25 |
|
| 26 | public MoveToTargetSink() {
|
| 27 | this(150, 250);
|
| 28 | }
|
| 29 |
|
| 30 | public MoveToTargetSink(final int minTimeout, final int maxTimeout) {
|
| 31 | super(
|
| 32 | ImmutableMap.of(
|
| 33 | MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE,
|
| 34 | MemoryStatus.REGISTERED,
|
| 35 | MemoryModuleType.PATH,
|
| 36 | MemoryStatus.VALUE_ABSENT,
|
| 37 | MemoryModuleType.WALK_TARGET,
|
| 38 | MemoryStatus.VALUE_PRESENT
|
| 39 | ),
|
| 40 | minTimeout,
|
| 41 | maxTimeout
|
| 42 | );
|
| 43 | }
|
| 44 |
|
| 45 | protected boolean checkExtraStartConditions(final ServerLevel level, final Mob body) {
|
| 46 | if (this.remainingCooldown > 0) {
|
| 47 | this.remainingCooldown--;
|
| 48 | return false;
|
| 49 | }
|
| 50 |
|
| 51 | Brain<?> brain = body.getBrain();
|
| 52 | WalkTarget walkTarget = brain.getMemory(MemoryModuleType.WALK_TARGET).get();
|
| 53 | boolean reachedTarget = this.reachedTarget(body, walkTarget);
|
| 54 | if (!reachedTarget && this.tryComputePath(body, walkTarget, level.getGameTime())) {
|
| 55 | this.lastTargetPos = walkTarget.getTarget().currentBlockPosition();
|
| 56 | return true;
|
| 57 | }
|
| 58 |
|
| 59 | brain.eraseMemory(MemoryModuleType.WALK_TARGET);
|
| 60 | if (reachedTarget) {
|
| 61 | brain.eraseMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
|
| 62 | }
|
| 63 |
|
| 64 | return false;
|
| 65 | }
|
| 66 |
|
| 67 | protected boolean canStillUse(final ServerLevel level, final Mob body, final long timestamp) {
|
| 68 | if (this.path != null && this.lastTargetPos != null) {
|
| 69 | Optional<WalkTarget> walkTarget = body.getBrain().getMemory(MemoryModuleType.WALK_TARGET);
|
| 70 | boolean isSpectator = walkTarget.map(MoveToTargetSink::isWalkTargetSpectator).orElse(false);
|
| 71 | PathNavigation navigation = body.getNavigation();
|
| 72 | return !navigation.isDone() && walkTarget.isPresent() && !this.reachedTarget(body, walkTarget.get()) && !isSpectator;
|
| 73 | } else {
|
| 74 | return false;
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | protected void stop(final ServerLevel level, final Mob body, final long timestamp) {
|
| 79 | if (body.getBrain().hasMemoryValue(MemoryModuleType.WALK_TARGET)
|
| 80 | && !this.reachedTarget(body, body.getBrain().getMemory(MemoryModuleType.WALK_TARGET).get())
|
| 81 | && body.getNavigation().isStuck()) {
|
| 82 | this.remainingCooldown = level.getRandom().nextInt(40);
|
| 83 | }
|
| 84 |
|
| 85 | body.getNavigation().stop();
|
| 86 | body.getBrain().eraseMemory(MemoryModuleType.WALK_TARGET);
|
| 87 | body.getBrain().eraseMemory(MemoryModuleType.PATH);
|
| 88 | this.path = null;
|
| 89 | }
|
| 90 |
|
| 91 | protected void start(final ServerLevel level, final Mob body, final long timestamp) {
|
| 92 | body.getBrain().setMemory(MemoryModuleType.PATH, this.path);
|
| 93 | body.getNavigation().moveTo(this.path, this.speedModifier);
|
| 94 | }
|
| 95 |
|
| 96 | protected void tick(final ServerLevel level, final Mob body, final long timestamp) {
|
| 97 | Path newPath = body.getNavigation().getPath();
|
| 98 | Brain<?> brain = body.getBrain();
|
| 99 | if (this.path != newPath) {
|
| 100 | this.path = newPath;
|
| 101 | brain.setMemory(MemoryModuleType.PATH, newPath);
|
| 102 | }
|
| 103 |
|
| 104 | if (newPath != null && this.lastTargetPos != null) {
|
| 105 | WalkTarget walkTarget = brain.getMemory(MemoryModuleType.WALK_TARGET).get();
|
| 106 | if (walkTarget.getTarget().currentBlockPosition().distSqr(this.lastTargetPos) > 4.0 && this.tryComputePath(body, walkTarget, level.getGameTime())) {
|
| 107 | this.lastTargetPos = walkTarget.getTarget().currentBlockPosition();
|
| 108 | this.start(level, body, timestamp);
|
| 109 | }
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | private boolean tryComputePath(final Mob body, final WalkTarget walkTarget, final long timestamp) {
|
| 114 | BlockPos targetPos = walkTarget.getTarget().currentBlockPosition();
|
| 115 | this.path = body.getNavigation().createPath(targetPos, 0);
|
| 116 | this.speedModifier = walkTarget.getSpeedModifier();
|
| 117 | Brain<?> brain = body.getBrain();
|
| 118 | if (this.reachedTarget(body, walkTarget)) {
|
| 119 | brain.eraseMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
|
| 120 | } else {
|
| 121 | boolean canReach = this.path != null && this.path.canReach();
|
| 122 | if (canReach) {
|
| 123 | brain.eraseMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
|
| 124 | } else if (!brain.hasMemoryValue(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE)) {
|
| 125 | brain.setMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE, timestamp);
|
| 126 | }
|
| 127 |
|
| 128 | if (this.path != null) {
|
| 129 | return true;
|
| 130 | }
|
| 131 |
|
| 132 | Vec3 partialStep = DefaultRandomPos.getPosTowards((PathfinderMob)body, 10, 7, Vec3.atBottomCenterOf(targetPos), (float) (Math.PI / 2));
|
| 133 | if (partialStep != null) {
|
| 134 | this.path = body.getNavigation().createPath(partialStep.x, partialStep.y, partialStep.z, 0);
|
| 135 | return this.path != null;
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | return false;
|
| 140 | }
|
| 141 |
|
| 142 | private boolean reachedTarget(final Mob body, final WalkTarget walkTarget) {
|
| 143 | return walkTarget.getTarget().currentBlockPosition().distManhattan(body.blockPosition()) <= walkTarget.getCloseEnoughDist();
|
| 144 | }
|
| 145 |
|
| 146 | private static boolean isWalkTargetSpectator(final WalkTarget walkTarget) {
|
| 147 | return walkTarget.getTarget() instanceof EntityTracker entityTracker ? entityTracker.getEntity().isSpectator() : false;
|
| 148 | }
|
| 149 | }
|
| 150 |
|