1
0

round_end_music.sp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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.6.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://git.csrd.science/"
  18. }
  19. // Note: All lists share the same handle reference, they aren't cloned.
  20. bool g_bQueueLocked = false;
  21. ArrayList g_QueuedSongs, g_ActiveSongs, g_PlayedSongs;
  22. ConVar g_ConVarMaxActiveSongs, g_ConVarEnabled, g_ConVarSongDelay;
  23. Handle g_RequestSongForward;
  24. Handle g_OnREMPlayedForward, g_OnREMPostPlayedForward;
  25. int g_nMaxActiveSongs = 5;
  26. bool g_bRoundEndMusicActive;
  27. public void OnPluginStart() {
  28. g_QueuedSongs = new ArrayList();
  29. g_ActiveSongs = new ArrayList();
  30. g_PlayedSongs = new ArrayList();
  31. g_RequestSongForward = CreateGlobalForward("OnRoundEndSongsRequested", ET_Ignore);
  32. g_OnREMPlayedForward = CreateGlobalForward("OnRoundEndMusicWillPlay", ET_Event, Param_Cell);
  33. g_OnREMPostPlayedForward = CreateGlobalForward("OnRoundEndMusicPlayed", ET_Ignore,
  34. Param_Cell);
  35. HookEvent("teamplay_round_win", OnRoundEnd, EventHookMode_PostNoCopy);
  36. RegAdminCmd("sm_playsong", AdminCmd_PlaySong, ADMFLAG_ROOT);
  37. RegAdminCmd("sm_peekqueue", AdminCmd_PeekQueue, ADMFLAG_ROOT);
  38. g_ConVarMaxActiveSongs = CreateConVar("sm_rem_active_songs", "5",
  39. "Maximum number of songs available for playback on a single map.", _,
  40. true, 1.0, false);
  41. g_ConVarEnabled = CreateConVar("sm_rem_enabled", "1", "Enables Round End Music.", _,
  42. true, 0.0, true, 1.0);
  43. g_ConVarSongDelay = CreateConVar("sm_rem_song_delay", "4.3",
  44. "Amount of time after the round end to wait before playing the song.", _,
  45. true, 0.0);
  46. AutoExecConfig();
  47. }
  48. public Action AdminCmd_PlaySong(int client, int argc) {
  49. PlayRoundEndMusic();
  50. return Plugin_Handled;
  51. }
  52. public Action AdminCmd_PeekQueue(int client, int argc) {
  53. if (GetCmdReplySource() == SM_REPLY_TO_CHAT) {
  54. ReplyToCommand(client, "See console for output.");
  55. }
  56. PrintSongList(client, "played songs", g_PlayedSongs);
  57. PrintSongList(client, "active songs", g_ActiveSongs);
  58. PrintSongList(client, "queued songs", g_QueuedSongs);
  59. return Plugin_Handled;
  60. }
  61. void PrintSongList(int client, const char[] listName, ArrayList songList) {
  62. char filePath[PLATFORM_MAX_PATH];
  63. if (songList.Length > 0) {
  64. PrintToConsole(client, "---- %s ----", listName);
  65. for (int i = 0; i < songList.Length; i++) {
  66. MusicEntry song = songList.Get(i);
  67. song.GetFilePath(filePath, sizeof(filePath));
  68. PrintToConsole(client, "%d. %s", i + 1, filePath);
  69. }
  70. }
  71. }
  72. public void OnConfigsExecuted() {
  73. g_nMaxActiveSongs = g_ConVarMaxActiveSongs.IntValue;
  74. g_bRoundEndMusicActive = g_ConVarEnabled.BoolValue;
  75. g_bQueueLocked = false;
  76. Call_StartForward(g_RequestSongForward);
  77. Call_Finish();
  78. g_bQueueLocked = true;
  79. /**
  80. * It's okay if there are songs in the active list, as it won't be processed if the plugin
  81. * is set as disabled. However...
  82. *
  83. * TODO make the active song count implicitly zero when disabled?
  84. */
  85. if (g_nMaxActiveSongs < g_ActiveSongs.Length) {
  86. // Put excess songs back in the head of the queue
  87. // Used if fewer active songs are required for long maps
  88. // (or disabled completely e.g. Arena)
  89. while (g_ActiveSongs.Length > g_nMaxActiveSongs) {
  90. int pos = g_ActiveSongs.Length - 1;
  91. MusicEntry song = g_ActiveSongs.Get(pos);
  92. // Insert songs at the top of the queued songs list.
  93. if (g_QueuedSongs.Length == 0) {
  94. // Fix attempting to shift contents of an empty queue up.
  95. g_QueuedSongs.Resize(1);
  96. } else {
  97. g_QueuedSongs.ShiftUp(0);
  98. }
  99. g_QueuedSongs.Set(0, song);
  100. g_ActiveSongs.Erase(pos);
  101. }
  102. } else {
  103. // Take up to g_nMaxActiveSongs songs from queue and move them to g_ActiveSongs
  104. while (g_ActiveSongs.Length < g_nMaxActiveSongs && g_QueuedSongs.Length > 0) {
  105. MusicEntry song = g_QueuedSongs.Get(0);
  106. g_ActiveSongs.Push(song);
  107. g_QueuedSongs.Erase(0);
  108. }
  109. }
  110. /**
  111. * Check to see if we should play music on this map. If not, then don't process the active
  112. * music list.
  113. */
  114. if (g_bRoundEndMusicActive) {
  115. // Do the Fisher-Yates.
  116. // http://spin.atomicobject.com/2014/08/11/fisher-yates-shuffle-randomization-algorithm/
  117. int nActiveSongs = g_ActiveSongs.Length < g_nMaxActiveSongs?
  118. g_ActiveSongs.Length : g_nMaxActiveSongs;
  119. for (int i = 0; i < nActiveSongs; i+= 1) {
  120. int s = GetRandomInt(i, nActiveSongs - 1);
  121. SwapArrayItems(g_ActiveSongs, i, s);
  122. MusicEntry song = g_ActiveSongs.Get(i);
  123. char title[64], source[64], filePath[PLATFORM_MAX_PATH];
  124. song.GetTitle(title, sizeof(title));
  125. song.GetSource(source, sizeof(source));
  126. song.GetFilePath(filePath, sizeof(filePath));
  127. char fileDownloadPath[PLATFORM_MAX_PATH];
  128. Format(fileDownloadPath, sizeof(fileDownloadPath), "sound/%s", filePath);
  129. AddFileToDownloadsTable(fileDownloadPath);
  130. PrecacheSound(filePath);
  131. PrintToServer("[rem] Added song %d: %s", i + 1, filePath);
  132. }
  133. PrintToServer("[rem] Round End Music plugin enabled.");
  134. } else {
  135. PrintToServer("[rem] Round End Music plugin disabled.");
  136. }
  137. }
  138. /**
  139. * Remove any already played songs from the queue
  140. * (so on map start they only contain unplayed tracks).
  141. */
  142. public void OnMapEnd() {
  143. for (int i = 0; i < g_PlayedSongs.Length; i++) {
  144. MusicEntry playedSong = g_PlayedSongs.Get(i);
  145. int activePos = -1;
  146. if ( (activePos = g_ActiveSongs.FindValue(playedSong)) != -1 ) {
  147. g_ActiveSongs.Erase(activePos);
  148. }
  149. delete playedSong;
  150. }
  151. g_PlayedSongs.Clear();
  152. }
  153. /**
  154. * Play pending endround music.
  155. */
  156. void PlayRoundEndMusic() {
  157. if (g_ActiveSongs.Length == 0 || !g_bRoundEndMusicActive) {
  158. PrintToServer("no songs to play :(");
  159. return;
  160. }
  161. // retrieve head of g_ActiveSongs, erase and put into g_PlayedSongs
  162. MusicEntry song = g_ActiveSongs.Get(0);
  163. // mock play for testing
  164. // TODO move into a function with shiny forwards
  165. EmitRoundEndMusic(song);
  166. // if 'active' is empty, copy all back into 'active' without removing from 'played'
  167. if (g_ActiveSongs.Length == 0) {
  168. for (int i = 0; i < g_PlayedSongs.Length; i++) {
  169. g_ActiveSongs.Push(g_PlayedSongs.Get(i));
  170. }
  171. int nActiveSongs = g_ActiveSongs.Length < g_nMaxActiveSongs?
  172. g_ActiveSongs.Length : g_nMaxActiveSongs;
  173. for (int i = 0; i < nActiveSongs; i+= 1) {
  174. int s = GetRandomInt(i, nActiveSongs - 1);
  175. SwapArrayItems(g_ActiveSongs, i, s);
  176. }
  177. }
  178. }
  179. void EmitRoundEndMusic(MusicEntry song) {
  180. Action result = FireOnRoundEndMusicPlayedEvent(song);
  181. if (result == Plugin_Continue) {
  182. char filePath[PLATFORM_MAX_PATH];
  183. song.GetFilePath(filePath, sizeof(filePath));
  184. PrintToServer("mock play song %s", filePath);
  185. EmitSoundToAll(filePath);
  186. }
  187. if (result != Plugin_Stop) {
  188. /**
  189. * Plugins listening to OnRoundEndMusicPlayed might check if the song was already
  190. * played, so we'll have to fire off the event before it gets put into the "played"
  191. * list.
  192. *
  193. * I can't remember if there was any reason why it was last in the first place...
  194. */
  195. FireOnREMPlayedPostEvent(song);
  196. if (g_PlayedSongs.FindValue(song) == -1) {
  197. g_PlayedSongs.Push(song);
  198. }
  199. int pos;
  200. if ( (pos = g_ActiveSongs.FindValue(song)) != -1 ) {
  201. g_ActiveSongs.Erase(pos);
  202. }
  203. }
  204. }
  205. Action FireOnRoundEndMusicPlayedEvent(MusicEntry song) {
  206. Action result;
  207. Call_StartForward(g_OnREMPlayedForward);
  208. Call_PushCell(song);
  209. Call_Finish(result);
  210. return result;
  211. }
  212. void FireOnREMPlayedPostEvent(MusicEntry song) {
  213. Call_StartForward(g_OnREMPostPlayedForward);
  214. Call_PushCell(song);
  215. Call_Finish();
  216. }
  217. public void OnRoundEnd(Event event, const char[] name, bool dontBroadcast) {
  218. float flSongDelay = g_ConVarSongDelay.FloatValue, flBonusRoundTime = GetBonusRoundTime();
  219. // Enforce checking of song delay.
  220. flSongDelay = flSongDelay > flBonusRoundTime? flBonusRoundTime : flSongDelay;
  221. CreateTimer(flSongDelay, RoundEndMusicPlaybackDelay, _, TIMER_FLAG_NO_MAPCHANGE);
  222. }
  223. public Action RoundEndMusicPlaybackDelay(Handle timer, any data) {
  224. PlayRoundEndMusic();
  225. }
  226. // menu: read entries from g_PlayedSongs and then g_ActiveSongs if not empty
  227. // that *should* maintain initial play order
  228. // maybe we should just provide a function that provides the entire list and active counts?
  229. /* Utility functions */
  230. float GetBonusRoundTime() {
  231. return FindConVar("mp_bonusroundtime").FloatValue;
  232. }
  233. /* Native function calls */
  234. public APLRes AskPluginLoad2(Handle self, bool late, char[] error, int err_max) {
  235. RegPluginLibrary("round-end-music");
  236. CreateNative("REM_AddSong", Native_AddSong);
  237. CreateNative("REM_GetActiveSongCount", Native_GetActiveSongCount);
  238. CreateNative("REM_SongWasRecentlyPlayed", Native_SongWasRecentlyPlayed);
  239. }
  240. public int Native_AddSong(Handle hPlugin, int nArgs) {
  241. if (g_bQueueLocked) {
  242. ThrowNativeError(1, "Queue is currently locked -- are you calling REM_AddSong outside "
  243. ... "of the registered callback?");
  244. return false;
  245. }
  246. MusicEntry song = view_as<MusicEntry>(GetNativeCell(1));
  247. // Ensure no more songs added than necessary
  248. if (g_QueuedSongs.Length < g_nMaxActiveSongs) {
  249. // Ensure no duplicates by file path in queue or active songs
  250. bool existing = false;
  251. char filePath[PLATFORM_MAX_PATH], existingFilePath[PLATFORM_MAX_PATH];
  252. song.GetFilePath(filePath, sizeof(filePath));
  253. for (int i = 0; i < g_QueuedSongs.Length; i++) {
  254. (view_as<MusicEntry>(g_QueuedSongs.Get(i))).GetFilePath(
  255. existingFilePath, sizeof(existingFilePath));
  256. existing |= StrEqual(filePath, existingFilePath);
  257. }
  258. for (int i = 0; i < g_ActiveSongs.Length; i++) {
  259. (view_as<MusicEntry>(g_ActiveSongs.Get(i)))
  260. .GetFilePath(existingFilePath, sizeof(existingFilePath));
  261. existing |= StrEqual(filePath, existingFilePath);
  262. }
  263. if (!existing) {
  264. g_QueuedSongs.Push(CloneHandle(song));
  265. return true;
  266. }
  267. }
  268. return false;
  269. }
  270. public int Native_GetActiveSongCount(Handle hPlugin, int nArgs) {
  271. return g_nMaxActiveSongs;
  272. }
  273. public int Native_SongWasRecentlyPlayed(Handle hPlugin, int nArgs) {
  274. MusicEntry song = GetNativeCell(1);
  275. for (int i = 0; i < g_PlayedSongs.Length; i++) {
  276. MusicEntry playedSong = g_PlayedSongs.Get(i);
  277. if (song.Equals(playedSong)) {
  278. return true;
  279. }
  280. }
  281. return false;
  282. }