Duke4.net Forums: A code for music randomizer? - Duke4.net Forums

Jump to content

Hide message Show message
Welcome to the Duke4.net Forums!

Register an account now to get access to all board features. After you've registered and logged in, you'll be able to create topics, post replies, send and receive private messages, disable the viewing of ads and more!

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

A code for music randomizer?

User is offline   Anvil 

  • 20

#1

Can anyone provide a code that randomly chooses one of the tracks for a map, instead of the player doing it manually? (shift+f5)

This post has been edited by Anvil: 28 November 2016 - 02:12 AM

1

User is online   Trooper Dan 

  • Duke Plus Developer
  • 2,141

#2

shift F5 sounds pretty damned easy, actually. I do have some code for a music randomizer that I wrote for Duke Forces. I need to sleep now but maybe I'll dig it out for you in the next few days.
0

User is online   Hendricks266 

  • EDuke32 Senior Developer
  • 5,452

  #3

This is special cased for the original game.

gamevar temp 0 0
gamevar temp2 0 0


// Advances the random number generator seed a bit since EDuke32 currently never calls srand().

defstate advance_rand
    getticks temp
    andvar temp 255
    whilevarn temp 0
    {
        displayrand temp2
        subvar temp 1
    }
ends


// Randomizes the music choice on level start.

gamevar music_level 0 0
gamevar music_volume 0 0

defstate apply_music
    setvarvar temp VOLUME
    setuserdef[].volume_number music_volume
    starttrackvar music_level
    setuserdef[].volume_number temp
ends

appendevent EVENT_ENTERLEVEL
    state advance_rand

    displayrandvar music_volume 3
    ifvare music_volume 0 // episode 1
        setvarvar temp 6
    else
        setvarvar temp 10
    displayrandvarvar music_level temp

    state apply_music
endevent


// HACK. Remove this section when EDuke32 supports saving starttrack music IDs.

appendevent EVENT_LOADGAME
    state apply_music
endevent


// HACK. Remove this section when EDuke32 supports saving starttracked music files' positions.

gamevar music_position 0 0

appendevent EVENT_SAVEGAME
    getmusicposition music_position
endevent

appendevent EVENT_LOADGAME
    setmusicposition music_position
endevent


Unfortunately, we need a new music system since this code can only do so much. Loading savegames will cause a slight music stutter, and get/setmusicposition doesn't work at all on MIDIs.
2

User is online   Trooper Dan 

  • Duke Plus Developer
  • 2,141

#4

That's very good, but it has one shortcoming imo. The main reason people want a music randomizer is when starting a user map. A user map sets VOLUME to 0, so that code would limit the random music selections for user maps to the episode 1 tracks. I don't know if that can be solved without adding all of the missing tracks to the episode 1 music definition (which is what I do in my version).
0

User is online   Hendricks266 

  • EDuke32 Senior Developer
  • 5,452

  #5

My code randomizes the episode as well as the level. Note the displayrandvar music_volume 3, followed by a check for episode 1 (7 levels instead of 11), then the displayrandvarvar for the level.
0

User is offline   Anvil 

  • 20

#6

View PostHendricks266, on 28 November 2016 - 12:30 PM, said:

Loading savegames will cause a slight music stutter

I tested it with OGG files: loading savegames doesn't cause a slight music stutter, it causes the track to continue from the point it was when saving instead of continuing regularly.
1

User is online   Hendricks266 

  • EDuke32 Senior Developer
  • 5,452

  #7

Good point. Total Meltdown should really have a new music system, which will require some work on the savegame format. Then this should work for real.
0

User is offline   Anvil 

  • 20

#8

I also tested Duke Forces randomizer: the track continues almost regularly when loading a game. (there is a slight stutter.)
0

User is offline   Anvil 

  • 20

#9

The code works well for user maps without the 2 hacks!

This post has been edited by Anvil: 03 December 2016 - 11:46 AM

1

User is online   Sanek 

  • 334

#10

Is it possible to go backwards when choosing the tracks?
0

User is offline   Anvil 

  • 20

#11

Is it possible to randomize the tracks also during the level, after each track ends?
0

User is online   Hendricks266 

  • EDuke32 Senior Developer
  • 5,452

  #12

There is no good way to know when tracks are finished, but thank you for asking so I can think about this when I get around to a better music system.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic


All copyrights and trademarks are property of their respective owners. Yes, our forum uses cookies. © 2017 Voidpoint, LLC

Enter your sign in name and password


Sign in options