round_end_music.sp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * [CSRD] Round End Music
  3. *
  4. * In-house rewrite of Round End Music. Hopefully it'll be much cleaner to work with.
  5. */
  6. #pragma semicolon 1
  7. #include <sourcemod>
  8. #include <sdktools>
  9. #pragma newdecls required
  10. #include <round_end_music>
  11. #define PLUGIN_VERSION "0.0.0"
  12. public Plugin myinfo = {
  13. name = "[CSRD] Round End Music",
  14. author = "nosoop",
  15. description = "A fresh rewrite of the Round End Music plugin.",
  16. version = PLUGIN_VERSION,
  17. url = "https://pika.nom-nom-nom.us/"
  18. }
  19. bool g_bQueueLocked = false;
  20. ArrayList g_QueuedSongs, g_ActiveSongs, g_PlayedSongs;
  21. Handle g_RequestSongForward;
  22. int g_nMaxActiveSongs = 5;
  23. public void OnPluginStart() {
  24. g_QueuedSongs = new ArrayList();
  25. g_ActiveSongs = new ArrayList();
  26. g_PlayedSongs = new ArrayList();
  27. g_RequestSongForward = CreateForward(ET_Ignore);
  28. HookEvent("teamplay_round_win", OnRoundEnd, EventHookMode_PostNoCopy);
  29. RegAdminCmd("sm_playsong", AdminCmd_PlaySong, ADMFLAG_ROOT);
  30. RegAdminCmd("sm_peekqueue", AdminCmd_PeekQueue, ADMFLAG_ROOT);
  31. }
  32. public Action AdminCmd_PlaySong(int client, int argc) {
  33. PlayRoundEndMusic();
  34. return Plugin_Handled;
  35. }
  36. public Action AdminCmd_PeekQueue(int client, int argc) {
  37. if (GetCmdReplySource() == SM_REPLY_TO_CHAT) {
  38. ReplyToCommand(client, "See console for output.");
  39. }
  40. PrintSongList(client, "played songs", g_PlayedSongs);
  41. PrintSongList(client, "active songs", g_ActiveSongs);
  42. PrintSongList(client, "queued songs", g_QueuedSongs);
  43. return Plugin_Handled;
  44. }
  45. void PrintSongList(int client, const char[] listName, ArrayList songList) {
  46. char filePath[PLATFORM_MAX_PATH];
  47. if (songList.Length > 0) {
  48. PrintToConsole(client, "---- %s ----", listName);
  49. for (int i = 0; i < songList.Length; i++) {
  50. MusicEntry song = songList.Get(i);
  51. song.GetFilePath(filePath, sizeof(filePath));
  52. PrintToConsole(client, "%d. %s", i + 1, filePath);
  53. }
  54. }
  55. }
  56. public void OnAutoConfigsBuffered() {
  57. // TODO pull g_nMaxActiveSongs from ConVar -- it can't change during map
  58. g_bQueueLocked = false;
  59. Call_StartForward(g_RequestSongForward);
  60. Call_Finish();
  61. g_bQueueLocked = true;
  62. if (g_nMaxActiveSongs < g_ActiveSongs.Length) {
  63. // Put excess songs back in the head of the queue
  64. // Used if fewer active songs are required for long maps
  65. // (or disabled completely e.g. Arena)
  66. while (g_ActiveSongs.Length > g_nMaxActiveSongs) {
  67. int pos = g_ActiveSongs.Length - 1;
  68. MusicEntry song = g_ActiveSongs.Get(pos);
  69. g_QueuedSongs.ShiftUp(0);
  70. g_QueuedSongs.Set(0, song);
  71. g_ActiveSongs.Erase(0);
  72. }
  73. } else {
  74. // Take up to g_nMaxActiveSongs songs from queue and move them to g_ActiveSongs
  75. while (g_ActiveSongs.Length < g_nMaxActiveSongs && g_QueuedSongs.Length > 0) {
  76. MusicEntry song = g_QueuedSongs.Get(0);
  77. g_ActiveSongs.Push(song);
  78. g_QueuedSongs.Erase(0);
  79. }
  80. }
  81. // Do the Fisher-Yates.
  82. // http://spin.atomicobject.com/2014/08/11/fisher-yates-shuffle-randomization-algorithm/
  83. int nActiveSongs = g_ActiveSongs.Length < g_nMaxActiveSongs?
  84. g_ActiveSongs.Length : g_nMaxActiveSongs;
  85. for (int i = 0; i < nActiveSongs; i+= 1) {
  86. int s = GetRandomInt(i, nActiveSongs - 1);
  87. SwapArrayItems(g_ActiveSongs, i, s);
  88. MusicEntry song = g_ActiveSongs.Get(i);
  89. char title[64], source[64], filePath[PLATFORM_MAX_PATH];
  90. song.GetTitle(title, sizeof(title));
  91. song.GetSource(source, sizeof(source));
  92. song.GetFilePath(filePath, sizeof(filePath));
  93. char fileDownloadPath[PLATFORM_MAX_PATH];
  94. Format(fileDownloadPath, sizeof(fileDownloadPath), "sound/%s", filePath);
  95. AddFileToDownloadsTable(fileDownloadPath);
  96. PrecacheSound(filePath);
  97. PrintToServer("[rem] Added song %d: %s", i + 1, filePath);
  98. }
  99. }
  100. /**
  101. * Remove any already played songs from the queue
  102. * (so on map start they only contain unplayed tracks).
  103. */
  104. public void OnMapEnd() {
  105. for (int i = 0; i < g_PlayedSongs.Length; i++) {
  106. MusicEntry playedSong = g_PlayedSongs.Get(i);
  107. int activePos = -1;
  108. if ( (activePos = g_ActiveSongs.FindValue(playedSong)) != -1 ) {
  109. g_ActiveSongs.Erase(activePos);
  110. }
  111. delete playedSong;
  112. }
  113. g_PlayedSongs.Clear();
  114. }
  115. /**
  116. * Play pending endround music.
  117. */
  118. void PlayRoundEndMusic() {
  119. if (g_ActiveSongs.Length == 0) {
  120. PrintToServer("no songs to play :(");
  121. return;
  122. }
  123. // retrieve head of g_ActiveSongs, erase and put into g_PlayedSongs
  124. MusicEntry song = g_ActiveSongs.Get(0);
  125. // mock play for testing
  126. // TODO move into a function with shiny forwards
  127. char filePath[PLATFORM_MAX_PATH];
  128. song.GetFilePath(filePath, sizeof(filePath));
  129. PrintToServer("mock play song %s", filePath);
  130. EmitSoundToAll(filePath);
  131. if (g_PlayedSongs.FindValue(song) == -1) {
  132. g_PlayedSongs.Push(song);
  133. }
  134. g_ActiveSongs.Erase(0);
  135. // if 'active' is empty, copy all back into 'active' without removing from 'played'
  136. if (g_ActiveSongs.Length == 0) {
  137. for (int i = 0; i < g_PlayedSongs.Length; i++) {
  138. g_ActiveSongs.Push(g_PlayedSongs.Get(i));
  139. }
  140. int nActiveSongs = g_ActiveSongs.Length < g_nMaxActiveSongs?
  141. g_ActiveSongs.Length : g_nMaxActiveSongs;
  142. for (int i = 0; i < nActiveSongs; i+= 1) {
  143. int s = GetRandomInt(i, nActiveSongs - 1);
  144. SwapArrayItems(g_ActiveSongs, i, s);
  145. }
  146. }
  147. }
  148. public void OnRoundEnd(Event event, const char[] name, bool dontBroadcast) {
  149. PlayRoundEndMusic();
  150. }
  151. // menu: read entries from g_PlayedSongs and then g_ActiveSongs if not empty
  152. // that *should* maintain initial play order
  153. /* Native function calls */
  154. public APLRes AskPluginLoad2(Handle self, bool late, char[] error, int err_max) {
  155. RegPluginLibrary("round-end-music");
  156. CreateNative("REM_RegisterSource", Native_RegisterSource);
  157. CreateNative("REM_AddSong", Native_AddSong);
  158. }
  159. public int Native_RegisterSource(Handle hPlugin, int nArgs) {
  160. Function callback = GetNativeFunction(1);
  161. // preemptive removal because it could be forwarded multiple times?
  162. for (int i = 0; i < GetForwardFunctionCount(g_RequestSongForward); i++) {
  163. RemoveFromForward(g_RequestSongForward, hPlugin, callback);
  164. }
  165. AddToForward(g_RequestSongForward, hPlugin, callback);
  166. return 1;
  167. }
  168. public int Native_AddSong(Handle hPlugin, int nArgs) {
  169. if (g_bQueueLocked) {
  170. ThrowNativeError(1, "Queue is currently locked -- are you calling REM_AddSong outside "
  171. ... "of the registered callback?");
  172. return false;
  173. }
  174. MusicEntry song = view_as<MusicEntry>(GetNativeCell(1));
  175. // Ensure no more songs added than necessary
  176. if (g_QueuedSongs.Length < g_nMaxActiveSongs) {
  177. // Ensure no duplicates by file path in queue or active songs
  178. bool existing = false;
  179. char filePath[PLATFORM_MAX_PATH], existingFilePath[PLATFORM_MAX_PATH];
  180. song.GetFilePath(filePath, sizeof(filePath));
  181. for (int i = 0; i < g_QueuedSongs.Length; i++) {
  182. (view_as<MusicEntry>(g_QueuedSongs.Get(i))).GetFilePath(
  183. existingFilePath, sizeof(existingFilePath));
  184. existing |= StrEqual(filePath, existingFilePath);
  185. }
  186. for (int i = 0; i < g_ActiveSongs.Length; i++) {
  187. (view_as<MusicEntry>(g_ActiveSongs.Get(i)))
  188. .GetFilePath(existingFilePath, sizeof(existingFilePath));
  189. existing |= StrEqual(filePath, existingFilePath);
  190. }
  191. if (!existing) {
  192. g_QueuedSongs.Push(CloneHandle(song));
  193. return true;
  194. }
  195. }
  196. return false;
  197. }