View Single Post
11-15-16, 04:15 PM   #10
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
Here's code for the main part of the addon that I whipped up. It uses metatables to handle falling back to a generic song table. Be sure the file containing this code loads first in your ToC.
Lua Code:
  1. --  Grab access to our shared addon table
  2. local _,Addon=...;
  3.  
  4. --  Set up our default song table and metatable for fallback
  5. local DefaultSongTable={};
  6. local FallbackMeta={__index=DefaultSongTable};
  7.  
  8. local SongTables={Alliance={},Horde={}};--  Build faction-based song tables
  9. for _,tbl in pairs(SongTables) do setmetatable(tbl,FallbackMeta); end-- Set our fallback metatable
  10. SongTables.Default=DefaultSongTable;--  Register the default song table
  11. Addon.SongTables=SongTables;--  Enable access to our song tables through the shared addon table
  12.  
  13. local CurrentSong;
  14. local function PlaySongForMap(mapid)
  15. --  Select the song table and grab the song for the map
  16.     local song=(SongTables[UnitFactionGroup("player")] or DefaultSongTable)[mapid];
  17.  
  18. --  Play the new song if different or stop if there is none
  19.     if song and song~=CurrentSong then
  20.         PlayMusic(song);
  21.     elseif not song then
  22.         StopMusic();
  23.     end
  24.  
  25. --  Update our saved variable
  26.     CurrentSong=song;
  27. end
  28.  
  29. local EventFrame=CreateFrame("Frame");
  30. EventFrame:RegisterEvent("PLAYER_ENTERING_WORLD");--    Player enters from loading screen
  31. EventFrame:RegisterEvent("ZONE_CHANGED_NEW_AREA");--    Zones changed
  32. EventFrame:RegisterEvent("ZONE_CHANGED_INDOORS");-- Some indoor zone changing requires this
  33.  
  34. EventFrame:SetScript("OnEvent",function()
  35. --  We don't want to mess around with the map while the user is looking at it, but we need our MapID, so we'll do some tricky stuff
  36.     if WorldMapFrame:IsShown() then
  37. --      Get a list of frames listening for map updates and disable them
  38.         local frames={GetFramesRegisteredByEvent("WORLD_MAP_UPDATE")};
  39.         for _,frame in ipairs(frames) do frame:UnregisterEvent("WORLD_MAP_UPDATE"); end
  40.  
  41. --      Save old MapID and select current zone (MapID is invalid if viewing a continent, store that too)
  42.         local mapid,continent=GetCurrentMapAreaID(),GetCurrentMapContinent();
  43.         SetMapToCurrentZone();
  44.  
  45. --      We have the zone we want, find and play our song
  46.         PlaySongForMap(GetCurrentMapAreaID());
  47.  
  48. --      Restore the map and re-enable the frames so the user would never know we did anything
  49.         if mapid>=0 then SetMapByID(mapid); else SetMapZoom(continent); end
  50.         for _,frame in ipairs(frames) do frame:RegisterEvent("WORLD_MAP_UPDATE"); end
  51.     else
  52. --      User isn't looking at the map, we'll use a more straightforward approach
  53.         SetMapToCurrentZone();
  54.         PlaySongForMap(GetCurrentMapAreaID());
  55.     end
  56. end);

In your sound packs placed in other Lua files in the same addon, you can register songs like this.
Lua Code:
  1. --  Grab access to our shared addon table
  2. local _,Addon=...;
  3.  
  4. --  Alliance and Horde have their own songs, though it's a good idea to define a default anyway
  5. Addon.SongTable.Default[123]="Path\\To\\DefaultSong";
  6. Addon.SongTable.Alliance[123]="Path\\To\\AllianceSong";
  7. Addon.SongTable.Horde[123]="Path\\To\\HordeSong";
  8.  
  9. --  Alliance has their own song, Horde uses default
  10. Addon.SongTable.Default[345]="Path\\To\\DefaultSong";
  11. Addon.SongTable.Alliance[345]="Path\\To\\AllianceSong";
  12.  
  13. --  Horde has their own song, Alliance uses default
  14. Addon.SongTable.Default[567]="Path\\To\\DefaultSong";
  15. Addon.SongTable.Horde[567]="Path\\To\\HordeSong";
  16.  
  17. --  Both factions use the default
  18. Addon.SongTable.Default[789]="Path\\To\\DefaultSong";



PS: The first return of UnitFactionGroup() is locale-independant, so it doesn't need to be localized.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 11-15-16 at 04:24 PM.
  Reply With Quote