File: music.lua

package info (click to toggle)
naev 0.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 386,084 kB
  • sloc: ansic: 93,149; xml: 87,292; python: 2,347; sh: 904; makefile: 654; lisp: 162; awk: 4
file content (381 lines) | stat: -rw-r--r-- 9,565 bytes parent folder | download | duplicates (2)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381

--[[
-- music will get called with a string parameter indicating status
-- valid parameters:
--    load - game is loading
--    land - player landed
--    takeoff - player took off
--    combat - player just got a hostile onscreen
--    idle - current playing music ran out
]]--
last = "idle"

-- Faction-specific songs.
factional = {
   Collective = { "collective1", "automat" },
   Pirate     = { "pirate1_theme1", "pirates_orchestra", "ambient4",
                  "terminal" },
   Empire     = { "empire1", "empire2"; add_neutral = true },
   Sirius     = { "sirius1", "sirius2"; add_neutral = true },
   Dvaered    = { "dvaered1", "dvaered2"; add_neutral = true },
   ["Za'lek"] = { "zalek1", "zalek2"; add_neutral = true },
   Thurion    = { "motherload", "dark_city", "ambient1", "ambient3" },
   Proteron   = { "heartofmachine", "imminent_threat", "ambient4" },
}

function choose( str )
   -- Stores all the available sound types and their functions
   choose_table = {
      ["load"]    = choose_load,
      ["intro"]   = choose_intro,
      ["credits"] = choose_credits,
      ["land"]    = choose_land,
      ["takeoff"] = choose_takeoff,
      ["ambient"] = choose_ambient,
      ["combat"]  = choose_combat
   }

   -- Don't change or play music if a mission or event doesn't want us to
   if var.peek( "music_off" ) then
      return
   end

   -- Allow restricting play of music until a song finishes
   if var.peek( "music_wait" ) then
      if music.isPlaying() then
         return
      else
         var.pop( "music_wait" )
      end
   end

   -- Means to only change song if needed
   if str == nil then
      str = "ambient"
   end

   -- If we are over idling then we do weird stuff
   local changed = false
   if str == "idle" and last ~= "idle" then

      -- We'll play the same as last unless it was takeoff
      if last == "takeoff" then
         changed = choose_ambient()
      else
         changed = choose(last)
      end

   -- Normal case
   else
      changed = choose_table[ str ]()
   end

   if changed and str ~= "idle" then
      last = str -- save the last string so we can use it
   end
end


--[[
-- @brief Checks to see if a song is being played, if it is it stops it.
--
--    @return true if music is playing.
--]]
function checkIfPlayingOrStop( song )
   if music.isPlaying() then
      if music.current() ~= song then
         music.stop()
      end
      return true
   end
   return false
end


--[[
-- @brief Play a song if it's not currently playing.
--]]
function playIfNotPlaying( song )
   if checkIfPlayingOrStop( song ) then
      return true
   end
   music.load( song )
   music.play()
   return true
end


--[[
-- @brief Chooses Loading songs.
--]]
function choose_load ()
   return playIfNotPlaying( "machina" )
end


--[[
-- @brief Chooses Intro songs.
--]]
function choose_intro ()
   return playIfNotPlaying( "intro" )
end


--[[
-- @brief Chooses Credit songs.
--]]
function choose_credits ()
   return playIfNotPlaying( "empire1" )
end


