SoundEnvironment.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program 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. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/Combat/SoundEnvironment.cpp $*
  25. * *
  26. * Author:: Ian Leslie *
  27. * *
  28. * $Modtime:: 5/21/01 2:16p $*
  29. * *
  30. * $Revision:: 7 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. // Includes.
  36. #include "soundenvironment.h"
  37. #include "camera.h"
  38. #include "combat.h"
  39. #include "gameobjmanager.h"
  40. #include "phys.h"
  41. #include "physcoltest.h"
  42. #include "pscene.h"
  43. /***********************************************************************************************
  44. * SoundEnvironmentClass::SoundEnvironmentClass -- *
  45. * *
  46. * INPUT: *
  47. * *
  48. * OUTPUT: *
  49. * *
  50. * WARNINGS: *
  51. * *
  52. * HISTORY: *
  53. * 04/16/01 IML : Created. *
  54. *=============================================================================================*/
  55. SoundEnvironmentClass::SoundEnvironmentClass()
  56. : UserCount (0)
  57. {
  58. // Initialize the amplitude mixing buffer.
  59. AmplitudeBuffer = new float [AMPLITUDE_BUFFER_SIZE];
  60. WWASSERT (AmplitudeBuffer != NULL);
  61. Reset();
  62. }
  63. /***********************************************************************************************
  64. * SoundEnvironmentClass::Reset -- *
  65. * *
  66. * INPUT: *
  67. * *
  68. * OUTPUT: *
  69. * *
  70. * WARNINGS: *
  71. * *
  72. * HISTORY: *
  73. * 04/16/01 IML : Created. *
  74. *=============================================================================================*/
  75. void SoundEnvironmentClass::Reset()
  76. {
  77. // Clear the amplitude buffer.
  78. AmplitudeIndex = 0;
  79. AmplitudeSum = 0.0f;
  80. for (unsigned i = 0; i < AMPLITUDE_BUFFER_SIZE; i++) {
  81. AmplitudeBuffer [i] = 0.0f;
  82. }
  83. }
  84. /***********************************************************************************************
  85. * SoundEnvironmentClass::~SoundEnvironmentClass -- *
  86. * *
  87. * INPUT: *
  88. * *
  89. * OUTPUT: *
  90. * *
  91. * WARNINGS: *
  92. * *
  93. * HISTORY: *
  94. * 04/20/01 PDS : Created. *
  95. *=============================================================================================*/
  96. SoundEnvironmentClass::~SoundEnvironmentClass()
  97. {
  98. delete [] AmplitudeBuffer;
  99. }
  100. /***********************************************************************************************
  101. * SoundEnvironmentClass::Update -- *
  102. * *
  103. * INPUT: *
  104. * *
  105. * OUTPUT: *
  106. * *
  107. * WARNINGS: *
  108. * *
  109. * HISTORY: *
  110. * 04/16/01 IML : Created. *
  111. *=============================================================================================*/
  112. void SoundEnvironmentClass::Update (PhysicsSceneClass *scene, CameraClass *camera)
  113. {
  114. // Optimization: only update if this object is being used.
  115. if (UserCount > 0) {
  116. Vector3 cameraposition;
  117. Vector3 scenemin, scenemax;
  118. float amplitude;
  119. cameraposition = camera->Get_Position();
  120. // Get the highest point in the scene.
  121. scene->Get_Level_Extents (scenemin, scenemax);
  122. // Expand the bounding box by a small amount so that we can distinguish between collisions with geometry and
  123. // collisions with the bounding box.
  124. scenemin.Z -= 1.0f;
  125. scenemax.Z += 1.0f;
  126. // Cast a ray from the camera position upwards.
  127. {
  128. Vector3 raycastendpoint (cameraposition.X, cameraposition.Y, scenemax.Z);
  129. LineSegClass raycast (cameraposition, raycastendpoint);
  130. CastResultStruct rayresult;
  131. PhysRayCollisionTestClass raytest (raycast, &rayresult, TERRAIN_ONLY_COLLISION_GROUP, COLLISION_TYPE_PROJECTILE);
  132. scene->Cast_Ray (raytest);
  133. // Did the ray hit an object?
  134. if (raytest.Result->Fraction < 1.0f) {
  135. // Is the camera in an environment zone (in which case environmental sounds should not be heard)?
  136. if (GameObjManager::Is_In_Environment_Zone (cameraposition)) {
  137. amplitude = 0.0f;
  138. } else {
  139. amplitude = 0.5f;
  140. }
  141. } else {
  142. amplitude = 1.0f;
  143. }
  144. }
  145. AmplitudeSum -= AmplitudeBuffer [AmplitudeIndex];
  146. AmplitudeBuffer [AmplitudeIndex] = amplitude;
  147. AmplitudeSum += amplitude;
  148. AmplitudeIndex++;
  149. if (AmplitudeIndex >= AMPLITUDE_BUFFER_SIZE) AmplitudeIndex = 0;
  150. }
  151. }