| 1 | package net.minecraft.world.entity.ai.control;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.tags.BlockTags;
|
| 6 | import net.minecraft.util.Mth;
|
| 7 | import net.minecraft.world.entity.Mob;
|
| 8 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 9 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 10 | import net.minecraft.world.level.block.state.BlockState;
|
| 11 | import net.minecraft.world.level.pathfinder.NodeEvaluator;
|
| 12 | import net.minecraft.world.level.pathfinder.PathType;
|
| 13 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 14 |
|
| 15 | public class MoveControl<T extends Mob> implements Control {
|
| 16 | public static final float MIN_SPEED = 5.0E-4F;
|
| 17 | public static final float MIN_SPEED_SQR = 2.5000003E-7F;
|
| 18 | protected static final int MAX_TURN = 90;
|
| 19 | protected final T mob;
|
| 20 | protected double wantedX;
|
| 21 | protected double wantedY;
|
| 22 | protected double wantedZ;
|
| 23 | protected double speedModifier;
|
| 24 | protected float strafeForwards;
|
| 25 | protected float strafeRight;
|
| 26 | protected MoveControl.Operation operation = MoveControl.Operation.WAIT;
|
| 27 |
|
| 28 | public MoveControl(final T mob) {
|
| 29 | this.mob = mob;
|
| 30 | }
|
| 31 |
|
| 32 | public boolean hasWanted() {
|
| 33 | return this.operation == MoveControl.Operation.MOVE_TO;
|
| 34 | }
|
| 35 |
|
| 36 | public double getSpeedModifier() {
|
| 37 | return this.speedModifier;
|
| 38 | }
|
| 39 |
|
| 40 | public void setWantedPosition(final double x, final double y, final double z, final double speedModifier) {
|
| 41 | this.wantedX = x;
|
| 42 | this.wantedY = y;
|
| 43 | this.wantedZ = z;
|
| 44 | this.speedModifier = speedModifier;
|
| 45 | if (this.operation != MoveControl.Operation.JUMPING) {
|
| 46 | this.operation = MoveControl.Operation.MOVE_TO;
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | public void strafe(final float forwards, final float right) {
|
| 51 | this.operation = MoveControl.Operation.STRAFE;
|
| 52 | this.strafeForwards = forwards;
|
| 53 | this.strafeRight = right;
|
| 54 | this.speedModifier = 0.25;
|
| 55 | }
|
| 56 |
|
| 57 | public void tick() {
|
| 58 | if (this.operation == MoveControl.Operation.STRAFE) {
|
| 59 | float speed = (float)this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED);
|
| 60 | float speedModified = (float)this.speedModifier * speed;
|
| 61 | float xa = this.strafeForwards;
|
| 62 | float za = this.strafeRight;
|
| 63 | float dist = Mth.sqrt(xa * xa + za * za);
|
| 64 | if (dist < 1.0F) {
|
| 65 | dist = 1.0F;
|
| 66 | }
|
| 67 |
|
| 68 | dist = speedModified / dist;
|
| 69 | xa *= dist;
|
| 70 | za *= dist;
|
| 71 | float sin = Mth.sin(this.mob.getYRot() * (float) (Math.PI / 180.0));
|
| 72 | float cos = Mth.cos(this.mob.getYRot() * (float) (Math.PI / 180.0));
|
| 73 | float dx = xa * cos - za * sin;
|
| 74 | float dz = za * cos + xa * sin;
|
| 75 | if (!this.isWalkable(dx, dz)) {
|
| 76 | this.strafeForwards = 1.0F;
|
| 77 | this.strafeRight = 0.0F;
|
| 78 | }
|
| 79 |
|
| 80 | this.mob.setSpeed(speedModified);
|
| 81 | this.mob.setZza(this.strafeForwards);
|
| 82 | this.mob.setXxa(this.strafeRight);
|
| 83 | this.operation = MoveControl.Operation.WAIT;
|
| 84 | } else if (this.operation == MoveControl.Operation.MOVE_TO) {
|
| 85 | this.operation = MoveControl.Operation.WAIT;
|
| 86 | double xd = this.wantedX - this.mob.getX();
|
| 87 | double zd = this.wantedZ - this.mob.getZ();
|
| 88 | double yd = this.wantedY - this.mob.getY();
|
| 89 | double dd = xd * xd + yd * yd + zd * zd;
|
| 90 | if (dd < 2.5000003E-7F) {
|
| 91 | this.mob.setZza(0.0F);
|
| 92 | return;
|
| 93 | }
|
| 94 |
|
| 95 | float yRotD = (float)(Mth.atan2(zd, xd) * 180.0F / (float)Math.PI) - 90.0F;
|
| 96 | this.mob.setYRot(this.rotlerp(this.mob.getYRot(), yRotD, 90.0F));
|
| 97 | this.mob.setSpeed((float)(this.speedModifier * this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED)));
|
| 98 | BlockPos pos = this.mob.blockPosition();
|
| 99 | BlockState blockState = this.mob.level().getBlockState(pos);
|
| 100 | VoxelShape shape = blockState.getCollisionShape(this.mob.level(), pos);
|
| 101 | if (yd > this.mob.maxUpStep() && xd * xd + zd * zd < Math.max(1.0F, this.mob.getBbWidth())
|
| 102 | || !shape.isEmpty() && this.mob.getY() < shape.max(Direction.Axis.Y) + pos.getY() && !blockState.is(BlockTags.DOORS) && !blockState.is(BlockTags.FENCES)) {
|
| 103 | this.mob.getJumpControl().jump();
|
| 104 | this.operation = MoveControl.Operation.JUMPING;
|
| 105 | }
|
| 106 | } else if (this.operation == MoveControl.Operation.JUMPING) {
|
| 107 | this.mob.setSpeed((float)(this.speedModifier * this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED)));
|
| 108 | if (this.mob.onGround() || this.mob.isInLiquid() && this.mob.isAffectedByFluids()) {
|
| 109 | this.operation = MoveControl.Operation.WAIT;
|
| 110 | }
|
| 111 | } else {
|
| 112 | this.mob.setZza(0.0F);
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | private boolean isWalkable(final float dx, final float dz) {
|
| 117 | PathNavigation pathNavigation = this.mob.getNavigation();
|
| 118 | if (pathNavigation != null) {
|
| 119 | NodeEvaluator nodeEvaluator = pathNavigation.getNodeEvaluator();
|
| 120 | if (nodeEvaluator != null
|
| 121 | && nodeEvaluator.getPathType(this.mob, BlockPos.containing(this.mob.getX() + dx, this.mob.getBlockY(), this.mob.getZ() + dz)) != PathType.WALKABLE) {
|
| 122 | return false;
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | return true;
|
| 127 | }
|
| 128 |
|
| 129 | protected float rotlerp(final float a, final float b, final float max) {
|
| 130 | float diff = Mth.wrapDegrees(b - a);
|
| 131 | if (diff > max) {
|
| 132 | diff = max;
|
| 133 | }
|
| 134 |
|
| 135 | if (diff < -max) {
|
| 136 | diff = -max;
|
| 137 | }
|
| 138 |
|
| 139 | float result = a + diff;
|
| 140 | if (result < 0.0F) {
|
| 141 | result += 360.0F;
|
| 142 | } else if (result > 360.0F) {
|
| 143 | result -= 360.0F;
|
| 144 | }
|
| 145 |
|
| 146 | return result;
|
| 147 | }
|
| 148 |
|
| 149 | public double getWantedX() {
|
| 150 | return this.wantedX;
|
| 151 | }
|
| 152 |
|
| 153 | public double getWantedY() {
|
| 154 | return this.wantedY;
|
| 155 | }
|
| 156 |
|
| 157 | public double getWantedZ() {
|
| 158 | return this.wantedZ;
|
| 159 | }
|
| 160 |
|
| 161 | public void setWait() {
|
| 162 | this.operation = MoveControl.Operation.WAIT;
|
| 163 | }
|
| 164 |
|
| 165 | protected enum Operation {
|
| 166 | WAIT,
|
| 167 | MOVE_TO,
|
| 168 | STRAFE,
|
| 169 | JUMPING;
|
| 170 | }
|
| 171 | }
|
| 172 |
|