round_end_music.sp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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.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, g_ConVarSongDelay;
  22. Handle g_RequestSongForward;
  23. Handle g_OnREMPlayedForward, g_OnREMPostPlayedForward;
  24. int g_nMaxActiveSongs = 5;
  25. bool g_bRoundEndMusicActive;
  26. public void OnPluginStart() {
  27. g_QueuedSongs = new ArrayList();
  28. g_ActiveSongs = new ArrayList();
  29. g_PlayedSongs = new ArrayList();
  30. g_RequestSongForward = CreateForward(ET_Ignore);
  31. g_OnREMPlayedForward = CreateGlobalForward("OnRoundEndMusicWillPlay", ET_Event, Param_Cell);
  32. g_OnREMPostPlayedForward = CreateGlobalForward("OnRoundEndMusicPlayed", ET_Ignore,
  33. Param_Cell);
  34. HookEvent("teamplay_round_win", OnRoundEnd, EventHookMode_PostNoCopy);
  35. RegAdminCmd("sm_playsong", AdminCmd_PlaySong, ADMFLAG_ROOT);
  36. RegAdminCmd("sm_peekqueue", AdminCmd_PeekQueue, ADMFLAG_ROOT);
  37. g_ConVarMaxActiveSongs = CreateConVar("sm_rem_active_songs", "5",
  38. "Maximum number of songs available for playback on a single map.", _,
  39. true, 1.0, false);
  40. g_ConVarEnabled = CreateConVar("sm_rem_enabled", "1", "Enables Round End Music.", _,
  41. true, 0.0, true, 1.0);
  42. g_ConVarSongDelay = CreateConVar("sm_rem_song_delay", "4.3",
  43. "Amount of time after the round end to wait before playing the song.", _,
  44. true, 0.0);
  45. AutoExecConfig();
  46. }
  47. public Action AdminCmd_PlaySong(int client, int argc) {
  48. PlayRoundEndMusic();
  49. return Plugin_Handled;
  50. }
  51. public Action AdminCmd_PeekQueue(int client, int argc) {
  52. if (GetCmdReplySource() == SM_REPLY_TO_CHAT) {
  53. ReplyToCommand(client, "See console for output.");
  54. }
  55. PrintSongList(client, "played songs", g_PlayedSongs);
  56. PrintSongList(client, "active songs", g_ActiveSongs);
  57. PrintSongList(client, "queued songs", g_QueuedSongs);
  58. return Plugin_Handled;
  59. }
  60. void PrintSongList(int client, const char[] listName, ArrayList songList) {
  61. char filePath[PLATFORM_MAX_PATH];
  62. if (songList.Length > 0) {
  63. PrintToConsole(client, "---- %s ----", listName);
  64. for (int i = 0; i < songList.Length; i++) {
  65. MusicEntry song = songList.Get(i);
  66. song.GetFilePath(filePath, sizeof(filePath));
  67. PrintToConsole(client, "%d. %s", i + 1, filePath);
  68. }
  69. }
  70. }
  71. public void OnConfigsExecuted() {
  72. // TODO pull g_nMaxActiveSongs from ConVar -- it can't change during map
  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. g_ActiveSongs.Erase(0);
  192. FireOnREMPlayedPostEvent(song);
  193. }
  194. }
  195. Action FireOnRoundEndMusicPlayedEvent(MusicEntry song) {
  196. Action result;
  197. Call_StartForward(g_OnREMPlayedForward);
  198. Call_PushCell(song);
  199. Call_Finish(result);
  200. return result;
  201. }
  202. void FireOnREMPlayedPostEvent(MusicEntry song) {
  203. Call_StartForward(g_OnREMPostPlayedForward);
  204. Call_PushCell(song);
  205. Call_Finish();
  206. }
  207. public void OnRoundEnd(Event event, const char[] name, bool dontBroadcast) {
  208. CreateTimer(g_ConVarSongDelay.FloatValue, RoundEndMusicPlaybackDelay);
  209. }
  210. public Action RoundEndMusicPlaybackDelay(Handle timer, any data) {
  211. PlayRoundEndMusic();
  212. }
  213. // menu: read entries from g_PlayedSongs and then g_ActiveSongs if not empty
  214. // that *should* maintain initial play order
  215. // maybe we should just provide a function that provides the entire list and active counts?
  216. /* Native function calls */
  217. public APLRes AskPluginLoad2(Handle self, bool late, char[] error, int err_max) {
  218. RegPluginLibrary("round-end-music");
  219. CreateNative("REM_RegisterSource", Native_RegisterSource);
  220. CreateNative("REM_AddSong", Native_AddSong);
  221. CreateNative("REM_GetActiveSongCount", Native_GetActiveSongCount);
  222. }
  223. public int Native_RegisterSource(Handle hPlugin, int nArgs) {
  224. Function callback = GetNativeFunction(1);
  225. // preemptive removal because it could be forwarded multiple times?
  226. for (int i = 0; i < GetForwardFunctionCount(g_RequestSongForward); i++) {
  227. RemoveFromForward(g_RequestSongForward, hPlugin, callback);
  228. }
  229. AddToForward(g_RequestSongForward, hPlugin, callback);
  230. return 1;
  231. }
  232. public int Native_AddSong(Handle hPlugin, int nArgs) {
  233. if (g_bQueueLocked) {
  234. ThrowNativeError(1, "Queue is currently locked -- are you calling REM_AddSong outside "
  235. ... "of the registered callback?");
  236. return false;
  237. }
  238. MusicEntry song = view_as<MusicEntry>(GetNativeCell(1));
  239. // Ensure no more songs added than necessary
  240. if (g_QueuedSongs.Length < g_nMaxActiveSongs) {
  241. // Ensure no duplicates by file path in queue or active songs
  242. bool existing = false;
  243. char filePath[PLATFORM_MAX_PATH], existingFilePath[PLATFORM_MAX_PATH];
  244. song.GetFilePath(filePath, sizeof(filePath));
  245. for (int i = 0; i < g_QueuedSongs.Length; i++) {
  246. (view_as<MusicEntry>(g_QueuedSongs.Get(i))).GetFilePath(
  247. existingFilePath, sizeof(existingFilePath));
  248. existing |= StrEqual(filePath, existingFilePath);
  249. }
  250. for (int i = 0; i < g_ActiveSongs.Length; i++) {
  251. (view_as<MusicEntry>(g_ActiveSongs.Get(i)))
  252. .GetFilePath(existingFilePath, sizeof(existingFilePath));
  253. existing |= StrEqual(filePath, existingFilePath);
  254. }
  255. if (!existing) {
  256. g_QueuedSongs.Push(CloneHandle(song));
  257. return true;
  258. }
  259. }
  260. return false;
  261. }
  262. public int Native_GetActiveSongCount(Handle hPlugin, int nArgs) {
  263. return g_nMaxActiveSongs;
  264. }