• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[Mod] Sound System

Very weird things are going on with this system or could just be me 🤔
  • As a test, I used the positions that were located in a straight horizontal line for example 1000/1000/7 - 1010/1000/7 : works perfectly the music plays!
  • When putting a new song right underneath the first config, this time I did a vertical line, shouldn't be any different right, well didn't work?
  • I decided to just use the vertical positions as a test as a single config, but nope didn't work.

So this setting would work:
Lua:
SOUNDS = {
{fromPos = {x=1000, y=1000, z=7}, toPos = {x=1010, y=1000, z=7}, sound = "FF VII - Main.ogg"},


So this setting would not work:
Code:
SOUNDS = {
{fromPos = {x=1000, y=1000, z=7}, toPos = {x=1010, y=1000, z=7}, sound = "FF VII - Main.ogg"},
{fromPos = {x=1000, y=1000, z=7}, toPos = {x=1000, y=1010, z=7}, sound = "FF VII - Other Song.ogg"},

This setting would also not work: :rolleyes:
Code:
SOUNDS = {
{fromPos = {x=1000, y=1000, z=7}, toPos = {x=1000, y=1010, z=7}, sound = "FF VII - Main.ogg"},

Setting up a multi-floor config would also not work, like the original script provided:
Code:
SOUNDS = {
    -- Rook Cave
    {fromPos = {x=879, y=803, z=8}, toPos = {x=1079, y=963, z=15}, sound = "FF VII - Main.ogg"},


Feels Justin Timberlake GIF

----
No , after the last declaration in original script or im wrong?
 
I tried to clean it up a bit first and it worked for me (Otclient v8):
Lua:
SoundsConfig = {
    soundChannel = SoundChannels.Music,
    checkInterval = 500,
    folder = 'music/',
    noSoundMessage = 'No sound file for this area.',
}

SoundsConfig.sounds = {
    -- Boss
    { fromPos = { x = 32434, y = 32223, z = 7 }, toPos = { x = 32439, y= 32228, z = 7 }, priority = 1, name = "Aproaching boss", file = "boss1.ogg" },
    { fromPos = { x = 32440, y = 32223, z = 7 }, toPos = { x = 32444, y= 32227, z = 7 }, priority = 2, name = "Fleeing boss", file = "boss1.ogg" },

    -- Bridge
    { fromPos = { x = 32429, y = 32224, z = 7 }, toPos = { x = 32433, y= 32227, z = 7 }, priority = 1, name = "Thais Bridge", file = "dragons.ogg" },

}

-- Sound
local rcSoundChannel
local playingSound

-- Design
soundWindow = nil
soundButton = nil

function toggle()
    if soundButton:isOn() then
        soundWindow:close()
        soundButton:setOn(false)
    else
        soundWindow:open()
        soundButton:setOn(true)
    end
end

function onMiniWindowClose()
    soundButton:setOn(false)
end

function init()
    connect(g_game, {
        onGameStart = onGameStart,
        onGameEnd = onGameEnd
    })

    rcSoundChannel = g_sounds.getChannel(SoundsConfig.soundChannel)
    soundButton = modules.client_topmenu.addRightGameToggleButton('soundButton', tr('Sound Info') .. '', 'images/audio', toggle)
    soundButton:setOn(true)

    soundWindow = g_ui.loadUI('rcsound', modules.game_interface.getRightPanel())
    soundWindow:disableResize()
    soundWindow:setup()

    if (g_game.isOnline()) then
        onGameStart()
    end
end

function terminate()
    disconnect(g_game, {
        onGameStart = onGameStart,
        onGameEnd = onGameEnd
    })
    onGameEnd()
    soundWindow:destroy()
    soundButton:destroy()
end

function onGameStart()
    g_sounds.enableAudio()
    rcSoundChannel:setEnabled(true)
    stopSound()
    toggleSoundEvent = addEvent(toggleSound, SoundsConfig.checkInterval)
end

function onGameEnd()
    stopSound()
    removeEvent(toggleSoundEvent)
end

function isInPos(pos, fromPos, toPos)
    local fromZ = math.min(fromPos.z, toPos.z)
    local toZ = math.max(toPos.z, toPos.z)
    return (pos.x >= fromPos.x and pos.x <= toPos.x) and (pos.y >= fromPos.y and pos.y <= toPos.y) and (pos.z >= fromZ and pos.z <= toZ)
end

function toggleSound()
    toggleSoundEvent = scheduleEvent(toggleSound, SoundsConfig.checkInterval)
    local player = g_game.getLocalPlayer()
    if not player then
        return
    end

    local pos = player:getPosition()
    local toPlay = nil

    for _, sound in pairs(SoundsConfig.sounds) do
        if isInPos(pos, sound.fromPos, sound.toPos) then
            local priority = sound.priority or 0
            if not toPlay or priority > toPlay.priority then
                toPlay = sound
                toPlay.priority = priority
            end
        end
    end

    if not toPlay then
        if playingSound then
            g_logger.info("RC Sounds: Nothing to play. Player left sound area.")
            stopSound()
        end

        return
    end

    local playingFile = playingSound and playingSound.file or ''
    if playingFile == toPlay.file then
        setLabel(getSoundTitle(toPlay))
        return
    end

    g_logger.info("RC Sounds: New sound area detected:")
    g_logger.info("  Position: {x=" .. pos.x .. ", y=" .. pos.y .. ", z=" .. pos.z .. "}")
    g_logger.info("  Music: " .. toPlay.file)
    stopSound()
    playSound(toPlay)
    playingSound = toPlay
end

function playSound(sound)
    local path = SoundsConfig.folder .. sound.file
    rcSoundChannel:enqueue(path, 0)
    setLabel(getSoundTitle(sound))
end

function getSoundTitle(sound)
    if sound.name then
        return sound.name
    end

    local name, _ext = unpack(string.explode(sound.file, ".ogg"))
    return name
end

function stopSound()
    setLabel(SoundsConfig.noSoundMessage)
    rcSoundChannel:stop()
    playingSound = nil
end

function setLabel(str)
    soundWindow:recursiveGetChildById('currentSound'):getChildById('value'):setText(str)
end
 
Back
Top