Improved shuffle (#1412)

pull/280/head
Harry 7 years ago committed by scrawl
parent 1578fcbec7
commit 86ae2ae395

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

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

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

Loading…
Cancel
Save