From 5d7d2606d5582d60c45696b1ad4aaf7d36036ed6 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Fri, 5 Jun 2020 19:47:02 +0930 Subject: [PATCH] Refactor war state points counting for later additions --- snd_music.lua | 127 ++++++++++++++++++++++++++------------------------ 1 file changed, 65 insertions(+), 62 deletions(-) diff --git a/snd_music.lua b/snd_music.lua index f3aeaa3..557b00d 100644 --- a/snd_music.lua +++ b/snd_music.lua @@ -55,7 +55,11 @@ local LOOP_BUFFER = 0.015 -- if looping track is this close to the end, go ahead local UPDATE_PERIOD = 1 local musicType = 'peace' -local dethklok = {} -- keeps track of the number of doods killed in each time frame +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 +local warPointsHostile = {} +local warPointsRollover = 4000000000 -- Roll back to zero after this many have accumulated local timeframetimer = 0 local timeframetimer_short = 0 local loopTrack = '' @@ -64,8 +68,8 @@ local previousTrackType = '' local newTrackWait = 1000 local numVisibleEnemy = 0 local fadeVol -local curTrack = "no name" -local songText = "no name" +local curTrac = "no name" +local songText = "no name" local haltMusic = false local looping = false local paused = false @@ -98,7 +102,7 @@ local function StartLoopingTrack(trackInit, trackLoop) haltMusic = true Spring.StopSoundStream() musicType = 'custom' - + curTrack = trackInit loopTrack = trackLoop Spring.PlaySoundStream(trackInit, WG.music_volume or 0.5) @@ -114,7 +118,7 @@ local function StartTrack(track) haltMusic = false looping = false Spring.StopSoundStream() - + local newTrack = previousTrack if musicType == 'custom' then previousTrackType = "peace" @@ -145,7 +149,7 @@ local function StartTrack(track) -- end firstFade = false previousTrack = newTrack - + -- if (oggInfo.comments.TITLE and oggInfo.comments.TITLE) then -- Spring.Echo("Song changed to: " .. oggInfo.comments.TITLE .. " By: " .. oggInfo.comments.ARTIST) -- else @@ -153,7 +157,7 @@ local function StartTrack(track) -- end curTrack = newTrack Spring.PlaySoundStream(newTrack,WG.music_volume or 0.5) - + WG.music_start_volume = WG.music_volume end @@ -201,7 +205,7 @@ function widget:Update(dt) 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) @@ -209,7 +213,7 @@ function widget:Update(dt) victoryTracks = victoryTracks or VFS.DirList('sounds/music/victory/', '*.ogg', vfsMode) defeatTracks = defeatTracks or VFS.DirList('sounds/music/defeat/', '*.ogg', vfsMode) end - + timeframetimer_short = timeframetimer_short + dt if timeframetimer_short > 0.03 then local playedTime, totalTime = Spring.GetSoundStreamTime() @@ -226,12 +230,13 @@ function widget:Update(dt) end timeframetimer_short = 0 end - + +-- (Spring.GetGameRulesParam("recentNukeLaunch") == 1) -- Might need this for superweapon music later + timeframetimer = timeframetimer + dt if (timeframetimer > UPDATE_PERIOD) then -- every second timeframetimer = 0 newTrackWait = newTrackWait + 1 - local PlayerTeam = Spring.GetMyTeamID() numVisibleEnemy = 0 local doods = Spring.GetVisibleUnits(-1, nil, true) for i=1,#doods do @@ -239,21 +244,23 @@ function widget:Update(dt) numVisibleEnemy = numVisibleEnemy + 1 end end - - local totalKilled = 0 - for i = 1, 10, 1 do --calculate the first half of the table (1-15) - totalKilled = totalKilled + (dethklok[i] * 2) - end - - for i = 11, 20, 1 do -- calculate the second half of the table (16-45) - totalKilled = totalKilled + dethklok[i] - end - - for i = 20, 1, -1 do -- shift value(s) to the end of table - dethklok[i+1] = dethklok[i] - end - dethklok[1] = 0 -- empty the first row - + + local totalKilled, friendliesKilled, hostilesKilled = 0, 0, 0 + local iLast = ((warPointsIter-11) % warPointsSize) + 1 -- Look back 10 periods. + local iLast2 = ((warPointsIter-21) % warPointsSize) + 1 -- Look back 20 periods. + -- Last 10 seconds count for double + friendliesKilled = friendliesKilled + ((warPointsFriendly[warPointsIter] - warPointsFriendly[iLast]) % warPointsRollover) + friendliesKilled = friendliesKilled + ((warPointsFriendly[warPointsIter] - warPointsFriendly[iLast2]) % warPointsRollover) + hostilesKilled = hostilesKilled + ((warPointsHostile[warPointsIter] - warPointsHostile[iLast]) % warPointsRollover) + hostilesKilled = hostilesKilled + ((warPointsHostile[warPointsIter] - warPointsHostile[iLast2]) % warPointsRollover) + totalKilled = (friendliesKilled * 1.5) + hostilesKilled + + -- Roll to next index in the circular buffers, continue cumulative sum + local iNext = (warPointsIter % warPointsSize) + 1 + warPointsFriendly[iNext] = warPointsFriendly[warPointsIter] % warPointsRollover + warPointsHostile[iNext] = warPointsHostile[warPointsIter] % warPointsRollover + warPointsIter = iNext + if (musicType == 'war' or musicType == 'peace') then if (totalKilled >= warThreshold) then musicType = 'war' @@ -261,19 +268,19 @@ function widget:Update(dt) musicType = 'peace' end end - + if (not firstTime) then StartTrack() - firstTime = true -- pop this cherry + firstTime = true end - + local playedTime, totalTime = Spring.GetSoundStreamTime() playedTime = math.floor(playedTime) totalTime = math.floor(totalTime) --Spring.Echo(playedTime, 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) @@ -293,7 +300,7 @@ function widget:Update(dt) and not(haltMusic or looping) then previousTrackType = musicType StartTrack() - + --Spring.Echo("Track: " .. newTrack) newTrackWait = 0 end @@ -312,7 +319,7 @@ function widget:GameStart() musicType = "peace" StartTrack() end - + --Spring.Echo("Track: " .. newTrack) newTrackWait = 0 end @@ -324,33 +331,25 @@ function widget:GameFrame() end function widget:UnitDamaged(unitID, unitDefID, unitTeam, damage, paralyzer) - if unitExceptions[unitDefID] then - return - end - + if unitExceptions[unitDefID] then return end if (damage < 1.5) then return end - local PlayerTeam = Spring.GetMyTeamID() - if (UnitDefs[unitDefID] == nil) then return end - - if paralyzer then - return + if paralyzer then return end + + local multifactor = 1 + if (numVisibleEnemy > 3) then + multifactor = math.log(numVisibleEnemy) + end + if (teamID == myTeam) then + warPointsFriendly[warPointsIter] = warPointsFriendly[warPointsIter] + (damage * multifactor); else - if (teamID == PlayerTeam) then - damage = damage * 1.5 - end - local multifactor = 1 - if (numVisibleEnemy > 3) then - multifactor = math.log(numVisibleEnemy) - end - dethklok[1] = dethklok[1] + (damage * multifactor); + warPointsHostile[warPointsIter] = warPointsHostile[warPointsIter] + (damage * multifactor); end end function widget:UnitDestroyed(unitID, unitDefID, teamID) - if unitExceptions[unitDefID] then - return - end + if unitExceptions[unitDefID] then return end + local unitWorth = 50 if (UnitDefs[unitDefID].metalCost > 500) then unitWorth = 200 @@ -364,14 +363,16 @@ function widget:UnitDestroyed(unitID, unitDefID, teamID) if (UnitDefs[unitDefID].metalCost > 8000) then unitWorth = 700 end - if (teamID == PlayerTeam) then - unitWorth = unitWorth * 1.5 - end + local multifactor = 1 if (numVisibleEnemy > 3) then multifactor = math.log(numVisibleEnemy) end - dethklok[1] = dethklok[1] + (unitWorth*multifactor); + if (teamID == myTeam) then + warPointsFriendly[warPointsIter] = warPointsFriendly[warPointsIter] + (unitWorth*multifactor); + else + warPointsHostile[warPointsIter] = warPointsHostile[warPointsIter] + (unitWorth*multifactor); + end end function widget:TeamDied(team) @@ -413,22 +414,24 @@ function widget:Initialize() -- Spring.Echo(math.random(), math.random()) -- Spring.Echo(os.clock()) - + -- for TrackName,TrackDef in pairs(peaceTracks) do -- Spring.Echo("Track: " .. TrackDef) -- end --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, 30, 1 do - dethklok[i]=0 + + for i = 1, warPointsSize do + warPoints[i] = 0 + warPointsFriendly[i] = 0 + warPointsHostile[i] = 0 end end function widget:Shutdown() Spring.StopSoundStream() WG.Music = nil - + for i=1,#windows do (windows[i]):Dispose() end