-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathsnd_music_lite.lua
More file actions
184 lines (153 loc) · 4.85 KB
/
Copy pathsnd_music_lite.lua
File metadata and controls
184 lines (153 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function widget:GetInfo()
return {
name = "Music Player Lite",
desc = "Plays music for ingame lobby client",
author = "GoogleFrog and KingRaptor",
date = "25 September 2016",
license = "GNU GPL, v2 or later",
layer = 2000,
enabled = true -- loaded by default?
}
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local playingTrack -- boolean
local previousTrack
local loopTrack -- string trackPath
local randomTrackList
local openTrack
local function GetRandomTrack(previousTrack)
local trackCount = #randomTrackList
local previousTrackIndex
if previousTrack then
for i = 1, #randomTrackList do
if randomTrackList[i] == previousTrack then
trackCount = trackCount - 1
previousTrackIndex = i
break
end
end
end
local randomTrack = math.random(1, trackCount)
if randomTrack == previousTrackIndex and previousTrackIndex ~= trackCount then
randomTrack = trackCount + 1
end
return randomTrackList[randomTrack]
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function StartTrack(trackName, volume)
trackName = trackName or GetRandomTrack(previousTrack)
volume = volume or WG.Chobby.Configuration.menuMusicVolume
Spring.Echo("Starting Track", trackName, volume)
if volume == 0 then
return
end
Spring.StopSoundStream()
Spring.PlaySoundStream(trackName, volume)
playingTrack = true
end
local function LoopTrack(trackName, trackNameIntro, volume)
trackNameIntro = trackNameIntro or trackName
loopTrack = trackName
StartTrack(trackNameIntro, volume)
end
local function StopTrack()
Spring.StopSoundStream()
playingTrack = false
loopTrack = nil
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function SetTrackVolume(volume)
if volume == 0 then
StopTrack()
return
end
if playingTrack then
Spring.SetSoundStreamVolume(volume)
return
end
StartTrack(GetRandomTrack(), volume)
previousTrack = nil
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local firstActivation = true
local ingame = false
function widget:Update()
if ingame or (WG.Chobby.Configuration.menuMusicVolume == 0 )then
return
end
if not playingTrack then
return
end
local playedTime, totalTime = Spring.GetSoundStreamTime()
playedTime = math.floor(playedTime)
totalTime = math.floor(totalTime)
if (playedTime >= totalTime) then
local newTrack = loopTrack or GetRandomTrack(previousTrack)
StartTrack(newTrack)
previousTrack = newTrack
end
end
local MusicHandler = {
StartTrack = StartTrack,
StopTrack = StopTrack,
LoopTrack = LoopTrack
}
-- Called just before the game loads
-- This could be used to implement music in the loadscreen
--function widget:GamePreload()
-- -- Ingame, no longer any of our business
-- if Spring.GetGameName() ~= "" then
-- ingame = true
-- StopTrack()
-- end
--end
-- called when returning to menu from a game
function widget:ActivateMenu()
ingame = false
if firstActivation then
StartTrack(openTrack)
previousTrack = openTrack
firstActivation = false
return
end
-- start playing music again
local newTrack = GetRandomTrack(previousTrack)
StartTrack(newTrack)
previousTrack = newTrack
end
function widget:Initialize()
-- load custom game dependent music
randomTrackList = WG.Chobby.Configuration.gameConfig.randomTrackList
if randomTrackList == nil or #randomTrackList == 0 then
Spring.Log("snd_music.lite.lua", LOG.NOTICE, "No random track list found, disabling lobby music")
widgetHandler:RemoveWidget()
return
end
math.randomseed(os.clock() * 100)
openTrack = WG.Chobby.Configuration.gameConfig.openTrack
if openTrack == nil then
openTrack = randomTrackList[math.random(1, #randomTrackList)]
end
local Configuration = WG.Chobby.Configuration
local function onConfigurationChange(listener, key, value)
if key == "menuMusicVolume" then
SetTrackVolume(value)
end
end
Configuration:AddListener("OnConfigurationChange", onConfigurationChange)
local function OnBattleAboutToStart()
ingame = true
StopTrack()
end
WG.LibLobby.localLobby:AddListener("OnBattleAboutToStart", OnBattleAboutToStart)
WG.LibLobby.lobby:AddListener("OnBattleAboutToStart", OnBattleAboutToStart)
WG.MusicHandler = MusicHandler
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------