diff --git a/snd_music.lua b/snd_music.lua index d94bb62..5fa246c 100644 --- a/snd_music.lua +++ b/snd_music.lua @@ -86,14 +86,12 @@ options = { local unitExceptions = include("Configs/snd_music_exception.lua") -local windows = {} - local spAreTeamsAllied = Spring.AreTeamsAllied local MOODS = {"peace", "war", "war2", "winning", "losing", "briefing", "victory", "defeat"} local moodPriorities = {peace=0, war=1, war2=2, winning=4, losing=4, briefing=10, victory=10, defeat=10, [""]=0} -- Determines which music moods will instantly interrupt others, and which will wait for playing track to finish -local moodDynamic = {peace=true, war=true, war2=true, winning=true, losing=true, briefing=false, victory=false, defeat=false, [""]=true} -- Determines which music moods will instantly interrupt others, and which will wait for playing track to finish +local moodDynamic = {peace=true, war=true, war2=true, winning=true, losing=true, briefing=false, victory=false, defeat=false, [""]=false} -- Determines which music moods will change dynamically local war2Threshold = 300000 local warThreshold = 30000 local peaceThreshold = 10000 @@ -126,18 +124,19 @@ local paused = false local lastTrackTime = -1 local tracks = {} -local firstTime = false +local firstTime = true local wasPaused = false local firstFade = true local initSeed = 0 local initialized = false -local gameStarted = Spring.GetGameFrame() > 0 +local gameStarted = Spring.GetGameFrame() > 30 --0 local gameOver = false local myTeam = Spring.GetMyTeamID() local isSpec = Spring.GetSpectatingState() or Spring.IsReplay() local defeat = false +local timesStartTrack = 0 -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- local function GetMusicType() @@ -149,34 +148,50 @@ local function StartLoopingTrack(trackInit, trackLoop) Spring.Log(widget:GetInfo().name, LOG.ERROR, "Missing one or both tracks for looping") end haltMusic = true - Spring.StopSoundStream() musicType = "custom" curTrack = trackInit loopTrack = trackLoop + Spring.StopSoundStream() Spring.PlaySoundStream(trackInit, WG.music_volume or 0.5) looping = 0.5 end +function CheckLoop() + local playedTime, totalTime = Spring.GetSoundStreamTime() + paused = (playedTime == lastTrackTime) + lastTrackTime = playedTime + if looping then + if looping == 0.5 then + looping = 1 + elseif playedTime >= totalTime - LOOP_BUFFER then + Spring.StopSoundStream() + Spring.PlaySoundStream(loopTrack, WG.music_volume or 0.5) + end + end +end + local function StartTrack(track) + timesStartTrack = timesStartTrack + 1 + Spring.Echo("StartTrack called with mood: " .. musicType .. " (" .. tostring(timesStartTrack) .. ")") if not tracks.peace then Spring.Echo("Missing tracks.peace file, no music started") return end - haltMusic = false - looping = false - Spring.StopSoundStream() - local newTrack = previousTrack if musicType == "custom" then prevMusicType = "peace" musicType = "peace" end if (not gameStarted) then + prevMusicType = "briefing" musicType = "briefing" end + haltMusic = false + looping = false + if track then newTrack = track -- play specified track musicType = "custom" @@ -192,6 +207,7 @@ local function StartTrack(track) firstFade = false previousTrack = newTrack curTrack = newTrack + Spring.StopSoundStream() Spring.PlaySoundStream(newTrack, WG.music_volume or 0.5) WG.music_start_volume = WG.music_volume @@ -243,24 +259,8 @@ function InitializeTracks() end end -function CheckLoop() - local playedTime, totalTime = Spring.GetSoundStreamTime() - paused = (playedTime == lastTrackTime) - lastTrackTime = playedTime - if looping then - if looping == 0.5 then - looping = 1 - elseif playedTime >= totalTime - LOOP_BUFFER then - Spring.StopSoundStream() - Spring.PlaySoundStream(loopTrack, WG.music_volume or 0.5) - end - end -end - function EvaluateMood() -- (Spring.GetGameRulesParam("recentNukeLaunch") == 1) -- Might need this for superweapon music later - newTrackWait = newTrackWait + 1 - numVisibleFriendly = 0 numVisibleEnemy = 0 local doods = Spring.GetVisibleUnits(-1, nil, true) @@ -300,46 +300,30 @@ function EvaluateMood() warPointsIter = iNext local warPoints = totalKilled + totalDmg - if moodDynamic[musicType] then - if (warPoints >= options.war1Threshold.value) then - musicType = "war" - if (warPoints >= options.war2Threshold.value) then musicType = "war2" end + if (warPoints >= options.war1Threshold.value) then + musicType = "war" + if (warPoints >= options.war2Threshold.value) then musicType = "war2" end + if (not isSpec) then if attritionRatio < options.attritionRatioLosing.value then musicType = "losing" elseif attritionRatio > options.attritionRatioWinning.value then musicType = "winning" end - else --if (warPoints <= peaceThreshold) then - musicType = "peace" end + else --if (warPoints <= peaceThreshold) then + musicType = "peace" end +end - if (not firstTime) then - StartTrack() - firstTime = true +function UpdateTick() + newTrackWait = newTrackWait + 1 + + if moodDynamic[musicType] then + EvaluateMood() end local playedTime, totalTime = Spring.GetSoundStreamTime() --- playedTime = math.floor(playedTime) --- totalTime = math.floor(totalTime) - - --Spring.Echo(playedTime, totalTime, newTrackWait) - - --if((totalTime - playedTime) <= 6 and (totalTime >= 1) ) then - --Spring.Echo("time left:", (totalTime - playedTime)) - --Spring.Echo("volume:", (totalTime - playedTime)/6) - --if ((totalTime - playedTime)/6 >= 0) then - -- Spring.SetSoundStreamVolume((totalTime - playedTime)/6) - --else - -- Spring.SetSoundStreamVolume(0.1) - --end - --elseif(playedTime <= 5 )then--and not firstFade - --Spring.Echo("time playing:", playedTime) - --Spring.Echo("volume:", playedTime/5) - --Spring.SetSoundStreamVolume( playedTime/5) - --end - --Spring.Echo(prevMusicType, musicType) - if (prevMusicType ~= musicType and moodPriorities[musicType] >= moodPriorities[prevMusicType]) + if ((prevMusicType ~= musicType) and (moodPriorities[musicType] >= moodPriorities[prevMusicType])) or (playedTime >= totalTime) -- both zero means track stopped and not(haltMusic or looping) then prevMusicType = musicType @@ -357,6 +341,21 @@ end function widget:Update(dt) if gameOver then return end + if firstTime then + musicType = "briefing" + StartTrack() + firstTime = false + return + end + + if ((not gameStarted) and (Spring.GetGameFrame() > 30)) then + gameStarted = true + prevMusicType = musicType + musicType = "peace" + StartTrack() + newTrackWait = 0 + end + if not initialized then math.randomseed(os.clock()* 100) initialized=true @@ -372,27 +371,27 @@ function widget:Update(dt) timeframetimer = timeframetimer + dt if (timeframetimer > UPDATE_PERIOD) then -- every second - EvaluateMood() + UpdateTick() timeframetimer = 0 end end -function widget:GameStart() - if not gameStarted then - gameStarted = true - prevMusicType = musicType - musicType = "peace" - StartTrack() - end - - newTrackWait = 0 -end +-- function widget:GameStart() +-- if not gameStarted then +-- gameStarted = true +-- prevMusicType = musicType +-- musicType = "peace" +-- StartTrack() +-- end +-- +-- newTrackWait = 0 +-- end -- Safety of a heisenbug -function widget:GameFrame() - widget:GameStart() - widgetHandler:RemoveCallIn("GameFrame") -end +-- function widget:GameFrame() +-- widget:GameStart() +-- widgetHandler:RemoveCallIn("GameFrame") +-- end function widget:UnitDamaged(unitID, unitDefID, unitTeam, damage, paralyzer) if unitExceptions[unitDefID] then return end @@ -424,20 +423,13 @@ function widget:TeamDied(team) end local function PlayGameOverMusic(gameWon) - local track + gameOver = true if gameWon then - if #tracks.victory <= 0 then return end - track = tracks.victory[math.random(1, #tracks.victory)] musicType = "victory" else - if #tracks.defeat <= 0 then return end - track = tracks.defeat[math.random(1, #tracks.defeat)] musicType = "defeat" end - looping = false - Spring.StopSoundStream() - Spring.PlaySoundStream(track, WG.music_volume or 0.5) - WG.music_start_volume = WG.music_volume + StartTrack() end function widget:GameOver() @@ -446,12 +438,12 @@ end function widget:Initialize() WG.Music = WG.Music or {} - WG.Music.StartTrack = StartTrack - WG.Music.StartLoopingTrack = StartLoopingTrack - WG.Music.StopTrack = StopTrack - WG.Music.SetWarThreshold = SetWarThreshold - WG.Music.SetPeaceThreshold = SetPeaceThreshold - WG.Music.GetMusicType = GetMusicType +-- WG.Music.StartTrack = StartTrack +-- WG.Music.StartLoopingTrack = StartLoopingTrack +-- WG.Music.StopTrack = StopTrack +-- WG.Music.SetWarThreshold = SetWarThreshold +-- WG.Music.SetPeaceThreshold = SetPeaceThreshold +-- WG.Music.GetMusicType = GetMusicType WG.Music.PlayGameOverMusic = PlayGameOverMusic -- for TrackName,TrackDef in pairs(tracks.peace) do @@ -471,10 +463,6 @@ end function widget:Shutdown() Spring.StopSoundStream() WG.Music = nil - - for i=1,#windows do - (windows[i]):Dispose() - end end --------------------------------------------------------------------------------