round_end_music.sp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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.1.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. ConVar g_ConVarMaxActiveSongs, g_ConVarEnabled;
  22. Handle g_RequestSongForward;
  23. int g_nMaxActiveSongs = 5;
  24. bool g_bRoundEndMusicActive;
  25. public void OnPluginStart() {
  26. g_QueuedSongs = new ArrayList();
  27. g_ActiveSongs = new ArrayList();
  28. g_PlayedSongs = new ArrayList();
  29. g_RequestSongForward = CreateForward(ET_Ignore);
  30. HookEvent("teamplay_round_win", OnRoundEnd, EventHookMode_PostNoCopy);
  31. RegAdminCmd("sm_playsong", AdminCmd_PlaySong, ADMFLAG_ROOT);
  32. RegAdminCmd("sm_peekqueue", AdminCmd_PeekQueue, ADMFLAG_ROOT);
  33. g_ConVarMaxActiveSongs = CreateConVar("sm_rem_active_songs", "5",
  34. "Maximum number of songs available for playback on a single map.", _,
  35. true, 1.0, false);
  36. g_ConVarEnabled = CreateConVar("sm_rem_enabled", "1", "Enables Round End Music.", _,
  37. true, 0.0, true, 1.0);
  38. AutoExecConfig();
  39. }
  40. public Action AdminCmd_PlaySong(int client, int argc) {
  41. PlayRoundEndMusic();
  42. return Plugin_Handled;
  43. }
  44. public Action AdminCmd_PeekQueue(int client, int argc) {
  45. if (GetCmdReplySource() == SM_REPLY_TO_CHAT) {
  46. ReplyToCommand(client, "See console for output.");
  47. }
  48. PrintSongList(client, "played songs", g_PlayedSongs);
  49. PrintSongList(client, "active songs", g_ActiveSongs);
  50. PrintSongList(client, "queued songs", g_QueuedSongs);
  51. return Plugin_Handled;
  52. }
  53. void PrintSongList(int client, const char[] listName, ArrayList songList) {
  54. char filePath[PLATFORM_MAX_PATH];
  55. if (songList.Length > 0) {
  56. PrintToConsole(client, "---- %s ----", listName);
  57. for (int i = 0; i < songList.Length; i++) {
  58. MusicEntry song = songList.Get(i);
  59. song.GetFilePath(filePath, sizeof(filePath));
  60. PrintToConsole(client, "%d. %s", i + 1, filePath);
  61. }
  62. }
  63. }
  64. public void OnConfigsExecuted() {
  65. // TODO pull g_nMaxActiveSongs from ConVar -- it can't change during map
  66. g_nMaxActiveSongs = g_ConVarMaxActiveSongs.IntValue;
  67. g_bRoundEndMusicActive = g_ConVarEnabled.BoolValue;
  68. g_bQueueLocked = false;
  69. Call_StartForward(g_RequestSongForward);
  70. Call_Finish();
  71. g_bQueueLocked = true;
  72. /**
  73. * It's okay if there are songs in the active list, as it won't be processed if the plugin
  74. * is set as disabled. However...
  75. *
  76. * TODO make the active song count implicitly zero when disabled?
  77. */
  78. if (g_nMaxActiveSongs < g_ActiveSongs.Length) {
  79. // Put excess songs back in the head of the queue
  80. // Used if fewer active songs are required for long maps
  81. // (or disabled completely e.g. Arena)
  82. while (g_ActiveSongs.Length > g_nMaxActiveSongs) {
  83. int pos = g_ActiveSongs.Length - 1;
  84. MusicEntry song = g_ActiveSongs.Get(pos);
  85. // Insert songs at the top of the queued songs list.
  86. if (g_QueuedSongs.Length == 0) {
  87. // Fix attempting to shift contents of an empty queue up.
  88. g_QueuedSongs.Resize(1);
  89. } else {
  90. g_QueuedSongs.ShiftUp(0);
  91. }
  92. g_QueuedSongs.Set(0, song);
  93. g_ActiveSongs.Erase(pos);
  94. }
  95. } else {
  96. // Take up to g_nMaxActiveSongs songs from queue and move them to g_ActiveSongs
  97. while (g_ActiveSongs.Length < g_nMaxActiveSongs && g_QueuedSongs.Length > 0) {
  98. MusicEntry song = g_QueuedSongs.Get(0);
  99. g_ActiveSongs.Push(song);
  100. g_QueuedSongs.Erase(0);
  101. }
  102. }
  103. /**
  104. * Check to see if we should play music on this map. If not, then don't process the active
  105. * music list.
  106. */
  107. if (g_bRoundEndMusicActive) {
  108. // Do the Fisher-Yates.
  109. // http://spin.atomicobject.com/2014/08/11/fisher-yates-shuffle-randomization-algorithm/
  110. int nActiveSongs = g_ActiveSongs.Length < g_nMaxActiveSongs?
  111. g_ActiveSongs.Length : g_nMaxActiveSongs;
  112. for (int i = 0; i < nActiveSongs; i+= 1) {
  113. int s = GetRandomInt(i, nActiveSongs - 1);
  114. SwapArrayItems(g_ActiveSongs, i, s);
  115. MusicEntry song = g_ActiveSongs.Get(i);
  116. char title[64], source[64], filePath[PLATFORM_MAX_PATH];
  117. song.GetTitle(title, sizeof(title));
  118. song.GetSource(source, sizeof(source));
  119. song.GetFilePath(filePath, sizeof(filePath));
  120. char fileDownloadPath[PLATFORM_MAX_PATH];
  121. Format(fileDownloadPath, sizeof(fileDownloadPath), "sound/%s", filePath);
  122. AddFileToDownloadsTable(fileDownloadPath);
  123. PrecacheSound(filePath);
  124. PrintToServer("[rem] Added song %d: %s", i + 1, filePath);
  125. }
  126. PrintToServer("[rem] Round End Music plugin enabled.");
  127. } else {
  128. PrintToServer("[rem] Round End Music plugin disabled.");
  129. }
  130. }
  131. /**
  132. * Remove any already played songs from the queue
  133. * (so on map start they only contain unplayed tracks).
  134. */
  135. public void OnMapEnd() {
  136. for (int i = 0; i < g_PlayedSongs.Length; i++) {
  137. MusicEntry playedSong = g_PlayedSongs.Get(i);
  138. int activePos = -1;
  139. if ( (activePos = g_ActiveSongs.FindValue(playedSong)) != -1 ) {
  140. g_ActiveSongs.Erase(activePos);
  141. }
  142. delete playedSong;
  143. }
  144. g_PlayedSongs.Clear();
  145. }
  146. /**
  147. * Play pending endround music.
  148. */
  149. void PlayRoundEndMusic() {
  150. if (g_ActiveSongs.Length == 0 || !g_bRoundEndMusicActive) {
  151. PrintToServer("no songs to play :(");
  152. return;
  153. }
  154. // retrieve head of g_ActiveSongs, erase and put into g_PlayedSongs
  155. MusicEntry song = g_ActiveSongs.Get(0);
  156. // mock play for testing
  157. // TODO move into a function with shiny forwards
  158. EmitRoundEndMusic(song);
  159. // if 'active' is empty, copy all back into 'active' without removing from 'played'
  160. if (g_ActiveSongs.Length == 0) {
  161. for (int i = 0; i < g_PlayedSongs.Length; i++) {
  162. g_ActiveSongs.Push(g_PlayedSongs.Get(i));
  163. }
  164. int nActiveSongs = g_ActiveSongs.Length < g_nMaxActiveSongs?
  165. g_ActiveSongs.Length : g_nMaxActiveSongs;
  166. for (int i = 0; i < nActiveSongs; i+= 1) {
  167. int s = GetRandomInt(i, nActiveSongs - 1);
  168. SwapArrayItems(g_ActiveSongs, i, s);
  169. }
  170. }
  171. }
  172. void EmitRoundEndMusic(MusicEntry song) {
  173. char filePath[PLATFORM_MAX_PATH];
  174. song.GetFilePath(filePath, sizeof(filePath));
  175. PrintToServer("mock play song %s", filePath);
  176. EmitSoundToAll(filePath);
  177. // TODO create new forward OnRoundEndMusicPlayed(MusicEntry song) ?
  178. // - Plugin_Continue uses default handling
  179. // - Plugin_Handled does not
  180. // - Plugin_Stop doesn't either, but the implication is that the entire endround is canceled
  181. // (i.e., it shouldn't move it to the played list)
  182. if (g_PlayedSongs.FindValue(song) == -1) {
  183. g_PlayedSongs.Push(song);
  184. }
  185. g_ActiveSongs.Erase(0);
  186. }
  187. public void OnRoundEnd(Event event, const char[] name, bool dontBroadcast) {
  188. PlayRoundEndMusic();
  189. }
  190. // menu: read entries from g_PlayedSongs and then g_ActiveSongs if not empty
  191. // that *should* maintain initial play order
  192. // maybe we should just provide a function that provides the entire list and active counts?
  193. /* Native function calls */
  194. public APLRes AskPluginLoad2(Handle self, bool late, char[] error, int err_max) {
  195. RegPluginLibrary("round-end-music");
  196. CreateNative("REM_RegisterSource", Native_RegisterSource);
  197. CreateNative("REM_AddSong", Native_AddSong);
  198. }
  199. public int Native_RegisterSource(Handle hPlugin, int nArgs) {
  200. Function callback = GetNativeFunction(1);
  201. // preemptive removal because it could be forwarded multiple times?
  202. for (int i = 0; i < GetForwardFunctionCount(g_RequestSongForward); i++) {
  203. RemoveFromForward(g_RequestSongForward, hPlugin, callback);
  204. }
  205. AddToForward(g_RequestSongForward, hPlugin, callback);
  206. return 1;
  207. }
  208. public int Native_AddSong(Handle hPlugin, int nArgs) {
  209. if (g_bQueueLocked) {
  210. ThrowNativeError(1, "Queue is currently locked -- are you calling REM_AddSong outside "
  211. ... "of the registered callback?");
  212. return false;
  213. }
  214. MusicEntry song = view_as<MusicEntry>(GetNativeCell(1));
  215. // Ensure no more songs added than necessary
  216. if (g_QueuedSongs.Length < g_nMaxActiveSongs) {
  217. // Ensure no duplicates by file path in queue or active songs
  218. bool existing = false;
  219. char filePath[PLATFORM_MAX_PATH], existingFilePath[PLATFORM_MAX_PATH];
  220. song.GetFilePath(filePath, sizeof(filePath));
  221. for (int i = 0; i < g_QueuedSongs.Length; i++) {
  222. (view_as<MusicEntry>(g_QueuedSongs.Get(i))).GetFilePath(
  223. existingFilePath, sizeof(existingFilePath));
  224. existing |= StrEqual(filePath, existingFilePath);
  225. }
  226. for (int i = 0; i < g_ActiveSongs.Length; i++) {
  227. (view_as<MusicEntry>(g_ActiveSongs.Get(i)))
  228. .GetFilePath(existingFilePath, sizeof(existingFilePath));
  229. existing |= StrEqual(filePath, existingFilePath);
  230. }
  231. if (!existing) {
  232. g_QueuedSongs.Push(CloneHandle(song));
  233. return true;
  234. }
  235. }
  236. return false;
  237. }