Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function widget:GetInfo()
- return {
- name = "Battle Dynamic Music Player V1.1",
- desc = "Plays .ogg music from music\* peace/coldwar/war folders according to the in-game action",
- author = "LEDZ",
- date = "May 19 2012",
- license = "GNU GPL v2",
- layer = 0,
- enabled = true
- }
- end
- ----------------------------------------------------------------------------------------[[
- --[[
- Readme:
- To use, put appropriately themed .ogg files in peace/coldwar/war folders within the music folder:
- Peace; calm, peaceful music
- Coldwar; more somber, ominous and suggestive of trouble
- War; Exciting music will work well here.
- At a certain level past the war threshold, the music will immediately switch to war,
- so if there's a big fight, the war music will cut-in just as you would expect
- Type /luaui nextsong to skip and /luaui debugmusic to see some information of the variables used to pick type of music
- This widget has borrowed heavily from Jool's snd_volume_osd.lua
- Functionality of the music player is an improvement on zwzsg's music.lua and Vebyast's gui_OTA_music_adv.lua
- --]]
- ----------------------------------------------------------------------------------------
- -- Spring accelerators --
- ----------------------------------------------------------------------------------------
- local spGetGameSpeed = Spring.GetGameSpeed
- local spStopSoundStream = Spring.StopSoundStream
- local spPlaySoundStream = Spring.PlaySoundStream --setting volume here overrides and disables snd_volmusic
- local spGetSoundStreamTime = Spring.GetSoundStreamTime
- local spGetTimer = Spring.GetTimer
- local spDiffTimers = Spring.DiffTimers
- local spGetDrawFrame = Spring.GetDrawFrame
- local spGetMouseState = Spring.GetMouseState
- local spGetTeamUnits = Spring.GetTeamUnits
- local spGetPlayerRoster = Spring.GetPlayerRoster
- local spGetUnitDefID = Spring.GetUnitDefID
- local spIsReplay = Spring.IsReplay
- local spGetSpectatingState = Spring.GetSpectatingState
- local spGetMyTeamID = Spring.GetMyTeamID
- local spGetMyAllyTeamID = Spring.GetMyAllyTeamID
- local spGetMyPlayerID = Spring.GetMyPlayerID
- local spGetPlayerInfo = Spring.GetPlayerInfo
- local spGetTeamList = Spring.GetTeamList
- local spGetAllyTeamList = Spring.GetAllyTeamList
- local spGetAllUnits = Spring.GetAllUnits
- local spGetUnitHealth = Spring.GetUnitHealth
- local spGetTeamUnits = Spring.GetTeamUnits
- local Echo = Spring.Echo
- ----------------------------------------------------------------------------------------
- -- Music Volume draw --
- ----------------------------------------------------------------------------------------
- local musicVolume
- local TextDraw = gl.Text
- local vsx,vsy = gl.GetViewSizes()
- local widgetPosX = vsx/3
- local widgetPosY = vsy/6
- local pressedToMove = false
- local altdown
- local dt = -1
- local drawDelay = 0
- ----------------------------------------------------------------------------------------
- -- Music Volume Settings, configurable --
- ----------------------------------------------------------------------------------------
- local step = 2 -- how many steps to change sound musicVolume on one key press
- local pluskey = 0x10E -- numpad+ (look in uikeys.txt in spring folder for key symbols)
- local minuskey = 0x10D -- numpad-
- local pluskey2 = 0x02B -- +key (duplicate key for musicVolume+, set to same as primary to disable)
- local minuskey2 = 0x02D -- -key
- local dtime = 3 --How long time the musicVolume display is drawn, in seconds
- local ftime = 2.5 --How long time before the musicVolume display starts fading, in seconds
- local widgetWidth = vsx/2.5 -- in pixels (changed from 400)
- local widgetHeight = vsy/25.6 -- in pixels (changed from 40)
- local rectangles = 50 -- number of boxes in musicVolume bar
- local boxspacing = 2 -- space between boxes
- local red1 = 0 -- musicVolume bar colour, 0 to 1.
- local green1 = 0.5 -- musicVolume bar colour, 0 to 1.
- local blue1 = 0.8 -- musicVolume bar colour, 0 to 1.
- local red2 = 1 -- generalVolume bar colour, 0 to 1.
- local green2 = 0.2 -- generalVolume bar colour, 0 to 1.
- local blue2 = 0 -- generalVolume bar colour, 0 to 1.
- local red3 = 0.8 -- masterVolume bar colour, 0 to 1.
- local green3 = 0.8 -- masterVolume bar colour, 0 to 1.
- local blue3 = 0 -- masterVolume bar colour, 0 to 1.
- ----------------------------------------------------------------------------------------
- -- Dynamic Music --
- ----------------------------------------------------------------------------------------
- local normalPEACE_VALUE = 0.0025 -- below this, peace
- local normalWAR_VALUE = 0.075 -- above this, war
- local normalWAR_COOLDOWN = .0005 -- rate at which metalDestroyedCounter goes down
- local PEACE_VALUE = 0.0025
- local WAR_VALUE = 0.075
- local WAR_COOLDOWN = 0.0005
- local musicVolume = Spring.GetConfigInt("snd_volmusic")
- local generalVolume = Spring.GetConfigInt("snd_volgeneral")
- local masterVolume = Spring.GetConfigInt("snd_volmaster")
- local musicType = 0 -- 0 = peace, 1 = coldwar, 2 = war
- local metalDestroyedCounter = 0
- local teamMetalTotal = 0
- local widgetTime = 0 -- Time elapsed since widget started, in seconds
- local lastupdate = 0 -- time at which last metal calculation occurred
- local ratioMetal = 1 -- how much the destroyed metal is above the war threshold, for dampening
- -- added this so that it wouldn't play war music for too long if there was a long break in the fighting after a big battle
- local spec = false
- local debug = false
- local numPlayers = {}
- local warTracks = {}
- local coldwarTracks = {}
- local peaceTracks = {}
- local CurrentTrack = nil
- local info = ""
- local LastTimer = nil
- local LastPlayedTime = 0
- local playNew = true
- local gameStarted = false
- ----------------------------------------------------------------------------------------
- ----------------------------------------------------------------------------------------
- -- Intialize --
- ----------------------------------------------------------------------------------------
- function widget:Initialize() --loads .ogg files from the directories to table
- for _,track in ipairs(VFS.DirList('music/War/','*.ogg')) do
- table.insert(warTracks,track)
- end
- for _,track in ipairs(VFS.DirList('music/Coldwar/','*.ogg')) do
- table.insert(coldwarTracks,track)
- end
- for _,track in ipairs(VFS.DirList('music/Peace/','*.ogg')) do
- table.insert(peaceTracks,track)
- end
- Echo("\255\1\205\205Use alt/ctrl/shift + plus/minus keys to adjust volume")
- musicType = 0
- curMusicType = 0
- metalDestroyedCounter = 0
- teamMetalTotal = 0
- widgetTime = 0
- lastupdate = 0
- end
- function widget:GameStart()
- Echo("\255\1\205\205Skip song by typing /luaui nextsong")
- end
- ----------------------------------------------------------------------------------------
- -- Volume Control --
- ----------------------------------------------------------------------------------------
- function widget:KeyPress(key, mods, isRepeat)
- if (key == pluskey or key == pluskey2) and mods.alt then -- KEY = Alt + pluskey
- musicVolume = Spring.GetConfigInt("snd_volmusic")
- musicVolume = musicVolume + step
- if musicVolume < 0 then musicVolume = 0 end
- if musicVolume > 100 then musicVolume = 100 end
- Spring.SetConfigInt("snd_volmusic", musicVolume)
- dt = os.clock()
- return true
- elseif (key == minuskey or key == minuskey2) and mods.alt then -- KEY = Alt + minuskey
- musicVolume = Spring.GetConfigInt("snd_volmusic")
- musicVolume = musicVolume - step
- if musicVolume < 0 then musicVolume = 0 end
- if musicVolume > 100 then musicVolume = 100 end
- Spring.SetConfigInt("snd_volmusic", musicVolume)
- dt = os.clock()
- return true
- elseif (key == pluskey or key == pluskey2) and mods.shift then -- KEY = Shift + pluskey
- generalVolume = Spring.GetConfigInt("snd_volgeneral")
- generalVolume = generalVolume + step
- if generalVolume < 0 then generalVolume = 0 end
- if generalVolume > 100 then generalVolume = 100 end
- Spring.SetConfigInt("snd_general", generalVolume)
- Spring.SetConfigInt("snd_volbattle", generalVolume)
- Spring.SetConfigInt("snd_volgeneral", generalVolume)
- Spring.SetConfigInt("snd_volui", generalVolume)
- Spring.SetConfigInt("snd_volunitreply", generalVolume)
- dt = os.clock()
- return true
- elseif (key == minuskey or key == minuskey2) and mods.shift then -- KEY = Shift + minuskey
- generalVolume = Spring.GetConfigInt("snd_volgeneral")
- generalVolume = generalVolume - step
- if generalVolume < 0 then generalVolume = 0 end
- if generalVolume > 100 then generalVolume = 100 end
- Spring.SetConfigInt("snd_general", generalVolume)
- Spring.SetConfigInt("snd_volbattle", generalVolume)
- Spring.SetConfigInt("snd_volgeneral", generalVolume)
- Spring.SetConfigInt("snd_volui", generalVolume)
- Spring.SetConfigInt("snd_volunitreply", generalVolume)
- dt = os.clock()
- return true
- elseif (key == pluskey or key == pluskey2) and mods.ctrl then -- KEY = Ctrl + pluskey
- masterVolume = Spring.GetConfigInt("snd_volmaster")
- masterVolume = masterVolume + step
- if masterVolume < 0 then masterVolume = 0 end
- if masterVolume > 100 then masterVolume = 100 end
- Spring.SetConfigInt("snd_volmaster", masterVolume)
- dt = os.clock()
- return true
- elseif (key == minuskey or key == minuskey2) and mods.ctrl then -- KEY = Ctrl + minuskey
- masterVolume = Spring.GetConfigInt("snd_volmaster")
- masterVolume = masterVolume - step
- if masterVolume < 0 then masterVolume = 0 end
- if masterVolume > 100 then masterVolume = 100 end
- Spring.SetConfigInt("snd_volmaster", masterVolume)
- dt = os.clock()
- return true
- elseif key == 0x134 then --ALT
- altdown = true
- end
- return false
- end
- function widget:KeyRelease(key)
- if altdown and (key == pluskey or key == minuskey or key == pluskey2 or key == minuskey2) then
- return true
- elseif key == 0x134 then --ALT
- altdown = false
- return true
- end
- return false
- end
- function widget:DrawScreen()
- local y1 = widgetPosY
- local y2 = widgetPosY + widgetHeight
- local x1 = widgetPosX
- local x2 = widgetPosX + widgetWidth
- local ostime = os.clock()
- local t = ostime - dt
- local boxwidth = widgetWidth/rectangles
- if debug then
- fontHandler.UseFont("LuaUI/Fonts/FreeSansBold_14")
- gl.Color(0.87,0.74,0)
- if spec then
- fontHandler.Draw("Spectator Mode",x1-300,y2+280)
- fontHandler.Draw("Number of players: "..numPlayers,x1-300,y2+260)
- end
- fontHandler.Draw("metalDestroyedCounter: "..metalDestroyedCounter,x1-300,y2+240)
- fontHandler.Draw("teamMetalTotal: "..teamMetalTotal,x1-300,y2+220)
- fontHandler.Draw("metalDestroyedCounter/teamMetalTotal: "..metalDestroyedCounter/teamMetalTotal,x1-300,y2+200)
- if musicType == 0 then
- fontHandler.Draw("Next Music Type: Peace",x1-300,y2+180)
- elseif musicType == 1 then
- fontHandler.Draw("Next Music Type: Coldwar",x1-300,y2+180)
- else
- fontHandler.Draw("Next Music Type: War",x1-300,y2+180)
- end
- if curMusicType == 0 then
- fontHandler.Draw("Current Music Type: Peace",x1-300,y2+160)
- elseif curMusicType == 1 then
- fontHandler.Draw("Current Music Type: Coldwar",x1-300,y2+160)
- else
- fontHandler.Draw("Current Music Type: War",x1-300,y2+160)
- end
- fontHandler.Draw("WAR_COOLDOWN = "..WAR_COOLDOWN,x1-300,y2+140)
- if COOLDOWNaccelerator then
- gl.Color(1,0.3,0.3)
- fontHandler.Draw("COOLDOWN accelerator: "..ratioMetal*ratioMetal,x1-300,y2+120)
- else
- gl.Color(0.87,0.74,0)
- fontHandler.Draw("COOLDOWN accelerator: "..ratioMetal*ratioMetal,x1-300,y2+120)
- end
- gl.Color(0.87,0.74,0)
- fontHandler.Draw("War%: "..100*metalDestroyedCounter/(teamMetalTotal*WAR_VALUE),x1-300,y2+100)
- --[[
- gl.Color(0.5,1,0.5)
- if spec then
- gl.Text("Spectator Mode",x1-300,y2+180)
- gl.Text("Number of players: "..numPlayers,x1-300,y2+160)
- end
- gl.Text("metalDestroyedCounter: "..metalDestroyedCounter,x1-300,y2+140)
- gl.Text("teamMetalTotal: "..teamMetalTotal,x1-300,y2+120)
- gl.Text("metalDestroyedCounter/teamMetalTotal: "..metalDestroyedCounter/teamMetalTotal,x1-300,y2+100)
- gl.Text("musictype: "..musicType,x1-300,y2+80)
- gl.Text("curmusictype: "..curMusicType,x1-300,y2+60)
- gl.Text("WAR_COOLDOWN = "..WAR_COOLDOWN,x1-300,y2+40)
- if COOLDOWNaccelerator then
- gl.Color(1,0.3,0.3)
- gl.Text("COOLDOWN accelerator: "..ratioMetal*ratioMetal,x1-300,y2+20)
- else
- gl.Color(0.5,1,0.5)
- gl.Text("COOLDOWN accelerator: "..ratioMetal*ratioMetal,x1-300,y2+20)
- end
- gl.Color(0.5,1,0.5)
- gl.Text("War%: "..100*metalDestroyedCounter/(teamMetalTotal*WAR_VALUE),x1-300,y2-0)
- --]]
- end
- if t < dtime and dt >= 0 then --dtime = 3
- local alpha
- if t < ftime then --ftime = 2
- alpha = 1
- else
- alpha = 3*(dtime-t)/dtime
- end
- ---------------------------------------------
- --Music Volume
- ---------------------------------------------
- gl.Color(0,0,0,0.1*alpha) -- draws background rectangle
- gl.Rect(x1,y1,x2-1,y2)
- gl.Color(red1,green1,blue1,alpha)
- musicVolume = Spring.GetConfigInt("snd_volmusic")
- gl.Text("Music Volume: ".. musicVolume.."% ALT & +/-",x1+5,y2+5)
- gl.Color(0.3,0.3,0.3,0.6*alpha) -- draws empty rectangles
- for i = 1,rectangles do
- local u1 = x1+(i-1)*boxwidth
- local u2= u1+boxwidth-boxspacing
- gl.Rect(u1,y1,u2,y2)
- end
- local vol1 = math.floor(musicVolume/(100/rectangles))
- gl.Color(0,0.8,0,alpha) -- draws filled rectangles
- for i = 1,vol1 do
- local u1 = x1+(i-1)*boxwidth
- local u2= u1+boxwidth-boxspacing
- gl.Color(red1,green1,blue1,alpha)
- gl.Rect(u1+1,y1+1,u1+2,y2-1)
- gl.Color(red1*1.2,green1*1.2,blue1*1.2,alpha)
- gl.Rect(u1+2,y1+1,u2-1,y2-1)
- end
- ---------------------------------------------
- --General Volume
- ---------------------------------------------
- gl.Color(0,0,0,0.1*alpha) -- draws background rectangle
- gl.Rect(x1,y1+(widgetHeight*1.5),x2-1,y2+(widgetHeight*1.5))
- gl.Color(red2,green2,blue2,alpha)
- gl.Text("General Volume: ".. generalVolume .."% SHIFT & +/-",x1+5,y2+5+(widgetHeight*1.5))
- gl.Color(0.3,0.3,0.3,0.6*alpha) -- draws empty rectangles
- for i = 1,rectangles do
- local u1 = x1+(i-1)*boxwidth
- local u2= u1+boxwidth-boxspacing
- gl.Rect(u1,y1+(widgetHeight*1.5),u2,y2+(widgetHeight*1.5))
- end
- local vol2 = math.floor(generalVolume/(100/rectangles))
- gl.Color(0,0.8,0,alpha) -- draws filled rectangles
- for i = 1,vol2 do
- local u1 = x1+(i-1)*boxwidth
- local u2= u1+boxwidth-boxspacing
- gl.Color(red2,green2,blue2,alpha)
- gl.Rect(u1+1,y1+1+(widgetHeight*1.5),u1+2,y2-1+(widgetHeight*1.5))
- gl.Color(red2*1.2,green2*1.2,blue2*1.2,alpha)
- gl.Rect(u1+2,y1+1+(widgetHeight*1.5),u2-1,y2-1+(widgetHeight*1.5))
- end
- ---------------------------------------------
- --Master Volume
- ---------------------------------------------
- gl.Color(0,0,0,0.1*alpha) -- draws background rectangle
- gl.Rect(x1,y1+(widgetHeight*3),x2-1,y2+(widgetHeight*3))
- gl.Color(red3,green3,blue3,alpha)
- gl.Text("Master Volume: ".. masterVolume .."% CTRL & +/-",x1+5,y2+5+(widgetHeight*3))
- gl.Color(0.3,0.3,0.3,0.6*alpha) -- draws empty rectangles
- for i = 1,rectangles do
- local u1 = x1+(i-1)*boxwidth
- local u2= u1+boxwidth-boxspacing
- gl.Rect(u1,y1+(widgetHeight*3),u2,y2+(widgetHeight*3))
- end
- local vol2 = math.floor(masterVolume/(100/rectangles))
- gl.Color(0,0.8,0,alpha) -- draws filled rectangles
- for i = 1,vol2 do
- local u1 = x1+(i-1)*boxwidth
- local u2= u1+boxwidth-boxspacing
- gl.Color(red3,green3,blue3,alpha)
- gl.Rect(u1+1,y1+1+(widgetHeight*3),u1+2,y2-1+(widgetHeight*3))
- gl.Color(red3*1.2,green3*1.2,blue3*1.2,alpha)
- gl.Rect(u1+2,y1+1+(widgetHeight*3),u2-1,y2-1+(widgetHeight*3))
- end
- end
- end
- function widget:TweakDrawScreen()
- local y1 = widgetPosY
- local y2 = widgetPosY + widgetHeight
- local x1 = widgetPosX
- local x2 = widgetPosX + widgetWidth
- gl.Color(0,0,0.5,1)
- gl.Rect(x1-1,y1-1,x1,y2+1)
- gl.Rect(x2-1,y1-1,x2,y2+1)
- gl.Rect(x1-1,y1-1,x2,y1)
- gl.Rect(x1-1,y2,x2,y2+1)
- gl.Color(0.5,1,0.5,1)
- gl.Text("Music Volume: ".. musicVolume .. "%",x1+5,y2+5)
- gl.Color(0,0,0,0.2) -- draws empty rectangles
- for i = 1,40 do
- local u1 = x1+(i-1)*10
- local u2= u1+8
- gl.Rect(u1,y1,u2,y2)
- end
- local vol2 = math.floor(musicVolume/2.5)
- gl.Color(0,0.8,0,1) -- draws filled rectangles
- for i = 1,vol2 do
- local u1 = x1+(i-1)*10
- local u2= u1+8
- gl.Rect(u1+1,y1+1,u2-1,y2-1)
- end
- end
- function widget:TweakMouseMove(x,y,dx,dy,button)
- if pressedToMove then
- if moveStartX == nil then -- move widget on y axis
- moveStartX = x - widgetPosX
- end
- if moveStartY == nil then -- move widget on y axis
- moveStartY = y - widgetPosY
- end
- widgetPosX = widgetPosX + dx
- widgetPosY = widgetPosY + dy
- if widgetPosY <= 0 then
- widgetPosY = 0
- end
- if widgetPosY + widgetHeight >= vsy then
- widgetPosY = vsy - widgetHeight
- end
- if widgetPosX < 5 then
- widgetPosX = 5
- end
- if widgetPosX + widgetWidth + 5 > vsx then
- widgetPosX = vsx - widgetWidth - 5
- end
- end
- end
- function widget:TweakMousePress(x, y, button)
- if button == 1 then
- if IsOnButton(x,y,widgetPosX,widgetPosY,widgetPosX+widgetWidth,widgetPosY+widgetHeight) then
- pressedToMove = true
- return true
- end
- else
- return false
- end
- end
- function widget:TweakMouseRelease(x,y,button)
- pressedToMove = false
- end
- function IsOnButton(x, y, BLcornerX, BLcornerY,TRcornerX,TRcornerY)
- if BLcornerX == nil then return false end
- -- check if the mouse is in a rectangle
- return x >= BLcornerX and x <= TRcornerX
- and y >= BLcornerY
- and y <= TRcornerY
- end
- ----------------------------------------------------------------------------------------
- -- SAVE/LOAD --
- ----------------------------------------------------------------------------------------
- function widget:GetConfigData(data) -- save
- return {
- widgetPosX = widgetPosX,
- widgetPosY = widgetPosY,
- }
- end
- function widget:SetConfigData(data) -- load
- widgetPosX = data.widgetPosX or widgetPosX
- widgetPosY = data.widgetPosY or widgetPosY
- end
- ----------------------------------------------------------------------------------------
- -- Music Control --
- ----------------------------------------------------------------------------------------
- function JustTheName(text)
- local EndIndex=(string.find(text,".",string.len(text)-4,true) or 1+string.len(text))-1
- local BeginIndex=1
- repeat
- local NewBeginIndex=string.find(text,"/",BeginIndex,true) or string.find(text,"\\",BeginIndex,true)
- BeginIndex=NewBeginIndex and NewBeginIndex+1 or BeginIndex
- until not NewBeginIndex
- return string.sub(text,BeginIndex,EndIndex)
- end
- function widget:Shutdown()
- spStopSoundStream()
- end
- function widget:Update(dt)
- widgetTime = widgetTime + dt
- --update once per second
- if (widgetTime - lastupdate > 1) then
- lastupdate = widgetTime
- _, fullView, _ = spGetSpectatingState()
- if fullView then
- spec = true
- else
- spec = false
- end
- totalMetal = 0
- if spec then
- local teamUnits = spGetAllUnits()
- for u = 1, #teamUnits do
- local uID = teamUnits[u]
- local uDefID = spGetUnitDefID(uID)
- local _, _, _, _, buildProg = spGetUnitHealth(uID)
- local unitMetalCost = UnitDefs[uDefID].metalCost*buildProg
- totalMetal = totalMetal + unitMetalCost
- teamMetalTotal = totalMetal
- end
- if Spring.GetGaiaTeamID() then
- numPlayers = #spGetTeamList() - 1
- else
- numPlayers = #spGetTeamList()
- end
- --[[
- PEACE_VALUE = normalPEACE_VALUE / (numPlayers)
- WAR_VALUE = normalWAR_VALUE / (numPlayers)
- WAR_COOLDOWN = normalWAR_COOLDOWN /(numPlayers) --will be reset below so needs adapting
- --]]
- else --not spectator mode
- local teamUnits = spGetTeamUnits(spGetMyTeamID())
- for u = 1, #teamUnits do
- local uID = teamUnits[u]
- local uDefID = spGetUnitDefID(uID)
- local _, _, _, _, buildProg = spGetUnitHealth(uID)
- local unitMetalCost = UnitDefs[uDefID].metalCost*buildProg
- totalMetal = totalMetal + unitMetalCost
- teamMetalTotal = totalMetal
- end
- end
- local PlayedTime, TotalTime = spGetSoundStreamTime()
- PlayedTime=PlayedTime or 0
- TotalTime=TotalTime or 0
- if not LastTimer then
- LastTimer=spGetTimer()
- return
- end
- local Timer=spGetTimer()
- if spDiffTimers(Timer,LastTimer)>2 and (PlayedTime>=TotalTime-0.1 or PlayedTime==0) then
- LastTimer=Timer
- if LastPlayedTime==PlayedTime then
- playNew=true
- else
- LastPlayedTime=PlayedTime
- end
- end
- if (teamMetalTotal > 0) then
- if (metalDestroyedCounter > (teamMetalTotal * WAR_VALUE)) then
- musicType = 2 --war!
- if (metalDestroyedCounter > (2 * teamMetalTotal * WAR_VALUE)) and (curMusicType ~= 2) then
- playNew = true
- end
- end
- if (metalDestroyedCounter > (teamMetalTotal * PEACE_VALUE)) and (metalDestroyedCounter < (teamMetalTotal * WAR_VALUE)) then
- musicType = 1 --coldwar
- end
- if ((metalDestroyedCounter < teamMetalTotal * PEACE_VALUE)) then
- musicType = 0 --peace!
- end
- if (metalDestroyedCounter > (teamMetalTotal * WAR_VALUE)) then
- ratioMetal = metalDestroyedCounter / (teamMetalTotal * WAR_VALUE)
- COOLDOWNaccelerator = true
- WAR_COOLDOWN = normalWAR_COOLDOWN * ratioMetal * ratioMetal
- else
- COOLDOWNaccelerator = false
- WAR_COOLDOWN = normalWAR_COOLDOWN
- end
- end
- if playNew then
- if not warTracks or #warTracks<1 then
- Spring.Echo("\255\1\205\205No music found for War! Copy some .ogg files into \\music\\war")
- widgetHandler:RemoveWidget()
- elseif not coldwarTracks or #coldwarTracks<1 then
- Spring.Echo("\255\1\205\205No music found for Coldwar! Copy some .ogg files into \\music\\coldwar")
- widgetHandler:RemoveWidget()
- elseif not peaceTracks or #peaceTracks<1 then
- Spring.Echo("\255\1\205\205No music found for Peace! Copy some .ogg files into \\music\\peace")
- widgetHandler:RemoveWidget()
- else
- local x,y=spGetMouseState()
- local BetterRand=x+y+math.floor(99*(os.clock()%99999)+(99*(os.time())%99999))--+spGetDrawFrame()+math.random(0,999)
- --Pick random track that wasn't just played
- curWarTrack = warTracks[1+(BetterRand%#warTracks)]
- if #warTracks == 1 then
- curWarTrack = warTracks[1]
- end
- if #warTracks >= 2 then
- if prevWarTrack == curWarTrack then
- curWarTrack = warTracks[1+(BetterRand%#warTracks)]
- end
- end
- prevWarTrack = curWarTrack
- curColdwarTrack = coldwarTracks[1+(BetterRand%#coldwarTracks)]
- if #coldwarTracks == 1 then
- curColdwarTrack = coldwarTracks[1]
- end
- if #coldwarTracks >= 2 then
- if prevColdwarTrack == curColdwarTrack then
- curColdwarTrack = coldwarTracks[1+(BetterRand%#coldwarTracks)]
- end
- end
- prevColdwarTrack = curColdwarTrack
- curPeaceTrack = peaceTracks[1+(BetterRand%#peaceTracks)]
- if #peaceTracks == 1 then
- curPeaceTrack = peaceTracks[1]
- end
- if #peaceTracks >= 2 then
- if prevPeaceTrack == curPeaceTrack then
- curPeaceTrack = peaceTracks[1+(BetterRand%#peaceTracks)]
- end
- end
- prevPeaceTrack = curPeaceTrack
- spStopSoundStream()
- if musicType == 0 then
- Echo("\255\1\205\205Peace Music: "..JustTheName(curPeaceTrack)) --plays at a default level that doesn't seem to be affected by snd_ settings
- spPlaySoundStream(curPeaceTrack)
- Spring.SetConfigInt("snd_volmusic", musicVolume+1) --ensures music volume is adjusted from weird default
- Spring.SetConfigInt("snd_volmusic", musicVolume)
- curMusicType = 0
- elseif musicType == 1 then
- Echo("\255\1\205\205Coldwar Music: "..JustTheName(curColdwarTrack))
- spPlaySoundStream(curColdwarTrack)
- Spring.SetConfigInt("snd_volmusic", musicVolume+1) --ensures music volume is adjusted from weird default
- Spring.SetConfigInt("snd_volmusic", musicVolume)
- curMusicType = 1
- elseif musicType == 2 then
- Echo("\255\1\205\205War Music: "..JustTheName(curWarTrack))
- spPlaySoundStream(curWarTrack)
- Spring.SetConfigInt("snd_volmusic", musicVolume+1) --ensures music volume is adjusted from weird default
- Spring.SetConfigInt("snd_volmusic", musicVolume)
- curMusicType = 2
- end
- playNew=false
- end
- end
- local _,speed,paused = spGetGameSpeed()
- if(not paused) then
- metalDestroyedCounter = metalDestroyedCounter - (teamMetalTotal * WAR_COOLDOWN * speed)
- --clamp metal counter to positive values
- if (metalDestroyedCounter < 0) then
- metalDestroyedCounter = 0
- end
- end
- end
- end
- function widget:TextCommand(command)
- if (string.find(command, 'nextsong') == 1) then
- playNew = true
- elseif (string.find(command, 'debugmusic') == 1) and debug == false then
- debug = true
- elseif (string.find(command, 'debugmusic') == 1) and debug == true then
- debug = false
- end
- end
- ----------------------------------------------------------------------------------------
- -- Unit Callins --
- ----------------------------------------------------------------------------------------
- function widget:UnitDestroyed(unitID, unitDefID, teamID)
- if spec then
- unitHealth, unitMaxHealth, paralyzeProgress, captureProgress, buildProgress = spGetUnitHealth(unitID)
- metalDestroyedCounter = metalDestroyedCounter + UnitDefs[unitDefID].metalCost*buildProgress
- elseif teamID == spGetMyTeamID() then
- unitHealth, unitMaxHealth, paralyzeProgress, captureProgress, buildProgress = spGetUnitHealth(unitID)
- metalDestroyedCounter = metalDestroyedCounter + UnitDefs[unitDefID].metalCost*buildProgress
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement