Guest User

Battle Dynamic Music Player V1

a guest
May 18th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.28 KB | None | 0 0
  1.  
  2. function widget:GetInfo()
  3.     return {
  4.         name      = "Battle Dynamic Music Player V1",
  5.         desc      = "Plays .ogg music from music\* peace/coldwar/war folders according to the in-game action",
  6.         author    = "LEDZ",
  7.         date      = "May 19 2012",
  8.         license   = "GNU GPL v2",
  9.         layer     = 0,
  10.         enabled   = true
  11.     }
  12. end
  13.  
  14. ----------------------------------------------------------------------------------------[[
  15. --[[
  16. Readme:
  17. To use, put appropriately themed .ogg files in peace/coldwar/war folders within the music folder:
  18. Peace; calm, peaceful music
  19. Coldwar; more somber, ominous and suggestive of trouble
  20. War; Exciting music will work well here.
  21.  
  22. At a certain level past the war threshold, the music will immediately switch to war,
  23. so if there's a big fight, the war music will cut-in just as you would expect
  24.  
  25. Type /luaui nextsong to skip and /luaui debugmusic to see some information of the variables used to pick type of music
  26.  
  27. This widget has borrowed heavily from Jool's snd_volume_osd.lua
  28. Functionality of the music player is an improvement on zwzsg's music.lua and Vebyast's gui_OTA_music_adv.lua
  29. --]]
  30. ----------------------------------------------------------------------------------------
  31. -- Spring accelerators --
  32. ----------------------------------------------------------------------------------------            
  33.  
  34. local spGetGameSpeed = Spring.GetGameSpeed
  35. local spStopSoundStream = Spring.StopSoundStream
  36. local spPlaySoundStream = Spring.PlaySoundStream --setting volume here overrides and disables snd_volmusic
  37. local spGetSoundStreamTime = Spring.GetSoundStreamTime
  38. local spGetTimer = Spring.GetTimer
  39. local spDiffTimers = Spring.DiffTimers
  40. local spGetDrawFrame = Spring.GetDrawFrame
  41. local spGetMouseState = Spring.GetMouseState
  42. local spGetTeamUnits = Spring.GetTeamUnits
  43. local spGetPlayerRoster = Spring.GetPlayerRoster
  44. local spGetUnitDefID = Spring.GetUnitDefID
  45. local spIsReplay = Spring.IsReplay
  46. local spGetSpectatingState = Spring.GetSpectatingState
  47. local spGetMyTeamID = Spring.GetMyTeamID
  48. local spGetMyAllyTeamID = Spring.GetMyAllyTeamID
  49. local spGetMyPlayerID = Spring.GetMyPlayerID
  50. local spGetPlayerInfo = Spring.GetPlayerInfo
  51. local spGetTeamList = Spring.GetTeamList
  52. local spGetAllyTeamList = Spring.GetAllyTeamList
  53. local spGetAllUnits = Spring.GetAllUnits
  54. local spGetUnitHealth = Spring.GetUnitHealth
  55. local spGetTeamUnits = Spring.GetTeamUnits
  56. local Echo = Spring.Echo
  57.  
  58. ----------------------------------------------------------------------------------------
  59. -- Music Volume draw --
  60. ----------------------------------------------------------------------------------------
  61.  
  62. local musicVolume
  63. local TextDraw                  = gl.Text
  64. local vsx,vsy                   = gl.GetViewSizes()
  65. local widgetPosX                            = vsx/3
  66. local widgetPosY                            = vsy/6
  67. local pressedToMove                     = false
  68. local altdown
  69. local dt                                            = -1
  70. local drawDelay                             = 0
  71.  
  72. ----------------------------------------------------------------------------------------
  73. -- Music Volume Settings, configurable  --
  74. ----------------------------------------------------------------------------------------
  75.  
  76. local step                                  = 2 -- how many steps to change sound musicVolume on one key press
  77. local pluskey                               = 0x10E -- numpad+ (look in uikeys.txt in spring folder for key symbols)
  78. local minuskey                          = 0x10D -- numpad-                                                            
  79. local pluskey2                          = 0x02B -- +key (duplicate key for musicVolume+, set to same as primary to disable)
  80. local minuskey2                         = 0x02D -- -key  
  81. local dtime                                 = 3 --How long time the musicVolume display is drawn, in seconds
  82. local ftime                                 = 2.5 --How long time before the musicVolume display starts fading, in seconds
  83. local widgetWidth                       = vsx/2.5 -- in pixels (changed from 400)
  84. local widgetHeight                  = vsy/25.6 -- in pixels (changed from 40)
  85. local rectangles                        = 50 -- number of boxes in musicVolume bar
  86. local boxspacing                        = 2 -- space between boxes
  87. local red1                                  = 0 -- musicVolume bar colour, 0 to 1.
  88. local green1                                = 0.5 -- musicVolume bar colour, 0 to 1.
  89. local blue1                                 = 0.8 -- musicVolume bar colour, 0 to 1.
  90. local red2                                  = 1 -- generalVolume bar colour, 0 to 1.
  91. local green2                                = 0.2 -- generalVolume bar colour, 0 to 1.
  92. local blue2                                 = 0 -- generalVolume bar colour, 0 to 1.
  93. local red3                                  = 0.8 -- masterVolume bar colour, 0 to 1.
  94. local green3                                = 0.8 -- masterVolume bar colour, 0 to 1.
  95. local blue3                                 = 0 -- masterVolume bar colour, 0 to 1.
  96.  
  97. ----------------------------------------------------------------------------------------
  98. -- Dynamic Music --
  99. ----------------------------------------------------------------------------------------
  100. local normalPEACE_VALUE     = 0.0025    -- below this, peace
  101. local normalWAR_VALUE       = 0.075     -- above this, war
  102. local normalWAR_COOLDOWN    = .0005  -- rate at which metalDestroyedCounter goes down
  103. local PEACE_VALUE   = 0.0025
  104. local WAR_VALUE     = 0.075
  105. local WAR_COOLDOWN = 0.0005
  106.  
  107. local musicVolume                       = Spring.GetConfigInt("snd_volmusic")
  108. local generalVolume                     = Spring.GetConfigInt("snd_volgeneral")
  109. local masterVolume                      = Spring.GetConfigInt("snd_volmaster")
  110. local musicType                             = 0     -- 0 = peace, 1 = coldwar, 2 = war
  111. local metalDestroyedCounter     = 0
  112. local teamMetalTotal                    = 0
  113. local widgetTime                            = 0     -- Time elapsed since widget started, in seconds
  114. local lastupdate                            = 0     -- time at which last metal calculation occurred
  115. local ratioMetal                            = 1   -- how much the destroyed metal is above the war threshold, for dampening
  116. -- 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
  117. local spec                                      = false
  118. local debug                                     = false
  119. local numPlayers                            = {}
  120. local warTracks                         = {}
  121. local coldwarTracks                     = {}
  122. local peaceTracks                   = {}
  123. local CurrentTrack                      = nil
  124. local info                              = ""
  125. local LastTimer                         = nil
  126. local LastPlayedTime                    = 0
  127. local playNew                           = true
  128. local gameStarted                       = false
  129.  
  130. ----------------------------------------------------------------------------------------
  131. ----------------------------------------------------------------------------------------
  132. -- Intialize --
  133. ----------------------------------------------------------------------------------------
  134.  
  135. function widget:Initialize()  --loads .ogg files from the directories to table
  136.     for _,track in ipairs(VFS.DirList('music\\war\\','*.ogg')) do
  137.         table.insert(warTracks,track)
  138.     end
  139.     for _,track in ipairs(VFS.DirList('music\\coldwar\\','*.ogg')) do
  140.         table.insert(coldwarTracks,track)
  141.     end
  142.     for _,track in ipairs(VFS.DirList('music\\peace\\','*.ogg')) do
  143.         table.insert(peaceTracks,track)
  144.     end
  145.   Echo("\255\1\205\205Use alt/ctrl/shift + plus/minus keys to adjust volume")
  146.    
  147.     musicType = 0
  148.     curMusicType = 0
  149.     metalDestroyedCounter = 0
  150.     teamMetalTotal = 0
  151.     widgetTime = 0
  152.     lastupdate = 0
  153.  
  154. end
  155.  
  156. function widget:GameStart()
  157.     Echo("\255\1\205\205Skip song by typing /luaui nextsong")
  158. end
  159.  
  160. ----------------------------------------------------------------------------------------
  161. -- Volume Control --
  162. ----------------------------------------------------------------------------------------
  163.  
  164. function widget:KeyPress(key, mods, isRepeat)
  165.     if (key == pluskey or key == pluskey2) and mods.alt then -- KEY = Alt + pluskey
  166.         musicVolume = Spring.GetConfigInt("snd_volmusic")
  167.         musicVolume = musicVolume + step
  168.         if musicVolume < 0 then musicVolume = 0 end
  169.         if musicVolume > 100 then musicVolume = 100 end
  170.         Spring.SetConfigInt("snd_volmusic", musicVolume)
  171.         dt = os.clock()
  172.         return true
  173.        
  174.     elseif (key == minuskey or key == minuskey2) and mods.alt then -- KEY = Alt + minuskey
  175.         musicVolume = Spring.GetConfigInt("snd_volmusic")
  176.         musicVolume = musicVolume - step
  177.         if musicVolume < 0 then musicVolume = 0 end
  178.         if musicVolume > 100 then musicVolume = 100 end
  179.         Spring.SetConfigInt("snd_volmusic", musicVolume)
  180.         dt = os.clock()
  181.         return true
  182.        
  183.     elseif (key == pluskey or key == pluskey2) and mods.shift then -- KEY = Shift + pluskey
  184.         generalVolume = Spring.GetConfigInt("snd_volgeneral")
  185.         generalVolume = generalVolume + step
  186.         if generalVolume < 0 then generalVolume = 0 end
  187.         if generalVolume > 100 then generalVolume = 100 end
  188.         Spring.SetConfigInt("snd_general", generalVolume)
  189.         Spring.SetConfigInt("snd_volbattle", generalVolume)
  190.         Spring.SetConfigInt("snd_volgeneral", generalVolume)
  191.         Spring.SetConfigInt("snd_volui", generalVolume)
  192.         Spring.SetConfigInt("snd_volunitreply", generalVolume)
  193.         dt = os.clock()
  194.         return true
  195.        
  196.     elseif (key == minuskey or key == minuskey2) and mods.shift then -- KEY = Shift + minuskey
  197.         generalVolume = Spring.GetConfigInt("snd_volgeneral")
  198.         generalVolume = generalVolume - step
  199.         if generalVolume < 0 then generalVolume = 0 end
  200.         if generalVolume > 100 then generalVolume = 100 end
  201.         Spring.SetConfigInt("snd_general", generalVolume)
  202.         Spring.SetConfigInt("snd_volbattle", generalVolume)
  203.         Spring.SetConfigInt("snd_volgeneral", generalVolume)
  204.         Spring.SetConfigInt("snd_volui", generalVolume)
  205.         Spring.SetConfigInt("snd_volunitreply", generalVolume)
  206.         dt = os.clock()
  207.         return true
  208.        
  209.     elseif (key == pluskey or key == pluskey2) and mods.ctrl then -- KEY = Ctrl + pluskey
  210.         masterVolume = Spring.GetConfigInt("snd_volmaster")
  211.         masterVolume = masterVolume + step
  212.         if masterVolume < 0 then masterVolume = 0 end
  213.         if masterVolume > 100 then masterVolume = 100 end
  214.         Spring.SetConfigInt("snd_volmaster", masterVolume)
  215.         dt = os.clock()
  216.         return true
  217.        
  218.     elseif (key == minuskey or key == minuskey2) and mods.ctrl then -- KEY = Ctrl + minuskey
  219.         masterVolume = Spring.GetConfigInt("snd_volmaster")
  220.         masterVolume = masterVolume - step
  221.         if masterVolume < 0 then masterVolume = 0 end
  222.         if masterVolume > 100 then masterVolume = 100 end
  223.         Spring.SetConfigInt("snd_volmaster", masterVolume)
  224.         dt = os.clock()
  225.         return true
  226.        
  227.     elseif key == 0x134 then --ALT
  228.         altdown = true
  229.     end
  230.     return false
  231. end
  232.  
  233. function widget:KeyRelease(key)
  234.     if altdown and (key == pluskey or key == minuskey or key == pluskey2 or key == minuskey2) then
  235.         return true
  236.     elseif key == 0x134 then --ALT
  237.         altdown = false
  238.         return true
  239.     end
  240.     return false
  241. end
  242.  
  243. function widget:DrawScreen()
  244.     local y1 = widgetPosY
  245.     local y2 = widgetPosY + widgetHeight
  246.     local x1 = widgetPosX
  247.     local x2 = widgetPosX + widgetWidth
  248.     local ostime = os.clock()
  249.     local t = ostime - dt
  250.     local boxwidth = widgetWidth/rectangles
  251.     if debug then
  252.         gl.Color(0.5,1,0.5)
  253.         if spec then
  254.             gl.Text("num players: "..#numPlayers-1,x1-300,y2+160)
  255.         end
  256.         gl.Text("metalDestroyedCounter: "..metalDestroyedCounter,x1-300,y2+140)
  257.         gl.Text("teamMetalTotal: "..teamMetalTotal,x1-300,y2+120)
  258.         gl.Text("metalDestroyedCounter/teamMetalTotal: "..metalDestroyedCounter/teamMetalTotal,x1-300,y2+100)
  259.         gl.Text("musictype: "..musicType,x1-300,y2+80)
  260.         gl.Text("curmusictype: "..curMusicType,x1-300,y2+60)
  261.         gl.Text("WAR_COOLDOWN = "..WAR_COOLDOWN,x1-300,y2+40)
  262.         if COOLDOWNaccelerator then
  263.             gl.Color(1,0.3,0.3)
  264.             gl.Text("COOLDOWN accelerator: "..ratioMetal*ratioMetal,x1-300,y2+20)
  265.         else
  266.             gl.Color(0.5,1,0.5)
  267.             gl.Text("COOLDOWN accelerator: "..ratioMetal*ratioMetal,x1-300,y2+20)
  268.         end
  269.         gl.Color(0.5,1,0.5)
  270.         gl.Text("War%: "..100*metalDestroyedCounter/(teamMetalTotal*WAR_VALUE),x1-300,y2-0)
  271.     end
  272.     if t < dtime and dt >= 0 then --dtime = 3
  273.         local alpha
  274.         if t < ftime then --ftime = 2
  275.             alpha = 1
  276.         else
  277.             alpha = 3*(dtime-t)/dtime
  278.         end
  279. ---------------------------------------------
  280. --Music Volume
  281. ---------------------------------------------
  282.        
  283.         gl.Color(0,0,0,0.1*alpha)                              -- draws background rectangle
  284.         gl.Rect(x1,y1,x2-1,y2)
  285.         gl.Color(red1,green1,blue1,alpha)
  286.         musicVolume = Spring.GetConfigInt("snd_volmusic")
  287.         gl.Text("Music Volume: ".. musicVolume.."% ALT & +/-",x1+5,y2+5)
  288.         gl.Color(0.3,0.3,0.3,0.6*alpha)                              -- draws empty rectangles
  289.        
  290.         for i = 1,rectangles do
  291.             local u1 = x1+(i-1)*boxwidth
  292.             local u2= u1+boxwidth-boxspacing
  293.             gl.Rect(u1,y1,u2,y2)
  294.         end
  295.        
  296.         local vol1 = math.floor(musicVolume/(100/rectangles))
  297.         gl.Color(0,0.8,0,alpha)                              -- draws filled rectangles
  298.         for i = 1,vol1 do
  299.             local u1 = x1+(i-1)*boxwidth
  300.             local u2= u1+boxwidth-boxspacing
  301.             gl.Color(red1,green1,blue1,alpha)                              
  302.             gl.Rect(u1+1,y1+1,u1+2,y2-1)
  303.             gl.Color(red1*1.2,green1*1.2,blue1*1.2,alpha)                              
  304.             gl.Rect(u1+2,y1+1,u2-1,y2-1)
  305.         end
  306. ---------------------------------------------
  307. --General Volume
  308. ---------------------------------------------
  309.         gl.Color(0,0,0,0.1*alpha)                              -- draws background rectangle
  310.         gl.Rect(x1,y1+(widgetHeight*1.5),x2-1,y2+(widgetHeight*1.5))
  311.         gl.Color(red2,green2,blue2,alpha)
  312.         gl.Text("General Volume: ".. generalVolume .."% SHIFT & +/-",x1+5,y2+5+(widgetHeight*1.5))
  313.         gl.Color(0.3,0.3,0.3,0.6*alpha)                              -- draws empty rectangles
  314.        
  315.         for i = 1,rectangles do
  316.             local u1 = x1+(i-1)*boxwidth
  317.             local u2= u1+boxwidth-boxspacing
  318.             gl.Rect(u1,y1+(widgetHeight*1.5),u2,y2+(widgetHeight*1.5))
  319.         end
  320.        
  321.         local vol2 = math.floor(generalVolume/(100/rectangles))
  322.         gl.Color(0,0.8,0,alpha)                              -- draws filled rectangles
  323.         for i = 1,vol2 do
  324.             local u1 = x1+(i-1)*boxwidth
  325.             local u2= u1+boxwidth-boxspacing
  326.             gl.Color(red2,green2,blue2,alpha)                              
  327.             gl.Rect(u1+1,y1+1+(widgetHeight*1.5),u1+2,y2-1+(widgetHeight*1.5))
  328.             gl.Color(red2*1.2,green2*1.2,blue2*1.2,alpha)                              
  329.             gl.Rect(u1+2,y1+1+(widgetHeight*1.5),u2-1,y2-1+(widgetHeight*1.5))
  330.         end
  331. ---------------------------------------------
  332. --Master Volume
  333. ---------------------------------------------
  334.         gl.Color(0,0,0,0.1*alpha)                              -- draws background rectangle
  335.         gl.Rect(x1,y1+(widgetHeight*3),x2-1,y2+(widgetHeight*3))
  336.         gl.Color(red3,green3,blue3,alpha)
  337.         gl.Text("Master Volume: ".. masterVolume .."%  CTRL & +/-",x1+5,y2+5+(widgetHeight*3))
  338.         gl.Color(0.3,0.3,0.3,0.6*alpha) -- draws empty rectangles
  339.        
  340.         for i = 1,rectangles do
  341.             local u1 = x1+(i-1)*boxwidth
  342.             local u2= u1+boxwidth-boxspacing
  343.             gl.Rect(u1,y1+(widgetHeight*3),u2,y2+(widgetHeight*3))
  344.         end
  345.        
  346.         local vol2 = math.floor(masterVolume/(100/rectangles))
  347.         gl.Color(0,0.8,0,alpha) -- draws filled rectangles
  348.         for i = 1,vol2 do
  349.             local u1 = x1+(i-1)*boxwidth
  350.             local u2= u1+boxwidth-boxspacing
  351.             gl.Color(red3,green3,blue3,alpha)                              
  352.             gl.Rect(u1+1,y1+1+(widgetHeight*3),u1+2,y2-1+(widgetHeight*3))
  353.             gl.Color(red3*1.2,green3*1.2,blue3*1.2,alpha)                              
  354.             gl.Rect(u1+2,y1+1+(widgetHeight*3),u2-1,y2-1+(widgetHeight*3))
  355.         end
  356.     end
  357. end
  358.  
  359. function widget:TweakDrawScreen()
  360.     local y1 = widgetPosY
  361.     local y2 = widgetPosY + widgetHeight
  362.     local x1 = widgetPosX
  363.     local x2 = widgetPosX + widgetWidth
  364.     gl.Color(0,0,0.5,1)
  365.     gl.Rect(x1-1,y1-1,x1,y2+1)
  366.     gl.Rect(x2-1,y1-1,x2,y2+1)
  367.     gl.Rect(x1-1,y1-1,x2,y1)
  368.     gl.Rect(x1-1,y2,x2,y2+1)
  369.     gl.Color(0.5,1,0.5,1)
  370.     gl.Text("Music Volume: ".. musicVolume .. "%",x1+5,y2+5)
  371.     gl.Color(0,0,0,0.2) -- draws empty rectangles
  372.     for i = 1,40 do
  373.         local u1 = x1+(i-1)*10
  374.         local u2= u1+8
  375.         gl.Rect(u1,y1,u2,y2)
  376.     end
  377.    
  378.     local vol2 = math.floor(musicVolume/2.5)
  379.     gl.Color(0,0.8,0,1) -- draws filled rectangles
  380.     for i = 1,vol2 do
  381.         local u1 = x1+(i-1)*10
  382.         local u2= u1+8
  383.         gl.Rect(u1+1,y1+1,u2-1,y2-1)
  384.     end
  385. end
  386.  
  387. function widget:TweakMouseMove(x,y,dx,dy,button)
  388.         if pressedToMove then
  389.         if moveStartX == nil then     -- move widget on y axis
  390.             moveStartX = x - widgetPosX
  391.         end
  392.         if moveStartY == nil then     -- move widget on y axis
  393.             moveStartY = y - widgetPosY
  394.         end
  395.         widgetPosX = widgetPosX + dx
  396.         widgetPosY = widgetPosY + dy
  397.  
  398.         if widgetPosY <= 0 then
  399.             widgetPosY = 0
  400.         end
  401.         if widgetPosY + widgetHeight >= vsy then
  402.             widgetPosY = vsy - widgetHeight
  403.         end
  404.         if widgetPosX < 5 then
  405.             widgetPosX = 5
  406.         end
  407.         if widgetPosX + widgetWidth + 5 > vsx then
  408.             widgetPosX = vsx - widgetWidth - 5
  409.         end
  410.     end
  411.    
  412. end
  413.  
  414. function widget:TweakMousePress(x, y, button)
  415.     if button == 1 then
  416.         if IsOnButton(x,y,widgetPosX,widgetPosY,widgetPosX+widgetWidth,widgetPosY+widgetHeight) then
  417.             pressedToMove = true
  418.             return true
  419.         end
  420.     else
  421.         return false
  422.     end
  423. end
  424.  
  425. function widget:TweakMouseRelease(x,y,button)
  426.     pressedToMove = false                                            
  427. end
  428.  
  429. function IsOnButton(x, y, BLcornerX, BLcornerY,TRcornerX,TRcornerY)
  430.     if BLcornerX == nil then return false end
  431.     -- check if the mouse is in a rectangle
  432.  
  433.     return x >= BLcornerX and x <= TRcornerX
  434.                           and y >= BLcornerY
  435.                           and y <= TRcornerY
  436.  
  437. end
  438.  
  439. ----------------------------------------------------------------------------------------
  440. -- SAVE/LOAD --
  441. ----------------------------------------------------------------------------------------
  442.  
  443. function widget:GetConfigData(data)      -- save
  444.     return {
  445.         widgetPosX         = widgetPosX,
  446.         widgetPosY         = widgetPosY,
  447.     }
  448. end
  449.  
  450. function widget:SetConfigData(data)      -- load
  451.     widgetPosX          = data.widgetPosX or widgetPosX
  452.     widgetPosY          = data.widgetPosY or widgetPosY
  453. end
  454. ----------------------------------------------------------------------------------------
  455. -- Music Control --
  456. ----------------------------------------------------------------------------------------
  457.    
  458. function JustTheName(text)
  459.     local EndIndex=(string.find(text,".",string.len(text)-4,true) or 1+string.len(text))-1
  460.     local BeginIndex=1
  461.     repeat
  462.         local NewBeginIndex=string.find(text,"/",BeginIndex,true) or string.find(text,"\\",BeginIndex,true)
  463.         BeginIndex=NewBeginIndex and NewBeginIndex+1 or BeginIndex
  464.     until not NewBeginIndex
  465.     return string.sub(text,BeginIndex,EndIndex)
  466. end
  467.  
  468. function widget:Shutdown()
  469.     spStopSoundStream()                                          
  470. end
  471.  
  472. function widget:Update(dt)
  473.     widgetTime = widgetTime + dt
  474.     --update once per second
  475.     if (widgetTime - lastupdate > 1) then
  476.         lastupdate = widgetTime
  477.         _, fullView, _ = spGetSpectatingState()
  478.         if fullView then
  479.             spec = true
  480.         else
  481.             spec = false
  482.         end
  483.         totalMetal = 0
  484.         local teamUnits = spGetTeamUnits(spGetMyTeamID())
  485.         for u = 1, #teamUnits do
  486.             local uID = teamUnits[u]
  487.             local uDefID = spGetUnitDefID(uID)
  488.             local _, _, _, _, buildProg = spGetUnitHealth(uID)
  489.             local unitMetalCost = UnitDefs[uDefID].metalCost*buildProg
  490.             totalMetal = totalMetal + unitMetalCost
  491.             teamMetalTotal = totalMetal
  492.         end
  493. --[[               
  494.         if spec then
  495.             numPlayers = spGetTeamList()
  496.  
  497.             PEACE_VALUE = normalPEACE_VALUE / (#numPlayers)
  498.             WAR_VALUE = normalWAR_VALUE / (#numPlayers)
  499.             WAR_COOLDOWN = normalWAR_COOLDOWN /(#numPlayers) --will be reset below so needs adapting
  500.  
  501.         end
  502. --]]
  503.         local PlayedTime, TotalTime = spGetSoundStreamTime()
  504.         PlayedTime=PlayedTime or 0
  505.         TotalTime=TotalTime or 0
  506.         if not LastTimer then
  507.          LastTimer=spGetTimer()
  508.          return
  509.         end
  510.         local Timer=spGetTimer()
  511.         if spDiffTimers(Timer,LastTimer)>2 and (PlayedTime>=TotalTime-0.1 or PlayedTime==0) then
  512.             LastTimer=Timer
  513.             if LastPlayedTime==PlayedTime then
  514.                 playNew=true
  515.             else
  516.                 LastPlayedTime=PlayedTime
  517.             end
  518.         end
  519.         if (teamMetalTotal > 0) then
  520.             if (metalDestroyedCounter > (teamMetalTotal * WAR_VALUE)) and (curMusicType ~= 2) then
  521.                 musicType = 2   --war!
  522.                 if (metalDestroyedCounter > (2 * teamMetalTotal * WAR_VALUE)) then
  523.                     playNew = true
  524.                 end
  525.             end
  526.            
  527.             if (metalDestroyedCounter > (teamMetalTotal * PEACE_VALUE)) and (metalDestroyedCounter < (teamMetalTotal * WAR_VALUE)) then
  528.                 musicType = 1   --coldwar
  529.             end
  530.        
  531.             if ((metalDestroyedCounter < teamMetalTotal * PEACE_VALUE)) then
  532.                 musicType = 0   --peace!
  533.             end
  534.            
  535.             if (metalDestroyedCounter > (teamMetalTotal * WAR_VALUE)) then
  536.                 ratioMetal = metalDestroyedCounter / (teamMetalTotal * WAR_VALUE)
  537.                 COOLDOWNaccelerator = true
  538.                 WAR_COOLDOWN = normalWAR_COOLDOWN * ratioMetal * ratioMetal
  539.             else
  540.                 COOLDOWNaccelerator = false
  541.                 WAR_COOLDOWN = normalWAR_COOLDOWN
  542.             end
  543.         end
  544.        
  545.         if playNew then
  546.             if not warTracks or #warTracks<1 then
  547.                 Spring.Echo("\255\1\205\205No music found for War! Copy some .ogg files into \\music\\war")
  548.                 widgetHandler:RemoveWidget()
  549.             elseif not coldwarTracks or #coldwarTracks<1 then                                                              
  550.                 Spring.Echo("\255\1\205\205No music found for Coldwar! Copy some .ogg files into \\music\\coldwar")
  551.                 widgetHandler:RemoveWidget()
  552.             elseif not peaceTracks or #peaceTracks<1 then
  553.                 Spring.Echo("\255\1\205\205No music found for Peace! Copy some .ogg files into \\music\\peace")
  554.                 widgetHandler:RemoveWidget()                          
  555.             else
  556.                 local x,y=spGetMouseState()
  557.                 local BetterRand=x+y+math.floor(99*(os.clock()%99999)+(99*(os.time())%99999))--+spGetDrawFrame()+math.random(0,999)
  558.                 --Pick random track that wasn't just played
  559.                
  560.                 curWarTrack = warTracks[1+(BetterRand%#warTracks)]
  561.                 if #warTracks == 1 then
  562.                     curWarTrack = warTracks[1]
  563.                 end
  564.                 if #warTracks >= 2 then
  565.                     if prevWarTrack == curWarTrack then
  566.                         curWarTrack = warTracks[1+(BetterRand%#warTracks)]
  567.                     end
  568.                 end
  569.                 prevWarTrack = curWarTrack
  570.                
  571.                 curColdwarTrack = coldwarTracks[1+(BetterRand%#coldwarTracks)]
  572.                 if #coldwarTracks == 1 then
  573.                     curColdwarTrack = coldwarTracks[1]
  574.                 end
  575.                 if #coldwarTracks >= 2 then
  576.                     if prevColdwarTrack == curColdwarTrack then
  577.                         curColdwarTrack = coldwarTracks[1+(BetterRand%#coldwarTracks)]
  578.                     end
  579.                 end
  580.                 prevColdwarTrack = curColdwarTrack
  581.                
  582.                 curPeaceTrack = peaceTracks[1+(BetterRand%#peaceTracks)]
  583.                 if #peaceTracks == 1 then
  584.                     curPeaceTrack = peaceTracks[1]
  585.                 end
  586.                 if #peaceTracks >= 2 then
  587.                     if prevPeaceTrack == curPeaceTrack then
  588.                         curPeaceTrack = peaceTracks[1+(BetterRand%#peaceTracks)]
  589.                     end
  590.                 end
  591.                 prevPeaceTrack = curPeaceTrack
  592.                
  593.                 spStopSoundStream()
  594.                 if musicType == 0 then
  595.                     Echo("\255\1\205\205Peace Music: "..JustTheName(curPeaceTrack)) --plays at a default level that doesn't seem to be affected by snd_ settings
  596.                     spPlaySoundStream(curPeaceTrack)
  597.                     Spring.SetConfigInt("snd_volmusic", musicVolume+1) --ensures music volume is adjusted from weird default
  598.                     Spring.SetConfigInt("snd_volmusic", musicVolume)
  599.                     curMusicType = 0
  600.                 elseif musicType == 1 then
  601.                     Echo("\255\1\205\205Coldwar Music: "..JustTheName(curColdwarTrack))
  602.                     spPlaySoundStream(curColdwarTrack)
  603.                     Spring.SetConfigInt("snd_volmusic", musicVolume+1) --ensures music volume is adjusted from weird default
  604.                     Spring.SetConfigInt("snd_volmusic", musicVolume)
  605.                     curMusicType = 1
  606.                 elseif musicType == 2 then
  607.                     Echo("\255\1\205\205War Music: "..JustTheName(curWarTrack))
  608.                     spPlaySoundStream(curWarTrack) 
  609.                     Spring.SetConfigInt("snd_volmusic", musicVolume+1) --ensures music volume is adjusted from weird default
  610.                     Spring.SetConfigInt("snd_volmusic", musicVolume)
  611.                     curMusicType = 2
  612.                 end
  613.                 playNew=false
  614.             end
  615.         end
  616.            
  617.         local _,speed,paused = spGetGameSpeed()
  618.         if(not paused) then
  619.             metalDestroyedCounter = metalDestroyedCounter - (teamMetalTotal * WAR_COOLDOWN * speed)
  620.             --clamp metal counter to positive values
  621.             if (metalDestroyedCounter < 0) then
  622.                 metalDestroyedCounter = 0
  623.             end
  624.         end
  625.        
  626.     end
  627. end
  628.  
  629. function widget:TextCommand(command)
  630.   if (string.find(command, 'nextsong') == 1) then
  631.     playNew = true
  632.   elseif (string.find(command, 'debugmusic') == 1) and debug == false then
  633.     debug = true
  634.   elseif (string.find(command, 'debugmusic') == 1) and debug == true then
  635.     debug = false
  636.     end
  637. end
  638.  
  639. ----------------------------------------------------------------------------------------
  640. -- Unit Callins --
  641. ----------------------------------------------------------------------------------------
  642.  
  643. function widget:UnitDestroyed(unitID, unitDefID, teamID)
  644.     if spec then
  645.         unitHealth, unitMaxHealth, paralyzeProgress, captureProgress, buildProgress = spGetUnitHealth(unitID)
  646.         metalDestroyedCounter = metalDestroyedCounter + UnitDefs[unitDefID].metalCost*buildProgress
  647.     elseif teamID == spGetMyTeamID then
  648.     unitHealth, unitMaxHealth, paralyzeProgress, captureProgress, buildProgress = spGetUnitHealth(unitID)
  649.     metalDestroyedCounter = metalDestroyedCounter + UnitDefs[unitDefID].metalCost*buildProgress
  650.     end
  651. end
Advertisement
Add Comment
Please, Sign In to add comment