--------------------------------------------------------------------------------
CyclicSounds = {};

--------------------------------------------------------------------------------
function ContinueCyclicSound(soundName, repeatInterval)

    if (CyclicSounds[soundName] == 1)
    then
        PlaySound(soundName);
    
        local callback = "ContinueCyclicSound(\"".. soundName .. "\", " .. repeatInterval .. ");"
        TimerDoString(callback, repeatInterval, play_field:GetTimer())
        
    end

end

--------------------------------------------------------------------------------
function PlayCyclicSound(soundName, repeatInterval)

    CyclicSounds[soundName] = 1;
    ContinueCyclicSound(soundName, repeatInterval);
    
end

--------------------------------------------------------------------------------
function StopCyclicSound(soundName)

    CyclicSounds[soundName] = 0;
    StopSound(soundName);

end

function MakeSoundEntity(soundName)

    local result = CreateSoundEntity(soundName);
    result:AttachTo(play_field);
    return result;

end

--------------------------------------------------------------------------------
currentMusicTrack = nil;

--------------------------------------------------------------------------------
function PlayAmbientMusic(musicTrack, fade)

    if (currentMusicTrack ~= musicTrack)
    then
        if (currentMusicTrack)
        then
            StopMusic(currentMusicTrack, fade or false);
        end
        
        currentMusicTrack = musicTrack;
        
        if (currentMusicTrack)
        then
            PlayMusic(currentMusicTrack, fade or false);
        end
    end

end

--------------------------------------------------------------------------------
function StopAmbientMusic(fade)
    PlayAmbientMusic(nil, fade);
end

--------------------------------------------------------------------------------
function PlayRandomAmbientMusic()
    local musicTrack = nil;
    
    local musicMap = {}
    musicMap[1] = "music3"
    musicMap[2] = "music4"
    musicMap[3] = "music5"
    musicMap[4] = "music6"
    
    if not IsSceneWithoutMusic(game_state.scene_path) then
        repeat
            musicTrack = musicMap[ math.random(table.getn(musicMap)) ]
        until (musicTrack ~= currentMusicTrack)
    end
    
    PlayAmbientMusic(musicTrack, true);
end

function IsSceneWithoutMusic(path)
    local scenes = {};
    
    scenes[1]  = 'data/Scenes/01_Dupens_House_Entrance_Main.lua';
    scenes[2]  = 'data/Scenes/01_Dupens_House_Entrance_Door.lua';
    scenes[3]  = 'data/Scenes/04_Bridge_Main.lua';
    scenes[4]  = 'data/Scenes/04_Bridge_Bench.lua';
    scenes[5]  = 'data/Scenes/03_Dupens_House_Exit_Carriage.lua';
    scenes[6]  = 'data/Scenes/03_Dupens_House_Exit_Main.lua';
    
    scenes[7]  = 'data/Scenes/13_Crimeplace_Street.lua';
    scenes[8]  = 'data/Scenes/19_Mill_Outside_Main.lua';
    scenes[9]  = 'data/Scenes/22_RiverPort_Main.lua';
    scenes[10] = 'data/Scenes/22_RiverPort_Deck1.lua';
    scenes[11] = 'data/Scenes/22_RiverPort_Deck2.lua';
    
    for i, v in ipairs(scenes) do
        if v == path then
            return true
        end
    end
    
    return false;
end

--------------------------------------------------------------------------------