| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.util.Mth;
|
| 5 | import net.minecraft.world.entity.EntityDimensions;
|
| 6 | import net.minecraft.world.entity.Mob;
|
| 7 | import net.minecraft.world.entity.Pose;
|
| 8 | import net.minecraft.world.phys.Vec3;
|
| 9 |
|
| 10 | public final class LongJumpUtil {
|
| 11 | public static Optional<Vec3> calculateJumpVectorForAngle(
|
| 12 | final Mob body, final Vec3 targetPos, final float maxJumpVelocity, final int angle, final boolean checkCollision
|
| 13 | ) {
|
| 14 | Vec3 mobPos = body.position();
|
| 15 | Vec3 directionVectorPlane = new Vec3(targetPos.x - mobPos.x, 0.0, targetPos.z - mobPos.z).normalize().scale(0.5);
|
| 16 | Vec3 targetPosition = targetPos.subtract(directionVectorPlane);
|
| 17 | Vec3 directionVector = targetPosition.subtract(mobPos);
|
| 18 | float angrad = angle * (float) Math.PI / 180.0F;
|
| 19 | double xzAng = Math.atan2(directionVector.z, directionVector.x);
|
| 20 | double r2 = directionVector.subtract(0.0, directionVector.y, 0.0).lengthSqr();
|
| 21 | double r = Math.sqrt(r2);
|
| 22 | double y = directionVector.y;
|
| 23 | double g = body.getGravity();
|
| 24 | double sin2ang = Math.sin(2.0F * angrad);
|
| 25 | double cosangsqr = Math.pow(Math.cos(angrad), 2.0);
|
| 26 | double sinangrad = Math.sin(angrad);
|
| 27 | double cosangrad = Math.cos(angrad);
|
| 28 | double sinxzAng = Math.sin(xzAng);
|
| 29 | double cosxzAng = Math.cos(xzAng);
|
| 30 | double v0sqr = r2 * g / (r * sin2ang - 2.0 * y * cosangsqr);
|
| 31 | if (v0sqr < 0.0) {
|
| 32 | return Optional.empty();
|
| 33 | }
|
| 34 |
|
| 35 | double v0 = Math.sqrt(v0sqr);
|
| 36 | if (v0 > maxJumpVelocity) {
|
| 37 | return Optional.empty();
|
| 38 | }
|
| 39 |
|
| 40 | double v0r = v0 * cosangrad;
|
| 41 | double v0y = v0 * sinangrad;
|
| 42 | if (checkCollision) {
|
| 43 | int samples = Mth.ceil(r / v0r) * 2;
|
| 44 | double ri = 0.0;
|
| 45 | Vec3 previousPos = null;
|
| 46 | EntityDimensions mobDimensions = body.getDimensions(Pose.LONG_JUMPING);
|
| 47 |
|
| 48 | for (int i = 0; i < samples - 1; i++) {
|
| 49 | ri += r / samples;
|
| 50 | double yi = sinangrad / cosangrad * ri - Math.pow(ri, 2.0) * g / (2.0 * v0sqr * Math.pow(cosangrad, 2.0));
|
| 51 | double xi = ri * cosxzAng;
|
| 52 | double zi = ri * sinxzAng;
|
| 53 | Vec3 samplePos = new Vec3(mobPos.x + xi, mobPos.y + yi, mobPos.z + zi);
|
| 54 | if (previousPos != null && !isClearTransition(body, mobDimensions, previousPos, samplePos)) {
|
| 55 | return Optional.empty();
|
| 56 | }
|
| 57 |
|
| 58 | previousPos = samplePos;
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | return Optional.of(new Vec3(v0r * cosxzAng, v0y, v0r * sinxzAng).scale(0.95F));
|
| 63 | }
|
| 64 |
|
| 65 | private static boolean isClearTransition(final Mob body, final EntityDimensions entityDimensions, final Vec3 position1, final Vec3 position2) {
|
| 66 | Vec3 direction = position2.subtract(position1);
|
| 67 | double minDimension = Math.min(entityDimensions.width(), entityDimensions.height());
|
| 68 | int checks = Mth.ceil(direction.length() / minDimension);
|
| 69 | Vec3 normalizedDirection = direction.normalize();
|
| 70 | Vec3 nextPointToCheck = position1;
|
| 71 |
|
| 72 | for (int i = 0; i < checks; i++) {
|
| 73 | nextPointToCheck = i == checks - 1 ? position2 : nextPointToCheck.add(normalizedDirection.scale(minDimension * 0.9F));
|
| 74 | if (!body.level().noCollision(body, entityDimensions.makeBoundingBox(nextPointToCheck))) {
|
| 75 | return false;
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | return true;
|
| 80 | }
|
| 81 | }
|
| 82 |
|