HLMOD.HU Forrás Megtekintés - www.hlmod.hu
  1. /*
  2. *
  3. * Background Sounds
  4. *
  5. * H3avY Ra1n (nikhilgupta345)
  6. *
  7. * Description
  8. * -----------
  9. *
  10. * This plugin enables you to play background music on your server
  11. * during the round. It will start playing at the beginning of the
  12. * round, and stop at when the round ends.
  13.  
  14. * Cvars
  15. * -----
  16. * ba_ambience <0/1> <default:1> - turns plugin on or off
  17. * ba_sound_change <any number greater than 1> <default:1> - how often a new sound is chosen to be played
  18. *
  19. * Changelog
  20. * ---------
  21. *
  22. * September 05, 2011 - v1.0 - Initial Release
  23. * September 06, 2011 - v1.0.1 - Fixed bug with death sounds not playing at the end of the round
  24. *
  25. * Credits
  26. * -------
  27. *
  28. * Vaan123 - Original Plugin Idea
  29. * ConnnorMcLeod - Helped me fix a problem I was having with trim()
  30. *
  31. *
  32. * Plugin Thread: http://forums.alliedmods.net/showthread.php?p=1548443
  33. *
  34. */
  35.  
  36. #include < amxmodx >
  37. #include < amxmisc >
  38. #include < cstrike >
  39.  
  40. #define TASK_LOOPSOUND 1000
  41.  
  42. new const g_szPlugin[] = "Background Sounds";
  43. new const g_szVersion[] = "1.0";
  44. new const g_szAuthor[] = "H3avY Ra1n";
  45.  
  46. new const g_szDefaultSounds[ ][ ] =
  47. {
  48. "music1.mp3",
  49. "music2.mp3"
  50. };
  51.  
  52. new Array:g_aSounds;
  53.  
  54. new bool:g_bFileExists;
  55.  
  56. new g_pPluginOn;
  57. new g_pSoundChange;
  58.  
  59. new g_iRandom;
  60.  
  61. new g_iRoundNumber;
  62.  
  63. public plugin_init()
  64. {
  65. register_plugin(g_szPlugin,g_szVersion,g_szAuthor);
  66.  
  67. register_event( "HLTV", "Event_RoundStart", "a", "1=0", "2=0" );
  68.  
  69. register_logevent( "LogEvent_RoundEnd", 2, "1=Round_End" );
  70.  
  71. g_pPluginOn = register_cvar( "ba_ambience", "1" );
  72. g_pSoundChange = register_cvar( "ba_sound_changee", "1" );
  73. }
  74.  
  75. public plugin_precache()
  76. {
  77. g_aSounds = ArrayCreate( 256 );
  78.  
  79. new szDirectory[ 256 ], szMapName[ 32 ];
  80. get_configsdir( szDirectory, charsmax( szDirectory ) );
  81.  
  82. get_mapname( szMapName, charsmax( szMapName ) );
  83.  
  84. format( szDirectory, charsmax( szDirectory ), "%s/sounds/%s.ini", szDirectory, szMapName );
  85.  
  86.  
  87. g_bFileExists = bool:file_exists( szDirectory );
  88.  
  89. new szPath[ 256 ], bool:bSuccess;
  90.  
  91. if( g_bFileExists )
  92. {
  93. new iFile = fopen( szDirectory, "rt" );
  94.  
  95. new szBuffer[ 256 ];
  96.  
  97. while( !feof( iFile ) )
  98. {
  99. fgets( iFile, szBuffer, charsmax( szBuffer ) );
  100.  
  101. trim( szBuffer );
  102.  
  103. remove_quotes( szBuffer );
  104.  
  105. bSuccess = false;
  106.  
  107. formatex( szPath, charsmax( szPath ), "sound/%s", szBuffer );
  108.  
  109. if( !file_exists( szPath ) )
  110. {
  111. log_amx( "[Hatter Hang] %s nem talalhato a fajl.", szPath );
  112. }
  113.  
  114. else
  115. {
  116. if( contain( szBuffer, ".mp3" ) )
  117. {
  118. precache_generic( szPath );
  119. bSuccess = true;
  120. }
  121.  
  122. else if( contain( szBuffer, ".wav" ) )
  123. {
  124. precache_sound( szBuffer );
  125. bSuccess = true;
  126. }
  127.  
  128. else
  129. {
  130. log_amx( "[Hatter Hang] %s nem talalhato a fajl", szPath );
  131. }
  132. }
  133.  
  134. if( bSuccess )
  135. ArrayPushString( g_aSounds, szBuffer );
  136. }
  137.  
  138. fclose( iFile );
  139. }
  140.  
  141. else
  142. {
  143. for( new i = 0; i < sizeof g_szDefaultSounds; i++ )
  144. {
  145. bSuccess = false;
  146.  
  147. formatex( szPath, charsmax( szPath ), "sound/%s", g_szDefaultSounds[ i ] );
  148.  
  149. if( !file_exists( szPath ) )
  150. {
  151. log_amx( "[Hatter Hang] %s nem letezik a fajl.", szPath );
  152. }
  153.  
  154. else
  155. {
  156. if( contain( g_szDefaultSounds[ i ], ".mp3" ) )
  157. {
  158. precache_generic( szPath );
  159. bSuccess = true;
  160. }
  161.  
  162. else if( contain( g_szDefaultSounds[ i ], ".wav" ) )
  163. {
  164. precache_sound( g_szDefaultSounds[ i ] );
  165. bSuccess = true;
  166. }
  167.  
  168. else
  169. {
  170. log_amx( "[Hatter Hang] %s nem talalhato a zene fajl.", szPath );
  171. }
  172. }
  173.  
  174. if( bSuccess )
  175. ArrayPushString( g_aSounds, g_szDefaultSounds[ i ] );
  176. }
  177. }
  178.  
  179. new iSize = ArraySize( g_aSounds );
  180.  
  181. if( !iSize )
  182. set_fail_state( "No sound files found." );
  183.  
  184. else
  185. g_iRandom = random( iSize );
  186. }
  187.  
  188. public Event_RoundStart()
  189. {
  190. if( !get_pcvar_num( g_pPluginOn ) )
  191. return;
  192.  
  193. if( ++g_iRoundNumber % get_pcvar_num( g_pSoundChange ) == 0 && ArraySize( g_aSounds ) > 1 )
  194. {
  195. new iOldSound = g_iRandom;
  196.  
  197. while( g_iRandom == iOldSound )
  198. g_iRandom = random( ArraySize( g_aSounds ) );
  199. }
  200.  
  201. new szBuffer[ 256 ];
  202. ArrayGetString( g_aSounds, g_iRandom, szBuffer, charsmax( szBuffer ) );
  203.  
  204. if( contain( szBuffer, ".mp3" ) != -1 )
  205. {
  206. client_cmd( 0, "mp3 loop ^"sound/%s^"", szBuffer );
  207. }
  208.  
  209. else if( contain( szBuffer, ".wav" ) != -1 )
  210. {
  211. client_cmd( 0, "stopsound" );
  212.  
  213. new szPath[ 256 ];
  214. formatex( szPath, charsmax( szPath ), "sound/%s", szBuffer );
  215.  
  216. client_cmd( 0, "spk ^"%s^"", szBuffer );
  217.  
  218. set_task( GetWavDuration( szPath ), "Task_LoopSound", TASK_LOOPSOUND, szBuffer, charsmax( szBuffer ), .flags="b" );
  219. }
  220. }
  221.  
  222. public LogEvent_RoundEnd()
  223. {
  224. set_task( 2.0, "Task_EndSound" );
  225.  
  226. remove_task( TASK_LOOPSOUND );
  227. }
  228.  
  229. public Task_EndSound()
  230. {
  231. client_cmd( 0, "stopsound" );
  232. client_cmd( 0, "mp3 stop" );
  233. }
  234.  
  235. public Task_LoopSound( szSound[ ], iTaskID )
  236. {
  237. client_cmd( 0, "stopsound" );
  238. client_cmd( 0, "spk ^"%s^"", szSound );
  239. }
  240.  
  241. // Provided by Arkshine
  242. Float:GetWavDuration( const WavFile[] )
  243. {
  244. new Frequence [ 4 ];
  245. new Bitrate [ 2 ];
  246. new DataLength[ 4 ];
  247. new File;
  248.  
  249. // --| Open the file.
  250. File = fopen( WavFile, "rb" );
  251.  
  252. // --| Get the frequence from offset 24. ( Read 4 bytes )
  253. fseek( File, 24, SEEK_SET );
  254. fread_blocks( File, Frequence, 4, BLOCK_INT );
  255.  
  256. // --| Get the bitrate from offset 34. ( read 2 bytes )
  257. fseek( File, 34, SEEK_SET );
  258. fread_blocks( File, Bitrate, 2, BLOCK_BYTE );
  259.  
  260. // --| Search 'data'. If the 'd' not on the offset 40, we search it.
  261. if ( fgetc( File ) != 'd' ) while( fgetc( File ) != 'd' && !feof( File ) ) {}
  262.  
  263. // --| Get the data length from offset 44. ( after 'data', read 4 bytes )
  264. fseek( File, 3, SEEK_CUR );
  265. fread_blocks( File, DataLength, 4, BLOCK_INT );
  266.  
  267. // --| Close file.
  268. fclose( File );
  269.  
  270. // --| Calculate the time. ( Data length / ( frequence * bitrate ) / 8 ).
  271. return float( DataLength[ 0 ] ) / ( float( Frequence[ 0 ] * Bitrate[ 0 ] ) / 8.0 );
  272. }