#include "helpers/mathfr.lua"

function init()
    MAX_FOV = 120.0

    MIN_VELOCITY = 20 / 3.6
    MAX_VELOCITY = 110 / 3.6

    SMOOTHNESS = 0.2
end

local transition = 0
local fovWasChanged
function tick()
    if GetPlayerVehicle() ~= 0 then
        if GetBool("level.first_person_cam") then return end

        velocity = VecLength(GetPlayerVelocity())
        velocityChange = math:Clamp((velocity - MIN_VELOCITY) / math:Clamp(MAX_VELOCITY - MIN_VELOCITY, 1, math.huge), 0, 1)

        transition = math:Lerp(transition, velocityChange, SMOOTHNESS)
        if transition < 0.001 then transition = 0 end
        newFOV = math:Lerp(GetFloat("options.gfx.fov"), MAX_FOV, transition)

        SetCameraFov(newFOV)
        fovWasChanged = true
    elseif fovWasChanged then
        SetCameraFov(GetFloat("options.gfx.fov"))
        fovWasChanged = false
    end
end
