## A Minecraft soundtrack for your daily grind

The problem with the real world is it's not enough like Minecraft. The fix is simple: pipe haunting and beautiful C418 tracks at various random times throughout the day, in a Minecraft-y fashion. To keep you on your toes, add in some rare-but-scary cave sounds. And that's exactly what my Grindcraft python script is for.

If you're a fan of the game (a fan is someone who has played it) then you'll know that the music in Minecraft is "special". The game proceeds largely in silence... you'll be deep in the process of constructing a summer house out of glass, or building a railroad to your lava pit when it starts... ever-so-slowly drifting in, and filling your body with smiles. Then it washes away without you even noticing.

I decided I needed this for my daily grind, so I whipped up a dodgy python script that plays random ogg files out of the Minecraft resources directory. But first I needed to figure out which songs Minecraft plays, and at what times.

## The algorithm

According to the Minecraft wiki on music "In-game music is cued by the time of day, with a random track being played at sunrise, sunset, noon and midnight". Which I thought sounded reasonable - at first. I asked on the IRC channel and received similar stories: Songs play in the early morning, evening and late at night. Not every morning though, and sometimes not at night. And only the twinkly song plays in the morning. And, and, and... Something seemed fishy, so I decided further investigation was necessary.

Minecraft is a standard Java program, and the Mojang team were nice enough not to do any crazy encrypting/obfuscating of the final product: so it can be relatively easily decompiled (and even re-compiled!). I consulted the source to learn the ways of the mystical music-playing algorithm. To my surprise, it goes a little something exactly like so:

When initialised, it does this complex algorithm to determine the optimum time to play the first morning song:

``` ```
``````rand = new Random();
ticksBeforeMusic = rand.nextInt(12000);``````

There are 24000 ticks in a Minecraft day, so this just picks a random time between now, and half-a-day away. That's it. I guessed the algorithm for successive tracks would be far more complex than this gimmee. Following the code along I found it calls this function once every game tick:

``` public void playRandomMusicIfReady() { if(ticksBeforeMusic > 0) { ticksBeforeMusic--; return; } SoundPoolEntry soundpoolentry = soundPoolMusic.getRandomSound(); ticksBeforeMusic = rand.nextInt(12000) + 12000; sndSystem.backgroundMusic("BgMusic", soundpoolentry.soundUrl, soundpoolentry.soundName, false); sndSystem.play("BgMusic"); } ```

When the counter is 0 it calls `getRandomSound` which simply returns a random song from the "music" or "newmusic" folders. It then resets the counter to another random number between half a day, and a full day away! It has absolutely no concept of morning, eventing, night, twinkly song, quiet song... it just plays a random track, at a random time! Damn you human brain for adding meaning where there is none!

Anyway, that's all I wanted to say. If you happen to have Minecraft, python, and pygame installed, and work with your volume up, but in silence: then this script is for you!