import java.awt.Robot;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.MouseInfo;
import java.lang.Math;

public class BodyRangeAim {
    private static boolean enabled = true;
    private static final double BODY_RANGE = 0.95;
    private static final int FOV = 75;
    private static final int SMOOTHNESS = 10;
    private static final int TRIGGER_KEY = KeyEvent.VK_MOUSE_BUTTON2;
    private static final int TOGGLE_KEY = KeyEvent.VK_F8;
    private static final boolean TEAM_IGNORE = true;
    private static final int MIN_DISTANCE = 3;
    private static final int MAX_DISTANCE = 230;
    private static long lastCheckTime = 0;
    private static final long CHECK_INTERVAL = 5;
    private static Robot robot;

    public static void main(String[] args) {
        try {
            robot = new Robot();
            new Thread(BodyRangeAim::keyMonitorThread).start();
            new Thread(BodyRangeAim::rangeAimControlThread).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void keyMonitorThread() {
        while (true) {
            if (isKeyPressed(TOGGLE_KEY)) {
                enabled = !enabled;
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

    private static void rangeAimControlThread() {
        while (true) {
            long currentTime = System.currentTimeMillis();
            if (enabled && isKeyPressed(TRIGGER_KEY) && currentTime - lastCheckTime >= CHECK_INTERVAL) {
                Target target = getValidTargetInRange();
                if (target != null) {
                    adjustMouseToBodyRange(target);
                }
                lastCheckTime = currentTime;
            }
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

    private static Target[] scanTargets() {
        return new Target[0];
    }

    private static Target getValidTargetInRange() {
        Target[] targets = scanTargets();
        if (targets == null || targets.length == 0) return null;

        Target validTarget = null;
        double minDist = Double.MAX_VALUE;
        for (Target t : targets) {
            if ((!TEAM_IGNORE || !t.isTeam) && t.distance >= MIN_DISTANCE && t.distance <= MAX_DISTANCE) {
                double bodyRangeCheck = calculateBodyRangeOffset(t);
                if (bodyRangeCheck <= BODY_RANGE && t.distance < minDist) {
                    minDist = t.distance;
                    validTarget = t;
                }
            }
        }
        return validTarget;
    }

    private static double calculateBodyRangeOffset(Target target) {
        Point screenCenter = MouseInfo.getPointerInfo().getLocation();
        double dx = target.screenX - screenCenter.x;
        double dy = target.screenY - screenCenter.y;
        return Math.sqrt(dx * dx + dy * dy) / (target.distance * 2);
    }

    private static Point calculateTargetBodyPos(Target target) {
        Point screenCenter = MouseInfo.getPointerInfo().getLocation();
        double angle = Math.atan2(target.screenY - screenCenter.y, target.screenX - screenCenter.x);
        int targetX = (int)(screenCenter.x + BODY_RANGE * 90 * Math.cos(angle));
        int targetY = (int)(screenCenter.y + BODY_RANGE * 90 * Math.sin(angle));
        return new Point(targetX, targetY);
    }

    private static void adjustMouseToBodyRange(Target target) {
        Point targetPos = calculateTargetBodyPos(target);
        Point currPos = MouseInfo.getPointerInfo().getLocation();
        double dx = (targetPos.x - currPos.x) / SMOOTHNESS;
        double dy = (targetPos.y - currPos.y) / SMOOTHNESS;
        robot.mouseMove((int)(currPos.x + dx), (int)(currPos.y + dy));
    }

    private static boolean isKeyPressed(int keyCode) {
        return java.awt.Toolkit.getDefaultToolkit().getLockingKeyState(keyCode)
                || (java.awt.event.KeyEvent.getExtendedKeyCodeForChar(keyCode) != KeyEvent.CHAR_UNDEFINED
                && java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue().peekEvent() instanceof KeyEvent
                && ((KeyEvent)java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue().peekEvent()).getKeyCode() == keyCode);
    }

    static class Target {
        int screenX;
        int screenY;
        double distance;
        boolean isTeam;

        Target(int x, int y, double dist, boolean team) {
            screenX = x;
            screenY = y;
            distance = dist;
            isTeam = team;
        }
    }
}





