wesnoth/wesnoth — pull request #265 by Zappaman (2014-08-03 21:48:52+00:00)
Title: add music shuffle, second attempt at https://gna.org/bugs/?19653 

This pull request is my second attempt at https://gna.org/bugs/?19653 , taking into consideration all the previous discussions from https://github.com/wesnoth/wesnoth/pull/255 and suggestions given by Soliton and zookeeper.

I decided to start a new pull request, and close the old one ( https://github.com/wesnoth/wesnoth/pull/255 ) for two reasons: the main one is that the other pull request got really messy and secondly, I changed the implementation altogether in my last commit there, so I figured a new pull request would make sense.

Each [music] WML tag gets the option to include the [shuffle] key or not. This key refers to each music track individually(not including the shuffle key defaults to shuffle=yes, because that was the previous behaviour). The steps in choosing a new track are the following:

- In the beginning an external index, current_track_index, referring to the position of the current track in the playlist, is initialized to 0. ( Sadly, I couldn't see a way to avoid this being external to sound::music_track; adding a field to the class meant that somewhere, probably in play_music_config, that field had to be set to a counter which in turn, had to have global scope, and fulfill an identical role ). If current_track_index exceeds the size of the playlist, it resets to 0.
- After the playlist is made, every time choose_track is called, we look at the element at current_track_index in the current playlist and:
- If it has "shuffle" on it, then we randomly choose a track, just like we would normally do, say track at position x in the playlist, return it and play it; the track to be inspected in the next iteration shall be the x + 1'th (current_track_index = x + 1)
- if it doesn't have "shuffle" on it, then we simply return it, play it, and increment the current_track_index.

This works regardless of whether the [music] tags are enclosed in an [event] tag or not.

Again, I'm open to any suggestions, any feedback is appreciated!