From 99aeedd8928078479536d5e7fce0b63035d7989e Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Sat, 6 Jun 2020 16:57:25 +0930 Subject: [PATCH] More refactoring --- snd_music.lua | 121 +++++++++++++++++++++++++------------------------- 1 file changed, 60 insertions(+), 61 deletions(-) diff --git a/snd_music.lua b/snd_music.lua index 557b00d..41dfc5c 100644 --- a/snd_music.lua +++ b/snd_music.lua @@ -26,18 +26,18 @@ end -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -options_path = 'Settings/Audio' +options_path = "Settings/Audio" options = { useIncludedTracks = { name = "Use Included Tracks", - type = 'bool', + type = "bool", value = true, - desc = 'Use the tracks included with Zero-K', + desc = "Use the tracks included with Zero-K", noHotkey = true, }, pausemusic = { - name = 'Pause Music', - type = 'bool', + name = "Pause Music", + type = "bool", value = false, desc = "Music pauses with game", noHotkey = true, @@ -50,11 +50,11 @@ local windows = {} local warThreshold = 5000 local peaceThreshold = 1000 -local PLAYLIST_FILE = 'sounds/music/playlist.lua' +local PLAYLIST_FILE = "sounds/music/playlist.lua" local LOOP_BUFFER = 0.015 -- if looping track is this close to the end, go ahead and loop local UPDATE_PERIOD = 1 -local musicType = 'peace' +local musicType = "peace" local warPointsIter = 1 -- Position in circular buffer. 1-indexed because L[ew]a local warPointsSize = 30 -- Size of circular buffer. Sampling is currently hardcoded but might change later. local warPointsFriendly = {} -- keeps track of the number of doods killed in each time frame @@ -62,9 +62,9 @@ local warPointsHostile = {} local warPointsRollover = 4000000000 -- Roll back to zero after this many have accumulated local timeframetimer = 0 local timeframetimer_short = 0 -local loopTrack = '' -local previousTrack = '' -local previousTrackType = '' +local loopTrack = "" +local previousTrack = "" +local previousTrackType = "" local newTrackWait = 1000 local numVisibleEnemy = 0 local fadeVol @@ -74,8 +74,7 @@ local haltMusic = false local looping = false local paused = false local lastTrackTime = -1 - -local warTracks, peaceTracks, briefingTracks, victoryTracks, defeatTracks +local tracks local firstTime = false local wasPaused = false @@ -101,7 +100,7 @@ local function StartLoopingTrack(trackInit, trackLoop) end haltMusic = true Spring.StopSoundStream() - musicType = 'custom' + musicType = "custom" curTrack = trackInit loopTrack = trackLoop @@ -110,8 +109,8 @@ local function StartLoopingTrack(trackInit, trackLoop) end local function StartTrack(track) - if not peaceTracks then - Spring.Echo("Missing peaceTracks file, no music started") + if not tracks.peace then + Spring.Echo("Missing tracks.peace file, no music started") return end @@ -120,30 +119,26 @@ local function StartTrack(track) Spring.StopSoundStream() local newTrack = previousTrack - if musicType == 'custom' then + if musicType == "custom" then previousTrackType = "peace" musicType = "peace" end - if track then - newTrack = track -- play specified track - musicType = 'custom' - else - local tries = 0 - repeat - if (not gameStarted) then - if (#briefingTracks == 0) then return end - newTrack = briefingTracks[math.random(1, #briefingTracks)] - musicType = "briefing" - elseif musicType == 'peace' then - if (#peaceTracks == 0) then return end - newTrack = peaceTracks[math.random(1, #peaceTracks)] - elseif musicType == 'war' then - if (#warTracks == 0) then return end - newTrack = warTracks[math.random(1, #warTracks)] - end - tries = tries + 1 - until newTrack ~= previousTrack or tries >= 10 + if (not gameStarted) then + musicType = "briefing" end + + if track then + newTrack = track -- play specified track + musicType = "custom" + else + local numTypeTracks = #tracks[musicType] + if (numTypeTracks == 0) then return end + for tries=1,10 do + newTrack = tracks[musicType][math.random(1, numTypeTracks)] + if newTrack ~= previousTrack then break end + end + end + -- for key, val in pairs(oggInfo) do -- Spring.Echo(key, val) -- end @@ -188,6 +183,24 @@ local function SetPeaceThreshold(num) end end +local function InitializeTracks() + if VFS.FileExists(PLAYLIST_FILE, VFS.RAW_FIRST) then + local plTracks = VFS.Include(PLAYLIST_FILE, nil, VFS.RAW_FIRST) + tracks.war = plTracks.war + tracks.peace = plTracks.peace + tracks.briefing = plTracks.briefing + tracks.victory = plTracks.victory + tracks.defeat = plTracks.defeat + end + + local vfsMode = (options.useIncludedTracks.value and VFS.RAW_FIRST) or VFS.RAW + tracks.war = tracks.war or VFS.DirList("sounds/music/war/", "*.ogg", vfsMode) + tracks.peace = tracks.peace or VFS.DirList("sounds/music/peace/", "*.ogg", vfsMode) + tracks.briefing = tracks.briefing or VFS.DirList("sounds/music/briefing/", "*.ogg", vfsMode) + tracks.victory = tracks.victory or VFS.DirList("sounds/music/victory/", "*.ogg", vfsMode) + tracks.defeat = tracks.defeat or VFS.DirList("sounds/music/defeat/", "*.ogg", vfsMode) +end + function widget:Update(dt) if gameOver then return @@ -197,21 +210,7 @@ function widget:Update(dt) initialized=true -- these are here to give epicmenu time to set the values properly -- (else it's always default at startup) - if VFS.FileExists(PLAYLIST_FILE, VFS.RAW_FIRST) then - local tracks = VFS.Include(PLAYLIST_FILE, nil, VFS.RAW_FIRST) - warTracks = tracks.war - peaceTracks = tracks.peace - briefingTracks = tracks.briefing - victoryTracks = tracks.victory - defeatTracks = tracks.defeat - end - - local vfsMode = (options.useIncludedTracks.value and VFS.RAW_FIRST) or VFS.RAW - warTracks = warTracks or VFS.DirList('sounds/music/war/', '*.ogg', vfsMode) - peaceTracks = peaceTracks or VFS.DirList('sounds/music/peace/', '*.ogg', vfsMode) - briefingTracks = briefingTracks or VFS.DirList('sounds/music/briefing/', '*.ogg', vfsMode) - victoryTracks = victoryTracks or VFS.DirList('sounds/music/victory/', '*.ogg', vfsMode) - defeatTracks = defeatTracks or VFS.DirList('sounds/music/defeat/', '*.ogg', vfsMode) + InitializeTracks() end timeframetimer_short = timeframetimer_short + dt @@ -225,7 +224,7 @@ function widget:Update(dt) looping = 1 elseif playedTime >= totalTime - LOOP_BUFFER then Spring.StopSoundStream() - Spring.PlaySoundStream(loopTrack,WG.music_volume or 0.5) + Spring.PlaySoundStream(loopTrack, WG.music_volume or 0.5) end end timeframetimer_short = 0 @@ -261,11 +260,11 @@ function widget:Update(dt) warPointsHostile[iNext] = warPointsHostile[warPointsIter] % warPointsRollover warPointsIter = iNext - if (musicType == 'war' or musicType == 'peace') then + if (musicType == "war" or musicType == "peace") then if (totalKilled >= warThreshold) then - musicType = 'war' + musicType = "war" elseif (totalKilled <= peaceThreshold) then - musicType = 'peace' + musicType = "peace" end end @@ -295,7 +294,7 @@ function widget:Update(dt) --Spring.SetSoundStreamVolume( playedTime/5) --end --Spring.Echo(previousTrackType, musicType) - if ( previousTrackType == "peace" and musicType == 'war' ) + if ( previousTrackType == "peace" and musicType == "war" ) or (playedTime >= totalTime) -- both zero means track stopped and not(haltMusic or looping) then previousTrackType = musicType @@ -327,7 +326,7 @@ end -- Safety of a heisenbug function widget:GameFrame() widget:GameStart() - widgetHandler:RemoveCallIn('GameFrame') + widgetHandler:RemoveCallIn("GameFrame") end function widget:UnitDamaged(unitID, unitDefID, unitTeam, damage, paralyzer) @@ -384,12 +383,12 @@ end local function PlayGameOverMusic(gameWon) local track if gameWon then - if #victoryTracks <= 0 then return end - track = victoryTracks[math.random(1, #victoryTracks)] + if #tracks.victory <= 0 then return end + track = tracks.victory[math.random(1, #tracks.victory)] musicType = "victory" else - if #defeatTracks <= 0 then return end - track = defeatTracks[math.random(1, #defeatTracks)] + if #tracks.defeat <= 0 then return end + track = tracks.defeat[math.random(1, #tracks.defeat)] musicType = "defeat" end looping = false @@ -415,7 +414,7 @@ function widget:Initialize() -- Spring.Echo(math.random(), math.random()) -- Spring.Echo(os.clock()) - -- for TrackName,TrackDef in pairs(peaceTracks) do + -- for TrackName,TrackDef in pairs(tracks.peace) do -- Spring.Echo("Track: " .. TrackDef) -- end --math.randomseed(os.clock()* 101.01)--lurker wants you to burn in hell rgn