﻿#include <iostream>
#include <windows.h>
#include <cmath>
#include <thread>
#include <chrono>

using namespace std;

// Calculate mouse movement pixels (based on Minecraft sensitivity formula)
int CalcPixelsPerDegree(double sensitivity, double angle)
{
    // Minecraft mouse movement formula: mouse movement distance = angle / (1.2 × (0.6 × (sensitivity ÷ 2) + 0.2)^3)
    // Simplified: mouse movement distance = angle / (1.2 × (0.3 × sensitivity + 0.2)^3)
    return round(angle / (1.2 * pow(0.3 * sensitivity + 0.2, 3)));
}

// Simulate mouse movement
void MoveMouse(uint64_t pixelsX, uint64_t pixelsY)
{
    // Convert pixels to units required by MOUSEINPUT
    // Windows mouse movement units are 0-65535, here we use relative movement
    INPUT input = { 0 };
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_MOVE;
    input.mi.dx = pixelsX;
    input.mi.dy = pixelsY;

    SendInput(1, &input, sizeof(INPUT));
}

// Check if F10 key is pressed
bool IsF10Pressed()
{
    return GetAsyncKeyState(VK_F10) & 0x8000;
}

// Wait for F10 key press
void WaitForF10()
{
    cout << "Press F10 to start rotating view..." << endl;
    while (true)
    {
        if (IsF10Pressed())
        {
            // Wait for key release to avoid repeated triggering
            while (IsF10Pressed())
            {
                Sleep(10);
            }
            return;
        }
        Sleep(50);
    }
}

int main()
{
    cout << "=== Minecraft View Auto-Rotation Tool ===" << endl;
    cout << "Note: Please ensure the game window is active" << endl;
    cout << "==========================================" << endl;

    // User input parameters
    double sensitivity;
    double rotations;

    cout << "Enter Minecraft sensitivity (usually 20-200, default is 100): ";
    int temp;
    cin >> temp;
    sensitivity = temp / 100.0;

    cout << "Enter number of rotations (e.g., 1, 2.5, 3.75): ";
    cin >> rotations;

    uint64_t time = 3000;

    cout << "Enter number of time: ";
    cin >> time;

    // Calculate total angle
    double totalAngle = rotations * 360.0;

    cout << "\n=== Configuration Information ===" << endl;
    cout << "Sensitivity: " << sensitivity << endl;
    cout << "Number of rotations: " << rotations << " rotations" << endl;
    cout << "Total angle: " << totalAngle << " degrees" << endl;

    // Calculate total pixel movement
    uint64_t totalPixels = CalcPixelsPerDegree(sensitivity, totalAngle);
    cout << "Pixels needed to move: " << totalPixels << " px" << endl;
    cout << "=================================" << endl;

    // Wait for user to press F10 to start
    WaitForF10();
    cout << "Starting view rotation..." << endl;

    const uint64_t sleep = 1000 / 60;
    const uint64_t step = totalPixels / (time / sleep);

    uint64_t progress = 0;
    while (progress <= totalPixels)
    {
        MoveMouse(step, 0);
        Sleep(sleep);
        progress += step;
    }

    cout << "Rotation complete!" << endl;
    cout << "Program will exit in 5 seconds..." << endl;

    // Countdown to exit
    for (int i = 5; i > 0; i--)
    {
        cout << i << "... " << endl;
        Sleep(1000);
    }

    return 0;
}

