import pyautogui
import numpy as np
import time
import math
import random

class SmoothAimBot:
    def __init__(self, screen_width=1920, screen_height=1080, aim_speed=0.1, fov=200):
        """
        初始化平滑自瞄
        :param screen_width: 屏幕宽度
        :param screen_height: 屏幕高度
        :param aim_speed: 自瞄速度（越小越平滑，但速度越慢）
        :param fov: 视野范围（像素），用于检测目标
        """
        self.screen_width = screen_width
        self.screen_height = screen_height
        self.aim_speed = aim_speed
        self.fov = fov
        self.center_x = screen_width // 2
        self.center_y = screen_height // 2

    def get_distance(self, x1, y1, x2, y2):
        """计算两点之间的距离"""
        return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)

    def smooth_move(self, target_x, target_y, steps=10):
        """
        平滑移动鼠标到目标位置
        :param target_x: 目标 x 坐标
        :param target_y: 目标 y 坐标
        :param steps: 平滑移动的步数
        """
        current_x, current_y = pyautogui.position()
        print(f"当前鼠标位置: ({current_x}, {current_y})")
        print(f"目标位置: ({target_x}, {target_y})")

        # 计算每一步的增量
        delta_x = (target_x - current_x) / steps
        delta_y = (target_y - current_y) / steps

        for step in range(steps):
            new_x = current_x + delta_x * (step + 1)
            new_y = current_y + delta_y * (step + 1)
            pyautogui.moveTo(new_x, new_y, duration=self.aim_speed / steps)
            time.sleep(self.aim_speed / steps)

    def simulate_aim(self, target_x, target_y):
        """
        模拟自瞄逻辑
        :param target_x: 目标 x 坐标
        :param target_y: 目标 y 坐标
        """
        distance = self.get_distance(self.center_x, self.center_y, target_x, target_y)
        if distance <= self.fov:
            print(f"目标在视野范围内，距离: {distance:.2f} 像素，开始平滑自瞄...")
            self.smooth_move(target_x, target_y)
        else:
            print(f"目标超出视野范围，距离: {distance:.2f} 像素，忽略...")

    def run_test(self):
        """运行测试，模拟自瞄"""
        print("平滑自瞄测试开始，按 Ctrl+C 退出...")
        try:
            while True:
                # 模拟随机生成目标位置（在实际游戏中需要通过图像识别获取目标位置）
                target_x = random.randint(0, self.screen_width)
                target_y = random.randint(0, self.screen_height)
                self.simulate_aim(target_x, target_y)
                time.sleep(1)  # 每秒模拟一次
        except KeyboardInterrupt:
            print("测试已结束。")

if __name__ == "__main__":
    # 初始化平滑自瞄
    aim_bot = SmoothAimBot(screen_width=1920, screen_height=1080, aim_speed=0.1, fov=200)

    # 运行测试
    aim_bot.run_test()