function init()
    lights = FindShapes("bus_light")
    doors = FindShapes("bus_door")
    door_lights = FindShapes("bus_door_light")
    vehicle = FindVehicle("EVF")
    for i = 1, #lights do
        SetShapeEmissiveScale(lights[i], 0)
    end
    for i = 1, #door_lights do
        SetShapeEmissiveScale(door_lights[i], 0)
    end
    door_joints = {}
    for i = 1, #doors do
        door_joints[i] = GetShapeJoints(doors[i])[1]
    end
    for i = 1, #door_joints do
        local min, max = GetJointLimits(door_joints[i])
        SetJointMotorTarget(door_joints[i], min, 1.5)
    end
    if IsHandleValid(doors[1]) then
        havedoors = true
    end
    if IsHandleValid(lights[1]) then
        havelights = true
    end

    buttons = FindShapes("door_button")
    for i = 1, #buttons do
        SetTag(buttons[i], "interact", "Toggle")
    end
    doorsound = LoadSound("MOD/extra-scripts/snd/door.ogg")
    click = LoadSound("MOD/extra-scripts/snd/click.ogg")
    click_out = LoadSound("MOD/extra-scripts/snd/click_out.ogg")

    routeTab = FindShapes("route")
    routeTabHide = FindShapes("routeHide")
    disableRouteSigns()

    cooldown = 0
end

function tick()
    for i = 1, #routeTab do
        if IsShapeBroken(routeTab[i]) then
            SetShapeEmissiveScale(routeTab[i], 0)
        end
    end 
    for i = 1, #routeTabHide do
        if IsShapeBroken(routeTabHide[i]) then
            SetTag(routeTabHide, "invisible")
        end
    end 
    if GetPlayerVehicle() == vehicle then
        enableRouteSigns()
        if InputPressed("K") then
            if lightenabled then
                lightenabled = false
                for i = 1, #lights do
                    SetShapeEmissiveScale(lights[i], 0)
                end
            else
                lightenabled = true
                for i = 1, #lights do
                    SetShapeEmissiveScale(lights[i], 1)
                end
            end
            if lightenabled then
                PlaySound(click_out, GetVehicleTransform(vehicle).pos, 0.5)
            else
                PlaySound(click, GetVehicleTransform(vehicle).pos, 0.5)
            end
        end
        if InputPressed("J") and cooldown == 0 then
            doorscontrol()
        end
    else
        disableRouteSigns()
    end
    for i = 1, #buttons do
        if GetPlayerInteractShape() == buttons[i] and InputPressed("interact") and cooldown == 0 then
            doorscontrol()
        end
    end
    if cooldown == 1 then
        cooldown = 0
    end

    if cooldown ~= 0 then
        SetBodyActive(GetVehicleBody(vehicle), true)
    end
end

function draw()
    if GetPlayerVehicle() == vehicle and not GetBool("hud.disable") then
        if havelights and havedoors then
            UiPush()
                UiAlign("center middle")
                UiTranslate(UiCenter()-900, UiMiddle()+496)
                UiPush()
                    UiColor(0,0,0,0.5)
                    UiTranslate(100, -20)
                    UiImageBox("ui/common/box-solid-6.png", 280, 100, 6, 6)
                UiPop()
                UiFont("regular.ttf", 30)
                UiPush()
                    UiText("K")
                    UiTranslate(120, 2)
                    UiColorFilter(1, 1, 1, 0.7)
                    if lightenabled then
                        UiText("Turn off int. light")
                    else
                        UiText("Turn on int. light")
                    end
                UiPop()
                UiTranslate(0, -40)
                UiPush()
                    UiText("J")
                    UiColorFilter(1, 1, 1, 0.7)
                    if doorsopen then
                        UiTranslate(93, 0)
                        UiText("Close doors")
                    else
                        UiTranslate(93, 2)
                        UiText("Open doors")
                    end
                UiPop()
            UiPop()
        elseif havedoors and not havelights then
            UiPush()
                UiAlign("center middle")
                UiTranslate(UiCenter()-900, UiMiddle()+496)
                UiPush()
                    UiColor(0,0,0,0.5)
                    UiTranslate(80, 0)
                    UiImageBox("ui/common/box-solid-6.png", 220, 50, 6, 6)
                UiPop()
                UiFont("regular.ttf", 30)
                UiText("J")
                UiColorFilter(1, 1, 1, 0.7)
                if doorsopen then
                    UiTranslate(100, 0)
                    UiText("Close doors")
                else
                    UiTranslate(100, 2)
                    UiText("Open doors")
                end
            UiPop()
        elseif havelights and not havedoors then
            UiPush()
                UiAlign("center middle")
                UiTranslate(UiCenter()-900, UiMiddle()+496)
                UiPush()
                    UiColor(0,0,0,0.5)
                    UiTranslate(107, 0)
                    UiImageBox("ui/common/box-solid-6.png", 280, 50, 6, 6)
                UiPop()
                UiFont("regular.ttf", 30)
                UiText("K")
                UiTranslate(120, 2)
                UiColorFilter(1, 1, 1, 0.7)
                if lightenabled then
                    UiText("Turn off int. light")
                else
                    UiText("Turn on int. light")
                end
            UiPop()
        end
    end
end

function doorscontrol()
    if doorsopen then
        doorsopen = false
        for i = 1, #door_joints do
            local min, max = GetJointLimits(door_joints[i])
            local pos = GetShapeWorldTransform(doors[i]).pos
            if not IsJointBroken(door_joints[i]) then
                PlaySound(doorsound, pos)
            end
            SetJointMotorTarget(door_joints[i], min, 1)
        end
        for i = 1, #door_lights do
            SetShapeEmissiveScale(door_lights[i], 0)
        end
        SetValue("cooldown", 1, "linear", 1.5)
    else
        doorsopen = true
        for i = 1, #door_joints do
            local min, max = GetJointLimits(door_joints[i])
            SetJointMotorTarget(door_joints[i], max, 1)
            local pos = GetShapeWorldTransform(doors[i]).pos
            if not IsJointBroken(door_joints[i]) then
                PlaySound(doorsound, pos)
            end
        end
        for i = 1, #door_lights do
            SetShapeEmissiveScale(door_lights[i], 1)
        end
        SetValue("cooldown", 1, "linear", 1.5)
    end
end

function enableRouteSigns()
    for i = 1, #routeTab do
        if not IsShapeBroken(routeTab[i]) then
            SetShapeEmissiveScale(routeTab[i], 0.8)
        end
    end
    for i = 1, #routeTabHide do
        if not IsShapeBroken(routeTabHide[i]) then
            RemoveTag(routeTabHide[i], "invisible")
            SetShapeEmissiveScale(routeTabHide[i], 0.8)
        end
    end
end

function disableRouteSigns()
    for i = 1, #routeTab do
        SetShapeEmissiveScale(routeTab[i], 0)
    end
    for i = 1, #routeTabHide do
        SetTag(routeTabHide[i], "invisible")
        SetShapeEmissiveScale(routeTabHide[i], 0)
    end
end