struct TrackingConfig {
    float maxDistance = 15.0f;      // 最大追踪距离（米）
    float minLockDistance = 0.5f;   // 最小锁定距离（防止近战抖动）
    float angleSpeed = 1200.0f;     // 瞄准角速度（度/秒）
    float snapThreshold = 0.1f;     // 快速吸附阈值（米）
    float predictionFactor = 0.3f;  // 弹道预测系数（0-1）
};

class CTTrackingSystem {
private:
    std::vector<CEnemy*> activeTargets;
    CEnemy* currentTarget = nullptr;
    TrackingConfig config;

    // 获取敌人屏幕坐标
    glm::vec2 GetScreenPosition(CEnemy* enemy) {
        glm::vec3 worldPos = enemy->GetPosition();
        return WorldToScreen(worldPos); // 实现坐标转换
    }

public:
    void UpdateTargets() {
        // 动态筛选有效目标（距离+可见性检测）
        activeTargets.clear();
        for (auto* enemy : GetAllEnemies()) {
            if (IsTargetValid(enemy)) {
                activeTargets.push_back(enemy);
            }
        }
    }

    // 动态目标选择算法
    CEnemy* SelectBestTarget() {
        CEnemy* bestTarget = nullptr;
        float minAngleDiff = FLT_MAX;

        for (auto* enemy : activeTargets) {
            glm::vec2 screenPos = GetScreenPosition(enemy);
            float angleDiff = CalculateAngleDifference(screenPos);
            
            if (angleDiff < minAngleDiff && 
                glm::distance(screenPos, GetScreenCenter()) > config.minLockDistance) {
                bestTarget = enemy;
                minAngleDiff = angleDiff;
            }
        }
        return bestTarget;
    }
};

void FastSnapToTarget(CEnemy* target) {
    if (!target) return;

    glm::vec2 targetPos = GetScreenPosition(target);
    glm::vec2 currentPos = GetAimPosition();

    // 计算方向向量
    glm::vec2 direction = targetPos - currentPos;
    direction = glm::normalize(direction);

    // 弹道预测修正
    glm::vec2 predictedPos = targetPos + direction * config.predictionFactor;

    // 平滑插值移动
    currentPos = glm::lerp(
        currentPos, 
        predictedPos, 
        config.angleSpeed * GetDeltaTime() / 1000.0f
    );

    // 强制吸附阈值处理
    if (glm::distance(currentPos, targetPos) < config.snapThreshold) {
        currentPos = targetPos;
    }

    SetAimPosition(currentPos);
}

float CalculateAngleDifference(glm::vec2 targetPos) {
    glm::vec2 center = GetScreenCenter();
    glm::vec2 direction = targetPos - center;

    // 计算极角
    float targetAngle = atan2(direction.y, direction.x) * 180.0f / M_PI;
    float currentAngle = GetAimRotation();

    // 计算最小角度差（处理360°环绕）
    float diff = fmod(targetAngle - currentAngle + 180.0f, 360.0f);
    return diff > 180.0f ? diff - 360.0f : diff;
}

enum class TargetPriority {
    LOW,
    MEDIUM,
    HIGH
};

TargetPriority GetTargetPriority(CEnemy* enemy) {
    float distance = glm::distance(
        GetPlayerPosition(), 
        enemy->GetPosition()
    );

    if (distance < 3.0f) return TargetPriority::HIGH;  // 近距离优先
    if (enemy->IsMoving()) return TargetPriority::MEDIUM; // 移动目标优先
    return TargetPriority::LOW;
}

void BypassAntiCheat() {
    // 鼠标移动轨迹伪装
    std::vector<POINT> fakePath;
    for (int i = 0; i < 10; ++i) {
        fakePath.push_back(InterpolatePosition(
            GetAimPosition(),
            GetRandomOffset(2.0f)
        ));
    }
    
    // 模拟人类移动延迟
    for (auto& pos : fakePath) {
        MouseMoveTo(pos.x, pos.y);
        Sleep(RandomRange(5, 15));
    }
}

void GameLoop() {
    while (true) {
        // 1. 更新目标列表
        trackingSystem.UpdateTargets();
        
        // 2. 选择最佳目标
        CEnemy* target = trackingSystem.SelectBestTarget();
        
        if (target && IsTriggerPressed()) {
            // 3. 执行快速吸附
            trackingSystem.FastSnapToTarget(target);
            
            // 4. 反作弊伪装
            trackingSystem.BypassAntiCheat();
        }
        
        // 5. 帧率控制
        Sleep(16); // ~60FPS
    }
}

#include <dinput.h>
LPDIRECTINPUT8 g_pDI;
LPDIRECTINPUTDEVICE8 g_pMouse;

void InitDirectInput() {
    DirectInput8Create(...);
    g_pDI->CreateDevice(GUID_SysMouse, &g_pMouse, NULL);
    g_pMouse->SetCooperativeLevel(hWnd, DISCL_EXCLUSIVE | DISCL_BACKGROUND);
}

Octree<CEntity> battleArea;
battleArea.Insert(player);
auto nearbyEnemies = battleArea.QueryRadius(playerPos, 200.0f);

std::thread logicThread(GameLoop);
std::thread renderThread(RenderLoop);
logicThread.join();
renderThread.join();

// 可视化调试输出
void DebugDraw() {
    if (g_debugMode) {
        DrawCircle(GetAimPosition(), 10.0f, RED);  // 瞄准点
        DrawLine(GetPlayerPos(), targetPos, BLUE); // 瞄准线
        DrawText("FPS: " + std::to_string(FPS), 10, 10, WHITE);
    }
}
花海