Slight refactor

This commit is contained in:
Luke Hubmayer-Werner 2020-06-06 23:28:07 +09:30
parent 95b4e08ebd
commit 250d614338
1 changed files with 53 additions and 31 deletions

View File

@ -15,8 +15,8 @@ function widget:GetInfo()
return { return {
name = "Music Player", name = "Music Player",
desc = "Plays music based on situation", desc = "Plays music based on situation",
author = "cake, trepan, Smoth, Licho, xponen", author = "cake, trepan, Smoth, Licho, xponen, Birdulon",
date = "Mar 01, 2008, Aug 20 2009, Nov 23 2011", date = "Mar 01, 2008, Aug 20 2009, Nov 23 2011, June 2020",
license = "GNU GPL, v2 or later", license = "GNU GPL, v2 or later",
layer = 0, layer = 0,
enabled = true -- loaded by default? enabled = true -- loaded by default?
@ -62,16 +62,40 @@ options = {
desc = "Music switches to Winning when recent attrition ratio rises above this value", desc = "Music switches to Winning when recent attrition ratio rises above this value",
noHotkey = true, noHotkey = true,
}, },
war1Threshold = {
name = "War Threshold 1",
type = "number",
value = 30000,
min = 10000,
max = 100000,
step = 100,
desc = "Music switches to War when recent war points rise above this value",
noHotkey = true,
},
war2Threshold = {
name = "War Threshold 2",
type = "number",
value = 300000,
min = 20000,
max = 1000000,
step = 100,
desc = "Music switches to War2 when recent war points rise above this value",
noHotkey = true,
},
} }
local unitExceptions = include("Configs/snd_music_exception.lua") local unitExceptions = include("Configs/snd_music_exception.lua")
local windows = {} local windows = {}
local moodPriorities = {peace=0, war=1, war2=2, winning=4, losing=4, briefing=10, victory=10, defeat=10} -- 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} -- Determines which music moods will instantly interrupt others, and which will wait for playing track to finish local spAreTeamsAllied = Spring.AreTeamsAllied
local war2Threshold = 400000
local warThreshold = 50000 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 war2Threshold = 300000
local warThreshold = 30000
local peaceThreshold = 10000 local peaceThreshold = 10000
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
@ -202,24 +226,21 @@ end
function InitializeTracks() function InitializeTracks()
Spring.Echo("Initializing music tracks") Spring.Echo("Initializing music tracks")
if VFS.FileExists(PLAYLIST_FILE, VFS.RAW_FIRST) then -- if VFS.FileExists(PLAYLIST_FILE, VFS.RAW_FIRST) then
local plTracks = VFS.Include(PLAYLIST_FILE, nil, VFS.RAW_FIRST) -- local plTracks = VFS.Include(PLAYLIST_FILE, nil, VFS.RAW_FIRST)
tracks.war = plTracks.war -- tracks.war = plTracks.war
tracks.peace = plTracks.peace -- tracks.peace = plTracks.peace
tracks.briefing = plTracks.briefing -- tracks.briefing = plTracks.briefing
tracks.victory = plTracks.victory -- tracks.victory = plTracks.victory
tracks.defeat = plTracks.defeat -- tracks.defeat = plTracks.defeat
end -- end
local vfsMode = (options.useIncludedTracks.value and VFS.RAW_FIRST) or VFS.RAW local vfsMode = (options.useIncludedTracks.value and VFS.RAW_FIRST) or VFS.RAW
tracks.war = tracks.war or VFS.DirList("sounds/music/war/", "*.ogg", vfsMode) for i=1,#MOODS do
tracks.war2 = tracks.war2 or VFS.DirList("sounds/music/war2/", "*.ogg", vfsMode) local mood = MOODS[i]
tracks.winning = tracks.winning or VFS.DirList("sounds/music/winning/", "*.ogg", vfsMode) tracks[mood] = VFS.DirList("sounds/music/" .. mood .. "/", "*.ogg", vfsMode)
tracks.losing = tracks.losing or VFS.DirList("sounds/music/losing/", "*.ogg", vfsMode) -- tracks[mood] = tracks[mood] or VFS.DirList("sounds/music/" .. mood .. "/", "*.ogg", vfsMode)
tracks.peace = tracks.peace or VFS.DirList("sounds/music/peace/", "*.ogg", vfsMode) end
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 end
function CheckLoop() function CheckLoop()
@ -278,16 +299,17 @@ function EvaluateMood()
deathPointsHostile[iNext] = deathPointsHostile[warPointsIter] % warPointsRollover deathPointsHostile[iNext] = deathPointsHostile[warPointsIter] % warPointsRollover
warPointsIter = iNext warPointsIter = iNext
local warPoints = totalKilled + totalDmg
if moodDynamic[musicType] then if moodDynamic[musicType] then
if (totalKilled >= warThreshold) then if (warPoints >= options.war1Threshold.value) then
musicType = "war" musicType = "war"
if (totalKilled >= war2Threshold) then musicType = "war2" end if (warPoints >= options.war2Threshold.value) then musicType = "war2" end
if attritionRatio < options.attritionRatioLosing then if attritionRatio < options.attritionRatioLosing.value then
musicType = "losing" musicType = "losing"
elseif attritionRatio > options.attritionRatioWinning then elseif attritionRatio > options.attritionRatioWinning.value then
musicType = "winning" musicType = "winning"
end end
else --if (totalKilled <= peaceThreshold) then else --if (warPoints <= peaceThreshold) then
musicType = "peace" musicType = "peace"
end end
end end
@ -377,7 +399,7 @@ function widget:UnitDamaged(unitID, unitDefID, unitTeam, damage, paralyzer)
if (UnitDefs[unitDefID] == nil) then return end if (UnitDefs[unitDefID] == nil) then return end
if paralyzer then return end if paralyzer then return end
if (teamID == myTeam) then if spAreTeamsAllied(unitTeam or 0, myTeam) then
dmgPointsFriendly[warPointsIter] = dmgPointsFriendly[warPointsIter] + damage dmgPointsFriendly[warPointsIter] = dmgPointsFriendly[warPointsIter] + damage
else else
dmgPointsHostile[warPointsIter] = dmgPointsHostile[warPointsIter] + damage dmgPointsHostile[warPointsIter] = dmgPointsHostile[warPointsIter] + damage
@ -388,7 +410,7 @@ function widget:UnitDestroyed(unitID, unitDefID, teamID)
if unitExceptions[unitDefID] then return end if unitExceptions[unitDefID] then return end
local unitCost = UnitDefs[unitDefID].metalCost local unitCost = UnitDefs[unitDefID].metalCost
if (teamID == myTeam) then if spAreTeamsAllied(teamID or 0, myTeam) then
deathPointsFriendly[warPointsIter] = deathPointsFriendly[warPointsIter] + unitCost deathPointsFriendly[warPointsIter] = deathPointsFriendly[warPointsIter] + unitCost
else else
deathPointsHostile[warPointsIter] = deathPointsHostile[warPointsIter] + unitCost deathPointsHostile[warPointsIter] = deathPointsHostile[warPointsIter] + unitCost
@ -414,7 +436,7 @@ local function PlayGameOverMusic(gameWon)
end end
looping = false looping = false
Spring.StopSoundStream() Spring.StopSoundStream()
Spring.PlaySoundStream(track,WG.music_volume or 0.5) Spring.PlaySoundStream(track, WG.music_volume or 0.5)
WG.music_start_volume = WG.music_volume WG.music_start_volume = WG.music_volume
end end
@ -438,7 +460,7 @@ function widget:Initialize()
--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
-- for i=1,20 do Spring.Echo(math.random()) end -- for i=1,20 do Spring.Echo(math.random()) end
for i = 1, warPointsSize do for i=1,warPointsSize do
dmgPointsFriendly[i] = 0 dmgPointsFriendly[i] = 0
dmgPointsHostile[i] = 0 dmgPointsHostile[i] = 0
deathPointsFriendly[i] = 0 deathPointsFriendly[i] = 0