round_end_music.sp 9.8 KB

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