Minor change to match current master. Will properly rebase all the other stuff later.
This commit is contained in:
parent
3e1154347e
commit
263359a7da
166
snd_music.lua
166
snd_music.lua
|
@ -86,14 +86,12 @@ options = {
|
||||||
|
|
||||||
local unitExceptions = include("Configs/snd_music_exception.lua")
|
local unitExceptions = include("Configs/snd_music_exception.lua")
|
||||||
|
|
||||||
local windows = {}
|
|
||||||
|
|
||||||
|
|
||||||
local spAreTeamsAllied = Spring.AreTeamsAllied
|
local spAreTeamsAllied = Spring.AreTeamsAllied
|
||||||
|
|
||||||
local MOODS = {"peace", "war", "war2", "winning", "losing", "briefing", "victory", "defeat"}
|
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 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 war2Threshold = 300000
|
||||||
local warThreshold = 30000
|
local warThreshold = 30000
|
||||||
local peaceThreshold = 10000
|
local peaceThreshold = 10000
|
||||||
|
@ -126,18 +124,19 @@ local paused = false
|
||||||
local lastTrackTime = -1
|
local lastTrackTime = -1
|
||||||
local tracks = {}
|
local tracks = {}
|
||||||
|
|
||||||
local firstTime = false
|
local firstTime = true
|
||||||
local wasPaused = false
|
local wasPaused = false
|
||||||
local firstFade = true
|
local firstFade = true
|
||||||
local initSeed = 0
|
local initSeed = 0
|
||||||
local initialized = false
|
local initialized = false
|
||||||
local gameStarted = Spring.GetGameFrame() > 0
|
local gameStarted = Spring.GetGameFrame() > 30 --0
|
||||||
local gameOver = false
|
local gameOver = false
|
||||||
|
|
||||||
local myTeam = Spring.GetMyTeamID()
|
local myTeam = Spring.GetMyTeamID()
|
||||||
local isSpec = Spring.GetSpectatingState() or Spring.IsReplay()
|
local isSpec = Spring.GetSpectatingState() or Spring.IsReplay()
|
||||||
local defeat = false
|
local defeat = false
|
||||||
|
|
||||||
|
local timesStartTrack = 0
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
local function GetMusicType()
|
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")
|
Spring.Log(widget:GetInfo().name, LOG.ERROR, "Missing one or both tracks for looping")
|
||||||
end
|
end
|
||||||
haltMusic = true
|
haltMusic = true
|
||||||
Spring.StopSoundStream()
|
|
||||||
musicType = "custom"
|
musicType = "custom"
|
||||||
|
|
||||||
curTrack = trackInit
|
curTrack = trackInit
|
||||||
loopTrack = trackLoop
|
loopTrack = trackLoop
|
||||||
|
Spring.StopSoundStream()
|
||||||
Spring.PlaySoundStream(trackInit, WG.music_volume or 0.5)
|
Spring.PlaySoundStream(trackInit, WG.music_volume or 0.5)
|
||||||
looping = 0.5
|
looping = 0.5
|
||||||
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
|
||||||
|
|
||||||
local function StartTrack(track)
|
local function StartTrack(track)
|
||||||
|
timesStartTrack = timesStartTrack + 1
|
||||||
|
Spring.Echo("StartTrack called with mood: " .. musicType .. " (" .. tostring(timesStartTrack) .. ")")
|
||||||
if not tracks.peace then
|
if not tracks.peace then
|
||||||
Spring.Echo("Missing tracks.peace file, no music started")
|
Spring.Echo("Missing tracks.peace file, no music started")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
haltMusic = false
|
|
||||||
looping = false
|
|
||||||
Spring.StopSoundStream()
|
|
||||||
|
|
||||||
local newTrack = previousTrack
|
local newTrack = previousTrack
|
||||||
if musicType == "custom" then
|
if musicType == "custom" then
|
||||||
prevMusicType = "peace"
|
prevMusicType = "peace"
|
||||||
musicType = "peace"
|
musicType = "peace"
|
||||||
end
|
end
|
||||||
if (not gameStarted) then
|
if (not gameStarted) then
|
||||||
|
prevMusicType = "briefing"
|
||||||
musicType = "briefing"
|
musicType = "briefing"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
haltMusic = false
|
||||||
|
looping = false
|
||||||
|
|
||||||
if track then
|
if track then
|
||||||
newTrack = track -- play specified track
|
newTrack = track -- play specified track
|
||||||
musicType = "custom"
|
musicType = "custom"
|
||||||
|
@ -192,6 +207,7 @@ local function StartTrack(track)
|
||||||
firstFade = false
|
firstFade = false
|
||||||
previousTrack = newTrack
|
previousTrack = newTrack
|
||||||
curTrack = newTrack
|
curTrack = newTrack
|
||||||
|
Spring.StopSoundStream()
|
||||||
Spring.PlaySoundStream(newTrack, WG.music_volume or 0.5)
|
Spring.PlaySoundStream(newTrack, WG.music_volume or 0.5)
|
||||||
|
|
||||||
WG.music_start_volume = WG.music_volume
|
WG.music_start_volume = WG.music_volume
|
||||||
|
@ -243,24 +259,8 @@ function InitializeTracks()
|
||||||
end
|
end
|
||||||
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()
|
function EvaluateMood()
|
||||||
-- (Spring.GetGameRulesParam("recentNukeLaunch") == 1) -- Might need this for superweapon music later
|
-- (Spring.GetGameRulesParam("recentNukeLaunch") == 1) -- Might need this for superweapon music later
|
||||||
newTrackWait = newTrackWait + 1
|
|
||||||
|
|
||||||
numVisibleFriendly = 0
|
numVisibleFriendly = 0
|
||||||
numVisibleEnemy = 0
|
numVisibleEnemy = 0
|
||||||
local doods = Spring.GetVisibleUnits(-1, nil, true)
|
local doods = Spring.GetVisibleUnits(-1, nil, true)
|
||||||
|
@ -300,46 +300,30 @@ function EvaluateMood()
|
||||||
warPointsIter = iNext
|
warPointsIter = iNext
|
||||||
|
|
||||||
local warPoints = totalKilled + totalDmg
|
local warPoints = totalKilled + totalDmg
|
||||||
if moodDynamic[musicType] then
|
if (warPoints >= options.war1Threshold.value) then
|
||||||
if (warPoints >= options.war1Threshold.value) then
|
musicType = "war"
|
||||||
musicType = "war"
|
if (warPoints >= options.war2Threshold.value) then musicType = "war2" end
|
||||||
if (warPoints >= options.war2Threshold.value) then musicType = "war2" end
|
if (not isSpec) then
|
||||||
if attritionRatio < options.attritionRatioLosing.value then
|
if attritionRatio < options.attritionRatioLosing.value then
|
||||||
musicType = "losing"
|
musicType = "losing"
|
||||||
elseif attritionRatio > options.attritionRatioWinning.value then
|
elseif attritionRatio > options.attritionRatioWinning.value then
|
||||||
musicType = "winning"
|
musicType = "winning"
|
||||||
end
|
end
|
||||||
else --if (warPoints <= peaceThreshold) then
|
|
||||||
musicType = "peace"
|
|
||||||
end
|
end
|
||||||
|
else --if (warPoints <= peaceThreshold) then
|
||||||
|
musicType = "peace"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if (not firstTime) then
|
function UpdateTick()
|
||||||
StartTrack()
|
newTrackWait = newTrackWait + 1
|
||||||
firstTime = true
|
|
||||||
|
if moodDynamic[musicType] then
|
||||||
|
EvaluateMood()
|
||||||
end
|
end
|
||||||
|
|
||||||
local playedTime, totalTime = Spring.GetSoundStreamTime()
|
local playedTime, totalTime = Spring.GetSoundStreamTime()
|
||||||
-- playedTime = math.floor(playedTime)
|
if ((prevMusicType ~= musicType) and (moodPriorities[musicType] >= moodPriorities[prevMusicType]))
|
||||||
-- 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])
|
|
||||||
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
|
||||||
prevMusicType = musicType
|
prevMusicType = musicType
|
||||||
|
@ -357,6 +341,21 @@ end
|
||||||
function widget:Update(dt)
|
function widget:Update(dt)
|
||||||
if gameOver then return end
|
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
|
if not initialized then
|
||||||
math.randomseed(os.clock()* 100)
|
math.randomseed(os.clock()* 100)
|
||||||
initialized=true
|
initialized=true
|
||||||
|
@ -372,27 +371,27 @@ function widget:Update(dt)
|
||||||
|
|
||||||
timeframetimer = timeframetimer + dt
|
timeframetimer = timeframetimer + dt
|
||||||
if (timeframetimer > UPDATE_PERIOD) then -- every second
|
if (timeframetimer > UPDATE_PERIOD) then -- every second
|
||||||
EvaluateMood()
|
UpdateTick()
|
||||||
timeframetimer = 0
|
timeframetimer = 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function widget:GameStart()
|
-- function widget:GameStart()
|
||||||
if not gameStarted then
|
-- if not gameStarted then
|
||||||
gameStarted = true
|
-- gameStarted = true
|
||||||
prevMusicType = musicType
|
-- prevMusicType = musicType
|
||||||
musicType = "peace"
|
-- musicType = "peace"
|
||||||
StartTrack()
|
-- StartTrack()
|
||||||
end
|
-- end
|
||||||
|
--
|
||||||
newTrackWait = 0
|
-- newTrackWait = 0
|
||||||
end
|
-- 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)
|
||||||
if unitExceptions[unitDefID] then return end
|
if unitExceptions[unitDefID] then return end
|
||||||
|
@ -424,20 +423,13 @@ function widget:TeamDied(team)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function PlayGameOverMusic(gameWon)
|
local function PlayGameOverMusic(gameWon)
|
||||||
local track
|
gameOver = true
|
||||||
if gameWon then
|
if gameWon then
|
||||||
if #tracks.victory <= 0 then return end
|
|
||||||
track = tracks.victory[math.random(1, #tracks.victory)]
|
|
||||||
musicType = "victory"
|
musicType = "victory"
|
||||||
else
|
else
|
||||||
if #tracks.defeat <= 0 then return end
|
|
||||||
track = tracks.defeat[math.random(1, #tracks.defeat)]
|
|
||||||
musicType = "defeat"
|
musicType = "defeat"
|
||||||
end
|
end
|
||||||
looping = false
|
StartTrack()
|
||||||
Spring.StopSoundStream()
|
|
||||||
Spring.PlaySoundStream(track, WG.music_volume or 0.5)
|
|
||||||
WG.music_start_volume = WG.music_volume
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function widget:GameOver()
|
function widget:GameOver()
|
||||||
|
@ -446,12 +438,12 @@ end
|
||||||
|
|
||||||
function widget:Initialize()
|
function widget:Initialize()
|
||||||
WG.Music = WG.Music or {}
|
WG.Music = WG.Music or {}
|
||||||
WG.Music.StartTrack = StartTrack
|
-- WG.Music.StartTrack = StartTrack
|
||||||
WG.Music.StartLoopingTrack = StartLoopingTrack
|
-- WG.Music.StartLoopingTrack = StartLoopingTrack
|
||||||
WG.Music.StopTrack = StopTrack
|
-- WG.Music.StopTrack = StopTrack
|
||||||
WG.Music.SetWarThreshold = SetWarThreshold
|
-- WG.Music.SetWarThreshold = SetWarThreshold
|
||||||
WG.Music.SetPeaceThreshold = SetPeaceThreshold
|
-- WG.Music.SetPeaceThreshold = SetPeaceThreshold
|
||||||
WG.Music.GetMusicType = GetMusicType
|
-- WG.Music.GetMusicType = GetMusicType
|
||||||
WG.Music.PlayGameOverMusic = PlayGameOverMusic
|
WG.Music.PlayGameOverMusic = PlayGameOverMusic
|
||||||
|
|
||||||
-- for TrackName,TrackDef in pairs(tracks.peace) do
|
-- for TrackName,TrackDef in pairs(tracks.peace) do
|
||||||
|
@ -471,10 +463,6 @@ end
|
||||||
function widget:Shutdown()
|
function widget:Shutdown()
|
||||||
Spring.StopSoundStream()
|
Spring.StopSoundStream()
|
||||||
WG.Music = nil
|
WG.Music = nil
|
||||||
|
|
||||||
for i=1,#windows do
|
|
||||||
(windows[i]):Dispose()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue