Advertisement
Guest User

Battle Dynamic Music Player V1.1

a guest
May 20th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.24 KB | None | 0 0
  1.  
  2. function widget:GetInfo()
  3.     return {
  4.         name      = "Battle Dynamic Music Player V1.1",
  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.         fontHandler.UseFont("LuaUI/Fonts/FreeSansBold_14")
  253.         gl.Color(0.87,0.74,0)
  254.         if spec then
  255.             fontHandler.Draw("Spectator Mode",x1-300,y2+280)
  256.             fontHandler.Draw("Number of players: "..numPlayers,x1-300,y2+260)
  257.         end
  258.         fontHandler.Draw("metalDestroyedCounter: "..metalDestroyedCounter,x1-300,y2+240)
  259.         fontHandler.Draw("teamMetalTotal: "..teamMetalTotal,x1-300,y2+220)
  260.         fontHandler.Draw("metalDestroyedCounter/teamMetalTotal: "..metalDestroyedCounter/teamMetalTotal,x1-300,y2+200)
  261.         if musicType == 0 then
  262.             fontHandler.Draw("Next Music Type: Peace",x1-300,y2+180)
  263.         elseif musicType == 1 then
  264.             fontHandler.Draw("Next Music Type: Coldwar",x1-300,y2+180)
  265.         else
  266.             fontHandler.Draw("Next Music Type: War",x1-300,y2+180)
  267.         end
  268.         if curMusicType == 0 then
  269.             fontHandler.Draw("Current Music Type: Peace",x1-300,y2+160)
  270.         elseif curMusicType == 1 then
  271.             fontHandler.Draw("Current Music Type: Coldwar",x1-300,y2+160)
  272.         else
  273.             fontHandler.Draw("Current Music Type: War",x1-300,y2+160)
  274.         end    
  275.         fontHandler.Draw("WAR_COOLDOWN = "..WAR_COOLDOWN,x1-300,y2+140)
  276.         if COOLDOWNaccelerator then
  277.             gl.Color(1,0.3,0.3)
  278.             fontHandler.Draw("COOLDOWN accelerator: "..ratioMetal*ratioMetal,x1-300,y2+120)
  279.         else
  280.             gl.Color(0.87,0.74,0)
  281.             fontHandler.Draw("COOLDOWN accelerator: "..ratioMetal*ratioMetal,x1-300,y2+120)
  282.         end
  283.         gl.Color(0.87,0.74,0)
  284.         fontHandler.Draw("War%: "..100*metalDestroyedCounter/(teamMetalTotal*WAR_VALUE),x1-300,y2+100)
  285. --[[
  286.         gl.Color(0.5,1,0.5)
  287.         if spec then
  288.             gl.Text("Spectator Mode",x1-300,y2+180)
  289.             gl.Text("Number of players: "..numPlayers,x1-300,y2+160)
  290.         end
  291.         gl.Text("metalDestroyedCounter: "..metalDestroyedCounter,x1-300,y2+140)
  292.         gl.Text("teamMetalTotal: "..teamMetalTotal,x1-300,y2+120)
  293.         gl.Text("metalDestroyedCounter/teamMetalTotal: "..metalDestroyedCounter/teamMetalTotal,x1-300,y2+100)
  294.         gl.Text("musictype: "..musicType,x1-300,y2+80)
  295.         gl.Text("curmusictype: "..curMusicType,x1-300,y2+60)
  296.         gl.Text("WAR_COOLDOWN = "..WAR_COOLDOWN,x1-300,y2+40)
  297.         if COOLDOWNaccelerator then
  298.             gl.Color(1,0.3,0.3)
  299.             gl.Text("COOLDOWN accelerator: "..ratioMetal*ratioMetal,x1-300,y2+20)
  300.         else
  301.             gl.Color(0.5,1,0.5)
  302.             gl.Text("COOLDOWN accelerator: "..ratioMetal*ratioMetal,x1-300,y2+20)
  303.         end
  304.         gl.Color(0.5,1,0.5)
  305.         gl.Text("War%: "..100*metalDestroyedCounter/(teamMetalTotal*WAR_VALUE),x1-300,y2-0)
  306.         --]]
  307.     end
  308.     if t < dtime and dt >= 0 then --dtime = 3
  309.         local alpha
  310.         if t < ftime then --ftime = 2
  311.             alpha = 1
  312.         else
  313.             alpha = 3*(dtime-t)/dtime
  314.         end
  315. ---------------------------------------------
  316. --Music Volume
  317. ---------------------------------------------
  318.        
  319.         gl.Color(0,0,0,0.1*alpha)                              -- draws background rectangle
  320.         gl.Rect(x1,y1,x2-1,y2)
  321.         gl.Color(red1,green1,blue1,alpha)
  322.         musicVolume = Spring.GetConfigInt("snd_volmusic")
  323.         gl.Text("Music Volume: ".. musicVolume.."% ALT & +/-",x1+5,y2+5)
  324.         gl.Color(0.3,0.3,0.3,0.6*alpha)                              -- draws empty rectangles
  325.        
  326.         for i = 1,rectangles do
  327.             local u1 = x1+(i-1)*boxwidth
  328.             local u2= u1+boxwidth-boxspacing
  329.             gl.Rect(u1,y1,u2,y2)
  330.         end
  331.        
  332.         local vol1 = math.floor(musicVolume/(100/rectangles))
  333.         gl.Color(0,0.8,0,alpha)                              -- draws filled rectangles
  334.         for i = 1,vol1 do
  335.             local u1 = x1+(i-1)*boxwidth
  336.             local u2= u1+boxwidth-boxspacing
  337.             gl.Color(red1,green1,blue1,alpha)                              
  338.             gl.Rect(u1+1,y1+1,u1+2,y2-1)
  339.             gl.Color(red1*1.2,green1*1.2,blue1*1.2,alpha)                              
  340.             gl.Rect(u1+2,y1+1,u2-1,y2-1)
  341.         end
  342. ---------------------------------------------
  343. --General Volume
  344. ---------------------------------------------
  345.         gl.Color(0,0,0,0.1*alpha)                              -- draws background rectangle
  346.         gl.Rect(x1,y1+(widgetHeight*1.5),x2-1,y2+(widgetHeight*1.5))
  347.         gl.Color(red2,green2,blue2,alpha)
  348.         gl.Text("General Volume: ".. generalVolume .."% SHIFT & +/-",x1+5,y2+5+(widgetHeight*1.5))
  349.         gl.Color(0.3,0.3,0.3,0.6*alpha)                              -- draws empty rectangles
  350.        
  351.         for i = 1,rectangles do
  352.             local u1 = x1+(i-1)*boxwidth
  353.             local u2= u1+boxwidth-boxspacing
  354.             gl.Rect(u1,y1+(widgetHeight*1.5),u2,y2+(widgetHeight*1.5))
  355.         end
  356.        
  357.         local vol2 = math.floor(generalVolume/(100/rectangles))
  358.         gl.Color(0,0.8,0,alpha)                              -- draws filled rectangles
  359.         for i = 1,vol2 do
  360.             local u1 = x1+(i-1)*boxwidth
  361.             local u2= u1+boxwidth-boxspacing
  362.             gl.Color(red2,green2,blue2,alpha)                              
  363.             gl.Rect(u1+1,y1+1+(widgetHeight*1.5),u1+2,y2-1+(widgetHeight*1.5))
  364.             gl.Color(red2*1.2,green2*1.2,blue2*1.2,alpha)                              
  365.             gl.Rect(u1+2,y1+1+(widgetHeight*1.5),u2-1,y2-1+(widgetHeight*1.5))
  366.         end
  367. ---------------------------------------------
  368. --Master Volume
  369. ---------------------------------------------
  370.         gl.Color(0,0,0,0.1*alpha)                              -- draws background rectangle
  371.         gl.Rect(x1,y1+(widgetHeight*3),x2-1,y2+(widgetHeight*3))
  372.         gl.Color(red3,green3,blue3,alpha)
  373.         gl.Text("Master Volume: ".. masterVolume .."%  CTRL & +/-",x1+5,y2+5+(widgetHeight*3))
  374.         gl.Color(0.3,0.3,0.3,0.6*alpha) -- draws empty rectangles
  375.        
  376.         for i = 1,rectangles do
  377.             local u1 = x1+(i-1)*boxwidth
  378.             local u2= u1+boxwidth-boxspacing
  379.             gl.Rect(u1,y1+(widgetHeight*3),u2,y2+(widgetHeight*3))
  380.         end
  381.        
  382.         local vol2 = math.floor(masterVolume/(100/rectangles))
  383.         gl.Color(0,0.8,0,alpha) -- draws filled rectangles
  384.         for i = 1,vol2 do
  385.             local u1 = x1+(i-1)*boxwidth
  386.             local u2= u1+boxwidth-boxspacing
  387.             gl.Color(red3,green3,blue3,alpha)                              
  388.             gl.Rect(u1+1,y1+1+(widgetHeight*3),u1+2,y2-1+(widgetHeight*3))
  389.             gl.Color(red3*1.2,green3*1.2,blue3*1.2,alpha)                              
  390.             gl.Rect(u1+2,y1+1+(widgetHeight*3),u2-1,y2-1+(widgetHeight*3))
  391.         end
  392.     end
  393. end
  394.  
  395. function widget:TweakDrawScreen()
  396.     local y1 = widgetPosY
  397.     local y2 = widgetPosY + widgetHeight
  398.     local x1 = widgetPosX
  399.     local x2 = widgetPosX + widgetWidth
  400.     gl.Color(0,0,0.5,1)
  401.     gl.Rect(x1-1,y1-1,x1,y2+1)
  402.     gl.Rect(x2-1,y1-1,x2,y2+1)
  403.     gl.Rect(x1-1,y1-1,x2,y1)
  404.     gl.Rect(x1-1,y2,x2,y2+1)
  405.     gl.Color(0.5,1,0.5,1)
  406.     gl.Text("Music Volume: ".. musicVolume .. "%",x1+5,y2+5)
  407.     gl.Color(0,0,0,0.2) -- draws empty rectangles
  408.     for i = 1,40 do
  409.         local u1 = x1+(i-1)*10
  410.         local u2= u1+8
  411.         gl.Rect(u1,y1,u2,y2)
  412.     end
  413.    
  414.     local vol2 = math.floor(musicVolume/2.5)
  415.     gl.Color(0,0.8,0,1) -- draws filled rectangles
  416.     for i = 1,vol2 do
  417.         local u1 = x1+(i-1)*10
  418.         local u2= u1+8
  419.         gl.Rect(u1+1,y1+1,u2-1,y2-1)
  420.     end
  421. end
  422.  
  423. function widget:TweakMouseMove(x,y,dx,dy,button)
  424.         if pressedToMove then
  425.         if moveStartX == nil then     -- move widget on y axis
  426.             moveStartX = x - widgetPosX
  427.         end
  428.         if moveStartY == nil then     -- move widget on y axis
  429.             moveStartY = y - widgetPosY
  430.         end
  431.         widgetPosX = widgetPosX + dx
  432.         widgetPosY = widgetPosY + dy
  433.  
  434.         if widgetPosY <= 0 then
  435.             widgetPosY = 0
  436.         end
  437.         if widgetPosY + widgetHeight >= vsy then
  438.             widgetPosY = vsy - widgetHeight
  439.         end
  440.         if widgetPosX < 5 then
  441.             widgetPosX = 5
  442.         end
  443.         if widgetPosX + widgetWidth + 5 > vsx then
  444.             widgetPosX = vsx - widgetWidth - 5
  445.         end
  446.     end
  447.    
  448. end
  449.  
  450. function widget:TweakMousePress(x, y, button)
  451.     if button == 1 then
  452.         if IsOnButton(x,y,widgetPosX,widgetPosY,widgetPosX+widgetWidth,widgetPosY+widgetHeight) then
  453.             pressedToMove = true
  454.             return true
  455.         end
  456.     else
  457.         return false
  458.     end
  459. end
  460.  
  461. function widget:TweakMouseRelease(x,y,button)
  462.     pressedToMove = false                                            
  463. end
  464.  
  465. function IsOnButton(x, y, BLcornerX, BLcornerY,TRcornerX,TRcornerY)
  466.     if BLcornerX == nil then return false end
  467.     -- check if the mouse is in a rectangle
  468.  
  469.     return x >= BLcornerX and x <= TRcornerX
  470.                           and y >= BLcornerY
  471.                           and y <= TRcornerY
  472.  
  473. end
  474.  
  475. ----------------------------------------------------------------------------------------
  476. -- SAVE/LOAD --
  477. ----------------------------------------------------------------------------------------
  478.  
  479. function widget:GetConfigData(data)      -- save
  480.     return {
  481.         widgetPosX         = widgetPosX,
  482.         widgetPosY         = widgetPosY,
  483.     }
  484. end
  485.  
  486. function widget:SetConfigData(data)      -- load
  487.     widgetPosX          = data.widgetPosX or widgetPosX
  488.     widgetPosY          = data.widgetPosY or widgetPosY
  489. end
  490. ----------------------------------------------------------------------------------------
  491. -- Music Control --
  492. ----------------------------------------------------------------------------------------
  493.    
  494. function JustTheName(text)
  495.     local EndIndex=(string.find(text,".",string.len(text)-4,true) or 1+string.len(text))-1
  496.     local BeginIndex=1
  497.     repeat
  498.         local NewBeginIndex=string.find(text,"/",BeginIndex,true) or string.find(text,"\\",BeginIndex,true)
  499.         BeginIndex=NewBeginIndex and NewBeginIndex+1 or BeginIndex
  500.     until not NewBeginIndex
  501.     return string.sub(text,BeginIndex,EndIndex)
  502. end
  503.  
  504. function widget:Shutdown()
  505.     spStopSoundStream()                                          
  506. end
  507.  
  508. function widget:Update(dt)
  509.     widgetTime = widgetTime + dt
  510.     --update once per second
  511.     if (widgetTime - lastupdate > 1) then
  512.         lastupdate = widgetTime
  513.         _, fullView, _ = spGetSpectatingState()
  514.         if fullView then
  515.             spec = true
  516.         else
  517.             spec = false
  518.         end
  519.         totalMetal = 0
  520.  
  521.                
  522.         if spec then
  523.             local teamUnits = spGetAllUnits()
  524.             for u = 1, #teamUnits do
  525.                 local uID = teamUnits[u]
  526.                 local uDefID = spGetUnitDefID(uID)
  527.                 local _, _, _, _, buildProg = spGetUnitHealth(uID)
  528.                 local unitMetalCost = UnitDefs[uDefID].metalCost*buildProg
  529.                 totalMetal = totalMetal + unitMetalCost
  530.                 teamMetalTotal = totalMetal
  531.             end
  532.             if Spring.GetGaiaTeamID() then
  533.                 numPlayers = #spGetTeamList() - 1
  534.             else
  535.                 numPlayers = #spGetTeamList()
  536.             end
  537.            
  538. --[[
  539.             PEACE_VALUE = normalPEACE_VALUE / (numPlayers)
  540.             WAR_VALUE = normalWAR_VALUE / (numPlayers)
  541.             WAR_COOLDOWN = normalWAR_COOLDOWN /(numPlayers) --will be reset below so needs adapting
  542. --]]
  543.         else --not spectator mode
  544.             local teamUnits = spGetTeamUnits(spGetMyTeamID())
  545.             for u = 1, #teamUnits do
  546.                 local uID = teamUnits[u]
  547.                 local uDefID = spGetUnitDefID(uID)
  548.                 local _, _, _, _, buildProg = spGetUnitHealth(uID)
  549.                 local unitMetalCost = UnitDefs[uDefID].metalCost*buildProg
  550.                 totalMetal = totalMetal + unitMetalCost
  551.                 teamMetalTotal = totalMetal
  552.             end
  553.         end
  554.  
  555.         local PlayedTime, TotalTime = spGetSoundStreamTime()
  556.         PlayedTime=PlayedTime or 0
  557.         TotalTime=TotalTime or 0
  558.         if not LastTimer then
  559.          LastTimer=spGetTimer()
  560.          return
  561.         end
  562.         local Timer=spGetTimer()
  563.         if spDiffTimers(Timer,LastTimer)>2 and (PlayedTime>=TotalTime-0.1 or PlayedTime==0) then
  564.             LastTimer=Timer
  565.             if LastPlayedTime==PlayedTime then
  566.                 playNew=true
  567.             else
  568.                 LastPlayedTime=PlayedTime
  569.             end
  570.         end
  571.         if (teamMetalTotal > 0) then
  572.             if (metalDestroyedCounter > (teamMetalTotal * WAR_VALUE)) then
  573.                 musicType = 2   --war!
  574.                 if (metalDestroyedCounter > (2 * teamMetalTotal * WAR_VALUE)) and (curMusicType ~= 2) then
  575.                     playNew = true
  576.                 end
  577.             end
  578.            
  579.             if (metalDestroyedCounter > (teamMetalTotal * PEACE_VALUE)) and (metalDestroyedCounter < (teamMetalTotal * WAR_VALUE)) then
  580.                 musicType = 1   --coldwar
  581.             end
  582.        
  583.             if ((metalDestroyedCounter < teamMetalTotal * PEACE_VALUE)) then
  584.                 musicType = 0   --peace!
  585.             end
  586.            
  587.             if (metalDestroyedCounter > (teamMetalTotal * WAR_VALUE)) then
  588.                 ratioMetal = metalDestroyedCounter / (teamMetalTotal * WAR_VALUE)
  589.                 COOLDOWNaccelerator = true
  590.                 WAR_COOLDOWN = normalWAR_COOLDOWN * ratioMetal * ratioMetal
  591.             else
  592.                 COOLDOWNaccelerator = false
  593.                 WAR_COOLDOWN = normalWAR_COOLDOWN
  594.             end
  595.         end
  596.        
  597.         if playNew then
  598.             if not warTracks or #warTracks<1 then
  599.                 Spring.Echo("\255\1\205\205No music found for War! Copy some .ogg files into \\music\\war")
  600.                 widgetHandler:RemoveWidget()
  601.             elseif not coldwarTracks or #coldwarTracks<1 then                                                              
  602.                 Spring.Echo("\255\1\205\205No music found for Coldwar! Copy some .ogg files into \\music\\coldwar")
  603.                 widgetHandler:RemoveWidget()
  604.             elseif not peaceTracks or #peaceTracks<1 then
  605.                 Spring.Echo("\255\1\205\205No music found for Peace! Copy some .ogg files into \\music\\peace")
  606.                 widgetHandler:RemoveWidget()                          
  607.             else
  608.                 local x,y=spGetMouseState()
  609.                 local BetterRand=x+y+math.floor(99*(os.clock()%99999)+(99*(os.time())%99999))--+spGetDrawFrame()+math.random(0,999)
  610.                 --Pick random track that wasn't just played
  611.                
  612.                 curWarTrack = warTracks[1+(BetterRand%#warTracks)]
  613.                 if #warTracks == 1 then
  614.                     curWarTrack = warTracks[1]
  615.                 end
  616.                 if #warTracks >= 2 then
  617.                     if prevWarTrack == curWarTrack then
  618.                         curWarTrack = warTracks[1+(BetterRand%#warTracks)]
  619.                     end
  620.                 end
  621.                 prevWarTrack = curWarTrack
  622.                
  623.                 curColdwarTrack = coldwarTracks[1+(BetterRand%#coldwarTracks)]
  624.                 if #coldwarTracks == 1 then
  625.                     curColdwarTrack = coldwarTracks[1]
  626.                 end
  627.                 if #coldwarTracks >= 2 then
  628.                     if prevColdwarTrack == curColdwarTrack then
  629.                         curColdwarTrack = coldwarTracks[1+(BetterRand%#coldwarTracks)]
  630.                     end
  631.                 end
  632.                 prevColdwarTrack = curColdwarTrack
  633.                
  634.                 curPeaceTrack = peaceTracks[1+(BetterRand%#peaceTracks)]
  635.                 if #peaceTracks == 1 then
  636.                     curPeaceTrack = peaceTracks[1]
  637.                 end
  638.                 if #peaceTracks >= 2 then
  639.                     if prevPeaceTrack == curPeaceTrack then
  640.                         curPeaceTrack = peaceTracks[1+(BetterRand%#peaceTracks)]
  641.                     end
  642.                 end
  643.                 prevPeaceTrack = curPeaceTrack
  644.                
  645.                 spStopSoundStream()
  646.                 if musicType == 0 then
  647.                     Echo("\255\1\205\205Peace Music: "..JustTheName(curPeaceTrack)) --plays at a default level that doesn't seem to be affected by snd_ settings
  648.                     spPlaySoundStream(curPeaceTrack)
  649.                     Spring.SetConfigInt("snd_volmusic", musicVolume+1) --ensures music volume is adjusted from weird default
  650.                     Spring.SetConfigInt("snd_volmusic", musicVolume)
  651.                     curMusicType = 0
  652.                 elseif musicType == 1 then
  653.                     Echo("\255\1\205\205Coldwar Music: "..JustTheName(curColdwarTrack))
  654.                     spPlaySoundStream(curColdwarTrack)
  655.                     Spring.SetConfigInt("snd_volmusic", musicVolume+1) --ensures music volume is adjusted from weird default
  656.                     Spring.SetConfigInt("snd_volmusic", musicVolume)
  657.                     curMusicType = 1
  658.                 elseif musicType == 2 then
  659.                     Echo("\255\1\205\205War Music: "..JustTheName(curWarTrack))
  660.                     spPlaySoundStream(curWarTrack) 
  661.                     Spring.SetConfigInt("snd_volmusic", musicVolume+1) --ensures music volume is adjusted from weird default
  662.                     Spring.SetConfigInt("snd_volmusic", musicVolume)
  663.                     curMusicType = 2
  664.                 end
  665.                 playNew=false
  666.             end
  667.         end
  668.            
  669.         local _,speed,paused = spGetGameSpeed()
  670.         if(not paused) then
  671.             metalDestroyedCounter = metalDestroyedCounter - (teamMetalTotal * WAR_COOLDOWN * speed)
  672.             --clamp metal counter to positive values
  673.             if (metalDestroyedCounter < 0) then
  674.                 metalDestroyedCounter = 0
  675.             end
  676.         end
  677.        
  678.     end
  679. end
  680.  
  681. function widget:TextCommand(command)
  682.   if (string.find(command, 'nextsong') == 1) then
  683.     playNew = true
  684.   elseif (string.find(command, 'debugmusic') == 1) and debug == false then
  685.     debug = true
  686.   elseif (string.find(command, 'debugmusic') == 1) and debug == true then
  687.     debug = false
  688.     end
  689. end
  690.  
  691. ----------------------------------------------------------------------------------------
  692. -- Unit Callins --
  693. ----------------------------------------------------------------------------------------
  694.  
  695. function widget:UnitDestroyed(unitID, unitDefID, teamID)
  696.     if spec then
  697.         unitHealth, unitMaxHealth, paralyzeProgress, captureProgress, buildProgress = spGetUnitHealth(unitID)
  698.         metalDestroyedCounter = metalDestroyedCounter + UnitDefs[unitDefID].metalCost*buildProgress
  699.     elseif teamID == spGetMyTeamID() then
  700.     unitHealth, unitMaxHealth, paralyzeProgress, captureProgress, buildProgress = spGetUnitHealth(unitID)
  701.     metalDestroyedCounter = metalDestroyedCounter + UnitDefs[unitDefID].metalCost*buildProgress
  702.     end
  703. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement