-- Define the script name and author
script_name("CamHack")
script_author("sanek a.k.a Maks_Fender, edited by ANIKI")

require "lib.moonloader"
local mad = require "MoonAdditions"

-- Create a table to encapsulate all variables and function
local camHack = {
    -- Define virtual key codes with descriptive names
    KEY_TOGGLE_FLY_MODE    = 0x43,  -- VK_C (Toggle fly mode)
    KEY_ENABLE_FLY_MODE    = 0x31,  -- VK_1 (Enable fly mode)
    KEY_DISABLE_FLY_MODE   = 0x32,  -- VK_2 (Disable fly mode)
    KEY_MOVE_FORWARD       = 0x57,  -- VK_W (Move forward)
    KEY_MOVE_BACKWARD      = 0x53,  -- VK_S (Move backward)
    KEY_STRAFE_LEFT        = 0x41,  -- VK_A (Strafe left)
    KEY_STRAFE_RIGHT       = 0x44,  -- VK_D (Strafe right)
    KEY_MOVE_UP            = 0x20,  -- VK_SPACE (Move up)
    KEY_MOVE_DOWN          = 0x10,  -- VK_SHIFT (Move down)
    KEY_INCREASE_SPEED     = 0xBB,  -- VK_ADD (Increase speed)
    KEY_DECREASE_SPEED     = 0xBD,  -- VK_SUBTRACT (Decrease speed)

    -- Define camera hack variables
    camHack_posX = 0,  -- Camera position coordinates
    camHack_posY = 0,
    camHack_posZ = 0,
    camHack_angZ = 0,  -- Camera angle in Z direction
    camHack_angY = 0,  -- Camera angle in Y direction
    camHack_speed = 1.0,  -- Movement speed of the camera
    camHack_flyModeActive = 0,  -- State of fly mode: 0 = inactive, 1 = active
    camHack_collisionEnabled = true,  -- Flag for collision detection

    -- Flags for different types of collision checks
    camHack_checkSolidObjects = true,
    camHack_checkVehicles = false,
    camHack_checkPeds = false,
    camHack_checkObjects = true,
    camHack_checkParticles = false,
    camHack_seeThroughObjects = false,
    camHack_ignoreSomeObjects = false,
    camHack_allowShootThrough = false
}

