Improved shuffle (#1412)

0.6.1
Harry 7 years ago committed by scrawl
parent 1578fcbec7
commit 86ae2ae395

@ -60,6 +60,7 @@ Programmers
Gašper Sedej
gugus/gus
Hallfaer Tuilinn
Haoda Wang (h313)
hristoast
Internecine
Jacob Essex (Yacoby)

@ -3,6 +3,7 @@
#include <iostream>
#include <algorithm>
#include <map>
#include <numeric>
#include <osg/Matrixf>
@ -271,7 +272,6 @@ namespace MWSound
return sound;
}
// Gets the combined volume settings for the given sound type
float SoundManager::volumeFromType(PlayType type) const
{
@ -298,7 +298,6 @@ namespace MWSound
return volume;
}
void SoundManager::stopMusic()
{
if(mMusic)
@ -349,6 +348,7 @@ namespace MWSound
void SoundManager::startRandomTitle()
{
std::vector<std::string> filelist;
auto &tracklist = mMusicToPlay[mCurrentPlaylist];
if (mMusicFiles.find(mCurrentPlaylist) == mMusicFiles.end())
{
const std::map<std::string, VFS::File*>& index = mVFS->getIndex();
@ -367,7 +367,6 @@ namespace MWSound
}
mMusicFiles[mCurrentPlaylist] = filelist;
}
else
filelist = mMusicFiles[mCurrentPlaylist];
@ -375,15 +374,25 @@ namespace MWSound
if(filelist.empty())
return;
int i = Misc::Rng::rollDice(filelist.size());
// Do a Fisher-Yates shuffle
// Don't play the same music track twice in a row
if (filelist[i] == mLastPlayedMusic)
// Repopulate if playlist is empty
if(tracklist.empty())
{
i = (i+1) % filelist.size();
tracklist.resize(filelist.size());
std::iota(tracklist.begin(), tracklist.end(), 0);
}
advanceMusic(filelist[i]);
int i = Misc::Rng::rollDice(tracklist.size());
// Reshuffle if last played music is the same after a repopulation
if(filelist[tracklist[i]] == mLastPlayedMusic)
i = (i+1) % tracklist.size();
// Remove music from list after advancing music
advanceMusic(filelist[tracklist[i]]);
tracklist[i] = tracklist.back();
tracklist.pop_back();
}
bool SoundManager::isMusicPlaying()

@ -6,6 +6,7 @@
#include <utility>
#include <deque>
#include <map>
#include <unordered_map>
#include <components/settings/settings.hpp>
@ -49,6 +50,7 @@ namespace MWSound
// Caches available music tracks by <playlist name, (sound files) >
std::map<std::string, std::vector<std::string> > mMusicFiles;
std::unordered_map<std::string, std::vector<int>> mMusicToPlay; // A list with music files not yet played
std::string mLastPlayedMusic; // The music file that was last played
float mMasterVolume;

Loading…
Cancel
Save