Sound.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "Game_local.h"
  23. /*
  24. ===============================================================================
  25. SOUND
  26. ===============================================================================
  27. */
  28. const idEventDef EV_Speaker_On( "On", NULL );
  29. const idEventDef EV_Speaker_Off( "Off", NULL );
  30. const idEventDef EV_Speaker_Timer( "<timer>", NULL );
  31. CLASS_DECLARATION( idEntity, idSound )
  32. EVENT( EV_Activate, idSound::Event_Trigger )
  33. EVENT( EV_Speaker_On, idSound::Event_On )
  34. EVENT( EV_Speaker_Off, idSound::Event_Off )
  35. EVENT( EV_Speaker_Timer, idSound::Event_Timer )
  36. END_CLASS
  37. /*
  38. ================
  39. idSound::idSound
  40. ================
  41. */
  42. idSound::idSound( void ) {
  43. lastSoundVol = 0.0f;
  44. soundVol = 0.0f;
  45. shakeTranslate.Zero();
  46. shakeRotate.Zero();
  47. random = 0.0f;
  48. wait = 0.0f;
  49. timerOn = false;
  50. playingUntilTime = 0;
  51. }
  52. /*
  53. ================
  54. idSound::Save
  55. ================
  56. */
  57. void idSound::Save( idSaveGame *savefile ) const {
  58. savefile->WriteFloat( lastSoundVol );
  59. savefile->WriteFloat( soundVol );
  60. savefile->WriteFloat( random );
  61. savefile->WriteFloat( wait );
  62. savefile->WriteBool( timerOn );
  63. savefile->WriteVec3( shakeTranslate );
  64. savefile->WriteAngles( shakeRotate );
  65. savefile->WriteInt( playingUntilTime );
  66. }
  67. /*
  68. ================
  69. idSound::Restore
  70. ================
  71. */
  72. void idSound::Restore( idRestoreGame *savefile ) {
  73. savefile->ReadFloat( lastSoundVol );
  74. savefile->ReadFloat( soundVol );
  75. savefile->ReadFloat( random );
  76. savefile->ReadFloat( wait );
  77. savefile->ReadBool( timerOn );
  78. savefile->ReadVec3( shakeTranslate );
  79. savefile->ReadAngles( shakeRotate );
  80. savefile->ReadInt( playingUntilTime );
  81. }
  82. /*
  83. ================
  84. idSound::Spawn
  85. ================
  86. */
  87. void idSound::Spawn( void ) {
  88. spawnArgs.GetVector( "move", "0 0 0", shakeTranslate );
  89. spawnArgs.GetAngles( "rotate", "0 0 0", shakeRotate );
  90. spawnArgs.GetFloat( "random", "0", random );
  91. spawnArgs.GetFloat( "wait", "0", wait );
  92. if ( ( wait > 0.0f ) && ( random >= wait ) ) {
  93. random = wait - 0.001;
  94. gameLocal.Warning( "speaker '%s' at (%s) has random >= wait", name.c_str(), GetPhysics()->GetOrigin().ToString(0) );
  95. }
  96. soundVol = 0.0f;
  97. lastSoundVol = 0.0f;
  98. if ( ( shakeRotate != ang_zero ) || ( shakeTranslate != vec3_zero ) ) {
  99. BecomeActive( TH_THINK );
  100. }
  101. if ( !refSound.waitfortrigger && ( wait > 0.0f ) ) {
  102. timerOn = true;
  103. PostEventSec( &EV_Speaker_Timer, wait + gameLocal.random.CRandomFloat() * random );
  104. } else {
  105. timerOn = false;
  106. }
  107. }
  108. /*
  109. ================
  110. idSound::Event_Trigger
  111. this will toggle the idle idSound on and off
  112. ================
  113. */
  114. void idSound::Event_Trigger( idEntity *activator ) {
  115. if ( wait > 0.0f ) {
  116. if ( timerOn ) {
  117. timerOn = false;
  118. CancelEvents( &EV_Speaker_Timer );
  119. } else {
  120. timerOn = true;
  121. DoSound( true );
  122. PostEventSec( &EV_Speaker_Timer, wait + gameLocal.random.CRandomFloat() * random );
  123. }
  124. } else {
  125. if ( gameLocal.isMultiplayer ) {
  126. if ( refSound.referenceSound && ( gameLocal.time < playingUntilTime ) ) {
  127. DoSound( false );
  128. } else {
  129. DoSound( true );
  130. }
  131. } else {
  132. if ( refSound.referenceSound && refSound.referenceSound->CurrentlyPlaying() ) {
  133. DoSound( false );
  134. } else {
  135. DoSound( true );
  136. }
  137. }
  138. }
  139. }
  140. /*
  141. ================
  142. idSound::Event_Timer
  143. ================
  144. */
  145. void idSound::Event_Timer( void ) {
  146. DoSound( true );
  147. PostEventSec( &EV_Speaker_Timer, wait + gameLocal.random.CRandomFloat() * random );
  148. }
  149. /*
  150. ================
  151. idSound::Think
  152. ================
  153. */
  154. void idSound::Think( void ) {
  155. idAngles ang;
  156. // run physics
  157. RunPhysics();
  158. // clear out our update visuals think flag since we never call Present
  159. BecomeInactive( TH_UPDATEVISUALS );
  160. }
  161. /*
  162. ===============
  163. idSound::UpdateChangableSpawnArgs
  164. ===============
  165. */
  166. void idSound::UpdateChangeableSpawnArgs( const idDict *source ) {
  167. idEntity::UpdateChangeableSpawnArgs( source );
  168. if ( source ) {
  169. FreeSoundEmitter( true );
  170. spawnArgs.Copy( *source );
  171. idSoundEmitter *saveRef = refSound.referenceSound;
  172. gameEdit->ParseSpawnArgsToRefSound( &spawnArgs, &refSound );
  173. refSound.referenceSound = saveRef;
  174. idVec3 origin;
  175. idMat3 axis;
  176. if ( GetPhysicsToSoundTransform( origin, axis ) ) {
  177. refSound.origin = GetPhysics()->GetOrigin() + origin * axis;
  178. } else {
  179. refSound.origin = GetPhysics()->GetOrigin();
  180. }
  181. spawnArgs.GetFloat( "random", "0", random );
  182. spawnArgs.GetFloat( "wait", "0", wait );
  183. if ( ( wait > 0.0f ) && ( random >= wait ) ) {
  184. random = wait - 0.001;
  185. gameLocal.Warning( "speaker '%s' at (%s) has random >= wait", name.c_str(), GetPhysics()->GetOrigin().ToString(0) );
  186. }
  187. if ( !refSound.waitfortrigger && ( wait > 0.0f ) ) {
  188. timerOn = true;
  189. DoSound( false );
  190. CancelEvents( &EV_Speaker_Timer );
  191. PostEventSec( &EV_Speaker_Timer, wait + gameLocal.random.CRandomFloat() * random );
  192. } else if ( !refSound.waitfortrigger && !(refSound.referenceSound && refSound.referenceSound->CurrentlyPlaying() ) ) {
  193. // start it if it isn't already playing, and we aren't waitForTrigger
  194. DoSound( true );
  195. timerOn = false;
  196. }
  197. }
  198. }
  199. /*
  200. ===============
  201. idSound::SetSound
  202. ===============
  203. */
  204. void idSound::SetSound( const char *sound, int channel ) {
  205. const idSoundShader *shader = declManager->FindSound( sound );
  206. if ( shader != refSound.shader ) {
  207. FreeSoundEmitter( true );
  208. }
  209. gameEdit->ParseSpawnArgsToRefSound(&spawnArgs, &refSound);
  210. refSound.shader = shader;
  211. // start it if it isn't already playing, and we aren't waitForTrigger
  212. if ( !refSound.waitfortrigger && !(refSound.referenceSound && refSound.referenceSound->CurrentlyPlaying() ) ) {
  213. DoSound( true );
  214. }
  215. }
  216. /*
  217. ================
  218. idSound::DoSound
  219. ================
  220. */
  221. void idSound::DoSound( bool play ) {
  222. if ( play ) {
  223. StartSoundShader( refSound.shader, SND_CHANNEL_ANY, refSound.parms.soundShaderFlags, true, &playingUntilTime );
  224. playingUntilTime += gameLocal.time;
  225. } else {
  226. StopSound( SND_CHANNEL_ANY, true );
  227. playingUntilTime = 0;
  228. }
  229. }
  230. /*
  231. ================
  232. idSound::Event_On
  233. ================
  234. */
  235. void idSound::Event_On( void ) {
  236. if ( wait > 0.0f ) {
  237. timerOn = true;
  238. PostEventSec( &EV_Speaker_Timer, wait + gameLocal.random.CRandomFloat() * random );
  239. }
  240. DoSound( true );
  241. }
  242. /*
  243. ================
  244. idSound::Event_Off
  245. ================
  246. */
  247. void idSound::Event_Off( void ) {
  248. if ( timerOn ) {
  249. timerOn = false;
  250. CancelEvents( &EV_Speaker_Timer );
  251. }
  252. DoSound( false );
  253. }
  254. /*
  255. ===============
  256. idSound::ShowEditingDialog
  257. ===============
  258. */
  259. void idSound::ShowEditingDialog( void ) {
  260. common->InitTool( EDITOR_SOUND, &spawnArgs );
  261. }