Загрузка данных


package skwix.mellstroyclient.core;

import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class RotationManager {

    private static final RotationManager INSTANCE = new RotationManager();

    private float serverYaw;
    private float serverPitch;
    private float prevServerYaw;
    private float prevServerPitch;
    private float visualYaw;
    private float visualPitch;
    private boolean active;

    private Vec3d lockedAimPoint;
    private Vec3d lockedAimOffset;
    private Vec3d lockedAimTargetPos;
    private int aimPointTicksRemaining;

    private final Random random = new Random();

    private float lazyOffsetYaw;
    private float lazyOffsetPitch;
    private int lazyCooldown;
    private int ftNewTickCounter;
    private long ftNewSettleStartMs = -1L;
    private long ftNewLastAttackMs = 0L;

    public static RotationManager instance() {
        return INSTANCE;
    }

    public boolean isActive() { return active; }
    public float getServerYaw() { return serverYaw; }
    public float getServerPitch() { return serverPitch; }
    public float getPrevServerYaw() { return prevServerYaw; }
    public float getPrevServerPitch() { return prevServerPitch; }
    public float getVisualYaw() { return visualYaw; }
    public float getVisualPitch() { return visualPitch; }

    public void updateMouseLook(double cursorDeltaX, double cursorDeltaY) {
        MinecraftClient client = MinecraftClient.getInstance();
        double sensitivity = client.options.getMouseSensitivity().getValue() * 0.6D + 0.2D;
        double multiplier = sensitivity * sensitivity * sensitivity * 8.0D;
        boolean invertY = client.options.getInvertYMouse().getValue();

        visualYaw += (float) (cursorDeltaX * multiplier * 0.15D);
        visualPitch += (float) ((invertY ? -cursorDeltaY : cursorDeltaY) * multiplier * 0.15D);
        visualPitch = MathHelper.clamp(visualPitch, -90.0F, 90.0F);
    }

    public void startRotation(float currentYaw, float currentPitch) {
        if (active) {
            return;
        }

        visualYaw = currentYaw;
        visualPitch = currentPitch;
        serverYaw = currentYaw;
        serverPitch = currentPitch;
        prevServerYaw = currentYaw;
        prevServerPitch = currentPitch;
        lockedAimPoint = null;
        lockedAimOffset = null;
        lockedAimTargetPos = null;
        aimPointTicksRemaining = 0;
        lazyCooldown = 0;
        ftNewTickCounter = 0;
        ftNewSettleStartMs = -1L;
        ftNewLastAttackMs = 0L;
        active = true;
    }

    public void stopRotation(Entity player) {
        if (!active) {
            return;
        }

        active = false;
        lockedAimPoint = null;
        lockedAimOffset = null;
        lockedAimTargetPos = null;
        aimPointTicksRemaining = 0;
        ftNewTickCounter = 0;
        ftNewSettleStartMs = -1L;
        ftNewLastAttackMs = 0L;

        if (player != null) {
            player.setYaw(visualYaw);
            player.setPitch(visualPitch);
        }
    }

    public void tickFtNew() {
        ftNewTickCounter++;
    }

    public void markFtNewAttack() {
        ftNewLastAttackMs = System.currentTimeMillis();
        ftNewSettleStartMs = -1L;
        ftNewTickCounter = 0;
    }

    public Vec3d getLockedAimPoint(MinecraftClient client, Entity target) {
        if (client.player == null || target == null) {
            return null;
        }

        Vec3d targetPos = target.getPos();
        if (lockedAimPoint != null && lockedAimOffset != null && lockedAimTargetPos != null && aimPointTicksRemaining > 0) {
            aimPointTicksRemaining--;
            if (lockedAimTargetPos.squaredDistanceTo(targetPos) < 1.0e-6d) {
                lockedAimPoint = lockedAimPoint.add(
                        lockedAimOffset.x * 0.12d,
                        lockedAimOffset.y * 0.05d,
                        lockedAimOffset.z * 0.12d
                );
                return lockedAimPoint;
            }
        }

        double width = Math.max(0.35d, target.getWidth());
        double height = Math.max(0.45d, target.getHeight());
        double halfWidth = width * 0.5d;

        double ox = (random.nextDouble() * 2.0d - 1.0d) * halfWidth * 0.35d;
        double oy = (random.nextDouble() - 0.5d) * height * 0.05d + height * 0.32d;
        double oz = (random.nextDouble() * 2.0d - 1.0d) * halfWidth * 0.35d;

        lockedAimTargetPos = targetPos;
        lockedAimOffset = new Vec3d(ox, oy, oz);
        if (lockedAimPoint == null) {
            lockedAimPoint = targetPos.add(lockedAimOffset);
        } else {
            lockedAimPoint = lockedAimPoint.add(
                    (targetPos.x + ox - lockedAimPoint.x) * 0.35d,
                    (targetPos.y + oy - lockedAimPoint.y) * 0.25d,
                    (targetPos.z + oz - lockedAimPoint.z) * 0.35d
            );
        }

        aimPointTicksRemaining = 4 + random.nextInt(4);
        return lockedAimPoint;
    }

    public void rotateTowards(MinecraftClient client, Vec3d targetPos, float maxSpeed) {
        if (client.player == null) {
            return;
        }

        prevServerYaw = serverYaw;
        prevServerPitch = serverPitch;

        updateLazyAim();

        Vec3d eyes = client.player.getEyePos();
        double dx = targetPos.x - eyes.x;
        double dy = targetPos.y - eyes.y;
        double dz = targetPos.z - eyes.z;
        double distXZ = Math.sqrt(dx * dx + dz * dz);

        float targetYaw = (float) (Math.toDegrees(Math.atan2(dz, dx)) - 90.0d);
        float targetPitch = (float) -Math.toDegrees(Math.atan2(dy, distXZ));

        targetYaw += lazyOffsetYaw;
        targetPitch = MathHelper.clamp(targetPitch + lazyOffsetPitch, -89.0f, 89.0f);

        float yawDiff = MathHelper.wrapDegrees(targetYaw - serverYaw);
        float pitchDiff = targetPitch - serverPitch;
        float angle = (float) Math.sqrt(yawDiff * yawDiff + pitchDiff * pitchDiff);
        float speed = Math.max(1.5f, Math.min(maxSpeed, angle));

        float finalYaw = serverYaw + MathHelper.clamp(yawDiff, -speed, speed);
        float finalPitch = MathHelper.clamp(serverPitch + MathHelper.clamp(pitchDiff, -speed, speed), -90.0f, 90.0f);
        applyQuantizedRotation(client, finalYaw, finalPitch);
    }

    public void rotateTowardsFtNew(MinecraftClient client, Vec3d targetPos, boolean attackReady) {
        if (client.player == null) {
            return;
        }

        prevServerYaw = serverYaw;
        prevServerPitch = serverPitch;

        Vec3d eyes = client.player.getEyePos();
        Vec3d jitteredTargetPos = targetPos.add(
                (random.nextDouble() - 0.5d) * 0.05d,
                (random.nextDouble() - 0.5d) * 0.03d,
                (random.nextDouble() - 0.5d) * 0.05d
        );
        double dx = jitteredTargetPos.x - eyes.x;
        double dy = jitteredTargetPos.y - eyes.y;
        double dz = jitteredTargetPos.z - eyes.z;
        double distXZ = Math.sqrt(dx * dx + dz * dz);

        float targetYaw = (float) (Math.toDegrees(Math.atan2(dz, dx)) - 90.0d);
        float targetPitch = (float) -Math.toDegrees(Math.atan2(dy, distXZ));
        targetPitch = MathHelper.clamp(targetPitch, -90.0f, 90.0f);

        long now = System.currentTimeMillis();
        if (attackReady) {
            ftNewSettleStartMs = -1L;
            applyFtNewStep(client, targetYaw, targetPitch, 130.0f, 130.0f, 0.85f, 0.0f, 0.0f, 0.0f);
            return;
        }

        if (ftNewSettleStartMs < 0L) {
            ftNewSettleStartMs = now;
        }

        long settleAge = now - ftNewSettleStartMs;
        float fade = 1.0f - MathHelper.clamp(settleAge / 300.0f, 0.0f, 1.0f);
        float yawDiff = MathHelper.wrapDegrees(targetYaw - serverYaw);
        float pitchDiff = targetPitch - serverPitch;
        float angle = Math.max((float) Math.hypot(Math.abs(yawDiff), Math.abs(pitchDiff)), 1.0e-4f);

        boolean pulse = now - ftNewLastAttackMs > 535L;
        float baseYawCap = angle > 35.0f ? 52.0f : 24.0f;
        float basePitchCap = angle > 20.0f ? 18.0f : 10.0f;
        float yawCap = Math.abs(yawDiff / angle) * (pulse ? Math.max(baseYawCap, 45.0f) : baseYawCap);
        float pitchCap = Math.abs(pitchDiff / angle) * (pulse ? Math.max(basePitchCap, 24.0f) : basePitchCap);

        float desiredYaw = serverYaw + MathHelper.clamp(yawDiff, -yawCap, yawCap);
        float desiredPitch = serverPitch + MathHelper.clamp(pitchDiff, -pitchCap, pitchCap);
        float smooth = angle > 30.0f ? 0.78f : 0.85f;
        applyFtNewStep(client, desiredYaw, desiredPitch, 360.0f, 360.0f, smooth, 0.0f, 0.0f, fade);
    }

    private void applyFtNewStep(MinecraftClient client, float desiredYaw, float desiredPitch, float yawLimit, float pitchLimit, float smooth, float yawBias, float pitchBias, float jitterFade) {
        float yawDiff = MathHelper.wrapDegrees(desiredYaw - serverYaw);
        float pitchDiff = desiredPitch - serverPitch;
        float angle = Math.max((float) Math.hypot(Math.abs(yawDiff), Math.abs(pitchDiff)), 1.0e-4f);

        float yawCap = Math.abs(yawDiff / angle) * yawLimit;
        float pitchCap = Math.abs(pitchDiff / angle) * pitchLimit;

        float nextYaw = lerp(smooth, serverYaw, serverYaw + MathHelper.clamp(yawDiff, -yawCap, yawCap) + yawBias);
        float nextPitch = lerp(smooth, serverPitch, serverPitch + MathHelper.clamp(pitchDiff, -pitchCap, pitchCap) + pitchBias);
        nextPitch = MathHelper.clamp(nextPitch, -90.0f, 90.0f);

        if (jitterFade > 0.0f) {
            nextPitch += (float) (Math.sin(System.currentTimeMillis() / 45.0d) * 0.18d * jitterFade);
        }

        float maxPerTick = 24.0f;
        float limitedYaw = serverYaw + MathHelper.clamp(MathHelper.wrapDegrees(nextYaw - serverYaw), -maxPerTick, maxPerTick);
        float limitedPitch = serverPitch + MathHelper.clamp(nextPitch - serverPitch, -maxPerTick, maxPerTick);
        applyQuantizedRotation(client, limitedYaw, limitedPitch);
    }

    private void applyQuantizedRotation(MinecraftClient client, float nextYaw, float nextPitch) {
        float yawDiff = MathHelper.wrapDegrees(nextYaw - serverYaw);
        float pitchDiff = nextPitch - serverPitch;

        double sens = client.options.getMouseSensitivity().getValue();
        double f = sens * 0.6D + 0.2D;
        double gcd = f * f * f * 8.0D * 0.15D;

        float yawStep = yawDiff;
        float pitchStep = pitchDiff;
        if (gcd > 1.0e-7D) {
            yawStep = (float) (Math.round(yawDiff / gcd) * gcd);
            pitchStep = (float) (Math.round(pitchDiff / gcd) * gcd);
        }

        float currentClientYaw = client.player.getYaw();
        float currentClientPitch = client.player.getPitch();

        float appliedYaw = currentClientYaw + yawStep;
        float appliedPitch = MathHelper.clamp(currentClientPitch + pitchStep, -90.0f, 90.0f);

        client.player.setYaw(appliedYaw);
        client.player.setPitch(appliedPitch);
        client.player.headYaw = appliedYaw;

        serverYaw = appliedYaw;
        serverPitch = appliedPitch;
    }

    private static float lerp(float t, float a, float b) {
        return a + t * (b - a);
    }

    private static float randRange(float min, float max) {
        return (float) ThreadLocalRandom.current().nextDouble(min, max);
    }

    private void updateLazyAim() {
        lazyCooldown--;
        if (lazyCooldown <= 0) {
            lazyOffsetYaw = (random.nextFloat() - 0.5f) * 0.4f;
            lazyOffsetPitch = (random.nextFloat() - 0.5f) * 0.2f;
            lazyCooldown = 9 + random.nextInt(8);
        } else {
            lazyOffsetYaw *= 0.9f;
            lazyOffsetPitch *= 0.9f;
        }
    }
}