3636#include " ../Savegame/Node.h"
3737#include " ../Battlescape/Position.h"
3838#include " ../Ruleset/MapDataSet.h"
39+ #include " ../Ruleset/RuleMusic.h"
3940#include " ../Engine/ShaderDraw.h"
4041#include " ../Engine/ShaderMove.h"
4142#include " ../Engine/Exception.h"
@@ -255,22 +256,7 @@ XcomResourcePack::XcomResourcePack(Ruleset *rules) : ResourcePack()
255256 {
256257#ifndef __NO_MUSIC
257258 // Load musics
258- std::string mus[] = {" GMDEFEND" ,
259- " GMENBASE" ,
260- " GMGEO1" ,
261- " GMGEO2" ,
262- " GMINTER" ,
263- " GMINTRO1" ,
264- " GMINTRO2" ,
265- " GMINTRO3" ,
266- " GMLOSE" ,
267- " GMMARS" ,
268- " GMNEWMAR" ,
269- " GMSTORY" ,
270- " GMTACTIC" ,
271- " GMWIN" };
272- int tracks[] = {3 , 6 , 0 , 18 , 2 , 19 , 20 , 21 , 10 , 9 , 8 , 12 , 17 , 11 };
273- float tracks_normalize[] = {0 .76f , 0 .83f , 1 .19f , 1 .0f , 0 .74f , 0 .8f , 0 .8f , 0 .8f , 1 .0f , 0 .92f , 0 .81f , 1 .0f , 1 .14f , 0 .84f };
259+ const std::map<std::string, RuleMusic *> musics = *rules->getMusic ();
274260
275261 // Check which music version is available
276262 CatFile *adlibcat = 0 , *aintrocat = 0 ;
@@ -292,54 +278,23 @@ XcomResourcePack::XcomResourcePack(Ruleset *rules) : ResourcePack()
292278
293279 // Try the preferred format first, otherwise use the default priority
294280 MusicFormat priority[] = {Options::preferredMusic, MUSIC_FLAC , MUSIC_OGG , MUSIC_MP3 , MUSIC_MOD , MUSIC_WAV , MUSIC_ADLIB , MUSIC_MIDI };
295-
296- for (size_t i = 0 ; i < sizeof (mus)/sizeof (mus[0 ]); ++i)
281+ for (std::map<std::string, RuleMusic *>::const_iterator i = musics.begin (); i != musics.end (); ++i)
297282 {
298283 Music *music = 0 ;
299284 for (size_t j = 0 ; j < sizeof (priority)/sizeof (priority[0 ]) && music == 0 ; ++j)
300285 {
301- music = loadMusic (priority[j], mus[i], tracks[i], tracks_normalize[i] , adlibcat, aintrocat, gmcat);
286+ music = loadMusic (priority[j], (*i). first , (*i). second -> getCatPos (), (*i). second -> getNormalization () , adlibcat, aintrocat, gmcat);
302287 }
303- if (! music)
288+ if (music)
304289 {
305- throw Exception (mus[i] + " not found " ) ;
290+ _musics[(*i). first ] = music ;
306291 }
307- _musics[mus[i]] = music;
292+
308293 }
294+
309295 delete gmcat;
310296 delete adlibcat;
311297 delete aintrocat;
312-
313- // Ok, now try to load the optional musics
314- std::string musOptional[] = {" GMGEO3" ,
315- " GMGEO4" ,
316- " GMGEO5" ,
317- " GMGEO6" ,
318- " GMGEO7" ,
319- " GMGEO8" ,
320- " GMGEO9" ,
321- " GMTACTIC1" ,
322- " GMTACTIC2" ,
323- " GMTACTIC3" ,
324- " GMTACTIC4" ,
325- " GMTACTIC5" ,
326- " GMTACTIC6" ,
327- " GMTACTIC7" ,
328- " GMTACTIC8" ,
329- " GMTACTIC9" };
330-
331- for (size_t i = 0 ; i < sizeof (musOptional)/sizeof (musOptional[0 ]); ++i)
332- {
333- Music *music = 0 ;
334- for (size_t j = 0 ; j < sizeof (priority) / sizeof (priority[0 ]) && music == 0 ; ++j)
335- {
336- music = loadMusic (priority[j], musOptional[i], 0 , 0 , 0 , 0 , 0 );
337- }
338- if (music)
339- {
340- _musics[musOptional[i]] = music;
341- }
342- }
343298#endif
344299
345300 if (rules->getSoundDefinitions ()->empty ())
@@ -1017,15 +972,23 @@ Music *XcomResourcePack::loadMusic(MusicFormat fmt, const std::string &file, int
1017972 else if (aintrocat)
1018973 {
1019974 track -= adlibcat->getAmount ();
1020- music->load (aintrocat->load (track, true ), aintrocat->getObjectSize (track));
975+ if (track < aintrocat->getAmount ())
976+ {
977+ music->load (aintrocat->load (track, true ), aintrocat->getObjectSize (track));
978+ }
979+ else
980+ {
981+ delete music;
982+ music = 0 ;
983+ }
1021984 }
1022985 }
1023986 }
1024987 // Try MIDI music
1025988 else if (fmt == MUSIC_MIDI )
1026989 {
1027990 // DOS MIDI
1028- if (gmcat)
991+ if (gmcat && track < gmcat-> getAmount () )
1029992 {
1030993 music = gmcat->loadMIDI (track);
1031994 }
0 commit comments