Рандомное воспроизведение

Arclite

Новичок
Автор темы
14
0
Версия MoonLoader
.026-beta
Здравствуйте, надо сделать чтобы 3 этих звука чередовались между собой.
Например, при 1 выстреле играет 1, при втором - 2. И так далее.


Код:
script_name('Zamena kolokola to morozov family')
script_author('remake by egorka morozov')

require 'lib.moonloader'
local imgui = require 'imgui'
local encoding = require 'encoding'
local inicfg = require 'inicfg'
local sampev = require 'lib.samp.events'
encoding.default = 'CP1251'
u8 = encoding.UTF8

local mainini = inicfg.load({
    config =
    {
        kolokol = false,
        kol = 1,
        volume = 2,

    }
    }, 'settings')

if not doesFileExist('moonloader/config/Kolokol/settings.ini') then
    inicfg.save(mainini, 'Kolokol/settings.ini')
else
    inicfg.load(mainini, 'Kolokol/settings.ini')
end

local kolokol = imgui.ImBool(mainini.config.kolokol)
local menu = imgui.ImBool(false)
local kol = imgui.ImInt(mainini.config.kol)
local volume = imgui.ImFloat(mainini.config.volume)

function imgui.OnDrawFrame()
    if menu.v then
        local sw, sh = getScreenResolution()
        imgui.SetNextWindowPos(imgui.ImVec2(sw / 2, sh / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
        imgui.SetNextWindowSize(imgui.ImVec2(237, 450), imgui.Cond.FirstUseEver)
        imgui.Begin(u8'Колокольчик для Morozov Fam (Private)', menu, imgui.WindowFlags.NoResize + imgui.WindowFlags.NoCollapse)

        for i = 1, 10 do
            if imgui.RadioButton(i..u8' Колокол', kol, i) then
                kol.v = i
                mainini.config.kol = i
                inicfg.save(mainini, 'Kolokol/settings.ini')
            end
            imgui.SameLine(130)
            if imgui.Button(u8'Послушать##'..i,imgui.ImVec2(90,25)) then
                local audio = loadAudioStream('moonloader/config/Kolokol/'..i..'.mp3')
                setAudioStreamState(audio, 1)
                setAudioStreamVolume(audio, math.floor(volume.v))
            end
        end

        imgui.Spacing()
        imgui.PushItemWidth(207)
        if imgui.SliderFloat(u8'##volume', volume, 0, 100, u8'Громкость %1.0f') then
            mainini.config.volume = volume.v
            inicfg.save(mainini, 'Kolokol/settings.ini')
        end
        imgui.Spacing()

        if imgui.Checkbox(u8'##sound', kolokol) then
            mainini.config.kolokol = kolokol.v
            inicfg.save(mainini, 'Kolokol/settings.ini')
        end
        imgui.SameLine()
        if kolokol.v then
            imgui.TextColored(hex2rgb('7FFF00'), u8'Включен')
        else
            imgui.TextColored(hex2rgb('FF4500'), u8'Выключен')
        end

        imgui.End()
    end
end

function main()
    while not isSampAvailable() do wait(100) end

    sampRegisterChatCommand('kolm', function() menu.v = not menu.v end)
    FileCheck()
    while true do
        wait(0)
        imgui.Process = true

        if menu.v then
            imgui.ShowCursor = true
        else
            imgui.ShowCursor = false
        end

    end
end

function sampev.onSendGiveDamage()
    for i = 1, 10 do
        if kolokol.v and kol.v == i and doesFileExist('moonloader/config/Kolokol/'..i..'.mp3') then
            local audio = loadAudioStream('moonloader/config/Kolokol/'..i..'.mp3')
            setAudioStreamState(audio, 1)
            setAudioStreamVolume(audio, math.floor(volume.v))
        end
    end
end

function FileCheck()
    lua_thread.create(function()
        if not doesDirectoryExist('moonloader/config/Kolokol') then
            createDirectory('moonloader/config/Kolokol')
        end

        local mp3_download = {
            [1] = 'https://zvukipro.com/uploads/files/2020-12/1609059348_12193-hentai-a-cutted.mp3',
            [2] = '',
            [3] = '',
            [4] = '',
            [5] = '',
            [6] = '',
            [7] = '',
            [8] = '',
            [9] = 'https://zvukipro.com/uploads/files/2021-04/1618815463_probitie1.mp3',
            [10] = 'https://zvukipro.com/uploads/files/2021-04/1618815550_probitie-2.mp3'
        }

        for i = 1, 10 do
            downloadFile('moonloader/config/Kolokol/'..i..'.mp3', mp3_download[i])
            wait(100)
        end
    end)
end

function downloadFile(path, link)
    if not doesFileExist(path) then
        downloadUrlToFile(link, path, function(id, status, p1, p2)
        end)
    end
end

function hex2rgb(hex)
    hex = hex:gsub('#','')
    R = tonumber('0x'..hex:sub(1,2)) / 255
    G = tonumber('0x'..hex:sub(3,4)) / 255
    B = tonumber('0x'..hex:sub(5,6)) / 255
    return imgui.ImVec4(R, G, B, 1)
end

function ImRGBA(r, g, b, a)
    return imgui.ImVec4(r/255, g/255, b/255, a/255);
end

function apply_custom_style()
    imgui.SwitchContext()
    local style = imgui.GetStyle()
    local colors = style.Colors
    local clr = imgui.Col
    local ImVec4 = imgui.ImVec4
    local ImVec2 = imgui.ImVec2
    style.WindowTitleAlign = imgui.ImVec2(0.5, 0.50)

    style.WindowPadding = ImVec2(15, 15)
    style.WindowRounding = 6.0
    style.FramePadding = ImVec2(7, 5)
    style.FrameRounding = 4.0
    style.ItemSpacing = ImVec2(12, 8)
    style.ItemInnerSpacing = ImVec2(5, 6)
    style.IndentSpacing = 25.0
    style.ScrollbarSize = 15.0
    style.ScrollbarRounding = 9.0
    style.GrabMinSize = 5.0
    style.GrabRounding = 3.0

    colors[clr.Text] = ImRGBA(255, 255, 255, 255);
    colors[clr.TextDisabled] = ImVec4(0.24, 0.23, 0.29, 1.00)
    colors[clr.WindowBg] = ImRGBA(29, 39, 43, 255);
    colors[clr.ChildWindowBg] = ImVec4(0.07, 0.07, 0.09, 1.00)
    colors[clr.PopupBg] = ImVec4(0.07, 0.07, 0.09, 1.00)
    colors[clr.Border] = ImVec4(0.80, 0.80, 0.83, 0.88)
    colors[clr.BorderShadow] = ImVec4(0.92, 0.91, 0.88, 0.00)
    colors[clr.FrameBg] = ImRGBA(50, 60, 65, 255);
    colors[clr.FrameBgHovered] = ImRGBA(31, 50, 70, 255);
    colors[clr.FrameBgActive] = ImRGBA(80, 140, 250, 255);
    colors[clr.TitleBg] = ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[clr.TitleBgCollapsed] = ImVec4(1.00, 0.98, 0.95, 0.75)
    colors[clr.TitleBgActive] = ImVec4(0.07, 0.07, 0.09, 1.00)
    colors[clr.MenuBarBg] = ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[clr.ScrollbarBg] = ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[clr.ScrollbarGrab] = ImVec4(0.80, 0.80, 0.83, 0.31)
    colors[clr.ScrollbarGrabHovered] = ImVec4(0.56, 0.56, 0.58, 1.00)
    colors[clr.ScrollbarGrabActive] = ImVec4(0.06, 0.05, 0.07, 1.00)
    colors[clr.ComboBg] = ImVec4(0.19, 0.18, 0.21, 1.00)
    colors[clr.CheckMark] = ImRGBA(80, 140, 250, 255);
    colors[clr.SliderGrab] = ImVec4(0.80, 0.80, 0.83, 0.31)
    colors[clr.SliderGrabActive] = ImVec4(0.06, 0.05, 0.07, 1.00)
    colors[clr.Button] = ImRGBA(51, 65, 74, 255);
    colors[clr.ButtonHovered] = ImRGBA(80, 140, 250, 255);
    colors[clr.ButtonActive] = ImRGBA(15, 135, 250, 155);
    colors[clr.Header] = ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[clr.HeaderHovered] = ImVec4(0.56, 0.56, 0.58, 1.00)
    colors[clr.HeaderActive] = ImVec4(0.06, 0.05, 0.07, 1.00)
    colors[clr.ResizeGrip] = ImVec4(0.00, 0.00, 0.00, 0.00)
    colors[clr.ResizeGripHovered] = ImVec4(0.56, 0.56, 0.58, 1.00)
    colors[clr.ResizeGripActive] = ImVec4(0.06, 0.05, 0.07, 1.00)
    colors[clr.CloseButton] = ImVec4(0.40, 0.39, 0.38, 0.16)
    colors[clr.CloseButtonHovered] = ImVec4(0.40, 0.39, 0.38, 0.39)
    colors[clr.CloseButtonActive] = ImVec4(0.40, 0.39, 0.38, 1.00)
    colors[clr.PlotLines] = ImVec4(0.40, 0.39, 0.38, 0.63)
    colors[clr.PlotLinesHovered] = ImVec4(0.25, 1.00, 0.00, 1.00)
    colors[clr.PlotHistogram] = ImVec4(0.40, 0.39, 0.38, 0.63)
    colors[clr.PlotHistogramHovered] = ImVec4(0.25, 1.00, 0.00, 1.00)
    colors[clr.TextSelectedBg] = ImRGBA(80, 140, 250, 255);
    colors[clr.ModalWindowDarkening] = ImVec4(1.00, 0.98, 0.95, 0.73)
end
apply_custom_style()
 

wojciech?

Известный
Проверенный
352
236
перемешает исходный массив в случайном порядке и вернет новый
источник https://stackoverflow.com/questions/35572435/how-do-you-do-the-fisher-yates-shuffle-in-lua
Lua:
function Shuffle(t)
  local s = {}
  for i = 1, #t do s[i] = t[i] end
  for i = #t, 2, -1 do
    local j = math.random(i)
    s[i], s[j] = s[j], s[i]
  end
  return s
end

* в начале кода стоит установить math.randomseed(os.time())