round_end_music.sp 9.3 KB

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