More refactoring

This commit is contained in:
Luke Hubmayer-Werner 2020-06-06 16:57:25 +09:30
parent 5d7d2606d5
commit 99aeedd892
1 changed files with 60 additions and 61 deletions

View File

@ -26,18 +26,18 @@ end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
options_path = 'Settings/Audio' options_path = "Settings/Audio"
options = { options = {
useIncludedTracks = { useIncludedTracks = {
name = "Use Included Tracks", name = "Use Included Tracks",
type = 'bool', type = "bool",
value = true, value = true,
desc = 'Use the tracks included with Zero-K', desc = "Use the tracks included with Zero-K",
noHotkey = true, noHotkey = true,
}, },
pausemusic = { pausemusic = {
name = 'Pause Music', name = "Pause Music",
type = 'bool', type = "bool",
value = false, value = false,
desc = "Music pauses with game", desc = "Music pauses with game",
noHotkey = true, noHotkey = true,
@ -50,11 +50,11 @@ local windows = {}
local warThreshold = 5000 local warThreshold = 5000
local peaceThreshold = 1000 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 LOOP_BUFFER = 0.015 -- if looping track is this close to the end, go ahead and loop
local UPDATE_PERIOD = 1 local UPDATE_PERIOD = 1
local musicType = 'peace' local musicType = "peace"
local warPointsIter = 1 -- Position in circular buffer. 1-indexed because L[ew]a 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 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 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 warPointsRollover = 4000000000 -- Roll back to zero after this many have accumulated
local timeframetimer = 0 local timeframetimer = 0
local timeframetimer_short = 0 local timeframetimer_short = 0
local loopTrack = '' local loopTrack = ""
local previousTrack = '' local previousTrack = ""
local previousTrackType = '' local previousTrackType = ""
local newTrackWait = 1000 local newTrackWait = 1000
local numVisibleEnemy = 0 local numVisibleEnemy = 0
local fadeVol local fadeVol
@ -74,8 +74,7 @@ local haltMusic = false
local looping = false local looping = false
local paused = false local paused = false
local lastTrackTime = -1 local lastTrackTime = -1
local tracks
local warTracks, peaceTracks, briefingTracks, victoryTracks, defeatTracks
local firstTime = false local firstTime = false
local wasPaused = false local wasPaused = false
@ -101,7 +100,7 @@ local function StartLoopingTrack(trackInit, trackLoop)
end end
haltMusic = true haltMusic = true
Spring.StopSoundStream() Spring.StopSoundStream()
musicType = 'custom' musicType = "custom"
curTrack = trackInit curTrack = trackInit
loopTrack = trackLoop loopTrack = trackLoop
@ -110,8 +109,8 @@ local function StartLoopingTrack(trackInit, trackLoop)
end end
local function StartTrack(track) local function StartTrack(track)
if not peaceTracks then if not tracks.peace then
Spring.Echo("Missing peaceTracks file, no music started") Spring.Echo("Missing tracks.peace file, no music started")
return return
end end
@ -120,30 +119,26 @@ local function StartTrack(track)
Spring.StopSoundStream() Spring.StopSoundStream()
local newTrack = previousTrack local newTrack = previousTrack
if musicType == 'custom' then if musicType == "custom" then
previousTrackType = "peace" previousTrackType = "peace"
musicType = "peace" musicType = "peace"
end end
if (not gameStarted) then
musicType = "briefing"
end
if track then if track then
newTrack = track -- play specified track newTrack = track -- play specified track
musicType = 'custom' musicType = "custom"
else else
local tries = 0 local numTypeTracks = #tracks[musicType]
repeat if (numTypeTracks == 0) then return end
if (not gameStarted) then for tries=1,10 do
if (#briefingTracks == 0) then return end newTrack = tracks[musicType][math.random(1, numTypeTracks)]
newTrack = briefingTracks[math.random(1, #briefingTracks)] if newTrack ~= previousTrack then break end
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 end
tries = tries + 1
until newTrack ~= previousTrack or tries >= 10
end end
-- for key, val in pairs(oggInfo) do -- for key, val in pairs(oggInfo) do
-- Spring.Echo(key, val) -- Spring.Echo(key, val)
-- end -- end
@ -188,6 +183,24 @@ local function SetPeaceThreshold(num)
end end
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) function widget:Update(dt)
if gameOver then if gameOver then
return return
@ -197,21 +210,7 @@ function widget:Update(dt)
initialized=true initialized=true
-- these are here to give epicmenu time to set the values properly -- these are here to give epicmenu time to set the values properly
-- (else it's always default at startup) -- (else it's always default at startup)
if VFS.FileExists(PLAYLIST_FILE, VFS.RAW_FIRST) then InitializeTracks()
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)
end end
timeframetimer_short = timeframetimer_short + dt timeframetimer_short = timeframetimer_short + dt
@ -225,7 +224,7 @@ function widget:Update(dt)
looping = 1 looping = 1
elseif playedTime >= totalTime - LOOP_BUFFER then elseif playedTime >= totalTime - LOOP_BUFFER then
Spring.StopSoundStream() Spring.StopSoundStream()
Spring.PlaySoundStream(loopTrack,WG.music_volume or 0.5) Spring.PlaySoundStream(loopTrack, WG.music_volume or 0.5)
end end
end end
timeframetimer_short = 0 timeframetimer_short = 0
@ -261,11 +260,11 @@ function widget:Update(dt)
warPointsHostile[iNext] = warPointsHostile[warPointsIter] % warPointsRollover warPointsHostile[iNext] = warPointsHostile[warPointsIter] % warPointsRollover
warPointsIter = iNext warPointsIter = iNext
if (musicType == 'war' or musicType == 'peace') then if (musicType == "war" or musicType == "peace") then
if (totalKilled >= warThreshold) then if (totalKilled >= warThreshold) then
musicType = 'war' musicType = "war"
elseif (totalKilled <= peaceThreshold) then elseif (totalKilled <= peaceThreshold) then
musicType = 'peace' musicType = "peace"
end end
end end
@ -295,7 +294,7 @@ function widget:Update(dt)
--Spring.SetSoundStreamVolume( playedTime/5) --Spring.SetSoundStreamVolume( playedTime/5)
--end --end
--Spring.Echo(previousTrackType, musicType) --Spring.Echo(previousTrackType, musicType)
if ( previousTrackType == "peace" and musicType == 'war' ) if ( previousTrackType == "peace" and musicType == "war" )
or (playedTime >= totalTime) -- both zero means track stopped or (playedTime >= totalTime) -- both zero means track stopped
and not(haltMusic or looping) then and not(haltMusic or looping) then
previousTrackType = musicType previousTrackType = musicType
@ -327,7 +326,7 @@ end
-- Safety of a heisenbug -- Safety of a heisenbug
function widget:GameFrame() function widget:GameFrame()
widget:GameStart() widget:GameStart()
widgetHandler:RemoveCallIn('GameFrame') widgetHandler:RemoveCallIn("GameFrame")
end end
function widget:UnitDamaged(unitID, unitDefID, unitTeam, damage, paralyzer) function widget:UnitDamaged(unitID, unitDefID, unitTeam, damage, paralyzer)
@ -384,12 +383,12 @@ end
local function PlayGameOverMusic(gameWon) local function PlayGameOverMusic(gameWon)
local track local track
if gameWon then if gameWon then
if #victoryTracks <= 0 then return end if #tracks.victory <= 0 then return end
track = victoryTracks[math.random(1, #victoryTracks)] track = tracks.victory[math.random(1, #tracks.victory)]
musicType = "victory" musicType = "victory"
else else
if #defeatTracks <= 0 then return end if #tracks.defeat <= 0 then return end
track = defeatTracks[math.random(1, #defeatTracks)] track = tracks.defeat[math.random(1, #tracks.defeat)]
musicType = "defeat" musicType = "defeat"
end end
looping = false looping = false
@ -415,7 +414,7 @@ function widget:Initialize()
-- Spring.Echo(math.random(), math.random()) -- Spring.Echo(math.random(), math.random())
-- Spring.Echo(os.clock()) -- Spring.Echo(os.clock())
-- for TrackName,TrackDef in pairs(peaceTracks) do -- for TrackName,TrackDef in pairs(tracks.peace) do
-- Spring.Echo("Track: " .. TrackDef) -- Spring.Echo("Track: " .. TrackDef)
-- end -- end
--math.randomseed(os.clock()* 101.01)--lurker wants you to burn in hell rgn --math.randomseed(os.clock()* 101.01)--lurker wants you to burn in hell rgn