function camHack.processCamHack()  -- Wait for a short time before continuing

    -- Activate fly mode if conditions are met
    if camHack.camHack_flyModeActive == 0 then
        if isKeyDown(camHack.KEY_TOGGLE_FLY_MODE) and isKeyDown(camHack.KEY_ENABLE_FLY_MODE) then
            displayRadar(false)  -- Hide the radar
            displayHud(false)  -- Hide the HUD
            camHack.camHack_posX, camHack.camHack_posY, camHack.camHack_posZ = getCharCoordinates(playerPed)  -- Get player position
            camHack.camHack_angZ = -getCharHeading(playerPed)  -- Set camera heading
            camHack.camHack_angY = 0.0  -- Reset vertical angle
            setFixedCameraPosition(camHack.camHack_posX, camHack.camHack_posY, camHack.camHack_posZ, 0.0, 0.0, 0.0)  -- Fix camera position
            lockPlayerControl(true)  -- Lock player controls
            camHack.camHack_flyModeActive = 1  -- Set fly mode to active
        end
    else
        -- Check for input and update camera position if fly mode is active
        if not sampIsChatInputActive() and not isSampfuncsConsoleActive() then
            local offMouX, offMouY = getPcMouseMovement()  -- Get mouse movement
            camHack.camHack_angZ = (camHack.camHack_angZ + offMouX / 4.0) % 360  -- Update camera Z angle
            camHack.camHack_angY = math.max(math.min(camHack.camHack_angY + offMouY / 4.0, 89.0), -89.0)  -- Update camera Y angle

            -- Calculate movement direction based on camera angles
            local radZ, radY = math.rad(camHack.camHack_angZ), math.rad(camHack.camHack_angY)
            local sinZ, cosZ, sinY, cosY = math.sin(radZ), math.cos(radZ), math.sin(radY), math.cos(radY)
            local movX, movY, movZ = cosY * sinZ, cosY * cosZ, sinY
            local newX, newY, newZ = camHack.camHack_posX, camHack.camHack_posY, camHack.camHack_posZ

            -- Handle forward and backward movement
            if isKeyDown(camHack.KEY_MOVE_FORWARD) then
                newX = camHack.camHack_posX + movX * camHack.camHack_speed
                newY = camHack.camHack_posY + movY * camHack.camHack_speed
                newZ = camHack.camHack_posZ + movZ * camHack.camHack_speed
            elseif isKeyDown(camHack.KEY_MOVE_BACKWARD) then
                newX = camHack.camHack_posX - movX * camHack.camHack_speed
                newY = camHack.camHack_posY - movY * camHack.camHack_speed
                newZ = camHack.camHack_posZ - movZ * camHack.camHack_speed
            end

            -- Handle side-to-side movement
            if isKeyDown(camHack.KEY_STRAFE_LEFT) or isKeyDown(camHack.KEY_STRAFE_RIGHT) then
                local sideOffset = isKeyDown(camHack.KEY_STRAFE_LEFT) and -math.pi / 2 or math.pi / 2
                local sideZ = math.sin(math.rad(camHack.camHack_angZ) + sideOffset)
                local sideX = math.cos(math.rad(camHack.camHack_angZ) + sideOffset)
                newX = newX + sideZ * camHack.camHack_speed
                newY = newY + sideX * camHack.camHack_speed
            end

            -- Handle vertical movement (up and down)
            if isKeyDown(camHack.KEY_MOVE_UP) then
                newZ = camHack.camHack_posZ + camHack.camHack_speed
            elseif isKeyDown(camHack.KEY_MOVE_DOWN) then
                newZ = camHack.camHack_posZ - camHack.camHack_speed
            end

            -- Handle collision detection if enabled
            local collision, colPoint, adjX, adjY, adjZ
            if camHack.camHack_collisionEnabled then
                local result
                result, colPoint = processLineOfSight(
                    camHack.camHack_posX, camHack.camHack_posY, camHack.camHack_posZ, newX, newY, newZ,
                    camHack.camHack_checkSolidObjects, camHack.camHack_checkVehicles, camHack.camHack_checkPeds,
                    camHack.camHack_checkObjects, camHack.camHack_checkParticles, camHack.camHack_seeThroughObjects,
                    camHack.camHack_ignoreSomeObjects, camHack.camHack_allowShootThrough
                )
                if result then
                    collision, adjX, adjY, adjZ = true, camHack.camHack_posX, camHack.camHack_posY, camHack.camHack_posZ
                else
                    collision, adjX, adjY, adjZ = false, newX, newY, newZ
                end
            else
                collision, adjX, adjY, adjZ = false, newX, newY, newZ
            end

            -- Update camera position and set new fixed camera position
            camHack.camHack_posX, camHack.camHack_posY, camHack.camHack_posZ = adjX, adjY, adjZ
            setFixedCameraPosition(camHack.camHack_posX, camHack.camHack_posY, camHack.camHack_posZ, 0.0, 0.0, 0.0)
            pointCameraAtPoint(camHack.camHack_posX + movX, camHack.camHack_posY + movY, camHack.camHack_posZ + movZ, 2)

            -- Adjust camera speed with keys
            if isKeyDown(camHack.KEY_INCREASE_SPEED) then
                camHack.camHack_speed = camHack.camHack_speed + 0.01
            elseif isKeyDown(camHack.KEY_DECREASE_SPEED) then
                camHack.camHack_speed = math.max(camHack.camHack_speed - 0.01, 0.01)
            end

            -- Display collision and debug information
            local collisionText
            if collision then
                collisionText = string.format(
                    "~ghhh~Collision Detected~n~" ..
                    "~w~Entity: ~ph~%d~n~" ..
                    "~w~Type: ~ph~%d~ph~ ~w~Surface: ~ph~%d~n~" ..
                    "~w~Position: ~ph~(%.2f, %.2f, %.2f)~n~" ..
                    "~w~Normal: ~ph~(%.2f, %.2f, %.2f)",
                    colPoint.entity, colPoint.entityType, colPoint.surfaceType[1],
                    colPoint.pos[1], colPoint.pos[2], colPoint.pos[3],
                    colPoint.normal[1], colPoint.normal[2], colPoint.normal[3]
                )
            else
                collisionText = "~rhhh~No Collision"
            end

            if not camHack.camHack_collisionEnabled then
                collisionText = "~yh~Collision Disabled"
            end

            -- Format and display debug information on screen
            local text = string.format(
                "~bhhh~CamHack Debug Info~n~" ..
                "~w~Position:~n~" ..
                " ~ph~(%.2f, %.2f, %.2f)~n~" ..
                "~w~Direction:~n~" ..
                " ~ph~(%.2f, %.2f, %.2f)~n~" ..
                "~w~Angle:~n~" ..
                " ~ph~Z: %.2f| ~ph~Y: %.2f|~n~" ..
                "~w~Speed:~n~" ..
                " ~p~~h~%.2f units/sec~n~" ..
                "~w~Collision Status:~n~" ..
                " %s",
                camHack.camHack_posX, camHack.camHack_posY, camHack.camHack_posZ,
                camHack.camHack_posX + movX, camHack.camHack_posY + movY, camHack.camHack_posZ + movZ,
                camHack.camHack_angZ, camHack.camHack_angY, camHack.camHack_speed,
                collisionText
            )

            mad.draw_text(text, 45, 240, mad.font_style.BEFORE_TEXT, 0.40, 1.0, mad.font_align.LEFT, 9999, true, false, 255, 255, 255, 255, 1, 2, 0, 0, 0, 150)

            -- Deactivate fly mode and restore player controls if conditions are met
            if isKeyDown(camHack.KEY_TOGGLE_FLY_MODE) and isKeyDown(camHack.KEY_DISABLE_FLY_MODE) then
                lockPlayerControl(false)  -- Unlock player controls
                restoreCameraJumpcut()  -- Restore default camera behavior
                setCameraBehindPlayer()  -- Set camera behind player
                camHack.camHack_flyModeActive = 0  -- Set fly mode to inactive
            end
        end
    end
end

return camHack

вынеси включение и выключение в отдельные функции