-- Services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local UserInputService = game:GetService("UserInputService")

-- Variables
local LocalPlayer = Players.LocalPlayer
local Camera = Workspace.CurrentCamera
local SilentAimEnabled = false -- Initial state of Silent Aim
local TargetPart = "Head" -- Preferred target part for Silent Aim
local FallbackPart = "UpperTorso" -- Fallback part if "Head" is unavailable
local SilentAimKey = Enum.KeyCode.G -- Keybind for toggling Silent Aim
local FOVRadius = 200 -- FOV radius in pixels (Updated to 200)
local SilentAimRange = 500 -- Max distance for target selection (Updated to 500)
local SmoothSpeed = 0.6 -- Adjusted for smoother aim movement

-- Cached Values
local ScreenCenter = Vector2.new(Camera.ViewportSize.X / 2, Camera.ViewportSize.Y / 2)

-- FOV Circle (Visual Representation)
local FOVCircle = Drawing.new("Circle")
FOVCircle.Visible = true
FOVCircle.Color = Color3.fromRGB(255, 0, 0) -- Default red for OFF
FOVCircle.Thickness = 2
FOVCircle.Filled = false
FOVCircle.Radius = FOVRadius
FOVCircle.Position = ScreenCenter

-- Function: Linear Interpolation for Smooth Movement
local function lerp(a, b, t)
    return a + (b - a) * math.clamp(t, 0, 1)
end

-- Function: Smoothly move towards a position
local function SmoothMouseMove(targetPosition)
    local mousePosition = UserInputService:GetMouseLocation()
    local moveVector = targetPosition - mousePosition

    -- Dynamic speed adjustment for responsiveness
    local dynamicSpeed = math.clamp(SmoothSpeed + (moveVector.Magnitude / 1000), SmoothSpeed, 1)

    local smoothMove = Vector2.new(
        lerp(mousePosition.X, targetPosition.X, dynamicSpeed),
        lerp(mousePosition.Y, targetPosition.Y, dynamicSpeed)
    )

    mousemoverel(smoothMove.X - mousePosition.X, smoothMove.Y - mousePosition.Y)
end

-- Function: Check if a target is within FOV
local function IsWithinFOV(targetPosition)
    local distance = (Vector2.new(targetPosition.X, targetPosition.Y) - ScreenCenter).Magnitude
    return distance <= FOVRadius
end

-- Function: Predict the target's future position
local function PredictTarget(target)
    if not target then return nil end

    local targetVelocity = target.Parent:FindFirstChild("HumanoidRootPart") and target.Parent.HumanoidRootPart.Velocity or Vector3.new(0, 0, 0)
    local targetPosition = target.Position
    local distance = (Camera.CFrame.Position - targetPosition).Magnitude
    local projectileSpeed = 1000 -- Example speed; replace with actual value for your game
    local timeToImpact = distance / projectileSpeed

    -- Adjust prediction based on movement
    local predictedPosition = targetPosition + (targetVelocity * timeToImpact)
    return predictedPosition
end

-- Function: Get the closest target within FOV
local function GetClosestTarget()
    local closestTarget = nil
    local shortestDistance = SilentAimRange

    for _, player in ipairs(Players:GetPlayers()) do
        if player ~= LocalPlayer and player.Character and player.Character:FindFirstChildOfClass("Humanoid") then
            local targetPart = player.Character:FindFirstChild(TargetPart) or player.Character:FindFirstChild(FallbackPart)

            if targetPart then
                local distanceToTarget = (Camera.CFrame.Position - targetPart.Position).Magnitude

                if distanceToTarget <= SilentAimRange then
                    local screenPosition, onScreen = Camera:WorldToViewportPoint(targetPart.Position)

                    if onScreen and IsWithinFOV(screenPosition) then
                        local distance = (Vector2.new(screenPosition.X, screenPosition.Y) - ScreenCenter).Magnitude

                        if distance < shortestDistance then
                            closestTarget = targetPart
                            shortestDistance = distance
                        end
                    end
                end
            end
        end
    end

    return closestTarget
end

-- Silent Aim Toggle
UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.KeyCode == SilentAimKey then
        SilentAimEnabled = not SilentAimEnabled

        -- Update FOV Circle color
        FOVCircle.Color = SilentAimEnabled and Color3.fromRGB(0, 255, 0) or Color3.fromRGB(255, 0, 0) -- Green for ON, Red for OFF

        print("Silent Aim " .. (SilentAimEnabled and "Enabled" or "Disabled"))
    end
end)

-- Main Loop
RunService.RenderStepped:Connect(function(deltaTime)
    if SilentAimEnabled then
        local target = GetClosestTarget()

        if target then
            local predictedPosition = PredictTarget(target)
            if predictedPosition then
                local screenPosition = Camera:WorldToViewportPoint(predictedPosition)
                SmoothMouseMove(Vector2.new(screenPosition.X, screenPosition.Y))
            end
        end
    end
end)