| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.world.entity.PathfinderMob;
|
| 6 | import net.minecraft.world.level.LevelReader;
|
| 7 |
|
| 8 | public abstract class MoveToBlockGoal extends Goal {
|
| 9 | private static final int GIVE_UP_TICKS = 1200;
|
| 10 | private static final int STAY_TICKS = 1200;
|
| 11 | private static final int INTERVAL_TICKS = 200;
|
| 12 | protected final PathfinderMob mob;
|
| 13 | public final double speedModifier;
|
| 14 | protected int nextStartTick;
|
| 15 | protected int tryTicks;
|
| 16 | private int maxStayTicks;
|
| 17 | protected BlockPos blockPos = BlockPos.ZERO;
|
| 18 | private boolean reachedTarget;
|
| 19 | private final int searchRange;
|
| 20 | private final int verticalSearchRange;
|
| 21 | protected int verticalSearchStart;
|
| 22 |
|
| 23 | public MoveToBlockGoal(final PathfinderMob mob, final double speedModifier, final int searchRange) {
|
| 24 | this(mob, speedModifier, searchRange, 1);
|
| 25 | }
|
| 26 |
|
| 27 | public MoveToBlockGoal(final PathfinderMob mob, final double speedModifier, final int searchRange, final int verticalSearchRange) {
|
| 28 | this.mob = mob;
|
| 29 | this.speedModifier = speedModifier;
|
| 30 | this.searchRange = searchRange;
|
| 31 | this.verticalSearchStart = 0;
|
| 32 | this.verticalSearchRange = verticalSearchRange;
|
| 33 | this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.JUMP));
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public boolean canUse() {
|
| 38 | if (this.nextStartTick > 0) {
|
| 39 | this.nextStartTick--;
|
| 40 | return false;
|
| 41 | } else {
|
| 42 | this.nextStartTick = this.nextStartTick(this.mob);
|
| 43 | return this.findNearestBlock();
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | protected int nextStartTick(final PathfinderMob mob) {
|
| 48 | return reducedTickDelay(200 + mob.getRandom().nextInt(200));
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public boolean canContinueToUse() {
|
| 53 | return this.tryTicks >= -this.maxStayTicks && this.tryTicks <= 1200 && this.isValidTarget(this.mob.level(), this.blockPos);
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public void start() {
|
| 58 | this.moveMobToBlock();
|
| 59 | this.tryTicks = 0;
|
| 60 | this.maxStayTicks = this.mob.getRandom().nextInt(this.mob.getRandom().nextInt(1200) + 1200) + 1200;
|
| 61 | }
|
| 62 |
|
| 63 | protected void moveMobToBlock() {
|
| 64 | this.mob.getNavigation().moveTo(this.blockPos.getX() + 0.5, this.blockPos.getY() + 1, this.blockPos.getZ() + 0.5, this.speedModifier);
|
| 65 | }
|
| 66 |
|
| 67 | public double acceptedDistance() {
|
| 68 | return 1.0;
|
| 69 | }
|
| 70 |
|
| 71 | protected BlockPos getMoveToTarget() {
|
| 72 | return this.blockPos.above();
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public boolean requiresUpdateEveryTick() {
|
| 77 | return true;
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public void tick() {
|
| 82 | BlockPos moveToTarget = this.getMoveToTarget();
|
| 83 | if (!moveToTarget.closerToCenterThan(this.mob.position(), this.acceptedDistance())) {
|
| 84 | this.reachedTarget = false;
|
| 85 | this.tryTicks++;
|
| 86 | if (this.shouldRecalculatePath()) {
|
| 87 | this.mob.getNavigation().moveTo(moveToTarget.getX() + 0.5, moveToTarget.getY(), moveToTarget.getZ() + 0.5, this.speedModifier);
|
| 88 | }
|
| 89 | } else {
|
| 90 | this.reachedTarget = true;
|
| 91 | this.tryTicks--;
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | public boolean shouldRecalculatePath() {
|
| 96 | return this.tryTicks % 40 == 0;
|
| 97 | }
|
| 98 |
|
| 99 | protected boolean isReachedTarget() {
|
| 100 | return this.reachedTarget;
|
| 101 | }
|
| 102 |
|
| 103 | protected boolean findNearestBlock() {
|
| 104 | int horizontalSearch = this.searchRange;
|
| 105 | int verticalSearch = this.verticalSearchRange;
|
| 106 | BlockPos mobPos = this.mob.blockPosition();
|
| 107 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 108 |
|
| 109 | for (int y = this.verticalSearchStart; y <= verticalSearch; y = y > 0 ? -y : 1 - y) {
|
| 110 | for (int r = 0; r < horizontalSearch; r++) {
|
| 111 | for (int x = 0; x <= r; x = x > 0 ? -x : 1 - x) {
|
| 112 | for (int z = x < r && x > -r ? r : 0; z <= r; z = z > 0 ? -z : 1 - z) {
|
| 113 | pos.setWithOffset(mobPos, x, y - 1, z);
|
| 114 | if (this.mob.isWithinHome(pos) && this.isValidTarget(this.mob.level(), pos)) {
|
| 115 | this.blockPos = pos;
|
| 116 | return true;
|
| 117 | }
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | return false;
|
| 124 | }
|
| 125 |
|
| 126 | protected abstract boolean isValidTarget(LevelReader level, BlockPos pos);
|
| 127 | }
|
| 128 |
|