-- This script prevents attaching more than one implement to a vehicle's attacher joint

AttacherControl = {};

function AttacherControl:preAttachImplement(attacherVehicle, implement)
    -- Check if the attacher vehicle already has an implement attached
    for _, jointDesc in pairs(attacherVehicle.spec_attacherJoints.attacherJoints) do
        if jointDesc.attachedImplement ~= nil then
            -- If an implement is already attached, print a message and prevent attaching
            print("You cannot attach more than one implement to this vehicle!")
            return false
        end
    end

    -- If no implement is attached, allow the attachment
    return true
end

-- Load the script into the game
function AttacherControl:loadMap(name)
    if g_currentMission ~= nil then
        -- Register the function to be called before attaching an implement
        VehicleTypeManager.finalizeTypes = Utils.appendedFunction(VehicleTypeManager.finalizeTypes, function()
            for vehicleType, typeDef in pairs(g_vehicleTypeManager.types) do
                SpecializationUtil.registerFunction(typeDef.specializationsByName.attacherJoints, "preAttachImplement", AttacherControl.preAttachImplement)
            end
        end)
    end
end

-- Register the event listeners
addModEventListener(AttacherControl)
