1
0

round_end_music.sp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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.4.2"
  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. // 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 = CreateForward(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. if (g_PlayedSongs.FindValue(song) == -1) {
  189. g_PlayedSongs.Push(song);
  190. }
  191. int pos;
  192. if ( (pos = g_ActiveSongs.FindValue(song)) != -1 ) {
  193. g_ActiveSongs.Erase(pos);
  194. }
  195. FireOnREMPlayedPostEvent(song);
  196. }
  197. }
  198. Action FireOnRoundEndMusicPlayedEvent(MusicEntry song) {
  199. Action result;
  200. Call_StartForward(g_OnREMPlayedForward);
  201. Call_PushCell(song);
  202. Call_Finish(result);
  203. return result;
  204. }
  205. void FireOnREMPlayedPostEvent(MusicEntry song) {
  206. Call_StartForward(g_OnREMPostPlayedForward);
  207. Call_PushCell(song);
  208. Call_Finish();
  209. }
  210. public void OnRoundEnd(Event event, const char[] name, bool dontBroadcast) {
  211. float flSongDelay = g_ConVarSongDelay.FloatValue, flBonusRoundTime = GetBonusRoundTime();
  212. // Enforce checking of song delay.
  213. flSongDelay = flSongDelay > flBonusRoundTime? flBonusRoundTime : flSongDelay;
  214. CreateTimer(flSongDelay, RoundEndMusicPlaybackDelay, _, TIMER_FLAG_NO_MAPCHANGE);
  215. }
  216. public Action RoundEndMusicPlaybackDelay(Handle timer, any data) {
  217. PlayRoundEndMusic();
  218. }
  219. // menu: read entries from g_PlayedSongs and then g_ActiveSongs if not empty
  220. // that *should* maintain initial play order
  221. // maybe we should just provide a function that provides the entire list and active counts?
  222. /* Utility functions */
  223. float GetBonusRoundTime() {
  224. return FindConVar("mp_bonusroundtime").FloatValue;
  225. }
  226. /* Native function calls */
  227. public APLRes AskPluginLoad2(Handle self, bool late, char[] error, int err_max) {
  228. RegPluginLibrary("round-end-music");
  229. CreateNative("REM_RegisterSource", Native_RegisterSource);
  230. CreateNative("REM_AddSong", Native_AddSong);
  231. CreateNative("REM_GetActiveSongCount", Native_GetActiveSongCount);
  232. }
  233. public int Native_RegisterSource(Handle hPlugin, int nArgs) {
  234. Function callback = GetNativeFunction(1);
  235. // preemptive removal because it could be forwarded multiple times?
  236. for (int i = 0; i < GetForwardFunctionCount(g_RequestSongForward); i++) {
  237. RemoveFromForward(g_RequestSongForward, hPlugin, callback);
  238. }
  239. AddToForward(g_RequestSongForward, hPlugin, callback);
  240. return 1;
  241. }
  242. public int Native_AddSong(Handle hPlugin, int nArgs) {
  243. if (g_bQueueLocked) {
  244. ThrowNativeError(1, "Queue is currently locked -- are you calling REM_AddSong outside "
  245. ... "of the registered callback?");
  246. return false;
  247. }
  248. MusicEntry song = view_as<MusicEntry>(GetNativeCell(1));
  249. // Ensure no more songs added than necessary
  250. if (g_QueuedSongs.Length < g_nMaxActiveSongs) {
  251. // Ensure no duplicates by file path in queue or active songs
  252. bool existing = false;
  253. char filePath[PLATFORM_MAX_PATH], existingFilePath[PLATFORM_MAX_PATH];
  254. song.GetFilePath(filePath, sizeof(filePath));
  255. for (int i = 0; i < g_QueuedSongs.Length; i++) {
  256. (view_as<MusicEntry>(g_QueuedSongs.Get(i))).GetFilePath(
  257. existingFilePath, sizeof(existingFilePath));
  258. existing |= StrEqual(filePath, existingFilePath);
  259. }
  260. for (int i = 0; i < g_ActiveSongs.Length; i++) {
  261. (view_as<MusicEntry>(g_ActiveSongs.Get(i)))
  262. .GetFilePath(existingFilePath, sizeof(existingFilePath));
  263. existing |= StrEqual(filePath, existingFilePath);
  264. }
  265. if (!existing) {
  266. g_QueuedSongs.Push(CloneHandle(song));
  267. return true;
  268. }
  269. }
  270. return false;
  271. }
  272. public int Native_GetActiveSongCount(Handle hPlugin, int nArgs) {
  273. return g_nMaxActiveSongs;
  274. }