function init()
    -- Config
    THRESHERVELOCITY = 8.0

    thisVehicle = FindVehicle("dumptruck")
    mainBody = GetVehicleBody(thisVehicle)
    thresherJoints = FindJoints("thresher")

    broken = false
end

function tick(dt)

    if not broken then

        -- Vehicle check
        isPlayerInVehicle = GetPlayerVehicle() == thisVehicle

        -- Update motor and check if it is intact
        local numConnected = 0
        for i=1, #thresherJoints do
            local joint = thresherJoints[i]
            local shapes = GetJointShapes(joint)

            local connected = false
            for j = 1, #shapes do
                local body = GetShapeBody(shapes[j])

                local jointedBodies = GetJointedBodies(body)

                for k = 1, #jointedBodies do
                    if jointedBodies[k] == mainBody  then
                        connected = true
                        numConnected = numConnected + 1
                    end
                end
            end

            SetJointMotor(joint, bool_to_number(isPlayerInVehicle and connected) * THRESHERVELOCITY)
        end

        if numConnected == 0 then
            broken = true
        end
    end


end

function bool_to_number(value)
    return value and 1 or 0
end