--[[
-- @brief Chooses landing songs.
--]]
function choose_land ()
   local pnt   = planet.cur()
   local class = pnt:class()

   if class == "M" then
      mus = { "agriculture" }
   elseif class == "O" then
      mus = { "ocean" }
   elseif class == "P" then
      mus = { "snow" }
   else
      if pnt:services()["inhabited"] then
         mus = { "cosmostation", "upbeat" }
      else
         mus = { "agriculture" }
      end
   end

   music.load( mus[ rnd.rnd(1, #mus) ] )
   music.play()
   return true
end


-- Takeoff songs
function choose_takeoff ()
   -- No need to restart
   if last == "takeoff" and music.isPlaying() then
      return true
   end
   takeoff = { "liftoff", "launch2", "launch3chatstart" }
   music.load( takeoff[ rnd.rnd(1,#takeoff) ])
   music.play()
   return true
end


-- Save old data
last_sysFaction  = nil
last_sysNebuDens = nil
last_sysNebuVol  = nil
ambient_neutral  = { "ambient2", "mission",
      "peace1", "peace2", "peace4", "peace6",
      "void_sensor", "ambiphonic",
      "ambient4", "terminal", "eureka",
      "ambient2_5" }
--[[
-- @brief Chooses ambient songs.
--]]
function choose_ambient ()
   local force = true
   local add_neutral = false

   -- Check to see if we want to update
   if music.isPlaying() then
      if last == "takeoff" then
         return true
      elseif last == "ambient" then
         force = false
      end

      -- Get music information.
      local songname, songpos = music.current()

      -- Do not change songs so soon
      if songpos < 10. then
         music.delay( "ambient", 10. - songpos )
         return false
      end
   end

   -- Get information about the current system
   local sys                  = system.cur()
   local factions             = sys:presences()
   local nebu_dens, nebu_vol  = sys:nebula()

   local strongest = var.peek("music_ambient_force")
   if strongest == nil then
      if factions then
         local strongest_amount = 0
         for k, v in pairs( factions ) do
            if v > strongest_amount then
               strongest = k
               strongest_amount = v
            end
         end
      end
   end

   -- Check to see if changing faction zone
   if strongest ~= last_sysFaction then
      force = true

      if force then
         last_sysFaction = strongest
      end
   end

   -- Check to see if entering nebula
   local nebu = nebu_dens > 0
   if nebu ~= last_sysNebuDens then
      force = true
      last_sysNebuDens = nebu
   end
 
   -- Must be forced
   if force then
      -- Choose the music, bias by faction first
      local add_neutral = false
      local neutral_prob = 0.6
      if strongest ~= nil and factional[strongest] then
         ambient = factional[strongest]
         add_neutral = factional[strongest].add_neutral
      elseif nebu then
         ambient = { "ambient1", "ambient3" }
         add_neutral = true
      else
         ambient = ambient_neutral
      end

      -- Clobber array with generic songs if allowed.
      if add_neutral and rnd.rnd() < neutral_prob then
         ambient = ambient_neutral
      end

      -- Make sure it's not already in the list or that we have to stop the
      -- currently playing song.
      if music.isPlaying() then
         local cur = music.current()
         for k,v in pairs(ambient) do
            if cur == v then
               return false
            end
         end

         music.stop()
         return true
      end

      -- Load music and play
      local new_track = ambient[ rnd.rnd(1,#ambient) ]

      -- Make it very unlikely (but not impossible) for the same music
      -- to play twice
      for i=1, 3 do
         if new_track == last_track then
            new_track = ambient[ rnd.rnd(1,#ambient) ]
         else
            break
         end
      end

      last_track = new_track
      music.load( new_track )
      music.play()
      return true
   end

   return false
end


-- Faction-specific combat songs
factional_combat = {
   Collective = { "collective2", "galacticbattle", "battlesomething1", "combat3" },
   Pirate     = { "battlesomething2", "blackmoor_tides", add_neutral = true },
   Empire     = { "galacticbattle", "battlesomething2"; add_neutral = true },
   Goddard    = { "flf_battle1", "battlesomething1"; add_neutral = true },
   Dvaered    = { "flf_battle1", "battlesomething1", "battlesomething2"; add_neutral = true },
   ["FLF"]    = { "flf_battle1", "battlesomething2"; add_neutral = true },
   Frontier   = { "flf_battle1"; add_neutral = true },
   Sirius     = { "galacticbattle", "battlesomething1"; add_neutral = true },
   Soromid    = { "galacticbattle", "battlesomething2"; add_neutral = true },
   ["Za'lek"] = { "collective2", "galacticbattle", "battlesomething1", add_neutral = true }
}

--[[
-- @brief Chooses battle songs.
--]]
function choose_combat ()
   -- Get some data about the system
   local sys                  = system.cur()
   local nebu_dens, nebu_vol  = sys:nebula()
   
   local strongest = var.peek("music_combat_force")
   if strongest == nil then
      local presences = sys:presences()
      if presences then
         local strongest_amount = 0
         for k, v in pairs( presences ) do
            if faction.get(k):playerStanding() < 0 and v > strongest_amount then
               strongest = k
               strongest_amount = v
            end
         end
      end
   end

   local nebu = nebu_dens > 0
   if nebu then
      combat = { "nebu_battle1", "nebu_battle2", "combat1", "combat2" }
   else
      combat = { "combat3", "combat1", "combat2" }
   end

   if factional_combat[strongest] then
      if factional_combat[strongest].add_neutral then
         for k, v in ipairs( factional_combat[strongest] ) do
            combat[ #combat + 1 ] = v
         end
      else
         combat = factional_combat[strongest]
      end
   end

   -- Make sure it's not already in the list or that we have to stop the
   -- currently playing song.
   if music.isPlaying() then
      local cur = music.current()
      for k,v in pairs(combat) do
         if cur == v then
            return true
         end
      end

      music.stop()
      return true
   end

   local new_track = combat[ rnd.rnd(1,#combat) ]

   -- Make it very unlikely (but not impossible) for the same music
   -- to play twice
   for i=1, 3 do
      if new_track == last_track then
         new_track = combat[ rnd.rnd(1,#combat) ]
      else
         break
      end
   end

   last_track = new_track
   music.load( new_track )
   music.play()
   return true
end