function init()
    lightmode = 0
    beacon1 = FindShapes("beacon1")
    beacon2 = FindShapes("beacon2")
    vehicle = FindVehicle("EVF")
    SetShapeEmissiveScale(beacon1, 0)
    SetShapeEmissiveScale(beacon2, 0)
    flashtime = 0
    siren = LoadLoop("MOD/extra-scripts/snd/siren1.ogg")
end

function tick()
    if GetPlayerVehicle() == vehicle then
        if InputPressed("I") then
            lightmode = lightmode + 1
        end

        if lightmode > 2 then
            lightmode = 0
        end
        inveh = true
    else
        inveh = false
    end
    vehiclepos = GetVehicleTransform(vehicle).pos
end

function update ()
    if lightmode == 0 then
        for i = 1, #beacon1 do
            SetShapeEmissiveScale(beacon1[i], 0)
        end
        for i = 1, #beacon2 do
            SetShapeEmissiveScale(beacon2[i], 0)
        end
    end

    if lightmode == 1 or lightmode == 2 then
        lightflash()
    end

    if lightmode == 2 then
        if not IsShapeBroken(beacon1[1]) and not IsShapeBroken(beacon2[1]) then
            PlayLoop(siren, vehiclepos, 0.75)
        end
    end
end

function lightflash()
    if lightmode ~= 0 then
        flashtime = flashtime + 0.05
        flashmode = math.modf(flashtime)
    else
        flashtime = 0
    end
    if flashtime >= 2 then
        flashtime = 0
    end

    if flashmode == 0 then
        for i = 1, #beacon1 do
            if not IsShapeBroken(beacon1[i]) then
                if not blink1 then
                    SetShapeEmissiveScale(beacon1[i], 1)
                end
            end
        end
        for i = 1, #beacon2 do
            SetShapeEmissiveScale(beacon2[i], 0)
        end
    end
    if flashmode == 1 then
        for i = 1, #beacon1 do
            SetShapeEmissiveScale(beacon1[i], 0)
        end
        for i = 1, #beacon2 do
            if not IsShapeBroken(beacon2[i]) then
                if not blink2 then
                    SetShapeEmissiveScale(beacon2[i], 1)
                end
            end
        end
    end

    if flashtime > 0.4 and flashtime < 0.6 then
        for i = 1, #beacon1 do
            SetShapeEmissiveScale(beacon1[i], 0)
        end
        blink1 = true
    else
        blink1 = false
    end

    if flashtime > 1.4 and flashtime < 1.6 then
        for i = 1, #beacon2 do
            SetShapeEmissiveScale(beacon2[i], 0)
        end
        blink2 = true
    else
        blink2 = false
    end
end

function draw()
    if inveh and not GetBool("hud.disable") then
        UiPush()
            UiAlign("center middle")
            UiTranslate(UiCenter()-900, UiMiddle()+496)

            UiPush()
                UiColor(0,0,0,0.5)
                UiTranslate(127, 0)
                UiImageBox("ui/common/box-solid-6.png", 320, 50, 6, 6)
            UiPop()

            UiFont("regular.ttf", 30)
            UiText("I")
            UiTranslate(140, 2)
            UiColorFilter(1, 1, 1, 0.7)
            UiText("Change lightbar mode")
        UiPop()
    end
end
